blob: e24c759688ef2672af7528aa84a2355aac672a1f [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) };
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000183 effect.reset(GrConvexPolyEffect::Create(kFillBW_GrEffectEdgeType,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000184 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.orgcabf4b22014-03-05 18:27:43 +0000189 GrEffectEdgeType type = isAA ? kFillAA_GrEffectEdgeType : kFillBW_GrEffectEdgeType;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000190 effect.reset(GrConvexPolyEffect::Create(type, path, &offset));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000191 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000192 } else if (isAA && SkClipStack::Element::kRRect_Type == type && !rt->isMultisampled()) {
193 const SkRRect& rrect = elements.tail()->getRRect();
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000194 effect.reset(GrRRectEffect::Create(kFillAA_GrEffectEdgeType, rrect));
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000195 } else if (isAA && SkClipStack::Element::kRect_Type == type && !rt->isMultisampled()) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000196 // We only handle AA/non-MSAA rects here. Coverage effect AA isn't MSAA friendly and
197 // non-AA rect clips are handled by the scissor.
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000198 SkRect rect = elements.tail()->getRect();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000199 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000200 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000201 rect.offset(offset);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000202 effect.reset(GrConvexPolyEffect::Create(kFillAA_GrEffectEdgeType, rect));
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000203 // This should never fail.
204 SkASSERT(effect);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000205 }
206 if (effect) {
207 are->set(fGpu->drawState());
208 fGpu->drawState()->addCoverageEffect(effect);
commit-bot@chromium.org6516d4b2014-02-07 14:04:48 +0000209 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
210 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
211 fGpu->enableScissor(scissorSpaceIBounds);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000212 this->setGpuStencil();
213 return true;
214 }
215 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000216
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000217#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000218 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000219 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000220 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000221
222 if (this->useSWOnlyPath(elements)) {
223 // The clip geometry is complex enough that it will be more efficient to create it
224 // entirely in software
225 result = this->createSoftwareClipMask(genID,
226 initialState,
227 elements,
228 clipSpaceIBounds);
229 } else {
230 result = this->createAlphaClipMask(genID,
231 initialState,
232 elements,
233 clipSpaceIBounds);
234 }
235
236 if (NULL != result) {
237 // The mask's top left coord should be pinned to the rounded-out top left corner of
238 // clipSpace bounds. We determine the mask's position WRT to the render target here.
239 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
240 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000241 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000242 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000243 fGpu->disableScissor();
244 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000245 return true;
246 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000247 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000248 }
249#endif // GR_AA_CLIP
250
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000251 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
252 // be created. In either case, free up the texture in the anti-aliased mask cache.
253 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
254 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
255 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000256 fAACache.reset();
257
bsalomon@google.coma3201942012-06-21 19:58:20 +0000258 // If the clip is a rectangle then just set the scissor. Otherwise, create
259 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000260 if (isRect) {
261 SkIRect clipRect = clipSpaceIBounds;
262 clipRect.offset(-clipDataIn->fOrigin);
263 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000264 this->setGpuStencil();
265 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000266 }
267
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000268 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000269 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000270 this->createStencilClipMask(genID,
271 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000272 elements,
273 clipSpaceIBounds,
274 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000275
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000276 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
277 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
278 // use both stencil and scissor test to the bounds for the final draw.
279 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
280 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
281 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000282 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000283 return true;
284}
285
286#define VISUALIZE_COMPLEX_CLIP 0
287
288#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000289 #include "SkRandom.h"
290 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
292#else
293 #define SET_RANDOM_COLOR
294#endif
295
296namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000297
298////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000299// set up the OpenGL blend function to perform the specified
300// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000301void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000302
303 switch (op) {
304 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000305 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000306 break;
307 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000308 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000309 break;
310 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000311 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000312 break;
313 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000314 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000315 break;
316 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000317 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000318 break;
319 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000320 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000321 break;
322 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000323 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000324 break;
325 }
326}
327
robertphillips@google.com72176b22012-05-23 13:19:12 +0000328}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000329
330////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000331bool GrClipMaskManager::drawElement(GrTexture* target,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000332 const SkClipStack::Element* element,
333 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000334 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000335
336 drawState->setRenderTarget(target->asRenderTarget());
337
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000338 // TODO: Draw rrects directly here.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000339 switch (element->getType()) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000340 case Element::kEmpty_Type:
341 SkDEBUGFAIL("Should never get here with an empty element.");
342 break;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000343 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
345 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000346 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000347 getContext()->getAARectRenderer()->fillAARect(fGpu,
348 fGpu,
349 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000350 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000351 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000352 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000353 } else {
354 fGpu->drawSimpleRect(element->getRect(), NULL);
355 }
356 return true;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000357 default: {
358 SkPath path;
359 element->asPath(&path);
360 if (path.isInverseFillType()) {
361 path.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000362 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000363 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000364 if (NULL == pr) {
365 GrPathRendererChain::DrawType type;
366 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
367 GrPathRendererChain::kColor_DrawType;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000368 pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
robertphillips@google.come79f3202014-02-11 16:30:21 +0000369 }
370 if (NULL == pr) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000371 return false;
372 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000373 pr->drawPath(path, stroke, fGpu, element->isAA());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000374 break;
375 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376 }
377 return true;
378}
379
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000380bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
381 const SkClipStack::Element* element,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000382 GrPathRenderer** pr) {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000383 GrDrawState* drawState = fGpu->drawState();
384 drawState->setRenderTarget(target->asRenderTarget());
385
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000386 if (Element::kRect_Type == element->getType()) {
387 return true;
388 } else {
389 // We shouldn't get here with an empty clip element.
390 SkASSERT(Element::kEmpty_Type != element->getType());
391 SkPath path;
392 element->asPath(&path);
393 if (path.isInverseFillType()) {
394 path.toggleInverseFillType();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000395 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000396 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
397 GrPathRendererChain::DrawType type = element->isAA() ?
398 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
399 GrPathRendererChain::kStencilAndColor_DrawType;
400 *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
401 return NULL != *pr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000402 }
403}
404
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000405void GrClipMaskManager::mergeMask(GrTexture* dstMask,
406 GrTexture* srcMask,
407 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000408 const SkIRect& dstBound,
409 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000410 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000411 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000412 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000413 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000414
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000415 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000416
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000417 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000418
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000419 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000420 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000421
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000422 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000423 GrTextureDomainEffect::Create(srcMask,
424 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000425 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
426 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000427 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000428 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000429}
430
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000431// get a texture to act as a temporary buffer for AA clip boolean operations
432// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000433void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000434 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000435 // we've already allocated the temp texture
436 return;
437 }
438
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000439 GrTextureDesc desc;
440 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000441 desc.fWidth = width;
442 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000443 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000444
robertphillips@google.com2c756812012-05-22 20:28:23 +0000445 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000446}
447
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000448////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000449// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
450// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
451// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000452bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000453 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000454 GrTexture** result,
455 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000456 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000457 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000458
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000459 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
460 // Since we are setting up the cache we know the last lookup was a miss. Free up the
461 // currently cached mask so it can be reused.
462 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000463
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000464 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000465 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000466 desc.fWidth = clipSpaceIBounds.width();
467 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000468 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000469 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000470 // We would always like A8 but it isn't supported on all platforms
471 desc.fConfig = kAlpha_8_GrPixelConfig;
472 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000473
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000474 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000475 }
476
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000477 *result = fAACache.getLastMask();
478 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000479}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000480
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000481////////////////////////////////////////////////////////////////////////////////
482// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000483GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000484 InitialState initialState,
485 const ElementList& elements,
486 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000487 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000488
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000489 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000490 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000491 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000492 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000493 }
494
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000495 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000496 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000497 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000498 }
499
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000500 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000501 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000502 SkIntToScalar(-clipSpaceIBounds.fLeft),
503 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000504 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000505 // The texture may be larger than necessary, this rect represents the part of the texture
506 // we populate with a rasterization of the clip.
507 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
508
bsalomon@google.com137f1342013-05-29 21:27:53 +0000509 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
510 SkMatrix translate;
511 translate.setTranslate(clipToMaskOffset);
512 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
513
514 GrDrawState* drawState = fGpu->drawState();
515
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000516 // We're drawing a coverage mask and want coverage to be run through the blend function.
517 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
518
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000519 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
520 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000521 fGpu->clear(&maskSpaceIBounds,
522 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000523 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000524 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000525
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000526 // When we use the stencil in the below loop it is important to have this clip installed.
527 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
528 // pass must not set values outside of this bounds or stencil values outside the rect won't be
529 // cleared.
530 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
531 drawState->enableState(GrDrawState::kClip_StateBit);
532
robertphillips@google.comf105b102012-05-14 12:18:26 +0000533 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000534 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000535 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000536 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000537 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000538 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000539
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000540 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000541 GrPathRenderer* pr = NULL;
542 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000543 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000544 // 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 +0000545 // mask buffer can be substantially larger than the actually clip stack element. We
546 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000547 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000548 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000549
550 if (useTemp) {
551 if (invert) {
552 maskSpaceElementIBounds = maskSpaceIBounds;
553 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000554 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000555 elementBounds.offset(clipToMaskOffset);
556 elementBounds.roundOut(&maskSpaceElementIBounds);
557 }
558
559 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
560 if (NULL == temp.texture()) {
561 fAACache.reset();
562 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000563 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000564 dst = temp.texture();
565 // clear the temp target and set blend to replace
566 fGpu->clear(&maskSpaceElementIBounds,
567 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000568 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000569 dst->asRenderTarget());
570 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000571
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000572 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000573 // draw directly into the result with the stencil set to make the pixels affected
574 // by the clip shape be non-zero.
575 dst = result;
576 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
577 kReplace_StencilOp,
578 kReplace_StencilOp,
579 kAlways_StencilFunc,
580 0xffff,
581 0xffff,
582 0xffff);
583 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000584 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000585 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000586
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000587 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000588
robertphillips@google.come79f3202014-02-11 16:30:21 +0000589 if (!this->drawElement(dst, element, pr)) {
590 fAACache.reset();
591 return NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000592 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000593
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000594 if (useTemp) {
595 // Now draw into the accumulator using the real operation and the temp buffer as a
596 // texture
597 this->mergeMask(result,
598 temp.texture(),
599 op,
600 maskSpaceIBounds,
601 maskSpaceElementIBounds);
602 } else {
603 // Draw to the exterior pixels (those with a zero stencil value).
604 drawState->setAlpha(invert ? 0xff : 0x00);
605 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
606 kZero_StencilOp,
607 kZero_StencilOp,
608 kEqual_StencilFunc,
609 0xffff,
610 0x0000,
611 0xffff);
612 drawState->setStencil(kDrawOutsideElement);
613 fGpu->drawSimpleRect(clipSpaceIBounds);
614 drawState->disableStencil();
615 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000616 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000617 // all the remaining ops can just be directly draw into the accumulation buffer
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000618 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000619 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000620 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000621 }
622 }
623
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000624 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000625 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626}
627
628////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000629// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000630// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000631bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
632 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000633 const ElementList& elements,
634 const SkIRect& clipSpaceIBounds,
635 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000636
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000637 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000638
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000639 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000640 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641
642 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000643 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000644
645 // TODO: dynamically attach a SB when needed.
646 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
647 if (NULL == stencilBuffer) {
648 return false;
649 }
650
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000651 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000652
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000653 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000654
bsalomon@google.com137f1342013-05-29 21:27:53 +0000655 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
656 SkVector translate = {
657 SkIntToScalar(clipSpaceToStencilOffset.fX),
658 SkIntToScalar(clipSpaceToStencilOffset.fY)
659 };
660 SkMatrix matrix;
661 matrix.setTranslate(translate);
662 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000663 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000664
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000666
bsalomon@google.com9f131742012-12-13 20:43:56 +0000667 // We set the current clip to the bounds so that our recursive draws are scissored to them.
668 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
669 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
670 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
671 drawState->enableState(GrDrawState::kClip_StateBit);
672
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000673#if !VISUALIZE_COMPLEX_CLIP
674 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
675#endif
676
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000677 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000678 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000679 clipBit = (1 << (clipBit-1));
680
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000681 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682
683 // walk through each clip element and perform its set op
684 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000685 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
686 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000687 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688 // enabled at bottom of loop
689 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000690 // if the target is MSAA then we want MSAA enabled when the clip is soft
691 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000692 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000693 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000695 // This will be used to determine whether the clip shape can be rendered into the
696 // stencil with arbitrary stencil settings.
697 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000699 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000700
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000701 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000702
robertphillips@google.come79f3202014-02-11 16:30:21 +0000703 GrPathRenderer* pr = NULL;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000704 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000705 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000706 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000708 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000709 element->asPath(&clipPath);
710 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000711 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000712 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000713 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000714 pr = this->getContext()->getPathRenderer(clipPath,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000715 stroke,
716 fGpu,
717 false,
718 GrPathRendererChain::kStencilOnly_DrawType,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000719 &stencilSupport);
720 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721 return false;
722 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 }
724
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 int passes;
726 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
727
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000728 bool canRenderDirectToStencil =
729 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000731 // fill rule, and set operation can
732 // we render the element directly to
733 // stencil bit used for clipping.
734 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
735 canRenderDirectToStencil,
736 clipBit,
737 fillInverted,
738 &passes,
739 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
741 // draw the element to the client stencil bits if necessary
742 if (!canDrawDirectToClip) {
743 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000744 kIncClamp_StencilOp,
745 kIncClamp_StencilOp,
746 kAlways_StencilFunc,
747 0xffff,
748 0x0000,
749 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000751 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000753 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000755 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000756 if (canRenderDirectToStencil) {
757 *drawState->stencil() = gDrawToStencil;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000758 pr->drawPath(clipPath, stroke, fGpu, false);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000759 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000760 pr->stencilPath(clipPath, stroke, fGpu);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000761 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762 }
763 }
764 }
765
766 // now we modify the clip bit by rendering either the clip
767 // element directly or a bounding rect of the entire clip.
768 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
769 for (int p = 0; p < passes; ++p) {
770 *drawState->stencil() = stencilSettings[p];
771 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000772 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000773 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000774 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 } else {
776 SET_RANDOM_COLOR
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000777 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778 }
779 } else {
780 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000781 // The view matrix is setup to do clip space -> stencil space translation, so
782 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000783 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000784 }
785 }
786 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000788 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000789 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000790 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791 return true;
792}
793
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000794
bsalomon@google.com411dad02012-06-05 20:24:20 +0000795// mapping of clip-respecting stencil funcs to normal stencil funcs
796// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000797static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000798 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
799 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
800 // In the Clip Funcs
801 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
802 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
803 kLess_StencilFunc, // kLessIfInClip_StencilFunc
804 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
805 // Special in the clip func that forces user's ref to be 0.
806 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
807 // make ref 0 and do normal nequal.
808 },
809 {// Stencil-Clipping is ENABLED
810 // In the Clip Funcs
811 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
812 // eq stencil clip bit, mask
813 // out user bits.
814
815 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
816 // add stencil bit to mask and ref
817
818 kLess_StencilFunc, // kLessIfInClip_StencilFunc
819 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
820 // for both of these we can add
821 // the clip bit to the mask and
822 // ref and compare as normal
823 // Special in the clip func that forces user's ref to be 0.
824 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
825 // make ref have only the clip bit set
826 // and make comparison be less
827 // 10..0 < 1..user_bits..
828 }
829};
830
bsalomon@google.coma3201942012-06-21 19:58:20 +0000831namespace {
832// Sets the settings to clip against the stencil buffer clip while ignoring the
833// client bits.
834const GrStencilSettings& basic_apply_stencil_clip_settings() {
835 // stencil settings to use when clip is in stencil
836 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
837 kKeep_StencilOp,
838 kKeep_StencilOp,
839 kAlwaysIfInClip_StencilFunc,
840 0x0000,
841 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000842 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000843 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
844}
845}
846
847void GrClipMaskManager::setGpuStencil() {
848 // We make two copies of the StencilSettings here (except in the early
849 // exit scenario. One copy from draw state to the stack var. Then another
850 // from the stack var to the gpu. We could make this class hold a ptr to
851 // GrGpu's fStencilSettings and eliminate the stack copy here.
852
853 const GrDrawState& drawState = fGpu->getDrawState();
854
855 // use stencil for clipping if clipping is enabled and the clip
856 // has been written into the stencil.
857 GrClipMaskManager::StencilClipMode clipMode;
858 if (this->isClipInStencil() && drawState.isClipState()) {
859 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
860 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000861 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000862 GrGpu::kModifyStencilClip_StateBit));
863 } else if (drawState.isStateFlagEnabled(
864 GrGpu::kModifyStencilClip_StateBit)) {
865 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
866 } else {
867 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
868 }
869
870 GrStencilSettings settings;
871 // The GrGpu client may not be using the stencil buffer but we may need to
872 // enable it in order to respect a stencil clip.
873 if (drawState.getStencil().isDisabled()) {
874 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
875 settings = basic_apply_stencil_clip_settings();
876 } else {
877 fGpu->disableStencil();
878 return;
879 }
880 } else {
881 settings = drawState.getStencil();
882 }
883
884 // TODO: dynamically attach a stencil buffer
885 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000886 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000887 drawState.getRenderTarget()->getStencilBuffer();
888 if (NULL != stencilBuffer) {
889 stencilBits = stencilBuffer->bits();
890 }
891
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000892 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
893 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000894 this->adjustStencilParams(&settings, clipMode, stencilBits);
895 fGpu->setStencilSettings(settings);
896}
897
898void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
899 StencilClipMode mode,
900 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000901 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000902
903 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000904 // We assume that this clip manager itself is drawing to the GrGpu and
905 // has already setup the correct values.
906 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000907 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000908
bsalomon@google.com411dad02012-06-05 20:24:20 +0000909 unsigned int clipBit = (1 << (stencilBitCnt - 1));
910 unsigned int userBits = clipBit - 1;
911
bsalomon@google.coma3201942012-06-21 19:58:20 +0000912 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000913 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000914
bsalomon@google.coma3201942012-06-21 19:58:20 +0000915 bool finished = false;
916 while (!finished) {
917 GrStencilFunc func = settings->func(face);
918 uint16_t writeMask = settings->writeMask(face);
919 uint16_t funcMask = settings->funcMask(face);
920 uint16_t funcRef = settings->funcRef(face);
921
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000922 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000923
924 writeMask &= userBits;
925
926 if (func >= kBasicStencilFuncCount) {
927 int respectClip = kRespectClip_StencilClipMode == mode;
928 if (respectClip) {
929 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000930 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000931 switch (func) {
932 case kAlwaysIfInClip_StencilFunc:
933 funcMask = clipBit;
934 funcRef = clipBit;
935 break;
936 case kEqualIfInClip_StencilFunc:
937 case kLessIfInClip_StencilFunc:
938 case kLEqualIfInClip_StencilFunc:
939 funcMask = (funcMask & userBits) | clipBit;
940 funcRef = (funcRef & userBits) | clipBit;
941 break;
942 case kNonZeroIfInClip_StencilFunc:
943 funcMask = (funcMask & userBits) | clipBit;
944 funcRef = clipBit;
945 break;
946 default:
947 GrCrash("Unknown stencil func");
948 }
949 } else {
950 funcMask &= userBits;
951 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000952 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000953 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000954 gSpecialToBasicStencilFunc[respectClip];
955 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000956 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000957 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958 funcMask &= userBits;
959 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000960 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000961
962 settings->setFunc(face, func);
963 settings->setWriteMask(face, writeMask);
964 settings->setFuncMask(face, funcMask);
965 settings->setFuncRef(face, funcRef);
966
967 if (GrStencilSettings::kFront_Face == face) {
968 face = GrStencilSettings::kBack_Face;
969 finished = !twoSided;
970 } else {
971 finished = true;
972 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000973 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000974 if (!twoSided) {
975 settings->copyFrontSettingsToBack();
976 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000977}
978
979////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000980GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000981 GrReducedClip::InitialState initialState,
982 const GrReducedClip::ElementList& elements,
983 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000984 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000985
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000986 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000987 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000988 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000989 }
990
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000991 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000992 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000993 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000994 }
995
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000996 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
997 // the top left corner of the resulting rect to the top left of the texture.
998 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
999
robertphillips@google.com2c756812012-05-22 20:28:23 +00001000 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001001
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001002 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001003 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1004 SkIntToScalar(-clipSpaceIBounds.fTop));
1005 helper.init(maskSpaceIBounds, &matrix);
1006
1007 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001008
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001009 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001010
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001011 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001012
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001013 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001014 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001015
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001016 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1017 // Intersect and reverse difference require modifying pixels outside of the geometry
1018 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1019 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1020 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +00001021 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001022 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001023 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001024 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001025 }
1026
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001027 SkPath clipPath;
1028 element->asPath(&clipPath);
1029 clipPath.toggleInverseFillType();
1030 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001031
1032 continue;
1033 }
1034
1035 // The other ops (union, xor, diff) only affect pixels inside
1036 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001037 if (Element::kRect_Type == element->getType()) {
1038 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1039 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +00001040 SkPath path;
1041 element->asPath(&path);
1042 helper.draw(path, stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001043 }
1044 }
1045
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001046 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001047
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001048 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001049 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001050}
1051
robertphillips@google.comf294b772012-04-27 14:29:26 +00001052////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001053void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001054 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001055}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001056
1057void GrClipMaskManager::setGpu(GrGpu* gpu) {
1058 fGpu = gpu;
1059 fAACache.setContext(gpu->getContext());
1060}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001061
1062void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1063 const GrDrawState& drawState = fGpu->getDrawState();
1064 GrClipMaskManager::StencilClipMode clipMode;
1065 if (this->isClipInStencil() && drawState.isClipState()) {
1066 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1067 // We can't be modifying the clip and respecting it at the same time.
1068 SkASSERT(!drawState.isStateFlagEnabled(
1069 GrGpu::kModifyStencilClip_StateBit));
1070 } else if (drawState.isStateFlagEnabled(
1071 GrGpu::kModifyStencilClip_StateBit)) {
1072 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1073 } else {
1074 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1075 }
1076
1077 // TODO: dynamically attach a stencil buffer
1078 int stencilBits = 0;
1079 GrStencilBuffer* stencilBuffer =
1080 drawState.getRenderTarget()->getStencilBuffer();
1081 if (NULL != stencilBuffer) {
1082 stencilBits = stencilBuffer->bits();
1083 this->adjustStencilParams(settings, clipMode, stencilBits);
1084 }
1085}