blob: 606b90eeeb8f86e2b8a36a76cd155b14eafd1718 [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;
91 clip = iter.next()) {
92
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;
120 clip = iter.next()) {
121
122 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000123 // Everything before a replace op can be ignored so start
124 // afresh w.r.t. determining if any element uses the SW path
125 useSW = false;
126 }
127
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000128 // rects can always be drawn directly w/o using the software path
129 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000130 if (NULL != clip->fPath &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000131 path_needs_SW_renderer(this->getContext(), fGpu,
132 *clip->fPath,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000133 get_path_fill(*clip->fPath),
134 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000135 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000136 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000137 }
138
139 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000140}
141
robertphillips@google.comf294b772012-04-27 14:29:26 +0000142////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000143// sort out what kind of clip mask needs to be created: alpha, stencil,
144// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000145bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000146 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000147
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000148 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000149 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000150 fGpu->disableScissor();
151 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000152 return true;
153 }
154
155 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000156 // GrDrawTarget should have filtered this for us
157 GrAssert(NULL != rt);
158
robertphillips@google.com7b112892012-07-31 15:18:21 +0000159 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000160 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000161
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000162 clipDataIn->getConservativeBounds(rt, &devClipBounds, &isIntersectionOfRects);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000163 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000164 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000165 }
166
bsalomon@google.com100abf42012-09-05 17:40:04 +0000167#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000168 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000169
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000170 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000171 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000172 // in software (this will only happen if the clip mask is anti-aliased
173 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000174 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000175 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000176 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000177 // The clip geometry is complex enough that it will be more
178 // efficient to create it entirely in software
179 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000180 GrIRect devBound;
181 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
182 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000183 fGpu->disableScissor();
184 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000185 return true;
186 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000187
188 // if SW clip mask creation fails fall through to the other
189 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000190 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000191#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000192
robertphillips@google.comf294b772012-04-27 14:29:26 +0000193#if GR_AA_CLIP
194 // If MSAA is enabled use the (faster) stencil path for AA clipping
195 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000196 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000197 // Since we are going to create a destination texture of the correct
198 // size for the mask (rather than being bound by the size of the
199 // render target) we aren't going to use scissoring like the stencil
200 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000201 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000202 GrIRect devBound;
203 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
204 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000205 fGpu->disableScissor();
206 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000207 return true;
208 }
209
210 // if alpha clip mask creation fails fall through to the stencil
211 // buffer method
212 }
213#endif // GR_AA_CLIP
214
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000215 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000216 // an antialiased clip couldn't be created. In either case, free up
217 // the texture in the antialiased mask cache.
218 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000219 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000220 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000221 // AA cache.
222 fAACache.reset();
223
bsalomon@google.coma3201942012-06-21 19:58:20 +0000224 // If the clip is a rectangle then just set the scissor. Otherwise, create
225 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000226 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000227 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000228 this->setGpuStencil();
229 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000230 }
231
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000232 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000233 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000234 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000235
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000236 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000237 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000238 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000239 // This must occur after createStencilClipMask. That function may change
240 // the scissor. Also, it only guarantees that the stencil mask is correct
241 // within the bounds it was passed, so we must use both stencil and scissor
242 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000243 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000244 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000245 return true;
246}
247
248#define VISUALIZE_COMPLEX_CLIP 0
249
250#if VISUALIZE_COMPLEX_CLIP
251 #include "GrRandom.h"
252 GrRandom gRandom;
253 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
254#else
255 #define SET_RANDOM_COLOR
256#endif
257
258namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000259/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000260 * Does "canvContainer" contain "devContainee"? If either is empty then
261 * no containment is possible. "canvContainer" is in canvas coordinates while
262 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000263 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000264 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000265bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000266 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000267 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000268 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000269 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000270 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000271 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000272 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000273}
274
robertphillips@google.comf294b772012-04-27 14:29:26 +0000275////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000276// determines how many elements at the head of the clip can be skipped and
277// whether the initial clear should be to the inside- or outside-the-clip value,
278// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000279const SkClipStack::Iter::Clip* process_initial_clip_elements(
280 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000281 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000282 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000283 SkRegion::Op* firstOp,
284 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000285
286 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000287
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000288 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289 // processed the clip is entirely open. However, depending on the
290 // first set op we may prefer to clear to 0 for performance. We may
291 // also be able to skip the initial clip paths/rects. We loop until
292 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000293 bool done = false;
294 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000296 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000297
298 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
299 NULL != clip && !done;
300 clip = iter->next()) {
301 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000302 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000303 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000304 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000305 *clearToInside = false;
306 done = true;
307 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000308 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000309 // if this element contains the entire bounds then we
310 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000311 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000312 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000313 break;
314 }
315 // if everything is initially clearToInside then intersect is
316 // same as clear to 0 and treat as a replace. Otherwise,
317 // set stays empty.
318 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000319 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320 *clearToInside = false;
321 done = true;
322 }
323 break;
324 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000325 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000326 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000327 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000328 // clearToInside
329 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000330 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331 done = true;
332 }
333 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000334 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 // xor is same as difference or replace both of which
336 // can be 1-pass instead of 2 for xor.
337 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000338 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000340 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000341 }
342 done = true;
343 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000344 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345 // if all pixels are clearToInside then we have to process the
346 // difference, otherwise it has no effect and all pixels
347 // remain outside.
348 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000349 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000350 done = true;
351 }
352 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000353 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000354 // if all pixels are clearToInside then reverse difference
355 // produces empty set. Otherise it is same as replace
356 if (*clearToInside) {
357 *clearToInside = false;
358 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000359 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000360 done = true;
361 }
362 break;
363 default:
364 GrCrash("Unknown set op.");
365 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000366
367 if (done) {
368 // we need to break out here (rather than letting the test in
369 // the loop do it) since backing up the iterator is very expensive
370 break;
371 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000372 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000373 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000374}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000375
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000376}
377
robertphillips@google.comf294b772012-04-27 14:29:26 +0000378namespace {
379
380////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000381// set up the OpenGL blend function to perform the specified
382// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000383void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000384
385 switch (op) {
386 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000387 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388 break;
389 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000390 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391 break;
392 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000393 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000394 break;
395 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000396 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397 break;
398 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000399 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000400 break;
401 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000402 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403 break;
404 default:
405 GrAssert(false);
406 break;
407 }
408}
409
robertphillips@google.comf294b772012-04-27 14:29:26 +0000410////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000411bool draw_path_in_software(GrContext* context,
412 GrGpu* gpu,
413 const SkPath& path,
414 GrPathFill fill,
415 bool doAA,
416 const GrIRect& resultBounds) {
417
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000418 SkAutoTUnref<GrTexture> texture(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000419 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
420 resultBounds, fill,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000421 doAA, NULL));
422 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000423 return false;
424 }
425
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000426 // The ClipMaskManager accumulates the clip mask in the UL corner
427 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000428
bsalomon@google.come3d32162012-07-20 13:37:06 +0000429 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000430
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000431 GrAssert(!GrIsFillInverted(fill));
432 return true;
433}
434
435
436////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000437bool draw_path(GrContext* context,
438 GrGpu* gpu,
439 const SkPath& path,
440 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000441 bool doAA,
442 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000443
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000444 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000445 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000446 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447 }
448
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000449 pr->drawPath(path, fill, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000450 return true;
451}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000452
robertphillips@google.com7b112892012-07-31 15:18:21 +0000453// 'rect' enters in device coordinates and leaves in canvas coordinates
454void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
455 GrAssert(NULL != rect);
456
457 rect->fLeft += SkIntToScalar(origin.fX);
458 rect->fTop += SkIntToScalar(origin.fY);
459 rect->fRight += SkIntToScalar(origin.fX);
460 rect->fBottom += SkIntToScalar(origin.fY);
461}
462
robertphillips@google.com72176b22012-05-23 13:19:12 +0000463}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464
465////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000466bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000467 const SkClipStack::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000468 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000469 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000470 GrAssert(NULL != drawState);
471
472 drawState->setRenderTarget(target->asRenderTarget());
473
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000474 if (NULL != clip->fRect) {
475 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000476 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000477 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000478 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000479 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000480 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000481 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000482 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000483 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000484 *clip->fPath,
485 get_path_fill(*clip->fPath),
486 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000487 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000488 }
489 return true;
490}
491
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000492void GrClipMaskManager::mergeMask(GrTexture* dstMask,
493 GrTexture* srcMask,
494 SkRegion::Op op,
495 const GrIRect& dstBound,
496 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000497 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000498 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000499 SkMatrix oldMatrix = drawState->getViewMatrix();
500 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000501
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000502 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000503
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000504 setup_boolean_blendcoeffs(drawState, op);
505
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000506 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000507 sampleM.setIDiv(srcMask->width(), srcMask->height());
508 drawState->stage(0)->setEffect(
509 GrTextureDomainEffect::Create(srcMask,
510 sampleM,
511 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
512 GrTextureDomainEffect::kDecal_WrapMode))->unref();
513 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000514
tomhudson@google.com676e6602012-07-10 17:21:48 +0000515 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000516 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000517}
518
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000519// get a texture to act as a temporary buffer for AA clip boolean operations
520// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000521void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000522 GrAutoScratchTexture* temp) {
523 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000524 // we've already allocated the temp texture
525 return;
526 }
527
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000528 GrTextureDesc desc;
529 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
530 desc.fWidth = bounds.width();
531 desc.fHeight = bounds.height();
532 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000533
robertphillips@google.com2c756812012-05-22 20:28:23 +0000534 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000535}
536
robertphillips@google.comf105b102012-05-14 12:18:26 +0000537
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000538void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000539 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000540 // Since we are setting up the cache we know the last lookup was a miss
541 // Free up the currently cached mask so it can be reused
542 fAACache.reset();
543
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000544 GrTextureDesc desc;
545 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
546 desc.fWidth = bounds.width();
547 desc.fHeight = bounds.height();
548 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000549
550 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000551}
552
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000553////////////////////////////////////////////////////////////////////////////////
554// Shared preamble between gpu and SW-only AA clip mask creation paths.
555// Handles caching, determination of clip mask bound & allocation (if needed)
556// of the result texture
557// 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 +0000558bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000559 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000560 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000561 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562 GrAssert(origDrawState->isClipState());
563
564 GrRenderTarget* rt = origDrawState->getRenderTarget();
565 GrAssert(NULL != rt);
566
robertphillips@google.comf294b772012-04-27 14:29:26 +0000567 // unlike the stencil path the alpha path is not bound to the size of the
568 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000569 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000570 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000571
572 // need to outset a pixel since the standard bounding box computation
573 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000574 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000575
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000576 // TODO: make sure we don't outset if bounds are still 0,0 @ min
577
robertphillips@google.comba998f22012-10-12 11:33:56 +0000578 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000579 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000580 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000581 return true;
582 }
583
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000584 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000585 return false;
586}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000587
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000588////////////////////////////////////////////////////////////////////////////////
589// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000590bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000591 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000592 GrIRect *devResultBounds) {
593 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000594 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
595
robertphillips@google.com7b112892012-07-31 15:18:21 +0000596 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000597 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000598 return true;
599 }
600
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000601 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
602
robertphillips@google.comf105b102012-05-14 12:18:26 +0000603 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000604 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000605 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000606 return false;
607 }
608
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000609 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
610 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000611
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000612 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000614 // The mask we generate is translated so that its upper-left corner is at devResultBounds
615 // upper-left corner in device space.
616 GrIRect maskResultBounds = GrIRect::MakeWH(devResultBounds->width(), devResultBounds->height());
617
618 // Set the matrix so that rendered clip elements are transformed from the space of the clip
619 // stack to the alpha-mask. This accounts for both translation due to the clip-origin and the
620 // placement of the mask within the device.
621 SkVector clipToMaskOffset = {
622 SkIntToScalar(-devResultBounds->fLeft - clipDataIn.fOrigin.fX),
623 SkIntToScalar(-devResultBounds->fTop - clipDataIn.fOrigin.fY)
624 };
625 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626
627 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000628 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
629
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000630 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000631 SkClipStack::Iter::kBottom_IterStart);
632 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000633 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000634 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000635 &firstOp,
636 clipDataIn);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000637 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
638 // clear the part that we care about.
639 fGpu->clear(&maskResultBounds,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000640 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000641 accum->asRenderTarget());
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000642 bool accumClearedToZero = !clearToInside;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000643
robertphillips@google.comf105b102012-05-14 12:18:26 +0000644 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000645 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000646 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000647 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000649 SkRegion::Op op = clip->fOp;
650 if (first) {
651 first = false;
652 op = firstOp;
653 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000654
655 if (SkRegion::kReplace_Op == op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000656 // clear the accumulator and draw the new object directly into it
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000657 if (!accumClearedToZero) {
658 fGpu->clear(&maskResultBounds, 0x00000000, accum->asRenderTarget());
659 }
660
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000661 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000662 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000663
664 } else if (SkRegion::kReverseDifference_Op == op ||
665 SkRegion::kIntersect_Op == op) {
666 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000667 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000668 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000669 continue;
670 }
671
robertphillips@google.com7b112892012-07-31 15:18:21 +0000672 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000673 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000674 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000675 return false;
676 }
677
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000678 // this is the bounds of the clip element in the space of the alpha-mask. The temporary
679 // mask buffer can be substantially larger than the actually clip stack element. We
680 // touch the minimum number of pixels necessary and use decal mode to combine it with
681 // the accumulator
682 GrRect elementMaskBounds = clip->getBounds();
683 elementMaskBounds.offset(clipToMaskOffset);
684 GrIRect elementMaskIBounds;
685 elementMaskBounds.roundOut(&elementMaskIBounds);
686
robertphillips@google.comf294b772012-04-27 14:29:26 +0000687 // clear the temp target & draw into it
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000688 fGpu->clear(&elementMaskIBounds, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000690 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000691 this->drawClipShape(temp.texture(), clip, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000692
693 // Now draw into the accumulator using the real operation
694 // and the temp buffer as a texture
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000695 this->mergeMask(accum, temp.texture(), op, maskResultBounds, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000696 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000697 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000698 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000699 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000700 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000701 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000702 accumClearedToZero = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000703 }
704
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000705 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000706 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000707 return true;
708}
709
710////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000711// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000712// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000713bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000714 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000716 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000718 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719 GrAssert(drawState->isClipState());
720
721 GrRenderTarget* rt = drawState->getRenderTarget();
722 GrAssert(NULL != rt);
723
724 // TODO: dynamically attach a SB when needed.
725 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
726 if (NULL == stencilBuffer) {
727 return false;
728 }
729
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000730 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000731
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000732 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733
734 // we set the current clip to the bounds so that our recursive
735 // draws are scissored to them. We use the copy of the complex clip
736 // we just stashed on the SB to render from. We set it back after
737 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000738 const GrClipData* oldClipData = fGpu->getClip();
739
robertphillips@google.com7b112892012-07-31 15:18:21 +0000740 // The origin of 'newClipData' is (0, 0) so it is okay to place
741 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000742 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000743 GrClipData newClipData;
744 newClipData.fClipStack = &newClipStack;
745
746 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000747
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000748 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
749 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000751 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000753 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000754 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000755 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000756 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000757 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000758 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000759 }
760
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000761#if !VISUALIZE_COMPLEX_CLIP
762 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
763#endif
764
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765 int clipBit = stencilBuffer->bits();
766 SkASSERT((clipBit <= 16) &&
767 "Ganesh only handles 16b or smaller stencil buffers");
768 clipBit = (1 << (clipBit-1));
769
robertphillips@google.com7b112892012-07-31 15:18:21 +0000770 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000771
772 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000773 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
774
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000775 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000776 SkClipStack::Iter::kBottom_IterStart);
777 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000778 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000779 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000780 &firstOp,
781 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782
robertphillips@google.com7b112892012-07-31 15:18:21 +0000783 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000784 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785
786 // walk through each clip element and perform its set op
787 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000788 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000789 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000790 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791 // enabled at bottom of loop
792 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000793 // if the target is MSAA then we want MSAA enabled when the clip is soft
794 if (rt->isMultisampled()) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000795 drawState->setState(GrDrawState::kHWAntialias_StateBit, clip->fDoAA);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000796 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797
tomhudson@google.com8afae612012-08-14 15:03:35 +0000798 // Can the clip element be drawn directly to the stencil buffer
799 // with a non-inverted fill rule without extra passes to
800 // resolve in/out status?
801 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000802
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000803 SkRegion::Op op = clip->fOp;
804 if (first) {
805 first = false;
806 op = firstOp;
807 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000808
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000810 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000811 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000813 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000814 fillInverted = false;
815 // there is no point in intersecting a screen filling
816 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000817 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000818 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 continue;
820 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000821 } else {
822 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000823 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 fillInverted = GrIsFillInverted(fill);
825 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000826 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000827 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000828 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000829 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830 return false;
831 }
832 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000833 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000834 }
835
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000836 int passes;
837 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
838
839 bool canDrawDirectToClip; // Given the renderer, the element,
840 // fill rule, and set operation can
841 // we render the element directly to
842 // stencil bit used for clipping.
843 canDrawDirectToClip =
844 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000845 canRenderDirectToStencil,
846 clipBit,
847 fillInverted,
848 &passes,
849 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000850
851 // draw the element to the client stencil bits if necessary
852 if (!canDrawDirectToClip) {
853 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
854 kIncClamp_StencilOp,
855 kIncClamp_StencilOp,
856 kAlways_StencilFunc,
857 0xffff,
858 0x0000,
859 0xffff);
860 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000861 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000862 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000863 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000864 } else {
865 if (canRenderDirectToStencil) {
866 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000867 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000868 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000869 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000870 }
871 }
872 }
873
874 // now we modify the clip bit by rendering either the clip
875 // element directly or a bounding rect of the entire clip.
876 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
877 for (int p = 0; p < passes; ++p) {
878 *drawState->stencil() = stencilSettings[p];
879 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000880 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000881 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000882 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000883 } else {
884 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000885 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000886 }
887 } else {
888 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000889 // 'devClipBounds' is already in device coordinates so the
890 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000891 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000892 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000893 GrRect canvClipBounds;
894 canvClipBounds.set(devClipBounds);
895 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
896 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000897 }
898 }
899 }
900 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000901 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000902 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000903 // set this last because recursive draws may overwrite it back to kNone.
904 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
905 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000906 return true;
907}
908
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000909
bsalomon@google.com411dad02012-06-05 20:24:20 +0000910// mapping of clip-respecting stencil funcs to normal stencil funcs
911// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000912static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000913 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
914 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
915 // In the Clip Funcs
916 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
917 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
918 kLess_StencilFunc, // kLessIfInClip_StencilFunc
919 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
920 // Special in the clip func that forces user's ref to be 0.
921 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
922 // make ref 0 and do normal nequal.
923 },
924 {// Stencil-Clipping is ENABLED
925 // In the Clip Funcs
926 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
927 // eq stencil clip bit, mask
928 // out user bits.
929
930 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
931 // add stencil bit to mask and ref
932
933 kLess_StencilFunc, // kLessIfInClip_StencilFunc
934 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
935 // for both of these we can add
936 // the clip bit to the mask and
937 // ref and compare as normal
938 // Special in the clip func that forces user's ref to be 0.
939 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
940 // make ref have only the clip bit set
941 // and make comparison be less
942 // 10..0 < 1..user_bits..
943 }
944};
945
bsalomon@google.coma3201942012-06-21 19:58:20 +0000946namespace {
947// Sets the settings to clip against the stencil buffer clip while ignoring the
948// client bits.
949const GrStencilSettings& basic_apply_stencil_clip_settings() {
950 // stencil settings to use when clip is in stencil
951 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
952 kKeep_StencilOp,
953 kKeep_StencilOp,
954 kAlwaysIfInClip_StencilFunc,
955 0x0000,
956 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000957 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
959}
960}
961
962void GrClipMaskManager::setGpuStencil() {
963 // We make two copies of the StencilSettings here (except in the early
964 // exit scenario. One copy from draw state to the stack var. Then another
965 // from the stack var to the gpu. We could make this class hold a ptr to
966 // GrGpu's fStencilSettings and eliminate the stack copy here.
967
968 const GrDrawState& drawState = fGpu->getDrawState();
969
970 // use stencil for clipping if clipping is enabled and the clip
971 // has been written into the stencil.
972 GrClipMaskManager::StencilClipMode clipMode;
973 if (this->isClipInStencil() && drawState.isClipState()) {
974 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
975 // We can't be modifying the clip and respecting it at the same time.
976 GrAssert(!drawState.isStateFlagEnabled(
977 GrGpu::kModifyStencilClip_StateBit));
978 } else if (drawState.isStateFlagEnabled(
979 GrGpu::kModifyStencilClip_StateBit)) {
980 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
981 } else {
982 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
983 }
984
985 GrStencilSettings settings;
986 // The GrGpu client may not be using the stencil buffer but we may need to
987 // enable it in order to respect a stencil clip.
988 if (drawState.getStencil().isDisabled()) {
989 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
990 settings = basic_apply_stencil_clip_settings();
991 } else {
992 fGpu->disableStencil();
993 return;
994 }
995 } else {
996 settings = drawState.getStencil();
997 }
998
999 // TODO: dynamically attach a stencil buffer
1000 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001001 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001002 drawState.getRenderTarget()->getStencilBuffer();
1003 if (NULL != stencilBuffer) {
1004 stencilBits = stencilBuffer->bits();
1005 }
1006
bsalomon@google.comf6601872012-08-28 21:11:35 +00001007 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001008 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001009 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001010 this->adjustStencilParams(&settings, clipMode, stencilBits);
1011 fGpu->setStencilSettings(settings);
1012}
1013
1014void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1015 StencilClipMode mode,
1016 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001017 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001018
1019 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001020 // We assume that this clip manager itself is drawing to the GrGpu and
1021 // has already setup the correct values.
1022 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001023 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001024
bsalomon@google.com411dad02012-06-05 20:24:20 +00001025 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1026 unsigned int userBits = clipBit - 1;
1027
bsalomon@google.coma3201942012-06-21 19:58:20 +00001028 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001029 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001030
bsalomon@google.coma3201942012-06-21 19:58:20 +00001031 bool finished = false;
1032 while (!finished) {
1033 GrStencilFunc func = settings->func(face);
1034 uint16_t writeMask = settings->writeMask(face);
1035 uint16_t funcMask = settings->funcMask(face);
1036 uint16_t funcRef = settings->funcRef(face);
1037
1038 GrAssert((unsigned) func < kStencilFuncCount);
1039
1040 writeMask &= userBits;
1041
1042 if (func >= kBasicStencilFuncCount) {
1043 int respectClip = kRespectClip_StencilClipMode == mode;
1044 if (respectClip) {
1045 // The GrGpu class should have checked this
1046 GrAssert(this->isClipInStencil());
1047 switch (func) {
1048 case kAlwaysIfInClip_StencilFunc:
1049 funcMask = clipBit;
1050 funcRef = clipBit;
1051 break;
1052 case kEqualIfInClip_StencilFunc:
1053 case kLessIfInClip_StencilFunc:
1054 case kLEqualIfInClip_StencilFunc:
1055 funcMask = (funcMask & userBits) | clipBit;
1056 funcRef = (funcRef & userBits) | clipBit;
1057 break;
1058 case kNonZeroIfInClip_StencilFunc:
1059 funcMask = (funcMask & userBits) | clipBit;
1060 funcRef = clipBit;
1061 break;
1062 default:
1063 GrCrash("Unknown stencil func");
1064 }
1065 } else {
1066 funcMask &= userBits;
1067 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001068 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001069 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001070 gSpecialToBasicStencilFunc[respectClip];
1071 func = table[func - kBasicStencilFuncCount];
1072 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001073 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001074 funcMask &= userBits;
1075 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001076 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001077
1078 settings->setFunc(face, func);
1079 settings->setWriteMask(face, writeMask);
1080 settings->setFuncMask(face, funcMask);
1081 settings->setFuncRef(face, funcRef);
1082
1083 if (GrStencilSettings::kFront_Face == face) {
1084 face = GrStencilSettings::kBack_Face;
1085 finished = !twoSided;
1086 } else {
1087 finished = true;
1088 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001089 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001090 if (!twoSided) {
1091 settings->copyFrontSettingsToBack();
1092 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001093}
1094
1095////////////////////////////////////////////////////////////////////////////////
1096
robertphillips@google.comfa662942012-05-17 12:20:22 +00001097namespace {
1098
1099GrPathFill invert_fill(GrPathFill fill) {
1100 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001101 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1102 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1103 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1104 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1105 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001106 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001107 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1108 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1109 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1110 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1111 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1112 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001113 return gInvertedFillTable[fill];
1114}
1115
1116}
1117
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001118bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001119 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001120 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001121 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001122
robertphillips@google.com7b112892012-07-31 15:18:21 +00001123 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001124 return true;
1125 }
1126
robertphillips@google.comf105b102012-05-14 12:18:26 +00001127 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001128 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001129 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001130 return false;
1131 }
1132
robertphillips@google.com2c756812012-05-22 20:28:23 +00001133 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001134
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001135 SkMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001136 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001137 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001138 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001139
robertphillips@google.comfa662942012-05-17 12:20:22 +00001140 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001141 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1142
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001143 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001144 SkClipStack::Iter::kBottom_IterStart);
1145 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001146 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001147 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001148 &firstOp,
1149 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001150
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001151 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001152
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001153 bool first = true;
1154 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001155
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001156 SkRegion::Op op = clip->fOp;
1157 if (first) {
1158 first = false;
1159 op = firstOp;
1160 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001161
1162 if (SkRegion::kIntersect_Op == op ||
1163 SkRegion::kReverseDifference_Op == op) {
1164 // Intersect and reverse difference require modifying pixels
1165 // outside of the geometry that is being "drawn". In both cases
1166 // we erase all the pixels outside of the geometry but
1167 // leave the pixels inside the geometry alone. For reverse
1168 // difference we invert all the pixels before clearing the ones
1169 // outside the geometry.
1170 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001171 SkRect temp;
1172 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001173 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1174 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001175
1176 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001177 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001178 }
1179
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001180 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001181
1182 // convert the rect to a path so we can invert the fill
1183 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001184 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001185
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001186 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001187 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001188 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001189 } else if (NULL != clip->fPath) {
1190 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001191 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001192 invert_fill(get_path_fill(*clip->fPath)),
1193 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001194 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001195 }
1196
1197 continue;
1198 }
1199
1200 // The other ops (union, xor, diff) only affect pixels inside
1201 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001202 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001203
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001204 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001205 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001206 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001207
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001208 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001209 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001210 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001211 get_path_fill(*clip->fPath),
1212 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001213 }
1214 }
1215
robertphillips@google.comfa662942012-05-17 12:20:22 +00001216 // Because we are using the scratch texture cache, "accum" may be
1217 // larger than expected and have some cruft in the areas we aren't using.
1218 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001219 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001220
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001221 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001222
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001223 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001224
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001225 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001226 return true;
1227}
1228
robertphillips@google.comf294b772012-04-27 14:29:26 +00001229////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001230void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001231 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001232}