blob: d80868d31ec86fa4dc583084b35a8731779e956b [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"
10#include "GrGpu.h"
11#include "GrRenderTarget.h"
12#include "GrStencilBuffer.h"
13#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000014#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000015#include "SkRasterClip.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000016#include "GrAAConvexPathRenderer.h"
17#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000019#include "GrCacheID.h"
20
21GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000022
23//#define GR_AA_CLIP 1
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000024//#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000025
robertphillips@google.comf294b772012-04-27 14:29:26 +000026////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000027namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028// set up the draw state to enable the aa clipping mask. Besides setting up the
robertphillips@google.coma72eef32012-05-01 17:22:59 +000029// sampler matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030void setup_drawstate_aaclip(GrGpu* gpu,
31 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000032 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000033 GrDrawState* drawState = gpu->drawState();
34 GrAssert(drawState);
35
36 static const int maskStage = GrPaint::kTotalStages+1;
37
38 GrMatrix mat;
39 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000041 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042 mat.preConcat(drawState->getViewMatrix());
43
bsalomon@google.comb8670992012-07-25 21:27:09 +000044 drawState->sampler(maskStage)->reset(mat);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000046 drawState->createTextureEffect(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047}
48
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000049bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000050 GrGpu* gpu,
51 const SkPath& path,
52 GrPathFill fill,
53 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000054 // last (false) parameter disallows use of the SW path renderer
55 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
56}
57
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000058GrPathFill get_path_fill(const SkPath& path) {
59 switch (path.getFillType()) {
60 case SkPath::kWinding_FillType:
61 return kWinding_GrPathFill;
62 case SkPath::kEvenOdd_FillType:
63 return kEvenOdd_GrPathFill;
64 case SkPath::kInverseWinding_FillType:
65 return kInverseWinding_GrPathFill;
66 case SkPath::kInverseEvenOdd_FillType:
67 return kInverseEvenOdd_GrPathFill;
68 default:
69 GrCrash("Unsupported path fill in clip.");
70 return kWinding_GrPathFill; // suppress warning
71 }
72}
73
robertphillips@google.comb99225c2012-07-24 18:20:10 +000074/**
75 * Does any individual clip in 'clipIn' use anti-aliasing?
76 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000077bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000078
robertphillips@google.com641f8b12012-07-31 19:15:58 +000079 SkClipStack::Iter iter;
80 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000081
robertphillips@google.com641f8b12012-07-31 19:15:58 +000082 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comb99225c2012-07-24 18:20:10 +000083 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
84 NULL != clip;
85 clip = iter.next()) {
86
87 if (clip->fDoAA) {
88 return true;
89 }
90 }
91
92 return false;
93}
94
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000095}
96
robertphillips@google.comfa662942012-05-17 12:20:22 +000097/*
98 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
99 * will be used on any element. If so, it returns true to indicate that the
100 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
101 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000102bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000103
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000104 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105 // a clip gets complex enough it can just be done in SW regardless
106 // of whether it would invoke the GrSoftwarePathRenderer.
107 bool useSW = false;
108
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000109 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
110 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000111
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000112 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
113 NULL != clip;
114 clip = iter.next()) {
115
116 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000117 // Everything before a replace op can be ignored so start
118 // afresh w.r.t. determining if any element uses the SW path
119 useSW = false;
120 }
121
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
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000156 clipDataIn->getConservativeBounds(rt, &devClipBounds,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000157 &isIntersectionOfRects);
158 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000159 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000160 }
161
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.com6b70a7b2012-05-11 15:32:48 +0000164#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000165 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000166 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000167 // in software (this will only happen if the clip mask is anti-aliased
168 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000169 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000170 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000171 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000172 // The clip geometry is complex enough that it will be more
173 // efficient to create it entirely in software
174 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000175 GrIRect devBound;
176 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
177 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000178 fGpu->disableScissor();
179 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000180 return true;
181 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000182
183 // if SW clip mask creation fails fall through to the other
184 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000185 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000186#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000187
robertphillips@google.comf294b772012-04-27 14:29:26 +0000188#if GR_AA_CLIP
189 // If MSAA is enabled use the (faster) stencil path for AA clipping
190 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000191 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000192 // Since we are going to create a destination texture of the correct
193 // size for the mask (rather than being bound by the size of the
194 // render target) we aren't going to use scissoring like the stencil
195 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000196 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000197 GrIRect devBound;
198 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
199 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000200 fGpu->disableScissor();
201 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000202 return true;
203 }
204
205 // if alpha clip mask creation fails fall through to the stencil
206 // buffer method
207 }
208#endif // GR_AA_CLIP
209
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000211 // an antialiased clip couldn't be created. In either case, free up
212 // the texture in the antialiased mask cache.
213 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000214 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000215 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000216 // AA cache.
217 fAACache.reset();
218
bsalomon@google.coma3201942012-06-21 19:58:20 +0000219 // If the clip is a rectangle then just set the scissor. Otherwise, create
220 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000221 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000222 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000223 this->setGpuStencil();
224 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000225 }
226
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000227 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000228 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000229 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000230
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000231 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000232 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000233 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000234 // This must occur after createStencilClipMask. That function may change
235 // the scissor. Also, it only guarantees that the stencil mask is correct
236 // within the bounds it was passed, so we must use both stencil and scissor
237 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000238 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000239 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000240 return true;
241}
242
243#define VISUALIZE_COMPLEX_CLIP 0
244
245#if VISUALIZE_COMPLEX_CLIP
246 #include "GrRandom.h"
247 GrRandom gRandom;
248 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
249#else
250 #define SET_RANDOM_COLOR
251#endif
252
253namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000254/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000255 * Does "canvContainer" contain "devContainee"? If either is empty then
256 * no containment is possible. "canvContainer" is in canvas coordinates while
257 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000258 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000259 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000260bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000261 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000262 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000263 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000264 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000265 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000266 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000267 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000268}
269
robertphillips@google.comf294b772012-04-27 14:29:26 +0000270////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000271// determines how many elements at the head of the clip can be skipped and
272// whether the initial clear should be to the inside- or outside-the-clip value,
273// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000274const SkClipStack::Iter::Clip* process_initial_clip_elements(
275 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000276 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000277 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000278 SkRegion::Op* firstOp,
279 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000280
281 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000282
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000283 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000284 // processed the clip is entirely open. However, depending on the
285 // first set op we may prefer to clear to 0 for performance. We may
286 // also be able to skip the initial clip paths/rects. We loop until
287 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000288 bool done = false;
289 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000290
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000291 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000292
293 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
294 NULL != clip && !done;
295 clip = iter->next()) {
296 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000297 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000298 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000299 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 *clearToInside = false;
301 done = true;
302 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000303 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000304 // if this element contains the entire bounds then we
305 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000306 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000307 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000308 break;
309 }
310 // if everything is initially clearToInside then intersect is
311 // same as clear to 0 and treat as a replace. Otherwise,
312 // set stays empty.
313 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000314 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000315 *clearToInside = false;
316 done = true;
317 }
318 break;
319 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000320 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000321 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000322 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000323 // clearToInside
324 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000325 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000326 done = true;
327 }
328 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000329 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000330 // xor is same as difference or replace both of which
331 // can be 1-pass instead of 2 for xor.
332 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000333 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000334 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000335 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000336 }
337 done = true;
338 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000339 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000340 // if all pixels are clearToInside then we have to process the
341 // difference, otherwise it has no effect and all pixels
342 // remain outside.
343 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000344 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345 done = true;
346 }
347 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000348 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000349 // if all pixels are clearToInside then reverse difference
350 // produces empty set. Otherise it is same as replace
351 if (*clearToInside) {
352 *clearToInside = false;
353 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000354 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000355 done = true;
356 }
357 break;
358 default:
359 GrCrash("Unknown set op.");
360 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000361
362 if (done) {
363 // we need to break out here (rather than letting the test in
364 // the loop do it) since backing up the iterator is very expensive
365 break;
366 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000367 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000368 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000369}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000371}
372
robertphillips@google.comf294b772012-04-27 14:29:26 +0000373namespace {
374
375////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000376// set up the OpenGL blend function to perform the specified
377// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000378void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000379
380 switch (op) {
381 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000382 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000383 break;
384 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000385 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386 break;
387 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000388 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389 break;
390 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000391 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392 break;
393 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000394 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000395 break;
396 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000397 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000398 break;
399 default:
400 GrAssert(false);
401 break;
402 }
403}
404
robertphillips@google.comf294b772012-04-27 14:29:26 +0000405////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000406bool draw_path_in_software(GrContext* context,
407 GrGpu* gpu,
408 const SkPath& path,
409 GrPathFill fill,
410 bool doAA,
411 const GrIRect& resultBounds) {
412
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000413 SkAutoTUnref<GrTexture> texture(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000414 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
415 resultBounds, fill,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000416 doAA, NULL));
417 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000418 return false;
419 }
420
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000421 // The ClipMaskManager accumulates the clip mask in the UL corner
422 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000423
bsalomon@google.come3d32162012-07-20 13:37:06 +0000424 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000425
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000426 GrAssert(!GrIsFillInverted(fill));
427 return true;
428}
429
430
431////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000432bool draw_path(GrContext* context,
433 GrGpu* gpu,
434 const SkPath& path,
435 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000436 bool doAA,
437 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000439 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000440 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000441 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000442 }
443
bsalomon@google.come3d32162012-07-20 13:37:06 +0000444 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000445 return true;
446}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000447
robertphillips@google.com7b112892012-07-31 15:18:21 +0000448// 'rect' enters in device coordinates and leaves in canvas coordinates
449void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
450 GrAssert(NULL != rect);
451
452 rect->fLeft += SkIntToScalar(origin.fX);
453 rect->fTop += SkIntToScalar(origin.fY);
454 rect->fRight += SkIntToScalar(origin.fX);
455 rect->fBottom += SkIntToScalar(origin.fY);
456}
457
robertphillips@google.com72176b22012-05-23 13:19:12 +0000458}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000459
460////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000461bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000462 const SkClipStack::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000463 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000464 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000465 GrAssert(NULL != drawState);
466
467 drawState->setRenderTarget(target->asRenderTarget());
468
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000469 if (NULL != clip->fRect) {
470 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000471 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000472 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000473 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000474 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000475 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000476 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000477 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000478 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000479 *clip->fPath,
480 get_path_fill(*clip->fPath),
481 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000482 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000483 }
484 return true;
485}
486
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000487void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000488 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000489 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000490 GrAssert(NULL != drawState);
491
492 // no AA here since it is encoded in the texture
493 drawState->setRenderTarget(target->asRenderTarget());
494
495 GrMatrix sampleM;
496 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497
bsalomon@google.comb8670992012-07-25 21:27:09 +0000498 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000499 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000500
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000501 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
robertphillips@google.comf105b102012-05-14 12:18:26 +0000502 SkIntToScalar(target->height()));
503
bsalomon@google.come3d32162012-07-20 13:37:06 +0000504 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000505
tomhudson@google.com676e6602012-07-10 17:21:48 +0000506 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000507}
508
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000509// get a texture to act as a temporary buffer for AA clip boolean operations
510// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000511void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000512 GrAutoScratchTexture* temp) {
513 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000514 // we've already allocated the temp texture
515 return;
516 }
517
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000518 GrTextureDesc desc;
519 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
520 desc.fWidth = bounds.width();
521 desc.fHeight = bounds.height();
522 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000523
robertphillips@google.com2c756812012-05-22 20:28:23 +0000524 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000525}
526
robertphillips@google.comf105b102012-05-14 12:18:26 +0000527
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000528void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000529 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000530 // Since we are setting up the cache we know the last lookup was a miss
531 // Free up the currently cached mask so it can be reused
532 fAACache.reset();
533
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000534 GrTextureDesc desc;
535 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
536 desc.fWidth = bounds.width();
537 desc.fHeight = bounds.height();
538 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000539
540 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000541}
542
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000543////////////////////////////////////////////////////////////////////////////////
544// Shared preamble between gpu and SW-only AA clip mask creation paths.
545// Handles caching, determination of clip mask bound & allocation (if needed)
546// of the result texture
547// 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 +0000548bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000549 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000550 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000551 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000552 GrAssert(origDrawState->isClipState());
553
554 GrRenderTarget* rt = origDrawState->getRenderTarget();
555 GrAssert(NULL != rt);
556
robertphillips@google.comf294b772012-04-27 14:29:26 +0000557 // unlike the stencil path the alpha path is not bound to the size of the
558 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000559 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com7b112892012-07-31 15:18:21 +0000560 GrIRect devClipBounds;
561 clipDataIn.getConservativeBounds(rt, &devClipBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562
563 // need to outset a pixel since the standard bounding box computation
564 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com7b112892012-07-31 15:18:21 +0000565 devClipBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000566
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000567 // TODO: make sure we don't outset if bounds are still 0,0 @ min
568
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000569 if (fAACache.canReuse(*clipDataIn.fClipStack,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000570 devClipBounds.width(),
571 devClipBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000572 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000573 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000574 return true;
575 }
576
robertphillips@google.com7b112892012-07-31 15:18:21 +0000577 this->setupCache(*clipDataIn.fClipStack, devClipBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000578
robertphillips@google.com7b112892012-07-31 15:18:21 +0000579 *devResultBounds = devClipBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000580 return false;
581}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000582
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000583////////////////////////////////////////////////////////////////////////////////
584// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000585bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000586 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000587 GrIRect *devResultBounds) {
588 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000589 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
590
robertphillips@google.com7b112892012-07-31 15:18:21 +0000591 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000592 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000593 return true;
594 }
595
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000596 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
597
robertphillips@google.comf105b102012-05-14 12:18:26 +0000598 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000599 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000600 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000601 return false;
602 }
603
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000604 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
605 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000606
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000607 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608
robertphillips@google.com7b112892012-07-31 15:18:21 +0000609 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000610 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000611 // if we were able to trim down the size of the mask we need to
robertphillips@google.comf294b772012-04-27 14:29:26 +0000612 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000613 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000614 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000615 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000616 }
617
618 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000619 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
620
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000621 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000622 SkClipStack::Iter::kBottom_IterStart);
623 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000624 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000625 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000626 &firstOp,
627 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000629 fGpu->clear(NULL,
630 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000631 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632
robertphillips@google.comf105b102012-05-14 12:18:26 +0000633 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000634 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000635 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000636 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000637
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000638 SkRegion::Op op = clip->fOp;
639 if (first) {
640 first = false;
641 op = firstOp;
642 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000643
644 if (SkRegion::kReplace_Op == op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000645 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000646 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000648 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000649 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000650
651 } else if (SkRegion::kReverseDifference_Op == op ||
652 SkRegion::kIntersect_Op == op) {
653 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000654 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000655 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000656 continue;
657 }
658
robertphillips@google.com7b112892012-07-31 15:18:21 +0000659 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000660 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000661 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000662 return false;
663 }
664
robertphillips@google.comf294b772012-04-27 14:29:26 +0000665 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000666 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000668 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000669 this->drawClipShape(temp.texture(), clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000670
671 // TODO: rather than adding these two translations here
672 // compute the bounding box needed to render the texture
673 // into temp
robertphillips@google.com7b112892012-07-31 15:18:21 +0000674 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000675 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
676 // In order for the merge of the temp clip into the accumulator
677 // to work we need to disable the translation
678 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000679 }
680
681 // Now draw into the accumulator using the real operation
682 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000683 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000684 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000685
robertphillips@google.com7b112892012-07-31 15:18:21 +0000686 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000687 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
688 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000689 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000690 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000691 }
692
693 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000694 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000695 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000696 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000697 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000698 }
699 }
700
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000701 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000702 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000703 return true;
704}
705
706////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000707// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000708// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000709bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000710 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000712 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000714 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715 GrAssert(drawState->isClipState());
716
717 GrRenderTarget* rt = drawState->getRenderTarget();
718 GrAssert(NULL != rt);
719
720 // TODO: dynamically attach a SB when needed.
721 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
722 if (NULL == stencilBuffer) {
723 return false;
724 }
725
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000726 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000728 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729
730 // we set the current clip to the bounds so that our recursive
731 // draws are scissored to them. We use the copy of the complex clip
732 // we just stashed on the SB to render from. We set it back after
733 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000734 const GrClipData* oldClipData = fGpu->getClip();
735
robertphillips@google.com7b112892012-07-31 15:18:21 +0000736 // The origin of 'newClipData' is (0, 0) so it is okay to place
737 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000738 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000739 GrClipData newClipData;
740 newClipData.fClipStack = &newClipStack;
741
742 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000744 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
745 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000747 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000748
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000749 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000750 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000751 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000752 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000753 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000754 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000755 }
756
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000757#if !VISUALIZE_COMPLEX_CLIP
758 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
759#endif
760
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000761 int clipBit = stencilBuffer->bits();
762 SkASSERT((clipBit <= 16) &&
763 "Ganesh only handles 16b or smaller stencil buffers");
764 clipBit = (1 << (clipBit-1));
765
robertphillips@google.com7b112892012-07-31 15:18:21 +0000766 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000767
768 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000769 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
770
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000771 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000772 SkClipStack::Iter::kBottom_IterStart);
773 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000774 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000775 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000776 &firstOp,
777 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778
robertphillips@google.com7b112892012-07-31 15:18:21 +0000779 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000780 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781
782 // walk through each clip element and perform its set op
783 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000784 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000786 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787 // enabled at bottom of loop
788 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000789 // if the target is MSAA then we want MSAA enabled when the clip is soft
790 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000791 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000792 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
793 } else {
794 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
795 }
796 }
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.come3d32162012-07-20 13:37:06 +0000867 pr->drawPath(*clipPath, fill, NULL, 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.come3d32162012-07-20 13:37:06 +0000885 pr->drawPath(*clipPath, fill, NULL, 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
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001135 GrMatrix 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.comfa662942012-05-17 12:20:22 +00001173
1174 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001175 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001176 }
1177
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001178 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001179
1180 // convert the rect to a path so we can invert the fill
1181 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001182 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001183
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001184 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001185 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001186 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001187 } else if (NULL != clip->fPath) {
1188 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001189 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001190 invert_fill(get_path_fill(*clip->fPath)),
1191 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001192 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001193 }
1194
1195 continue;
1196 }
1197
1198 // The other ops (union, xor, diff) only affect pixels inside
1199 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001200 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001201
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001202 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001203 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001204 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001205
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001206 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001207 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001208 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001209 get_path_fill(*clip->fPath),
1210 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001211 }
1212 }
1213
robertphillips@google.comfa662942012-05-17 12:20:22 +00001214 // Because we are using the scratch texture cache, "accum" may be
1215 // larger than expected and have some cruft in the areas we aren't using.
1216 // Clear it out.
1217
1218 // TODO: need a simpler way to clear the texture - can we combine
1219 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001220 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001221 GrAssert(NULL != drawState);
1222 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001223 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001224 // can't leave the accum bound as a rendertarget
1225 drawState->setRenderTarget(temp);
1226
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001227 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001228
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001229 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001230
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001231 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001232 return true;
1233}
1234
robertphillips@google.comf294b772012-04-27 14:29:26 +00001235////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001236void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001237 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001238}