blob: 164e1f86877626f6e8a9dffe4dbe32bdb0865d7d [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000010#include "GrAAConvexPathRenderer.h"
11#include "GrAAHairLinePathRenderer.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
13#include "GrGpu.h"
14#include "GrPaint.h"
15#include "GrPathRenderer.h"
16#include "GrRenderTarget.h"
17#include "GrStencilBuffer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000019#include "effects/GrTextureDomainEffect.h"
20#include "SkRasterClip.h"
21#include "SkStrokeRec.h"
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;
bsalomon@google.comc7818882013-03-20 19:19:53 +000043 // We want to use device coords to compute the texture coordinates. We set our matrix to be
44 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
45 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000048 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000049 mat.preConcat(drawState->getViewMatrix());
50
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000051 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000052 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.comadc65362013-01-28 14:26:09 +000053 drawState->setEffect(kMaskStage,
54 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000055 mat,
56 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
bsalomon@google.comc7818882013-03-20 19:19:53 +000057 GrTextureDomainEffect::kDecal_WrapMode,
58 false,
59 GrEffect::kPosition_CoordsType))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000060}
61
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000062bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000063 GrGpu* gpu,
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000064 const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000065 const SkStrokeRec& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000066 bool doAA) {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000067 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
68 SkTCopyOnFirstWrite<SkPath> path(origPath);
69 if (path->isInverseFillType()) {
70 path.writable()->toggleInverseFillType();
71 }
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000072 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000073 GrPathRendererChain::DrawType type = doAA ?
74 GrPathRendererChain::kColorAntiAlias_DrawType :
75 GrPathRendererChain::kColor_DrawType;
76
77 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000078}
79
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000080}
81
robertphillips@google.comfa662942012-05-17 12:20:22 +000082/*
83 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
84 * will be used on any element. If so, it returns true to indicate that the
85 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
86 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000087bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000088
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000089 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000090 // a clip gets complex enough it can just be done in SW regardless
91 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000092 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000093
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000094 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
95 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000096 // rects can always be drawn directly w/o using the software path
97 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000098 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000100 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000101 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000102 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000103 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000106 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000107}
108
robertphillips@google.comf294b772012-04-27 14:29:26 +0000109////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000110// sort out what kind of clip mask needs to be created: alpha, stencil,
111// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000112bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000113 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000114
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000115 ElementList elements(16);
116 InitialState initialState;
117 SkIRect clipSpaceIBounds;
118 bool requiresAA;
119 bool isRect = false;
120
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000121 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000122
123 const GrRenderTarget* rt = drawState->getRenderTarget();
124 // GrDrawTarget should have filtered this for us
125 GrAssert(NULL != rt);
126
127 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
128
129 if (!ignoreClip) {
130 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
131 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
132 ReduceClipStack(*clipDataIn->fClipStack,
133 clipSpaceRTIBounds,
134 &elements,
135 &initialState,
136 &clipSpaceIBounds,
137 &requiresAA);
138 if (elements.isEmpty()) {
139 if (kAllIn_InitialState == initialState) {
140 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
141 isRect = true;
142 } else {
143 return false;
144 }
145 }
146 }
147
148 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000149 fGpu->disableScissor();
150 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000151 return true;
152 }
153
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000154#if GR_AA_CLIP
155 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000156
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000157 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000158 if (0 == rt->numSamples() && requiresAA) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000159 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000160 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000161
162 if (this->useSWOnlyPath(elements)) {
163 // The clip geometry is complex enough that it will be more efficient to create it
164 // entirely in software
165 result = this->createSoftwareClipMask(genID,
166 initialState,
167 elements,
168 clipSpaceIBounds);
169 } else {
170 result = this->createAlphaClipMask(genID,
171 initialState,
172 elements,
173 clipSpaceIBounds);
174 }
175
176 if (NULL != result) {
177 // The mask's top left coord should be pinned to the rounded-out top left corner of
178 // clipSpace bounds. We determine the mask's position WRT to the render target here.
179 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
180 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
181 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000182 fGpu->disableScissor();
183 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000184 return true;
185 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000186 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000187 }
188#endif // GR_AA_CLIP
189
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000190 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
191 // be created. In either case, free up the texture in the anti-aliased mask cache.
192 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
193 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
194 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000195 fAACache.reset();
196
bsalomon@google.coma3201942012-06-21 19:58:20 +0000197 // If the clip is a rectangle then just set the scissor. Otherwise, create
198 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000199 if (isRect) {
200 SkIRect clipRect = clipSpaceIBounds;
201 clipRect.offset(-clipDataIn->fOrigin);
202 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000203 this->setGpuStencil();
204 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000205 }
206
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000207 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000208 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
209 this->createStencilClipMask(initialState,
210 elements,
211 clipSpaceIBounds,
212 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000213
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000214 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
215 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
216 // use both stencil and scissor test to the bounds for the final draw.
217 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
218 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
219 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000220 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000221 return true;
222}
223
224#define VISUALIZE_COMPLEX_CLIP 0
225
226#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000227 #include "SkRandom.h"
228 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000229 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
230#else
231 #define SET_RANDOM_COLOR
232#endif
233
234namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000235
236////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000237// set up the OpenGL blend function to perform the specified
238// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000239void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000240
241 switch (op) {
242 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000243 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000244 break;
245 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000246 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000247 break;
248 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000249 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000250 break;
251 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000252 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000253 break;
254 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000255 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000256 break;
257 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000258 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000259 break;
260 default:
261 GrAssert(false);
262 break;
263 }
264}
265
robertphillips@google.com72176b22012-05-23 13:19:12 +0000266}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000267
268////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000269bool GrClipMaskManager::drawElement(GrTexture* target,
270 const SkClipStack::Element* element,
271 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000272 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000273
274 drawState->setRenderTarget(target->asRenderTarget());
275
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000276 switch (element->getType()) {
277 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000278 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
279 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000280 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000281 getContext()->getAARectRenderer()->fillAARect(fGpu,
282 fGpu,
283 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000284 SkMatrix::I(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000285 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000286 } else {
287 fGpu->drawSimpleRect(element->getRect(), NULL);
288 }
289 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000290 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000291 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
292 if (path->isInverseFillType()) {
293 path.writable()->toggleInverseFillType();
294 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000295 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000296 if (NULL == pr) {
297 GrPathRendererChain::DrawType type;
298 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
299 GrPathRendererChain::kColor_DrawType;
300 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
301 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000302 if (NULL == pr) {
303 return false;
304 }
305 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
306 break;
307 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000308 default:
309 // something is wrong if we're trying to draw an empty element.
310 GrCrash("Unexpected element type");
311 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000312 }
313 return true;
314}
315
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000316bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
317 const SkClipStack::Element* element,
318 GrPathRenderer** pr) {
319 GrDrawState* drawState = fGpu->drawState();
320 drawState->setRenderTarget(target->asRenderTarget());
321
322 switch (element->getType()) {
323 case Element::kRect_Type:
324 return true;
325 case Element::kPath_Type: {
326 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
327 if (path->isInverseFillType()) {
328 path.writable()->toggleInverseFillType();
329 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000330 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000331 GrPathRendererChain::DrawType type = element->isAA() ?
332 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
333 GrPathRendererChain::kStencilAndColor_DrawType;
334 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
335 return NULL != *pr;
336 }
337 default:
338 // something is wrong if we're trying to draw an empty element.
339 GrCrash("Unexpected element type");
340 return false;
341 }
342}
343
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000344void GrClipMaskManager::mergeMask(GrTexture* dstMask,
345 GrTexture* srcMask,
346 SkRegion::Op op,
347 const GrIRect& dstBound,
348 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000349 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000350 SkMatrix oldMatrix = drawState->getViewMatrix();
351 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000352
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000353 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000354
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000355 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000356
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000357 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000358 sampleM.setIDiv(srcMask->width(), srcMask->height());
bsalomon@google.comadc65362013-01-28 14:26:09 +0000359 drawState->setEffect(0,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000360 GrTextureDomainEffect::Create(srcMask,
361 sampleM,
362 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
bsalomon@google.comc7818882013-03-20 19:19:53 +0000363 GrTextureDomainEffect::kDecal_WrapMode,
364 false))->unref();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000365 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000366
tomhudson@google.com676e6602012-07-10 17:21:48 +0000367 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000368 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369}
370
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000371// get a texture to act as a temporary buffer for AA clip boolean operations
372// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000373void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000374 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000375 // we've already allocated the temp texture
376 return;
377 }
378
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000379 GrTextureDesc desc;
380 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000381 desc.fWidth = width;
382 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000383 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000384
robertphillips@google.com2c756812012-05-22 20:28:23 +0000385 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000386}
387
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000388////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000389// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
390// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
391// hit)
392bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
393 const SkIRect& clipSpaceIBounds,
394 GrTexture** result) {
395 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
396 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000398 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
399 // Since we are setting up the cache we know the last lookup was a miss. Free up the
400 // currently cached mask so it can be reused.
401 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000403 GrTextureDesc desc;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000404 desc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000405 desc.fWidth = clipSpaceIBounds.width();
406 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000407 desc.fConfig = kRGBA_8888_GrPixelConfig;
408 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
409 // We would always like A8 but it isn't supported on all platforms
410 desc.fConfig = kAlpha_8_GrPixelConfig;
411 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000412
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000413 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000414 }
415
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000416 *result = fAACache.getLastMask();
417 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000418}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000419
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000420////////////////////////////////////////////////////////////////////////////////
421// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000422GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
423 InitialState initialState,
424 const ElementList& elements,
425 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000426 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
427
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000428 GrTexture* result;
429 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000430 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000431 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000432 }
433
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000434 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000435 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000436 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437 }
438
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000439 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000440 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000441
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000442 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000443 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000444 SkIntToScalar(-clipSpaceIBounds.fLeft),
445 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000446 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000447 // The texture may be larger than necessary, this rect represents the part of the texture
448 // we populate with a rasterization of the clip.
449 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
450
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000451 // We're drawing a coverage mask and want coverage to be run through the blend function.
452 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
453
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000454 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000455 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000456
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000457 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
458 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000459 fGpu->clear(&maskSpaceIBounds,
460 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
461 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000462
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000463 // When we use the stencil in the below loop it is important to have this clip installed.
464 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
465 // pass must not set values outside of this bounds or stencil values outside the rect won't be
466 // cleared.
467 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
468 drawState->enableState(GrDrawState::kClip_StateBit);
469
robertphillips@google.comf105b102012-05-14 12:18:26 +0000470 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000471 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000472 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000473 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000474 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000475 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000476
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000477 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
478 GrPathRenderer* pr = NULL;
479 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
480 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000481 // 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 +0000482 // mask buffer can be substantially larger than the actually clip stack element. We
483 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000484 // the accumulator.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000485 GrIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000486
487 if (useTemp) {
488 if (invert) {
489 maskSpaceElementIBounds = maskSpaceIBounds;
490 } else {
491 GrRect elementBounds = element->getBounds();
492 elementBounds.offset(clipToMaskOffset);
493 elementBounds.roundOut(&maskSpaceElementIBounds);
494 }
495
496 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
497 if (NULL == temp.texture()) {
498 fAACache.reset();
499 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000500 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000501 dst = temp.texture();
502 // clear the temp target and set blend to replace
503 fGpu->clear(&maskSpaceElementIBounds,
504 invert ? 0xffffffff : 0x00000000,
505 dst->asRenderTarget());
506 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000507
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000508 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000509 // draw directly into the result with the stencil set to make the pixels affected
510 // by the clip shape be non-zero.
511 dst = result;
512 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
513 kReplace_StencilOp,
514 kReplace_StencilOp,
515 kAlways_StencilFunc,
516 0xffff,
517 0xffff,
518 0xffff);
519 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000520 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000521 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000522
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000523 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000524
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000525 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000526 fAACache.reset();
527 return NULL;
528 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000529
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000530 if (useTemp) {
531 // Now draw into the accumulator using the real operation and the temp buffer as a
532 // texture
533 this->mergeMask(result,
534 temp.texture(),
535 op,
536 maskSpaceIBounds,
537 maskSpaceElementIBounds);
538 } else {
539 // Draw to the exterior pixels (those with a zero stencil value).
540 drawState->setAlpha(invert ? 0xff : 0x00);
541 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
542 kZero_StencilOp,
543 kZero_StencilOp,
544 kEqual_StencilFunc,
545 0xffff,
546 0x0000,
547 0xffff);
548 drawState->setStencil(kDrawOutsideElement);
549 fGpu->drawSimpleRect(clipSpaceIBounds);
550 drawState->disableStencil();
551 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000552 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000553 // all the remaining ops can just be directly draw into the accumulation buffer
554 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000555 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000556 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000557 }
558 }
559
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000560 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000561 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562}
563
564////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000565// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000566// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000567bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
568 const ElementList& elements,
569 const SkIRect& clipSpaceIBounds,
570 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000571
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000572 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000573
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000574 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000575 GrAssert(drawState->isClipState());
576
577 GrRenderTarget* rt = drawState->getRenderTarget();
578 GrAssert(NULL != rt);
579
580 // TODO: dynamically attach a SB when needed.
581 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
582 if (NULL == stencilBuffer) {
583 return false;
584 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000585 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000586
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000587 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000588
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000589 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000590
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000591 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000592 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000593 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000594
bsalomon@google.com9f131742012-12-13 20:43:56 +0000595 // We set the current clip to the bounds so that our recursive draws are scissored to them.
596 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
597 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
598 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
599 drawState->enableState(GrDrawState::kClip_StateBit);
600
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000601 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
602 SkVector translate = {
603 SkIntToScalar(clipSpaceToStencilOffset.fX),
604 SkIntToScalar(clipSpaceToStencilOffset.fY)
605 };
606 drawState->viewMatrix()->setTranslate(translate);
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000607
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000608#if !VISUALIZE_COMPLEX_CLIP
609 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
610#endif
611
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000612 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000613 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000614 clipBit = (1 << (clipBit-1));
615
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000616 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000617
618 // walk through each clip element and perform its set op
619 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000620 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
621 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000622 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000623 // enabled at bottom of loop
624 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000625 // if the target is MSAA then we want MSAA enabled when the clip is soft
626 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000627 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000628 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000629
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000630 // This will be used to determine whether the clip shape can be rendered into the
631 // stencil with arbitrary stencil settings.
632 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000633
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000634 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000635
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000636 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000637
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000638 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000639 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000640 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000641 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000642 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000643 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000644 GrAssert(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000645 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000646 fillInverted = clipPath->isInverseFillType();
647 if (fillInverted) {
648 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000649 }
650 pr = this->getContext()->getPathRenderer(*clipPath,
651 stroke,
652 fGpu,
653 false,
654 GrPathRendererChain::kStencilOnly_DrawType,
655 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000656 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000657 return false;
658 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 }
660
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000661 int passes;
662 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
663
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000664 bool canRenderDirectToStencil =
665 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000666 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000667 // fill rule, and set operation can
668 // we render the element directly to
669 // stencil bit used for clipping.
670 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
671 canRenderDirectToStencil,
672 clipBit,
673 fillInverted,
674 &passes,
675 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676
677 // draw the element to the client stencil bits if necessary
678 if (!canDrawDirectToClip) {
679 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000680 kIncClamp_StencilOp,
681 kIncClamp_StencilOp,
682 kAlways_StencilFunc,
683 0xffff,
684 0x0000,
685 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000686 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000687 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000689 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000691 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692 if (canRenderDirectToStencil) {
693 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000694 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000695 } else {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000696 pr->stencilPath(*clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697 }
698 }
699 }
700
701 // now we modify the clip bit by rendering either the clip
702 // element directly or a bounding rect of the entire clip.
703 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
704 for (int p = 0; p < passes; ++p) {
705 *drawState->stencil() = stencilSettings[p];
706 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000707 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000709 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000711 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000713 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 }
715 } else {
716 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000717 // The view matrix is setup to do clip space -> stencil space translation, so
718 // draw rect in clip space.
719 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 }
721 }
722 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000724 // set this last because recursive draws may overwrite it back to kNone.
725 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
726 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727 return true;
728}
729
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000730
bsalomon@google.com411dad02012-06-05 20:24:20 +0000731// mapping of clip-respecting stencil funcs to normal stencil funcs
732// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000733static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000734 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
735 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
736 // In the Clip Funcs
737 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
738 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
739 kLess_StencilFunc, // kLessIfInClip_StencilFunc
740 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
741 // Special in the clip func that forces user's ref to be 0.
742 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
743 // make ref 0 and do normal nequal.
744 },
745 {// Stencil-Clipping is ENABLED
746 // In the Clip Funcs
747 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
748 // eq stencil clip bit, mask
749 // out user bits.
750
751 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
752 // add stencil bit to mask and ref
753
754 kLess_StencilFunc, // kLessIfInClip_StencilFunc
755 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
756 // for both of these we can add
757 // the clip bit to the mask and
758 // ref and compare as normal
759 // Special in the clip func that forces user's ref to be 0.
760 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
761 // make ref have only the clip bit set
762 // and make comparison be less
763 // 10..0 < 1..user_bits..
764 }
765};
766
bsalomon@google.coma3201942012-06-21 19:58:20 +0000767namespace {
768// Sets the settings to clip against the stencil buffer clip while ignoring the
769// client bits.
770const GrStencilSettings& basic_apply_stencil_clip_settings() {
771 // stencil settings to use when clip is in stencil
772 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
773 kKeep_StencilOp,
774 kKeep_StencilOp,
775 kAlwaysIfInClip_StencilFunc,
776 0x0000,
777 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000778 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000779 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
780}
781}
782
783void GrClipMaskManager::setGpuStencil() {
784 // We make two copies of the StencilSettings here (except in the early
785 // exit scenario. One copy from draw state to the stack var. Then another
786 // from the stack var to the gpu. We could make this class hold a ptr to
787 // GrGpu's fStencilSettings and eliminate the stack copy here.
788
789 const GrDrawState& drawState = fGpu->getDrawState();
790
791 // use stencil for clipping if clipping is enabled and the clip
792 // has been written into the stencil.
793 GrClipMaskManager::StencilClipMode clipMode;
794 if (this->isClipInStencil() && drawState.isClipState()) {
795 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
796 // We can't be modifying the clip and respecting it at the same time.
797 GrAssert(!drawState.isStateFlagEnabled(
798 GrGpu::kModifyStencilClip_StateBit));
799 } else if (drawState.isStateFlagEnabled(
800 GrGpu::kModifyStencilClip_StateBit)) {
801 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
802 } else {
803 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
804 }
805
806 GrStencilSettings settings;
807 // The GrGpu client may not be using the stencil buffer but we may need to
808 // enable it in order to respect a stencil clip.
809 if (drawState.getStencil().isDisabled()) {
810 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
811 settings = basic_apply_stencil_clip_settings();
812 } else {
813 fGpu->disableStencil();
814 return;
815 }
816 } else {
817 settings = drawState.getStencil();
818 }
819
820 // TODO: dynamically attach a stencil buffer
821 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000822 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000823 drawState.getRenderTarget()->getStencilBuffer();
824 if (NULL != stencilBuffer) {
825 stencilBits = stencilBuffer->bits();
826 }
827
bsalomon@google.combcce8922013-03-25 15:38:39 +0000828 GrAssert(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
829 GrAssert(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000830 this->adjustStencilParams(&settings, clipMode, stencilBits);
831 fGpu->setStencilSettings(settings);
832}
833
834void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
835 StencilClipMode mode,
836 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000837 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000838
839 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000840 // We assume that this clip manager itself is drawing to the GrGpu and
841 // has already setup the correct values.
842 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000843 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000844
bsalomon@google.com411dad02012-06-05 20:24:20 +0000845 unsigned int clipBit = (1 << (stencilBitCnt - 1));
846 unsigned int userBits = clipBit - 1;
847
bsalomon@google.coma3201942012-06-21 19:58:20 +0000848 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000849 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000850
bsalomon@google.coma3201942012-06-21 19:58:20 +0000851 bool finished = false;
852 while (!finished) {
853 GrStencilFunc func = settings->func(face);
854 uint16_t writeMask = settings->writeMask(face);
855 uint16_t funcMask = settings->funcMask(face);
856 uint16_t funcRef = settings->funcRef(face);
857
858 GrAssert((unsigned) func < kStencilFuncCount);
859
860 writeMask &= userBits;
861
862 if (func >= kBasicStencilFuncCount) {
863 int respectClip = kRespectClip_StencilClipMode == mode;
864 if (respectClip) {
865 // The GrGpu class should have checked this
866 GrAssert(this->isClipInStencil());
867 switch (func) {
868 case kAlwaysIfInClip_StencilFunc:
869 funcMask = clipBit;
870 funcRef = clipBit;
871 break;
872 case kEqualIfInClip_StencilFunc:
873 case kLessIfInClip_StencilFunc:
874 case kLEqualIfInClip_StencilFunc:
875 funcMask = (funcMask & userBits) | clipBit;
876 funcRef = (funcRef & userBits) | clipBit;
877 break;
878 case kNonZeroIfInClip_StencilFunc:
879 funcMask = (funcMask & userBits) | clipBit;
880 funcRef = clipBit;
881 break;
882 default:
883 GrCrash("Unknown stencil func");
884 }
885 } else {
886 funcMask &= userBits;
887 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000888 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000889 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000890 gSpecialToBasicStencilFunc[respectClip];
891 func = table[func - kBasicStencilFuncCount];
892 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000893 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000894 funcMask &= userBits;
895 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000896 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000897
898 settings->setFunc(face, func);
899 settings->setWriteMask(face, writeMask);
900 settings->setFuncMask(face, funcMask);
901 settings->setFuncRef(face, funcRef);
902
903 if (GrStencilSettings::kFront_Face == face) {
904 face = GrStencilSettings::kBack_Face;
905 finished = !twoSided;
906 } else {
907 finished = true;
908 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000909 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000910 if (!twoSided) {
911 settings->copyFrontSettingsToBack();
912 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000913}
914
915////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000916GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
917 GrReducedClip::InitialState initialState,
918 const GrReducedClip::ElementList& elements,
919 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000920 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000921
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000922 GrTexture* result;
923 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
924 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000925 }
926
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000927 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000928 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000929 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000930 }
931
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000932 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
933 // the top left corner of the resulting rect to the top left of the texture.
934 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
935
robertphillips@google.com2c756812012-05-22 20:28:23 +0000936 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000937
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000938 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000939 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
940 SkIntToScalar(-clipSpaceIBounds.fTop));
941 helper.init(maskSpaceIBounds, &matrix);
942
943 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000944
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000945 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000946
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000947 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000948
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000949 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000950 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000951
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000952 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
953 // Intersect and reverse difference require modifying pixels outside of the geometry
954 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
955 // but leave the pixels inside the geometry alone. For reverse difference we invert all
956 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000957 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000958 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000959 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000960 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000961 }
962
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000963 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000964 // convert the rect to a path so we can invert the fill
965 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000966 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000967 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000968
sugoi@google.com12b4e272012-12-06 20:13:11 +0000969 helper.draw(temp, stroke, SkRegion::kReplace_Op,
970 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000971 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000972 } else {
973 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000974 SkPath clipPath = element->getPath();
975 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000976 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000977 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000978 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000979 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000980 }
981
982 continue;
983 }
984
985 // The other ops (union, xor, diff) only affect pixels inside
986 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000987 if (Element::kRect_Type == element->getType()) {
988 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
989 } else {
990 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000991 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000992 }
993 }
994
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000995 helper.toTexture(result, kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000996
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000997 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000998 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000999}
1000
robertphillips@google.comf294b772012-04-27 14:29:26 +00001001////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001002void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001003 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001004}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001005
1006void GrClipMaskManager::setGpu(GrGpu* gpu) {
1007 fGpu = gpu;
1008 fAACache.setContext(gpu->getContext());
1009}