blob: bc7b79188bd3175af036d54d7509941550ade932 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000010#include "effects/GrTextureDomainEffect.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000011#include "GrGpu.h"
12#include "GrRenderTarget.h"
13#include "GrStencilBuffer.h"
14#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000015#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000016#include "SkRasterClip.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000017#include "SkStrokeRec.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000018#include "GrAAConvexPathRenderer.h"
19#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000021
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000022#include "SkTLazy.h"
23
robertphillips@google.comba998f22012-10-12 11:33:56 +000024#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000025
bsalomon@google.com8182fa02012-12-04 14:06:06 +000026typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000027
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000028using namespace GrReducedClip;
29
bsalomon@google.com51a62862012-11-26 21:19:43 +000030////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000031namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000033// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034void setup_drawstate_aaclip(GrGpu* gpu,
35 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000036 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037 GrDrawState* drawState = gpu->drawState();
38 GrAssert(drawState);
39
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000040 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041
bsalomon@google.comb9086a02012-11-01 18:02:54 +000042 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000045 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046 mat.preConcat(drawState->getViewMatrix());
47
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000048 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000049 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.comadc65362013-01-28 14:26:09 +000050 drawState->setEffect(kMaskStage,
51 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052 mat,
53 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
54 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000055}
56
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000057bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000058 GrGpu* gpu,
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000059 const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000060 const SkStrokeRec& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000061 bool doAA) {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000062 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
63 SkTCopyOnFirstWrite<SkPath> path(origPath);
64 if (path->isInverseFillType()) {
65 path.writable()->toggleInverseFillType();
66 }
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000067 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000068 GrPathRendererChain::DrawType type = doAA ?
69 GrPathRendererChain::kColorAntiAlias_DrawType :
70 GrPathRendererChain::kColor_DrawType;
71
72 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000073}
74
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000075}
76
robertphillips@google.comfa662942012-05-17 12:20:22 +000077/*
78 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
79 * will be used on any element. If so, it returns true to indicate that the
80 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
81 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000082bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000083
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000084 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000085 // a clip gets complex enough it can just be done in SW regardless
86 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000087 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000088
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000089 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
90 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000091 // rects can always be drawn directly w/o using the software path
92 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000093 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000094 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000095 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +000096 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000097 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000098 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +000099 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000100 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000101 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000102}
103
robertphillips@google.comf294b772012-04-27 14:29:26 +0000104////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000105// sort out what kind of clip mask needs to be created: alpha, stencil,
106// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000107bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000108 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000109
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000110 ElementList elements(16);
111 InitialState initialState;
112 SkIRect clipSpaceIBounds;
113 bool requiresAA;
114 bool isRect = false;
115
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000116 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000117
118 const GrRenderTarget* rt = drawState->getRenderTarget();
119 // GrDrawTarget should have filtered this for us
120 GrAssert(NULL != rt);
121
122 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
123
124 if (!ignoreClip) {
125 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
126 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
127 ReduceClipStack(*clipDataIn->fClipStack,
128 clipSpaceRTIBounds,
129 &elements,
130 &initialState,
131 &clipSpaceIBounds,
132 &requiresAA);
133 if (elements.isEmpty()) {
134 if (kAllIn_InitialState == initialState) {
135 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
136 isRect = true;
137 } else {
138 return false;
139 }
140 }
141 }
142
143 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000144 fGpu->disableScissor();
145 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000146 return true;
147 }
148
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000149#if GR_AA_CLIP
150 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000151
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000152 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000153 if (0 == rt->numSamples() && requiresAA) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000154 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000155 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000156
157 if (this->useSWOnlyPath(elements)) {
158 // The clip geometry is complex enough that it will be more efficient to create it
159 // entirely in software
160 result = this->createSoftwareClipMask(genID,
161 initialState,
162 elements,
163 clipSpaceIBounds);
164 } else {
165 result = this->createAlphaClipMask(genID,
166 initialState,
167 elements,
168 clipSpaceIBounds);
169 }
170
171 if (NULL != result) {
172 // The mask's top left coord should be pinned to the rounded-out top left corner of
173 // clipSpace bounds. We determine the mask's position WRT to the render target here.
174 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
175 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
176 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000177 fGpu->disableScissor();
178 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000179 return true;
180 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000181 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000182 }
183#endif // GR_AA_CLIP
184
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000185 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
186 // be created. In either case, free up the texture in the anti-aliased mask cache.
187 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
188 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
189 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000190 fAACache.reset();
191
bsalomon@google.coma3201942012-06-21 19:58:20 +0000192 // If the clip is a rectangle then just set the scissor. Otherwise, create
193 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000194 if (isRect) {
195 SkIRect clipRect = clipSpaceIBounds;
196 clipRect.offset(-clipDataIn->fOrigin);
197 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000198 this->setGpuStencil();
199 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000200 }
201
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000202 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000203 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
204 this->createStencilClipMask(initialState,
205 elements,
206 clipSpaceIBounds,
207 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000208
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000209 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
210 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
211 // use both stencil and scissor test to the bounds for the final draw.
212 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
213 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
214 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000215 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000216 return true;
217}
218
219#define VISUALIZE_COMPLEX_CLIP 0
220
221#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000222 #include "SkRandom.h"
223 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000224 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
225#else
226 #define SET_RANDOM_COLOR
227#endif
228
229namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000230
231////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000232// set up the OpenGL blend function to perform the specified
233// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000234void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000235
236 switch (op) {
237 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000238 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000239 break;
240 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000241 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000242 break;
243 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000244 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000245 break;
246 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000247 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000248 break;
249 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000250 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000251 break;
252 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000253 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000254 break;
255 default:
256 GrAssert(false);
257 break;
258 }
259}
260
robertphillips@google.com72176b22012-05-23 13:19:12 +0000261}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000262
263////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000264bool GrClipMaskManager::drawElement(GrTexture* target,
265 const SkClipStack::Element* element,
266 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000267 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000268
269 drawState->setRenderTarget(target->asRenderTarget());
270
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000271 switch (element->getType()) {
272 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000273 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
274 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000275 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000276 getContext()->getAARectRenderer()->fillAARect(fGpu,
277 fGpu,
278 element->getRect(),
279 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000280 } else {
281 fGpu->drawSimpleRect(element->getRect(), NULL);
282 }
283 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000284 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000285 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
286 if (path->isInverseFillType()) {
287 path.writable()->toggleInverseFillType();
288 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000289 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000290 if (NULL == pr) {
291 GrPathRendererChain::DrawType type;
292 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
293 GrPathRendererChain::kColor_DrawType;
294 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
295 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000296 if (NULL == pr) {
297 return false;
298 }
299 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
300 break;
301 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000302 default:
303 // something is wrong if we're trying to draw an empty element.
304 GrCrash("Unexpected element type");
305 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000306 }
307 return true;
308}
309
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000310bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
311 const SkClipStack::Element* element,
312 GrPathRenderer** pr) {
313 GrDrawState* drawState = fGpu->drawState();
314 drawState->setRenderTarget(target->asRenderTarget());
315
316 switch (element->getType()) {
317 case Element::kRect_Type:
318 return true;
319 case Element::kPath_Type: {
320 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
321 if (path->isInverseFillType()) {
322 path.writable()->toggleInverseFillType();
323 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000324 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000325 GrPathRendererChain::DrawType type = element->isAA() ?
326 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
327 GrPathRendererChain::kStencilAndColor_DrawType;
328 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
329 return NULL != *pr;
330 }
331 default:
332 // something is wrong if we're trying to draw an empty element.
333 GrCrash("Unexpected element type");
334 return false;
335 }
336}
337
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000338void GrClipMaskManager::mergeMask(GrTexture* dstMask,
339 GrTexture* srcMask,
340 SkRegion::Op op,
341 const GrIRect& dstBound,
342 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000343 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000344 SkMatrix oldMatrix = drawState->getViewMatrix();
345 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000346
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000347 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000349 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000350
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000351 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000352 sampleM.setIDiv(srcMask->width(), srcMask->height());
bsalomon@google.comadc65362013-01-28 14:26:09 +0000353 drawState->setEffect(0,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000354 GrTextureDomainEffect::Create(srcMask,
355 sampleM,
356 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
357 GrTextureDomainEffect::kDecal_WrapMode))->unref();
358 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359
tomhudson@google.com676e6602012-07-10 17:21:48 +0000360 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000361 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000362}
363
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000364// get a texture to act as a temporary buffer for AA clip boolean operations
365// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000366void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000367 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000368 // we've already allocated the temp texture
369 return;
370 }
371
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000372 GrTextureDesc desc;
373 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000374 desc.fWidth = width;
375 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000376 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000377
robertphillips@google.com2c756812012-05-22 20:28:23 +0000378 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000379}
380
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000381////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000382// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
383// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
384// hit)
385bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
386 const SkIRect& clipSpaceIBounds,
387 GrTexture** result) {
388 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
389 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000390
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000391 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
392 // Since we are setting up the cache we know the last lookup was a miss. Free up the
393 // currently cached mask so it can be reused.
394 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000395
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000396 GrTextureDesc desc;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000397 desc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000398 desc.fWidth = clipSpaceIBounds.width();
399 desc.fHeight = clipSpaceIBounds.height();
400 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000401
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000402 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000403 }
404
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000405 *result = fAACache.getLastMask();
406 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000407}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000408
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000409////////////////////////////////////////////////////////////////////////////////
410// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000411GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
412 InitialState initialState,
413 const ElementList& elements,
414 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000415 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
416
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000417 GrTexture* result;
418 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000419 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000420 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000421 }
422
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000423 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000424 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000425 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000426 }
427
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000428 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
429 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000430
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000431 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000432
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000433 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000434 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000435 SkIntToScalar(-clipSpaceIBounds.fLeft),
436 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000437 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000438 // The texture may be larger than necessary, this rect represents the part of the texture
439 // we populate with a rasterization of the clip.
440 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
441
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000442 // We're drawing a coverage mask and want coverage to be run through the blend function.
443 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
444
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000445 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000446 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000448 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
449 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000450 fGpu->clear(&maskSpaceIBounds,
451 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
452 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000453
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000454 // When we use the stencil in the below loop it is important to have this clip installed.
455 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
456 // pass must not set values outside of this bounds or stencil values outside the rect won't be
457 // cleared.
458 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
459 drawState->enableState(GrDrawState::kClip_StateBit);
460
robertphillips@google.comf105b102012-05-14 12:18:26 +0000461 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000462 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000464 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000465 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000466 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000467
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000468 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
469 GrPathRenderer* pr = NULL;
470 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
471 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000472 // 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 +0000473 // mask buffer can be substantially larger than the actually clip stack element. We
474 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000475 // the accumulator.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000476 GrIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000477
478 if (useTemp) {
479 if (invert) {
480 maskSpaceElementIBounds = maskSpaceIBounds;
481 } else {
482 GrRect elementBounds = element->getBounds();
483 elementBounds.offset(clipToMaskOffset);
484 elementBounds.roundOut(&maskSpaceElementIBounds);
485 }
486
487 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
488 if (NULL == temp.texture()) {
489 fAACache.reset();
490 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000491 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000492 dst = temp.texture();
493 // clear the temp target and set blend to replace
494 fGpu->clear(&maskSpaceElementIBounds,
495 invert ? 0xffffffff : 0x00000000,
496 dst->asRenderTarget());
497 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000498
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000499 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000500 // draw directly into the result with the stencil set to make the pixels affected
501 // by the clip shape be non-zero.
502 dst = result;
503 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
504 kReplace_StencilOp,
505 kReplace_StencilOp,
506 kAlways_StencilFunc,
507 0xffff,
508 0xffff,
509 0xffff);
510 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000511 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000512 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000513
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000514 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000515
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000516 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000517 fAACache.reset();
518 return NULL;
519 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000520
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000521 if (useTemp) {
522 // Now draw into the accumulator using the real operation and the temp buffer as a
523 // texture
524 this->mergeMask(result,
525 temp.texture(),
526 op,
527 maskSpaceIBounds,
528 maskSpaceElementIBounds);
529 } else {
530 // Draw to the exterior pixels (those with a zero stencil value).
531 drawState->setAlpha(invert ? 0xff : 0x00);
532 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
533 kZero_StencilOp,
534 kZero_StencilOp,
535 kEqual_StencilFunc,
536 0xffff,
537 0x0000,
538 0xffff);
539 drawState->setStencil(kDrawOutsideElement);
540 fGpu->drawSimpleRect(clipSpaceIBounds);
541 drawState->disableStencil();
542 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000543 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000544 // all the remaining ops can just be directly draw into the accumulation buffer
545 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000546 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000547 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000548 }
549 }
550
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000551 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000552 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000553}
554
555////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000556// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000557// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000558bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
559 const ElementList& elements,
560 const SkIRect& clipSpaceIBounds,
561 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000562
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000563 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000564
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000565 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000566 GrAssert(drawState->isClipState());
567
568 GrRenderTarget* rt = drawState->getRenderTarget();
569 GrAssert(NULL != rt);
570
571 // TODO: dynamically attach a SB when needed.
572 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
573 if (NULL == stencilBuffer) {
574 return false;
575 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000576 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000577
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000578 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000579
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000580 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000581
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000582 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
583 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000584 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000585 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000586
bsalomon@google.com9f131742012-12-13 20:43:56 +0000587 // We set the current clip to the bounds so that our recursive draws are scissored to them.
588 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
589 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
590 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
591 drawState->enableState(GrDrawState::kClip_StateBit);
592
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000593 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
594 SkVector translate = {
595 SkIntToScalar(clipSpaceToStencilOffset.fX),
596 SkIntToScalar(clipSpaceToStencilOffset.fY)
597 };
598 drawState->viewMatrix()->setTranslate(translate);
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000599
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000600#if !VISUALIZE_COMPLEX_CLIP
601 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
602#endif
603
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000604 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000605 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000606 clipBit = (1 << (clipBit-1));
607
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000608 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609
610 // walk through each clip element and perform its set op
611 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000612 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
613 const Element* element = iter.get();
sugoi@google.com12b4e272012-12-06 20:13:11 +0000614 SkPath::FillType fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000615 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000616 // enabled at bottom of loop
617 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000618 // if the target is MSAA then we want MSAA enabled when the clip is soft
619 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000620 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000621 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000622
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000623 // This will be used to determine whether the clip shape can be rendered into the
624 // stencil with arbitrary stencil settings.
625 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000626
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000627 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000628
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000629 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000630
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000632 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000633 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000634 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000635 fill = SkPath::kEvenOdd_FillType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000636 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000637 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000638 GrAssert(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000639 clipPath.init(element->getPath());
640 fill = clipPath->getFillType();
641 fillInverted = clipPath->isInverseFillType();
642 if (fillInverted) {
643 clipPath.writable()->toggleInverseFillType();
644 fill = clipPath->getFillType();
645 }
646 pr = this->getContext()->getPathRenderer(*clipPath,
647 stroke,
648 fGpu,
649 false,
650 GrPathRendererChain::kStencilOnly_DrawType,
651 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000652 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000653 return false;
654 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000655 }
656
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000657 int passes;
658 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
659
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000660 bool canRenderDirectToStencil =
661 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000662 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000663 // fill rule, and set operation can
664 // we render the element directly to
665 // stencil bit used for clipping.
666 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
667 canRenderDirectToStencil,
668 clipBit,
669 fillInverted,
670 &passes,
671 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000672
673 // draw the element to the client stencil bits if necessary
674 if (!canDrawDirectToClip) {
675 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000676 kIncClamp_StencilOp,
677 kIncClamp_StencilOp,
678 kAlways_StencilFunc,
679 0xffff,
680 0x0000,
681 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000683 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000684 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000685 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000686 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000687 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688 if (canRenderDirectToStencil) {
689 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000690 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691 } else {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000692 pr->stencilPath(*clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693 }
694 }
695 }
696
697 // now we modify the clip bit by rendering either the clip
698 // element directly or a bounding rect of the entire clip.
699 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
700 for (int p = 0; p < passes; ++p) {
701 *drawState->stencil() = stencilSettings[p];
702 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000703 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000705 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000707 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000709 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710 }
711 } else {
712 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000713 // The view matrix is setup to do clip space -> stencil space translation, so
714 // draw rect in clip space.
715 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716 }
717 }
718 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000720 // set this last because recursive draws may overwrite it back to kNone.
721 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
722 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723 return true;
724}
725
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000726
bsalomon@google.com411dad02012-06-05 20:24:20 +0000727// mapping of clip-respecting stencil funcs to normal stencil funcs
728// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000729static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000730 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
731 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
732 // In the Clip Funcs
733 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
734 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
735 kLess_StencilFunc, // kLessIfInClip_StencilFunc
736 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
737 // Special in the clip func that forces user's ref to be 0.
738 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
739 // make ref 0 and do normal nequal.
740 },
741 {// Stencil-Clipping is ENABLED
742 // In the Clip Funcs
743 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
744 // eq stencil clip bit, mask
745 // out user bits.
746
747 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
748 // add stencil bit to mask and ref
749
750 kLess_StencilFunc, // kLessIfInClip_StencilFunc
751 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
752 // for both of these we can add
753 // the clip bit to the mask and
754 // ref and compare as normal
755 // Special in the clip func that forces user's ref to be 0.
756 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
757 // make ref have only the clip bit set
758 // and make comparison be less
759 // 10..0 < 1..user_bits..
760 }
761};
762
bsalomon@google.coma3201942012-06-21 19:58:20 +0000763namespace {
764// Sets the settings to clip against the stencil buffer clip while ignoring the
765// client bits.
766const GrStencilSettings& basic_apply_stencil_clip_settings() {
767 // stencil settings to use when clip is in stencil
768 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
769 kKeep_StencilOp,
770 kKeep_StencilOp,
771 kAlwaysIfInClip_StencilFunc,
772 0x0000,
773 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000774 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000775 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
776}
777}
778
779void GrClipMaskManager::setGpuStencil() {
780 // We make two copies of the StencilSettings here (except in the early
781 // exit scenario. One copy from draw state to the stack var. Then another
782 // from the stack var to the gpu. We could make this class hold a ptr to
783 // GrGpu's fStencilSettings and eliminate the stack copy here.
784
785 const GrDrawState& drawState = fGpu->getDrawState();
786
787 // use stencil for clipping if clipping is enabled and the clip
788 // has been written into the stencil.
789 GrClipMaskManager::StencilClipMode clipMode;
790 if (this->isClipInStencil() && drawState.isClipState()) {
791 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
792 // We can't be modifying the clip and respecting it at the same time.
793 GrAssert(!drawState.isStateFlagEnabled(
794 GrGpu::kModifyStencilClip_StateBit));
795 } else if (drawState.isStateFlagEnabled(
796 GrGpu::kModifyStencilClip_StateBit)) {
797 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
798 } else {
799 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
800 }
801
802 GrStencilSettings settings;
803 // The GrGpu client may not be using the stencil buffer but we may need to
804 // enable it in order to respect a stencil clip.
805 if (drawState.getStencil().isDisabled()) {
806 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
807 settings = basic_apply_stencil_clip_settings();
808 } else {
809 fGpu->disableStencil();
810 return;
811 }
812 } else {
813 settings = drawState.getStencil();
814 }
815
816 // TODO: dynamically attach a stencil buffer
817 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000818 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000819 drawState.getRenderTarget()->getStencilBuffer();
820 if (NULL != stencilBuffer) {
821 stencilBits = stencilBuffer->bits();
822 }
823
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000824 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() || !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000825 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000826 this->adjustStencilParams(&settings, clipMode, stencilBits);
827 fGpu->setStencilSettings(settings);
828}
829
830void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
831 StencilClipMode mode,
832 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000833 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000834
835 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000836 // We assume that this clip manager itself is drawing to the GrGpu and
837 // has already setup the correct values.
838 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000839 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000840
bsalomon@google.com411dad02012-06-05 20:24:20 +0000841 unsigned int clipBit = (1 << (stencilBitCnt - 1));
842 unsigned int userBits = clipBit - 1;
843
bsalomon@google.coma3201942012-06-21 19:58:20 +0000844 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000845 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000846
bsalomon@google.coma3201942012-06-21 19:58:20 +0000847 bool finished = false;
848 while (!finished) {
849 GrStencilFunc func = settings->func(face);
850 uint16_t writeMask = settings->writeMask(face);
851 uint16_t funcMask = settings->funcMask(face);
852 uint16_t funcRef = settings->funcRef(face);
853
854 GrAssert((unsigned) func < kStencilFuncCount);
855
856 writeMask &= userBits;
857
858 if (func >= kBasicStencilFuncCount) {
859 int respectClip = kRespectClip_StencilClipMode == mode;
860 if (respectClip) {
861 // The GrGpu class should have checked this
862 GrAssert(this->isClipInStencil());
863 switch (func) {
864 case kAlwaysIfInClip_StencilFunc:
865 funcMask = clipBit;
866 funcRef = clipBit;
867 break;
868 case kEqualIfInClip_StencilFunc:
869 case kLessIfInClip_StencilFunc:
870 case kLEqualIfInClip_StencilFunc:
871 funcMask = (funcMask & userBits) | clipBit;
872 funcRef = (funcRef & userBits) | clipBit;
873 break;
874 case kNonZeroIfInClip_StencilFunc:
875 funcMask = (funcMask & userBits) | clipBit;
876 funcRef = clipBit;
877 break;
878 default:
879 GrCrash("Unknown stencil func");
880 }
881 } else {
882 funcMask &= userBits;
883 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000884 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000885 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000886 gSpecialToBasicStencilFunc[respectClip];
887 func = table[func - kBasicStencilFuncCount];
888 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000889 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000890 funcMask &= userBits;
891 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000892 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000893
894 settings->setFunc(face, func);
895 settings->setWriteMask(face, writeMask);
896 settings->setFuncMask(face, funcMask);
897 settings->setFuncRef(face, funcRef);
898
899 if (GrStencilSettings::kFront_Face == face) {
900 face = GrStencilSettings::kBack_Face;
901 finished = !twoSided;
902 } else {
903 finished = true;
904 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000905 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000906 if (!twoSided) {
907 settings->copyFrontSettingsToBack();
908 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000909}
910
911////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000912GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
913 GrReducedClip::InitialState initialState,
914 const GrReducedClip::ElementList& elements,
915 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000916 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000917
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000918 GrTexture* result;
919 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
920 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000921 }
922
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000923 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000924 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000925 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000926 }
927
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000928 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
929 // the top left corner of the resulting rect to the top left of the texture.
930 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
931
robertphillips@google.com2c756812012-05-22 20:28:23 +0000932 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000933
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000934 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000935 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
936 SkIntToScalar(-clipSpaceIBounds.fTop));
937 helper.init(maskSpaceIBounds, &matrix);
938
939 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000940
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000941 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000942
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000943 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000944
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000945 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000946 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000947
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000948 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
949 // Intersect and reverse difference require modifying pixels outside of the geometry
950 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
951 // but leave the pixels inside the geometry alone. For reverse difference we invert all
952 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000953 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000954 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000955 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000956 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000957 }
958
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000959 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000960 // convert the rect to a path so we can invert the fill
961 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000962 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000963 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000964
sugoi@google.com12b4e272012-12-06 20:13:11 +0000965 helper.draw(temp, stroke, SkRegion::kReplace_Op,
966 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000967 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000968 } else {
969 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000970 SkPath clipPath = element->getPath();
971 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000972 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000973 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000974 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000975 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000976 }
977
978 continue;
979 }
980
981 // The other ops (union, xor, diff) only affect pixels inside
982 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000983 if (Element::kRect_Type == element->getType()) {
984 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
985 } else {
986 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000987 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000988 }
989 }
990
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000991 helper.toTexture(result, kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000992
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000993 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000994 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000995}
996
robertphillips@google.comf294b772012-04-27 14:29:26 +0000997////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +0000998void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000999 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001000}