blob: d076ced2811d6fca2c6966b95d17ee9372c0866c [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.com12b4e272012-12-06 20:13:11 +000017#include "SkStroke.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
23GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000024
robertphillips@google.comba998f22012-10-12 11:33:56 +000025#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000026
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000029using namespace GrReducedClip;
30
bsalomon@google.com51a62862012-11-26 21:19:43 +000031////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000032namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000034// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035void setup_drawstate_aaclip(GrGpu* gpu,
36 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000037 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000038 GrDrawState* drawState = gpu->drawState();
39 GrAssert(drawState);
40
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000041 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042
bsalomon@google.comb9086a02012-11-01 18:02:54 +000043 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000045 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000046 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047 mat.preConcat(drawState->getViewMatrix());
48
bsalomon@google.com08283af2012-10-26 13:01:20 +000049 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000050
51 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.com7b7cdd12012-11-07 16:17:24 +000053 drawState->stage(kMaskStage)->setEffect(
54 GrTextureDomainEffect::Create(result,
55 mat,
56 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
57 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000058}
59
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000060bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000061 GrGpu* gpu,
62 const SkPath& path,
sugoi@google.com12b4e272012-12-06 20:13:11 +000063 const SkStroke& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000064 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000065 // last (false) parameter disallows use of the SW path renderer
sugoi@google.com12b4e272012-12-06 20:13:11 +000066 return NULL == context->getPathRenderer(path, stroke, gpu, doAA, false);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000067}
68
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000069}
70
robertphillips@google.comfa662942012-05-17 12:20:22 +000071/*
72 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
73 * will be used on any element. If so, it returns true to indicate that the
74 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
75 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000076bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000077
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000078 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000079 // a clip gets complex enough it can just be done in SW regardless
80 // of whether it would invoke the GrSoftwarePathRenderer.
81 bool useSW = false;
sugoi@google.com12b4e272012-12-06 20:13:11 +000082 SkStroke stroke;
83 stroke.setDoFill(true);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000084
85 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
86 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000087 // rects can always be drawn directly w/o using the software path
88 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000089 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000091 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +000092 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000093 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000094 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +000095 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000096 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000097 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000098}
99
robertphillips@google.comf294b772012-04-27 14:29:26 +0000100////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000101// sort out what kind of clip mask needs to be created: alpha, stencil,
102// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000103bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000104 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000105
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000106 ElementList elements(16);
107 InitialState initialState;
108 SkIRect clipSpaceIBounds;
109 bool requiresAA;
110 bool isRect = false;
111
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000112 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000113
114 const GrRenderTarget* rt = drawState->getRenderTarget();
115 // GrDrawTarget should have filtered this for us
116 GrAssert(NULL != rt);
117
118 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
119
120 if (!ignoreClip) {
121 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
122 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
123 ReduceClipStack(*clipDataIn->fClipStack,
124 clipSpaceRTIBounds,
125 &elements,
126 &initialState,
127 &clipSpaceIBounds,
128 &requiresAA);
129 if (elements.isEmpty()) {
130 if (kAllIn_InitialState == initialState) {
131 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
132 isRect = true;
133 } else {
134 return false;
135 }
136 }
137 }
138
139 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000140 fGpu->disableScissor();
141 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000142 return true;
143 }
144
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000145#if GR_AA_CLIP
146 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000147
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000148 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000149 if (0 == rt->numSamples() && requiresAA) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000150 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000151 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000152
153 if (this->useSWOnlyPath(elements)) {
154 // The clip geometry is complex enough that it will be more efficient to create it
155 // entirely in software
156 result = this->createSoftwareClipMask(genID,
157 initialState,
158 elements,
159 clipSpaceIBounds);
160 } else {
161 result = this->createAlphaClipMask(genID,
162 initialState,
163 elements,
164 clipSpaceIBounds);
165 }
166
167 if (NULL != result) {
168 // The mask's top left coord should be pinned to the rounded-out top left corner of
169 // clipSpace bounds. We determine the mask's position WRT to the render target here.
170 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
171 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
172 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000173 fGpu->disableScissor();
174 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000175 return true;
176 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000177 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000178 }
179#endif // GR_AA_CLIP
180
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000181 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
182 // be created. In either case, free up the texture in the anti-aliased mask cache.
183 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
184 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
185 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000186 fAACache.reset();
187
bsalomon@google.coma3201942012-06-21 19:58:20 +0000188 // If the clip is a rectangle then just set the scissor. Otherwise, create
189 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000190 if (isRect) {
191 SkIRect clipRect = clipSpaceIBounds;
192 clipRect.offset(-clipDataIn->fOrigin);
193 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000194 this->setGpuStencil();
195 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000196 }
197
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000198 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000199 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
200 this->createStencilClipMask(initialState,
201 elements,
202 clipSpaceIBounds,
203 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000204
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000205 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
206 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
207 // use both stencil and scissor test to the bounds for the final draw.
208 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
209 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
210 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000211 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000212 return true;
213}
214
215#define VISUALIZE_COMPLEX_CLIP 0
216
217#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000218 #include "SkRandom.h"
219 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000220 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
221#else
222 #define SET_RANDOM_COLOR
223#endif
224
225namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000226
227////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000228// set up the OpenGL blend function to perform the specified
229// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000230void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000231
232 switch (op) {
233 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000234 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000235 break;
236 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000237 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000238 break;
239 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000240 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000241 break;
242 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000243 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000244 break;
245 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000246 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000247 break;
248 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000249 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000250 break;
251 default:
252 GrAssert(false);
253 break;
254 }
255}
256
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000257////////////////////////////////////////////////////////////////////////////////
258bool draw_path_in_software(GrContext* context,
259 GrGpu* gpu,
260 const SkPath& path,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000261 bool doAA,
262 const GrIRect& resultBounds) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000263 SkStroke stroke;
264 stroke.setDoFill(true);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000265
266 SkAutoTUnref<GrTexture> texture(
267 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000268 stroke,
269 resultBounds,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000270 doAA, NULL));
271 if (NULL == texture) {
272 return false;
273 }
274
275 // The ClipMaskManager accumulates the clip mask in the UL corner
276 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
277
278 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
279
sugoi@google.com12b4e272012-12-06 20:13:11 +0000280 GrAssert(!path.isInverseFillType());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000281 return true;
282}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000283}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000284
285////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000286bool GrClipMaskManager::drawClipShape(GrTexture* target, const SkClipStack::Element* element) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000287 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000288 GrAssert(NULL != drawState);
289
290 drawState->setRenderTarget(target->asRenderTarget());
291
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000292 switch (element->getType()) {
293 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000294 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
295 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000296 if (element->isAA()) {
297 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu, element->getRect(), true);
298 } else {
299 fGpu->drawSimpleRect(element->getRect(), NULL);
300 }
301 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000302 case Element::kPath_Type: {
303 SkStroke stroke;
304 stroke.setDoFill(true);
305 GrPathRenderer* pr = this->getContext()->getPathRenderer(element->getPath(),
306 stroke,
307 fGpu,
308 element->isAA(), false);
309 if (NULL == pr) {
310 return false;
311 }
312 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
313 break;
314 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000315 default:
316 // something is wrong if we're trying to draw an empty element.
317 GrCrash("Unexpected element type");
318 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000319 }
320 return true;
321}
322
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000323void GrClipMaskManager::mergeMask(GrTexture* dstMask,
324 GrTexture* srcMask,
325 SkRegion::Op op,
326 const GrIRect& dstBound,
327 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000328 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000329 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000330 SkMatrix oldMatrix = drawState->getViewMatrix();
331 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000332
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000333 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000334
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000335 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000336
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000337 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000338 sampleM.setIDiv(srcMask->width(), srcMask->height());
339 drawState->stage(0)->setEffect(
340 GrTextureDomainEffect::Create(srcMask,
341 sampleM,
342 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
343 GrTextureDomainEffect::kDecal_WrapMode))->unref();
344 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000345
tomhudson@google.com676e6602012-07-10 17:21:48 +0000346 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000347 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348}
349
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000350// get a texture to act as a temporary buffer for AA clip boolean operations
351// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000352void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000353 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000354 // we've already allocated the temp texture
355 return;
356 }
357
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000358 GrTextureDesc desc;
359 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000360 desc.fWidth = width;
361 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000362 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000363
robertphillips@google.com2c756812012-05-22 20:28:23 +0000364 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000365}
366
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000367////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000368// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
369// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
370// hit)
371bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
372 const SkIRect& clipSpaceIBounds,
373 GrTexture** result) {
374 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
375 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000376
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000377 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
378 // Since we are setting up the cache we know the last lookup was a miss. Free up the
379 // currently cached mask so it can be reused.
380 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000381
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000382 GrTextureDesc desc;
383 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
384 desc.fWidth = clipSpaceIBounds.width();
385 desc.fHeight = clipSpaceIBounds.height();
386 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000388 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000389 }
390
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000391 *result = fAACache.getLastMask();
392 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000393}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000394
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000395////////////////////////////////////////////////////////////////////////////////
396// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000397GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
398 InitialState initialState,
399 const ElementList& elements,
400 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000401 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
402
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000403 GrTexture* result;
404 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000405 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000406 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000407 }
408
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000409 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000410 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000411 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000412 }
413
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000414 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
415 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000416
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000417 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000418
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000419 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000420 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000421 SkIntToScalar(-clipSpaceIBounds.fLeft),
422 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000423 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000424 // The texture may be larger than necessary, this rect represents the part of the texture
425 // we populate with a rasterization of the clip.
426 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
427
428 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000429 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000430
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000431 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
432 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000433 fGpu->clear(&maskSpaceIBounds,
434 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
435 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000436
robertphillips@google.comf105b102012-05-14 12:18:26 +0000437 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000439 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000440
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000441 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000442 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000443
bsalomon@google.com6794a252012-11-08 15:30:53 +0000444 if (SkRegion::kReplace_Op == op) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000445 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000446 this->drawClipShape(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447
448 } else if (SkRegion::kReverseDifference_Op == op ||
449 SkRegion::kIntersect_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000450 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000451 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000452 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000453 return NULL;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000454 }
455
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000456 // 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 +0000457 // mask buffer can be substantially larger than the actually clip stack element. We
458 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000459 // the accumulator
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000460 GrIRect maskSpaceElementIBounds;
461 if (element->isInverseFilled()) {
462 maskSpaceElementIBounds = maskSpaceIBounds;
463 } else {
464 GrRect elementBounds = element->getBounds();
465 elementBounds.offset(clipToMaskOffset);
466 elementBounds.roundOut(&maskSpaceElementIBounds);
467 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000468
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000469 // clear the temp target & draw into it
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000470 fGpu->clear(&maskSpaceElementIBounds, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000471
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000472 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000473 if (!this->drawClipShape(temp.texture(), element)) {
474 fAACache.reset();
475 return NULL;
476 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000477
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000478 // Now draw into the accumulator using the real operation
479 // and the temp buffer as a texture
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000480 this->mergeMask(result, temp.texture(), op, maskSpaceIBounds, maskSpaceElementIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000481 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000482 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000483 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000484 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000485 this->drawClipShape(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000486 }
487 }
488
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000489 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000490 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000491}
492
493////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000494// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000495// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000496bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
497 const ElementList& elements,
498 const SkIRect& clipSpaceIBounds,
499 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000500
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000501 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000502
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000503 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000504 GrAssert(drawState->isClipState());
505
506 GrRenderTarget* rt = drawState->getRenderTarget();
507 GrAssert(NULL != rt);
508
509 // TODO: dynamically attach a SB when needed.
510 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
511 if (NULL == stencilBuffer) {
512 return false;
513 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000514 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000515
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000516 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000517
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000518 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000519
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000520 // we set the current clip to the bounds so that our recursive draws are scissored to them.
521 // We use the copy of the GrClipData we just stashed on the SB to render from. We set it
522 // back after we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000523 const GrClipData* oldClipData = fGpu->getClip();
524
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000525 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
526 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
527
528 SkClipStack newClipStack(stencilSpaceIBounds);
529 GrClipData newClipData; // origin defaults to (0,0)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000530 newClipData.fClipStack = &newClipStack;
531
532 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000533
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000534 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
535 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000536 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000537 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000538
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000539 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
540 SkVector translate = {
541 SkIntToScalar(clipSpaceToStencilOffset.fX),
542 SkIntToScalar(clipSpaceToStencilOffset.fY)
543 };
544 drawState->viewMatrix()->setTranslate(translate);
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000545
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000546#if !VISUALIZE_COMPLEX_CLIP
547 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
548#endif
549
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000550 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000551 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000552 clipBit = (1 << (clipBit-1));
553
robertphillips@google.com7b112892012-07-31 15:18:21 +0000554 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000555
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000556 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000557
558 // walk through each clip element and perform its set op
559 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
561 const Element* element = iter.get();
sugoi@google.com12b4e272012-12-06 20:13:11 +0000562 SkPath::FillType fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000563 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000564 // enabled at bottom of loop
565 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000566 // if the target is MSAA then we want MSAA enabled when the clip is soft
567 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000568 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000569 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000570
tomhudson@google.com8afae612012-08-14 15:03:35 +0000571 // Can the clip element be drawn directly to the stencil buffer
572 // with a non-inverted fill rule without extra passes to
573 // resolve in/out status?
574 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000575
sugoi@google.com12b4e272012-12-06 20:13:11 +0000576 SkStroke stroke;
577 stroke.setDoFill(true);
578
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000579 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000580
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000581 GrPathRenderer* pr = NULL;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000582 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000583 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000584 canRenderDirectToStencil = true;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000585 fill = SkPath::kEvenOdd_FillType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000586 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000587 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000588 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000589 clipPath = element->getPath();
590 fill = clipPath.getFillType();
591 fillInverted = clipPath.isInverseFillType();
592 fill = SkPath::NonInverseFill(fill);
593 clipPath.setFillType(fill);
594 pr = this->getContext()->getPathRenderer(clipPath, stroke, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000595 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000596 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000597 return false;
598 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000599 canRenderDirectToStencil = !pr->requiresStencilPass(clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000600 }
601
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000602 int passes;
603 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
604
605 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000606 // fill rule, and set operation can
607 // we render the element directly to
608 // stencil bit used for clipping.
609 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
610 canRenderDirectToStencil,
611 clipBit,
612 fillInverted,
613 &passes,
614 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000615
616 // draw the element to the client stencil bits if necessary
617 if (!canDrawDirectToClip) {
618 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000619 kIncClamp_StencilOp,
620 kIncClamp_StencilOp,
621 kAlways_StencilFunc,
622 0xffff,
623 0x0000,
624 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000625 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000626 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000627 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000628 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000629 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000630 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631 if (canRenderDirectToStencil) {
632 *drawState->stencil() = gDrawToStencil;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000633 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000634 } else {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000635 pr->drawPathToStencil(clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000636 }
637 }
638 }
639
640 // now we modify the clip bit by rendering either the clip
641 // element directly or a bounding rect of the entire clip.
642 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
643 for (int p = 0; p < passes; ++p) {
644 *drawState->stencil() = stencilSettings[p];
645 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000646 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000647 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000648 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000649 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000650 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000651 SET_RANDOM_COLOR
sugoi@google.com12b4e272012-12-06 20:13:11 +0000652 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000653 }
654 } else {
655 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000656 // The view matrix is setup to do clip space -> stencil space translation, so
657 // draw rect in clip space.
658 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 }
660 }
661 }
662 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000663 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000665 // set this last because recursive draws may overwrite it back to kNone.
666 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
667 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000668 return true;
669}
670
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000671
bsalomon@google.com411dad02012-06-05 20:24:20 +0000672// mapping of clip-respecting stencil funcs to normal stencil funcs
673// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000674static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000675 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
676 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
677 // In the Clip Funcs
678 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
679 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
680 kLess_StencilFunc, // kLessIfInClip_StencilFunc
681 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
682 // Special in the clip func that forces user's ref to be 0.
683 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
684 // make ref 0 and do normal nequal.
685 },
686 {// Stencil-Clipping is ENABLED
687 // In the Clip Funcs
688 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
689 // eq stencil clip bit, mask
690 // out user bits.
691
692 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
693 // add stencil bit to mask and ref
694
695 kLess_StencilFunc, // kLessIfInClip_StencilFunc
696 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
697 // for both of these we can add
698 // the clip bit to the mask and
699 // ref and compare as normal
700 // Special in the clip func that forces user's ref to be 0.
701 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
702 // make ref have only the clip bit set
703 // and make comparison be less
704 // 10..0 < 1..user_bits..
705 }
706};
707
bsalomon@google.coma3201942012-06-21 19:58:20 +0000708namespace {
709// Sets the settings to clip against the stencil buffer clip while ignoring the
710// client bits.
711const GrStencilSettings& basic_apply_stencil_clip_settings() {
712 // stencil settings to use when clip is in stencil
713 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
714 kKeep_StencilOp,
715 kKeep_StencilOp,
716 kAlwaysIfInClip_StencilFunc,
717 0x0000,
718 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000719 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000720 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
721}
722}
723
724void GrClipMaskManager::setGpuStencil() {
725 // We make two copies of the StencilSettings here (except in the early
726 // exit scenario. One copy from draw state to the stack var. Then another
727 // from the stack var to the gpu. We could make this class hold a ptr to
728 // GrGpu's fStencilSettings and eliminate the stack copy here.
729
730 const GrDrawState& drawState = fGpu->getDrawState();
731
732 // use stencil for clipping if clipping is enabled and the clip
733 // has been written into the stencil.
734 GrClipMaskManager::StencilClipMode clipMode;
735 if (this->isClipInStencil() && drawState.isClipState()) {
736 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
737 // We can't be modifying the clip and respecting it at the same time.
738 GrAssert(!drawState.isStateFlagEnabled(
739 GrGpu::kModifyStencilClip_StateBit));
740 } else if (drawState.isStateFlagEnabled(
741 GrGpu::kModifyStencilClip_StateBit)) {
742 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
743 } else {
744 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
745 }
746
747 GrStencilSettings settings;
748 // The GrGpu client may not be using the stencil buffer but we may need to
749 // enable it in order to respect a stencil clip.
750 if (drawState.getStencil().isDisabled()) {
751 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
752 settings = basic_apply_stencil_clip_settings();
753 } else {
754 fGpu->disableStencil();
755 return;
756 }
757 } else {
758 settings = drawState.getStencil();
759 }
760
761 // TODO: dynamically attach a stencil buffer
762 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000763 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000764 drawState.getRenderTarget()->getStencilBuffer();
765 if (NULL != stencilBuffer) {
766 stencilBits = stencilBuffer->bits();
767 }
768
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000769 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() || !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000770 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000771 this->adjustStencilParams(&settings, clipMode, stencilBits);
772 fGpu->setStencilSettings(settings);
773}
774
775void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
776 StencilClipMode mode,
777 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000778 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000779
780 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000781 // We assume that this clip manager itself is drawing to the GrGpu and
782 // has already setup the correct values.
783 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000784 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000785
bsalomon@google.com411dad02012-06-05 20:24:20 +0000786 unsigned int clipBit = (1 << (stencilBitCnt - 1));
787 unsigned int userBits = clipBit - 1;
788
bsalomon@google.coma3201942012-06-21 19:58:20 +0000789 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000790 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000791
bsalomon@google.coma3201942012-06-21 19:58:20 +0000792 bool finished = false;
793 while (!finished) {
794 GrStencilFunc func = settings->func(face);
795 uint16_t writeMask = settings->writeMask(face);
796 uint16_t funcMask = settings->funcMask(face);
797 uint16_t funcRef = settings->funcRef(face);
798
799 GrAssert((unsigned) func < kStencilFuncCount);
800
801 writeMask &= userBits;
802
803 if (func >= kBasicStencilFuncCount) {
804 int respectClip = kRespectClip_StencilClipMode == mode;
805 if (respectClip) {
806 // The GrGpu class should have checked this
807 GrAssert(this->isClipInStencil());
808 switch (func) {
809 case kAlwaysIfInClip_StencilFunc:
810 funcMask = clipBit;
811 funcRef = clipBit;
812 break;
813 case kEqualIfInClip_StencilFunc:
814 case kLessIfInClip_StencilFunc:
815 case kLEqualIfInClip_StencilFunc:
816 funcMask = (funcMask & userBits) | clipBit;
817 funcRef = (funcRef & userBits) | clipBit;
818 break;
819 case kNonZeroIfInClip_StencilFunc:
820 funcMask = (funcMask & userBits) | clipBit;
821 funcRef = clipBit;
822 break;
823 default:
824 GrCrash("Unknown stencil func");
825 }
826 } else {
827 funcMask &= userBits;
828 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000829 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000830 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000831 gSpecialToBasicStencilFunc[respectClip];
832 func = table[func - kBasicStencilFuncCount];
833 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000834 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000835 funcMask &= userBits;
836 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000837 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000838
839 settings->setFunc(face, func);
840 settings->setWriteMask(face, writeMask);
841 settings->setFuncMask(face, funcMask);
842 settings->setFuncRef(face, funcRef);
843
844 if (GrStencilSettings::kFront_Face == face) {
845 face = GrStencilSettings::kBack_Face;
846 finished = !twoSided;
847 } else {
848 finished = true;
849 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000850 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000851 if (!twoSided) {
852 settings->copyFrontSettingsToBack();
853 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000854}
855
856////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000857GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
858 GrReducedClip::InitialState initialState,
859 const GrReducedClip::ElementList& elements,
860 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000861 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000862
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000863 GrTexture* result;
864 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
865 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000866 }
867
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000868 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000869 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000870 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000871 }
872
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000873 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
874 // the top left corner of the resulting rect to the top left of the texture.
875 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
876
robertphillips@google.com2c756812012-05-22 20:28:23 +0000877 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000878
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000879 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000880 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
881 SkIntToScalar(-clipSpaceIBounds.fTop));
882 helper.init(maskSpaceIBounds, &matrix);
883
884 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000885
sugoi@google.com12b4e272012-12-06 20:13:11 +0000886 SkStroke stroke;
887 stroke.setDoFill(true);
888
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000889 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000890
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000891 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000892 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000893
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000894 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
895 // Intersect and reverse difference require modifying pixels outside of the geometry
896 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
897 // but leave the pixels inside the geometry alone. For reverse difference we invert all
898 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000899 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000900 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000901 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000902 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000903 }
904
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000905 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000906 // convert the rect to a path so we can invert the fill
907 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000908 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000909 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000910
sugoi@google.com12b4e272012-12-06 20:13:11 +0000911 helper.draw(temp, stroke, SkRegion::kReplace_Op,
912 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000913 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000914 } else {
915 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000916 SkPath clipPath = element->getPath();
917 clipPath.toggleInverseFillType();
918 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000919 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000920 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000921 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000922 }
923
924 continue;
925 }
926
927 // The other ops (union, xor, diff) only affect pixels inside
928 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000929 if (Element::kRect_Type == element->getType()) {
930 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
931 } else {
932 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000933 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000934 }
935 }
936
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000937 helper.toTexture(result, kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000938
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000939 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000940 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000941}
942
robertphillips@google.comf294b772012-04-27 14:29:26 +0000943////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +0000944void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000945 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000946}