blob: e2597dfad8b04cbcceeb6ec120565092df23ef8c [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"
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000012#include "GrAARectRenderer.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
14#include "GrGpu.h"
15#include "GrPaint.h"
16#include "GrPathRenderer.h"
17#include "GrRenderTarget.h"
18#include "GrStencilBuffer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000020#include "effects/GrTextureDomainEffect.h"
21#include "SkRasterClip.h"
22#include "SkStrokeRec.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000023#include "SkTLazy.h"
24
robertphillips@google.comba998f22012-10-12 11:33:56 +000025#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000026
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000029using namespace GrReducedClip;
30
bsalomon@google.com51a62862012-11-26 21:19:43 +000031////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000032namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000034// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035void setup_drawstate_aaclip(GrGpu* gpu,
36 GrTexture* result,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000037 const SkIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000038 GrDrawState* drawState = gpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000039 SkASSERT(drawState);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040
bsalomon@google.comb9086a02012-11-01 18:02:54 +000041 SkMatrix mat;
bsalomon@google.comc7818882013-03-20 19:19:53 +000042 // We want to use device coords to compute the texture coordinates. We set our matrix to be
43 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
44 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000047 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000048 mat.preConcat(drawState->getViewMatrix());
49
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000050 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000051 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000052 drawState->addCoverageEffect(
53 GrTextureDomainEffect::Create(result,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000054 mat,
55 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
bsalomon@google.comc7818882013-03-20 19:19:53 +000056 GrTextureDomainEffect::kDecal_WrapMode,
humper@google.comb86add12013-07-25 18:49:07 +000057 GrTextureParams::kNone_FilterMode,
bsalomon@google.com77af6802013-10-02 13:04:56 +000058 kPosition_GrCoordSet))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000059}
60
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000061bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000062 GrGpu* gpu,
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000063 const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000064 const SkStrokeRec& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000065 bool doAA) {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000066 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
67 SkTCopyOnFirstWrite<SkPath> path(origPath);
68 if (path->isInverseFillType()) {
69 path.writable()->toggleInverseFillType();
70 }
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000071 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000072 GrPathRendererChain::DrawType type = doAA ?
73 GrPathRendererChain::kColorAntiAlias_DrawType :
74 GrPathRendererChain::kColor_DrawType;
75
76 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000077}
78
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000079}
80
robertphillips@google.comfa662942012-05-17 12:20:22 +000081/*
82 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
83 * will be used on any element. If so, it returns true to indicate that the
84 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
85 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000086bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000087
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000088 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000089 // a clip gets complex enough it can just be done in SW regardless
90 // of whether it would invoke the GrSoftwarePathRenderer.
sugoi@google.com5f74cf82012-12-17 21:16:45 +000091 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000092
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000093 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
94 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000095 // rects can always be drawn directly w/o using the software path
96 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +000097 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +000098 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +000099 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000100 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000101 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000102 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000103 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000105 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000106}
107
robertphillips@google.comf294b772012-04-27 14:29:26 +0000108////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000109// sort out what kind of clip mask needs to be created: alpha, stencil,
110// scissor, or entirely software
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000111bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
112 GrDrawState::AutoRestoreEffects* are) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000113 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000114
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000115 ElementList elements(16);
116 InitialState initialState;
117 SkIRect clipSpaceIBounds;
118 bool requiresAA;
119 bool isRect = false;
120
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000121 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000122
123 const GrRenderTarget* rt = drawState->getRenderTarget();
124 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000125 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000126
127 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
128
129 if (!ignoreClip) {
130 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
131 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
132 ReduceClipStack(*clipDataIn->fClipStack,
133 clipSpaceRTIBounds,
134 &elements,
135 &initialState,
136 &clipSpaceIBounds,
137 &requiresAA);
138 if (elements.isEmpty()) {
139 if (kAllIn_InitialState == initialState) {
140 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
141 isRect = true;
142 } else {
143 return false;
144 }
145 }
146 }
147
148 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000149 fGpu->disableScissor();
150 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000151 return true;
152 }
153
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000154#if GR_AA_CLIP
155 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000156
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000157 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000158 if (0 == rt->numSamples() && requiresAA) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000159 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000160 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000161
162 if (this->useSWOnlyPath(elements)) {
163 // The clip geometry is complex enough that it will be more efficient to create it
164 // entirely in software
165 result = this->createSoftwareClipMask(genID,
166 initialState,
167 elements,
168 clipSpaceIBounds);
169 } else {
170 result = this->createAlphaClipMask(genID,
171 initialState,
172 elements,
173 clipSpaceIBounds);
174 }
175
176 if (NULL != result) {
177 // The mask's top left coord should be pinned to the rounded-out top left corner of
178 // clipSpace bounds. We determine the mask's position WRT to the render target here.
179 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
180 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000181 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000182 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000183 fGpu->disableScissor();
184 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000185 return true;
186 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000187 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000188 }
189#endif // GR_AA_CLIP
190
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000191 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
192 // be created. In either case, free up the texture in the anti-aliased mask cache.
193 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
194 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
195 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000196 fAACache.reset();
197
bsalomon@google.coma3201942012-06-21 19:58:20 +0000198 // If the clip is a rectangle then just set the scissor. Otherwise, create
199 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000200 if (isRect) {
201 SkIRect clipRect = clipSpaceIBounds;
202 clipRect.offset(-clipDataIn->fOrigin);
203 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000204 this->setGpuStencil();
205 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000206 }
207
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000208 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000209 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
210 this->createStencilClipMask(initialState,
211 elements,
212 clipSpaceIBounds,
213 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000214
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000215 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
216 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
217 // use both stencil and scissor test to the bounds for the final draw.
218 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
219 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
220 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000221 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000222 return true;
223}
224
225#define VISUALIZE_COMPLEX_CLIP 0
226
227#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000228 #include "SkRandom.h"
229 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000230 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
231#else
232 #define SET_RANDOM_COLOR
233#endif
234
235namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000236
237////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000238// set up the OpenGL blend function to perform the specified
239// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000240void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000241
242 switch (op) {
243 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000244 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000245 break;
246 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000247 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000248 break;
249 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000250 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000251 break;
252 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000253 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000254 break;
255 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000256 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000257 break;
258 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000259 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000260 break;
261 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000262 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000263 break;
264 }
265}
266
robertphillips@google.com72176b22012-05-23 13:19:12 +0000267}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000268
269////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000270bool GrClipMaskManager::drawElement(GrTexture* target,
271 const SkClipStack::Element* element,
272 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000273 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000274
275 drawState->setRenderTarget(target->asRenderTarget());
276
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000277 switch (element->getType()) {
278 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000279 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
280 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000281 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000282 getContext()->getAARectRenderer()->fillAARect(fGpu,
283 fGpu,
284 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000285 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000286 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000287 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000288 } else {
289 fGpu->drawSimpleRect(element->getRect(), NULL);
290 }
291 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000292 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000293 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
294 if (path->isInverseFillType()) {
295 path.writable()->toggleInverseFillType();
296 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000297 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000298 if (NULL == pr) {
299 GrPathRendererChain::DrawType type;
300 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
301 GrPathRendererChain::kColor_DrawType;
302 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
303 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000304 if (NULL == pr) {
305 return false;
306 }
307 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
308 break;
309 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000310 default:
311 // something is wrong if we're trying to draw an empty element.
312 GrCrash("Unexpected element type");
313 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000314 }
315 return true;
316}
317
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000318bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
319 const SkClipStack::Element* element,
320 GrPathRenderer** pr) {
321 GrDrawState* drawState = fGpu->drawState();
322 drawState->setRenderTarget(target->asRenderTarget());
323
324 switch (element->getType()) {
325 case Element::kRect_Type:
326 return true;
327 case Element::kPath_Type: {
328 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
329 if (path->isInverseFillType()) {
330 path.writable()->toggleInverseFillType();
331 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000332 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000333 GrPathRendererChain::DrawType type = element->isAA() ?
334 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
335 GrPathRendererChain::kStencilAndColor_DrawType;
336 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
337 return NULL != *pr;
338 }
339 default:
340 // something is wrong if we're trying to draw an empty element.
341 GrCrash("Unexpected element type");
342 return false;
343 }
344}
345
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000346void GrClipMaskManager::mergeMask(GrTexture* dstMask,
347 GrTexture* srcMask,
348 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000349 const SkIRect& dstBound,
350 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000351 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000352 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000353 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000354 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000356 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000357
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000358 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000359
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000360 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000361 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000362
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000363 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000364 GrTextureDomainEffect::Create(srcMask,
365 sampleM,
366 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
bsalomon@google.comc7818882013-03-20 19:19:53 +0000367 GrTextureDomainEffect::kDecal_WrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000368 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000369 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370}
371
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000372// get a texture to act as a temporary buffer for AA clip boolean operations
373// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000374void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000375 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000376 // we've already allocated the temp texture
377 return;
378 }
379
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000380 GrTextureDesc desc;
381 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000382 desc.fWidth = width;
383 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000384 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000385
robertphillips@google.com2c756812012-05-22 20:28:23 +0000386 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000387}
388
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000389////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000390// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
391// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
392// hit)
393bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
394 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000395 GrTexture** result,
396 bool willUpload) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000397 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
398 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000399
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000400 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
401 // Since we are setting up the cache we know the last lookup was a miss. Free up the
402 // currently cached mask so it can be reused.
403 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000404
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000405 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000406 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000407 desc.fWidth = clipSpaceIBounds.width();
408 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000409 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000410 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000411 // We would always like A8 but it isn't supported on all platforms
412 desc.fConfig = kAlpha_8_GrPixelConfig;
413 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000414
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000415 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000416 }
417
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000418 *result = fAACache.getLastMask();
419 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000420}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000421
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000422////////////////////////////////////////////////////////////////////////////////
423// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000424GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
425 InitialState initialState,
426 const ElementList& elements,
427 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000428 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000429
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000430 GrTexture* result;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000431 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000432 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000433 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000434 }
435
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000436 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000437 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000438 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000439 }
440
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000441 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000442 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443 SkIntToScalar(-clipSpaceIBounds.fLeft),
444 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000445 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000446 // The texture may be larger than necessary, this rect represents the part of the texture
447 // we populate with a rasterization of the clip.
448 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
449
bsalomon@google.com137f1342013-05-29 21:27:53 +0000450 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
451 SkMatrix translate;
452 translate.setTranslate(clipToMaskOffset);
453 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
454
455 GrDrawState* drawState = fGpu->drawState();
456
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000457 // We're drawing a coverage mask and want coverage to be run through the blend function.
458 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
459
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000460 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
461 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000462 fGpu->clear(&maskSpaceIBounds,
463 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
464 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000465
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000466 // When we use the stencil in the below loop it is important to have this clip installed.
467 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
468 // pass must not set values outside of this bounds or stencil values outside the rect won't be
469 // cleared.
470 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
471 drawState->enableState(GrDrawState::kClip_StateBit);
472
robertphillips@google.comf105b102012-05-14 12:18:26 +0000473 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000474 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000475 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000476 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000477 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000478 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000479
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000480 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
481 GrPathRenderer* pr = NULL;
482 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
483 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000484 // 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 +0000485 // mask buffer can be substantially larger than the actually clip stack element. We
486 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000487 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000488 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000489
490 if (useTemp) {
491 if (invert) {
492 maskSpaceElementIBounds = maskSpaceIBounds;
493 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000494 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000495 elementBounds.offset(clipToMaskOffset);
496 elementBounds.roundOut(&maskSpaceElementIBounds);
497 }
498
499 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
500 if (NULL == temp.texture()) {
501 fAACache.reset();
502 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000503 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000504 dst = temp.texture();
505 // clear the temp target and set blend to replace
506 fGpu->clear(&maskSpaceElementIBounds,
507 invert ? 0xffffffff : 0x00000000,
508 dst->asRenderTarget());
509 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000510
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000511 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000512 // draw directly into the result with the stencil set to make the pixels affected
513 // by the clip shape be non-zero.
514 dst = result;
515 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
516 kReplace_StencilOp,
517 kReplace_StencilOp,
518 kAlways_StencilFunc,
519 0xffff,
520 0xffff,
521 0xffff);
522 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000523 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000524 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000525
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000526 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000527
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000528 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000529 fAACache.reset();
530 return NULL;
531 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000532
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000533 if (useTemp) {
534 // Now draw into the accumulator using the real operation and the temp buffer as a
535 // texture
536 this->mergeMask(result,
537 temp.texture(),
538 op,
539 maskSpaceIBounds,
540 maskSpaceElementIBounds);
541 } else {
542 // Draw to the exterior pixels (those with a zero stencil value).
543 drawState->setAlpha(invert ? 0xff : 0x00);
544 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
545 kZero_StencilOp,
546 kZero_StencilOp,
547 kEqual_StencilFunc,
548 0xffff,
549 0x0000,
550 0xffff);
551 drawState->setStencil(kDrawOutsideElement);
552 fGpu->drawSimpleRect(clipSpaceIBounds);
553 drawState->disableStencil();
554 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000556 // all the remaining ops can just be directly draw into the accumulation buffer
557 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000558 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000559 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560 }
561 }
562
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000563 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000564 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000565}
566
567////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000568// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000569// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000570bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
571 const ElementList& elements,
572 const SkIRect& clipSpaceIBounds,
573 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000574
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000575 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000576
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000577 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000578 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000579
580 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000581 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000582
583 // TODO: dynamically attach a SB when needed.
584 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
585 if (NULL == stencilBuffer) {
586 return false;
587 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000588 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000589
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000590 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000591
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000592 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000593
bsalomon@google.com137f1342013-05-29 21:27:53 +0000594 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
595 SkVector translate = {
596 SkIntToScalar(clipSpaceToStencilOffset.fX),
597 SkIntToScalar(clipSpaceToStencilOffset.fY)
598 };
599 SkMatrix matrix;
600 matrix.setTranslate(translate);
601 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000602 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000603
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000604 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000605
bsalomon@google.com9f131742012-12-13 20:43:56 +0000606 // We set the current clip to the bounds so that our recursive draws are scissored to them.
607 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
608 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
609 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
610 drawState->enableState(GrDrawState::kClip_StateBit);
611
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000612#if !VISUALIZE_COMPLEX_CLIP
613 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
614#endif
615
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000616 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000617 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000618 clipBit = (1 << (clipBit-1));
619
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000620 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000621
622 // walk through each clip element and perform its set op
623 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000624 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
625 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000626 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000627 // enabled at bottom of loop
628 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000629 // if the target is MSAA then we want MSAA enabled when the clip is soft
630 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000631 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000632 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000633
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000634 // This will be used to determine whether the clip shape can be rendered into the
635 // stencil with arbitrary stencil settings.
636 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000637
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000638 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000639
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000640 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000641
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000642 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000643 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000644 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000645 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000646 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000647 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000648 SkASSERT(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000649 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000650 fillInverted = clipPath->isInverseFillType();
651 if (fillInverted) {
652 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000653 }
654 pr = this->getContext()->getPathRenderer(*clipPath,
655 stroke,
656 fGpu,
657 false,
658 GrPathRendererChain::kStencilOnly_DrawType,
659 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000660 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000661 return false;
662 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663 }
664
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665 int passes;
666 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
667
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000668 bool canRenderDirectToStencil =
669 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000670 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000671 // fill rule, and set operation can
672 // we render the element directly to
673 // stencil bit used for clipping.
674 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
675 canRenderDirectToStencil,
676 clipBit,
677 fillInverted,
678 &passes,
679 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000680
681 // draw the element to the client stencil bits if necessary
682 if (!canDrawDirectToClip) {
683 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000684 kIncClamp_StencilOp,
685 kIncClamp_StencilOp,
686 kAlways_StencilFunc,
687 0xffff,
688 0x0000,
689 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000691 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000693 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000695 SkASSERT(Element::kPath_Type == element->getType());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000696 if (!clipPath->isEmpty()) {
697 if (canRenderDirectToStencil) {
698 *drawState->stencil() = gDrawToStencil;
699 pr->drawPath(*clipPath, stroke, fGpu, false);
700 } else {
701 pr->stencilPath(*clipPath, stroke, fGpu);
702 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000703 }
704 }
705 }
706
707 // now we modify the clip bit by rendering either the clip
708 // element directly or a bounding rect of the entire clip.
709 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
710 for (int p = 0; p < passes; ++p) {
711 *drawState->stencil() = stencilSettings[p];
712 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000713 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000715 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000717 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000719 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 }
721 } else {
722 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000723 // The view matrix is setup to do clip space -> stencil space translation, so
724 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000725 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 }
727 }
728 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000730 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000731 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000732 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733 return true;
734}
735
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000736
bsalomon@google.com411dad02012-06-05 20:24:20 +0000737// mapping of clip-respecting stencil funcs to normal stencil funcs
738// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000739static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000740 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
741 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
742 // In the Clip Funcs
743 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
744 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
745 kLess_StencilFunc, // kLessIfInClip_StencilFunc
746 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
747 // Special in the clip func that forces user's ref to be 0.
748 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
749 // make ref 0 and do normal nequal.
750 },
751 {// Stencil-Clipping is ENABLED
752 // In the Clip Funcs
753 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
754 // eq stencil clip bit, mask
755 // out user bits.
756
757 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
758 // add stencil bit to mask and ref
759
760 kLess_StencilFunc, // kLessIfInClip_StencilFunc
761 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
762 // for both of these we can add
763 // the clip bit to the mask and
764 // ref and compare as normal
765 // Special in the clip func that forces user's ref to be 0.
766 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
767 // make ref have only the clip bit set
768 // and make comparison be less
769 // 10..0 < 1..user_bits..
770 }
771};
772
bsalomon@google.coma3201942012-06-21 19:58:20 +0000773namespace {
774// Sets the settings to clip against the stencil buffer clip while ignoring the
775// client bits.
776const GrStencilSettings& basic_apply_stencil_clip_settings() {
777 // stencil settings to use when clip is in stencil
778 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
779 kKeep_StencilOp,
780 kKeep_StencilOp,
781 kAlwaysIfInClip_StencilFunc,
782 0x0000,
783 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000784 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000785 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
786}
787}
788
789void GrClipMaskManager::setGpuStencil() {
790 // We make two copies of the StencilSettings here (except in the early
791 // exit scenario. One copy from draw state to the stack var. Then another
792 // from the stack var to the gpu. We could make this class hold a ptr to
793 // GrGpu's fStencilSettings and eliminate the stack copy here.
794
795 const GrDrawState& drawState = fGpu->getDrawState();
796
797 // use stencil for clipping if clipping is enabled and the clip
798 // has been written into the stencil.
799 GrClipMaskManager::StencilClipMode clipMode;
800 if (this->isClipInStencil() && drawState.isClipState()) {
801 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
802 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000803 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000804 GrGpu::kModifyStencilClip_StateBit));
805 } else if (drawState.isStateFlagEnabled(
806 GrGpu::kModifyStencilClip_StateBit)) {
807 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
808 } else {
809 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
810 }
811
812 GrStencilSettings settings;
813 // The GrGpu client may not be using the stencil buffer but we may need to
814 // enable it in order to respect a stencil clip.
815 if (drawState.getStencil().isDisabled()) {
816 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
817 settings = basic_apply_stencil_clip_settings();
818 } else {
819 fGpu->disableStencil();
820 return;
821 }
822 } else {
823 settings = drawState.getStencil();
824 }
825
826 // TODO: dynamically attach a stencil buffer
827 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000828 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000829 drawState.getRenderTarget()->getStencilBuffer();
830 if (NULL != stencilBuffer) {
831 stencilBits = stencilBuffer->bits();
832 }
833
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000834 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
835 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000836 this->adjustStencilParams(&settings, clipMode, stencilBits);
837 fGpu->setStencilSettings(settings);
838}
839
840void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
841 StencilClipMode mode,
842 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000843 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000844
845 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000846 // We assume that this clip manager itself is drawing to the GrGpu and
847 // has already setup the correct values.
848 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000849 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000850
bsalomon@google.com411dad02012-06-05 20:24:20 +0000851 unsigned int clipBit = (1 << (stencilBitCnt - 1));
852 unsigned int userBits = clipBit - 1;
853
bsalomon@google.coma3201942012-06-21 19:58:20 +0000854 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000855 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000856
bsalomon@google.coma3201942012-06-21 19:58:20 +0000857 bool finished = false;
858 while (!finished) {
859 GrStencilFunc func = settings->func(face);
860 uint16_t writeMask = settings->writeMask(face);
861 uint16_t funcMask = settings->funcMask(face);
862 uint16_t funcRef = settings->funcRef(face);
863
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000864 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000865
866 writeMask &= userBits;
867
868 if (func >= kBasicStencilFuncCount) {
869 int respectClip = kRespectClip_StencilClipMode == mode;
870 if (respectClip) {
871 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000872 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000873 switch (func) {
874 case kAlwaysIfInClip_StencilFunc:
875 funcMask = clipBit;
876 funcRef = clipBit;
877 break;
878 case kEqualIfInClip_StencilFunc:
879 case kLessIfInClip_StencilFunc:
880 case kLEqualIfInClip_StencilFunc:
881 funcMask = (funcMask & userBits) | clipBit;
882 funcRef = (funcRef & userBits) | clipBit;
883 break;
884 case kNonZeroIfInClip_StencilFunc:
885 funcMask = (funcMask & userBits) | clipBit;
886 funcRef = clipBit;
887 break;
888 default:
889 GrCrash("Unknown stencil func");
890 }
891 } else {
892 funcMask &= userBits;
893 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000894 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000895 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896 gSpecialToBasicStencilFunc[respectClip];
897 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000898 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000899 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000900 funcMask &= userBits;
901 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000902 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000903
904 settings->setFunc(face, func);
905 settings->setWriteMask(face, writeMask);
906 settings->setFuncMask(face, funcMask);
907 settings->setFuncRef(face, funcRef);
908
909 if (GrStencilSettings::kFront_Face == face) {
910 face = GrStencilSettings::kBack_Face;
911 finished = !twoSided;
912 } else {
913 finished = true;
914 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000915 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000916 if (!twoSided) {
917 settings->copyFrontSettingsToBack();
918 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000919}
920
921////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000922GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
923 GrReducedClip::InitialState initialState,
924 const GrReducedClip::ElementList& elements,
925 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000926 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000927
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000928 GrTexture* result;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000929 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000930 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000931 }
932
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000933 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000934 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000935 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000936 }
937
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000938 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
939 // the top left corner of the resulting rect to the top left of the texture.
940 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
941
robertphillips@google.com2c756812012-05-22 20:28:23 +0000942 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000943
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000944 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000945 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
946 SkIntToScalar(-clipSpaceIBounds.fTop));
947 helper.init(maskSpaceIBounds, &matrix);
948
949 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000950
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000951 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000952
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000953 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000954
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000955 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000956 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000957
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000958 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
959 // Intersect and reverse difference require modifying pixels outside of the geometry
960 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
961 // but leave the pixels inside the geometry alone. For reverse difference we invert all
962 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000963 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000964 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000965 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000966 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000967 }
968
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000969 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000970 // convert the rect to a path so we can invert the fill
971 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000972 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000973 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000974
sugoi@google.com12b4e272012-12-06 20:13:11 +0000975 helper.draw(temp, stroke, SkRegion::kReplace_Op,
976 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000977 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000978 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000979 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000980 SkPath clipPath = element->getPath();
981 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000982 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000983 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000984 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000985 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000986 }
987
988 continue;
989 }
990
991 // The other ops (union, xor, diff) only affect pixels inside
992 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000993 if (Element::kRect_Type == element->getType()) {
994 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
995 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000996 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000997 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000998 }
999 }
1000
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001001 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001002
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001003 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001004 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001005}
1006
robertphillips@google.comf294b772012-04-27 14:29:26 +00001007////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001008void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001009 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001010}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001011
1012void GrClipMaskManager::setGpu(GrGpu* gpu) {
1013 fGpu = gpu;
1014 fAACache.setContext(gpu->getContext());
1015}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001016
1017void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1018 const GrDrawState& drawState = fGpu->getDrawState();
1019 GrClipMaskManager::StencilClipMode clipMode;
1020 if (this->isClipInStencil() && drawState.isClipState()) {
1021 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1022 // We can't be modifying the clip and respecting it at the same time.
1023 SkASSERT(!drawState.isStateFlagEnabled(
1024 GrGpu::kModifyStencilClip_StateBit));
1025 } else if (drawState.isStateFlagEnabled(
1026 GrGpu::kModifyStencilClip_StateBit)) {
1027 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1028 } else {
1029 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1030 }
1031
1032 // TODO: dynamically attach a stencil buffer
1033 int stencilBits = 0;
1034 GrStencilBuffer* stencilBuffer =
1035 drawState.getRenderTarget()->getStencilBuffer();
1036 if (NULL != stencilBuffer) {
1037 stencilBits = stencilBuffer->bits();
1038 this->adjustStencilParams(settings, clipMode, stencilBits);
1039 }
1040}