blob: f3f773d536901a6054b54008703c0ed26960e523 [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
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000022#include "SkTLazy.h"
23
robertphillips@google.comba998f22012-10-12 11:33:56 +000024#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000025
bsalomon@google.com8182fa02012-12-04 14:06:06 +000026typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000027
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000028using namespace GrReducedClip;
29
bsalomon@google.com51a62862012-11-26 21:19:43 +000030////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000031namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000033// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034void setup_drawstate_aaclip(GrGpu* gpu,
35 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000036 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037 GrDrawState* drawState = gpu->drawState();
38 GrAssert(drawState);
39
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000040 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041
bsalomon@google.comb9086a02012-11-01 18:02:54 +000042 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000045 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046 mat.preConcat(drawState->getViewMatrix());
47
bsalomon@google.com08283af2012-10-26 13:01:20 +000048 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000049
50 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000051 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052 drawState->stage(kMaskStage)->setEffect(
53 GrTextureDomainEffect::Create(result,
54 mat,
55 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
56 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000057}
58
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000059bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000060 GrGpu* gpu,
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000061 const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000062 const SkStrokeRec& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000063 bool doAA) {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000064 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
65 SkTCopyOnFirstWrite<SkPath> path(origPath);
66 if (path->isInverseFillType()) {
67 path.writable()->toggleInverseFillType();
68 }
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000069 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000070 GrPathRendererChain::DrawType type = doAA ?
71 GrPathRendererChain::kColorAntiAlias_DrawType :
72 GrPathRendererChain::kColor_DrawType;
73
74 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000075}
76
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000077}
78
robertphillips@google.comfa662942012-05-17 12:20:22 +000079/*
80 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
81 * will be used on any element. If so, it returns true to indicate that the
82 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
83 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000084bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000085
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000086 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000087 // a clip gets complex enough it can just be done in SW regardless
88 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000089 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000090
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000091 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
92 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000093 // rects can always be drawn directly w/o using the software path
94 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000095 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000096 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000097 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +000098 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000099 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000100 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000101 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000102 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000103 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000104}
105
robertphillips@google.comf294b772012-04-27 14:29:26 +0000106////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000107// sort out what kind of clip mask needs to be created: alpha, stencil,
108// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000109bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000110 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000111
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000112 ElementList elements(16);
113 InitialState initialState;
114 SkIRect clipSpaceIBounds;
115 bool requiresAA;
116 bool isRect = false;
117
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000118 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000119
120 const GrRenderTarget* rt = drawState->getRenderTarget();
121 // GrDrawTarget should have filtered this for us
122 GrAssert(NULL != rt);
123
124 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
125
126 if (!ignoreClip) {
127 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
128 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
129 ReduceClipStack(*clipDataIn->fClipStack,
130 clipSpaceRTIBounds,
131 &elements,
132 &initialState,
133 &clipSpaceIBounds,
134 &requiresAA);
135 if (elements.isEmpty()) {
136 if (kAllIn_InitialState == initialState) {
137 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
138 isRect = true;
139 } else {
140 return false;
141 }
142 }
143 }
144
145 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000146 fGpu->disableScissor();
147 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000148 return true;
149 }
150
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000151#if GR_AA_CLIP
152 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000153
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000154 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000155 if (0 == rt->numSamples() && requiresAA) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000156 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000157 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000158
159 if (this->useSWOnlyPath(elements)) {
160 // The clip geometry is complex enough that it will be more efficient to create it
161 // entirely in software
162 result = this->createSoftwareClipMask(genID,
163 initialState,
164 elements,
165 clipSpaceIBounds);
166 } else {
167 result = this->createAlphaClipMask(genID,
168 initialState,
169 elements,
170 clipSpaceIBounds);
171 }
172
173 if (NULL != result) {
174 // The mask's top left coord should be pinned to the rounded-out top left corner of
175 // clipSpace bounds. We determine the mask's position WRT to the render target here.
176 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
177 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
178 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000179 fGpu->disableScissor();
180 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000181 return true;
182 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000183 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000184 }
185#endif // GR_AA_CLIP
186
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000187 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
188 // be created. In either case, free up the texture in the anti-aliased mask cache.
189 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
190 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
191 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000192 fAACache.reset();
193
bsalomon@google.coma3201942012-06-21 19:58:20 +0000194 // If the clip is a rectangle then just set the scissor. Otherwise, create
195 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000196 if (isRect) {
197 SkIRect clipRect = clipSpaceIBounds;
198 clipRect.offset(-clipDataIn->fOrigin);
199 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000200 this->setGpuStencil();
201 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000202 }
203
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000204 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000205 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
206 this->createStencilClipMask(initialState,
207 elements,
208 clipSpaceIBounds,
209 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000210
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000211 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
212 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
213 // use both stencil and scissor test to the bounds for the final draw.
214 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
215 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
216 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000217 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000218 return true;
219}
220
221#define VISUALIZE_COMPLEX_CLIP 0
222
223#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000224 #include "SkRandom.h"
225 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000226 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
227#else
228 #define SET_RANDOM_COLOR
229#endif
230
231namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000232
233////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000234// set up the OpenGL blend function to perform the specified
235// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000236void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000237
238 switch (op) {
239 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000240 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000241 break;
242 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000243 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000244 break;
245 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000246 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000247 break;
248 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000249 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000250 break;
251 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000252 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000253 break;
254 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000255 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000256 break;
257 default:
258 GrAssert(false);
259 break;
260 }
261}
262
robertphillips@google.com72176b22012-05-23 13:19:12 +0000263}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000264
265////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000266bool GrClipMaskManager::drawElement(GrTexture* target,
267 const SkClipStack::Element* element,
268 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000269 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000270
271 drawState->setRenderTarget(target->asRenderTarget());
272
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000273 switch (element->getType()) {
274 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000275 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
276 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000277 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000278 getContext()->getAARectRenderer()->fillAARect(fGpu,
279 fGpu,
280 element->getRect(),
281 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000282 } else {
283 fGpu->drawSimpleRect(element->getRect(), NULL);
284 }
285 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000286 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000287 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
288 if (path->isInverseFillType()) {
289 path.writable()->toggleInverseFillType();
290 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000291 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000292 if (NULL == pr) {
293 GrPathRendererChain::DrawType type;
294 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
295 GrPathRendererChain::kColor_DrawType;
296 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
297 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000298 if (NULL == pr) {
299 return false;
300 }
301 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
302 break;
303 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000304 default:
305 // something is wrong if we're trying to draw an empty element.
306 GrCrash("Unexpected element type");
307 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000308 }
309 return true;
310}
311
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000312bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
313 const SkClipStack::Element* element,
314 GrPathRenderer** pr) {
315 GrDrawState* drawState = fGpu->drawState();
316 drawState->setRenderTarget(target->asRenderTarget());
317
318 switch (element->getType()) {
319 case Element::kRect_Type:
320 return true;
321 case Element::kPath_Type: {
322 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
323 if (path->isInverseFillType()) {
324 path.writable()->toggleInverseFillType();
325 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000326 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000327 GrPathRendererChain::DrawType type = element->isAA() ?
328 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
329 GrPathRendererChain::kStencilAndColor_DrawType;
330 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
331 return NULL != *pr;
332 }
333 default:
334 // something is wrong if we're trying to draw an empty element.
335 GrCrash("Unexpected element type");
336 return false;
337 }
338}
339
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000340void GrClipMaskManager::mergeMask(GrTexture* dstMask,
341 GrTexture* srcMask,
342 SkRegion::Op op,
343 const GrIRect& dstBound,
344 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000345 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000346 SkMatrix oldMatrix = drawState->getViewMatrix();
347 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000349 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000350
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000351 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000352
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000353 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000354 sampleM.setIDiv(srcMask->width(), srcMask->height());
355 drawState->stage(0)->setEffect(
356 GrTextureDomainEffect::Create(srcMask,
357 sampleM,
358 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
359 GrTextureDomainEffect::kDecal_WrapMode))->unref();
360 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361
tomhudson@google.com676e6602012-07-10 17:21:48 +0000362 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000363 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000364}
365
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000366// get a texture to act as a temporary buffer for AA clip boolean operations
367// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000368void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000369 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000370 // we've already allocated the temp texture
371 return;
372 }
373
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000374 GrTextureDesc desc;
375 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000376 desc.fWidth = width;
377 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000378 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000379
robertphillips@google.com2c756812012-05-22 20:28:23 +0000380 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000381}
382
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000383////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000384// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
385// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
386// hit)
387bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
388 const SkIRect& clipSpaceIBounds,
389 GrTexture** result) {
390 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
391 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000393 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
394 // Since we are setting up the cache we know the last lookup was a miss. Free up the
395 // currently cached mask so it can be reused.
396 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000398 GrTextureDesc desc;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000399 desc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000400 desc.fWidth = clipSpaceIBounds.width();
401 desc.fHeight = clipSpaceIBounds.height();
402 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000404 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000405 }
406
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000407 *result = fAACache.getLastMask();
408 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000409}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000410
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000411////////////////////////////////////////////////////////////////////////////////
412// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000413GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
414 InitialState initialState,
415 const ElementList& elements,
416 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000417 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
418
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000419 GrTexture* result;
420 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000421 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000422 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000423 }
424
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000425 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000426 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000428 }
429
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000430 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
431 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000432
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000433 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000434
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000435 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000436 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000437 SkIntToScalar(-clipSpaceIBounds.fLeft),
438 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000439 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 // The texture may be larger than necessary, this rect represents the part of the texture
441 // we populate with a rasterization of the clip.
442 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
443
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000444 // We're drawing a coverage mask and want coverage to be run through the blend function.
445 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
446
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000447 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000448 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000449
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000450 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
451 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000452 fGpu->clear(&maskSpaceIBounds,
453 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
454 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000455
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000456 // When we use the stencil in the below loop it is important to have this clip installed.
457 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
458 // pass must not set values outside of this bounds or stencil values outside the rect won't be
459 // cleared.
460 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
461 drawState->enableState(GrDrawState::kClip_StateBit);
462
robertphillips@google.comf105b102012-05-14 12:18:26 +0000463 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000465 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000466 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000467 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000468 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000469
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000470 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
471 GrPathRenderer* pr = NULL;
472 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
473 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000474 // 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 +0000475 // mask buffer can be substantially larger than the actually clip stack element. We
476 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000477 // the accumulator.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000478 GrIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000479
480 if (useTemp) {
481 if (invert) {
482 maskSpaceElementIBounds = maskSpaceIBounds;
483 } else {
484 GrRect elementBounds = element->getBounds();
485 elementBounds.offset(clipToMaskOffset);
486 elementBounds.roundOut(&maskSpaceElementIBounds);
487 }
488
489 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
490 if (NULL == temp.texture()) {
491 fAACache.reset();
492 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000493 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000494 dst = temp.texture();
495 // clear the temp target and set blend to replace
496 fGpu->clear(&maskSpaceElementIBounds,
497 invert ? 0xffffffff : 0x00000000,
498 dst->asRenderTarget());
499 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000500
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000501 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000502 // draw directly into the result with the stencil set to make the pixels affected
503 // by the clip shape be non-zero.
504 dst = result;
505 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
506 kReplace_StencilOp,
507 kReplace_StencilOp,
508 kAlways_StencilFunc,
509 0xffff,
510 0xffff,
511 0xffff);
512 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000513 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000514 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000515
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000516 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000517
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000518 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000519 fAACache.reset();
520 return NULL;
521 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000522
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000523 if (useTemp) {
524 // Now draw into the accumulator using the real operation and the temp buffer as a
525 // texture
526 this->mergeMask(result,
527 temp.texture(),
528 op,
529 maskSpaceIBounds,
530 maskSpaceElementIBounds);
531 } else {
532 // Draw to the exterior pixels (those with a zero stencil value).
533 drawState->setAlpha(invert ? 0xff : 0x00);
534 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
535 kZero_StencilOp,
536 kZero_StencilOp,
537 kEqual_StencilFunc,
538 0xffff,
539 0x0000,
540 0xffff);
541 drawState->setStencil(kDrawOutsideElement);
542 fGpu->drawSimpleRect(clipSpaceIBounds);
543 drawState->disableStencil();
544 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000545 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000546 // all the remaining ops can just be directly draw into the accumulation buffer
547 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000548 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000549 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000550 }
551 }
552
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000553 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000554 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555}
556
557////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000558// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000559// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
561 const ElementList& elements,
562 const SkIRect& clipSpaceIBounds,
563 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000564
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000565 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000566
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000567 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000568 GrAssert(drawState->isClipState());
569
570 GrRenderTarget* rt = drawState->getRenderTarget();
571 GrAssert(NULL != rt);
572
573 // TODO: dynamically attach a SB when needed.
574 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
575 if (NULL == stencilBuffer) {
576 return false;
577 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000578 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000579
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000580 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000581
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000582 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000583
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000584 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
585 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000586 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000587 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000588
bsalomon@google.com9f131742012-12-13 20:43:56 +0000589 // We set the current clip to the bounds so that our recursive draws are scissored to them.
590 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
591 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
592 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
593 drawState->enableState(GrDrawState::kClip_StateBit);
594
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000595 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
596 SkVector translate = {
597 SkIntToScalar(clipSpaceToStencilOffset.fX),
598 SkIntToScalar(clipSpaceToStencilOffset.fY)
599 };
600 drawState->viewMatrix()->setTranslate(translate);
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000601
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000602#if !VISUALIZE_COMPLEX_CLIP
603 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
604#endif
605
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000606 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000607 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000608 clipBit = (1 << (clipBit-1));
609
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000610 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000611
612 // walk through each clip element and perform its set op
613 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000614 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
615 const Element* element = iter.get();
sugoi@google.com12b4e272012-12-06 20:13:11 +0000616 SkPath::FillType fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000617 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000618 // enabled at bottom of loop
619 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000620 // if the target is MSAA then we want MSAA enabled when the clip is soft
621 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000622 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000623 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000624
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000625 // This will be used to determine whether the clip shape can be rendered into the
626 // stencil with arbitrary stencil settings.
627 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000628
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000629 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000630
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000631 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000633 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000634 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000635 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000636 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000637 fill = SkPath::kEvenOdd_FillType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000638 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000639 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000640 GrAssert(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000641 clipPath.init(element->getPath());
642 fill = clipPath->getFillType();
643 fillInverted = clipPath->isInverseFillType();
644 if (fillInverted) {
645 clipPath.writable()->toggleInverseFillType();
646 fill = clipPath->getFillType();
647 }
648 pr = this->getContext()->getPathRenderer(*clipPath,
649 stroke,
650 fGpu,
651 false,
652 GrPathRendererChain::kStencilOnly_DrawType,
653 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000654 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000655 return false;
656 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000657 }
658
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 int passes;
660 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
661
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000662 bool canRenderDirectToStencil =
663 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000665 // fill rule, and set operation can
666 // we render the element directly to
667 // stencil bit used for clipping.
668 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
669 canRenderDirectToStencil,
670 clipBit,
671 fillInverted,
672 &passes,
673 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000674
675 // draw the element to the client stencil bits if necessary
676 if (!canDrawDirectToClip) {
677 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000678 kIncClamp_StencilOp,
679 kIncClamp_StencilOp,
680 kAlways_StencilFunc,
681 0xffff,
682 0x0000,
683 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000684 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000685 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000686 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000687 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000689 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690 if (canRenderDirectToStencil) {
691 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000692 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693 } else {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000694 pr->stencilPath(*clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000695 }
696 }
697 }
698
699 // now we modify the clip bit by rendering either the clip
700 // element directly or a bounding rect of the entire clip.
701 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
702 for (int p = 0; p < passes; ++p) {
703 *drawState->stencil() = stencilSettings[p];
704 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000705 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000707 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000709 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000711 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 }
713 } else {
714 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000715 // The view matrix is setup to do clip space -> stencil space translation, so
716 // draw rect in clip space.
717 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 }
719 }
720 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000722 // set this last because recursive draws may overwrite it back to kNone.
723 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
724 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 return true;
726}
727
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000728
bsalomon@google.com411dad02012-06-05 20:24:20 +0000729// mapping of clip-respecting stencil funcs to normal stencil funcs
730// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000731static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000732 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
733 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
734 // In the Clip Funcs
735 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
736 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
737 kLess_StencilFunc, // kLessIfInClip_StencilFunc
738 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
739 // Special in the clip func that forces user's ref to be 0.
740 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
741 // make ref 0 and do normal nequal.
742 },
743 {// Stencil-Clipping is ENABLED
744 // In the Clip Funcs
745 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
746 // eq stencil clip bit, mask
747 // out user bits.
748
749 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
750 // add stencil bit to mask and ref
751
752 kLess_StencilFunc, // kLessIfInClip_StencilFunc
753 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
754 // for both of these we can add
755 // the clip bit to the mask and
756 // ref and compare as normal
757 // Special in the clip func that forces user's ref to be 0.
758 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
759 // make ref have only the clip bit set
760 // and make comparison be less
761 // 10..0 < 1..user_bits..
762 }
763};
764
bsalomon@google.coma3201942012-06-21 19:58:20 +0000765namespace {
766// Sets the settings to clip against the stencil buffer clip while ignoring the
767// client bits.
768const GrStencilSettings& basic_apply_stencil_clip_settings() {
769 // stencil settings to use when clip is in stencil
770 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
771 kKeep_StencilOp,
772 kKeep_StencilOp,
773 kAlwaysIfInClip_StencilFunc,
774 0x0000,
775 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000776 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000777 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
778}
779}
780
781void GrClipMaskManager::setGpuStencil() {
782 // We make two copies of the StencilSettings here (except in the early
783 // exit scenario. One copy from draw state to the stack var. Then another
784 // from the stack var to the gpu. We could make this class hold a ptr to
785 // GrGpu's fStencilSettings and eliminate the stack copy here.
786
787 const GrDrawState& drawState = fGpu->getDrawState();
788
789 // use stencil for clipping if clipping is enabled and the clip
790 // has been written into the stencil.
791 GrClipMaskManager::StencilClipMode clipMode;
792 if (this->isClipInStencil() && drawState.isClipState()) {
793 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
794 // We can't be modifying the clip and respecting it at the same time.
795 GrAssert(!drawState.isStateFlagEnabled(
796 GrGpu::kModifyStencilClip_StateBit));
797 } else if (drawState.isStateFlagEnabled(
798 GrGpu::kModifyStencilClip_StateBit)) {
799 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
800 } else {
801 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
802 }
803
804 GrStencilSettings settings;
805 // The GrGpu client may not be using the stencil buffer but we may need to
806 // enable it in order to respect a stencil clip.
807 if (drawState.getStencil().isDisabled()) {
808 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
809 settings = basic_apply_stencil_clip_settings();
810 } else {
811 fGpu->disableStencil();
812 return;
813 }
814 } else {
815 settings = drawState.getStencil();
816 }
817
818 // TODO: dynamically attach a stencil buffer
819 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000820 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000821 drawState.getRenderTarget()->getStencilBuffer();
822 if (NULL != stencilBuffer) {
823 stencilBits = stencilBuffer->bits();
824 }
825
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000826 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() || !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000827 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000828 this->adjustStencilParams(&settings, clipMode, stencilBits);
829 fGpu->setStencilSettings(settings);
830}
831
832void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
833 StencilClipMode mode,
834 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000835 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000836
837 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000838 // We assume that this clip manager itself is drawing to the GrGpu and
839 // has already setup the correct values.
840 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000841 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000842
bsalomon@google.com411dad02012-06-05 20:24:20 +0000843 unsigned int clipBit = (1 << (stencilBitCnt - 1));
844 unsigned int userBits = clipBit - 1;
845
bsalomon@google.coma3201942012-06-21 19:58:20 +0000846 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000847 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000848
bsalomon@google.coma3201942012-06-21 19:58:20 +0000849 bool finished = false;
850 while (!finished) {
851 GrStencilFunc func = settings->func(face);
852 uint16_t writeMask = settings->writeMask(face);
853 uint16_t funcMask = settings->funcMask(face);
854 uint16_t funcRef = settings->funcRef(face);
855
856 GrAssert((unsigned) func < kStencilFuncCount);
857
858 writeMask &= userBits;
859
860 if (func >= kBasicStencilFuncCount) {
861 int respectClip = kRespectClip_StencilClipMode == mode;
862 if (respectClip) {
863 // The GrGpu class should have checked this
864 GrAssert(this->isClipInStencil());
865 switch (func) {
866 case kAlwaysIfInClip_StencilFunc:
867 funcMask = clipBit;
868 funcRef = clipBit;
869 break;
870 case kEqualIfInClip_StencilFunc:
871 case kLessIfInClip_StencilFunc:
872 case kLEqualIfInClip_StencilFunc:
873 funcMask = (funcMask & userBits) | clipBit;
874 funcRef = (funcRef & userBits) | clipBit;
875 break;
876 case kNonZeroIfInClip_StencilFunc:
877 funcMask = (funcMask & userBits) | clipBit;
878 funcRef = clipBit;
879 break;
880 default:
881 GrCrash("Unknown stencil func");
882 }
883 } else {
884 funcMask &= userBits;
885 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000886 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000887 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000888 gSpecialToBasicStencilFunc[respectClip];
889 func = table[func - kBasicStencilFuncCount];
890 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000891 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000892 funcMask &= userBits;
893 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000894 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000895
896 settings->setFunc(face, func);
897 settings->setWriteMask(face, writeMask);
898 settings->setFuncMask(face, funcMask);
899 settings->setFuncRef(face, funcRef);
900
901 if (GrStencilSettings::kFront_Face == face) {
902 face = GrStencilSettings::kBack_Face;
903 finished = !twoSided;
904 } else {
905 finished = true;
906 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000907 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000908 if (!twoSided) {
909 settings->copyFrontSettingsToBack();
910 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000911}
912
913////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000914GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
915 GrReducedClip::InitialState initialState,
916 const GrReducedClip::ElementList& elements,
917 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000918 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000919
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000920 GrTexture* result;
921 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
922 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000923 }
924
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000925 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000926 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000927 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000928 }
929
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000930 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
931 // the top left corner of the resulting rect to the top left of the texture.
932 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
933
robertphillips@google.com2c756812012-05-22 20:28:23 +0000934 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000935
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000936 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000937 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
938 SkIntToScalar(-clipSpaceIBounds.fTop));
939 helper.init(maskSpaceIBounds, &matrix);
940
941 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000942
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000943 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000944
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000945 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000946
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000947 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000948 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000949
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000950 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
951 // Intersect and reverse difference require modifying pixels outside of the geometry
952 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
953 // but leave the pixels inside the geometry alone. For reverse difference we invert all
954 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000955 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000956 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000957 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000958 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000959 }
960
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000961 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000962 // convert the rect to a path so we can invert the fill
963 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000964 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000965 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000966
sugoi@google.com12b4e272012-12-06 20:13:11 +0000967 helper.draw(temp, stroke, SkRegion::kReplace_Op,
968 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000969 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000970 } else {
971 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000972 SkPath clipPath = element->getPath();
973 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000974 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000975 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000976 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000977 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000978 }
979
980 continue;
981 }
982
983 // The other ops (union, xor, diff) only affect pixels inside
984 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000985 if (Element::kRect_Type == element->getType()) {
986 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
987 } else {
988 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000989 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000990 }
991 }
992
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000993 helper.toTexture(result, kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000994
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000995 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000996 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000997}
998
robertphillips@google.comf294b772012-04-27 14:29:26 +0000999////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001000void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001001 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001002}