blob: 2be92dc24aa8b86d59d7e03209c74507674f822d [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
robertphillips@google.comba998f22012-10-12 11:33:56 +000023#define GR_AA_CLIP 1
24#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
bsalomon@google.com08283af2012-10-26 13:01:20 +000029// stage 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
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000036 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037
bsalomon@google.comb9086a02012-11-01 18:02:54 +000038 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039 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.com08283af2012-10-26 13:01:20 +000044 drawState->stage(kMaskStage)->reset();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000045 drawState->createTextureEffect(kMaskStage, result, mat);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046}
47
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000048bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000049 GrGpu* gpu,
50 const SkPath& path,
51 GrPathFill fill,
52 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000053 // last (false) parameter disallows use of the SW path renderer
54 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
55}
56
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000057GrPathFill get_path_fill(const SkPath& path) {
58 switch (path.getFillType()) {
59 case SkPath::kWinding_FillType:
60 return kWinding_GrPathFill;
61 case SkPath::kEvenOdd_FillType:
62 return kEvenOdd_GrPathFill;
63 case SkPath::kInverseWinding_FillType:
64 return kInverseWinding_GrPathFill;
65 case SkPath::kInverseEvenOdd_FillType:
66 return kInverseEvenOdd_GrPathFill;
67 default:
68 GrCrash("Unsupported path fill in clip.");
69 return kWinding_GrPathFill; // suppress warning
70 }
71}
72
robertphillips@google.comb99225c2012-07-24 18:20:10 +000073/**
74 * Does any individual clip in 'clipIn' use anti-aliasing?
75 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000076bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000077
robertphillips@google.com641f8b12012-07-31 19:15:58 +000078 SkClipStack::Iter iter;
79 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000080
robertphillips@google.com641f8b12012-07-31 19:15:58 +000081 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comb99225c2012-07-24 18:20:10 +000082 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
83 NULL != clip;
84 clip = iter.next()) {
85
86 if (clip->fDoAA) {
87 return true;
88 }
89 }
90
91 return false;
92}
93
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000094}
95
robertphillips@google.comfa662942012-05-17 12:20:22 +000096/*
97 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
98 * will be used on any element. If so, it returns true to indicate that the
99 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
100 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000101bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000102
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000103 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104 // a clip gets complex enough it can just be done in SW regardless
105 // of whether it would invoke the GrSoftwarePathRenderer.
106 bool useSW = false;
107
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000108 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
109 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000110
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000111 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
112 NULL != clip;
113 clip = iter.next()) {
114
115 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000116 // Everything before a replace op can be ignored so start
117 // afresh w.r.t. determining if any element uses the SW path
118 useSW = false;
119 }
120
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000121 // rects can always be drawn directly w/o using the software path
122 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000123 if (NULL != clip->fPath &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000124 path_needs_SW_renderer(this->getContext(), fGpu,
125 *clip->fPath,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000126 get_path_fill(*clip->fPath),
127 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000128 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000129 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000130 }
131
132 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000133}
134
robertphillips@google.comf294b772012-04-27 14:29:26 +0000135////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000136// sort out what kind of clip mask needs to be created: alpha, stencil,
137// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000138bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000139 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000140
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000141 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000142 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000143 fGpu->disableScissor();
144 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000145 return true;
146 }
147
148 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000149 // GrDrawTarget should have filtered this for us
150 GrAssert(NULL != rt);
151
robertphillips@google.com7b112892012-07-31 15:18:21 +0000152 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000153 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000154
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000155 clipDataIn->getConservativeBounds(rt, &devClipBounds,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000156 &isIntersectionOfRects);
157 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000158 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000159 }
160
bsalomon@google.com100abf42012-09-05 17:40:04 +0000161#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000162 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000163
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000164 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000165 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000166 // in software (this will only happen if the clip mask is anti-aliased
167 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000168 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000169 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000170 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000171 // The clip geometry is complex enough that it will be more
172 // efficient to create it entirely in software
173 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000174 GrIRect devBound;
175 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
176 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000177 fGpu->disableScissor();
178 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000179 return true;
180 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000181
182 // if SW clip mask creation fails fall through to the other
183 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000184 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000185#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000186
robertphillips@google.comf294b772012-04-27 14:29:26 +0000187#if GR_AA_CLIP
188 // If MSAA is enabled use the (faster) stencil path for AA clipping
189 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000190 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000191 // Since we are going to create a destination texture of the correct
192 // size for the mask (rather than being bound by the size of the
193 // render target) we aren't going to use scissoring like the stencil
194 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000195 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000196 GrIRect devBound;
197 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
198 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000199 fGpu->disableScissor();
200 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000201 return true;
202 }
203
204 // if alpha clip mask creation fails fall through to the stencil
205 // buffer method
206 }
207#endif // GR_AA_CLIP
208
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000209 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000210 // an antialiased clip couldn't be created. In either case, free up
211 // the texture in the antialiased mask cache.
212 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000213 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000214 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000215 // AA cache.
216 fAACache.reset();
217
bsalomon@google.coma3201942012-06-21 19:58:20 +0000218 // If the clip is a rectangle then just set the scissor. Otherwise, create
219 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000220 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000221 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000222 this->setGpuStencil();
223 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000224 }
225
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000226 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000227 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000228 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000229
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000230 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000231 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000232 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000233 // This must occur after createStencilClipMask. That function may change
234 // the scissor. Also, it only guarantees that the stencil mask is correct
235 // within the bounds it was passed, so we must use both stencil and scissor
236 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000237 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000238 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000239 return true;
240}
241
242#define VISUALIZE_COMPLEX_CLIP 0
243
244#if VISUALIZE_COMPLEX_CLIP
245 #include "GrRandom.h"
246 GrRandom gRandom;
247 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
248#else
249 #define SET_RANDOM_COLOR
250#endif
251
252namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000253/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000254 * Does "canvContainer" contain "devContainee"? If either is empty then
255 * no containment is possible. "canvContainer" is in canvas coordinates while
256 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000257 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000258 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000259bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000260 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000261 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000262 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000263 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000264 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000265 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000266 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000267}
268
robertphillips@google.comf294b772012-04-27 14:29:26 +0000269////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000270// determines how many elements at the head of the clip can be skipped and
271// whether the initial clear should be to the inside- or outside-the-clip value,
272// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000273const SkClipStack::Iter::Clip* process_initial_clip_elements(
274 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000275 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000276 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000277 SkRegion::Op* firstOp,
278 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000279
280 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000281
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000282 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000283 // processed the clip is entirely open. However, depending on the
284 // first set op we may prefer to clear to 0 for performance. We may
285 // also be able to skip the initial clip paths/rects. We loop until
286 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000287 bool done = false;
288 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000290 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000291
292 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
293 NULL != clip && !done;
294 clip = iter->next()) {
295 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000296 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000297 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000298 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000299 *clearToInside = false;
300 done = true;
301 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000302 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000303 // if this element contains the entire bounds then we
304 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000305 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000306 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000307 break;
308 }
309 // if everything is initially clearToInside then intersect is
310 // same as clear to 0 and treat as a replace. Otherwise,
311 // set stays empty.
312 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000313 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000314 *clearToInside = false;
315 done = true;
316 }
317 break;
318 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000319 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000321 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000322 // clearToInside
323 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000324 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000325 done = true;
326 }
327 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000328 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000329 // xor is same as difference or replace both of which
330 // can be 1-pass instead of 2 for xor.
331 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000332 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000333 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000334 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 }
336 done = true;
337 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000338 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 // if all pixels are clearToInside then we have to process the
340 // difference, otherwise it has no effect and all pixels
341 // remain outside.
342 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000343 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000344 done = true;
345 }
346 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000347 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000348 // if all pixels are clearToInside then reverse difference
349 // produces empty set. Otherise it is same as replace
350 if (*clearToInside) {
351 *clearToInside = false;
352 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000353 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000354 done = true;
355 }
356 break;
357 default:
358 GrCrash("Unknown set op.");
359 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000360
361 if (done) {
362 // we need to break out here (rather than letting the test in
363 // the loop do it) since backing up the iterator is very expensive
364 break;
365 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000366 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000367 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000368}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000370}
371
robertphillips@google.comf294b772012-04-27 14:29:26 +0000372namespace {
373
374////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000375// set up the OpenGL blend function to perform the specified
376// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000377void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000378
379 switch (op) {
380 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000381 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382 break;
383 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000384 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385 break;
386 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000387 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388 break;
389 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000390 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391 break;
392 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000393 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000394 break;
395 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000396 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000397 break;
398 default:
399 GrAssert(false);
400 break;
401 }
402}
403
robertphillips@google.comf294b772012-04-27 14:29:26 +0000404////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000405bool draw_path_in_software(GrContext* context,
406 GrGpu* gpu,
407 const SkPath& path,
408 GrPathFill fill,
409 bool doAA,
410 const GrIRect& resultBounds) {
411
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000412 SkAutoTUnref<GrTexture> texture(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000413 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
414 resultBounds, fill,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000415 doAA, NULL));
416 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000417 return false;
418 }
419
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000420 // The ClipMaskManager accumulates the clip mask in the UL corner
421 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000422
bsalomon@google.come3d32162012-07-20 13:37:06 +0000423 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000424
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000425 GrAssert(!GrIsFillInverted(fill));
426 return true;
427}
428
429
430////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000431bool draw_path(GrContext* context,
432 GrGpu* gpu,
433 const SkPath& path,
434 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000435 bool doAA,
436 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000438 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000439 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000440 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000441 }
442
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000443 pr->drawPath(path, fill, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000444 return true;
445}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000446
robertphillips@google.com7b112892012-07-31 15:18:21 +0000447// 'rect' enters in device coordinates and leaves in canvas coordinates
448void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
449 GrAssert(NULL != rect);
450
451 rect->fLeft += SkIntToScalar(origin.fX);
452 rect->fTop += SkIntToScalar(origin.fY);
453 rect->fRight += SkIntToScalar(origin.fX);
454 rect->fBottom += SkIntToScalar(origin.fY);
455}
456
robertphillips@google.com72176b22012-05-23 13:19:12 +0000457}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000458
459////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000460bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000461 const SkClipStack::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000462 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000463 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464 GrAssert(NULL != drawState);
465
466 drawState->setRenderTarget(target->asRenderTarget());
467
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000468 if (NULL != clip->fRect) {
469 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000470 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000471 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000472 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000473 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000474 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000475 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000476 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000477 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000478 *clip->fPath,
479 get_path_fill(*clip->fPath),
480 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000481 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000482 }
483 return true;
484}
485
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000486void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000487 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000488 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000489 GrAssert(NULL != drawState);
490
491 // no AA here since it is encoded in the texture
492 drawState->setRenderTarget(target->asRenderTarget());
493
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000494 SkMatrix sampleM;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000496
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000497 drawState->createTextureEffect(0, texture, sampleM);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000498
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000499 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
robertphillips@google.comf105b102012-05-14 12:18:26 +0000500 SkIntToScalar(target->height()));
501
bsalomon@google.come3d32162012-07-20 13:37:06 +0000502 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000503
tomhudson@google.com676e6602012-07-10 17:21:48 +0000504 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000505}
506
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000507// get a texture to act as a temporary buffer for AA clip boolean operations
508// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000509void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000510 GrAutoScratchTexture* temp) {
511 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000512 // we've already allocated the temp texture
513 return;
514 }
515
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000516 GrTextureDesc desc;
517 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
518 desc.fWidth = bounds.width();
519 desc.fHeight = bounds.height();
520 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000521
robertphillips@google.com2c756812012-05-22 20:28:23 +0000522 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000523}
524
robertphillips@google.comf105b102012-05-14 12:18:26 +0000525
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000526void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000527 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000528 // Since we are setting up the cache we know the last lookup was a miss
529 // Free up the currently cached mask so it can be reused
530 fAACache.reset();
531
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000532 GrTextureDesc desc;
533 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
534 desc.fWidth = bounds.width();
535 desc.fHeight = bounds.height();
536 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000537
538 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000539}
540
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000541////////////////////////////////////////////////////////////////////////////////
542// Shared preamble between gpu and SW-only AA clip mask creation paths.
543// Handles caching, determination of clip mask bound & allocation (if needed)
544// of the result texture
545// 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 +0000546bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000547 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000548 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000549 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000550 GrAssert(origDrawState->isClipState());
551
552 GrRenderTarget* rt = origDrawState->getRenderTarget();
553 GrAssert(NULL != rt);
554
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555 // unlike the stencil path the alpha path is not bound to the size of the
556 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000557 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000558 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559
560 // need to outset a pixel since the standard bounding box computation
561 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000562 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000564 // TODO: make sure we don't outset if bounds are still 0,0 @ min
565
robertphillips@google.comba998f22012-10-12 11:33:56 +0000566 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000567 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000568 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000569 return true;
570 }
571
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000572 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000573 return false;
574}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000575
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000576////////////////////////////////////////////////////////////////////////////////
577// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000578bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000579 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000580 GrIRect *devResultBounds) {
581 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000582 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
583
robertphillips@google.com7b112892012-07-31 15:18:21 +0000584 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000585 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000586 return true;
587 }
588
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000589 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
590
robertphillips@google.comf105b102012-05-14 12:18:26 +0000591 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000592 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000593 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594 return false;
595 }
596
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000597 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
598 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000599
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000600 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000601
robertphillips@google.com7b112892012-07-31 15:18:21 +0000602 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000603 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000604 // if we were able to trim down the size of the mask we need to
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000606 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000607 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000608 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000609 }
610
611 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000612 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
613
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000614 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000615 SkClipStack::Iter::kBottom_IterStart);
616 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000617 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000618 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000619 &firstOp,
620 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000621
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000622 fGpu->clear(NULL,
623 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000624 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000625
robertphillips@google.comf105b102012-05-14 12:18:26 +0000626 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000627 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000629 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000630
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000631 SkRegion::Op op = clip->fOp;
632 if (first) {
633 first = false;
634 op = firstOp;
635 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000636
637 if (SkRegion::kReplace_Op == op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000638 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000639 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000641 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000642 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000643
644 } else if (SkRegion::kReverseDifference_Op == op ||
645 SkRegion::kIntersect_Op == op) {
646 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000647 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000648 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000649 continue;
650 }
651
robertphillips@google.com7b112892012-07-31 15:18:21 +0000652 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000653 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000654 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000655 return false;
656 }
657
robertphillips@google.comf294b772012-04-27 14:29:26 +0000658 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000659 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000660
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000661 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000662 this->drawClipShape(temp.texture(), clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000663
664 // TODO: rather than adding these two translations here
665 // compute the bounding box needed to render the texture
666 // into temp
robertphillips@google.com7b112892012-07-31 15:18:21 +0000667 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000668 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
669 // In order for the merge of the temp clip into the accumulator
670 // to work we need to disable the translation
671 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000672 }
673
674 // Now draw into the accumulator using the real operation
675 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000676 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000677 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000678
robertphillips@google.com7b112892012-07-31 15:18:21 +0000679 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000680 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
681 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000682 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000683 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684 }
685
686 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000687 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000688 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000689 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000690 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000691 }
692 }
693
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000694 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000695 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000696 return true;
697}
698
699////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000700// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000701// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000702bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000703 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000705 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000707 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708 GrAssert(drawState->isClipState());
709
710 GrRenderTarget* rt = drawState->getRenderTarget();
711 GrAssert(NULL != rt);
712
713 // TODO: dynamically attach a SB when needed.
714 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
715 if (NULL == stencilBuffer) {
716 return false;
717 }
718
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000719 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000721 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722
723 // we set the current clip to the bounds so that our recursive
724 // draws are scissored to them. We use the copy of the complex clip
725 // we just stashed on the SB to render from. We set it back after
726 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000727 const GrClipData* oldClipData = fGpu->getClip();
728
robertphillips@google.com7b112892012-07-31 15:18:21 +0000729 // The origin of 'newClipData' is (0, 0) so it is okay to place
730 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000731 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000732 GrClipData newClipData;
733 newClipData.fClipStack = &newClipStack;
734
735 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000737 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
738 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000740 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000742 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000743 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000744 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000745 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000746 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000747 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000748 }
749
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750#if !VISUALIZE_COMPLEX_CLIP
751 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
752#endif
753
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754 int clipBit = stencilBuffer->bits();
755 SkASSERT((clipBit <= 16) &&
756 "Ganesh only handles 16b or smaller stencil buffers");
757 clipBit = (1 << (clipBit-1));
758
robertphillips@google.com7b112892012-07-31 15:18:21 +0000759 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760
761 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000762 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
763
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000764 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000765 SkClipStack::Iter::kBottom_IterStart);
766 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000767 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000768 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000769 &firstOp,
770 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000771
robertphillips@google.com7b112892012-07-31 15:18:21 +0000772 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000773 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774
775 // walk through each clip element and perform its set op
776 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000777 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000779 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780 // enabled at bottom of loop
781 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000782 // if the target is MSAA then we want MSAA enabled when the clip is soft
783 if (rt->isMultisampled()) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000784 drawState->setState(GrDrawState::kHWAntialias_StateBit, clip->fDoAA);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000785 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786
tomhudson@google.com8afae612012-08-14 15:03:35 +0000787 // Can the clip element be drawn directly to the stencil buffer
788 // with a non-inverted fill rule without extra passes to
789 // resolve in/out status?
790 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000792 SkRegion::Op op = clip->fOp;
793 if (first) {
794 first = false;
795 op = firstOp;
796 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000797
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000799 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000800 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000801 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000802 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803 fillInverted = false;
804 // there is no point in intersecting a screen filling
805 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000806 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000807 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000808 continue;
809 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000810 } else {
811 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000812 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 fillInverted = GrIsFillInverted(fill);
814 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000815 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000816 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000817 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000818 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 return false;
820 }
821 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000822 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000823 }
824
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 int passes;
826 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
827
828 bool canDrawDirectToClip; // Given the renderer, the element,
829 // fill rule, and set operation can
830 // we render the element directly to
831 // stencil bit used for clipping.
832 canDrawDirectToClip =
833 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000834 canRenderDirectToStencil,
835 clipBit,
836 fillInverted,
837 &passes,
838 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000839
840 // draw the element to the client stencil bits if necessary
841 if (!canDrawDirectToClip) {
842 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
843 kIncClamp_StencilOp,
844 kIncClamp_StencilOp,
845 kAlways_StencilFunc,
846 0xffff,
847 0x0000,
848 0xffff);
849 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000850 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000851 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000852 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000853 } else {
854 if (canRenderDirectToStencil) {
855 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000856 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000857 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000858 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000859 }
860 }
861 }
862
863 // now we modify the clip bit by rendering either the clip
864 // element directly or a bounding rect of the entire clip.
865 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
866 for (int p = 0; p < passes; ++p) {
867 *drawState->stencil() = stencilSettings[p];
868 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000869 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000870 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000871 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000872 } else {
873 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000874 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000875 }
876 } else {
877 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000878 // 'devClipBounds' is already in device coordinates so the
879 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000880 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000881 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000882 GrRect canvClipBounds;
883 canvClipBounds.set(devClipBounds);
884 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
885 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000886 }
887 }
888 }
889 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000890 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000891 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000892 // set this last because recursive draws may overwrite it back to kNone.
893 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
894 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000895 return true;
896}
897
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000898
bsalomon@google.com411dad02012-06-05 20:24:20 +0000899// mapping of clip-respecting stencil funcs to normal stencil funcs
900// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000901static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000902 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
903 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
904 // In the Clip Funcs
905 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
906 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
907 kLess_StencilFunc, // kLessIfInClip_StencilFunc
908 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
909 // Special in the clip func that forces user's ref to be 0.
910 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
911 // make ref 0 and do normal nequal.
912 },
913 {// Stencil-Clipping is ENABLED
914 // In the Clip Funcs
915 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
916 // eq stencil clip bit, mask
917 // out user bits.
918
919 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
920 // add stencil bit to mask and ref
921
922 kLess_StencilFunc, // kLessIfInClip_StencilFunc
923 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
924 // for both of these we can add
925 // the clip bit to the mask and
926 // ref and compare as normal
927 // Special in the clip func that forces user's ref to be 0.
928 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
929 // make ref have only the clip bit set
930 // and make comparison be less
931 // 10..0 < 1..user_bits..
932 }
933};
934
bsalomon@google.coma3201942012-06-21 19:58:20 +0000935namespace {
936// Sets the settings to clip against the stencil buffer clip while ignoring the
937// client bits.
938const GrStencilSettings& basic_apply_stencil_clip_settings() {
939 // stencil settings to use when clip is in stencil
940 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
941 kKeep_StencilOp,
942 kKeep_StencilOp,
943 kAlwaysIfInClip_StencilFunc,
944 0x0000,
945 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000946 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000947 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
948}
949}
950
951void GrClipMaskManager::setGpuStencil() {
952 // We make two copies of the StencilSettings here (except in the early
953 // exit scenario. One copy from draw state to the stack var. Then another
954 // from the stack var to the gpu. We could make this class hold a ptr to
955 // GrGpu's fStencilSettings and eliminate the stack copy here.
956
957 const GrDrawState& drawState = fGpu->getDrawState();
958
959 // use stencil for clipping if clipping is enabled and the clip
960 // has been written into the stencil.
961 GrClipMaskManager::StencilClipMode clipMode;
962 if (this->isClipInStencil() && drawState.isClipState()) {
963 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
964 // We can't be modifying the clip and respecting it at the same time.
965 GrAssert(!drawState.isStateFlagEnabled(
966 GrGpu::kModifyStencilClip_StateBit));
967 } else if (drawState.isStateFlagEnabled(
968 GrGpu::kModifyStencilClip_StateBit)) {
969 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
970 } else {
971 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
972 }
973
974 GrStencilSettings settings;
975 // The GrGpu client may not be using the stencil buffer but we may need to
976 // enable it in order to respect a stencil clip.
977 if (drawState.getStencil().isDisabled()) {
978 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
979 settings = basic_apply_stencil_clip_settings();
980 } else {
981 fGpu->disableStencil();
982 return;
983 }
984 } else {
985 settings = drawState.getStencil();
986 }
987
988 // TODO: dynamically attach a stencil buffer
989 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000990 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000991 drawState.getRenderTarget()->getStencilBuffer();
992 if (NULL != stencilBuffer) {
993 stencilBits = stencilBuffer->bits();
994 }
995
bsalomon@google.comf6601872012-08-28 21:11:35 +0000996 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000997 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000998 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000999 this->adjustStencilParams(&settings, clipMode, stencilBits);
1000 fGpu->setStencilSettings(settings);
1001}
1002
1003void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1004 StencilClipMode mode,
1005 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001006 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001007
1008 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001009 // We assume that this clip manager itself is drawing to the GrGpu and
1010 // has already setup the correct values.
1011 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001012 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001013
bsalomon@google.com411dad02012-06-05 20:24:20 +00001014 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1015 unsigned int userBits = clipBit - 1;
1016
bsalomon@google.coma3201942012-06-21 19:58:20 +00001017 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001018 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001019
bsalomon@google.coma3201942012-06-21 19:58:20 +00001020 bool finished = false;
1021 while (!finished) {
1022 GrStencilFunc func = settings->func(face);
1023 uint16_t writeMask = settings->writeMask(face);
1024 uint16_t funcMask = settings->funcMask(face);
1025 uint16_t funcRef = settings->funcRef(face);
1026
1027 GrAssert((unsigned) func < kStencilFuncCount);
1028
1029 writeMask &= userBits;
1030
1031 if (func >= kBasicStencilFuncCount) {
1032 int respectClip = kRespectClip_StencilClipMode == mode;
1033 if (respectClip) {
1034 // The GrGpu class should have checked this
1035 GrAssert(this->isClipInStencil());
1036 switch (func) {
1037 case kAlwaysIfInClip_StencilFunc:
1038 funcMask = clipBit;
1039 funcRef = clipBit;
1040 break;
1041 case kEqualIfInClip_StencilFunc:
1042 case kLessIfInClip_StencilFunc:
1043 case kLEqualIfInClip_StencilFunc:
1044 funcMask = (funcMask & userBits) | clipBit;
1045 funcRef = (funcRef & userBits) | clipBit;
1046 break;
1047 case kNonZeroIfInClip_StencilFunc:
1048 funcMask = (funcMask & userBits) | clipBit;
1049 funcRef = clipBit;
1050 break;
1051 default:
1052 GrCrash("Unknown stencil func");
1053 }
1054 } else {
1055 funcMask &= userBits;
1056 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001057 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001058 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001059 gSpecialToBasicStencilFunc[respectClip];
1060 func = table[func - kBasicStencilFuncCount];
1061 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001062 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001063 funcMask &= userBits;
1064 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001065 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001066
1067 settings->setFunc(face, func);
1068 settings->setWriteMask(face, writeMask);
1069 settings->setFuncMask(face, funcMask);
1070 settings->setFuncRef(face, funcRef);
1071
1072 if (GrStencilSettings::kFront_Face == face) {
1073 face = GrStencilSettings::kBack_Face;
1074 finished = !twoSided;
1075 } else {
1076 finished = true;
1077 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001078 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001079 if (!twoSided) {
1080 settings->copyFrontSettingsToBack();
1081 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001082}
1083
1084////////////////////////////////////////////////////////////////////////////////
1085
robertphillips@google.comfa662942012-05-17 12:20:22 +00001086namespace {
1087
1088GrPathFill invert_fill(GrPathFill fill) {
1089 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001090 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1091 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1092 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1093 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1094 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001095 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001096 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1097 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1098 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1099 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1100 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1101 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001102 return gInvertedFillTable[fill];
1103}
1104
1105}
1106
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001107bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001108 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001109 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001110 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001111
robertphillips@google.com7b112892012-07-31 15:18:21 +00001112 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001113 return true;
1114 }
1115
robertphillips@google.comf105b102012-05-14 12:18:26 +00001116 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001117 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001118 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001119 return false;
1120 }
1121
robertphillips@google.com2c756812012-05-22 20:28:23 +00001122 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001123
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001124 SkMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001125 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001126 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001127 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001128
robertphillips@google.comfa662942012-05-17 12:20:22 +00001129 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001130 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1131
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001132 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001133 SkClipStack::Iter::kBottom_IterStart);
1134 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001135 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001136 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001137 &firstOp,
1138 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001139
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001140 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001141
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001142 bool first = true;
1143 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001144
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001145 SkRegion::Op op = clip->fOp;
1146 if (first) {
1147 first = false;
1148 op = firstOp;
1149 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001150
1151 if (SkRegion::kIntersect_Op == op ||
1152 SkRegion::kReverseDifference_Op == op) {
1153 // Intersect and reverse difference require modifying pixels
1154 // outside of the geometry that is being "drawn". In both cases
1155 // we erase all the pixels outside of the geometry but
1156 // leave the pixels inside the geometry alone. For reverse
1157 // difference we invert all the pixels before clearing the ones
1158 // outside the geometry.
1159 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001160 SkRect temp;
1161 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001162 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1163 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001164
1165 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001166 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001167 }
1168
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001169 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001170
1171 // convert the rect to a path so we can invert the fill
1172 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001173 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001174
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001175 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001176 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001177 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001178 } else if (NULL != clip->fPath) {
1179 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001180 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001181 invert_fill(get_path_fill(*clip->fPath)),
1182 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001183 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001184 }
1185
1186 continue;
1187 }
1188
1189 // The other ops (union, xor, diff) only affect pixels inside
1190 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001191 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001192
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001193 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001194 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001195 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001196
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001197 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001198 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001199 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001200 get_path_fill(*clip->fPath),
1201 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001202 }
1203 }
1204
robertphillips@google.comfa662942012-05-17 12:20:22 +00001205 // Because we are using the scratch texture cache, "accum" may be
1206 // larger than expected and have some cruft in the areas we aren't using.
1207 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001208 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001209
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001210 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001211
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001212 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001213
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001214 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001215 return true;
1216}
1217
robertphillips@google.comf294b772012-04-27 14:29:26 +00001218////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001219void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001220 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001221}