blob: 6415859db9a0d5d997db7d0e333790960c4f1c27 [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;
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(),
284 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000285 } else {
286 fGpu->drawSimpleRect(element->getRect(), NULL);
287 }
288 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000289 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000290 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
291 if (path->isInverseFillType()) {
292 path.writable()->toggleInverseFillType();
293 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000294 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000295 if (NULL == pr) {
296 GrPathRendererChain::DrawType type;
297 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
298 GrPathRendererChain::kColor_DrawType;
299 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
300 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000301 if (NULL == pr) {
302 return false;
303 }
304 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
305 break;
306 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000307 default:
308 // something is wrong if we're trying to draw an empty element.
309 GrCrash("Unexpected element type");
310 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000311 }
312 return true;
313}
314
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000315bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
316 const SkClipStack::Element* element,
317 GrPathRenderer** pr) {
318 GrDrawState* drawState = fGpu->drawState();
319 drawState->setRenderTarget(target->asRenderTarget());
320
321 switch (element->getType()) {
322 case Element::kRect_Type:
323 return true;
324 case Element::kPath_Type: {
325 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
326 if (path->isInverseFillType()) {
327 path.writable()->toggleInverseFillType();
328 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000329 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000330 GrPathRendererChain::DrawType type = element->isAA() ?
331 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
332 GrPathRendererChain::kStencilAndColor_DrawType;
333 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
334 return NULL != *pr;
335 }
336 default:
337 // something is wrong if we're trying to draw an empty element.
338 GrCrash("Unexpected element type");
339 return false;
340 }
341}
342
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000343void GrClipMaskManager::mergeMask(GrTexture* dstMask,
344 GrTexture* srcMask,
345 SkRegion::Op op,
346 const GrIRect& dstBound,
347 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000348 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000349 SkMatrix oldMatrix = drawState->getViewMatrix();
350 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000351
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000352 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000353
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000354 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000355
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000356 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000357 sampleM.setIDiv(srcMask->width(), srcMask->height());
bsalomon@google.comadc65362013-01-28 14:26:09 +0000358 drawState->setEffect(0,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000359 GrTextureDomainEffect::Create(srcMask,
360 sampleM,
361 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
bsalomon@google.comc7818882013-03-20 19:19:53 +0000362 GrTextureDomainEffect::kDecal_WrapMode,
363 false))->unref();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000364 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365
tomhudson@google.com676e6602012-07-10 17:21:48 +0000366 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000367 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000368}
369
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000370// get a texture to act as a temporary buffer for AA clip boolean operations
371// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000372void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000373 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000374 // we've already allocated the temp texture
375 return;
376 }
377
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000378 GrTextureDesc desc;
379 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000380 desc.fWidth = width;
381 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000382 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000383
robertphillips@google.com2c756812012-05-22 20:28:23 +0000384 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000385}
386
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000387////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000388// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
389// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
390// hit)
391bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
392 const SkIRect& clipSpaceIBounds,
393 GrTexture** result) {
394 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
395 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000396
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000397 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
398 // Since we are setting up the cache we know the last lookup was a miss. Free up the
399 // currently cached mask so it can be reused.
400 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000401
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000402 GrTextureDesc desc;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000403 desc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000404 desc.fWidth = clipSpaceIBounds.width();
405 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000406 desc.fConfig = kRGBA_8888_GrPixelConfig;
407 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
408 // We would always like A8 but it isn't supported on all platforms
409 desc.fConfig = kAlpha_8_GrPixelConfig;
410 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000411
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000412 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000413 }
414
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000415 *result = fAACache.getLastMask();
416 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000417}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000418
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000419////////////////////////////////////////////////////////////////////////////////
420// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000421GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
422 InitialState initialState,
423 const ElementList& elements,
424 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000425 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
426
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 GrTexture* result;
428 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000429 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000430 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000431 }
432
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000433 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000434 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000435 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000436 }
437
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000438 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000439 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000440
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000441 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000442 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443 SkIntToScalar(-clipSpaceIBounds.fLeft),
444 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000445 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000446 // The texture may be larger than necessary, this rect represents the part of the texture
447 // we populate with a rasterization of the clip.
448 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
449
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000450 // We're drawing a coverage mask and want coverage to be run through the blend function.
451 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
452
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000453 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000454 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000455
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000456 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
457 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000458 fGpu->clear(&maskSpaceIBounds,
459 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
460 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000461
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000462 // When we use the stencil in the below loop it is important to have this clip installed.
463 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
464 // pass must not set values outside of this bounds or stencil values outside the rect won't be
465 // cleared.
466 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
467 drawState->enableState(GrDrawState::kClip_StateBit);
468
robertphillips@google.comf105b102012-05-14 12:18:26 +0000469 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000470 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000471 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000472 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000473 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000474 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000475
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000476 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
477 GrPathRenderer* pr = NULL;
478 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
479 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000480 // 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 +0000481 // mask buffer can be substantially larger than the actually clip stack element. We
482 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000483 // the accumulator.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000484 GrIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000485
486 if (useTemp) {
487 if (invert) {
488 maskSpaceElementIBounds = maskSpaceIBounds;
489 } else {
490 GrRect elementBounds = element->getBounds();
491 elementBounds.offset(clipToMaskOffset);
492 elementBounds.roundOut(&maskSpaceElementIBounds);
493 }
494
495 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
496 if (NULL == temp.texture()) {
497 fAACache.reset();
498 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000499 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000500 dst = temp.texture();
501 // clear the temp target and set blend to replace
502 fGpu->clear(&maskSpaceElementIBounds,
503 invert ? 0xffffffff : 0x00000000,
504 dst->asRenderTarget());
505 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000506
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000507 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000508 // draw directly into the result with the stencil set to make the pixels affected
509 // by the clip shape be non-zero.
510 dst = result;
511 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
512 kReplace_StencilOp,
513 kReplace_StencilOp,
514 kAlways_StencilFunc,
515 0xffff,
516 0xffff,
517 0xffff);
518 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000519 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000520 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000521
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000522 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000523
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000524 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000525 fAACache.reset();
526 return NULL;
527 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000528
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000529 if (useTemp) {
530 // Now draw into the accumulator using the real operation and the temp buffer as a
531 // texture
532 this->mergeMask(result,
533 temp.texture(),
534 op,
535 maskSpaceIBounds,
536 maskSpaceElementIBounds);
537 } else {
538 // Draw to the exterior pixels (those with a zero stencil value).
539 drawState->setAlpha(invert ? 0xff : 0x00);
540 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
541 kZero_StencilOp,
542 kZero_StencilOp,
543 kEqual_StencilFunc,
544 0xffff,
545 0x0000,
546 0xffff);
547 drawState->setStencil(kDrawOutsideElement);
548 fGpu->drawSimpleRect(clipSpaceIBounds);
549 drawState->disableStencil();
550 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000551 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000552 // all the remaining ops can just be directly draw into the accumulation buffer
553 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000554 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000555 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000556 }
557 }
558
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000559 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561}
562
563////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000564// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000565// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000566bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
567 const ElementList& elements,
568 const SkIRect& clipSpaceIBounds,
569 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000570
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000571 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000572
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000573 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000574 GrAssert(drawState->isClipState());
575
576 GrRenderTarget* rt = drawState->getRenderTarget();
577 GrAssert(NULL != rt);
578
579 // TODO: dynamically attach a SB when needed.
580 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
581 if (NULL == stencilBuffer) {
582 return false;
583 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000584 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000585
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000586 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000587
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000588 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000589
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000590 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000591 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000592 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000593
bsalomon@google.com9f131742012-12-13 20:43:56 +0000594 // We set the current clip to the bounds so that our recursive draws are scissored to them.
595 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
596 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
597 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
598 drawState->enableState(GrDrawState::kClip_StateBit);
599
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000600 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
601 SkVector translate = {
602 SkIntToScalar(clipSpaceToStencilOffset.fX),
603 SkIntToScalar(clipSpaceToStencilOffset.fY)
604 };
605 drawState->viewMatrix()->setTranslate(translate);
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000606
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000607#if !VISUALIZE_COMPLEX_CLIP
608 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
609#endif
610
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000611 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000612 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000613 clipBit = (1 << (clipBit-1));
614
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000615 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000616
617 // walk through each clip element and perform its set op
618 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000619 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
620 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000621 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000622 // enabled at bottom of loop
623 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000624 // if the target is MSAA then we want MSAA enabled when the clip is soft
625 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000626 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000627 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000628
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000629 // This will be used to determine whether the clip shape can be rendered into the
630 // stencil with arbitrary stencil settings.
631 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000632
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000633 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000634
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000635 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000636
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000637 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000638 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000639 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000640 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000642 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000643 GrAssert(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000644 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000645 fillInverted = clipPath->isInverseFillType();
646 if (fillInverted) {
647 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000648 }
649 pr = this->getContext()->getPathRenderer(*clipPath,
650 stroke,
651 fGpu,
652 false,
653 GrPathRendererChain::kStencilOnly_DrawType,
654 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000655 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000656 return false;
657 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000658 }
659
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000660 int passes;
661 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
662
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000663 bool canRenderDirectToStencil =
664 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000666 // fill rule, and set operation can
667 // we render the element directly to
668 // stencil bit used for clipping.
669 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
670 canRenderDirectToStencil,
671 clipBit,
672 fillInverted,
673 &passes,
674 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000675
676 // draw the element to the client stencil bits if necessary
677 if (!canDrawDirectToClip) {
678 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000679 kIncClamp_StencilOp,
680 kIncClamp_StencilOp,
681 kAlways_StencilFunc,
682 0xffff,
683 0x0000,
684 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000685 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000686 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000688 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000690 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691 if (canRenderDirectToStencil) {
692 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000693 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 } else {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000695 pr->stencilPath(*clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696 }
697 }
698 }
699
700 // now we modify the clip bit by rendering either the clip
701 // element directly or a bounding rect of the entire clip.
702 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
703 for (int p = 0; p < passes; ++p) {
704 *drawState->stencil() = stencilSettings[p];
705 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000706 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000708 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000709 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000710 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000712 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713 }
714 } else {
715 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000716 // The view matrix is setup to do clip space -> stencil space translation, so
717 // draw rect in clip space.
718 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719 }
720 }
721 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000723 // set this last because recursive draws may overwrite it back to kNone.
724 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
725 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 return true;
727}
728
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000729
bsalomon@google.com411dad02012-06-05 20:24:20 +0000730// mapping of clip-respecting stencil funcs to normal stencil funcs
731// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000732static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000733 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
734 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
735 // In the Clip Funcs
736 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
737 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
738 kLess_StencilFunc, // kLessIfInClip_StencilFunc
739 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
740 // Special in the clip func that forces user's ref to be 0.
741 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
742 // make ref 0 and do normal nequal.
743 },
744 {// Stencil-Clipping is ENABLED
745 // In the Clip Funcs
746 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
747 // eq stencil clip bit, mask
748 // out user bits.
749
750 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
751 // add stencil bit to mask and ref
752
753 kLess_StencilFunc, // kLessIfInClip_StencilFunc
754 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
755 // for both of these we can add
756 // the clip bit to the mask and
757 // ref and compare as normal
758 // Special in the clip func that forces user's ref to be 0.
759 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
760 // make ref have only the clip bit set
761 // and make comparison be less
762 // 10..0 < 1..user_bits..
763 }
764};
765
bsalomon@google.coma3201942012-06-21 19:58:20 +0000766namespace {
767// Sets the settings to clip against the stencil buffer clip while ignoring the
768// client bits.
769const GrStencilSettings& basic_apply_stencil_clip_settings() {
770 // stencil settings to use when clip is in stencil
771 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
772 kKeep_StencilOp,
773 kKeep_StencilOp,
774 kAlwaysIfInClip_StencilFunc,
775 0x0000,
776 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000777 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000778 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
779}
780}
781
782void GrClipMaskManager::setGpuStencil() {
783 // We make two copies of the StencilSettings here (except in the early
784 // exit scenario. One copy from draw state to the stack var. Then another
785 // from the stack var to the gpu. We could make this class hold a ptr to
786 // GrGpu's fStencilSettings and eliminate the stack copy here.
787
788 const GrDrawState& drawState = fGpu->getDrawState();
789
790 // use stencil for clipping if clipping is enabled and the clip
791 // has been written into the stencil.
792 GrClipMaskManager::StencilClipMode clipMode;
793 if (this->isClipInStencil() && drawState.isClipState()) {
794 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
795 // We can't be modifying the clip and respecting it at the same time.
796 GrAssert(!drawState.isStateFlagEnabled(
797 GrGpu::kModifyStencilClip_StateBit));
798 } else if (drawState.isStateFlagEnabled(
799 GrGpu::kModifyStencilClip_StateBit)) {
800 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
801 } else {
802 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
803 }
804
805 GrStencilSettings settings;
806 // The GrGpu client may not be using the stencil buffer but we may need to
807 // enable it in order to respect a stencil clip.
808 if (drawState.getStencil().isDisabled()) {
809 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
810 settings = basic_apply_stencil_clip_settings();
811 } else {
812 fGpu->disableStencil();
813 return;
814 }
815 } else {
816 settings = drawState.getStencil();
817 }
818
819 // TODO: dynamically attach a stencil buffer
820 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000821 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000822 drawState.getRenderTarget()->getStencilBuffer();
823 if (NULL != stencilBuffer) {
824 stencilBits = stencilBuffer->bits();
825 }
826
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000827 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() || !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000828 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000829 this->adjustStencilParams(&settings, clipMode, stencilBits);
830 fGpu->setStencilSettings(settings);
831}
832
833void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
834 StencilClipMode mode,
835 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000836 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000837
838 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000839 // We assume that this clip manager itself is drawing to the GrGpu and
840 // has already setup the correct values.
841 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000842 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000843
bsalomon@google.com411dad02012-06-05 20:24:20 +0000844 unsigned int clipBit = (1 << (stencilBitCnt - 1));
845 unsigned int userBits = clipBit - 1;
846
bsalomon@google.coma3201942012-06-21 19:58:20 +0000847 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000848 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000849
bsalomon@google.coma3201942012-06-21 19:58:20 +0000850 bool finished = false;
851 while (!finished) {
852 GrStencilFunc func = settings->func(face);
853 uint16_t writeMask = settings->writeMask(face);
854 uint16_t funcMask = settings->funcMask(face);
855 uint16_t funcRef = settings->funcRef(face);
856
857 GrAssert((unsigned) func < kStencilFuncCount);
858
859 writeMask &= userBits;
860
861 if (func >= kBasicStencilFuncCount) {
862 int respectClip = kRespectClip_StencilClipMode == mode;
863 if (respectClip) {
864 // The GrGpu class should have checked this
865 GrAssert(this->isClipInStencil());
866 switch (func) {
867 case kAlwaysIfInClip_StencilFunc:
868 funcMask = clipBit;
869 funcRef = clipBit;
870 break;
871 case kEqualIfInClip_StencilFunc:
872 case kLessIfInClip_StencilFunc:
873 case kLEqualIfInClip_StencilFunc:
874 funcMask = (funcMask & userBits) | clipBit;
875 funcRef = (funcRef & userBits) | clipBit;
876 break;
877 case kNonZeroIfInClip_StencilFunc:
878 funcMask = (funcMask & userBits) | clipBit;
879 funcRef = clipBit;
880 break;
881 default:
882 GrCrash("Unknown stencil func");
883 }
884 } else {
885 funcMask &= userBits;
886 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000887 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000888 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000889 gSpecialToBasicStencilFunc[respectClip];
890 func = table[func - kBasicStencilFuncCount];
891 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000892 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000893 funcMask &= userBits;
894 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000895 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896
897 settings->setFunc(face, func);
898 settings->setWriteMask(face, writeMask);
899 settings->setFuncMask(face, funcMask);
900 settings->setFuncRef(face, funcRef);
901
902 if (GrStencilSettings::kFront_Face == face) {
903 face = GrStencilSettings::kBack_Face;
904 finished = !twoSided;
905 } else {
906 finished = true;
907 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000908 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000909 if (!twoSided) {
910 settings->copyFrontSettingsToBack();
911 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000912}
913
914////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000915GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
916 GrReducedClip::InitialState initialState,
917 const GrReducedClip::ElementList& elements,
918 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000919 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000920
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000921 GrTexture* result;
922 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
923 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000924 }
925
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000926 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000927 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000928 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000929 }
930
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000931 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
932 // the top left corner of the resulting rect to the top left of the texture.
933 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
934
robertphillips@google.com2c756812012-05-22 20:28:23 +0000935 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000936
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000937 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000938 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
939 SkIntToScalar(-clipSpaceIBounds.fTop));
940 helper.init(maskSpaceIBounds, &matrix);
941
942 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000943
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000944 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000945
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000946 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000947
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000948 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000949 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000950
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000951 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
952 // Intersect and reverse difference require modifying pixels outside of the geometry
953 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
954 // but leave the pixels inside the geometry alone. For reverse difference we invert all
955 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000956 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000957 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000958 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000959 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000960 }
961
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000962 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000963 // convert the rect to a path so we can invert the fill
964 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000965 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000966 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000967
sugoi@google.com12b4e272012-12-06 20:13:11 +0000968 helper.draw(temp, stroke, SkRegion::kReplace_Op,
969 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000970 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000971 } else {
972 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000973 SkPath clipPath = element->getPath();
974 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000975 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000976 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000977 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000978 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000979 }
980
981 continue;
982 }
983
984 // The other ops (union, xor, diff) only affect pixels inside
985 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000986 if (Element::kRect_Type == element->getType()) {
987 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
988 } else {
989 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000990 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000991 }
992 }
993
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000994 helper.toTexture(result, kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000995
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000996 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000997 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000998}
999
robertphillips@google.comf294b772012-04-27 14:29:26 +00001000////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001001void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001002 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001003}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001004
1005void GrClipMaskManager::setGpu(GrGpu* gpu) {
1006 fGpu = gpu;
1007 fAACache.setContext(gpu->getContext());
1008}