blob: a596088ccfa95a7ec09585ef1d996e8e3bd1eca2 [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"
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000010#include "effects/GrTextureDomainEffect.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000011#include "GrGpu.h"
12#include "GrRenderTarget.h"
13#include "GrStencilBuffer.h"
14#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000015#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000016#include "SkRasterClip.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000017#include "SkStrokeRec.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000018#include "GrAAConvexPathRenderer.h"
19#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000021#include "GrCacheID.h"
22
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000023#include "SkTLazy.h"
24
robertphillips@google.com46a86002012-08-08 10:42:44 +000025GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000026
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.coma72eef32012-05-01 17:22:59 +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
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037void setup_drawstate_aaclip(GrGpu* gpu,
38 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000039 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 GrDrawState* drawState = gpu->drawState();
41 GrAssert(drawState);
42
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000043 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044
bsalomon@google.comb9086a02012-11-01 18:02:54 +000045 SkMatrix mat;
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.com08283af2012-10-26 13:01:20 +000051 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052
53 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000054 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000055 drawState->stage(kMaskStage)->setEffect(
56 GrTextureDomainEffect::Create(result,
57 mat,
58 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
59 GrTextureDomainEffect::kDecal_WrapMode))->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
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000112bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000113 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000114
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000115 ElementList elements(16);
116 InitialState initialState;
117 SkIRect clipSpaceIBounds;
118 bool requiresAA;
119 bool isRect = false;
120
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000121 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000122
123 const GrRenderTarget* rt = drawState->getRenderTarget();
124 // GrDrawTarget should have filtered this for us
125 GrAssert(NULL != rt);
126
127 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
128
129 if (!ignoreClip) {
130 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
131 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
132 ReduceClipStack(*clipDataIn->fClipStack,
133 clipSpaceRTIBounds,
134 &elements,
135 &initialState,
136 &clipSpaceIBounds,
137 &requiresAA);
138 if (elements.isEmpty()) {
139 if (kAllIn_InitialState == initialState) {
140 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
141 isRect = true;
142 } else {
143 return false;
144 }
145 }
146 }
147
148 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000149 fGpu->disableScissor();
150 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000151 return true;
152 }
153
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000154#if GR_AA_CLIP
155 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000156
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000157 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000158 if (0 == rt->numSamples() && requiresAA) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000159 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000160 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000161
162 if (this->useSWOnlyPath(elements)) {
163 // The clip geometry is complex enough that it will be more efficient to create it
164 // entirely in software
165 result = this->createSoftwareClipMask(genID,
166 initialState,
167 elements,
168 clipSpaceIBounds);
169 } else {
170 result = this->createAlphaClipMask(genID,
171 initialState,
172 elements,
173 clipSpaceIBounds);
174 }
175
176 if (NULL != result) {
177 // The mask's top left coord should be pinned to the rounded-out top left corner of
178 // clipSpace bounds. We determine the mask's position WRT to the render target here.
179 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
180 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
181 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000182 fGpu->disableScissor();
183 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000184 return true;
185 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000186 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000187 }
188#endif // GR_AA_CLIP
189
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000190 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
191 // be created. In either case, free up the texture in the anti-aliased mask cache.
192 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
193 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
194 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000195 fAACache.reset();
196
bsalomon@google.coma3201942012-06-21 19:58:20 +0000197 // If the clip is a rectangle then just set the scissor. Otherwise, create
198 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000199 if (isRect) {
200 SkIRect clipRect = clipSpaceIBounds;
201 clipRect.offset(-clipDataIn->fOrigin);
202 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000203 this->setGpuStencil();
204 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000205 }
206
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000207 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000208 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
209 this->createStencilClipMask(initialState,
210 elements,
211 clipSpaceIBounds,
212 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000213
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000214 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
215 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
216 // use both stencil and scissor test to the bounds for the final draw.
217 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
218 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
219 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000220 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000221 return true;
222}
223
224#define VISUALIZE_COMPLEX_CLIP 0
225
226#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000227 #include "SkRandom.h"
228 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000229 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
230#else
231 #define SET_RANDOM_COLOR
232#endif
233
234namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000235
236////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000237// set up the OpenGL blend function to perform the specified
238// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000239void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000240
241 switch (op) {
242 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000243 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000244 break;
245 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000246 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000247 break;
248 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000249 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000250 break;
251 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000252 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000253 break;
254 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000255 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000256 break;
257 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000258 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000259 break;
260 default:
261 GrAssert(false);
262 break;
263 }
264}
265
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000266////////////////////////////////////////////////////////////////////////////////
267bool draw_path_in_software(GrContext* context,
268 GrGpu* gpu,
269 const SkPath& path,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000270 bool doAA,
271 const GrIRect& resultBounds) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000272 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000273
274 SkAutoTUnref<GrTexture> texture(
275 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000276 rec,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000277 resultBounds,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000278 doAA, NULL));
279 if (NULL == texture) {
280 return false;
281 }
282
283 // The ClipMaskManager accumulates the clip mask in the UL corner
284 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
285
286 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
287
sugoi@google.com12b4e272012-12-06 20:13:11 +0000288 GrAssert(!path.isInverseFillType());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000289 return true;
290}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000291}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000292
293////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000294bool GrClipMaskManager::drawElement(GrTexture* target,
295 const SkClipStack::Element* element,
296 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000297 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000298
299 drawState->setRenderTarget(target->asRenderTarget());
300
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000301 switch (element->getType()) {
302 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000303 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
304 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000305 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000306 getContext()->getAARectRenderer()->fillAARect(fGpu,
307 fGpu,
308 element->getRect(),
309 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000310 } else {
311 fGpu->drawSimpleRect(element->getRect(), NULL);
312 }
313 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000314 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000315 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
316 if (path->isInverseFillType()) {
317 path.writable()->toggleInverseFillType();
318 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000319 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000320 if (NULL == pr) {
321 GrPathRendererChain::DrawType type;
322 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
323 GrPathRendererChain::kColor_DrawType;
324 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
325 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 if (NULL == pr) {
327 return false;
328 }
329 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
330 break;
331 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000332 default:
333 // something is wrong if we're trying to draw an empty element.
334 GrCrash("Unexpected element type");
335 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000336 }
337 return true;
338}
339
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000340bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
341 const SkClipStack::Element* element,
342 GrPathRenderer** pr) {
343 GrDrawState* drawState = fGpu->drawState();
344 drawState->setRenderTarget(target->asRenderTarget());
345
346 switch (element->getType()) {
347 case Element::kRect_Type:
348 return true;
349 case Element::kPath_Type: {
350 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
351 if (path->isInverseFillType()) {
352 path.writable()->toggleInverseFillType();
353 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000354 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000355 GrPathRendererChain::DrawType type = element->isAA() ?
356 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
357 GrPathRendererChain::kStencilAndColor_DrawType;
358 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
359 return NULL != *pr;
360 }
361 default:
362 // something is wrong if we're trying to draw an empty element.
363 GrCrash("Unexpected element type");
364 return false;
365 }
366}
367
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000368void GrClipMaskManager::mergeMask(GrTexture* dstMask,
369 GrTexture* srcMask,
370 SkRegion::Op op,
371 const GrIRect& dstBound,
372 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000373 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000374 SkMatrix oldMatrix = drawState->getViewMatrix();
375 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000377 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000378
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000379 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000380
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000381 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000382 sampleM.setIDiv(srcMask->width(), srcMask->height());
383 drawState->stage(0)->setEffect(
384 GrTextureDomainEffect::Create(srcMask,
385 sampleM,
386 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
387 GrTextureDomainEffect::kDecal_WrapMode))->unref();
388 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389
tomhudson@google.com676e6602012-07-10 17:21:48 +0000390 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000391 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392}
393
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000394// get a texture to act as a temporary buffer for AA clip boolean operations
395// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000396void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000397 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000398 // we've already allocated the temp texture
399 return;
400 }
401
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000402 GrTextureDesc desc;
403 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000404 desc.fWidth = width;
405 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000406 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000407
robertphillips@google.com2c756812012-05-22 20:28:23 +0000408 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000409}
410
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000411////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000412// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
413// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
414// hit)
415bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
416 const SkIRect& clipSpaceIBounds,
417 GrTexture** result) {
418 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
419 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000420
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000421 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
422 // Since we are setting up the cache we know the last lookup was a miss. Free up the
423 // currently cached mask so it can be reused.
424 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000425
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000426 GrTextureDesc desc;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000427 desc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000428 desc.fWidth = clipSpaceIBounds.width();
429 desc.fHeight = clipSpaceIBounds.height();
430 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000433 }
434
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000435 *result = fAACache.getLastMask();
436 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000437}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000439////////////////////////////////////////////////////////////////////////////////
440// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000441GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
442 InitialState initialState,
443 const ElementList& elements,
444 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000445 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
446
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000447 GrTexture* result;
448 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000449 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000450 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000451 }
452
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000453 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000454 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000455 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000456 }
457
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000458 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
459 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000460
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000461 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000462
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000464 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000465 SkIntToScalar(-clipSpaceIBounds.fLeft),
466 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000467 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000468 // The texture may be larger than necessary, this rect represents the part of the texture
469 // we populate with a rasterization of the clip.
470 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
471
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000472 // We're drawing a coverage mask and want coverage to be run through the blend function.
473 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
474
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000475 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000476 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000477
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000478 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
479 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000480 fGpu->clear(&maskSpaceIBounds,
481 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
482 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000483
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000484 // When we use the stencil in the below loop it is important to have this clip installed.
485 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
486 // pass must not set values outside of this bounds or stencil values outside the rect won't be
487 // cleared.
488 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
489 drawState->enableState(GrDrawState::kClip_StateBit);
490
robertphillips@google.comf105b102012-05-14 12:18:26 +0000491 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000492 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000493 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000494 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000495 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000496 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000498 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
499 GrPathRenderer* pr = NULL;
500 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
501 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000502 // 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 +0000503 // mask buffer can be substantially larger than the actually clip stack element. We
504 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000505 // the accumulator.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000506 GrIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000507
508 if (useTemp) {
509 if (invert) {
510 maskSpaceElementIBounds = maskSpaceIBounds;
511 } else {
512 GrRect elementBounds = element->getBounds();
513 elementBounds.offset(clipToMaskOffset);
514 elementBounds.roundOut(&maskSpaceElementIBounds);
515 }
516
517 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
518 if (NULL == temp.texture()) {
519 fAACache.reset();
520 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000521 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000522 dst = temp.texture();
523 // clear the temp target and set blend to replace
524 fGpu->clear(&maskSpaceElementIBounds,
525 invert ? 0xffffffff : 0x00000000,
526 dst->asRenderTarget());
527 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000528
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000529 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000530 // draw directly into the result with the stencil set to make the pixels affected
531 // by the clip shape be non-zero.
532 dst = result;
533 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
534 kReplace_StencilOp,
535 kReplace_StencilOp,
536 kAlways_StencilFunc,
537 0xffff,
538 0xffff,
539 0xffff);
540 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000541 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000542 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000543
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000544 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000545
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000546 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000547 fAACache.reset();
548 return NULL;
549 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000550
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000551 if (useTemp) {
552 // Now draw into the accumulator using the real operation and the temp buffer as a
553 // texture
554 this->mergeMask(result,
555 temp.texture(),
556 op,
557 maskSpaceIBounds,
558 maskSpaceElementIBounds);
559 } else {
560 // Draw to the exterior pixels (those with a zero stencil value).
561 drawState->setAlpha(invert ? 0xff : 0x00);
562 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
563 kZero_StencilOp,
564 kZero_StencilOp,
565 kEqual_StencilFunc,
566 0xffff,
567 0x0000,
568 0xffff);
569 drawState->setStencil(kDrawOutsideElement);
570 fGpu->drawSimpleRect(clipSpaceIBounds);
571 drawState->disableStencil();
572 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000573 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000574 // all the remaining ops can just be directly draw into the accumulation buffer
575 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000576 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000577 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000578 }
579 }
580
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000581 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000582 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000583}
584
585////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000586// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000587// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000588bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
589 const ElementList& elements,
590 const SkIRect& clipSpaceIBounds,
591 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000592
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000593 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000594
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000595 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000596 GrAssert(drawState->isClipState());
597
598 GrRenderTarget* rt = drawState->getRenderTarget();
599 GrAssert(NULL != rt);
600
601 // TODO: dynamically attach a SB when needed.
602 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
603 if (NULL == stencilBuffer) {
604 return false;
605 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000606 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000607
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000608 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000610 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000611
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000612 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
613 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000614 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000615 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000616
bsalomon@google.com9f131742012-12-13 20:43:56 +0000617 // We set the current clip to the bounds so that our recursive draws are scissored to them.
618 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
619 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
620 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
621 drawState->enableState(GrDrawState::kClip_StateBit);
622
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000623 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
624 SkVector translate = {
625 SkIntToScalar(clipSpaceToStencilOffset.fX),
626 SkIntToScalar(clipSpaceToStencilOffset.fY)
627 };
628 drawState->viewMatrix()->setTranslate(translate);
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000629
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000630#if !VISUALIZE_COMPLEX_CLIP
631 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
632#endif
633
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000634 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000635 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000636 clipBit = (1 << (clipBit-1));
637
robertphillips@google.com7b112892012-07-31 15:18:21 +0000638 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000640 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641
642 // walk through each clip element and perform its set op
643 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000644 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
645 const Element* element = iter.get();
sugoi@google.com12b4e272012-12-06 20:13:11 +0000646 SkPath::FillType fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000647 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000648 // enabled at bottom of loop
649 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000650 // if the target is MSAA then we want MSAA enabled when the clip is soft
651 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000652 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000653 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000654
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000655 // This will be used to determine whether the clip shape can be rendered into the
656 // stencil with arbitrary stencil settings.
657 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000658
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000659 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000660
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000661 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000664 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000665 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000666 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000667 fill = SkPath::kEvenOdd_FillType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000668 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000669 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000670 GrAssert(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000671 clipPath.init(element->getPath());
672 fill = clipPath->getFillType();
673 fillInverted = clipPath->isInverseFillType();
674 if (fillInverted) {
675 clipPath.writable()->toggleInverseFillType();
676 fill = clipPath->getFillType();
677 }
678 pr = this->getContext()->getPathRenderer(*clipPath,
679 stroke,
680 fGpu,
681 false,
682 GrPathRendererChain::kStencilOnly_DrawType,
683 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000684 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000685 return false;
686 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687 }
688
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 int passes;
690 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
691
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000692 bool canRenderDirectToStencil =
693 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000695 // fill rule, and set operation can
696 // we render the element directly to
697 // stencil bit used for clipping.
698 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
699 canRenderDirectToStencil,
700 clipBit,
701 fillInverted,
702 &passes,
703 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704
705 // draw the element to the client stencil bits if necessary
706 if (!canDrawDirectToClip) {
707 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000708 kIncClamp_StencilOp,
709 kIncClamp_StencilOp,
710 kAlways_StencilFunc,
711 0xffff,
712 0x0000,
713 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000715 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000717 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000719 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 if (canRenderDirectToStencil) {
721 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000722 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 } else {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000724 pr->stencilPath(*clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 }
726 }
727 }
728
729 // now we modify the clip bit by rendering either the clip
730 // element directly or a bounding rect of the entire clip.
731 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
732 for (int p = 0; p < passes; ++p) {
733 *drawState->stencil() = stencilSettings[p];
734 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000735 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000737 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000739 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000741 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742 }
743 } else {
744 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000745 // The view matrix is setup to do clip space -> stencil space translation, so
746 // draw rect in clip space.
747 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000748 }
749 }
750 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000752 // set this last because recursive draws may overwrite it back to kNone.
753 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
754 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755 return true;
756}
757
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000758
bsalomon@google.com411dad02012-06-05 20:24:20 +0000759// mapping of clip-respecting stencil funcs to normal stencil funcs
760// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000761static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000762 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
763 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
764 // In the Clip Funcs
765 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
766 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
767 kLess_StencilFunc, // kLessIfInClip_StencilFunc
768 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
769 // Special in the clip func that forces user's ref to be 0.
770 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
771 // make ref 0 and do normal nequal.
772 },
773 {// Stencil-Clipping is ENABLED
774 // In the Clip Funcs
775 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
776 // eq stencil clip bit, mask
777 // out user bits.
778
779 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
780 // add stencil bit to mask and ref
781
782 kLess_StencilFunc, // kLessIfInClip_StencilFunc
783 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
784 // for both of these we can add
785 // the clip bit to the mask and
786 // ref and compare as normal
787 // Special in the clip func that forces user's ref to be 0.
788 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
789 // make ref have only the clip bit set
790 // and make comparison be less
791 // 10..0 < 1..user_bits..
792 }
793};
794
bsalomon@google.coma3201942012-06-21 19:58:20 +0000795namespace {
796// Sets the settings to clip against the stencil buffer clip while ignoring the
797// client bits.
798const GrStencilSettings& basic_apply_stencil_clip_settings() {
799 // stencil settings to use when clip is in stencil
800 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
801 kKeep_StencilOp,
802 kKeep_StencilOp,
803 kAlwaysIfInClip_StencilFunc,
804 0x0000,
805 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000806 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000807 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
808}
809}
810
811void GrClipMaskManager::setGpuStencil() {
812 // We make two copies of the StencilSettings here (except in the early
813 // exit scenario. One copy from draw state to the stack var. Then another
814 // from the stack var to the gpu. We could make this class hold a ptr to
815 // GrGpu's fStencilSettings and eliminate the stack copy here.
816
817 const GrDrawState& drawState = fGpu->getDrawState();
818
819 // use stencil for clipping if clipping is enabled and the clip
820 // has been written into the stencil.
821 GrClipMaskManager::StencilClipMode clipMode;
822 if (this->isClipInStencil() && drawState.isClipState()) {
823 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
824 // We can't be modifying the clip and respecting it at the same time.
825 GrAssert(!drawState.isStateFlagEnabled(
826 GrGpu::kModifyStencilClip_StateBit));
827 } else if (drawState.isStateFlagEnabled(
828 GrGpu::kModifyStencilClip_StateBit)) {
829 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
830 } else {
831 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
832 }
833
834 GrStencilSettings settings;
835 // The GrGpu client may not be using the stencil buffer but we may need to
836 // enable it in order to respect a stencil clip.
837 if (drawState.getStencil().isDisabled()) {
838 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
839 settings = basic_apply_stencil_clip_settings();
840 } else {
841 fGpu->disableStencil();
842 return;
843 }
844 } else {
845 settings = drawState.getStencil();
846 }
847
848 // TODO: dynamically attach a stencil buffer
849 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000850 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000851 drawState.getRenderTarget()->getStencilBuffer();
852 if (NULL != stencilBuffer) {
853 stencilBits = stencilBuffer->bits();
854 }
855
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000856 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() || !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000857 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000858 this->adjustStencilParams(&settings, clipMode, stencilBits);
859 fGpu->setStencilSettings(settings);
860}
861
862void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
863 StencilClipMode mode,
864 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000865 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000866
867 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000868 // We assume that this clip manager itself is drawing to the GrGpu and
869 // has already setup the correct values.
870 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000871 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000872
bsalomon@google.com411dad02012-06-05 20:24:20 +0000873 unsigned int clipBit = (1 << (stencilBitCnt - 1));
874 unsigned int userBits = clipBit - 1;
875
bsalomon@google.coma3201942012-06-21 19:58:20 +0000876 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000877 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000878
bsalomon@google.coma3201942012-06-21 19:58:20 +0000879 bool finished = false;
880 while (!finished) {
881 GrStencilFunc func = settings->func(face);
882 uint16_t writeMask = settings->writeMask(face);
883 uint16_t funcMask = settings->funcMask(face);
884 uint16_t funcRef = settings->funcRef(face);
885
886 GrAssert((unsigned) func < kStencilFuncCount);
887
888 writeMask &= userBits;
889
890 if (func >= kBasicStencilFuncCount) {
891 int respectClip = kRespectClip_StencilClipMode == mode;
892 if (respectClip) {
893 // The GrGpu class should have checked this
894 GrAssert(this->isClipInStencil());
895 switch (func) {
896 case kAlwaysIfInClip_StencilFunc:
897 funcMask = clipBit;
898 funcRef = clipBit;
899 break;
900 case kEqualIfInClip_StencilFunc:
901 case kLessIfInClip_StencilFunc:
902 case kLEqualIfInClip_StencilFunc:
903 funcMask = (funcMask & userBits) | clipBit;
904 funcRef = (funcRef & userBits) | clipBit;
905 break;
906 case kNonZeroIfInClip_StencilFunc:
907 funcMask = (funcMask & userBits) | clipBit;
908 funcRef = clipBit;
909 break;
910 default:
911 GrCrash("Unknown stencil func");
912 }
913 } else {
914 funcMask &= userBits;
915 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000916 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000917 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000918 gSpecialToBasicStencilFunc[respectClip];
919 func = table[func - kBasicStencilFuncCount];
920 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000921 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000922 funcMask &= userBits;
923 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000924 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000925
926 settings->setFunc(face, func);
927 settings->setWriteMask(face, writeMask);
928 settings->setFuncMask(face, funcMask);
929 settings->setFuncRef(face, funcRef);
930
931 if (GrStencilSettings::kFront_Face == face) {
932 face = GrStencilSettings::kBack_Face;
933 finished = !twoSided;
934 } else {
935 finished = true;
936 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000937 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000938 if (!twoSided) {
939 settings->copyFrontSettingsToBack();
940 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000941}
942
943////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000944GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
945 GrReducedClip::InitialState initialState,
946 const GrReducedClip::ElementList& elements,
947 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000948 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000949
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000950 GrTexture* result;
951 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
952 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000953 }
954
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000955 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000956 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000957 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000958 }
959
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000960 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
961 // the top left corner of the resulting rect to the top left of the texture.
962 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
963
robertphillips@google.com2c756812012-05-22 20:28:23 +0000964 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000965
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000966 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000967 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
968 SkIntToScalar(-clipSpaceIBounds.fTop));
969 helper.init(maskSpaceIBounds, &matrix);
970
971 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000972
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000973 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000974
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000975 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000976
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000977 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000978 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000979
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000980 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
981 // Intersect and reverse difference require modifying pixels outside of the geometry
982 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
983 // but leave the pixels inside the geometry alone. For reverse difference we invert all
984 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000985 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000986 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000987 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000988 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000989 }
990
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000991 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000992 // convert the rect to a path so we can invert the fill
993 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000994 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000995 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000996
sugoi@google.com12b4e272012-12-06 20:13:11 +0000997 helper.draw(temp, stroke, SkRegion::kReplace_Op,
998 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000999 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001000 } else {
1001 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001002 SkPath clipPath = element->getPath();
1003 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +00001004 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001005 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001006 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001007 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001008 }
1009
1010 continue;
1011 }
1012
1013 // The other ops (union, xor, diff) only affect pixels inside
1014 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001015 if (Element::kRect_Type == element->getType()) {
1016 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1017 } else {
1018 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001019 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001020 }
1021 }
1022
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001023 helper.toTexture(result, kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001024
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001025 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001026 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001027}
1028
robertphillips@google.comf294b772012-04-27 14:29:26 +00001029////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001030void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001031 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001032}