blob: 5f1f0e3899316e62cffccfb48756651eea9ddfd2 [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
bsalomon@google.com100abf42012-09-05 17:40:04 +0000162#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000163 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000164
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.com0f11e1a2012-10-08 14:48:36 +0000444 pr->drawPath(path, fill, 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.com9cb5adf2012-08-30 11:05:08 +0000560 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561
562 // need to outset a pixel since the standard bounding box computation
563 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000564 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000565
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000566 // TODO: make sure we don't outset if bounds are still 0,0 @ min
567
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000568 if (fAACache.canReuse(*clipDataIn.fClipStack,
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000569 devResultBounds->width(),
570 devResultBounds->height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000571 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000572 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000573 return true;
574 }
575
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000576 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000577 return false;
578}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000579
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000580////////////////////////////////////////////////////////////////////////////////
581// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000582bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000583 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000584 GrIRect *devResultBounds) {
585 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000586 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
587
robertphillips@google.com7b112892012-07-31 15:18:21 +0000588 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000589 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000590 return true;
591 }
592
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000593 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
594
robertphillips@google.comf105b102012-05-14 12:18:26 +0000595 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000596 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000597 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000598 return false;
599 }
600
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000601 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
602 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000603
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000604 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605
robertphillips@google.com7b112892012-07-31 15:18:21 +0000606 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000607 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000608 // if we were able to trim down the size of the mask we need to
robertphillips@google.comf294b772012-04-27 14:29:26 +0000609 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000610 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000611 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000612 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613 }
614
615 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000616 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
617
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000618 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000619 SkClipStack::Iter::kBottom_IterStart);
620 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000621 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000622 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000623 &firstOp,
624 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000625
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000626 fGpu->clear(NULL,
627 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000628 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000629
robertphillips@google.comf105b102012-05-14 12:18:26 +0000630 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000631 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000633 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000634
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000635 SkRegion::Op op = clip->fOp;
636 if (first) {
637 first = false;
638 op = firstOp;
639 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640
641 if (SkRegion::kReplace_Op == op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000642 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000643 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000644
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000645 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000646 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647
648 } else if (SkRegion::kReverseDifference_Op == op ||
649 SkRegion::kIntersect_Op == op) {
650 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000651 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000652 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000653 continue;
654 }
655
robertphillips@google.com7b112892012-07-31 15:18:21 +0000656 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000657 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000658 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000659 return false;
660 }
661
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000663 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000664
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000665 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000666 this->drawClipShape(temp.texture(), clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667
668 // TODO: rather than adding these two translations here
669 // compute the bounding box needed to render the texture
670 // into temp
robertphillips@google.com7b112892012-07-31 15:18:21 +0000671 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000672 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
673 // In order for the merge of the temp clip into the accumulator
674 // to work we need to disable the translation
675 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000676 }
677
678 // Now draw into the accumulator using the real operation
679 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000680 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000681 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000682
robertphillips@google.com7b112892012-07-31 15:18:21 +0000683 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000684 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
685 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000686 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000687 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000688 }
689
690 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000691 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000692 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000693 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000694 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000695 }
696 }
697
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000698 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000699 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000700 return true;
701}
702
703////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000704// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000705// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000706bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000707 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000709 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000711 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 GrAssert(drawState->isClipState());
713
714 GrRenderTarget* rt = drawState->getRenderTarget();
715 GrAssert(NULL != rt);
716
717 // TODO: dynamically attach a SB when needed.
718 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
719 if (NULL == stencilBuffer) {
720 return false;
721 }
722
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000723 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000725 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726
727 // we set the current clip to the bounds so that our recursive
728 // draws are scissored to them. We use the copy of the complex clip
729 // we just stashed on the SB to render from. We set it back after
730 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000731 const GrClipData* oldClipData = fGpu->getClip();
732
robertphillips@google.com7b112892012-07-31 15:18:21 +0000733 // The origin of 'newClipData' is (0, 0) so it is okay to place
734 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000735 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000736 GrClipData newClipData;
737 newClipData.fClipStack = &newClipStack;
738
739 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000741 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
742 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000744 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000746 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000747 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000748 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000749 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000750 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000751 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000752 }
753
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754#if !VISUALIZE_COMPLEX_CLIP
755 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
756#endif
757
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 int clipBit = stencilBuffer->bits();
759 SkASSERT((clipBit <= 16) &&
760 "Ganesh only handles 16b or smaller stencil buffers");
761 clipBit = (1 << (clipBit-1));
762
robertphillips@google.com7b112892012-07-31 15:18:21 +0000763 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764
765 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000766 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
767
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000768 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000769 SkClipStack::Iter::kBottom_IterStart);
770 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000771 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000772 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000773 &firstOp,
774 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775
robertphillips@google.com7b112892012-07-31 15:18:21 +0000776 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000777 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778
779 // walk through each clip element and perform its set op
780 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000781 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000783 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000784 // enabled at bottom of loop
785 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000786 // if the target is MSAA then we want MSAA enabled when the clip is soft
787 if (rt->isMultisampled()) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000788 drawState->setState(GrDrawState::kHWAntialias_StateBit, clip->fDoAA);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000789 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790
tomhudson@google.com8afae612012-08-14 15:03:35 +0000791 // Can the clip element be drawn directly to the stencil buffer
792 // with a non-inverted fill rule without extra passes to
793 // resolve in/out status?
794 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000795
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000796 SkRegion::Op op = clip->fOp;
797 if (first) {
798 first = false;
799 op = firstOp;
800 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000801
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000802 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000803 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000804 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000805 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000806 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 fillInverted = false;
808 // there is no point in intersecting a screen filling
809 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000810 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000811 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 continue;
813 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000814 } else {
815 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000816 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000817 fillInverted = GrIsFillInverted(fill);
818 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000819 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000820 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000821 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000822 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000823 return false;
824 }
825 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000826 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000827 }
828
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000829 int passes;
830 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
831
832 bool canDrawDirectToClip; // Given the renderer, the element,
833 // fill rule, and set operation can
834 // we render the element directly to
835 // stencil bit used for clipping.
836 canDrawDirectToClip =
837 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000838 canRenderDirectToStencil,
839 clipBit,
840 fillInverted,
841 &passes,
842 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000843
844 // draw the element to the client stencil bits if necessary
845 if (!canDrawDirectToClip) {
846 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
847 kIncClamp_StencilOp,
848 kIncClamp_StencilOp,
849 kAlways_StencilFunc,
850 0xffff,
851 0x0000,
852 0xffff);
853 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000854 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000855 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000856 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000857 } else {
858 if (canRenderDirectToStencil) {
859 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000860 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000861 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000862 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000863 }
864 }
865 }
866
867 // now we modify the clip bit by rendering either the clip
868 // element directly or a bounding rect of the entire clip.
869 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
870 for (int p = 0; p < passes; ++p) {
871 *drawState->stencil() = stencilSettings[p];
872 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000873 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000874 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000875 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000876 } else {
877 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000878 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000879 }
880 } else {
881 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000882 // 'devClipBounds' is already in device coordinates so the
883 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000884 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000885 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000886 GrRect canvClipBounds;
887 canvClipBounds.set(devClipBounds);
888 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
889 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000890 }
891 }
892 }
893 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000894 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000895 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000896 // set this last because recursive draws may overwrite it back to kNone.
897 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
898 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000899 return true;
900}
901
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000902
bsalomon@google.com411dad02012-06-05 20:24:20 +0000903// mapping of clip-respecting stencil funcs to normal stencil funcs
904// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000905static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000906 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
907 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
908 // In the Clip Funcs
909 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
910 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
911 kLess_StencilFunc, // kLessIfInClip_StencilFunc
912 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
913 // Special in the clip func that forces user's ref to be 0.
914 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
915 // make ref 0 and do normal nequal.
916 },
917 {// Stencil-Clipping is ENABLED
918 // In the Clip Funcs
919 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
920 // eq stencil clip bit, mask
921 // out user bits.
922
923 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
924 // add stencil bit to mask and ref
925
926 kLess_StencilFunc, // kLessIfInClip_StencilFunc
927 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
928 // for both of these we can add
929 // the clip bit to the mask and
930 // ref and compare as normal
931 // Special in the clip func that forces user's ref to be 0.
932 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
933 // make ref have only the clip bit set
934 // and make comparison be less
935 // 10..0 < 1..user_bits..
936 }
937};
938
bsalomon@google.coma3201942012-06-21 19:58:20 +0000939namespace {
940// Sets the settings to clip against the stencil buffer clip while ignoring the
941// client bits.
942const GrStencilSettings& basic_apply_stencil_clip_settings() {
943 // stencil settings to use when clip is in stencil
944 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
945 kKeep_StencilOp,
946 kKeep_StencilOp,
947 kAlwaysIfInClip_StencilFunc,
948 0x0000,
949 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000950 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
952}
953}
954
955void GrClipMaskManager::setGpuStencil() {
956 // We make two copies of the StencilSettings here (except in the early
957 // exit scenario. One copy from draw state to the stack var. Then another
958 // from the stack var to the gpu. We could make this class hold a ptr to
959 // GrGpu's fStencilSettings and eliminate the stack copy here.
960
961 const GrDrawState& drawState = fGpu->getDrawState();
962
963 // use stencil for clipping if clipping is enabled and the clip
964 // has been written into the stencil.
965 GrClipMaskManager::StencilClipMode clipMode;
966 if (this->isClipInStencil() && drawState.isClipState()) {
967 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
968 // We can't be modifying the clip and respecting it at the same time.
969 GrAssert(!drawState.isStateFlagEnabled(
970 GrGpu::kModifyStencilClip_StateBit));
971 } else if (drawState.isStateFlagEnabled(
972 GrGpu::kModifyStencilClip_StateBit)) {
973 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
974 } else {
975 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
976 }
977
978 GrStencilSettings settings;
979 // The GrGpu client may not be using the stencil buffer but we may need to
980 // enable it in order to respect a stencil clip.
981 if (drawState.getStencil().isDisabled()) {
982 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
983 settings = basic_apply_stencil_clip_settings();
984 } else {
985 fGpu->disableStencil();
986 return;
987 }
988 } else {
989 settings = drawState.getStencil();
990 }
991
992 // TODO: dynamically attach a stencil buffer
993 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000994 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000995 drawState.getRenderTarget()->getStencilBuffer();
996 if (NULL != stencilBuffer) {
997 stencilBits = stencilBuffer->bits();
998 }
999
bsalomon@google.comf6601872012-08-28 21:11:35 +00001000 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001001 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001002 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001003 this->adjustStencilParams(&settings, clipMode, stencilBits);
1004 fGpu->setStencilSettings(settings);
1005}
1006
1007void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1008 StencilClipMode mode,
1009 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001010 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001011
1012 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001013 // We assume that this clip manager itself is drawing to the GrGpu and
1014 // has already setup the correct values.
1015 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001016 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001017
bsalomon@google.com411dad02012-06-05 20:24:20 +00001018 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1019 unsigned int userBits = clipBit - 1;
1020
bsalomon@google.coma3201942012-06-21 19:58:20 +00001021 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001022 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001023
bsalomon@google.coma3201942012-06-21 19:58:20 +00001024 bool finished = false;
1025 while (!finished) {
1026 GrStencilFunc func = settings->func(face);
1027 uint16_t writeMask = settings->writeMask(face);
1028 uint16_t funcMask = settings->funcMask(face);
1029 uint16_t funcRef = settings->funcRef(face);
1030
1031 GrAssert((unsigned) func < kStencilFuncCount);
1032
1033 writeMask &= userBits;
1034
1035 if (func >= kBasicStencilFuncCount) {
1036 int respectClip = kRespectClip_StencilClipMode == mode;
1037 if (respectClip) {
1038 // The GrGpu class should have checked this
1039 GrAssert(this->isClipInStencil());
1040 switch (func) {
1041 case kAlwaysIfInClip_StencilFunc:
1042 funcMask = clipBit;
1043 funcRef = clipBit;
1044 break;
1045 case kEqualIfInClip_StencilFunc:
1046 case kLessIfInClip_StencilFunc:
1047 case kLEqualIfInClip_StencilFunc:
1048 funcMask = (funcMask & userBits) | clipBit;
1049 funcRef = (funcRef & userBits) | clipBit;
1050 break;
1051 case kNonZeroIfInClip_StencilFunc:
1052 funcMask = (funcMask & userBits) | clipBit;
1053 funcRef = clipBit;
1054 break;
1055 default:
1056 GrCrash("Unknown stencil func");
1057 }
1058 } else {
1059 funcMask &= userBits;
1060 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001061 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001062 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001063 gSpecialToBasicStencilFunc[respectClip];
1064 func = table[func - kBasicStencilFuncCount];
1065 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001066 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001067 funcMask &= userBits;
1068 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001069 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001070
1071 settings->setFunc(face, func);
1072 settings->setWriteMask(face, writeMask);
1073 settings->setFuncMask(face, funcMask);
1074 settings->setFuncRef(face, funcRef);
1075
1076 if (GrStencilSettings::kFront_Face == face) {
1077 face = GrStencilSettings::kBack_Face;
1078 finished = !twoSided;
1079 } else {
1080 finished = true;
1081 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001082 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001083 if (!twoSided) {
1084 settings->copyFrontSettingsToBack();
1085 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001086}
1087
1088////////////////////////////////////////////////////////////////////////////////
1089
robertphillips@google.comfa662942012-05-17 12:20:22 +00001090namespace {
1091
1092GrPathFill invert_fill(GrPathFill fill) {
1093 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001094 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1095 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1096 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1097 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1098 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001099 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001100 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1101 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1102 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1103 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1104 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1105 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001106 return gInvertedFillTable[fill];
1107}
1108
1109}
1110
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001111bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001112 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001113 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001114 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001115
robertphillips@google.com7b112892012-07-31 15:18:21 +00001116 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001117 return true;
1118 }
1119
robertphillips@google.comf105b102012-05-14 12:18:26 +00001120 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001121 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001122 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001123 return false;
1124 }
1125
robertphillips@google.com2c756812012-05-22 20:28:23 +00001126 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001127
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001128 GrMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001129 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001130 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001131 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001132
robertphillips@google.comfa662942012-05-17 12:20:22 +00001133 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001134 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1135
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001136 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001137 SkClipStack::Iter::kBottom_IterStart);
1138 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001139 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001140 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001141 &firstOp,
1142 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001143
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001144 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001145
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001146 bool first = true;
1147 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001148
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001149 SkRegion::Op op = clip->fOp;
1150 if (first) {
1151 first = false;
1152 op = firstOp;
1153 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001154
1155 if (SkRegion::kIntersect_Op == op ||
1156 SkRegion::kReverseDifference_Op == op) {
1157 // Intersect and reverse difference require modifying pixels
1158 // outside of the geometry that is being "drawn". In both cases
1159 // we erase all the pixels outside of the geometry but
1160 // leave the pixels inside the geometry alone. For reverse
1161 // difference we invert all the pixels before clearing the ones
1162 // outside the geometry.
1163 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001164 SkRect temp;
1165 temp.set(*devResultBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001166
1167 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001168 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001169 }
1170
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001171 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001172
1173 // convert the rect to a path so we can invert the fill
1174 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001175 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001176
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001177 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001178 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001179 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001180 } else if (NULL != clip->fPath) {
1181 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001182 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001183 invert_fill(get_path_fill(*clip->fPath)),
1184 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001185 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001186 }
1187
1188 continue;
1189 }
1190
1191 // The other ops (union, xor, diff) only affect pixels inside
1192 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001193 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001194
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001195 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001196 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001197 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001198
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001199 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001200 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001201 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001202 get_path_fill(*clip->fPath),
1203 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001204 }
1205 }
1206
robertphillips@google.comfa662942012-05-17 12:20:22 +00001207 // Because we are using the scratch texture cache, "accum" may be
1208 // larger than expected and have some cruft in the areas we aren't using.
1209 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001210 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001211
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001212 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001213
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001214 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001215
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001216 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001217 return true;
1218}
1219
robertphillips@google.comf294b772012-04-27 14:29:26 +00001220////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001221void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001222 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001223}