blob: b637bd6c7add0bf911618177661a8ed0e1c84337 [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.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000054 drawState->addCoverageEffect(
55 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
robertphillips@google.comf294b772012-04-27 14:29:26 +0000110////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000111// sort out what kind of clip mask needs to be created: alpha, stencil,
112// scissor, or entirely software
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000113bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000114 GrDrawState::AutoRestoreEffects* are,
115 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000116 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000117
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000118 ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000119 int32_t genID;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000120 InitialState initialState;
121 SkIRect clipSpaceIBounds;
122 bool requiresAA;
123 bool isRect = false;
124
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000125 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000126
127 const GrRenderTarget* rt = drawState->getRenderTarget();
128 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000129 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000130
131 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
132
133 if (!ignoreClip) {
134 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
135 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
136 ReduceClipStack(*clipDataIn->fClipStack,
137 clipSpaceRTIBounds,
138 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000139 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000140 &initialState,
141 &clipSpaceIBounds,
142 &requiresAA);
143 if (elements.isEmpty()) {
144 if (kAllIn_InitialState == initialState) {
145 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
146 isRect = true;
147 } else {
148 return false;
149 }
150 }
151 }
152
153 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000154 fGpu->disableScissor();
155 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000156 return true;
157 }
158
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000159 // If there is only one clip element we check whether the draw's bounds are contained
160 // fully within the clip. If not, we install an effect that handles the clip for some
161 // cases.
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000162 if (1 == elements.count() && SkRegion::kReplace_Op == elements.tail()->getOp()) {
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000163 if (NULL != devBounds) {
164 SkRect boundsInClipSpace = *devBounds;
165 boundsInClipSpace.offset(SkIntToScalar(clipDataIn->fOrigin.fX),
166 SkIntToScalar(clipDataIn->fOrigin.fY));
167 if (elements.tail()->contains(boundsInClipSpace)) {
168 fGpu->disableScissor();
169 this->setGpuStencil();
170 return true;
171 }
172 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000173 Element::Type type = elements.tail()->getType();
174 bool isAA = GR_AA_CLIP && elements.tail()->isAA();
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000175 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000176 if (SkClipStack::Element::kPath_Type == type) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000177 const SkPath& path = elements.tail()->getPath();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000178 if (rt->isMultisampled()) {
179 // A coverage effect for AA clipping won't play nicely with MSAA.
180 if (!isAA) {
181 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
182 SkIntToScalar(-clipDataIn->fOrigin.fY) };
183 effect.reset(GrConvexPolyEffect::Create(GrConvexPolyEffect::kFillNoAA_EdgeType,
184 path, &offset));
185 }
186 } else {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000187 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
188 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000189 GrConvexPolyEffect::EdgeType type = isAA ? GrConvexPolyEffect::kFillAA_EdgeType :
190 GrConvexPolyEffect::kFillNoAA_EdgeType;
191 effect.reset(GrConvexPolyEffect::Create(type, path, &offset));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000192 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000193 } else if (isAA && SkClipStack::Element::kRRect_Type == type && !rt->isMultisampled()) {
194 const SkRRect& rrect = elements.tail()->getRRect();
195 effect.reset(GrRRectEffect::Create(rrect));
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000196 } else if (isAA && SkClipStack::Element::kRect_Type == type && !rt->isMultisampled()) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000197 // We only handle AA/non-MSAA rects here. Coverage effect AA isn't MSAA friendly and
198 // non-AA rect clips are handled by the scissor.
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000199 SkRect rect = elements.tail()->getRect();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000200 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000201 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000202 rect.offset(offset);
203 effect.reset(GrConvexPolyEffect::CreateForAAFillRect(rect));
204 // This should never fail.
205 SkASSERT(effect);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000206 }
207 if (effect) {
208 are->set(fGpu->drawState());
209 fGpu->drawState()->addCoverageEffect(effect);
commit-bot@chromium.org6516d4b2014-02-07 14:04:48 +0000210 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
211 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
212 fGpu->enableScissor(scissorSpaceIBounds);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000213 this->setGpuStencil();
214 return true;
215 }
216 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000217
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000218#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000219 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000220 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000221 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000222
223 if (this->useSWOnlyPath(elements)) {
224 // The clip geometry is complex enough that it will be more efficient to create it
225 // entirely in software
226 result = this->createSoftwareClipMask(genID,
227 initialState,
228 elements,
229 clipSpaceIBounds);
230 } else {
231 result = this->createAlphaClipMask(genID,
232 initialState,
233 elements,
234 clipSpaceIBounds);
235 }
236
237 if (NULL != result) {
238 // The mask's top left coord should be pinned to the rounded-out top left corner of
239 // clipSpace bounds. We determine the mask's position WRT to the render target here.
240 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
241 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000242 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000243 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000244 fGpu->disableScissor();
245 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000246 return true;
247 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000248 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000249 }
250#endif // GR_AA_CLIP
251
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000252 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
253 // be created. In either case, free up the texture in the anti-aliased mask cache.
254 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
255 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
256 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000257 fAACache.reset();
258
bsalomon@google.coma3201942012-06-21 19:58:20 +0000259 // If the clip is a rectangle then just set the scissor. Otherwise, create
260 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000261 if (isRect) {
262 SkIRect clipRect = clipSpaceIBounds;
263 clipRect.offset(-clipDataIn->fOrigin);
264 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000265 this->setGpuStencil();
266 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000267 }
268
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000269 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000270 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000271 this->createStencilClipMask(genID,
272 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000273 elements,
274 clipSpaceIBounds,
275 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000276
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000277 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
278 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
279 // use both stencil and scissor test to the bounds for the final draw.
280 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
281 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
282 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000283 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000284 return true;
285}
286
287#define VISUALIZE_COMPLEX_CLIP 0
288
289#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000290 #include "SkRandom.h"
291 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000292 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
293#else
294 #define SET_RANDOM_COLOR
295#endif
296
297namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000298
299////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000300// set up the OpenGL blend function to perform the specified
301// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000302void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000303
304 switch (op) {
305 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000306 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000307 break;
308 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000309 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000310 break;
311 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000312 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000313 break;
314 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000315 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000316 break;
317 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000318 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000319 break;
320 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000321 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000322 break;
323 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000324 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000325 break;
326 }
327}
328
robertphillips@google.com72176b22012-05-23 13:19:12 +0000329}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000330
331////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000332bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000333 const SkClipStack::Element* element,
334 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000335 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000336
337 drawState->setRenderTarget(target->asRenderTarget());
338
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000339 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000340 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000341 case Element::kEmpty_Type:
342 SkDEBUGFAIL("Should never get here with an empty element.");
343 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000344 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000345 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
346 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000347 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000348 getContext()->getAARectRenderer()->fillAARect(fGpu,
349 fGpu,
350 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000351 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000352 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000353 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000354 } else {
355 fGpu->drawSimpleRect(element->getRect(), NULL);
356 }
357 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000358 default: {
359 SkPath path;
360 element->asPath(&path);
361 if (path.isInverseFillType()) {
362 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000363 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000364 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000365 if (NULL == pr) {
366 GrPathRendererChain::DrawType type;
367 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
368 GrPathRendererChain::kColor_DrawType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000369 pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000370 }
371 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000372 return false;
373 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000374 pr->drawPath(path, stroke, fGpu, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000375 break;
376 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 }
378 return true;
379}
380
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000381bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
382 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000383 GrPathRenderer** pr) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000384 GrDrawState* drawState = fGpu->drawState();
385 drawState->setRenderTarget(target->asRenderTarget());
386
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000387 if (Element::kRect_Type == element->getType()) {
388 return true;
389 } else {
390 // We shouldn't get here with an empty clip element.
391 SkASSERT(Element::kEmpty_Type != element->getType());
392 SkPath path;
393 element->asPath(&path);
394 if (path.isInverseFillType()) {
395 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000396 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000397 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
398 GrPathRendererChain::DrawType type = element->isAA() ?
399 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
400 GrPathRendererChain::kStencilAndColor_DrawType;
401 *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
402 return NULL != *pr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000403 }
404}
405
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000406void GrClipMaskManager::mergeMask(GrTexture* dstMask,
407 GrTexture* srcMask,
408 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000409 const SkIRect& dstBound,
410 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000411 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000412 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000413 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000414 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000415
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000416 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000417
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000418 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000419
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000420 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000421 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000422
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000423 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000424 GrTextureDomainEffect::Create(srcMask,
425 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000426 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
427 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000428 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000429 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000430}
431
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000432// get a texture to act as a temporary buffer for AA clip boolean operations
433// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000434void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000435 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000436 // we've already allocated the temp texture
437 return;
438 }
439
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000440 GrTextureDesc desc;
441 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000442 desc.fWidth = width;
443 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000444 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000445
robertphillips@google.com2c756812012-05-22 20:28:23 +0000446 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000447}
448
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000449////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000450// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
451// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
452// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000453bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000454 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000455 GrTexture** result,
456 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000457 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000458 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000459
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000460 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
461 // Since we are setting up the cache we know the last lookup was a miss. Free up the
462 // currently cached mask so it can be reused.
463 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000465 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000466 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 desc.fWidth = clipSpaceIBounds.width();
468 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000469 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000470 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000471 // We would always like A8 but it isn't supported on all platforms
472 desc.fConfig = kAlpha_8_GrPixelConfig;
473 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000474
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000475 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000476 }
477
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000478 *result = fAACache.getLastMask();
479 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000480}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000481
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000482////////////////////////////////////////////////////////////////////////////////
483// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000484GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000485 InitialState initialState,
486 const ElementList& elements,
487 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000488 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000489
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000490 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000491 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000492 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000493 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000494 }
495
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000496 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000497 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000498 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000499 }
500
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000501 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000502 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000503 SkIntToScalar(-clipSpaceIBounds.fLeft),
504 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000505 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000506 // The texture may be larger than necessary, this rect represents the part of the texture
507 // we populate with a rasterization of the clip.
508 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
509
bsalomon@google.com137f1342013-05-29 21:27:53 +0000510 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
511 SkMatrix translate;
512 translate.setTranslate(clipToMaskOffset);
513 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
514
515 GrDrawState* drawState = fGpu->drawState();
516
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000517 // We're drawing a coverage mask and want coverage to be run through the blend function.
518 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
519
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000520 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
521 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000522 fGpu->clear(&maskSpaceIBounds,
523 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000524 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000525 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000526
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000527 // When we use the stencil in the below loop it is important to have this clip installed.
528 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
529 // pass must not set values outside of this bounds or stencil values outside the rect won't be
530 // cleared.
531 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
532 drawState->enableState(GrDrawState::kClip_StateBit);
533
robertphillips@google.comf105b102012-05-14 12:18:26 +0000534 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000535 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000536 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000537 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000538 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000539 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000540
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000541 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000542 GrPathRenderer* pr = NULL;
543 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000544 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000545 // 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 +0000546 // mask buffer can be substantially larger than the actually clip stack element. We
547 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000548 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000549 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000550
551 if (useTemp) {
552 if (invert) {
553 maskSpaceElementIBounds = maskSpaceIBounds;
554 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000555 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000556 elementBounds.offset(clipToMaskOffset);
557 elementBounds.roundOut(&maskSpaceElementIBounds);
558 }
559
560 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
561 if (NULL == temp.texture()) {
562 fAACache.reset();
563 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000564 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000565 dst = temp.texture();
566 // clear the temp target and set blend to replace
567 fGpu->clear(&maskSpaceElementIBounds,
568 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000569 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000570 dst->asRenderTarget());
571 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000572
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000573 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000574 // draw directly into the result with the stencil set to make the pixels affected
575 // by the clip shape be non-zero.
576 dst = result;
577 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
578 kReplace_StencilOp,
579 kReplace_StencilOp,
580 kAlways_StencilFunc,
581 0xffff,
582 0xffff,
583 0xffff);
584 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000585 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000586 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000587
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000588 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000589
robertphillips@google.come79f3202014-02-11 16:30:21 +0000590 if (!this->drawElement(dst, element, pr)) {
591 fAACache.reset();
592 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000593 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000595 if (useTemp) {
596 // Now draw into the accumulator using the real operation and the temp buffer as a
597 // texture
598 this->mergeMask(result,
599 temp.texture(),
600 op,
601 maskSpaceIBounds,
602 maskSpaceElementIBounds);
603 } else {
604 // Draw to the exterior pixels (those with a zero stencil value).
605 drawState->setAlpha(invert ? 0xff : 0x00);
606 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
607 kZero_StencilOp,
608 kZero_StencilOp,
609 kEqual_StencilFunc,
610 0xffff,
611 0x0000,
612 0xffff);
613 drawState->setStencil(kDrawOutsideElement);
614 fGpu->drawSimpleRect(clipSpaceIBounds);
615 drawState->disableStencil();
616 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000617 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000618 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000619 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000620 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000621 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000622 }
623 }
624
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000625 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000626 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000627}
628
629////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000630// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000631// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000632bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
633 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000634 const ElementList& elements,
635 const SkIRect& clipSpaceIBounds,
636 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000637
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000638 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000640 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000641 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000642
643 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000644 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000645
646 // TODO: dynamically attach a SB when needed.
647 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
648 if (NULL == stencilBuffer) {
649 return false;
650 }
651
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000652 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000653
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000654 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000655
bsalomon@google.com137f1342013-05-29 21:27:53 +0000656 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
657 SkVector translate = {
658 SkIntToScalar(clipSpaceToStencilOffset.fX),
659 SkIntToScalar(clipSpaceToStencilOffset.fY)
660 };
661 SkMatrix matrix;
662 matrix.setTranslate(translate);
663 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000664 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000665
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000666 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667
bsalomon@google.com9f131742012-12-13 20:43:56 +0000668 // We set the current clip to the bounds so that our recursive draws are scissored to them.
669 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
670 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
671 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
672 drawState->enableState(GrDrawState::kClip_StateBit);
673
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000674#if !VISUALIZE_COMPLEX_CLIP
675 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
676#endif
677
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000678 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000679 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000680 clipBit = (1 << (clipBit-1));
681
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000682 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000683
684 // walk through each clip element and perform its set op
685 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000686 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
687 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000688 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 // enabled at bottom of loop
690 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000691 // if the target is MSAA then we want MSAA enabled when the clip is soft
692 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000693 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000694 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000695
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000696 // This will be used to determine whether the clip shape can be rendered into the
697 // stencil with arbitrary stencil settings.
698 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000700 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000701
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000702 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000703
robertphillips@google.come79f3202014-02-11 16:30:21 +0000704 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000705 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000706 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000707 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000709 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000710 element->asPath(&clipPath);
711 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000712 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000713 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000714 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000715 pr = this->getContext()->getPathRenderer(clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000716 stroke,
717 fGpu,
718 false,
719 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000720 &stencilSupport);
721 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722 return false;
723 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724 }
725
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 int passes;
727 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
728
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000729 bool canRenderDirectToStencil =
730 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000731 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000732 // fill rule, and set operation can
733 // we render the element directly to
734 // stencil bit used for clipping.
735 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
736 canRenderDirectToStencil,
737 clipBit,
738 fillInverted,
739 &passes,
740 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741
742 // draw the element to the client stencil bits if necessary
743 if (!canDrawDirectToClip) {
744 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000745 kIncClamp_StencilOp,
746 kIncClamp_StencilOp,
747 kAlways_StencilFunc,
748 0xffff,
749 0x0000,
750 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000752 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000754 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000756 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000757 if (canRenderDirectToStencil) {
758 *drawState->stencil() = gDrawToStencil;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000759 pr->drawPath(clipPath, stroke, fGpu, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000760 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000761 pr->stencilPath(clipPath, stroke, fGpu);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000762 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000763 }
764 }
765 }
766
767 // now we modify the clip bit by rendering either the clip
768 // element directly or a bounding rect of the entire clip.
769 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
770 for (int p = 0; p < passes; ++p) {
771 *drawState->stencil() = stencilSettings[p];
772 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000773 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000775 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776 } else {
777 SET_RANDOM_COLOR
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000778 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000779 }
780 } else {
781 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000782 // The view matrix is setup to do clip space -> stencil space translation, so
783 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000784 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785 }
786 }
787 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000789 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000790 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000791 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000792 return true;
793}
794
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000795
bsalomon@google.com411dad02012-06-05 20:24:20 +0000796// mapping of clip-respecting stencil funcs to normal stencil funcs
797// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000798static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000799 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
800 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
801 // In the Clip Funcs
802 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
803 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
804 kLess_StencilFunc, // kLessIfInClip_StencilFunc
805 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
806 // Special in the clip func that forces user's ref to be 0.
807 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
808 // make ref 0 and do normal nequal.
809 },
810 {// Stencil-Clipping is ENABLED
811 // In the Clip Funcs
812 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
813 // eq stencil clip bit, mask
814 // out user bits.
815
816 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
817 // add stencil bit to mask and ref
818
819 kLess_StencilFunc, // kLessIfInClip_StencilFunc
820 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
821 // for both of these we can add
822 // the clip bit to the mask and
823 // ref and compare as normal
824 // Special in the clip func that forces user's ref to be 0.
825 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
826 // make ref have only the clip bit set
827 // and make comparison be less
828 // 10..0 < 1..user_bits..
829 }
830};
831
bsalomon@google.coma3201942012-06-21 19:58:20 +0000832namespace {
833// Sets the settings to clip against the stencil buffer clip while ignoring the
834// client bits.
835const GrStencilSettings& basic_apply_stencil_clip_settings() {
836 // stencil settings to use when clip is in stencil
837 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
838 kKeep_StencilOp,
839 kKeep_StencilOp,
840 kAlwaysIfInClip_StencilFunc,
841 0x0000,
842 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000843 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000844 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
845}
846}
847
848void GrClipMaskManager::setGpuStencil() {
849 // We make two copies of the StencilSettings here (except in the early
850 // exit scenario. One copy from draw state to the stack var. Then another
851 // from the stack var to the gpu. We could make this class hold a ptr to
852 // GrGpu's fStencilSettings and eliminate the stack copy here.
853
854 const GrDrawState& drawState = fGpu->getDrawState();
855
856 // use stencil for clipping if clipping is enabled and the clip
857 // has been written into the stencil.
858 GrClipMaskManager::StencilClipMode clipMode;
859 if (this->isClipInStencil() && drawState.isClipState()) {
860 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
861 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000862 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000863 GrGpu::kModifyStencilClip_StateBit));
864 } else if (drawState.isStateFlagEnabled(
865 GrGpu::kModifyStencilClip_StateBit)) {
866 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
867 } else {
868 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
869 }
870
871 GrStencilSettings settings;
872 // The GrGpu client may not be using the stencil buffer but we may need to
873 // enable it in order to respect a stencil clip.
874 if (drawState.getStencil().isDisabled()) {
875 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
876 settings = basic_apply_stencil_clip_settings();
877 } else {
878 fGpu->disableStencil();
879 return;
880 }
881 } else {
882 settings = drawState.getStencil();
883 }
884
885 // TODO: dynamically attach a stencil buffer
886 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000887 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000888 drawState.getRenderTarget()->getStencilBuffer();
889 if (NULL != stencilBuffer) {
890 stencilBits = stencilBuffer->bits();
891 }
892
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000893 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
894 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000895 this->adjustStencilParams(&settings, clipMode, stencilBits);
896 fGpu->setStencilSettings(settings);
897}
898
899void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
900 StencilClipMode mode,
901 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000902 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000903
904 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000905 // We assume that this clip manager itself is drawing to the GrGpu and
906 // has already setup the correct values.
907 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000908 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000909
bsalomon@google.com411dad02012-06-05 20:24:20 +0000910 unsigned int clipBit = (1 << (stencilBitCnt - 1));
911 unsigned int userBits = clipBit - 1;
912
bsalomon@google.coma3201942012-06-21 19:58:20 +0000913 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000914 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000915
bsalomon@google.coma3201942012-06-21 19:58:20 +0000916 bool finished = false;
917 while (!finished) {
918 GrStencilFunc func = settings->func(face);
919 uint16_t writeMask = settings->writeMask(face);
920 uint16_t funcMask = settings->funcMask(face);
921 uint16_t funcRef = settings->funcRef(face);
922
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000923 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000924
925 writeMask &= userBits;
926
927 if (func >= kBasicStencilFuncCount) {
928 int respectClip = kRespectClip_StencilClipMode == mode;
929 if (respectClip) {
930 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000931 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932 switch (func) {
933 case kAlwaysIfInClip_StencilFunc:
934 funcMask = clipBit;
935 funcRef = clipBit;
936 break;
937 case kEqualIfInClip_StencilFunc:
938 case kLessIfInClip_StencilFunc:
939 case kLEqualIfInClip_StencilFunc:
940 funcMask = (funcMask & userBits) | clipBit;
941 funcRef = (funcRef & userBits) | clipBit;
942 break;
943 case kNonZeroIfInClip_StencilFunc:
944 funcMask = (funcMask & userBits) | clipBit;
945 funcRef = clipBit;
946 break;
947 default:
948 GrCrash("Unknown stencil func");
949 }
950 } else {
951 funcMask &= userBits;
952 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000953 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000954 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000955 gSpecialToBasicStencilFunc[respectClip];
956 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000957 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000958 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000959 funcMask &= userBits;
960 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000961 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000962
963 settings->setFunc(face, func);
964 settings->setWriteMask(face, writeMask);
965 settings->setFuncMask(face, funcMask);
966 settings->setFuncRef(face, funcRef);
967
968 if (GrStencilSettings::kFront_Face == face) {
969 face = GrStencilSettings::kBack_Face;
970 finished = !twoSided;
971 } else {
972 finished = true;
973 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000974 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000975 if (!twoSided) {
976 settings->copyFrontSettingsToBack();
977 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000978}
979
980////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000981GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000982 GrReducedClip::InitialState initialState,
983 const GrReducedClip::ElementList& elements,
984 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000985 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000986
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000987 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000988 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000989 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000990 }
991
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000992 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000993 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000994 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000995 }
996
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000997 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
998 // the top left corner of the resulting rect to the top left of the texture.
999 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1000
robertphillips@google.com2c756812012-05-22 20:28:23 +00001001 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001002
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001003 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001004 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1005 SkIntToScalar(-clipSpaceIBounds.fTop));
1006 helper.init(maskSpaceIBounds, &matrix);
1007
1008 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001009
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001010 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001011
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001012 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001013
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001014 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001015 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001016
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001017 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1018 // Intersect and reverse difference require modifying pixels outside of the geometry
1019 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1020 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1021 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001022 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001023 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001024 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001025 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001026 }
1027
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001028 SkPath clipPath;
1029 element->asPath(&clipPath);
1030 clipPath.toggleInverseFillType();
1031 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001032
1033 continue;
1034 }
1035
1036 // The other ops (union, xor, diff) only affect pixels inside
1037 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001038 if (Element::kRect_Type == element->getType()) {
1039 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1040 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001041 SkPath path;
1042 element->asPath(&path);
1043 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001044 }
1045 }
1046
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001047 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001048
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001049 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001050 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001051}
1052
robertphillips@google.comf294b772012-04-27 14:29:26 +00001053////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001054void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001055 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001056}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001057
1058void GrClipMaskManager::setGpu(GrGpu* gpu) {
1059 fGpu = gpu;
1060 fAACache.setContext(gpu->getContext());
1061}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001062
1063void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1064 const GrDrawState& drawState = fGpu->getDrawState();
1065 GrClipMaskManager::StencilClipMode clipMode;
1066 if (this->isClipInStencil() && drawState.isClipState()) {
1067 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1068 // We can't be modifying the clip and respecting it at the same time.
1069 SkASSERT(!drawState.isStateFlagEnabled(
1070 GrGpu::kModifyStencilClip_StateBit));
1071 } else if (drawState.isStateFlagEnabled(
1072 GrGpu::kModifyStencilClip_StateBit)) {
1073 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1074 } else {
1075 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1076 }
1077
1078 // TODO: dynamically attach a stencil buffer
1079 int stencilBits = 0;
1080 GrStencilBuffer* stencilBuffer =
1081 drawState.getRenderTarget()->getStencilBuffer();
1082 if (NULL != stencilBuffer) {
1083 stencilBits = stencilBuffer->bits();
1084 this->adjustStencilParams(settings, clipMode, stencilBits);
1085 }
1086}