blob: 0f915666fb617ee65ad2560a4c9dc381413e133f [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,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000464 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000465 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000466
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000467 // When we use the stencil in the below loop it is important to have this clip installed.
468 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
469 // pass must not set values outside of this bounds or stencil values outside the rect won't be
470 // cleared.
471 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
472 drawState->enableState(GrDrawState::kClip_StateBit);
473
robertphillips@google.comf105b102012-05-14 12:18:26 +0000474 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000475 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000476 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000477 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000478 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000479 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000480
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000481 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
482 GrPathRenderer* pr = NULL;
483 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
484 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000485 // 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 +0000486 // mask buffer can be substantially larger than the actually clip stack element. We
487 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000488 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000489 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000490
491 if (useTemp) {
492 if (invert) {
493 maskSpaceElementIBounds = maskSpaceIBounds;
494 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000495 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000496 elementBounds.offset(clipToMaskOffset);
497 elementBounds.roundOut(&maskSpaceElementIBounds);
498 }
499
500 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
501 if (NULL == temp.texture()) {
502 fAACache.reset();
503 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000504 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000505 dst = temp.texture();
506 // clear the temp target and set blend to replace
507 fGpu->clear(&maskSpaceElementIBounds,
508 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000509 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000510 dst->asRenderTarget());
511 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000512
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000513 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000514 // draw directly into the result with the stencil set to make the pixels affected
515 // by the clip shape be non-zero.
516 dst = result;
517 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
518 kReplace_StencilOp,
519 kReplace_StencilOp,
520 kAlways_StencilFunc,
521 0xffff,
522 0xffff,
523 0xffff);
524 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000525 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000526 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000527
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000528 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000529
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000530 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000531 fAACache.reset();
532 return NULL;
533 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000534
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000535 if (useTemp) {
536 // Now draw into the accumulator using the real operation and the temp buffer as a
537 // texture
538 this->mergeMask(result,
539 temp.texture(),
540 op,
541 maskSpaceIBounds,
542 maskSpaceElementIBounds);
543 } else {
544 // Draw to the exterior pixels (those with a zero stencil value).
545 drawState->setAlpha(invert ? 0xff : 0x00);
546 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
547 kZero_StencilOp,
548 kZero_StencilOp,
549 kEqual_StencilFunc,
550 0xffff,
551 0x0000,
552 0xffff);
553 drawState->setStencil(kDrawOutsideElement);
554 fGpu->drawSimpleRect(clipSpaceIBounds);
555 drawState->disableStencil();
556 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000557 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000558 // all the remaining ops can just be directly draw into the accumulation buffer
559 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000560 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000561 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562 }
563 }
564
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000565 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000566 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000567}
568
569////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000570// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000571// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000572bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
573 const ElementList& elements,
574 const SkIRect& clipSpaceIBounds,
575 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000576
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000577 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000578
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000579 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000580 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000581
582 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000583 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000584
585 // TODO: dynamically attach a SB when needed.
586 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
587 if (NULL == stencilBuffer) {
588 return false;
589 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000590 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000591
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000592 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000593
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000594 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000595
bsalomon@google.com137f1342013-05-29 21:27:53 +0000596 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
597 SkVector translate = {
598 SkIntToScalar(clipSpaceToStencilOffset.fX),
599 SkIntToScalar(clipSpaceToStencilOffset.fY)
600 };
601 SkMatrix matrix;
602 matrix.setTranslate(translate);
603 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000604 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000605
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000606 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000607
bsalomon@google.com9f131742012-12-13 20:43:56 +0000608 // We set the current clip to the bounds so that our recursive draws are scissored to them.
609 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
610 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
611 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
612 drawState->enableState(GrDrawState::kClip_StateBit);
613
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000614#if !VISUALIZE_COMPLEX_CLIP
615 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
616#endif
617
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000618 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000619 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000620 clipBit = (1 << (clipBit-1));
621
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000622 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000623
624 // walk through each clip element and perform its set op
625 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000626 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
627 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000628 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000629 // enabled at bottom of loop
630 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000631 // if the target is MSAA then we want MSAA enabled when the clip is soft
632 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000633 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000634 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000635
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000636 // This will be used to determine whether the clip shape can be rendered into the
637 // stencil with arbitrary stencil settings.
638 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000640 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000641
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000642 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000643
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000644 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000645 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000646 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000647 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000648 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000649 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000650 SkASSERT(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000651 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000652 fillInverted = clipPath->isInverseFillType();
653 if (fillInverted) {
654 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000655 }
656 pr = this->getContext()->getPathRenderer(*clipPath,
657 stroke,
658 fGpu,
659 false,
660 GrPathRendererChain::kStencilOnly_DrawType,
661 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000662 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663 return false;
664 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665 }
666
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667 int passes;
668 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
669
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000670 bool canRenderDirectToStencil =
671 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000672 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000673 // fill rule, and set operation can
674 // we render the element directly to
675 // stencil bit used for clipping.
676 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
677 canRenderDirectToStencil,
678 clipBit,
679 fillInverted,
680 &passes,
681 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682
683 // draw the element to the client stencil bits if necessary
684 if (!canDrawDirectToClip) {
685 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000686 kIncClamp_StencilOp,
687 kIncClamp_StencilOp,
688 kAlways_StencilFunc,
689 0xffff,
690 0x0000,
691 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000693 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000695 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000697 SkASSERT(Element::kPath_Type == element->getType());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000698 if (!clipPath->isEmpty()) {
699 if (canRenderDirectToStencil) {
700 *drawState->stencil() = gDrawToStencil;
701 pr->drawPath(*clipPath, stroke, fGpu, false);
702 } else {
703 pr->stencilPath(*clipPath, stroke, fGpu);
704 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000705 }
706 }
707 }
708
709 // now we modify the clip bit by rendering either the clip
710 // element directly or a bounding rect of the entire clip.
711 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
712 for (int p = 0; p < passes; ++p) {
713 *drawState->stencil() = stencilSettings[p];
714 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000715 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000717 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000719 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000721 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722 }
723 } else {
724 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000725 // The view matrix is setup to do clip space -> stencil space translation, so
726 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000727 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728 }
729 }
730 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000731 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000732 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000733 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000734 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735 return true;
736}
737
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000738
bsalomon@google.com411dad02012-06-05 20:24:20 +0000739// mapping of clip-respecting stencil funcs to normal stencil funcs
740// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000741static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000742 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
743 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
744 // In the Clip Funcs
745 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
746 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
747 kLess_StencilFunc, // kLessIfInClip_StencilFunc
748 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
749 // Special in the clip func that forces user's ref to be 0.
750 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
751 // make ref 0 and do normal nequal.
752 },
753 {// Stencil-Clipping is ENABLED
754 // In the Clip Funcs
755 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
756 // eq stencil clip bit, mask
757 // out user bits.
758
759 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
760 // add stencil bit to mask and ref
761
762 kLess_StencilFunc, // kLessIfInClip_StencilFunc
763 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
764 // for both of these we can add
765 // the clip bit to the mask and
766 // ref and compare as normal
767 // Special in the clip func that forces user's ref to be 0.
768 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
769 // make ref have only the clip bit set
770 // and make comparison be less
771 // 10..0 < 1..user_bits..
772 }
773};
774
bsalomon@google.coma3201942012-06-21 19:58:20 +0000775namespace {
776// Sets the settings to clip against the stencil buffer clip while ignoring the
777// client bits.
778const GrStencilSettings& basic_apply_stencil_clip_settings() {
779 // stencil settings to use when clip is in stencil
780 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
781 kKeep_StencilOp,
782 kKeep_StencilOp,
783 kAlwaysIfInClip_StencilFunc,
784 0x0000,
785 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000786 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000787 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
788}
789}
790
791void GrClipMaskManager::setGpuStencil() {
792 // We make two copies of the StencilSettings here (except in the early
793 // exit scenario. One copy from draw state to the stack var. Then another
794 // from the stack var to the gpu. We could make this class hold a ptr to
795 // GrGpu's fStencilSettings and eliminate the stack copy here.
796
797 const GrDrawState& drawState = fGpu->getDrawState();
798
799 // use stencil for clipping if clipping is enabled and the clip
800 // has been written into the stencil.
801 GrClipMaskManager::StencilClipMode clipMode;
802 if (this->isClipInStencil() && drawState.isClipState()) {
803 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
804 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000805 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000806 GrGpu::kModifyStencilClip_StateBit));
807 } else if (drawState.isStateFlagEnabled(
808 GrGpu::kModifyStencilClip_StateBit)) {
809 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
810 } else {
811 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
812 }
813
814 GrStencilSettings settings;
815 // The GrGpu client may not be using the stencil buffer but we may need to
816 // enable it in order to respect a stencil clip.
817 if (drawState.getStencil().isDisabled()) {
818 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
819 settings = basic_apply_stencil_clip_settings();
820 } else {
821 fGpu->disableStencil();
822 return;
823 }
824 } else {
825 settings = drawState.getStencil();
826 }
827
828 // TODO: dynamically attach a stencil buffer
829 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000830 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000831 drawState.getRenderTarget()->getStencilBuffer();
832 if (NULL != stencilBuffer) {
833 stencilBits = stencilBuffer->bits();
834 }
835
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000836 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
837 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000838 this->adjustStencilParams(&settings, clipMode, stencilBits);
839 fGpu->setStencilSettings(settings);
840}
841
842void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
843 StencilClipMode mode,
844 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000845 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000846
847 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000848 // We assume that this clip manager itself is drawing to the GrGpu and
849 // has already setup the correct values.
850 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000851 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000852
bsalomon@google.com411dad02012-06-05 20:24:20 +0000853 unsigned int clipBit = (1 << (stencilBitCnt - 1));
854 unsigned int userBits = clipBit - 1;
855
bsalomon@google.coma3201942012-06-21 19:58:20 +0000856 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000857 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000858
bsalomon@google.coma3201942012-06-21 19:58:20 +0000859 bool finished = false;
860 while (!finished) {
861 GrStencilFunc func = settings->func(face);
862 uint16_t writeMask = settings->writeMask(face);
863 uint16_t funcMask = settings->funcMask(face);
864 uint16_t funcRef = settings->funcRef(face);
865
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000866 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000867
868 writeMask &= userBits;
869
870 if (func >= kBasicStencilFuncCount) {
871 int respectClip = kRespectClip_StencilClipMode == mode;
872 if (respectClip) {
873 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000874 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000875 switch (func) {
876 case kAlwaysIfInClip_StencilFunc:
877 funcMask = clipBit;
878 funcRef = clipBit;
879 break;
880 case kEqualIfInClip_StencilFunc:
881 case kLessIfInClip_StencilFunc:
882 case kLEqualIfInClip_StencilFunc:
883 funcMask = (funcMask & userBits) | clipBit;
884 funcRef = (funcRef & userBits) | clipBit;
885 break;
886 case kNonZeroIfInClip_StencilFunc:
887 funcMask = (funcMask & userBits) | clipBit;
888 funcRef = clipBit;
889 break;
890 default:
891 GrCrash("Unknown stencil func");
892 }
893 } else {
894 funcMask &= userBits;
895 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000896 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000897 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000898 gSpecialToBasicStencilFunc[respectClip];
899 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000900 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000901 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000902 funcMask &= userBits;
903 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000904 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000905
906 settings->setFunc(face, func);
907 settings->setWriteMask(face, writeMask);
908 settings->setFuncMask(face, funcMask);
909 settings->setFuncRef(face, funcRef);
910
911 if (GrStencilSettings::kFront_Face == face) {
912 face = GrStencilSettings::kBack_Face;
913 finished = !twoSided;
914 } else {
915 finished = true;
916 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000917 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000918 if (!twoSided) {
919 settings->copyFrontSettingsToBack();
920 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000921}
922
923////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000924GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
925 GrReducedClip::InitialState initialState,
926 const GrReducedClip::ElementList& elements,
927 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000928 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000929
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000930 GrTexture* result;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000931 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000932 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000933 }
934
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000935 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000936 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000937 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000938 }
939
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000940 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
941 // the top left corner of the resulting rect to the top left of the texture.
942 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
943
robertphillips@google.com2c756812012-05-22 20:28:23 +0000944 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000945
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000946 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000947 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
948 SkIntToScalar(-clipSpaceIBounds.fTop));
949 helper.init(maskSpaceIBounds, &matrix);
950
951 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000952
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000953 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000954
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000955 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000956
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000957 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000958 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000959
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000960 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
961 // Intersect and reverse difference require modifying pixels outside of the geometry
962 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
963 // but leave the pixels inside the geometry alone. For reverse difference we invert all
964 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000965 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000966 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000967 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000968 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000969 }
970
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000971 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000972 // convert the rect to a path so we can invert the fill
973 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000974 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000975 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000976
sugoi@google.com12b4e272012-12-06 20:13:11 +0000977 helper.draw(temp, stroke, SkRegion::kReplace_Op,
978 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000979 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000980 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000981 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000982 SkPath clipPath = element->getPath();
983 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000984 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000985 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000986 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000987 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000988 }
989
990 continue;
991 }
992
993 // The other ops (union, xor, diff) only affect pixels inside
994 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000995 if (Element::kRect_Type == element->getType()) {
996 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
997 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000998 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000999 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001000 }
1001 }
1002
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001003 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001004
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001005 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001006 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001007}
1008
robertphillips@google.comf294b772012-04-27 14:29:26 +00001009////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001010void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001011 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001012}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001013
1014void GrClipMaskManager::setGpu(GrGpu* gpu) {
1015 fGpu = gpu;
1016 fAACache.setContext(gpu->getContext());
1017}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001018
1019void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1020 const GrDrawState& drawState = fGpu->getDrawState();
1021 GrClipMaskManager::StencilClipMode clipMode;
1022 if (this->isClipInStencil() && drawState.isClipState()) {
1023 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1024 // We can't be modifying the clip and respecting it at the same time.
1025 SkASSERT(!drawState.isStateFlagEnabled(
1026 GrGpu::kModifyStencilClip_StateBit));
1027 } else if (drawState.isStateFlagEnabled(
1028 GrGpu::kModifyStencilClip_StateBit)) {
1029 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1030 } else {
1031 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1032 }
1033
1034 // TODO: dynamically attach a stencil buffer
1035 int stencilBits = 0;
1036 GrStencilBuffer* stencilBuffer =
1037 drawState.getRenderTarget()->getStencilBuffer();
1038 if (NULL != stencilBuffer) {
1039 stencilBits = stencilBuffer->bits();
1040 this->adjustStencilParams(settings, clipMode, stencilBits);
1041 }
1042}