blob: adb6ceee46c9d9165e68e54154ae4e9800f68263 [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.coma72eef32012-05-01 17:22:59 +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
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036void setup_drawstate_aaclip(GrGpu* gpu,
37 GrTexture* result,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000038 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.com8a4fc402012-05-24 12:42:24 +000062bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000063 GrGpu* gpu,
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000064 const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000065 const SkStrokeRec& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000066 bool doAA) {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000067 // 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 }
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000072 // 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
77 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000078}
79
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000080}
81
robertphillips@google.comfa662942012-05-17 12:20:22 +000082/*
83 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
84 * will be used on any element. If so, it returns true to indicate that the
85 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
86 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000087bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000088
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000089 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000090 // a clip gets complex enough it can just be done in SW regardless
91 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000092 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000093
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000094 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
95 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000096 // rects can always be drawn directly w/o using the software path
97 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000098 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000100 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000101 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000102 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000103 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000106 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000107}
108
robertphillips@google.comf294b772012-04-27 14:29:26 +0000109////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000110// sort out what kind of clip mask needs to be created: alpha, stencil,
111// scissor, or entirely software
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000112bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
113 GrDrawState::AutoRestoreEffects* are) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000114 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000115
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000116 ElementList elements(16);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000117 int32_t genID;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000118 InitialState initialState;
119 SkIRect clipSpaceIBounds;
120 bool requiresAA;
121 bool isRect = false;
122
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000123 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000124
125 const GrRenderTarget* rt = drawState->getRenderTarget();
126 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000127 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000128
129 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
130
131 if (!ignoreClip) {
132 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
133 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
134 ReduceClipStack(*clipDataIn->fClipStack,
135 clipSpaceRTIBounds,
136 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000137 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000138 &initialState,
139 &clipSpaceIBounds,
140 &requiresAA);
141 if (elements.isEmpty()) {
142 if (kAllIn_InitialState == initialState) {
143 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
144 isRect = true;
145 } else {
146 return false;
147 }
148 }
149 }
150
151 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000152 fGpu->disableScissor();
153 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000154 return true;
155 }
156
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000157 // If there is only one clip element and it is a convex polygon we just install an effect that
158 // clips against the edges.
159 if (1 == elements.count() && SkClipStack::Element::kPath_Type == elements.tail()->getType() &&
160 SkRegion::kReplace_Op == elements.tail()->getOp()) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000161 const SkPath& path = elements.tail()->getPath();
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000162 bool isAA = GR_AA_CLIP && elements.tail()->isAA();
163 SkAutoTUnref<GrEffectRef> effect;
164 if (rt->isMultisampled()) {
165 // A coverage effect for AA clipping won't play nicely with MSAA.
166 if (!isAA) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000167 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
168 SkIntToScalar(-clipDataIn->fOrigin.fY) };
169 effect.reset(GrConvexPolyEffect::Create(GrConvexPolyEffect::kFillNoAA_EdgeType,
170 path, &offset));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000171 }
172 } else {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000173 SkVector offset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
174 SkIntToScalar(-clipDataIn->fOrigin.fY) };
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000175 GrConvexPolyEffect::EdgeType type = isAA ? GrConvexPolyEffect::kFillAA_EdgeType :
176 GrConvexPolyEffect::kFillNoAA_EdgeType;
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000177 effect.reset(GrConvexPolyEffect::Create(type, path, &offset));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000178 }
179 if (effect) {
180 are->set(fGpu->drawState());
181 fGpu->drawState()->addCoverageEffect(effect);
commit-bot@chromium.org6516d4b2014-02-07 14:04:48 +0000182 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
183 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
184 fGpu->enableScissor(scissorSpaceIBounds);
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000185 this->setGpuStencil();
186 return true;
187 }
188 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000189
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000190#if GR_AA_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000191 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000192 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000193 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000194
195 if (this->useSWOnlyPath(elements)) {
196 // The clip geometry is complex enough that it will be more efficient to create it
197 // entirely in software
198 result = this->createSoftwareClipMask(genID,
199 initialState,
200 elements,
201 clipSpaceIBounds);
202 } else {
203 result = this->createAlphaClipMask(genID,
204 initialState,
205 elements,
206 clipSpaceIBounds);
207 }
208
209 if (NULL != result) {
210 // The mask's top left coord should be pinned to the rounded-out top left corner of
211 // clipSpace bounds. We determine the mask's position WRT to the render target here.
212 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
213 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000214 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000215 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000216 fGpu->disableScissor();
217 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000218 return true;
219 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000220 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000221 }
222#endif // GR_AA_CLIP
223
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000224 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
225 // be created. In either case, free up the texture in the anti-aliased mask cache.
226 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
227 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
228 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000229 fAACache.reset();
230
bsalomon@google.coma3201942012-06-21 19:58:20 +0000231 // If the clip is a rectangle then just set the scissor. Otherwise, create
232 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000233 if (isRect) {
234 SkIRect clipRect = clipSpaceIBounds;
235 clipRect.offset(-clipDataIn->fOrigin);
236 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000237 this->setGpuStencil();
238 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000239 }
240
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000241 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000242 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000243 this->createStencilClipMask(genID,
244 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000245 elements,
246 clipSpaceIBounds,
247 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000248
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000249 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
250 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
251 // use both stencil and scissor test to the bounds for the final draw.
252 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
253 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
254 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000255 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000256 return true;
257}
258
259#define VISUALIZE_COMPLEX_CLIP 0
260
261#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000262 #include "SkRandom.h"
263 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000264 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
265#else
266 #define SET_RANDOM_COLOR
267#endif
268
269namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000270
271////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000272// set up the OpenGL blend function to perform the specified
273// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000274void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000275
276 switch (op) {
277 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000278 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000279 break;
280 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000281 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000282 break;
283 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000284 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000285 break;
286 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000287 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000288 break;
289 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000290 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000291 break;
292 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000293 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000294 break;
295 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000296 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000297 break;
298 }
299}
300
robertphillips@google.com72176b22012-05-23 13:19:12 +0000301}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000302
303////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000304bool GrClipMaskManager::drawElement(GrTexture* target,
305 const SkClipStack::Element* element,
306 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000307 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000308
309 drawState->setRenderTarget(target->asRenderTarget());
310
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000311 switch (element->getType()) {
312 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
314 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000315 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000316 getContext()->getAARectRenderer()->fillAARect(fGpu,
317 fGpu,
318 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000319 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000320 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000321 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000322 } else {
323 fGpu->drawSimpleRect(element->getRect(), NULL);
324 }
325 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000327 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
328 if (path->isInverseFillType()) {
329 path.writable()->toggleInverseFillType();
330 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000331 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000332 if (NULL == pr) {
333 GrPathRendererChain::DrawType type;
334 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
335 GrPathRendererChain::kColor_DrawType;
336 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
337 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000338 if (NULL == pr) {
339 return false;
340 }
341 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
342 break;
343 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000344 default:
345 // something is wrong if we're trying to draw an empty element.
346 GrCrash("Unexpected element type");
347 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348 }
349 return true;
350}
351
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000352bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
353 const SkClipStack::Element* element,
354 GrPathRenderer** pr) {
355 GrDrawState* drawState = fGpu->drawState();
356 drawState->setRenderTarget(target->asRenderTarget());
357
358 switch (element->getType()) {
359 case Element::kRect_Type:
360 return true;
361 case Element::kPath_Type: {
362 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
363 if (path->isInverseFillType()) {
364 path.writable()->toggleInverseFillType();
365 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000366 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000367 GrPathRendererChain::DrawType type = element->isAA() ?
368 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
369 GrPathRendererChain::kStencilAndColor_DrawType;
370 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
371 return NULL != *pr;
372 }
373 default:
374 // something is wrong if we're trying to draw an empty element.
375 GrCrash("Unexpected element type");
376 return false;
377 }
378}
379
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000380void GrClipMaskManager::mergeMask(GrTexture* dstMask,
381 GrTexture* srcMask,
382 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000383 const SkIRect& dstBound,
384 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000385 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000386 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000387 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000388 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000390 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000392 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000393
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000394 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000395 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000396
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000397 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000398 GrTextureDomainEffect::Create(srcMask,
399 sampleM,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000400 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
401 GrTextureDomain::kDecal_Mode,
humper@google.comb86add12013-07-25 18:49:07 +0000402 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000403 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000404}
405
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000406// get a texture to act as a temporary buffer for AA clip boolean operations
407// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000408void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000409 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000410 // we've already allocated the temp texture
411 return;
412 }
413
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000414 GrTextureDesc desc;
415 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000416 desc.fWidth = width;
417 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000418 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000419
robertphillips@google.com2c756812012-05-22 20:28:23 +0000420 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000421}
422
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000423////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000424// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
425// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
426// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000427bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000428 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000429 GrTexture** result,
430 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000431 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000434 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
435 // Since we are setting up the cache we know the last lookup was a miss. Free up the
436 // currently cached mask so it can be reused.
437 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000439 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000440 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000441 desc.fWidth = clipSpaceIBounds.width();
442 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000443 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000444 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000445 // We would always like A8 but it isn't supported on all platforms
446 desc.fConfig = kAlpha_8_GrPixelConfig;
447 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000448
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000449 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000450 }
451
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000452 *result = fAACache.getLastMask();
453 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000454}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000455
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000456////////////////////////////////////////////////////////////////////////////////
457// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000458GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000459 InitialState initialState,
460 const ElementList& elements,
461 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000462 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000463
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000464 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000465 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000466 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000468 }
469
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000470 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000471 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000472 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000473 }
474
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000475 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000476 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000477 SkIntToScalar(-clipSpaceIBounds.fLeft),
478 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000479 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000480 // The texture may be larger than necessary, this rect represents the part of the texture
481 // we populate with a rasterization of the clip.
482 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
483
bsalomon@google.com137f1342013-05-29 21:27:53 +0000484 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
485 SkMatrix translate;
486 translate.setTranslate(clipToMaskOffset);
487 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
488
489 GrDrawState* drawState = fGpu->drawState();
490
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000491 // We're drawing a coverage mask and want coverage to be run through the blend function.
492 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
493
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000494 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
495 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000496 fGpu->clear(&maskSpaceIBounds,
497 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000498 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000499 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000500
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000501 // When we use the stencil in the below loop it is important to have this clip installed.
502 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
503 // pass must not set values outside of this bounds or stencil values outside the rect won't be
504 // cleared.
505 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
506 drawState->enableState(GrDrawState::kClip_StateBit);
507
robertphillips@google.comf105b102012-05-14 12:18:26 +0000508 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000509 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000510 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000511 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000512 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000513 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000514
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000515 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
516 GrPathRenderer* pr = NULL;
517 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
518 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000519 // 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 +0000520 // mask buffer can be substantially larger than the actually clip stack element. We
521 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000522 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000523 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000524
525 if (useTemp) {
526 if (invert) {
527 maskSpaceElementIBounds = maskSpaceIBounds;
528 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000529 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000530 elementBounds.offset(clipToMaskOffset);
531 elementBounds.roundOut(&maskSpaceElementIBounds);
532 }
533
534 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
535 if (NULL == temp.texture()) {
536 fAACache.reset();
537 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000538 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000539 dst = temp.texture();
540 // clear the temp target and set blend to replace
541 fGpu->clear(&maskSpaceElementIBounds,
542 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000543 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000544 dst->asRenderTarget());
545 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000546
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000547 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000548 // draw directly into the result with the stencil set to make the pixels affected
549 // by the clip shape be non-zero.
550 dst = result;
551 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
552 kReplace_StencilOp,
553 kReplace_StencilOp,
554 kAlways_StencilFunc,
555 0xffff,
556 0xffff,
557 0xffff);
558 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000559 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000561
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000562 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000563
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000564 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000565 fAACache.reset();
566 return NULL;
567 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000568
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000569 if (useTemp) {
570 // Now draw into the accumulator using the real operation and the temp buffer as a
571 // texture
572 this->mergeMask(result,
573 temp.texture(),
574 op,
575 maskSpaceIBounds,
576 maskSpaceElementIBounds);
577 } else {
578 // Draw to the exterior pixels (those with a zero stencil value).
579 drawState->setAlpha(invert ? 0xff : 0x00);
580 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
581 kZero_StencilOp,
582 kZero_StencilOp,
583 kEqual_StencilFunc,
584 0xffff,
585 0x0000,
586 0xffff);
587 drawState->setStencil(kDrawOutsideElement);
588 fGpu->drawSimpleRect(clipSpaceIBounds);
589 drawState->disableStencil();
590 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000591 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000592 // all the remaining ops can just be directly draw into the accumulation buffer
593 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000594 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000595 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000596 }
597 }
598
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000599 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000600 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000601}
602
603////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000604// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000605// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000606bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
607 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000608 const ElementList& elements,
609 const SkIRect& clipSpaceIBounds,
610 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000611
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000612 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000613
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000614 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000615 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000616
617 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000618 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000619
620 // TODO: dynamically attach a SB when needed.
621 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
622 if (NULL == stencilBuffer) {
623 return false;
624 }
625
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000626 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000627
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000628 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000629
bsalomon@google.com137f1342013-05-29 21:27:53 +0000630 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
631 SkVector translate = {
632 SkIntToScalar(clipSpaceToStencilOffset.fX),
633 SkIntToScalar(clipSpaceToStencilOffset.fY)
634 };
635 SkMatrix matrix;
636 matrix.setTranslate(translate);
637 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000638 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000639
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000640 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641
bsalomon@google.com9f131742012-12-13 20:43:56 +0000642 // We set the current clip to the bounds so that our recursive draws are scissored to them.
643 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
644 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
645 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
646 drawState->enableState(GrDrawState::kClip_StateBit);
647
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000648#if !VISUALIZE_COMPLEX_CLIP
649 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
650#endif
651
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000652 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000653 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000654 clipBit = (1 << (clipBit-1));
655
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000656 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000657
658 // walk through each clip element and perform its set op
659 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000660 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
661 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000662 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663 // enabled at bottom of loop
664 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000665 // if the target is MSAA then we want MSAA enabled when the clip is soft
666 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000667 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000668 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000669
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000670 // This will be used to determine whether the clip shape can be rendered into the
671 // stencil with arbitrary stencil settings.
672 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000673
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000674 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000675
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000676 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000677
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000678 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000679 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000680 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000681 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000683 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000684 SkASSERT(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000685 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000686 fillInverted = clipPath->isInverseFillType();
687 if (fillInverted) {
688 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000689 }
690 pr = this->getContext()->getPathRenderer(*clipPath,
691 stroke,
692 fGpu,
693 false,
694 GrPathRendererChain::kStencilOnly_DrawType,
695 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697 return false;
698 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699 }
700
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000701 int passes;
702 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
703
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000704 bool canRenderDirectToStencil =
705 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000707 // fill rule, and set operation can
708 // we render the element directly to
709 // stencil bit used for clipping.
710 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
711 canRenderDirectToStencil,
712 clipBit,
713 fillInverted,
714 &passes,
715 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716
717 // draw the element to the client stencil bits if necessary
718 if (!canDrawDirectToClip) {
719 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000720 kIncClamp_StencilOp,
721 kIncClamp_StencilOp,
722 kAlways_StencilFunc,
723 0xffff,
724 0x0000,
725 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000727 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000729 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000731 SkASSERT(Element::kPath_Type == element->getType());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000732 if (!clipPath->isEmpty()) {
733 if (canRenderDirectToStencil) {
734 *drawState->stencil() = gDrawToStencil;
735 pr->drawPath(*clipPath, stroke, fGpu, false);
736 } else {
737 pr->stencilPath(*clipPath, stroke, fGpu);
738 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739 }
740 }
741 }
742
743 // now we modify the clip bit by rendering either the clip
744 // element directly or a bounding rect of the entire clip.
745 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
746 for (int p = 0; p < passes; ++p) {
747 *drawState->stencil() = stencilSettings[p];
748 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000749 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000751 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000753 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000755 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756 }
757 } else {
758 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000759 // The view matrix is setup to do clip space -> stencil space translation, so
760 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000761 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762 }
763 }
764 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000766 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000767 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000768 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000769 return true;
770}
771
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000772
bsalomon@google.com411dad02012-06-05 20:24:20 +0000773// mapping of clip-respecting stencil funcs to normal stencil funcs
774// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000775static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000776 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
777 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
778 // In the Clip Funcs
779 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
780 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
781 kLess_StencilFunc, // kLessIfInClip_StencilFunc
782 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
783 // Special in the clip func that forces user's ref to be 0.
784 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
785 // make ref 0 and do normal nequal.
786 },
787 {// Stencil-Clipping is ENABLED
788 // In the Clip Funcs
789 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
790 // eq stencil clip bit, mask
791 // out user bits.
792
793 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
794 // add stencil bit to mask and ref
795
796 kLess_StencilFunc, // kLessIfInClip_StencilFunc
797 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
798 // for both of these we can add
799 // the clip bit to the mask and
800 // ref and compare as normal
801 // Special in the clip func that forces user's ref to be 0.
802 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
803 // make ref have only the clip bit set
804 // and make comparison be less
805 // 10..0 < 1..user_bits..
806 }
807};
808
bsalomon@google.coma3201942012-06-21 19:58:20 +0000809namespace {
810// Sets the settings to clip against the stencil buffer clip while ignoring the
811// client bits.
812const GrStencilSettings& basic_apply_stencil_clip_settings() {
813 // stencil settings to use when clip is in stencil
814 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
815 kKeep_StencilOp,
816 kKeep_StencilOp,
817 kAlwaysIfInClip_StencilFunc,
818 0x0000,
819 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000820 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000821 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
822}
823}
824
825void GrClipMaskManager::setGpuStencil() {
826 // We make two copies of the StencilSettings here (except in the early
827 // exit scenario. One copy from draw state to the stack var. Then another
828 // from the stack var to the gpu. We could make this class hold a ptr to
829 // GrGpu's fStencilSettings and eliminate the stack copy here.
830
831 const GrDrawState& drawState = fGpu->getDrawState();
832
833 // use stencil for clipping if clipping is enabled and the clip
834 // has been written into the stencil.
835 GrClipMaskManager::StencilClipMode clipMode;
836 if (this->isClipInStencil() && drawState.isClipState()) {
837 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
838 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000839 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000840 GrGpu::kModifyStencilClip_StateBit));
841 } else if (drawState.isStateFlagEnabled(
842 GrGpu::kModifyStencilClip_StateBit)) {
843 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
844 } else {
845 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
846 }
847
848 GrStencilSettings settings;
849 // The GrGpu client may not be using the stencil buffer but we may need to
850 // enable it in order to respect a stencil clip.
851 if (drawState.getStencil().isDisabled()) {
852 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
853 settings = basic_apply_stencil_clip_settings();
854 } else {
855 fGpu->disableStencil();
856 return;
857 }
858 } else {
859 settings = drawState.getStencil();
860 }
861
862 // TODO: dynamically attach a stencil buffer
863 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000864 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000865 drawState.getRenderTarget()->getStencilBuffer();
866 if (NULL != stencilBuffer) {
867 stencilBits = stencilBuffer->bits();
868 }
869
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000870 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
871 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000872 this->adjustStencilParams(&settings, clipMode, stencilBits);
873 fGpu->setStencilSettings(settings);
874}
875
876void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
877 StencilClipMode mode,
878 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000879 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000880
881 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000882 // We assume that this clip manager itself is drawing to the GrGpu and
883 // has already setup the correct values.
884 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000885 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000886
bsalomon@google.com411dad02012-06-05 20:24:20 +0000887 unsigned int clipBit = (1 << (stencilBitCnt - 1));
888 unsigned int userBits = clipBit - 1;
889
bsalomon@google.coma3201942012-06-21 19:58:20 +0000890 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000891 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000892
bsalomon@google.coma3201942012-06-21 19:58:20 +0000893 bool finished = false;
894 while (!finished) {
895 GrStencilFunc func = settings->func(face);
896 uint16_t writeMask = settings->writeMask(face);
897 uint16_t funcMask = settings->funcMask(face);
898 uint16_t funcRef = settings->funcRef(face);
899
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000900 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000901
902 writeMask &= userBits;
903
904 if (func >= kBasicStencilFuncCount) {
905 int respectClip = kRespectClip_StencilClipMode == mode;
906 if (respectClip) {
907 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000908 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000909 switch (func) {
910 case kAlwaysIfInClip_StencilFunc:
911 funcMask = clipBit;
912 funcRef = clipBit;
913 break;
914 case kEqualIfInClip_StencilFunc:
915 case kLessIfInClip_StencilFunc:
916 case kLEqualIfInClip_StencilFunc:
917 funcMask = (funcMask & userBits) | clipBit;
918 funcRef = (funcRef & userBits) | clipBit;
919 break;
920 case kNonZeroIfInClip_StencilFunc:
921 funcMask = (funcMask & userBits) | clipBit;
922 funcRef = clipBit;
923 break;
924 default:
925 GrCrash("Unknown stencil func");
926 }
927 } else {
928 funcMask &= userBits;
929 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000930 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000931 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000932 gSpecialToBasicStencilFunc[respectClip];
933 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000934 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000935 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000936 funcMask &= userBits;
937 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000938 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939
940 settings->setFunc(face, func);
941 settings->setWriteMask(face, writeMask);
942 settings->setFuncMask(face, funcMask);
943 settings->setFuncRef(face, funcRef);
944
945 if (GrStencilSettings::kFront_Face == face) {
946 face = GrStencilSettings::kBack_Face;
947 finished = !twoSided;
948 } else {
949 finished = true;
950 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000951 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000952 if (!twoSided) {
953 settings->copyFrontSettingsToBack();
954 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000955}
956
957////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000958GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000959 GrReducedClip::InitialState initialState,
960 const GrReducedClip::ElementList& elements,
961 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000962 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000963
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000964 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000965 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000966 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000967 }
968
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000969 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000970 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000971 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000972 }
973
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000974 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
975 // the top left corner of the resulting rect to the top left of the texture.
976 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
977
robertphillips@google.com2c756812012-05-22 20:28:23 +0000978 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000979
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000980 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000981 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
982 SkIntToScalar(-clipSpaceIBounds.fTop));
983 helper.init(maskSpaceIBounds, &matrix);
984
985 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000986
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000987 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000988
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000989 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000990
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000991 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000992 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000993
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000994 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
995 // Intersect and reverse difference require modifying pixels outside of the geometry
996 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
997 // but leave the pixels inside the geometry alone. For reverse difference we invert all
998 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000999 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +00001000 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001001 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001002 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001003 }
1004
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001005 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001006 // convert the rect to a path so we can invert the fill
1007 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001008 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001009 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001010
sugoi@google.com12b4e272012-12-06 20:13:11 +00001011 helper.draw(temp, stroke, SkRegion::kReplace_Op,
1012 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001013 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001014 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001015 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001016 SkPath clipPath = element->getPath();
1017 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +00001018 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001019 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001020 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001021 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001022 }
1023
1024 continue;
1025 }
1026
1027 // The other ops (union, xor, diff) only affect pixels inside
1028 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001029 if (Element::kRect_Type == element->getType()) {
1030 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1031 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001032 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001033 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001034 }
1035 }
1036
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001037 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001038
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001039 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001040 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001041}
1042
robertphillips@google.comf294b772012-04-27 14:29:26 +00001043////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001044void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001045 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001046}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001047
1048void GrClipMaskManager::setGpu(GrGpu* gpu) {
1049 fGpu = gpu;
1050 fAACache.setContext(gpu->getContext());
1051}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001052
1053void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1054 const GrDrawState& drawState = fGpu->getDrawState();
1055 GrClipMaskManager::StencilClipMode clipMode;
1056 if (this->isClipInStencil() && drawState.isClipState()) {
1057 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1058 // We can't be modifying the clip and respecting it at the same time.
1059 SkASSERT(!drawState.isStateFlagEnabled(
1060 GrGpu::kModifyStencilClip_StateBit));
1061 } else if (drawState.isStateFlagEnabled(
1062 GrGpu::kModifyStencilClip_StateBit)) {
1063 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1064 } else {
1065 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1066 }
1067
1068 // TODO: dynamically attach a stencil buffer
1069 int stencilBits = 0;
1070 GrStencilBuffer* stencilBuffer =
1071 drawState.getRenderTarget()->getStencilBuffer();
1072 if (NULL != stencilBuffer) {
1073 stencilBits = stencilBuffer->bits();
1074 this->adjustStencilParams(settings, clipMode, stencilBits);
1075 }
1076}