blob: 7465283c3818913737f659c2902bee3070609189 [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,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000036 const SkIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037 GrDrawState* drawState = gpu->drawState();
38 GrAssert(drawState);
39
bsalomon@google.comb9086a02012-11-01 18:02:54 +000040 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000041 // We want to use device coords to compute the texture coordinates. We set our matrix to be
42 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
43 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000045 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000046 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047 mat.preConcat(drawState->getViewMatrix());
48
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000049 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000050 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000051 drawState->addCoverageEffect(
52 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000053 mat,
54 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
bsalomon@google.comc7818882013-03-20 19:19:53 +000055 GrTextureDomainEffect::kDecal_WrapMode,
56 false,
57 GrEffect::kPosition_CoordsType))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000058}
59
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000060bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000061 GrGpu* gpu,
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000062 const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000063 const SkStrokeRec& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000064 bool doAA) {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000065 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
66 SkTCopyOnFirstWrite<SkPath> path(origPath);
67 if (path->isInverseFillType()) {
68 path.writable()->toggleInverseFillType();
69 }
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000070 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000071 GrPathRendererChain::DrawType type = doAA ?
72 GrPathRendererChain::kColorAntiAlias_DrawType :
73 GrPathRendererChain::kColor_DrawType;
74
75 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000076}
77
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000078}
79
robertphillips@google.comfa662942012-05-17 12:20:22 +000080/*
81 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
82 * will be used on any element. If so, it returns true to indicate that the
83 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
84 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000085bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000086
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000087 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000088 // a clip gets complex enough it can just be done in SW regardless
89 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000090 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000091
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000092 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
93 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000094 // rects can always be drawn directly w/o using the software path
95 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000096 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000097 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000098 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +000099 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000100 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000101 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000102 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000103 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000104 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000105}
106
robertphillips@google.comf294b772012-04-27 14:29:26 +0000107////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000108// sort out what kind of clip mask needs to be created: alpha, stencil,
109// scissor, or entirely software
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000110bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
111 GrDrawState::AutoRestoreEffects* are) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000112 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000113
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000114 ElementList elements(16);
115 InitialState initialState;
116 SkIRect clipSpaceIBounds;
117 bool requiresAA;
118 bool isRect = false;
119
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000120 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000121
122 const GrRenderTarget* rt = drawState->getRenderTarget();
123 // GrDrawTarget should have filtered this for us
124 GrAssert(NULL != rt);
125
126 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
127
128 if (!ignoreClip) {
129 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
130 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
131 ReduceClipStack(*clipDataIn->fClipStack,
132 clipSpaceRTIBounds,
133 &elements,
134 &initialState,
135 &clipSpaceIBounds,
136 &requiresAA);
137 if (elements.isEmpty()) {
138 if (kAllIn_InitialState == initialState) {
139 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
140 isRect = true;
141 } else {
142 return false;
143 }
144 }
145 }
146
147 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000148 fGpu->disableScissor();
149 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000150 return true;
151 }
152
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000153#if GR_AA_CLIP
154 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000155
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000156 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000157 if (0 == rt->numSamples() && requiresAA) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000158 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000159 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000160
161 if (this->useSWOnlyPath(elements)) {
162 // The clip geometry is complex enough that it will be more efficient to create it
163 // entirely in software
164 result = this->createSoftwareClipMask(genID,
165 initialState,
166 elements,
167 clipSpaceIBounds);
168 } else {
169 result = this->createAlphaClipMask(genID,
170 initialState,
171 elements,
172 clipSpaceIBounds);
173 }
174
175 if (NULL != result) {
176 // The mask's top left coord should be pinned to the rounded-out top left corner of
177 // clipSpace bounds. We determine the mask's position WRT to the render target here.
178 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
179 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000180 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000181 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(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000285 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000286 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000287 } else {
288 fGpu->drawSimpleRect(element->getRect(), NULL);
289 }
290 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000291 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000292 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
293 if (path->isInverseFillType()) {
294 path.writable()->toggleInverseFillType();
295 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000296 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000297 if (NULL == pr) {
298 GrPathRendererChain::DrawType type;
299 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
300 GrPathRendererChain::kColor_DrawType;
301 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
302 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000303 if (NULL == pr) {
304 return false;
305 }
306 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
307 break;
308 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000309 default:
310 // something is wrong if we're trying to draw an empty element.
311 GrCrash("Unexpected element type");
312 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000313 }
314 return true;
315}
316
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000317bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
318 const SkClipStack::Element* element,
319 GrPathRenderer** pr) {
320 GrDrawState* drawState = fGpu->drawState();
321 drawState->setRenderTarget(target->asRenderTarget());
322
323 switch (element->getType()) {
324 case Element::kRect_Type:
325 return true;
326 case Element::kPath_Type: {
327 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
328 if (path->isInverseFillType()) {
329 path.writable()->toggleInverseFillType();
330 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000331 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000332 GrPathRendererChain::DrawType type = element->isAA() ?
333 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
334 GrPathRendererChain::kStencilAndColor_DrawType;
335 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
336 return NULL != *pr;
337 }
338 default:
339 // something is wrong if we're trying to draw an empty element.
340 GrCrash("Unexpected element type");
341 return false;
342 }
343}
344
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000345void GrClipMaskManager::mergeMask(GrTexture* dstMask,
346 GrTexture* srcMask,
347 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000348 const SkIRect& dstBound,
349 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000350 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000351 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000352 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000353 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000354
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000355 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000356
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000357 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000358
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000359 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000360 sampleM.setIDiv(srcMask->width(), srcMask->height());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000361 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000362 GrTextureDomainEffect::Create(srcMask,
363 sampleM,
364 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
bsalomon@google.comc7818882013-03-20 19:19:53 +0000365 GrTextureDomainEffect::kDecal_WrapMode,
366 false))->unref();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000367 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
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
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000438 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000439 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 SkIntToScalar(-clipSpaceIBounds.fLeft),
441 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000442 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443 // The texture may be larger than necessary, this rect represents the part of the texture
444 // we populate with a rasterization of the clip.
445 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
446
bsalomon@google.com137f1342013-05-29 21:27:53 +0000447 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
448 SkMatrix translate;
449 translate.setTranslate(clipToMaskOffset);
450 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
451
452 GrDrawState* drawState = fGpu->drawState();
453
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000454 // We're drawing a coverage mask and want coverage to be run through the blend function.
455 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
456
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.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000485 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000486
487 if (useTemp) {
488 if (invert) {
489 maskSpaceElementIBounds = maskSpaceIBounds;
490 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000491 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000492 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
bsalomon@google.com137f1342013-05-29 21:27:53 +0000591 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
592 SkVector translate = {
593 SkIntToScalar(clipSpaceToStencilOffset.fX),
594 SkIntToScalar(clipSpaceToStencilOffset.fY)
595 };
596 SkMatrix matrix;
597 matrix.setTranslate(translate);
598 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000599 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000600
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000601 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000602
bsalomon@google.com9f131742012-12-13 20:43:56 +0000603 // We set the current clip to the bounds so that our recursive draws are scissored to them.
604 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
605 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
606 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
607 drawState->enableState(GrDrawState::kClip_StateBit);
608
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609#if !VISUALIZE_COMPLEX_CLIP
610 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
611#endif
612
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000613 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000614 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000615 clipBit = (1 << (clipBit-1));
616
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000617 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000618
619 // walk through each clip element and perform its set op
620 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000621 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
622 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000623 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000624 // enabled at bottom of loop
625 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000626 // if the target is MSAA then we want MSAA enabled when the clip is soft
627 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000628 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000629 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000630
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000631 // This will be used to determine whether the clip shape can be rendered into the
632 // stencil with arbitrary stencil settings.
633 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000634
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000635 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000636
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000637 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000638
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000640 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000641 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000642 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000643 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000644 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000645 GrAssert(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000646 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000647 fillInverted = clipPath->isInverseFillType();
648 if (fillInverted) {
649 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000650 }
651 pr = this->getContext()->getPathRenderer(*clipPath,
652 stroke,
653 fGpu,
654 false,
655 GrPathRendererChain::kStencilOnly_DrawType,
656 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000657 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000658 return false;
659 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000660 }
661
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000662 int passes;
663 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
664
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000665 bool canRenderDirectToStencil =
666 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000668 // fill rule, and set operation can
669 // we render the element directly to
670 // stencil bit used for clipping.
671 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
672 canRenderDirectToStencil,
673 clipBit,
674 fillInverted,
675 &passes,
676 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000677
678 // draw the element to the client stencil bits if necessary
679 if (!canDrawDirectToClip) {
680 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000681 kIncClamp_StencilOp,
682 kIncClamp_StencilOp,
683 kAlways_StencilFunc,
684 0xffff,
685 0x0000,
686 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000688 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000690 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000692 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693 if (canRenderDirectToStencil) {
694 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000695 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696 } else {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000697 pr->stencilPath(*clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698 }
699 }
700 }
701
702 // now we modify the clip bit by rendering either the clip
703 // element directly or a bounding rect of the entire clip.
704 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
705 for (int p = 0; p < passes; ++p) {
706 *drawState->stencil() = stencilSettings[p];
707 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000708 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000709 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000710 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000712 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000714 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715 }
716 } else {
717 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000718 // The view matrix is setup to do clip space -> stencil space translation, so
719 // draw rect in clip space.
720 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721 }
722 }
723 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000725 // set this last because recursive draws may overwrite it back to kNone.
726 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
727 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728 return true;
729}
730
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000731
bsalomon@google.com411dad02012-06-05 20:24:20 +0000732// mapping of clip-respecting stencil funcs to normal stencil funcs
733// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000734static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000735 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
736 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
737 // In the Clip Funcs
738 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
739 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
740 kLess_StencilFunc, // kLessIfInClip_StencilFunc
741 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
742 // Special in the clip func that forces user's ref to be 0.
743 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
744 // make ref 0 and do normal nequal.
745 },
746 {// Stencil-Clipping is ENABLED
747 // In the Clip Funcs
748 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
749 // eq stencil clip bit, mask
750 // out user bits.
751
752 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
753 // add stencil bit to mask and ref
754
755 kLess_StencilFunc, // kLessIfInClip_StencilFunc
756 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
757 // for both of these we can add
758 // the clip bit to the mask and
759 // ref and compare as normal
760 // Special in the clip func that forces user's ref to be 0.
761 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
762 // make ref have only the clip bit set
763 // and make comparison be less
764 // 10..0 < 1..user_bits..
765 }
766};
767
bsalomon@google.coma3201942012-06-21 19:58:20 +0000768namespace {
769// Sets the settings to clip against the stencil buffer clip while ignoring the
770// client bits.
771const GrStencilSettings& basic_apply_stencil_clip_settings() {
772 // stencil settings to use when clip is in stencil
773 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
774 kKeep_StencilOp,
775 kKeep_StencilOp,
776 kAlwaysIfInClip_StencilFunc,
777 0x0000,
778 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000779 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000780 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
781}
782}
783
784void GrClipMaskManager::setGpuStencil() {
785 // We make two copies of the StencilSettings here (except in the early
786 // exit scenario. One copy from draw state to the stack var. Then another
787 // from the stack var to the gpu. We could make this class hold a ptr to
788 // GrGpu's fStencilSettings and eliminate the stack copy here.
789
790 const GrDrawState& drawState = fGpu->getDrawState();
791
792 // use stencil for clipping if clipping is enabled and the clip
793 // has been written into the stencil.
794 GrClipMaskManager::StencilClipMode clipMode;
795 if (this->isClipInStencil() && drawState.isClipState()) {
796 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
797 // We can't be modifying the clip and respecting it at the same time.
798 GrAssert(!drawState.isStateFlagEnabled(
799 GrGpu::kModifyStencilClip_StateBit));
800 } else if (drawState.isStateFlagEnabled(
801 GrGpu::kModifyStencilClip_StateBit)) {
802 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
803 } else {
804 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
805 }
806
807 GrStencilSettings settings;
808 // The GrGpu client may not be using the stencil buffer but we may need to
809 // enable it in order to respect a stencil clip.
810 if (drawState.getStencil().isDisabled()) {
811 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
812 settings = basic_apply_stencil_clip_settings();
813 } else {
814 fGpu->disableStencil();
815 return;
816 }
817 } else {
818 settings = drawState.getStencil();
819 }
820
821 // TODO: dynamically attach a stencil buffer
822 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000823 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000824 drawState.getRenderTarget()->getStencilBuffer();
825 if (NULL != stencilBuffer) {
826 stencilBits = stencilBuffer->bits();
827 }
828
bsalomon@google.combcce8922013-03-25 15:38:39 +0000829 GrAssert(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
830 GrAssert(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000831 this->adjustStencilParams(&settings, clipMode, stencilBits);
832 fGpu->setStencilSettings(settings);
833}
834
835void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
836 StencilClipMode mode,
837 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000838 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000839
840 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000841 // We assume that this clip manager itself is drawing to the GrGpu and
842 // has already setup the correct values.
843 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000844 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000845
bsalomon@google.com411dad02012-06-05 20:24:20 +0000846 unsigned int clipBit = (1 << (stencilBitCnt - 1));
847 unsigned int userBits = clipBit - 1;
848
bsalomon@google.coma3201942012-06-21 19:58:20 +0000849 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000850 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000851
bsalomon@google.coma3201942012-06-21 19:58:20 +0000852 bool finished = false;
853 while (!finished) {
854 GrStencilFunc func = settings->func(face);
855 uint16_t writeMask = settings->writeMask(face);
856 uint16_t funcMask = settings->funcMask(face);
857 uint16_t funcRef = settings->funcRef(face);
858
859 GrAssert((unsigned) func < kStencilFuncCount);
860
861 writeMask &= userBits;
862
863 if (func >= kBasicStencilFuncCount) {
864 int respectClip = kRespectClip_StencilClipMode == mode;
865 if (respectClip) {
866 // The GrGpu class should have checked this
867 GrAssert(this->isClipInStencil());
868 switch (func) {
869 case kAlwaysIfInClip_StencilFunc:
870 funcMask = clipBit;
871 funcRef = clipBit;
872 break;
873 case kEqualIfInClip_StencilFunc:
874 case kLessIfInClip_StencilFunc:
875 case kLEqualIfInClip_StencilFunc:
876 funcMask = (funcMask & userBits) | clipBit;
877 funcRef = (funcRef & userBits) | clipBit;
878 break;
879 case kNonZeroIfInClip_StencilFunc:
880 funcMask = (funcMask & userBits) | clipBit;
881 funcRef = clipBit;
882 break;
883 default:
884 GrCrash("Unknown stencil func");
885 }
886 } else {
887 funcMask &= userBits;
888 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000889 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000890 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000891 gSpecialToBasicStencilFunc[respectClip];
892 func = table[func - kBasicStencilFuncCount];
893 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000894 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000895 funcMask &= userBits;
896 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000897 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000898
899 settings->setFunc(face, func);
900 settings->setWriteMask(face, writeMask);
901 settings->setFuncMask(face, funcMask);
902 settings->setFuncRef(face, funcRef);
903
904 if (GrStencilSettings::kFront_Face == face) {
905 face = GrStencilSettings::kBack_Face;
906 finished = !twoSided;
907 } else {
908 finished = true;
909 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000910 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000911 if (!twoSided) {
912 settings->copyFrontSettingsToBack();
913 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000914}
915
916////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000917GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
918 GrReducedClip::InitialState initialState,
919 const GrReducedClip::ElementList& elements,
920 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000921 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000922
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000923 GrTexture* result;
924 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
925 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000926 }
927
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000928 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000929 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000930 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000931 }
932
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000933 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
934 // the top left corner of the resulting rect to the top left of the texture.
935 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
936
robertphillips@google.com2c756812012-05-22 20:28:23 +0000937 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000938
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000939 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000940 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
941 SkIntToScalar(-clipSpaceIBounds.fTop));
942 helper.init(maskSpaceIBounds, &matrix);
943
944 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000945
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000946 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000947
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000948 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000949
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000950 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000951 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000952
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000953 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
954 // Intersect and reverse difference require modifying pixels outside of the geometry
955 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
956 // but leave the pixels inside the geometry alone. For reverse difference we invert all
957 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000958 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000959 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000960 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000961 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000962 }
963
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000964 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000965 // convert the rect to a path so we can invert the fill
966 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000967 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000968 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000969
sugoi@google.com12b4e272012-12-06 20:13:11 +0000970 helper.draw(temp, stroke, SkRegion::kReplace_Op,
971 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000972 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000973 } else {
974 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000975 SkPath clipPath = element->getPath();
976 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000977 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000978 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000979 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000980 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000981 }
982
983 continue;
984 }
985
986 // The other ops (union, xor, diff) only affect pixels inside
987 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000988 if (Element::kRect_Type == element->getType()) {
989 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
990 } else {
991 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000992 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000993 }
994 }
995
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +0000996 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000997
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000998 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000999 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001000}
1001
robertphillips@google.comf294b772012-04-27 14:29:26 +00001002////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001003void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001004 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001005}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001006
1007void GrClipMaskManager::setGpu(GrGpu* gpu) {
1008 fGpu = gpu;
1009 fAACache.setContext(gpu->getContext());
1010}