blob: 8a7aac247aaaf8ea45ff892c6391eac97d7406a8 [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
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000097 // Skip rrects once we're drawing them directly.
98 if (Element::kRect_Type != element->getType()) {
99 SkPath path;
100 element->asPath(&path);
101 if (path_needs_SW_renderer(this->getContext(), fGpu, path, stroke, element->isAA())) {
102 return true;
103 }
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,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000113 GrDrawState::AutoRestoreEffects* are,
114 const SkRect* devBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000115 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000116
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000117 ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000118 int32_t genID;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000119 InitialState initialState;
120 SkIRect clipSpaceIBounds;
121 bool requiresAA;
122 bool isRect = false;
123
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000124 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000125
126 const GrRenderTarget* rt = drawState->getRenderTarget();
127 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000128 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000129
130 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
131
132 if (!ignoreClip) {
133 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
134 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
135 ReduceClipStack(*clipDataIn->fClipStack,
136 clipSpaceRTIBounds,
137 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000138 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000139 &initialState,
140 &clipSpaceIBounds,
141 &requiresAA);
142 if (elements.isEmpty()) {
143 if (kAllIn_InitialState == initialState) {
144 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
145 isRect = true;
146 } else {
147 return false;
148 }
149 }
150 }
151
152 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000153 fGpu->disableScissor();
154 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000155 return true;
156 }
157
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000158 // If there is only one clip element we check whether the draw's bounds are contained
159 // fully within the clip. If not, we install an effect that handles the clip for some
160 // cases.
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000161 if (1 == elements.count() && SkRegion::kReplace_Op == elements.tail()->getOp()) {
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000162 if (NULL != devBounds) {
163 SkRect boundsInClipSpace = *devBounds;
164 boundsInClipSpace.offset(SkIntToScalar(clipDataIn->fOrigin.fX),
165 SkIntToScalar(clipDataIn->fOrigin.fY));
166 if (elements.tail()->contains(boundsInClipSpace)) {
167 fGpu->disableScissor();
168 this->setGpuStencil();
169 return true;
170 }
171 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000172 Element::Type type = elements.tail()->getType();
173 bool isAA = GR_AA_CLIP && elements.tail()->isAA();
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000174 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000175 if (SkClipStack::Element::kPath_Type == type) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000176 const SkPath& path = elements.tail()->getPath();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000177 if (rt->isMultisampled()) {
178 // A coverage effect for AA clipping won't play nicely with MSAA.
179 if (!isAA) {
180 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
181 SkIntToScalar(-clipDataIn->fOrigin.fY) };
182 effect.reset(GrConvexPolyEffect::Create(GrConvexPolyEffect::kFillNoAA_EdgeType,
183 path, &offset));
184 }
185 } else {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000186 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
187 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000188 GrConvexPolyEffect::EdgeType type = isAA ? GrConvexPolyEffect::kFillAA_EdgeType :
189 GrConvexPolyEffect::kFillNoAA_EdgeType;
190 effect.reset(GrConvexPolyEffect::Create(type, path, &offset));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000191 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000192 } else if (isAA && SkClipStack::Element::kRect_Type == type && !rt->isMultisampled()) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000193 // We only handle AA/non-MSAA rects here. Coverage effect AA isn't MSAA friendly and
194 // non-AA rect clips are handled by the scissor.
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000195 SkRect rect = elements.tail()->getRect();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000196 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000197 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000198 rect.offset(offset);
199 effect.reset(GrConvexPolyEffect::CreateForAAFillRect(rect));
200 // This should never fail.
201 SkASSERT(effect);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000202 }
203 if (effect) {
204 are->set(fGpu->drawState());
205 fGpu->drawState()->addCoverageEffect(effect);
commit-bot@chromium.org6516d4b2014-02-07 14:04:48 +0000206 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
207 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
208 fGpu->enableScissor(scissorSpaceIBounds);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000209 this->setGpuStencil();
210 return true;
211 }
212 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000213
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000214#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000215 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000216 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000217 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000218
219 if (this->useSWOnlyPath(elements)) {
220 // The clip geometry is complex enough that it will be more efficient to create it
221 // entirely in software
222 result = this->createSoftwareClipMask(genID,
223 initialState,
224 elements,
225 clipSpaceIBounds);
226 } else {
227 result = this->createAlphaClipMask(genID,
228 initialState,
229 elements,
230 clipSpaceIBounds);
231 }
232
233 if (NULL != result) {
234 // The mask's top left coord should be pinned to the rounded-out top left corner of
235 // clipSpace bounds. We determine the mask's position WRT to the render target here.
236 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
237 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000238 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000239 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000240 fGpu->disableScissor();
241 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000242 return true;
243 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000244 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000245 }
246#endif // GR_AA_CLIP
247
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000248 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
249 // be created. In either case, free up the texture in the anti-aliased mask cache.
250 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
251 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
252 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000253 fAACache.reset();
254
bsalomon@google.coma3201942012-06-21 19:58:20 +0000255 // If the clip is a rectangle then just set the scissor. Otherwise, create
256 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000257 if (isRect) {
258 SkIRect clipRect = clipSpaceIBounds;
259 clipRect.offset(-clipDataIn->fOrigin);
260 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000261 this->setGpuStencil();
262 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000263 }
264
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000265 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000266 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000267 this->createStencilClipMask(genID,
268 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000269 elements,
270 clipSpaceIBounds,
271 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000272
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000273 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
274 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
275 // use both stencil and scissor test to the bounds for the final draw.
276 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
277 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
278 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000279 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000280 return true;
281}
282
283#define VISUALIZE_COMPLEX_CLIP 0
284
285#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000286 #include "SkRandom.h"
287 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000288 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
289#else
290 #define SET_RANDOM_COLOR
291#endif
292
293namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000294
295////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000296// set up the OpenGL blend function to perform the specified
297// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000298void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000299
300 switch (op) {
301 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000302 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000303 break;
304 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000305 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000306 break;
307 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000308 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000309 break;
310 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000311 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000312 break;
313 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000314 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000315 break;
316 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000317 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000318 break;
319 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000320 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000321 break;
322 }
323}
324
robertphillips@google.com72176b22012-05-23 13:19:12 +0000325}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000326
327////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000328bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000329 const SkClipStack::Element* element,
330 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000331 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000332
333 drawState->setRenderTarget(target->asRenderTarget());
334
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000335 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000336 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000337 case Element::kEmpty_Type:
338 SkDEBUGFAIL("Should never get here with an empty element.");
339 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000340 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000341 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
342 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000343 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000344 getContext()->getAARectRenderer()->fillAARect(fGpu,
345 fGpu,
346 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000347 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000348 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000349 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000350 } else {
351 fGpu->drawSimpleRect(element->getRect(), NULL);
352 }
353 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000354 default: {
355 SkPath path;
356 element->asPath(&path);
357 if (path.isInverseFillType()) {
358 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000359 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000360 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000361 if (NULL == pr) {
362 GrPathRendererChain::DrawType type;
363 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
364 GrPathRendererChain::kColor_DrawType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000365 pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000366 }
367 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000368 return false;
369 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000370 pr->drawPath(path, stroke, fGpu, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000371 break;
372 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000373 }
374 return true;
375}
376
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000377bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
378 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000379 GrPathRenderer** pr) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000380 GrDrawState* drawState = fGpu->drawState();
381 drawState->setRenderTarget(target->asRenderTarget());
382
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000383 if (Element::kRect_Type == element->getType()) {
384 return true;
385 } else {
386 // We shouldn't get here with an empty clip element.
387 SkASSERT(Element::kEmpty_Type != element->getType());
388 SkPath path;
389 element->asPath(&path);
390 if (path.isInverseFillType()) {
391 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000392 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000393 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
394 GrPathRendererChain::DrawType type = element->isAA() ?
395 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
396 GrPathRendererChain::kStencilAndColor_DrawType;
397 *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
398 return NULL != *pr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000399 }
400}
401
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000402void GrClipMaskManager::mergeMask(GrTexture* dstMask,
403 GrTexture* srcMask,
404 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000405 const SkIRect& dstBound,
406 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000407 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000408 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000409 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000410 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000411
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000412 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000414 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000415
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000416 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000417 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000418
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000419 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000420 GrTextureDomainEffect::Create(srcMask,
421 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000422 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
423 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000424 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000425 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000426}
427
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000428// get a texture to act as a temporary buffer for AA clip boolean operations
429// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000430void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000431 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000432 // we've already allocated the temp texture
433 return;
434 }
435
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000436 GrTextureDesc desc;
437 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000438 desc.fWidth = width;
439 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000440 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000441
robertphillips@google.com2c756812012-05-22 20:28:23 +0000442 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000443}
444
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000445////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000446// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
447// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
448// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000449bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000450 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000451 GrTexture** result,
452 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000453 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000454 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000455
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000456 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
457 // Since we are setting up the cache we know the last lookup was a miss. Free up the
458 // currently cached mask so it can be reused.
459 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000460
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000461 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000462 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463 desc.fWidth = clipSpaceIBounds.width();
464 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000465 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000466 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000467 // We would always like A8 but it isn't supported on all platforms
468 desc.fConfig = kAlpha_8_GrPixelConfig;
469 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000470
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000471 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000472 }
473
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000474 *result = fAACache.getLastMask();
475 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000476}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000477
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000478////////////////////////////////////////////////////////////////////////////////
479// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000480GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000481 InitialState initialState,
482 const ElementList& elements,
483 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000484 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000485
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000486 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000487 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000488 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000489 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000490 }
491
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000492 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000493 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000494 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495 }
496
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000497 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000498 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000499 SkIntToScalar(-clipSpaceIBounds.fLeft),
500 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000501 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000502 // The texture may be larger than necessary, this rect represents the part of the texture
503 // we populate with a rasterization of the clip.
504 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
505
bsalomon@google.com137f1342013-05-29 21:27:53 +0000506 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
507 SkMatrix translate;
508 translate.setTranslate(clipToMaskOffset);
509 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
510
511 GrDrawState* drawState = fGpu->drawState();
512
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000513 // We're drawing a coverage mask and want coverage to be run through the blend function.
514 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
515
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000516 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
517 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000518 fGpu->clear(&maskSpaceIBounds,
519 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000520 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000521 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000522
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000523 // When we use the stencil in the below loop it is important to have this clip installed.
524 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
525 // pass must not set values outside of this bounds or stencil values outside the rect won't be
526 // cleared.
527 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
528 drawState->enableState(GrDrawState::kClip_StateBit);
529
robertphillips@google.comf105b102012-05-14 12:18:26 +0000530 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000531 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000532 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000533 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000534 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000535 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000536
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000537 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000538 GrPathRenderer* pr = NULL;
539 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000540 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000541 // 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 +0000542 // mask buffer can be substantially larger than the actually clip stack element. We
543 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000544 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000545 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000546
547 if (useTemp) {
548 if (invert) {
549 maskSpaceElementIBounds = maskSpaceIBounds;
550 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000551 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000552 elementBounds.offset(clipToMaskOffset);
553 elementBounds.roundOut(&maskSpaceElementIBounds);
554 }
555
556 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
557 if (NULL == temp.texture()) {
558 fAACache.reset();
559 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000560 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000561 dst = temp.texture();
562 // clear the temp target and set blend to replace
563 fGpu->clear(&maskSpaceElementIBounds,
564 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000565 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000566 dst->asRenderTarget());
567 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000568
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000569 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000570 // draw directly into the result with the stencil set to make the pixels affected
571 // by the clip shape be non-zero.
572 dst = result;
573 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
574 kReplace_StencilOp,
575 kReplace_StencilOp,
576 kAlways_StencilFunc,
577 0xffff,
578 0xffff,
579 0xffff);
580 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000581 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000582 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000583
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000584 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000585
robertphillips@google.come79f3202014-02-11 16:30:21 +0000586 if (!this->drawElement(dst, element, pr)) {
587 fAACache.reset();
588 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000589 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000590
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000591 if (useTemp) {
592 // Now draw into the accumulator using the real operation and the temp buffer as a
593 // texture
594 this->mergeMask(result,
595 temp.texture(),
596 op,
597 maskSpaceIBounds,
598 maskSpaceElementIBounds);
599 } else {
600 // Draw to the exterior pixels (those with a zero stencil value).
601 drawState->setAlpha(invert ? 0xff : 0x00);
602 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
603 kZero_StencilOp,
604 kZero_StencilOp,
605 kEqual_StencilFunc,
606 0xffff,
607 0x0000,
608 0xffff);
609 drawState->setStencil(kDrawOutsideElement);
610 fGpu->drawSimpleRect(clipSpaceIBounds);
611 drawState->disableStencil();
612 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000614 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000615 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000616 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000617 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000618 }
619 }
620
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000621 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000622 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000623}
624
625////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000626// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000627// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000628bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
629 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000630 const ElementList& elements,
631 const SkIRect& clipSpaceIBounds,
632 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000633
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000634 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000635
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000636 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000637 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000638
639 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000640 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641
642 // TODO: dynamically attach a SB when needed.
643 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
644 if (NULL == stencilBuffer) {
645 return false;
646 }
647
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000648 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000649
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000650 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000651
bsalomon@google.com137f1342013-05-29 21:27:53 +0000652 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
653 SkVector translate = {
654 SkIntToScalar(clipSpaceToStencilOffset.fX),
655 SkIntToScalar(clipSpaceToStencilOffset.fY)
656 };
657 SkMatrix matrix;
658 matrix.setTranslate(translate);
659 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000660 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000661
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000662 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663
bsalomon@google.com9f131742012-12-13 20:43:56 +0000664 // We set the current clip to the bounds so that our recursive draws are scissored to them.
665 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
666 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
667 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
668 drawState->enableState(GrDrawState::kClip_StateBit);
669
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000670#if !VISUALIZE_COMPLEX_CLIP
671 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
672#endif
673
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000674 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000675 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676 clipBit = (1 << (clipBit-1));
677
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000678 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000679
680 // walk through each clip element and perform its set op
681 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000682 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
683 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000684 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000685 // enabled at bottom of loop
686 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000687 // if the target is MSAA then we want MSAA enabled when the clip is soft
688 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000689 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000690 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000692 // This will be used to determine whether the clip shape can be rendered into the
693 // stencil with arbitrary stencil settings.
694 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000695
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000696 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000697
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000698 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000699
robertphillips@google.come79f3202014-02-11 16:30:21 +0000700 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000701 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000702 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000703 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000705 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000706 element->asPath(&clipPath);
707 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000708 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000709 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000710 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000711 pr = this->getContext()->getPathRenderer(clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000712 stroke,
713 fGpu,
714 false,
715 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000716 &stencilSupport);
717 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 return false;
719 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 }
721
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722 int passes;
723 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
724
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000725 bool canRenderDirectToStencil =
726 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000728 // fill rule, and set operation can
729 // we render the element directly to
730 // stencil bit used for clipping.
731 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
732 canRenderDirectToStencil,
733 clipBit,
734 fillInverted,
735 &passes,
736 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737
738 // draw the element to the client stencil bits if necessary
739 if (!canDrawDirectToClip) {
740 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000741 kIncClamp_StencilOp,
742 kIncClamp_StencilOp,
743 kAlways_StencilFunc,
744 0xffff,
745 0x0000,
746 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000747 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000748 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000750 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000752 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000753 if (canRenderDirectToStencil) {
754 *drawState->stencil() = gDrawToStencil;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000755 pr->drawPath(clipPath, stroke, fGpu, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000756 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000757 pr->stencilPath(clipPath, stroke, fGpu);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000758 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759 }
760 }
761 }
762
763 // now we modify the clip bit by rendering either the clip
764 // element directly or a bounding rect of the entire clip.
765 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
766 for (int p = 0; p < passes; ++p) {
767 *drawState->stencil() = stencilSettings[p];
768 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000769 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000770 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000771 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772 } else {
773 SET_RANDOM_COLOR
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000774 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 }
776 } else {
777 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000778 // The view matrix is setup to do clip space -> stencil space translation, so
779 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000780 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 }
782 }
783 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000784 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000785 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000786 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000787 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 return true;
789}
790
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000791
bsalomon@google.com411dad02012-06-05 20:24:20 +0000792// mapping of clip-respecting stencil funcs to normal stencil funcs
793// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000794static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000795 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
796 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
797 // In the Clip Funcs
798 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
799 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
800 kLess_StencilFunc, // kLessIfInClip_StencilFunc
801 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
802 // Special in the clip func that forces user's ref to be 0.
803 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
804 // make ref 0 and do normal nequal.
805 },
806 {// Stencil-Clipping is ENABLED
807 // In the Clip Funcs
808 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
809 // eq stencil clip bit, mask
810 // out user bits.
811
812 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
813 // add stencil bit to mask and ref
814
815 kLess_StencilFunc, // kLessIfInClip_StencilFunc
816 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
817 // for both of these we can add
818 // the clip bit to the mask and
819 // ref and compare as normal
820 // Special in the clip func that forces user's ref to be 0.
821 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
822 // make ref have only the clip bit set
823 // and make comparison be less
824 // 10..0 < 1..user_bits..
825 }
826};
827
bsalomon@google.coma3201942012-06-21 19:58:20 +0000828namespace {
829// Sets the settings to clip against the stencil buffer clip while ignoring the
830// client bits.
831const GrStencilSettings& basic_apply_stencil_clip_settings() {
832 // stencil settings to use when clip is in stencil
833 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
834 kKeep_StencilOp,
835 kKeep_StencilOp,
836 kAlwaysIfInClip_StencilFunc,
837 0x0000,
838 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000839 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000840 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
841}
842}
843
844void GrClipMaskManager::setGpuStencil() {
845 // We make two copies of the StencilSettings here (except in the early
846 // exit scenario. One copy from draw state to the stack var. Then another
847 // from the stack var to the gpu. We could make this class hold a ptr to
848 // GrGpu's fStencilSettings and eliminate the stack copy here.
849
850 const GrDrawState& drawState = fGpu->getDrawState();
851
852 // use stencil for clipping if clipping is enabled and the clip
853 // has been written into the stencil.
854 GrClipMaskManager::StencilClipMode clipMode;
855 if (this->isClipInStencil() && drawState.isClipState()) {
856 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
857 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000858 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000859 GrGpu::kModifyStencilClip_StateBit));
860 } else if (drawState.isStateFlagEnabled(
861 GrGpu::kModifyStencilClip_StateBit)) {
862 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
863 } else {
864 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
865 }
866
867 GrStencilSettings settings;
868 // The GrGpu client may not be using the stencil buffer but we may need to
869 // enable it in order to respect a stencil clip.
870 if (drawState.getStencil().isDisabled()) {
871 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
872 settings = basic_apply_stencil_clip_settings();
873 } else {
874 fGpu->disableStencil();
875 return;
876 }
877 } else {
878 settings = drawState.getStencil();
879 }
880
881 // TODO: dynamically attach a stencil buffer
882 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000883 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000884 drawState.getRenderTarget()->getStencilBuffer();
885 if (NULL != stencilBuffer) {
886 stencilBits = stencilBuffer->bits();
887 }
888
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000889 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
890 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000891 this->adjustStencilParams(&settings, clipMode, stencilBits);
892 fGpu->setStencilSettings(settings);
893}
894
895void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
896 StencilClipMode mode,
897 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000898 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000899
900 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000901 // We assume that this clip manager itself is drawing to the GrGpu and
902 // has already setup the correct values.
903 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000904 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000905
bsalomon@google.com411dad02012-06-05 20:24:20 +0000906 unsigned int clipBit = (1 << (stencilBitCnt - 1));
907 unsigned int userBits = clipBit - 1;
908
bsalomon@google.coma3201942012-06-21 19:58:20 +0000909 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000910 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000911
bsalomon@google.coma3201942012-06-21 19:58:20 +0000912 bool finished = false;
913 while (!finished) {
914 GrStencilFunc func = settings->func(face);
915 uint16_t writeMask = settings->writeMask(face);
916 uint16_t funcMask = settings->funcMask(face);
917 uint16_t funcRef = settings->funcRef(face);
918
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000919 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000920
921 writeMask &= userBits;
922
923 if (func >= kBasicStencilFuncCount) {
924 int respectClip = kRespectClip_StencilClipMode == mode;
925 if (respectClip) {
926 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000927 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000928 switch (func) {
929 case kAlwaysIfInClip_StencilFunc:
930 funcMask = clipBit;
931 funcRef = clipBit;
932 break;
933 case kEqualIfInClip_StencilFunc:
934 case kLessIfInClip_StencilFunc:
935 case kLEqualIfInClip_StencilFunc:
936 funcMask = (funcMask & userBits) | clipBit;
937 funcRef = (funcRef & userBits) | clipBit;
938 break;
939 case kNonZeroIfInClip_StencilFunc:
940 funcMask = (funcMask & userBits) | clipBit;
941 funcRef = clipBit;
942 break;
943 default:
944 GrCrash("Unknown stencil func");
945 }
946 } else {
947 funcMask &= userBits;
948 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000949 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000950 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951 gSpecialToBasicStencilFunc[respectClip];
952 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000953 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000954 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000955 funcMask &= userBits;
956 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000957 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958
959 settings->setFunc(face, func);
960 settings->setWriteMask(face, writeMask);
961 settings->setFuncMask(face, funcMask);
962 settings->setFuncRef(face, funcRef);
963
964 if (GrStencilSettings::kFront_Face == face) {
965 face = GrStencilSettings::kBack_Face;
966 finished = !twoSided;
967 } else {
968 finished = true;
969 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000970 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000971 if (!twoSided) {
972 settings->copyFrontSettingsToBack();
973 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000974}
975
976////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000977GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000978 GrReducedClip::InitialState initialState,
979 const GrReducedClip::ElementList& elements,
980 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000981 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000982
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000983 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000984 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000985 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000986 }
987
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000988 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000989 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000990 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000991 }
992
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000993 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
994 // the top left corner of the resulting rect to the top left of the texture.
995 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
996
robertphillips@google.com2c756812012-05-22 20:28:23 +0000997 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000998
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000999 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001000 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1001 SkIntToScalar(-clipSpaceIBounds.fTop));
1002 helper.init(maskSpaceIBounds, &matrix);
1003
1004 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001005
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001006 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001007
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001008 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001009
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001010 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001011 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001012
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001013 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1014 // Intersect and reverse difference require modifying pixels outside of the geometry
1015 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1016 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1017 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001018 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001019 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001020 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001021 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001022 }
1023
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001024 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001025 // convert the rect to a path so we can invert the fill
1026 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001027 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001028 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001029
sugoi@google.com12b4e272012-12-06 20:13:11 +00001030 helper.draw(temp, stroke, SkRegion::kReplace_Op,
1031 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001032 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001033 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001034 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001035 SkPath clipPath = element->getPath();
1036 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +00001037 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001038 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001039 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001040 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001041 }
1042
1043 continue;
1044 }
1045
1046 // The other ops (union, xor, diff) only affect pixels inside
1047 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001048 if (Element::kRect_Type == element->getType()) {
1049 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1050 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001051 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001052 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001053 }
1054 }
1055
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001056 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001057
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001058 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001059 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001060}
1061
robertphillips@google.comf294b772012-04-27 14:29:26 +00001062////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001063void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001064 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001065}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001066
1067void GrClipMaskManager::setGpu(GrGpu* gpu) {
1068 fGpu = gpu;
1069 fAACache.setContext(gpu->getContext());
1070}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001071
1072void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1073 const GrDrawState& drawState = fGpu->getDrawState();
1074 GrClipMaskManager::StencilClipMode clipMode;
1075 if (this->isClipInStencil() && drawState.isClipState()) {
1076 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1077 // We can't be modifying the clip and respecting it at the same time.
1078 SkASSERT(!drawState.isStateFlagEnabled(
1079 GrGpu::kModifyStencilClip_StateBit));
1080 } else if (drawState.isStateFlagEnabled(
1081 GrGpu::kModifyStencilClip_StateBit)) {
1082 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1083 } else {
1084 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1085 }
1086
1087 // TODO: dynamically attach a stencil buffer
1088 int stencilBits = 0;
1089 GrStencilBuffer* stencilBuffer =
1090 drawState.getRenderTarget()->getStencilBuffer();
1091 if (NULL != stencilBuffer) {
1092 stencilBits = stencilBuffer->bits();
1093 this->adjustStencilParams(settings, clipMode, stencilBits);
1094 }
1095}