blob: 84658807e331a9202da9c16831f55a74dc3cd650 [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.comc7818882013-03-20 19:19:53 +000058 GrEffect::kPosition_CoordsType))->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();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000369 fGpu->drawSimpleRect(SkRect::MakeFromIRect(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,
395 GrTexture** result) {
396 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
397 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000398
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000399 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
400 // Since we are setting up the cache we know the last lookup was a miss. Free up the
401 // currently cached mask so it can be reused.
402 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000404 GrTextureDesc desc;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000405 desc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000406 desc.fWidth = clipSpaceIBounds.width();
407 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000408 desc.fConfig = kRGBA_8888_GrPixelConfig;
409 if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
410 // We would always like A8 but it isn't supported on all platforms
411 desc.fConfig = kAlpha_8_GrPixelConfig;
412 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000414 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000415 }
416
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000417 *result = fAACache.getLastMask();
418 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000419}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000420
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000421////////////////////////////////////////////////////////////////////////////////
422// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000423GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
424 InitialState initialState,
425 const ElementList& elements,
426 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000427 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000428
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000429 GrTexture* result;
430 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000431 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000433 }
434
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000435 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000436 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000437 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438 }
439
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000441 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000442 SkIntToScalar(-clipSpaceIBounds.fLeft),
443 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000444 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000445 // The texture may be larger than necessary, this rect represents the part of the texture
446 // we populate with a rasterization of the clip.
447 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
448
bsalomon@google.com137f1342013-05-29 21:27:53 +0000449 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
450 SkMatrix translate;
451 translate.setTranslate(clipToMaskOffset);
452 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
453
454 GrDrawState* drawState = fGpu->drawState();
455
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000456 // We're drawing a coverage mask and want coverage to be run through the blend function.
457 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
458
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000459 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
460 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000461 fGpu->clear(&maskSpaceIBounds,
462 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
463 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000464
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000465 // When we use the stencil in the below loop it is important to have this clip installed.
466 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
467 // pass must not set values outside of this bounds or stencil values outside the rect won't be
468 // cleared.
469 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
470 drawState->enableState(GrDrawState::kClip_StateBit);
471
robertphillips@google.comf105b102012-05-14 12:18:26 +0000472 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000473 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000474 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000475 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000476 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000477 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000478
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000479 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
480 GrPathRenderer* pr = NULL;
481 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
482 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000483 // 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 +0000484 // mask buffer can be substantially larger than the actually clip stack element. We
485 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000486 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000487 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000488
489 if (useTemp) {
490 if (invert) {
491 maskSpaceElementIBounds = maskSpaceIBounds;
492 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000493 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000494 elementBounds.offset(clipToMaskOffset);
495 elementBounds.roundOut(&maskSpaceElementIBounds);
496 }
497
498 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
499 if (NULL == temp.texture()) {
500 fAACache.reset();
501 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000502 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000503 dst = temp.texture();
504 // clear the temp target and set blend to replace
505 fGpu->clear(&maskSpaceElementIBounds,
506 invert ? 0xffffffff : 0x00000000,
507 dst->asRenderTarget());
508 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000509
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000510 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000511 // draw directly into the result with the stencil set to make the pixels affected
512 // by the clip shape be non-zero.
513 dst = result;
514 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
515 kReplace_StencilOp,
516 kReplace_StencilOp,
517 kAlways_StencilFunc,
518 0xffff,
519 0xffff,
520 0xffff);
521 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000522 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000523 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000524
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000525 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000526
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000527 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000528 fAACache.reset();
529 return NULL;
530 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000531
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000532 if (useTemp) {
533 // Now draw into the accumulator using the real operation and the temp buffer as a
534 // texture
535 this->mergeMask(result,
536 temp.texture(),
537 op,
538 maskSpaceIBounds,
539 maskSpaceElementIBounds);
540 } else {
541 // Draw to the exterior pixels (those with a zero stencil value).
542 drawState->setAlpha(invert ? 0xff : 0x00);
543 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
544 kZero_StencilOp,
545 kZero_StencilOp,
546 kEqual_StencilFunc,
547 0xffff,
548 0x0000,
549 0xffff);
550 drawState->setStencil(kDrawOutsideElement);
551 fGpu->drawSimpleRect(clipSpaceIBounds);
552 drawState->disableStencil();
553 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000554 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000555 // all the remaining ops can just be directly draw into the accumulation buffer
556 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000557 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000558 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559 }
560 }
561
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000562 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000563 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000564}
565
566////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000567// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000568// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000569bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
570 const ElementList& elements,
571 const SkIRect& clipSpaceIBounds,
572 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000573
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000574 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000575
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000576 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000577 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000578
579 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000580 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000581
582 // TODO: dynamically attach a SB when needed.
583 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
584 if (NULL == stencilBuffer) {
585 return false;
586 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000587 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000588
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000589 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000590
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000591 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000592
bsalomon@google.com137f1342013-05-29 21:27:53 +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 SkMatrix matrix;
599 matrix.setTranslate(translate);
600 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000601 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000602
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000603 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000604
bsalomon@google.com9f131742012-12-13 20:43:56 +0000605 // We set the current clip to the bounds so that our recursive draws are scissored to them.
606 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
607 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
608 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
609 drawState->enableState(GrDrawState::kClip_StateBit);
610
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000611#if !VISUALIZE_COMPLEX_CLIP
612 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
613#endif
614
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000615 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000616 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000617 clipBit = (1 << (clipBit-1));
618
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000619 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000620
621 // walk through each clip element and perform its set op
622 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000623 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
624 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000625 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000626 // enabled at bottom of loop
627 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000628 // if the target is MSAA then we want MSAA enabled when the clip is soft
629 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000630 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000631 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000632
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000633 // This will be used to determine whether the clip shape can be rendered into the
634 // stencil with arbitrary stencil settings.
635 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000636
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000637 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000638
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000639 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000642 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000643 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000644 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000645 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000646 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000647 SkASSERT(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000648 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000649 fillInverted = clipPath->isInverseFillType();
650 if (fillInverted) {
651 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000652 }
653 pr = this->getContext()->getPathRenderer(*clipPath,
654 stroke,
655 fGpu,
656 false,
657 GrPathRendererChain::kStencilOnly_DrawType,
658 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000660 return false;
661 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000662 }
663
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664 int passes;
665 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
666
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000667 bool canRenderDirectToStencil =
668 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000669 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000670 // fill rule, and set operation can
671 // we render the element directly to
672 // stencil bit used for clipping.
673 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
674 canRenderDirectToStencil,
675 clipBit,
676 fillInverted,
677 &passes,
678 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000679
680 // draw the element to the client stencil bits if necessary
681 if (!canDrawDirectToClip) {
682 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000683 kIncClamp_StencilOp,
684 kIncClamp_StencilOp,
685 kAlways_StencilFunc,
686 0xffff,
687 0x0000,
688 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000690 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000692 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000694 SkASSERT(Element::kPath_Type == element->getType());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000695 if (!clipPath->isEmpty()) {
696 if (canRenderDirectToStencil) {
697 *drawState->stencil() = gDrawToStencil;
698 pr->drawPath(*clipPath, stroke, fGpu, false);
699 } else {
700 pr->stencilPath(*clipPath, stroke, fGpu);
701 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000702 }
703 }
704 }
705
706 // now we modify the clip bit by rendering either the clip
707 // element directly or a bounding rect of the entire clip.
708 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
709 for (int p = 0; p < passes; ++p) {
710 *drawState->stencil() = stencilSettings[p];
711 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000712 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000714 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000716 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000718 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719 }
720 } else {
721 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000722 // The view matrix is setup to do clip space -> stencil space translation, so
723 // draw rect in clip space.
724 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 }
726 }
727 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000729 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000730 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000731 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732 return true;
733}
734
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000735
bsalomon@google.com411dad02012-06-05 20:24:20 +0000736// mapping of clip-respecting stencil funcs to normal stencil funcs
737// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000738static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000739 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
740 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
741 // In the Clip Funcs
742 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
743 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
744 kLess_StencilFunc, // kLessIfInClip_StencilFunc
745 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
746 // Special in the clip func that forces user's ref to be 0.
747 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
748 // make ref 0 and do normal nequal.
749 },
750 {// Stencil-Clipping is ENABLED
751 // In the Clip Funcs
752 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
753 // eq stencil clip bit, mask
754 // out user bits.
755
756 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
757 // add stencil bit to mask and ref
758
759 kLess_StencilFunc, // kLessIfInClip_StencilFunc
760 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
761 // for both of these we can add
762 // the clip bit to the mask and
763 // ref and compare as normal
764 // Special in the clip func that forces user's ref to be 0.
765 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
766 // make ref have only the clip bit set
767 // and make comparison be less
768 // 10..0 < 1..user_bits..
769 }
770};
771
bsalomon@google.coma3201942012-06-21 19:58:20 +0000772namespace {
773// Sets the settings to clip against the stencil buffer clip while ignoring the
774// client bits.
775const GrStencilSettings& basic_apply_stencil_clip_settings() {
776 // stencil settings to use when clip is in stencil
777 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
778 kKeep_StencilOp,
779 kKeep_StencilOp,
780 kAlwaysIfInClip_StencilFunc,
781 0x0000,
782 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000783 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000784 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
785}
786}
787
788void GrClipMaskManager::setGpuStencil() {
789 // We make two copies of the StencilSettings here (except in the early
790 // exit scenario. One copy from draw state to the stack var. Then another
791 // from the stack var to the gpu. We could make this class hold a ptr to
792 // GrGpu's fStencilSettings and eliminate the stack copy here.
793
794 const GrDrawState& drawState = fGpu->getDrawState();
795
796 // use stencil for clipping if clipping is enabled and the clip
797 // has been written into the stencil.
798 GrClipMaskManager::StencilClipMode clipMode;
799 if (this->isClipInStencil() && drawState.isClipState()) {
800 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
801 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000802 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000803 GrGpu::kModifyStencilClip_StateBit));
804 } else if (drawState.isStateFlagEnabled(
805 GrGpu::kModifyStencilClip_StateBit)) {
806 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
807 } else {
808 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
809 }
810
811 GrStencilSettings settings;
812 // The GrGpu client may not be using the stencil buffer but we may need to
813 // enable it in order to respect a stencil clip.
814 if (drawState.getStencil().isDisabled()) {
815 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
816 settings = basic_apply_stencil_clip_settings();
817 } else {
818 fGpu->disableStencil();
819 return;
820 }
821 } else {
822 settings = drawState.getStencil();
823 }
824
825 // TODO: dynamically attach a stencil buffer
826 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000827 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000828 drawState.getRenderTarget()->getStencilBuffer();
829 if (NULL != stencilBuffer) {
830 stencilBits = stencilBuffer->bits();
831 }
832
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000833 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
834 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000835 this->adjustStencilParams(&settings, clipMode, stencilBits);
836 fGpu->setStencilSettings(settings);
837}
838
839void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
840 StencilClipMode mode,
841 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000842 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000843
844 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000845 // We assume that this clip manager itself is drawing to the GrGpu and
846 // has already setup the correct values.
847 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000848 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000849
bsalomon@google.com411dad02012-06-05 20:24:20 +0000850 unsigned int clipBit = (1 << (stencilBitCnt - 1));
851 unsigned int userBits = clipBit - 1;
852
bsalomon@google.coma3201942012-06-21 19:58:20 +0000853 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000854 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000855
bsalomon@google.coma3201942012-06-21 19:58:20 +0000856 bool finished = false;
857 while (!finished) {
858 GrStencilFunc func = settings->func(face);
859 uint16_t writeMask = settings->writeMask(face);
860 uint16_t funcMask = settings->funcMask(face);
861 uint16_t funcRef = settings->funcRef(face);
862
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000863 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000864
865 writeMask &= userBits;
866
867 if (func >= kBasicStencilFuncCount) {
868 int respectClip = kRespectClip_StencilClipMode == mode;
869 if (respectClip) {
870 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000871 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000872 switch (func) {
873 case kAlwaysIfInClip_StencilFunc:
874 funcMask = clipBit;
875 funcRef = clipBit;
876 break;
877 case kEqualIfInClip_StencilFunc:
878 case kLessIfInClip_StencilFunc:
879 case kLEqualIfInClip_StencilFunc:
880 funcMask = (funcMask & userBits) | clipBit;
881 funcRef = (funcRef & userBits) | clipBit;
882 break;
883 case kNonZeroIfInClip_StencilFunc:
884 funcMask = (funcMask & userBits) | clipBit;
885 funcRef = clipBit;
886 break;
887 default:
888 GrCrash("Unknown stencil func");
889 }
890 } else {
891 funcMask &= userBits;
892 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000893 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000894 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000895 gSpecialToBasicStencilFunc[respectClip];
896 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000897 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000898 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000899 funcMask &= userBits;
900 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000901 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000902
903 settings->setFunc(face, func);
904 settings->setWriteMask(face, writeMask);
905 settings->setFuncMask(face, funcMask);
906 settings->setFuncRef(face, funcRef);
907
908 if (GrStencilSettings::kFront_Face == face) {
909 face = GrStencilSettings::kBack_Face;
910 finished = !twoSided;
911 } else {
912 finished = true;
913 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000914 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000915 if (!twoSided) {
916 settings->copyFrontSettingsToBack();
917 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000918}
919
920////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000921GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
922 GrReducedClip::InitialState initialState,
923 const GrReducedClip::ElementList& elements,
924 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000925 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000926
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000927 GrTexture* result;
928 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
929 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000930 }
931
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000932 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000933 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000934 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000935 }
936
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000937 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
938 // the top left corner of the resulting rect to the top left of the texture.
939 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
940
robertphillips@google.com2c756812012-05-22 20:28:23 +0000941 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000942
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000943 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000944 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
945 SkIntToScalar(-clipSpaceIBounds.fTop));
946 helper.init(maskSpaceIBounds, &matrix);
947
948 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000949
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000950 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000951
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000952 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000953
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000954 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000955 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000956
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000957 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
958 // Intersect and reverse difference require modifying pixels outside of the geometry
959 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
960 // but leave the pixels inside the geometry alone. For reverse difference we invert all
961 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000962 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000963 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000964 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000965 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000966 }
967
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000968 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000969 // convert the rect to a path so we can invert the fill
970 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000971 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000972 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000973
sugoi@google.com12b4e272012-12-06 20:13:11 +0000974 helper.draw(temp, stroke, SkRegion::kReplace_Op,
975 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000976 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000977 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000978 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000979 SkPath clipPath = element->getPath();
980 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000981 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000982 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000983 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000984 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000985 }
986
987 continue;
988 }
989
990 // The other ops (union, xor, diff) only affect pixels inside
991 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000992 if (Element::kRect_Type == element->getType()) {
993 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
994 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000995 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000996 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000997 }
998 }
999
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001000 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001001
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001002 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001003 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001004}
1005
robertphillips@google.comf294b772012-04-27 14:29:26 +00001006////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001007void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001008 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001009}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001010
1011void GrClipMaskManager::setGpu(GrGpu* gpu) {
1012 fGpu = gpu;
1013 fAACache.setContext(gpu->getContext());
1014}