blob: 893bcb18ef13a2ab19de44c59da3ff58f074909b [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"
robertphillips@google.comfa662942012-05-17 12:20:22 +000017#include "GrAAConvexPathRenderer.h"
18#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000020#include "GrCacheID.h"
21
22GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000023
robertphillips@google.comba998f22012-10-12 11:33:56 +000024#define GR_AA_CLIP 1
25#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000026
robertphillips@google.comf294b772012-04-27 14:29:26 +000027////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000028namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000030// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031void setup_drawstate_aaclip(GrGpu* gpu,
32 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000033 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000034 GrDrawState* drawState = gpu->drawState();
35 GrAssert(drawState);
36
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000037 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000038
bsalomon@google.comb9086a02012-11-01 18:02:54 +000039 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000041 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000042 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043 mat.preConcat(drawState->getViewMatrix());
44
bsalomon@google.com08283af2012-10-26 13:01:20 +000045 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000046
47 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
48 drawState->stage(kMaskStage)->setEffect(
49 GrTextureDomainEffect::Create(result,
50 mat,
51 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
52 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000053}
54
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000055bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000056 GrGpu* gpu,
57 const SkPath& path,
58 GrPathFill fill,
59 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000060 // last (false) parameter disallows use of the SW path renderer
61 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
62}
63
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000064GrPathFill get_path_fill(const SkPath& path) {
65 switch (path.getFillType()) {
66 case SkPath::kWinding_FillType:
67 return kWinding_GrPathFill;
68 case SkPath::kEvenOdd_FillType:
69 return kEvenOdd_GrPathFill;
70 case SkPath::kInverseWinding_FillType:
71 return kInverseWinding_GrPathFill;
72 case SkPath::kInverseEvenOdd_FillType:
73 return kInverseEvenOdd_GrPathFill;
74 default:
75 GrCrash("Unsupported path fill in clip.");
76 return kWinding_GrPathFill; // suppress warning
77 }
78}
79
robertphillips@google.comb99225c2012-07-24 18:20:10 +000080/**
81 * Does any individual clip in 'clipIn' use anti-aliasing?
82 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000083bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000084
robertphillips@google.com641f8b12012-07-31 19:15:58 +000085 SkClipStack::Iter iter;
86 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000087
robertphillips@google.com641f8b12012-07-31 19:15:58 +000088 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comb99225c2012-07-24 18:20:10 +000089 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
90 NULL != clip;
bsalomon@google.come8ca6c62012-11-07 21:19:10 +000091 clip = iter.nextCombined()) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000092
93 if (clip->fDoAA) {
94 return true;
95 }
96 }
97
98 return false;
99}
100
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000101}
102
robertphillips@google.comfa662942012-05-17 12:20:22 +0000103/*
104 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
105 * will be used on any element. If so, it returns true to indicate that the
106 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
107 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000108bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000109
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000110 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000111 // a clip gets complex enough it can just be done in SW regardless
112 // of whether it would invoke the GrSoftwarePathRenderer.
113 bool useSW = false;
114
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000115 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
116 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000117
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000118 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
119 NULL != clip;
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000120 clip = iter.nextCombined()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000121
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000122 // rects can always be drawn directly w/o using the software path
123 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000124 if (NULL != clip->fPath &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000125 path_needs_SW_renderer(this->getContext(), fGpu,
126 *clip->fPath,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000127 get_path_fill(*clip->fPath),
128 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000129 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000130 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000131 }
132
133 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000134}
135
robertphillips@google.comf294b772012-04-27 14:29:26 +0000136////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000137// sort out what kind of clip mask needs to be created: alpha, stencil,
138// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000139bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000140 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000141
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000142 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000143 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000144 fGpu->disableScissor();
145 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000146 return true;
147 }
148
149 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000150 // GrDrawTarget should have filtered this for us
151 GrAssert(NULL != rt);
152
robertphillips@google.com7b112892012-07-31 15:18:21 +0000153 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000154 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000155
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000156 clipDataIn->getConservativeBounds(rt, &devClipBounds, &isIntersectionOfRects);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000157 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000158 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000159 }
160
bsalomon@google.com100abf42012-09-05 17:40:04 +0000161#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000162 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000163
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000164 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000165 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000166 // in software (this will only happen if the clip mask is anti-aliased
167 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000168 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000169 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000170 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000171 // The clip geometry is complex enough that it will be more
172 // efficient to create it entirely in software
173 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000174 GrIRect devBound;
175 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
176 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000177 fGpu->disableScissor();
178 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000179 return true;
180 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000181
182 // if SW clip mask creation fails fall through to the other
183 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000184 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000185#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000186
robertphillips@google.comf294b772012-04-27 14:29:26 +0000187#if GR_AA_CLIP
188 // If MSAA is enabled use the (faster) stencil path for AA clipping
189 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000190 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000191 // Since we are going to create a destination texture of the correct
192 // size for the mask (rather than being bound by the size of the
193 // render target) we aren't going to use scissoring like the stencil
194 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000195 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000196 GrIRect devBound;
197 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
198 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000199 fGpu->disableScissor();
200 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000201 return true;
202 }
203
204 // if alpha clip mask creation fails fall through to the stencil
205 // buffer method
206 }
207#endif // GR_AA_CLIP
208
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000209 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000210 // an antialiased clip couldn't be created. In either case, free up
211 // the texture in the antialiased mask cache.
212 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000213 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000214 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000215 // AA cache.
216 fAACache.reset();
217
bsalomon@google.coma3201942012-06-21 19:58:20 +0000218 // If the clip is a rectangle then just set the scissor. Otherwise, create
219 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000220 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000221 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000222 this->setGpuStencil();
223 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000224 }
225
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000226 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000227 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000228 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000229
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000230 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000231 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000232 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000233 // This must occur after createStencilClipMask. That function may change
234 // the scissor. Also, it only guarantees that the stencil mask is correct
235 // within the bounds it was passed, so we must use both stencil and scissor
236 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000237 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000238 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000239 return true;
240}
241
242#define VISUALIZE_COMPLEX_CLIP 0
243
244#if VISUALIZE_COMPLEX_CLIP
245 #include "GrRandom.h"
246 GrRandom gRandom;
247 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
248#else
249 #define SET_RANDOM_COLOR
250#endif
251
252namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000253/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000254 * Does "canvContainer" contain "devContainee"? If either is empty then
255 * no containment is possible. "canvContainer" is in canvas coordinates while
256 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000257 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000258 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000259bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000260 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000261 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000262 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000263 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000264 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000265 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000266 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000267}
268
robertphillips@google.comf294b772012-04-27 14:29:26 +0000269////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000270// determines how many elements at the head of the clip can be skipped and
271// whether the initial clear should be to the inside- or outside-the-clip value,
272// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000273const SkClipStack::Iter::Clip* process_initial_clip_elements(
274 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000275 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000276 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000277 SkRegion::Op* firstOp,
278 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000279
280 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000281
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000282 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000283 // processed the clip is entirely open. However, depending on the
284 // first set op we may prefer to clear to 0 for performance. We may
285 // also be able to skip the initial clip paths/rects. We loop until
286 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000287 bool done = false;
288 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000290 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000291
292 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
293 NULL != clip && !done;
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000294 clip = iter->nextCombined()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000295 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000296 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000297 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000298 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000299 *clearToInside = false;
300 done = true;
301 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000302 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000303 // if this element contains the entire bounds then we
304 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000305 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000306 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000307 break;
308 }
309 // if everything is initially clearToInside then intersect is
310 // same as clear to 0 and treat as a replace. Otherwise,
311 // set stays empty.
312 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000313 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000314 *clearToInside = false;
315 done = true;
316 }
317 break;
318 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000319 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000321 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000322 // clearToInside
323 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000324 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000325 done = true;
326 }
327 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000328 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000329 // xor is same as difference or replace both of which
330 // can be 1-pass instead of 2 for xor.
331 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000332 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000333 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000334 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 }
336 done = true;
337 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000338 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 // if all pixels are clearToInside then we have to process the
340 // difference, otherwise it has no effect and all pixels
341 // remain outside.
342 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000343 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000344 done = true;
345 }
346 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000347 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000348 // if all pixels are clearToInside then reverse difference
349 // produces empty set. Otherise it is same as replace
350 if (*clearToInside) {
351 *clearToInside = false;
352 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000353 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000354 done = true;
355 }
356 break;
357 default:
358 GrCrash("Unknown set op.");
359 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000360
361 if (done) {
362 // we need to break out here (rather than letting the test in
363 // the loop do it) since backing up the iterator is very expensive
364 break;
365 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000366 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000367 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000368}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000370}
371
robertphillips@google.comf294b772012-04-27 14:29:26 +0000372namespace {
373
374////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000375// set up the OpenGL blend function to perform the specified
376// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000377void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000378
379 switch (op) {
380 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000381 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382 break;
383 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000384 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385 break;
386 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000387 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388 break;
389 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000390 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391 break;
392 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000393 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000394 break;
395 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000396 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397 break;
398 default:
399 GrAssert(false);
400 break;
401 }
402}
403
robertphillips@google.comf294b772012-04-27 14:29:26 +0000404////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000405bool draw_path_in_software(GrContext* context,
406 GrGpu* gpu,
407 const SkPath& path,
408 GrPathFill fill,
409 bool doAA,
410 const GrIRect& resultBounds) {
411
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000412 SkAutoTUnref<GrTexture> texture(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000413 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
414 resultBounds, fill,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000415 doAA, NULL));
416 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000417 return false;
418 }
419
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000420 // The ClipMaskManager accumulates the clip mask in the UL corner
421 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000422
bsalomon@google.come3d32162012-07-20 13:37:06 +0000423 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000424
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000425 GrAssert(!GrIsFillInverted(fill));
426 return true;
427}
428
429
430////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000431bool draw_path(GrContext* context,
432 GrGpu* gpu,
433 const SkPath& path,
434 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000435 bool doAA,
436 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000438 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000439 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000440 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000441 }
442
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000443 pr->drawPath(path, fill, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000444 return true;
445}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000446
robertphillips@google.com7b112892012-07-31 15:18:21 +0000447// 'rect' enters in device coordinates and leaves in canvas coordinates
448void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
449 GrAssert(NULL != rect);
450
451 rect->fLeft += SkIntToScalar(origin.fX);
452 rect->fTop += SkIntToScalar(origin.fY);
453 rect->fRight += SkIntToScalar(origin.fX);
454 rect->fBottom += SkIntToScalar(origin.fY);
455}
456
robertphillips@google.com72176b22012-05-23 13:19:12 +0000457}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000458
459////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000460bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000461 const SkClipStack::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000462 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000463 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464 GrAssert(NULL != drawState);
465
466 drawState->setRenderTarget(target->asRenderTarget());
467
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000468 if (NULL != clip->fRect) {
469 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000470 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000471 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000472 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000473 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000474 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000475 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000476 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000477 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000478 *clip->fPath,
479 get_path_fill(*clip->fPath),
480 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000481 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000482 }
483 return true;
484}
485
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000486void GrClipMaskManager::mergeMask(GrTexture* dstMask,
487 GrTexture* srcMask,
488 SkRegion::Op op,
489 const GrIRect& dstBound,
490 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000491 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000492 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000493 SkMatrix oldMatrix = drawState->getViewMatrix();
494 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000496 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000498 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000499
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000500 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000501 sampleM.setIDiv(srcMask->width(), srcMask->height());
502 drawState->stage(0)->setEffect(
503 GrTextureDomainEffect::Create(srcMask,
504 sampleM,
505 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
506 GrTextureDomainEffect::kDecal_WrapMode))->unref();
507 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000508
tomhudson@google.com676e6602012-07-10 17:21:48 +0000509 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000510 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000511}
512
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000513// get a texture to act as a temporary buffer for AA clip boolean operations
514// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000515void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000516 GrAutoScratchTexture* temp) {
517 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000518 // we've already allocated the temp texture
519 return;
520 }
521
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000522 GrTextureDesc desc;
523 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
524 desc.fWidth = bounds.width();
525 desc.fHeight = bounds.height();
526 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000527
robertphillips@google.com2c756812012-05-22 20:28:23 +0000528 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000529}
530
robertphillips@google.comf105b102012-05-14 12:18:26 +0000531
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000532void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000533 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000534 // Since we are setting up the cache we know the last lookup was a miss
535 // Free up the currently cached mask so it can be reused
536 fAACache.reset();
537
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000538 GrTextureDesc desc;
539 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
540 desc.fWidth = bounds.width();
541 desc.fHeight = bounds.height();
542 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000543
544 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000545}
546
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000547////////////////////////////////////////////////////////////////////////////////
548// Shared preamble between gpu and SW-only AA clip mask creation paths.
549// Handles caching, determination of clip mask bound & allocation (if needed)
550// of the result texture
551// Returns true if there is no more work to be done (i.e., we got a cache hit)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000552bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000553 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000554 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000555 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000556 GrAssert(origDrawState->isClipState());
557
558 GrRenderTarget* rt = origDrawState->getRenderTarget();
559 GrAssert(NULL != rt);
560
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561 // unlike the stencil path the alpha path is not bound to the size of the
562 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000563 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000564 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000565
566 // need to outset a pixel since the standard bounding box computation
567 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000568 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000569
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000570 // TODO: make sure we don't outset if bounds are still 0,0 @ min
571
robertphillips@google.comba998f22012-10-12 11:33:56 +0000572 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000573 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000574 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000575 return true;
576 }
577
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000578 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000579 return false;
580}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000581
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000582////////////////////////////////////////////////////////////////////////////////
583// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000584bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000585 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000586 GrIRect *devResultBounds) {
587 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000588 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
589
robertphillips@google.com7b112892012-07-31 15:18:21 +0000590 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000591 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000592 return true;
593 }
594
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000595 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
596
robertphillips@google.comf105b102012-05-14 12:18:26 +0000597 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000598 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000599 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000600 return false;
601 }
602
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000603 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
604 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000606 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000607
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000608 // The mask we generate is translated so that its upper-left corner is at devResultBounds
609 // upper-left corner in device space.
610 GrIRect maskResultBounds = GrIRect::MakeWH(devResultBounds->width(), devResultBounds->height());
611
612 // Set the matrix so that rendered clip elements are transformed from the space of the clip
613 // stack to the alpha-mask. This accounts for both translation due to the clip-origin and the
614 // placement of the mask within the device.
615 SkVector clipToMaskOffset = {
616 SkIntToScalar(-devResultBounds->fLeft - clipDataIn.fOrigin.fX),
617 SkIntToScalar(-devResultBounds->fTop - clipDataIn.fOrigin.fY)
618 };
619 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620
621 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000622 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
623
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000624 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000625 SkClipStack::Iter::kBottom_IterStart);
626 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000627 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000628 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000629 &firstOp,
630 clipDataIn);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000631 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
632 // clear the part that we care about.
633 fGpu->clear(&maskResultBounds,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000634 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000635 accum->asRenderTarget());
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000636 bool accumClearedToZero = !clearToInside;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000637
robertphillips@google.comf105b102012-05-14 12:18:26 +0000638 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000639 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640 // walk through each clip element and perform its set op
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000641 for ( ; NULL != clip; clip = iter.nextCombined()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000642
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000643 SkRegion::Op op = clip->fOp;
644 if (first) {
645 first = false;
646 op = firstOp;
647 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648
649 if (SkRegion::kReplace_Op == op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000650 // clear the accumulator and draw the new object directly into it
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000651 if (!accumClearedToZero) {
652 fGpu->clear(&maskResultBounds, 0x00000000, accum->asRenderTarget());
653 }
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000654
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000655 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000656 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000657
658 } else if (SkRegion::kReverseDifference_Op == op ||
659 SkRegion::kIntersect_Op == op) {
660 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000661 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000662 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000663 continue;
664 }
665
robertphillips@google.com7b112892012-07-31 15:18:21 +0000666 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000667 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000668 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000669 return false;
670 }
671
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000672 // this is the bounds of the clip element in the space of the alpha-mask. The temporary
673 // mask buffer can be substantially larger than the actually clip stack element. We
674 // touch the minimum number of pixels necessary and use decal mode to combine it with
675 // the accumulator
676 GrRect elementMaskBounds = clip->getBounds();
677 elementMaskBounds.offset(clipToMaskOffset);
678 GrIRect elementMaskIBounds;
679 elementMaskBounds.roundOut(&elementMaskIBounds);
680
robertphillips@google.comf294b772012-04-27 14:29:26 +0000681 // clear the temp target & draw into it
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000682 fGpu->clear(&elementMaskIBounds, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000683
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000684 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000685 this->drawClipShape(temp.texture(), clip, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000686
687 // Now draw into the accumulator using the real operation
688 // and the temp buffer as a texture
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000689 this->mergeMask(accum, temp.texture(), op, maskResultBounds, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000690 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000691 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000692 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000693 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000694 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000695 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000696 accumClearedToZero = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000697 }
698
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000699 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000700 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000701 return true;
702}
703
704////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000705// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000706// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000707bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000708 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000709
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000710 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000712 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713 GrAssert(drawState->isClipState());
714
715 GrRenderTarget* rt = drawState->getRenderTarget();
716 GrAssert(NULL != rt);
717
718 // TODO: dynamically attach a SB when needed.
719 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
720 if (NULL == stencilBuffer) {
721 return false;
722 }
723
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000724 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000726 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727
728 // we set the current clip to the bounds so that our recursive
729 // draws are scissored to them. We use the copy of the complex clip
730 // we just stashed on the SB to render from. We set it back after
731 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000732 const GrClipData* oldClipData = fGpu->getClip();
733
robertphillips@google.com7b112892012-07-31 15:18:21 +0000734 // The origin of 'newClipData' is (0, 0) so it is okay to place
735 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000736 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000737 GrClipData newClipData;
738 newClipData.fClipStack = &newClipStack;
739
740 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000742 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
743 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000744 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000745 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000747 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000748 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000749 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000750 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000751 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000752 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000753 }
754
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755#if !VISUALIZE_COMPLEX_CLIP
756 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
757#endif
758
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759 int clipBit = stencilBuffer->bits();
760 SkASSERT((clipBit <= 16) &&
761 "Ganesh only handles 16b or smaller stencil buffers");
762 clipBit = (1 << (clipBit-1));
763
robertphillips@google.com7b112892012-07-31 15:18:21 +0000764 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765
766 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000767 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
768
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000769 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000770 SkClipStack::Iter::kBottom_IterStart);
771 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000772 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000773 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000774 &firstOp,
775 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776
robertphillips@google.com7b112892012-07-31 15:18:21 +0000777 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000778 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000779
780 // walk through each clip element and perform its set op
781 // with the existing clip.
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000782 for ( ; NULL != clip; clip = iter.nextCombined()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000783 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000784 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785 // enabled at bottom of loop
786 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000787 // if the target is MSAA then we want MSAA enabled when the clip is soft
788 if (rt->isMultisampled()) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000789 drawState->setState(GrDrawState::kHWAntialias_StateBit, clip->fDoAA);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000790 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791
tomhudson@google.com8afae612012-08-14 15:03:35 +0000792 // Can the clip element be drawn directly to the stencil buffer
793 // with a non-inverted fill rule without extra passes to
794 // resolve in/out status?
795 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000796
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000797 SkRegion::Op op = clip->fOp;
798 if (first) {
799 first = false;
800 op = firstOp;
801 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000802
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000804 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000805 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000806 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000807 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000808 fillInverted = false;
809 // there is no point in intersecting a screen filling
810 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000811 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000812 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 continue;
814 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000815 } else {
816 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000817 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 fillInverted = GrIsFillInverted(fill);
819 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000820 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000821 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000823 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 return false;
825 }
826 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000827 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000828 }
829
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830 int passes;
831 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
832
833 bool canDrawDirectToClip; // Given the renderer, the element,
834 // fill rule, and set operation can
835 // we render the element directly to
836 // stencil bit used for clipping.
837 canDrawDirectToClip =
838 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000839 canRenderDirectToStencil,
840 clipBit,
841 fillInverted,
842 &passes,
843 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844
845 // draw the element to the client stencil bits if necessary
846 if (!canDrawDirectToClip) {
847 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
848 kIncClamp_StencilOp,
849 kIncClamp_StencilOp,
850 kAlways_StencilFunc,
851 0xffff,
852 0x0000,
853 0xffff);
854 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000855 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000857 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000858 } else {
859 if (canRenderDirectToStencil) {
860 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000861 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000862 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000863 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000864 }
865 }
866 }
867
868 // now we modify the clip bit by rendering either the clip
869 // element directly or a bounding rect of the entire clip.
870 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
871 for (int p = 0; p < passes; ++p) {
872 *drawState->stencil() = stencilSettings[p];
873 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000874 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000875 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000876 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000877 } else {
878 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000879 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000880 }
881 } else {
882 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000883 // 'devClipBounds' is already in device coordinates so the
884 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000885 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000886 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000887 GrRect canvClipBounds;
888 canvClipBounds.set(devClipBounds);
889 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
890 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000891 }
892 }
893 }
894 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000895 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000896 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000897 // set this last because recursive draws may overwrite it back to kNone.
898 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
899 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000900 return true;
901}
902
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000903
bsalomon@google.com411dad02012-06-05 20:24:20 +0000904// mapping of clip-respecting stencil funcs to normal stencil funcs
905// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000906static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000907 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
908 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
909 // In the Clip Funcs
910 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
911 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
912 kLess_StencilFunc, // kLessIfInClip_StencilFunc
913 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
914 // Special in the clip func that forces user's ref to be 0.
915 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
916 // make ref 0 and do normal nequal.
917 },
918 {// Stencil-Clipping is ENABLED
919 // In the Clip Funcs
920 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
921 // eq stencil clip bit, mask
922 // out user bits.
923
924 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
925 // add stencil bit to mask and ref
926
927 kLess_StencilFunc, // kLessIfInClip_StencilFunc
928 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
929 // for both of these we can add
930 // the clip bit to the mask and
931 // ref and compare as normal
932 // Special in the clip func that forces user's ref to be 0.
933 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
934 // make ref have only the clip bit set
935 // and make comparison be less
936 // 10..0 < 1..user_bits..
937 }
938};
939
bsalomon@google.coma3201942012-06-21 19:58:20 +0000940namespace {
941// Sets the settings to clip against the stencil buffer clip while ignoring the
942// client bits.
943const GrStencilSettings& basic_apply_stencil_clip_settings() {
944 // stencil settings to use when clip is in stencil
945 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
946 kKeep_StencilOp,
947 kKeep_StencilOp,
948 kAlwaysIfInClip_StencilFunc,
949 0x0000,
950 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000951 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000952 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
953}
954}
955
956void GrClipMaskManager::setGpuStencil() {
957 // We make two copies of the StencilSettings here (except in the early
958 // exit scenario. One copy from draw state to the stack var. Then another
959 // from the stack var to the gpu. We could make this class hold a ptr to
960 // GrGpu's fStencilSettings and eliminate the stack copy here.
961
962 const GrDrawState& drawState = fGpu->getDrawState();
963
964 // use stencil for clipping if clipping is enabled and the clip
965 // has been written into the stencil.
966 GrClipMaskManager::StencilClipMode clipMode;
967 if (this->isClipInStencil() && drawState.isClipState()) {
968 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
969 // We can't be modifying the clip and respecting it at the same time.
970 GrAssert(!drawState.isStateFlagEnabled(
971 GrGpu::kModifyStencilClip_StateBit));
972 } else if (drawState.isStateFlagEnabled(
973 GrGpu::kModifyStencilClip_StateBit)) {
974 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
975 } else {
976 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
977 }
978
979 GrStencilSettings settings;
980 // The GrGpu client may not be using the stencil buffer but we may need to
981 // enable it in order to respect a stencil clip.
982 if (drawState.getStencil().isDisabled()) {
983 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
984 settings = basic_apply_stencil_clip_settings();
985 } else {
986 fGpu->disableStencil();
987 return;
988 }
989 } else {
990 settings = drawState.getStencil();
991 }
992
993 // TODO: dynamically attach a stencil buffer
994 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000995 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000996 drawState.getRenderTarget()->getStencilBuffer();
997 if (NULL != stencilBuffer) {
998 stencilBits = stencilBuffer->bits();
999 }
1000
bsalomon@google.comf6601872012-08-28 21:11:35 +00001001 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001002 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001003 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001004 this->adjustStencilParams(&settings, clipMode, stencilBits);
1005 fGpu->setStencilSettings(settings);
1006}
1007
1008void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1009 StencilClipMode mode,
1010 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001011 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001012
1013 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001014 // We assume that this clip manager itself is drawing to the GrGpu and
1015 // has already setup the correct values.
1016 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001017 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001018
bsalomon@google.com411dad02012-06-05 20:24:20 +00001019 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1020 unsigned int userBits = clipBit - 1;
1021
bsalomon@google.coma3201942012-06-21 19:58:20 +00001022 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001023 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001024
bsalomon@google.coma3201942012-06-21 19:58:20 +00001025 bool finished = false;
1026 while (!finished) {
1027 GrStencilFunc func = settings->func(face);
1028 uint16_t writeMask = settings->writeMask(face);
1029 uint16_t funcMask = settings->funcMask(face);
1030 uint16_t funcRef = settings->funcRef(face);
1031
1032 GrAssert((unsigned) func < kStencilFuncCount);
1033
1034 writeMask &= userBits;
1035
1036 if (func >= kBasicStencilFuncCount) {
1037 int respectClip = kRespectClip_StencilClipMode == mode;
1038 if (respectClip) {
1039 // The GrGpu class should have checked this
1040 GrAssert(this->isClipInStencil());
1041 switch (func) {
1042 case kAlwaysIfInClip_StencilFunc:
1043 funcMask = clipBit;
1044 funcRef = clipBit;
1045 break;
1046 case kEqualIfInClip_StencilFunc:
1047 case kLessIfInClip_StencilFunc:
1048 case kLEqualIfInClip_StencilFunc:
1049 funcMask = (funcMask & userBits) | clipBit;
1050 funcRef = (funcRef & userBits) | clipBit;
1051 break;
1052 case kNonZeroIfInClip_StencilFunc:
1053 funcMask = (funcMask & userBits) | clipBit;
1054 funcRef = clipBit;
1055 break;
1056 default:
1057 GrCrash("Unknown stencil func");
1058 }
1059 } else {
1060 funcMask &= userBits;
1061 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001062 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001063 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001064 gSpecialToBasicStencilFunc[respectClip];
1065 func = table[func - kBasicStencilFuncCount];
1066 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001067 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001068 funcMask &= userBits;
1069 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001070 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001071
1072 settings->setFunc(face, func);
1073 settings->setWriteMask(face, writeMask);
1074 settings->setFuncMask(face, funcMask);
1075 settings->setFuncRef(face, funcRef);
1076
1077 if (GrStencilSettings::kFront_Face == face) {
1078 face = GrStencilSettings::kBack_Face;
1079 finished = !twoSided;
1080 } else {
1081 finished = true;
1082 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001083 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001084 if (!twoSided) {
1085 settings->copyFrontSettingsToBack();
1086 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001087}
1088
1089////////////////////////////////////////////////////////////////////////////////
1090
robertphillips@google.comfa662942012-05-17 12:20:22 +00001091namespace {
1092
1093GrPathFill invert_fill(GrPathFill fill) {
1094 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001095 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1096 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1097 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1098 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1099 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001100 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001101 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1102 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1103 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1104 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1105 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1106 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001107 return gInvertedFillTable[fill];
1108}
1109
1110}
1111
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001112bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001113 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001114 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001115 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001116
robertphillips@google.com7b112892012-07-31 15:18:21 +00001117 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001118 return true;
1119 }
1120
robertphillips@google.comf105b102012-05-14 12:18:26 +00001121 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001122 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001123 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001124 return false;
1125 }
1126
robertphillips@google.com2c756812012-05-22 20:28:23 +00001127 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001128
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001129 SkMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001130 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001131 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001132 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001133
robertphillips@google.comfa662942012-05-17 12:20:22 +00001134 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001135 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1136
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001137 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001138 SkClipStack::Iter::kBottom_IterStart);
1139 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001140 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001141 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001142 &firstOp,
1143 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001144
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001145 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001146
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001147 bool first = true;
bsalomon@google.come8ca6c62012-11-07 21:19:10 +00001148 for ( ; NULL != clip; clip = iter.nextCombined()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001149
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001150 SkRegion::Op op = clip->fOp;
1151 if (first) {
1152 first = false;
1153 op = firstOp;
1154 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001155
1156 if (SkRegion::kIntersect_Op == op ||
1157 SkRegion::kReverseDifference_Op == op) {
1158 // Intersect and reverse difference require modifying pixels
1159 // outside of the geometry that is being "drawn". In both cases
1160 // we erase all the pixels outside of the geometry but
1161 // leave the pixels inside the geometry alone. For reverse
1162 // difference we invert all the pixels before clearing the ones
1163 // outside the geometry.
1164 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001165 SkRect temp;
1166 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001167 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1168 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001169
1170 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001171 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001172 }
1173
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001174 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001175
1176 // convert the rect to a path so we can invert the fill
1177 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001178 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001179
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001180 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001181 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001182 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001183 } else if (NULL != clip->fPath) {
1184 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001185 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001186 invert_fill(get_path_fill(*clip->fPath)),
1187 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001188 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001189 }
1190
1191 continue;
1192 }
1193
1194 // The other ops (union, xor, diff) only affect pixels inside
1195 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001196 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001197
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001198 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001199 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001200 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001201
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001202 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001203 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001204 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001205 get_path_fill(*clip->fPath),
1206 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001207 }
1208 }
1209
robertphillips@google.comfa662942012-05-17 12:20:22 +00001210 // Because we are using the scratch texture cache, "accum" may be
1211 // larger than expected and have some cruft in the areas we aren't using.
1212 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001213 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001214
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001215 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001216
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001217 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001218
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001219 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001220 return true;
1221}
1222
robertphillips@google.comf294b772012-04-27 14:29:26 +00001223////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001224void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001225 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001226}