blob: 806928ca1412796f6049e720223d915264363442 [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,
humper@google.comb86add12013-07-25 18:49:07 +000056 GrTextureParams::kNone_FilterMode,
bsalomon@google.comc7818882013-03-20 19:19:53 +000057 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());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000361
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000362 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000363 GrTextureDomainEffect::Create(srcMask,
364 sampleM,
365 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
bsalomon@google.comc7818882013-03-20 19:19:53 +0000366 GrTextureDomainEffect::kDecal_WrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000367 GrTextureParams::kNone_FilterMode))->unref();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000368 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369}
370
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000371// get a texture to act as a temporary buffer for AA clip boolean operations
372// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000373void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000374 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000375 // we've already allocated the temp texture
376 return;
377 }
378
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000379 GrTextureDesc desc;
380 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000381 desc.fWidth = width;
382 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000383 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000384
robertphillips@google.com2c756812012-05-22 20:28:23 +0000385 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000386}
387
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000388////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000389// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
390// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
391// hit)
392bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
393 const SkIRect& clipSpaceIBounds,
394 GrTexture** result) {
395 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
396 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000398 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
399 // Since we are setting up the cache we know the last lookup was a miss. Free up the
400 // currently cached mask so it can be reused.
401 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000403 GrTextureDesc desc;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000404 desc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000405 desc.fWidth = clipSpaceIBounds.width();
406 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000407 desc.fConfig = kRGBA_8888_GrPixelConfig;
408 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
409 // We would always like A8 but it isn't supported on all platforms
410 desc.fConfig = kAlpha_8_GrPixelConfig;
411 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000412
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000413 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000414 }
415
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000416 *result = fAACache.getLastMask();
417 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000418}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000419
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000420////////////////////////////////////////////////////////////////////////////////
421// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000422GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
423 InitialState initialState,
424 const ElementList& elements,
425 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000426 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
427
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000428 GrTexture* result;
429 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000430 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000431 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000432 }
433
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000434 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000435 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000436 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437 }
438
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000439 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000440 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000441 SkIntToScalar(-clipSpaceIBounds.fLeft),
442 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000443 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000444 // The texture may be larger than necessary, this rect represents the part of the texture
445 // we populate with a rasterization of the clip.
446 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
447
bsalomon@google.com137f1342013-05-29 21:27:53 +0000448 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
449 SkMatrix translate;
450 translate.setTranslate(clipToMaskOffset);
451 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
452
453 GrDrawState* drawState = fGpu->drawState();
454
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000455 // We're drawing a coverage mask and want coverage to be run through the blend function.
456 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
457
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000458 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
459 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000460 fGpu->clear(&maskSpaceIBounds,
461 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
462 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000463
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000464 // When we use the stencil in the below loop it is important to have this clip installed.
465 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
466 // pass must not set values outside of this bounds or stencil values outside the rect won't be
467 // cleared.
468 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
469 drawState->enableState(GrDrawState::kClip_StateBit);
470
robertphillips@google.comf105b102012-05-14 12:18:26 +0000471 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000472 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000473 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000474 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000475 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000476 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000477
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000478 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
479 GrPathRenderer* pr = NULL;
480 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
481 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000482 // 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 +0000483 // mask buffer can be substantially larger than the actually clip stack element. We
484 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000485 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000486 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000487
488 if (useTemp) {
489 if (invert) {
490 maskSpaceElementIBounds = maskSpaceIBounds;
491 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000492 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000493 elementBounds.offset(clipToMaskOffset);
494 elementBounds.roundOut(&maskSpaceElementIBounds);
495 }
496
497 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
498 if (NULL == temp.texture()) {
499 fAACache.reset();
500 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000501 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000502 dst = temp.texture();
503 // clear the temp target and set blend to replace
504 fGpu->clear(&maskSpaceElementIBounds,
505 invert ? 0xffffffff : 0x00000000,
506 dst->asRenderTarget());
507 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000508
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000509 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000510 // draw directly into the result with the stencil set to make the pixels affected
511 // by the clip shape be non-zero.
512 dst = result;
513 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
514 kReplace_StencilOp,
515 kReplace_StencilOp,
516 kAlways_StencilFunc,
517 0xffff,
518 0xffff,
519 0xffff);
520 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000521 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000522 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000523
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000524 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000525
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000526 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000527 fAACache.reset();
528 return NULL;
529 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000530
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000531 if (useTemp) {
532 // Now draw into the accumulator using the real operation and the temp buffer as a
533 // texture
534 this->mergeMask(result,
535 temp.texture(),
536 op,
537 maskSpaceIBounds,
538 maskSpaceElementIBounds);
539 } else {
540 // Draw to the exterior pixels (those with a zero stencil value).
541 drawState->setAlpha(invert ? 0xff : 0x00);
542 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
543 kZero_StencilOp,
544 kZero_StencilOp,
545 kEqual_StencilFunc,
546 0xffff,
547 0x0000,
548 0xffff);
549 drawState->setStencil(kDrawOutsideElement);
550 fGpu->drawSimpleRect(clipSpaceIBounds);
551 drawState->disableStencil();
552 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000553 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000554 // all the remaining ops can just be directly draw into the accumulation buffer
555 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000556 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000557 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000558 }
559 }
560
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000561 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000562 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563}
564
565////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000566// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000567// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000568bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
569 const ElementList& elements,
570 const SkIRect& clipSpaceIBounds,
571 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000572
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000573 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000574
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000575 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000576 GrAssert(drawState->isClipState());
577
578 GrRenderTarget* rt = drawState->getRenderTarget();
579 GrAssert(NULL != rt);
580
581 // TODO: dynamically attach a SB when needed.
582 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
583 if (NULL == stencilBuffer) {
584 return false;
585 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000586 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000587
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000588 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000589
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000590 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000591
bsalomon@google.com137f1342013-05-29 21:27:53 +0000592 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
593 SkVector translate = {
594 SkIntToScalar(clipSpaceToStencilOffset.fX),
595 SkIntToScalar(clipSpaceToStencilOffset.fY)
596 };
597 SkMatrix matrix;
598 matrix.setTranslate(translate);
599 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000600 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000601
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000602 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000603
bsalomon@google.com9f131742012-12-13 20:43:56 +0000604 // We set the current clip to the bounds so that our recursive draws are scissored to them.
605 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
606 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
607 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
608 drawState->enableState(GrDrawState::kClip_StateBit);
609
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000610#if !VISUALIZE_COMPLEX_CLIP
611 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
612#endif
613
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000614 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000615 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000616 clipBit = (1 << (clipBit-1));
617
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000618 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000619
620 // walk through each clip element and perform its set op
621 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000622 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
623 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000624 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000625 // enabled at bottom of loop
626 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000627 // if the target is MSAA then we want MSAA enabled when the clip is soft
628 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000629 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000630 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000632 // This will be used to determine whether the clip shape can be rendered into the
633 // stencil with arbitrary stencil settings.
634 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000635
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000636 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000637
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000638 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000639
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000640 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000641 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000642 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000643 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000644 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000645 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000646 GrAssert(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000647 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000648 fillInverted = clipPath->isInverseFillType();
649 if (fillInverted) {
650 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000651 }
652 pr = this->getContext()->getPathRenderer(*clipPath,
653 stroke,
654 fGpu,
655 false,
656 GrPathRendererChain::kStencilOnly_DrawType,
657 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000658 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 return false;
660 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000661 }
662
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663 int passes;
664 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
665
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000666 bool canRenderDirectToStencil =
667 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000668 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000669 // fill rule, and set operation can
670 // we render the element directly to
671 // stencil bit used for clipping.
672 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
673 canRenderDirectToStencil,
674 clipBit,
675 fillInverted,
676 &passes,
677 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000678
679 // draw the element to the client stencil bits if necessary
680 if (!canDrawDirectToClip) {
681 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000682 kIncClamp_StencilOp,
683 kIncClamp_StencilOp,
684 kAlways_StencilFunc,
685 0xffff,
686 0x0000,
687 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000689 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000691 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000693 GrAssert(Element::kPath_Type == element->getType());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000694 if (!clipPath->isEmpty()) {
695 if (canRenderDirectToStencil) {
696 *drawState->stencil() = gDrawToStencil;
697 pr->drawPath(*clipPath, stroke, fGpu, false);
698 } else {
699 pr->stencilPath(*clipPath, stroke, fGpu);
700 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000701 }
702 }
703 }
704
705 // now we modify the clip bit by rendering either the clip
706 // element directly or a bounding rect of the entire clip.
707 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
708 for (int p = 0; p < passes; ++p) {
709 *drawState->stencil() = stencilSettings[p];
710 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000711 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000713 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000715 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000717 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 }
719 } else {
720 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000721 // The view matrix is setup to do clip space -> stencil space translation, so
722 // draw rect in clip space.
723 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724 }
725 }
726 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000728 // set this last because recursive draws may overwrite it back to kNone.
729 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
730 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000731 return true;
732}
733
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000734
bsalomon@google.com411dad02012-06-05 20:24:20 +0000735// mapping of clip-respecting stencil funcs to normal stencil funcs
736// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000737static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000738 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
739 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
740 // In the Clip Funcs
741 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
742 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
743 kLess_StencilFunc, // kLessIfInClip_StencilFunc
744 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
745 // Special in the clip func that forces user's ref to be 0.
746 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
747 // make ref 0 and do normal nequal.
748 },
749 {// Stencil-Clipping is ENABLED
750 // In the Clip Funcs
751 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
752 // eq stencil clip bit, mask
753 // out user bits.
754
755 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
756 // add stencil bit to mask and ref
757
758 kLess_StencilFunc, // kLessIfInClip_StencilFunc
759 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
760 // for both of these we can add
761 // the clip bit to the mask and
762 // ref and compare as normal
763 // Special in the clip func that forces user's ref to be 0.
764 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
765 // make ref have only the clip bit set
766 // and make comparison be less
767 // 10..0 < 1..user_bits..
768 }
769};
770
bsalomon@google.coma3201942012-06-21 19:58:20 +0000771namespace {
772// Sets the settings to clip against the stencil buffer clip while ignoring the
773// client bits.
774const GrStencilSettings& basic_apply_stencil_clip_settings() {
775 // stencil settings to use when clip is in stencil
776 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
777 kKeep_StencilOp,
778 kKeep_StencilOp,
779 kAlwaysIfInClip_StencilFunc,
780 0x0000,
781 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000782 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000783 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
784}
785}
786
787void GrClipMaskManager::setGpuStencil() {
788 // We make two copies of the StencilSettings here (except in the early
789 // exit scenario. One copy from draw state to the stack var. Then another
790 // from the stack var to the gpu. We could make this class hold a ptr to
791 // GrGpu's fStencilSettings and eliminate the stack copy here.
792
793 const GrDrawState& drawState = fGpu->getDrawState();
794
795 // use stencil for clipping if clipping is enabled and the clip
796 // has been written into the stencil.
797 GrClipMaskManager::StencilClipMode clipMode;
798 if (this->isClipInStencil() && drawState.isClipState()) {
799 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
800 // We can't be modifying the clip and respecting it at the same time.
801 GrAssert(!drawState.isStateFlagEnabled(
802 GrGpu::kModifyStencilClip_StateBit));
803 } else if (drawState.isStateFlagEnabled(
804 GrGpu::kModifyStencilClip_StateBit)) {
805 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
806 } else {
807 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
808 }
809
810 GrStencilSettings settings;
811 // The GrGpu client may not be using the stencil buffer but we may need to
812 // enable it in order to respect a stencil clip.
813 if (drawState.getStencil().isDisabled()) {
814 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
815 settings = basic_apply_stencil_clip_settings();
816 } else {
817 fGpu->disableStencil();
818 return;
819 }
820 } else {
821 settings = drawState.getStencil();
822 }
823
824 // TODO: dynamically attach a stencil buffer
825 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000826 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000827 drawState.getRenderTarget()->getStencilBuffer();
828 if (NULL != stencilBuffer) {
829 stencilBits = stencilBuffer->bits();
830 }
831
bsalomon@google.combcce8922013-03-25 15:38:39 +0000832 GrAssert(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
833 GrAssert(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000834 this->adjustStencilParams(&settings, clipMode, stencilBits);
835 fGpu->setStencilSettings(settings);
836}
837
838void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
839 StencilClipMode mode,
840 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000841 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000842
843 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000844 // We assume that this clip manager itself is drawing to the GrGpu and
845 // has already setup the correct values.
846 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000847 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000848
bsalomon@google.com411dad02012-06-05 20:24:20 +0000849 unsigned int clipBit = (1 << (stencilBitCnt - 1));
850 unsigned int userBits = clipBit - 1;
851
bsalomon@google.coma3201942012-06-21 19:58:20 +0000852 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000853 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000854
bsalomon@google.coma3201942012-06-21 19:58:20 +0000855 bool finished = false;
856 while (!finished) {
857 GrStencilFunc func = settings->func(face);
858 uint16_t writeMask = settings->writeMask(face);
859 uint16_t funcMask = settings->funcMask(face);
860 uint16_t funcRef = settings->funcRef(face);
861
862 GrAssert((unsigned) func < kStencilFuncCount);
863
864 writeMask &= userBits;
865
866 if (func >= kBasicStencilFuncCount) {
867 int respectClip = kRespectClip_StencilClipMode == mode;
868 if (respectClip) {
869 // The GrGpu class should have checked this
870 GrAssert(this->isClipInStencil());
871 switch (func) {
872 case kAlwaysIfInClip_StencilFunc:
873 funcMask = clipBit;
874 funcRef = clipBit;
875 break;
876 case kEqualIfInClip_StencilFunc:
877 case kLessIfInClip_StencilFunc:
878 case kLEqualIfInClip_StencilFunc:
879 funcMask = (funcMask & userBits) | clipBit;
880 funcRef = (funcRef & userBits) | clipBit;
881 break;
882 case kNonZeroIfInClip_StencilFunc:
883 funcMask = (funcMask & userBits) | clipBit;
884 funcRef = clipBit;
885 break;
886 default:
887 GrCrash("Unknown stencil func");
888 }
889 } else {
890 funcMask &= userBits;
891 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000892 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000893 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000894 gSpecialToBasicStencilFunc[respectClip];
895 func = table[func - kBasicStencilFuncCount];
896 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000897 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000898 funcMask &= userBits;
899 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000900 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000901
902 settings->setFunc(face, func);
903 settings->setWriteMask(face, writeMask);
904 settings->setFuncMask(face, funcMask);
905 settings->setFuncRef(face, funcRef);
906
907 if (GrStencilSettings::kFront_Face == face) {
908 face = GrStencilSettings::kBack_Face;
909 finished = !twoSided;
910 } else {
911 finished = true;
912 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000913 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000914 if (!twoSided) {
915 settings->copyFrontSettingsToBack();
916 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000917}
918
919////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000920GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
921 GrReducedClip::InitialState initialState,
922 const GrReducedClip::ElementList& elements,
923 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000924 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000925
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000926 GrTexture* result;
927 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
928 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000929 }
930
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000931 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000932 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000933 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000934 }
935
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000936 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
937 // the top left corner of the resulting rect to the top left of the texture.
938 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
939
robertphillips@google.com2c756812012-05-22 20:28:23 +0000940 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000941
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000942 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000943 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
944 SkIntToScalar(-clipSpaceIBounds.fTop));
945 helper.init(maskSpaceIBounds, &matrix);
946
947 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000948
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000949 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000950
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000951 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000952
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000953 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000954 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000955
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000956 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
957 // Intersect and reverse difference require modifying pixels outside of the geometry
958 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
959 // but leave the pixels inside the geometry alone. For reverse difference we invert all
960 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000961 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000962 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000963 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000964 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000965 }
966
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000967 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000968 // convert the rect to a path so we can invert the fill
969 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000970 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000971 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000972
sugoi@google.com12b4e272012-12-06 20:13:11 +0000973 helper.draw(temp, stroke, SkRegion::kReplace_Op,
974 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000975 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000976 } else {
977 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000978 SkPath clipPath = element->getPath();
979 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000980 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000981 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000982 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000983 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000984 }
985
986 continue;
987 }
988
989 // The other ops (union, xor, diff) only affect pixels inside
990 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000991 if (Element::kRect_Type == element->getType()) {
992 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
993 } else {
994 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000995 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000996 }
997 }
998
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +0000999 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001000
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001001 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001002 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001003}
1004
robertphillips@google.comf294b772012-04-27 14:29:26 +00001005////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001006void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001007 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001008}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001009
1010void GrClipMaskManager::setGpu(GrGpu* gpu) {
1011 fGpu = gpu;
1012 fAACache.setContext(gpu->getContext());
1013}