blob: 3aef3dee67d1b36d4f0a498516de0ad46c783533 [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);
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000116 int32_t genID;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000117 InitialState initialState;
118 SkIRect clipSpaceIBounds;
119 bool requiresAA;
120 bool isRect = false;
121
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000122 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000123
124 const GrRenderTarget* rt = drawState->getRenderTarget();
125 // GrDrawTarget should have filtered this for us
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000126 SkASSERT(NULL != rt);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000127
128 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
129
130 if (!ignoreClip) {
131 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
132 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
133 ReduceClipStack(*clipDataIn->fClipStack,
134 clipSpaceRTIBounds,
135 &elements,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000136 &genID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000137 &initialState,
138 &clipSpaceIBounds,
139 &requiresAA);
140 if (elements.isEmpty()) {
141 if (kAllIn_InitialState == initialState) {
142 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
143 isRect = true;
144 } else {
145 return false;
146 }
147 }
148 }
149
150 if (ignoreClip) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000151 fGpu->disableScissor();
152 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000153 return true;
154 }
155
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000156#if GR_AA_CLIP
157 // TODO: catch isRect && requiresAA and use clip planes if available rather than a mask.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000158
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000159 // If MSAA is enabled we can do everything in the stencil buffer.
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000160 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000161 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000162
163 if (this->useSWOnlyPath(elements)) {
164 // The clip geometry is complex enough that it will be more efficient to create it
165 // entirely in software
166 result = this->createSoftwareClipMask(genID,
167 initialState,
168 elements,
169 clipSpaceIBounds);
170 } else {
171 result = this->createAlphaClipMask(genID,
172 initialState,
173 elements,
174 clipSpaceIBounds);
175 }
176
177 if (NULL != result) {
178 // The mask's top left coord should be pinned to the rounded-out top left corner of
179 // clipSpace bounds. We determine the mask's position WRT to the render target here.
180 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
181 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000182 are->set(fGpu->drawState());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000183 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000184 fGpu->disableScissor();
185 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000186 return true;
187 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000188 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000189 }
190#endif // GR_AA_CLIP
191
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000192 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
193 // be created. In either case, free up the texture in the anti-aliased mask cache.
194 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
195 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
196 // "incorrectly" clearing the AA cache.
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000197 fAACache.reset();
198
bsalomon@google.coma3201942012-06-21 19:58:20 +0000199 // If the clip is a rectangle then just set the scissor. Otherwise, create
200 // a stencil mask.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000201 if (isRect) {
202 SkIRect clipRect = clipSpaceIBounds;
203 clipRect.offset(-clipDataIn->fOrigin);
204 fGpu->enableScissor(clipRect);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000205 this->setGpuStencil();
206 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000207 }
208
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000209 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000210 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000211 this->createStencilClipMask(genID,
212 initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000213 elements,
214 clipSpaceIBounds,
215 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000216
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000217 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
218 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
219 // use both stencil and scissor test to the bounds for the final draw.
220 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
221 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
222 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000223 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000224 return true;
225}
226
227#define VISUALIZE_COMPLEX_CLIP 0
228
229#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000230 #include "SkRandom.h"
231 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000232 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
233#else
234 #define SET_RANDOM_COLOR
235#endif
236
237namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000238
239////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000240// set up the OpenGL blend function to perform the specified
241// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000242void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000243
244 switch (op) {
245 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000246 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000247 break;
248 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000249 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000250 break;
251 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000252 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000253 break;
254 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000255 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000256 break;
257 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000258 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000259 break;
260 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000261 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000262 break;
263 default:
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000264 SkASSERT(false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000265 break;
266 }
267}
268
robertphillips@google.com72176b22012-05-23 13:19:12 +0000269}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000270
271////////////////////////////////////////////////////////////////////////////////
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000272bool GrClipMaskManager::drawElement(GrTexture* target,
273 const SkClipStack::Element* element,
274 GrPathRenderer* pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000275 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000276
277 drawState->setRenderTarget(target->asRenderTarget());
278
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000279 switch (element->getType()) {
280 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000281 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
282 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000283 if (element->isAA()) {
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000284 getContext()->getAARectRenderer()->fillAARect(fGpu,
285 fGpu,
286 element->getRect(),
robertphillips@google.comb19cb7f2013-05-02 15:37:20 +0000287 SkMatrix::I(),
robertphillips@google.comafd1cba2013-05-14 19:47:47 +0000288 element->getRect(),
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000289 false);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000290 } else {
291 fGpu->drawSimpleRect(element->getRect(), NULL);
292 }
293 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000294 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000295 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
296 if (path->isInverseFillType()) {
297 path.writable()->toggleInverseFillType();
298 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000299 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000300 if (NULL == pr) {
301 GrPathRendererChain::DrawType type;
302 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
303 GrPathRendererChain::kColor_DrawType;
304 pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
305 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000306 if (NULL == pr) {
307 return false;
308 }
309 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
310 break;
311 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000312 default:
313 // something is wrong if we're trying to draw an empty element.
314 GrCrash("Unexpected element type");
315 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000316 }
317 return true;
318}
319
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000320bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
321 const SkClipStack::Element* element,
322 GrPathRenderer** pr) {
323 GrDrawState* drawState = fGpu->drawState();
324 drawState->setRenderTarget(target->asRenderTarget());
325
326 switch (element->getType()) {
327 case Element::kRect_Type:
328 return true;
329 case Element::kPath_Type: {
330 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
331 if (path->isInverseFillType()) {
332 path.writable()->toggleInverseFillType();
333 }
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000334 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000335 GrPathRendererChain::DrawType type = element->isAA() ?
336 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
337 GrPathRendererChain::kStencilAndColor_DrawType;
338 *pr = this->getContext()->getPathRenderer(*path, stroke, fGpu, false, type);
339 return NULL != *pr;
340 }
341 default:
342 // something is wrong if we're trying to draw an empty element.
343 GrCrash("Unexpected element type");
344 return false;
345 }
346}
347
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000348void GrClipMaskManager::mergeMask(GrTexture* dstMask,
349 GrTexture* srcMask,
350 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000351 const SkIRect& dstBound,
352 const SkIRect& srcBound) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000353 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000354 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000355 SkAssertResult(avmr.setIdentity(drawState));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000356 GrDrawState::AutoRestoreEffects are(drawState);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000357
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000358 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000360 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000361
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000362 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000363 sampleM.setIDiv(srcMask->width(), srcMask->height());
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000364
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000365 drawState->addColorEffect(
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000366 GrTextureDomainEffect::Create(srcMask,
367 sampleM,
368 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
bsalomon@google.comc7818882013-03-20 19:19:53 +0000369 GrTextureDomainEffect::kDecal_WrapMode,
humper@google.comb86add12013-07-25 18:49:07 +0000370 GrTextureParams::kNone_FilterMode))->unref();
reed@google.com44699382013-10-31 17:28:30 +0000371 fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000372}
373
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000374// get a texture to act as a temporary buffer for AA clip boolean operations
375// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000376void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000377 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000378 // we've already allocated the temp texture
379 return;
380 }
381
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000382 GrTextureDesc desc;
383 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000384 desc.fWidth = width;
385 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000386 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000387
robertphillips@google.com2c756812012-05-22 20:28:23 +0000388 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000389}
390
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000391////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000392// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
393// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
394// hit)
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000395bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000396 const SkIRect& clipSpaceIBounds,
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000397 GrTexture** result,
398 bool willUpload) {
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000399 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000400 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000401
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000402 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
403 // Since we are setting up the cache we know the last lookup was a miss. Free up the
404 // currently cached mask so it can be reused.
405 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000406
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000407 GrTextureDesc desc;
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000408 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000409 desc.fWidth = clipSpaceIBounds.width();
410 desc.fHeight = clipSpaceIBounds.height();
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000411 desc.fConfig = kRGBA_8888_GrPixelConfig;
robertphillips@google.com94bdd7e2013-10-31 15:50:43 +0000412 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000413 // We would always like A8 but it isn't supported on all platforms
414 desc.fConfig = kAlpha_8_GrPixelConfig;
415 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000416
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000417 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000418 }
419
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000420 *result = fAACache.getLastMask();
421 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000422}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000423
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000424////////////////////////////////////////////////////////////////////////////////
425// Create a 8-bit clip mask in alpha
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000426GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000427 InitialState initialState,
428 const ElementList& elements,
429 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000430 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000431
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000432 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000433 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000434 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000435 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000436 }
437
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000438 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000439 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000441 }
442
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000444 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000445 SkIntToScalar(-clipSpaceIBounds.fLeft),
446 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000447 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000448 // The texture may be larger than necessary, this rect represents the part of the texture
449 // we populate with a rasterization of the clip.
450 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
451
bsalomon@google.com137f1342013-05-29 21:27:53 +0000452 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
453 SkMatrix translate;
454 translate.setTranslate(clipToMaskOffset);
455 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
456
457 GrDrawState* drawState = fGpu->drawState();
458
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000459 // We're drawing a coverage mask and want coverage to be run through the blend function.
460 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
461
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000462 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
463 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000464 fGpu->clear(&maskSpaceIBounds,
465 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000466 true,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000468
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000469 // When we use the stencil in the below loop it is important to have this clip installed.
470 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
471 // pass must not set values outside of this bounds or stencil values outside the rect won't be
472 // cleared.
473 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
474 drawState->enableState(GrDrawState::kClip_StateBit);
475
robertphillips@google.comf105b102012-05-14 12:18:26 +0000476 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000477 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000478 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000479 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000480 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000481 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000482
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000483 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
484 GrPathRenderer* pr = NULL;
485 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
486 GrTexture* dst;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000487 // 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 +0000488 // mask buffer can be substantially larger than the actually clip stack element. We
489 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000490 // the accumulator.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000491 SkIRect maskSpaceElementIBounds;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000492
493 if (useTemp) {
494 if (invert) {
495 maskSpaceElementIBounds = maskSpaceIBounds;
496 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000497 SkRect elementBounds = element->getBounds();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000498 elementBounds.offset(clipToMaskOffset);
499 elementBounds.roundOut(&maskSpaceElementIBounds);
500 }
501
502 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
503 if (NULL == temp.texture()) {
504 fAACache.reset();
505 return NULL;
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000506 }
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000507 dst = temp.texture();
508 // clear the temp target and set blend to replace
509 fGpu->clear(&maskSpaceElementIBounds,
510 invert ? 0xffffffff : 0x00000000,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000511 true,
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000512 dst->asRenderTarget());
513 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000514
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000515 } else {
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000516 // draw directly into the result with the stencil set to make the pixels affected
517 // by the clip shape be non-zero.
518 dst = result;
519 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
520 kReplace_StencilOp,
521 kReplace_StencilOp,
522 kAlways_StencilFunc,
523 0xffff,
524 0xffff,
525 0xffff);
526 drawState->setStencil(kStencilInElement);
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000527 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000528 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000529
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000530 drawState->setAlpha(invert ? 0x00 : 0xff);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000531
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000532 if (!this->drawElement(dst, element, pr)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000533 fAACache.reset();
534 return NULL;
535 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000536
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000537 if (useTemp) {
538 // Now draw into the accumulator using the real operation and the temp buffer as a
539 // texture
540 this->mergeMask(result,
541 temp.texture(),
542 op,
543 maskSpaceIBounds,
544 maskSpaceElementIBounds);
545 } else {
546 // Draw to the exterior pixels (those with a zero stencil value).
547 drawState->setAlpha(invert ? 0xff : 0x00);
548 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
549 kZero_StencilOp,
550 kZero_StencilOp,
551 kEqual_StencilFunc,
552 0xffff,
553 0x0000,
554 0xffff);
555 drawState->setStencil(kDrawOutsideElement);
556 fGpu->drawSimpleRect(clipSpaceIBounds);
557 drawState->disableStencil();
558 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000560 // all the remaining ops can just be directly draw into the accumulation buffer
561 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000562 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000563 this->drawElement(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000564 }
565 }
566
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000567 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000568 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000569}
570
571////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000572// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000573// (as opposed to canvas) coordinates
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000574bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
575 InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000576 const ElementList& elements,
577 const SkIRect& clipSpaceIBounds,
578 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000579
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000580 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000581
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000582 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000583 SkASSERT(drawState->isClipState());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000584
585 GrRenderTarget* rt = drawState->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000586 SkASSERT(NULL != rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000587
588 // TODO: dynamically attach a SB when needed.
589 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
590 if (NULL == stencilBuffer) {
591 return false;
592 }
593
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000594 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000595
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000596 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000597
bsalomon@google.com137f1342013-05-29 21:27:53 +0000598 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
599 SkVector translate = {
600 SkIntToScalar(clipSpaceToStencilOffset.fX),
601 SkIntToScalar(clipSpaceToStencilOffset.fY)
602 };
603 SkMatrix matrix;
604 matrix.setTranslate(translate);
605 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000606 drawState = fGpu->drawState();
bsalomon@google.com137f1342013-05-29 21:27:53 +0000607
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000608 drawState->setRenderTarget(rt);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609
bsalomon@google.com9f131742012-12-13 20:43:56 +0000610 // We set the current clip to the bounds so that our recursive draws are scissored to them.
611 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
612 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
613 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
614 drawState->enableState(GrDrawState::kClip_StateBit);
615
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000616#if !VISUALIZE_COMPLEX_CLIP
617 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
618#endif
619
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000620 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000621 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000622 clipBit = (1 << (clipBit-1));
623
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000624 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000625
626 // walk through each clip element and perform its set op
627 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000628 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
629 const Element* element = iter.get();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000630 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631 // enabled at bottom of loop
632 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000633 // if the target is MSAA then we want MSAA enabled when the clip is soft
634 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000635 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000636 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000637
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000638 // This will be used to determine whether the clip shape can be rendered into the
639 // stencil with arbitrary stencil settings.
640 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000642 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000643
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000644 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000645
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000646 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000647 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000648 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000649 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000650 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000651 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000652 SkASSERT(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000653 clipPath.init(element->getPath());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000654 fillInverted = clipPath->isInverseFillType();
655 if (fillInverted) {
656 clipPath.writable()->toggleInverseFillType();
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000657 }
658 pr = this->getContext()->getPathRenderer(*clipPath,
659 stroke,
660 fGpu,
661 false,
662 GrPathRendererChain::kStencilOnly_DrawType,
663 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664 if (NULL == pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000665 return false;
666 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667 }
668
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000669 int passes;
670 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
671
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000672 bool canRenderDirectToStencil =
673 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000674 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000675 // fill rule, and set operation can
676 // we render the element directly to
677 // stencil bit used for clipping.
678 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
679 canRenderDirectToStencil,
680 clipBit,
681 fillInverted,
682 &passes,
683 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000684
685 // draw the element to the client stencil bits if necessary
686 if (!canDrawDirectToClip) {
687 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000688 kIncClamp_StencilOp,
689 kIncClamp_StencilOp,
690 kAlways_StencilFunc,
691 0xffff,
692 0x0000,
693 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000695 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000697 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000699 SkASSERT(Element::kPath_Type == element->getType());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000700 if (!clipPath->isEmpty()) {
701 if (canRenderDirectToStencil) {
702 *drawState->stencil() = gDrawToStencil;
703 pr->drawPath(*clipPath, stroke, fGpu, false);
704 } else {
705 pr->stencilPath(*clipPath, stroke, fGpu);
706 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707 }
708 }
709 }
710
711 // now we modify the clip bit by rendering either the clip
712 // element directly or a bounding rect of the entire clip.
713 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
714 for (int p = 0; p < passes; ++p) {
715 *drawState->stencil() = stencilSettings[p];
716 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000717 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000719 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000721 SkASSERT(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000723 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724 }
725 } else {
726 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000727 // The view matrix is setup to do clip space -> stencil space translation, so
728 // draw rect in clip space.
reed@google.com44699382013-10-31 17:28:30 +0000729 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730 }
731 }
732 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000734 // set this last because recursive draws may overwrite it back to kNone.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000735 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000736 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737 return true;
738}
739
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000740
bsalomon@google.com411dad02012-06-05 20:24:20 +0000741// mapping of clip-respecting stencil funcs to normal stencil funcs
742// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000743static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000744 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
745 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
746 // In the Clip Funcs
747 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
748 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
749 kLess_StencilFunc, // kLessIfInClip_StencilFunc
750 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
751 // Special in the clip func that forces user's ref to be 0.
752 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
753 // make ref 0 and do normal nequal.
754 },
755 {// Stencil-Clipping is ENABLED
756 // In the Clip Funcs
757 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
758 // eq stencil clip bit, mask
759 // out user bits.
760
761 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
762 // add stencil bit to mask and ref
763
764 kLess_StencilFunc, // kLessIfInClip_StencilFunc
765 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
766 // for both of these we can add
767 // the clip bit to the mask and
768 // ref and compare as normal
769 // Special in the clip func that forces user's ref to be 0.
770 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
771 // make ref have only the clip bit set
772 // and make comparison be less
773 // 10..0 < 1..user_bits..
774 }
775};
776
bsalomon@google.coma3201942012-06-21 19:58:20 +0000777namespace {
778// Sets the settings to clip against the stencil buffer clip while ignoring the
779// client bits.
780const GrStencilSettings& basic_apply_stencil_clip_settings() {
781 // stencil settings to use when clip is in stencil
782 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
783 kKeep_StencilOp,
784 kKeep_StencilOp,
785 kAlwaysIfInClip_StencilFunc,
786 0x0000,
787 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000788 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000789 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
790}
791}
792
793void GrClipMaskManager::setGpuStencil() {
794 // We make two copies of the StencilSettings here (except in the early
795 // exit scenario. One copy from draw state to the stack var. Then another
796 // from the stack var to the gpu. We could make this class hold a ptr to
797 // GrGpu's fStencilSettings and eliminate the stack copy here.
798
799 const GrDrawState& drawState = fGpu->getDrawState();
800
801 // use stencil for clipping if clipping is enabled and the clip
802 // has been written into the stencil.
803 GrClipMaskManager::StencilClipMode clipMode;
804 if (this->isClipInStencil() && drawState.isClipState()) {
805 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
806 // We can't be modifying the clip and respecting it at the same time.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000807 SkASSERT(!drawState.isStateFlagEnabled(
bsalomon@google.coma3201942012-06-21 19:58:20 +0000808 GrGpu::kModifyStencilClip_StateBit));
809 } else if (drawState.isStateFlagEnabled(
810 GrGpu::kModifyStencilClip_StateBit)) {
811 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
812 } else {
813 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
814 }
815
816 GrStencilSettings settings;
817 // The GrGpu client may not be using the stencil buffer but we may need to
818 // enable it in order to respect a stencil clip.
819 if (drawState.getStencil().isDisabled()) {
820 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
821 settings = basic_apply_stencil_clip_settings();
822 } else {
823 fGpu->disableStencil();
824 return;
825 }
826 } else {
827 settings = drawState.getStencil();
828 }
829
830 // TODO: dynamically attach a stencil buffer
831 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000832 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000833 drawState.getRenderTarget()->getStencilBuffer();
834 if (NULL != stencilBuffer) {
835 stencilBits = stencilBuffer->bits();
836 }
837
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000838 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
839 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000840 this->adjustStencilParams(&settings, clipMode, stencilBits);
841 fGpu->setStencilSettings(settings);
842}
843
844void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
845 StencilClipMode mode,
846 int stencilBitCnt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000847 SkASSERT(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000848
849 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000850 // We assume that this clip manager itself is drawing to the GrGpu and
851 // has already setup the correct values.
852 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000853 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000854
bsalomon@google.com411dad02012-06-05 20:24:20 +0000855 unsigned int clipBit = (1 << (stencilBitCnt - 1));
856 unsigned int userBits = clipBit - 1;
857
bsalomon@google.coma3201942012-06-21 19:58:20 +0000858 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000859 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000860
bsalomon@google.coma3201942012-06-21 19:58:20 +0000861 bool finished = false;
862 while (!finished) {
863 GrStencilFunc func = settings->func(face);
864 uint16_t writeMask = settings->writeMask(face);
865 uint16_t funcMask = settings->funcMask(face);
866 uint16_t funcRef = settings->funcRef(face);
867
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000868 SkASSERT((unsigned) func < kStencilFuncCount);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000869
870 writeMask &= userBits;
871
872 if (func >= kBasicStencilFuncCount) {
873 int respectClip = kRespectClip_StencilClipMode == mode;
874 if (respectClip) {
875 // The GrGpu class should have checked this
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000876 SkASSERT(this->isClipInStencil());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000877 switch (func) {
878 case kAlwaysIfInClip_StencilFunc:
879 funcMask = clipBit;
880 funcRef = clipBit;
881 break;
882 case kEqualIfInClip_StencilFunc:
883 case kLessIfInClip_StencilFunc:
884 case kLEqualIfInClip_StencilFunc:
885 funcMask = (funcMask & userBits) | clipBit;
886 funcRef = (funcRef & userBits) | clipBit;
887 break;
888 case kNonZeroIfInClip_StencilFunc:
889 funcMask = (funcMask & userBits) | clipBit;
890 funcRef = clipBit;
891 break;
892 default:
893 GrCrash("Unknown stencil func");
894 }
895 } else {
896 funcMask &= userBits;
897 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000898 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000899 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000900 gSpecialToBasicStencilFunc[respectClip];
901 func = table[func - kBasicStencilFuncCount];
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000902 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000903 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000904 funcMask &= userBits;
905 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000906 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000907
908 settings->setFunc(face, func);
909 settings->setWriteMask(face, writeMask);
910 settings->setFuncMask(face, funcMask);
911 settings->setFuncRef(face, funcRef);
912
913 if (GrStencilSettings::kFront_Face == face) {
914 face = GrStencilSettings::kBack_Face;
915 finished = !twoSided;
916 } else {
917 finished = true;
918 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000919 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000920 if (!twoSided) {
921 settings->copyFrontSettingsToBack();
922 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000923}
924
925////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000926GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000927 GrReducedClip::InitialState initialState,
928 const GrReducedClip::ElementList& elements,
929 const SkIRect& clipSpaceIBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000930 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000931
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000932 GrTexture* result;
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000933 if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000934 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000935 }
936
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000937 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000938 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000939 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000940 }
941
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000942 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
943 // the top left corner of the resulting rect to the top left of the texture.
944 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
945
robertphillips@google.com2c756812012-05-22 20:28:23 +0000946 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000947
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000948 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000949 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
950 SkIntToScalar(-clipSpaceIBounds.fTop));
951 helper.init(maskSpaceIBounds, &matrix);
952
953 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000954
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000955 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000956
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000957 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000958
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000959 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000960 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000961
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000962 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
963 // Intersect and reverse difference require modifying pixels outside of the geometry
964 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
965 // but leave the pixels inside the geometry alone. For reverse difference we invert all
966 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000967 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000968 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000969 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000970 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000971 }
972
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000973 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000974 // convert the rect to a path so we can invert the fill
975 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000976 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000977 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000978
sugoi@google.com12b4e272012-12-06 20:13:11 +0000979 helper.draw(temp, stroke, SkRegion::kReplace_Op,
980 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000981 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000982 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000983 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000984 SkPath clipPath = element->getPath();
985 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000986 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000987 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000988 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000989 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000990 }
991
992 continue;
993 }
994
995 // The other ops (union, xor, diff) only affect pixels inside
996 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000997 if (Element::kRect_Type == element->getType()) {
998 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
999 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001000 SkASSERT(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001001 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001002 }
1003 }
1004
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +00001005 helper.toTexture(result);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001006
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001007 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +00001008 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001009}
1010
robertphillips@google.comf294b772012-04-27 14:29:26 +00001011////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001012void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001013 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001014}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001015
1016void GrClipMaskManager::setGpu(GrGpu* gpu) {
1017 fGpu = gpu;
1018 fAACache.setContext(gpu->getContext());
1019}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001020
1021void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1022 const GrDrawState& drawState = fGpu->getDrawState();
1023 GrClipMaskManager::StencilClipMode clipMode;
1024 if (this->isClipInStencil() && drawState.isClipState()) {
1025 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1026 // We can't be modifying the clip and respecting it at the same time.
1027 SkASSERT(!drawState.isStateFlagEnabled(
1028 GrGpu::kModifyStencilClip_StateBit));
1029 } else if (drawState.isStateFlagEnabled(
1030 GrGpu::kModifyStencilClip_StateBit)) {
1031 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1032 } else {
1033 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1034 }
1035
1036 // TODO: dynamically attach a stencil buffer
1037 int stencilBits = 0;
1038 GrStencilBuffer* stencilBuffer =
1039 drawState.getRenderTarget()->getStencilBuffer();
1040 if (NULL != stencilBuffer) {
1041 stencilBits = stencilBuffer->bits();
1042 this->adjustStencilParams(settings, clipMode, stencilBits);
1043 }
1044}