blob: 11b301d61ad788a7599ebe471862ffcf0fd02322 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000010#include "effects/GrTextureDomainEffect.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000011#include "GrGpu.h"
12#include "GrRenderTarget.h"
13#include "GrStencilBuffer.h"
14#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000015#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000016#include "SkRasterClip.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000017#include "SkStroke.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000018#include "GrAAConvexPathRenderer.h"
19#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000021#include "GrCacheID.h"
22
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000023#include "SkTLazy.h"
24
robertphillips@google.com46a86002012-08-08 10:42:44 +000025GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000026
robertphillips@google.comba998f22012-10-12 11:33:56 +000027#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000028
bsalomon@google.com8182fa02012-12-04 14:06:06 +000029typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000030
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000031using namespace GrReducedClip;
32
bsalomon@google.com51a62862012-11-26 21:19:43 +000033////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000034namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000036// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037void setup_drawstate_aaclip(GrGpu* gpu,
38 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000039 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 GrDrawState* drawState = gpu->drawState();
41 GrAssert(drawState);
42
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000043 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000044
bsalomon@google.comb9086a02012-11-01 18:02:54 +000045 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000048 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000049 mat.preConcat(drawState->getViewMatrix());
50
bsalomon@google.com08283af2012-10-26 13:01:20 +000051 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052
53 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000054 // This could be a long-lived effect that is cached with the alpha-mask.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000055 drawState->stage(kMaskStage)->setEffect(
56 GrTextureDomainEffect::Create(result,
57 mat,
58 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
59 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000060}
61
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000062bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000063 GrGpu* gpu,
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000064 const SkPath& origPath,
sugoi@google.com12b4e272012-12-06 20:13:11 +000065 const SkStroke& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000066 bool doAA) {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000067 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
68 SkTCopyOnFirstWrite<SkPath> path(origPath);
69 if (path->isInverseFillType()) {
70 path.writable()->toggleInverseFillType();
71 }
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000072 // last (false) parameter disallows use of the SW path renderer
bsalomon@google.com45a15f52012-12-10 19:10:17 +000073 GrPathRendererChain::DrawType type = doAA ?
74 GrPathRendererChain::kColorAntiAlias_DrawType :
75 GrPathRendererChain::kColor_DrawType;
76
77 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000078}
79
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000080}
81
robertphillips@google.comfa662942012-05-17 12:20:22 +000082/*
83 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
84 * will be used on any element. If so, it returns true to indicate that the
85 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
86 */
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000087bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000088
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000089 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000090 // a clip gets complex enough it can just be done in SW regardless
91 // of whether it would invoke the GrSoftwarePathRenderer.
92 bool useSW = false;
sugoi@google.com12b4e272012-12-06 20:13:11 +000093 SkStroke stroke;
94 stroke.setDoFill(true);
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000095
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000096 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
97 const Element* element = iter.get();
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000098 // rects can always be drawn directly w/o using the software path
99 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000100 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000101 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000102 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000103 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000104 element->isAA())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000105 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000108 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000109}
110
robertphillips@google.comf294b772012-04-27 14:29:26 +0000111////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000112// sort out what kind of clip mask needs to be created: alpha, stencil,
113// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000114bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000115 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000116
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000117 ElementList elements(16);
118 InitialState initialState;
119 SkIRect clipSpaceIBounds;
120 bool requiresAA;
121 bool isRect = false;
122
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000123 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000124
125 const GrRenderTarget* rt = drawState->getRenderTarget();
126 // GrDrawTarget should have filtered this for us
127 GrAssert(NULL != rt);
128
129 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
130
131 if (!ignoreClip) {
132 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
133 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
134 ReduceClipStack(*clipDataIn->fClipStack,
135 clipSpaceRTIBounds,
136 &elements,
137 &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) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000161 int32_t genID = clipDataIn->fClipStack->getTopmostGenID();
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000162 GrTexture* result = NULL;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000163
164 if (this->useSWOnlyPath(elements)) {
165 // The clip geometry is complex enough that it will be more efficient to create it
166 // entirely in software
167 result = this->createSoftwareClipMask(genID,
168 initialState,
169 elements,
170 clipSpaceIBounds);
171 } else {
172 result = this->createAlphaClipMask(genID,
173 initialState,
174 elements,
175 clipSpaceIBounds);
176 }
177
178 if (NULL != result) {
179 // The mask's top left coord should be pinned to the rounded-out top left corner of
180 // clipSpace bounds. We determine the mask's position WRT to the render target here.
181 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
182 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
183 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;
211 this->createStencilClipMask(initialState,
212 elements,
213 clipSpaceIBounds,
214 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000215
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000216 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
217 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
218 // use both stencil and scissor test to the bounds for the final draw.
219 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
220 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
221 fGpu->enableScissor(scissorSpaceIBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000222 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000223 return true;
224}
225
226#define VISUALIZE_COMPLEX_CLIP 0
227
228#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000229 #include "SkRandom.h"
230 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000231 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
232#else
233 #define SET_RANDOM_COLOR
234#endif
235
236namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000237
238////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000239// set up the OpenGL blend function to perform the specified
240// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000241void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000242
243 switch (op) {
244 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000245 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000246 break;
247 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000248 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000249 break;
250 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000251 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000252 break;
253 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000254 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000255 break;
256 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000257 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000258 break;
259 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000260 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000261 break;
262 default:
263 GrAssert(false);
264 break;
265 }
266}
267
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000268////////////////////////////////////////////////////////////////////////////////
269bool draw_path_in_software(GrContext* context,
270 GrGpu* gpu,
271 const SkPath& path,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000272 bool doAA,
273 const GrIRect& resultBounds) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000274 SkStroke stroke;
275 stroke.setDoFill(true);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000276
277 SkAutoTUnref<GrTexture> texture(
278 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000279 stroke,
280 resultBounds,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000281 doAA, NULL));
282 if (NULL == texture) {
283 return false;
284 }
285
286 // The ClipMaskManager accumulates the clip mask in the UL corner
287 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
288
289 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
290
sugoi@google.com12b4e272012-12-06 20:13:11 +0000291 GrAssert(!path.isInverseFillType());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000292 return true;
293}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000294}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000295
296////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000297bool GrClipMaskManager::drawClipShape(GrTexture* target, const SkClipStack::Element* element) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000298 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000299 GrAssert(NULL != drawState);
300
301 drawState->setRenderTarget(target->asRenderTarget());
302
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000303 switch (element->getType()) {
304 case Element::kRect_Type:
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000305 // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the
306 // entire mask bounds and writes 0 outside the rect.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000307 if (element->isAA()) {
308 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu, element->getRect(), true);
309 } else {
310 fGpu->drawSimpleRect(element->getRect(), NULL);
311 }
312 return true;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313 case Element::kPath_Type: {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000314 SkTCopyOnFirstWrite<SkPath> path(element->getPath());
315 if (path->isInverseFillType()) {
316 path.writable()->toggleInverseFillType();
317 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000318 SkStroke stroke;
319 stroke.setDoFill(true);
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000320 GrPathRendererChain::DrawType type = element->isAA() ?
321 GrPathRendererChain::kColorAntiAlias_DrawType :
322 GrPathRendererChain::kColor_DrawType;
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000323 GrPathRenderer* pr = this->getContext()->getPathRenderer(*path,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000324 stroke,
325 fGpu,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000326 false,
327 type);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000328 if (NULL == pr) {
329 return false;
330 }
331 pr->drawPath(element->getPath(), stroke, fGpu, element->isAA());
332 break;
333 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000334 default:
335 // something is wrong if we're trying to draw an empty element.
336 GrCrash("Unexpected element type");
337 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000338 }
339 return true;
340}
341
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000342void GrClipMaskManager::mergeMask(GrTexture* dstMask,
343 GrTexture* srcMask,
344 SkRegion::Op op,
345 const GrIRect& dstBound,
346 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000347 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000349 SkMatrix oldMatrix = drawState->getViewMatrix();
350 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000351
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000352 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000353
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000354 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000355
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000356 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000357 sampleM.setIDiv(srcMask->width(), srcMask->height());
358 drawState->stage(0)->setEffect(
359 GrTextureDomainEffect::Create(srcMask,
360 sampleM,
361 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
362 GrTextureDomainEffect::kDecal_WrapMode))->unref();
363 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000364
tomhudson@google.com676e6602012-07-10 17:21:48 +0000365 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000366 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000367}
368
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000369// get a texture to act as a temporary buffer for AA clip boolean operations
370// TODO: given the expense of createTexture we may want to just cache this too
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000371void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000372 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000373 // we've already allocated the temp texture
374 return;
375 }
376
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000377 GrTextureDesc desc;
378 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000379 desc.fWidth = width;
380 desc.fHeight = height;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000381 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000382
robertphillips@google.com2c756812012-05-22 20:28:23 +0000383 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000384}
385
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000386////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000387// Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload
388// or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache
389// hit)
390bool GrClipMaskManager::getMaskTexture(int32_t clipStackGenID,
391 const SkIRect& clipSpaceIBounds,
392 GrTexture** result) {
393 bool cached = fAACache.canReuse(clipStackGenID, clipSpaceIBounds);
394 if (!cached) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000395
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000396 // There isn't a suitable entry in the cache so we create a new texture to store the mask.
397 // Since we are setting up the cache we know the last lookup was a miss. Free up the
398 // currently cached mask so it can be reused.
399 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000400
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000401 GrTextureDesc desc;
402 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
403 desc.fWidth = clipSpaceIBounds.width();
404 desc.fHeight = clipSpaceIBounds.height();
405 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000406
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000407 fAACache.acquireMask(clipStackGenID, desc, clipSpaceIBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000408 }
409
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000410 *result = fAACache.getLastMask();
411 return cached;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000412}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000414////////////////////////////////////////////////////////////////////////////////
415// Create a 8-bit clip mask in alpha
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000416GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t clipStackGenID,
417 InitialState initialState,
418 const ElementList& elements,
419 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000420 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
421
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000422 GrTexture* result;
423 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000424 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000425 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000426 }
427
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000428 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000429 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000430 return NULL;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431 }
432
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000433 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
434 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000435
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000436 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000438 // The top-left of the mask corresponds to the top-left corner of the bounds.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000439 SkVector clipToMaskOffset = {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 SkIntToScalar(-clipSpaceIBounds.fLeft),
441 SkIntToScalar(-clipSpaceIBounds.fTop)
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000442 };
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443 // The texture may be larger than necessary, this rect represents the part of the texture
444 // we populate with a rasterization of the clip.
445 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
446
447 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000448 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000449
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000450 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
451 // clear the part that we care about.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000452 fGpu->clear(&maskSpaceIBounds,
453 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
454 result->asRenderTarget());
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000455
robertphillips@google.comf105b102012-05-14 12:18:26 +0000456 GrAutoScratchTexture temp;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000457 // walk through each clip element and perform its set op
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000458 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000459 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000460 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000461
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000462 if (SkRegion::kReverseDifference_Op == op || SkRegion::kIntersect_Op == op ||
463 element->isInverseFilled()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000464 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000465 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000466 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 return NULL;
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000468 }
469
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000470 // 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 +0000471 // mask buffer can be substantially larger than the actually clip stack element. We
472 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000473 // the accumulator
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000474 GrIRect maskSpaceElementIBounds;
475 if (element->isInverseFilled()) {
476 maskSpaceElementIBounds = maskSpaceIBounds;
477 } else {
478 GrRect elementBounds = element->getBounds();
479 elementBounds.offset(clipToMaskOffset);
480 elementBounds.roundOut(&maskSpaceElementIBounds);
481 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000482
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000483 // determines whether we're drawing white-on-black or black-on-white
484 bool invert = element->isInverseFilled();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000485
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000486 // clear the temp target & draw into it
487 fGpu->clear(&maskSpaceElementIBounds,
488 invert ? 0xffffffff : 0x00000000,
489 temp.texture()->asRenderTarget());
490 drawState->setAlpha(invert ? 0x00 : 0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000491 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000492
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000493 if (!this->drawClipShape(temp.texture(), element)) {
494 fAACache.reset();
495 return NULL;
496 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000498 // Now draw into the accumulator using the real operation and the temp buffer as a
499 // texture
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000500 this->mergeMask(result, temp.texture(), op, maskSpaceIBounds, maskSpaceElementIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000501 } else {
bsalomon@google.comc6b3e482012-12-07 20:43:52 +0000502 // all the remaining ops can just be directly draw into the accumulation buffer
503 drawState->setAlpha(0xff);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000504 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000505 this->drawClipShape(result, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000506 }
507 }
508
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000509 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000510 return result;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000511}
512
513////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000514// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000515// (as opposed to canvas) coordinates
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000516bool GrClipMaskManager::createStencilClipMask(InitialState initialState,
517 const ElementList& elements,
518 const SkIRect& clipSpaceIBounds,
519 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000520
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000521 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000522
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000523 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000524 GrAssert(drawState->isClipState());
525
526 GrRenderTarget* rt = drawState->getRenderTarget();
527 GrAssert(NULL != rt);
528
529 // TODO: dynamically attach a SB when needed.
530 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
531 if (NULL == stencilBuffer) {
532 return false;
533 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000534 int32_t genID = elements.tail()->getGenID();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000535
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000536 if (stencilBuffer->mustRenderClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000537
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000538 stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000539
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000540 // we set the current clip to the bounds so that our recursive draws are scissored to them.
541 // We use the copy of the GrClipData we just stashed on the SB to render from. We set it
542 // back after we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000543 const GrClipData* oldClipData = fGpu->getClip();
544
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000545 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
546 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
547
548 SkClipStack newClipStack(stencilSpaceIBounds);
549 GrClipData newClipData; // origin defaults to (0,0)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000550 newClipData.fClipStack = &newClipStack;
551
552 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000553
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000554 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
555 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000556 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000557 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000558
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000559 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
560 SkVector translate = {
561 SkIntToScalar(clipSpaceToStencilOffset.fX),
562 SkIntToScalar(clipSpaceToStencilOffset.fY)
563 };
564 drawState->viewMatrix()->setTranslate(translate);
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000565
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000566#if !VISUALIZE_COMPLEX_CLIP
567 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
568#endif
569
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000570 int clipBit = stencilBuffer->bits();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000571 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000572 clipBit = (1 << (clipBit-1));
573
robertphillips@google.com7b112892012-07-31 15:18:21 +0000574 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000575
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000576 fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000577
578 // walk through each clip element and perform its set op
579 // with the existing clip.
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000580 for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) {
581 const Element* element = iter.get();
sugoi@google.com12b4e272012-12-06 20:13:11 +0000582 SkPath::FillType fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000583 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000584 // enabled at bottom of loop
585 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000586 // if the target is MSAA then we want MSAA enabled when the clip is soft
587 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000588 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000589 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000590
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000591 // This will be used to determine whether the clip shape can be rendered into the
592 // stencil with arbitrary stencil settings.
593 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000594
sugoi@google.com12b4e272012-12-06 20:13:11 +0000595 SkStroke stroke;
596 stroke.setDoFill(true);
597
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000598 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000599
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000600 GrPathRenderer* pr = NULL;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000601 SkTCopyOnFirstWrite<SkPath> clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000602 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000603 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000604 fill = SkPath::kEvenOdd_FillType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000605 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000606 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000607 GrAssert(Element::kPath_Type == element->getType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000608 clipPath.init(element->getPath());
609 fill = clipPath->getFillType();
610 fillInverted = clipPath->isInverseFillType();
611 if (fillInverted) {
612 clipPath.writable()->toggleInverseFillType();
613 fill = clipPath->getFillType();
614 }
615 pr = this->getContext()->getPathRenderer(*clipPath,
616 stroke,
617 fGpu,
618 false,
619 GrPathRendererChain::kStencilOnly_DrawType,
620 &stencilSupport);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000621 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000622 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000623 return false;
624 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000625 }
626
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000627 int passes;
628 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
629
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000630 bool canRenderDirectToStencil =
631 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000632 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000633 // fill rule, and set operation can
634 // we render the element directly to
635 // stencil bit used for clipping.
636 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
637 canRenderDirectToStencil,
638 clipBit,
639 fillInverted,
640 &passes,
641 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000642
643 // draw the element to the client stencil bits if necessary
644 if (!canDrawDirectToClip) {
645 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000646 kIncClamp_StencilOp,
647 kIncClamp_StencilOp,
648 kAlways_StencilFunc,
649 0xffff,
650 0x0000,
651 0xffff);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000652 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000653 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000654 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000655 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000656 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000657 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000658 if (canRenderDirectToStencil) {
659 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000660 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000661 } else {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000662 pr->stencilPath(*clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663 }
664 }
665 }
666
667 // now we modify the clip bit by rendering either the clip
668 // element directly or a bounding rect of the entire clip.
669 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
670 for (int p = 0; p < passes; ++p) {
671 *drawState->stencil() = stencilSettings[p];
672 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000673 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000674 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000675 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000677 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000678 SET_RANDOM_COLOR
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000679 pr->drawPath(*clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000680 }
681 } else {
682 SET_RANDOM_COLOR
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000683 // The view matrix is setup to do clip space -> stencil space translation, so
684 // draw rect in clip space.
685 fGpu->drawSimpleRect(SkRect::MakeFromIRect(clipSpaceIBounds), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000686 }
687 }
688 }
689 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000690 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000692 // set this last because recursive draws may overwrite it back to kNone.
693 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
694 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000695 return true;
696}
697
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000698
bsalomon@google.com411dad02012-06-05 20:24:20 +0000699// mapping of clip-respecting stencil funcs to normal stencil funcs
700// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000701static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000702 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
703 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
704 // In the Clip Funcs
705 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
706 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
707 kLess_StencilFunc, // kLessIfInClip_StencilFunc
708 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
709 // Special in the clip func that forces user's ref to be 0.
710 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
711 // make ref 0 and do normal nequal.
712 },
713 {// Stencil-Clipping is ENABLED
714 // In the Clip Funcs
715 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
716 // eq stencil clip bit, mask
717 // out user bits.
718
719 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
720 // add stencil bit to mask and ref
721
722 kLess_StencilFunc, // kLessIfInClip_StencilFunc
723 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
724 // for both of these we can add
725 // the clip bit to the mask and
726 // ref and compare as normal
727 // Special in the clip func that forces user's ref to be 0.
728 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
729 // make ref have only the clip bit set
730 // and make comparison be less
731 // 10..0 < 1..user_bits..
732 }
733};
734
bsalomon@google.coma3201942012-06-21 19:58:20 +0000735namespace {
736// Sets the settings to clip against the stencil buffer clip while ignoring the
737// client bits.
738const GrStencilSettings& basic_apply_stencil_clip_settings() {
739 // stencil settings to use when clip is in stencil
740 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
741 kKeep_StencilOp,
742 kKeep_StencilOp,
743 kAlwaysIfInClip_StencilFunc,
744 0x0000,
745 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000746 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000747 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
748}
749}
750
751void GrClipMaskManager::setGpuStencil() {
752 // We make two copies of the StencilSettings here (except in the early
753 // exit scenario. One copy from draw state to the stack var. Then another
754 // from the stack var to the gpu. We could make this class hold a ptr to
755 // GrGpu's fStencilSettings and eliminate the stack copy here.
756
757 const GrDrawState& drawState = fGpu->getDrawState();
758
759 // use stencil for clipping if clipping is enabled and the clip
760 // has been written into the stencil.
761 GrClipMaskManager::StencilClipMode clipMode;
762 if (this->isClipInStencil() && drawState.isClipState()) {
763 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
764 // We can't be modifying the clip and respecting it at the same time.
765 GrAssert(!drawState.isStateFlagEnabled(
766 GrGpu::kModifyStencilClip_StateBit));
767 } else if (drawState.isStateFlagEnabled(
768 GrGpu::kModifyStencilClip_StateBit)) {
769 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
770 } else {
771 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
772 }
773
774 GrStencilSettings settings;
775 // The GrGpu client may not be using the stencil buffer but we may need to
776 // enable it in order to respect a stencil clip.
777 if (drawState.getStencil().isDisabled()) {
778 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
779 settings = basic_apply_stencil_clip_settings();
780 } else {
781 fGpu->disableStencil();
782 return;
783 }
784 } else {
785 settings = drawState.getStencil();
786 }
787
788 // TODO: dynamically attach a stencil buffer
789 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000790 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000791 drawState.getRenderTarget()->getStencilBuffer();
792 if (NULL != stencilBuffer) {
793 stencilBits = stencilBuffer->bits();
794 }
795
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000796 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() || !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000797 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000798 this->adjustStencilParams(&settings, clipMode, stencilBits);
799 fGpu->setStencilSettings(settings);
800}
801
802void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
803 StencilClipMode mode,
804 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000805 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000806
807 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000808 // We assume that this clip manager itself is drawing to the GrGpu and
809 // has already setup the correct values.
810 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000811 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000812
bsalomon@google.com411dad02012-06-05 20:24:20 +0000813 unsigned int clipBit = (1 << (stencilBitCnt - 1));
814 unsigned int userBits = clipBit - 1;
815
bsalomon@google.coma3201942012-06-21 19:58:20 +0000816 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000817 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000818
bsalomon@google.coma3201942012-06-21 19:58:20 +0000819 bool finished = false;
820 while (!finished) {
821 GrStencilFunc func = settings->func(face);
822 uint16_t writeMask = settings->writeMask(face);
823 uint16_t funcMask = settings->funcMask(face);
824 uint16_t funcRef = settings->funcRef(face);
825
826 GrAssert((unsigned) func < kStencilFuncCount);
827
828 writeMask &= userBits;
829
830 if (func >= kBasicStencilFuncCount) {
831 int respectClip = kRespectClip_StencilClipMode == mode;
832 if (respectClip) {
833 // The GrGpu class should have checked this
834 GrAssert(this->isClipInStencil());
835 switch (func) {
836 case kAlwaysIfInClip_StencilFunc:
837 funcMask = clipBit;
838 funcRef = clipBit;
839 break;
840 case kEqualIfInClip_StencilFunc:
841 case kLessIfInClip_StencilFunc:
842 case kLEqualIfInClip_StencilFunc:
843 funcMask = (funcMask & userBits) | clipBit;
844 funcRef = (funcRef & userBits) | clipBit;
845 break;
846 case kNonZeroIfInClip_StencilFunc:
847 funcMask = (funcMask & userBits) | clipBit;
848 funcRef = clipBit;
849 break;
850 default:
851 GrCrash("Unknown stencil func");
852 }
853 } else {
854 funcMask &= userBits;
855 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000856 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000857 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000858 gSpecialToBasicStencilFunc[respectClip];
859 func = table[func - kBasicStencilFuncCount];
860 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000861 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000862 funcMask &= userBits;
863 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000864 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000865
866 settings->setFunc(face, func);
867 settings->setWriteMask(face, writeMask);
868 settings->setFuncMask(face, funcMask);
869 settings->setFuncRef(face, funcRef);
870
871 if (GrStencilSettings::kFront_Face == face) {
872 face = GrStencilSettings::kBack_Face;
873 finished = !twoSided;
874 } else {
875 finished = true;
876 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000877 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000878 if (!twoSided) {
879 settings->copyFrontSettingsToBack();
880 }
bsalomon@google.com411dad02012-06-05 20:24:20 +0000881}
882
883////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000884GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t clipStackGenID,
885 GrReducedClip::InitialState initialState,
886 const GrReducedClip::ElementList& elements,
887 const SkIRect& clipSpaceIBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000888 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000889
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000890 GrTexture* result;
891 if (this->getMaskTexture(clipStackGenID, clipSpaceIBounds, &result)) {
892 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000893 }
894
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000895 if (NULL == result) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000896 fAACache.reset();
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000897 return NULL;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000898 }
899
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000900 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
901 // the top left corner of the resulting rect to the top left of the texture.
902 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
903
robertphillips@google.com2c756812012-05-22 20:28:23 +0000904 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000905
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000906 SkMatrix matrix;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000907 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
908 SkIntToScalar(-clipSpaceIBounds.fTop));
909 helper.init(maskSpaceIBounds, &matrix);
910
911 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000912
sugoi@google.com12b4e272012-12-06 20:13:11 +0000913 SkStroke stroke;
914 stroke.setDoFill(true);
915
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000916 for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000917
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000918 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000919 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000920
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000921 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
922 // Intersect and reverse difference require modifying pixels outside of the geometry
923 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
924 // but leave the pixels inside the geometry alone. For reverse difference we invert all
925 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000926 if (SkRegion::kReverseDifference_Op == op) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000927 SkRect temp = SkRect::MakeFromIRect(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000928 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000929 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000930 }
931
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000932 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000933 // convert the rect to a path so we can invert the fill
934 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000935 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000936 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000937
sugoi@google.com12b4e272012-12-06 20:13:11 +0000938 helper.draw(temp, stroke, SkRegion::kReplace_Op,
939 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000940 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000941 } else {
942 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000943 SkPath clipPath = element->getPath();
944 clipPath.toggleInverseFillType();
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000945 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000946 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000947 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000948 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000949 }
950
951 continue;
952 }
953
954 // The other ops (union, xor, diff) only affect pixels inside
955 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000956 if (Element::kRect_Type == element->getType()) {
957 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
958 } else {
959 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000960 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000961 }
962 }
963
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000964 helper.toTexture(result, kAllIn_InitialState == initialState ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000965
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000966 fCurrClipMaskType = kAlpha_ClipMaskType;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000967 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000968}
969
robertphillips@google.comf294b772012-04-27 14:29:26 +0000970////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +0000971void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000972 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000973}