blob: ff473069dfcad219808102a0c27e5a1439d73089 [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.coma72eef32012-05-01 17:22:59 +000019
20//#define GR_AA_CLIP 1
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000021//#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000022
robertphillips@google.comf8d904a2012-07-31 12:18:16 +000023GrClipMaskCache::GrClipMaskCache()
24 : fContext(NULL)
25 , fStack(sizeof(GrClipStackFrame)) {
26 // We need an initial frame to capture the clip state prior to
27 // any pushes
28 SkNEW_PLACEMENT(fStack.push_back(), GrClipStackFrame);
29}
30
31void GrClipMaskCache::push() {
32 SkNEW_PLACEMENT(fStack.push_back(), GrClipStackFrame);
33}
34
robertphillips@google.comf294b772012-04-27 14:29:26 +000035////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000036namespace {
37// set up the draw state to enable the aa clipping mask. Besides setting up the
38// sampler matrix this also alters the vertex layout
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000039void setup_drawstate_aaclip(GrGpu* gpu,
40 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000041 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042 GrDrawState* drawState = gpu->drawState();
43 GrAssert(drawState);
44
45 static const int maskStage = GrPaint::kTotalStages+1;
46
47 GrMatrix mat;
48 mat.setIDiv(result->width(), result->height());
robertphillips@google.com7b112892012-07-31 15:18:21 +000049 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
50 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000051 mat.preConcat(drawState->getViewMatrix());
52
bsalomon@google.comb8670992012-07-25 21:27:09 +000053 drawState->sampler(maskStage)->reset(mat);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000054
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000055 drawState->createTextureEffect(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000056}
57
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000058bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000059 GrGpu* gpu,
60 const SkPath& path,
61 GrPathFill fill,
62 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000063 // last (false) parameter disallows use of the SW path renderer
64 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
65}
66
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000067GrPathFill get_path_fill(const SkPath& path) {
68 switch (path.getFillType()) {
69 case SkPath::kWinding_FillType:
70 return kWinding_GrPathFill;
71 case SkPath::kEvenOdd_FillType:
72 return kEvenOdd_GrPathFill;
73 case SkPath::kInverseWinding_FillType:
74 return kInverseWinding_GrPathFill;
75 case SkPath::kInverseEvenOdd_FillType:
76 return kInverseEvenOdd_GrPathFill;
77 default:
78 GrCrash("Unsupported path fill in clip.");
79 return kWinding_GrPathFill; // suppress warning
80 }
81}
82
robertphillips@google.comb99225c2012-07-24 18:20:10 +000083/**
84 * Does any individual clip in 'clipIn' use anti-aliasing?
85 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000086bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000087
robertphillips@google.com641f8b12012-07-31 19:15:58 +000088 SkClipStack::Iter iter;
89 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000090
robertphillips@google.com641f8b12012-07-31 19:15:58 +000091 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comb99225c2012-07-24 18:20:10 +000092 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
93 NULL != clip;
94 clip = iter.next()) {
95
96 if (clip->fDoAA) {
97 return true;
98 }
99 }
100
101 return false;
102}
103
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000104}
105
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106/*
107 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
108 * will be used on any element. If so, it returns true to indicate that the
109 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
110 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000111bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000112
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000113 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000114 // a clip gets complex enough it can just be done in SW regardless
115 // of whether it would invoke the GrSoftwarePathRenderer.
116 bool useSW = false;
117
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000118 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
119 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000120
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000121 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
122 NULL != clip;
123 clip = iter.next()) {
124
125 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000126 // Everything before a replace op can be ignored so start
127 // afresh w.r.t. determining if any element uses the SW path
128 useSW = false;
129 }
130
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000131 // rects can always be drawn directly w/o using the software path
132 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000133 if (NULL != clip->fPath &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000134 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000135 *clip->fPath,
136 get_path_fill(*clip->fPath),
137 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000138 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000139 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000140 }
141
142 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000143}
144
robertphillips@google.comf294b772012-04-27 14:29:26 +0000145////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000146// sort out what kind of clip mask needs to be created: alpha, stencil,
147// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000148bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000149 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000150
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000151 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000152 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000153 fGpu->disableScissor();
154 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000155 return true;
156 }
157
158 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000159 // GrDrawTarget should have filtered this for us
160 GrAssert(NULL != rt);
161
robertphillips@google.com7b112892012-07-31 15:18:21 +0000162 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000163 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000164
robertphillips@google.com7b112892012-07-31 15:18:21 +0000165 clipDataIn->getConservativeBounds(rt, &devClipBounds,
166 &isIntersectionOfRects);
167 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000168 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000169 }
170
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000171 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000172
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000173#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000174 // If MSAA is enabled we can do everything in the stencil buffer.
175 // Otherwise check if we should just create the entire clip mask
176 // in software (this will only happen if the clip mask is anti-aliased
177 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000178 if (0 == rt->numSamples() &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000179 requiresAA &&
180 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000181 // The clip geometry is complex enough that it will be more
182 // efficient to create it entirely in software
183 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000184 GrIRect devBound;
185 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
186 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000187 fGpu->disableScissor();
188 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000189 return true;
190 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000191
192 // if SW clip mask creation fails fall through to the other
193 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000194 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000195#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000196
robertphillips@google.comf294b772012-04-27 14:29:26 +0000197#if GR_AA_CLIP
198 // If MSAA is enabled use the (faster) stencil path for AA clipping
199 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000200 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000201 // Since we are going to create a destination texture of the correct
202 // size for the mask (rather than being bound by the size of the
203 // render target) we aren't going to use scissoring like the stencil
204 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000205 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000206 GrIRect devBound;
207 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
208 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000209 fGpu->disableScissor();
210 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000211 return true;
212 }
213
214 // if alpha clip mask creation fails fall through to the stencil
215 // buffer method
216 }
217#endif // GR_AA_CLIP
218
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000219 // Either a hard (stencil buffer) clip was explicitly requested or
220 // an antialiased clip couldn't be created. In either case, free up
221 // the texture in the antialiased mask cache.
222 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000223 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
224 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000225 // AA cache.
226 fAACache.reset();
227
bsalomon@google.coma3201942012-06-21 19:58:20 +0000228 // If the clip is a rectangle then just set the scissor. Otherwise, create
229 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000230 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000231 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000232 this->setGpuStencil();
233 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000234 }
235
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000236 // use the stencil clip if we can't represent the clip as a rectangle.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000237 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
238 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000239
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000240 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000241 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000242 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000243 // This must occur after createStencilClipMask. That function may change
244 // the scissor. Also, it only guarantees that the stencil mask is correct
245 // within the bounds it was passed, so we must use both stencil and scissor
246 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000247 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000248 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000249 return true;
250}
251
252#define VISUALIZE_COMPLEX_CLIP 0
253
254#if VISUALIZE_COMPLEX_CLIP
255 #include "GrRandom.h"
256 GrRandom gRandom;
257 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
258#else
259 #define SET_RANDOM_COLOR
260#endif
261
262namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000263/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000264 * Does "canvContainer" contain "devContainee"? If either is empty then
265 * no containment is possible. "canvContainer" is in canvas coordinates while
266 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000267 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000268 */
robertphillips@google.com7b112892012-07-31 15:18:21 +0000269bool contains(const SkRect& canvContainer,
270 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000271 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000272 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
273 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
274 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
275 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
276 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000277}
278
robertphillips@google.comf294b772012-04-27 14:29:26 +0000279////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000280// determines how many elements at the head of the clip can be skipped and
281// whether the initial clear should be to the inside- or outside-the-clip value,
282// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000283const SkClipStack::Iter::Clip* process_initial_clip_elements(
284 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000285 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000286 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000287 SkRegion::Op* firstOp,
288 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000289
290 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291
292 // logically before the first element of the clip stack is
293 // processed the clip is entirely open. However, depending on the
294 // first set op we may prefer to clear to 0 for performance. We may
295 // also be able to skip the initial clip paths/rects. We loop until
296 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000297 bool done = false;
298 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000299
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000300 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000301
302 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
303 NULL != clip && !done;
304 clip = iter->next()) {
305 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000306 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000307 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000308 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000309 *clearToInside = false;
310 done = true;
311 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000312 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000313 // if this element contains the entire bounds then we
314 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000315 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000316 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000317 break;
318 }
319 // if everything is initially clearToInside then intersect is
320 // same as clear to 0 and treat as a replace. Otherwise,
321 // set stays empty.
322 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000323 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000324 *clearToInside = false;
325 done = true;
326 }
327 break;
328 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000329 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000330 // if everything is initially outside then union is
331 // same as replace. Otherwise, every pixel is still
332 // clearToInside
333 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000334 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 done = true;
336 }
337 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000338 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 // xor is same as difference or replace both of which
340 // can be 1-pass instead of 2 for xor.
341 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000342 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000343 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000344 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345 }
346 done = true;
347 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000348 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000349 // if all pixels are clearToInside then we have to process the
350 // difference, otherwise it has no effect and all pixels
351 // remain outside.
352 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000353 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000354 done = true;
355 }
356 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000357 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000358 // if all pixels are clearToInside then reverse difference
359 // produces empty set. Otherise it is same as replace
360 if (*clearToInside) {
361 *clearToInside = false;
362 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000363 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000364 done = true;
365 }
366 break;
367 default:
368 GrCrash("Unknown set op.");
369 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000370
371 if (done) {
372 // we need to break out here (rather than letting the test in
373 // the loop do it) since backing up the iterator is very expensive
374 break;
375 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000376 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000377 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000378}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000379
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000380}
381
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382namespace {
383
384////////////////////////////////////////////////////////////////////////////////
385// set up the OpenGL blend function to perform the specified
386// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000387void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388
389 switch (op) {
390 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000391 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392 break;
393 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000394 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000395 break;
396 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000397 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000398 break;
399 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000400 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000401 break;
402 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000403 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000404 break;
405 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000406 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000407 break;
408 default:
409 GrAssert(false);
410 break;
411 }
412}
413
robertphillips@google.comf294b772012-04-27 14:29:26 +0000414////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000415bool draw_path_in_software(GrContext* context,
416 GrGpu* gpu,
417 const SkPath& path,
418 GrPathFill fill,
419 bool doAA,
420 const GrIRect& resultBounds) {
421
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000422 SkAutoTUnref<GrTexture> texture(
423 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
424 resultBounds, fill,
425 doAA, NULL));
426 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000427 return false;
428 }
429
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000430 // The ClipMaskManager accumulates the clip mask in the UL corner
431 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000432
bsalomon@google.come3d32162012-07-20 13:37:06 +0000433 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000434
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000435 GrAssert(!GrIsFillInverted(fill));
436 return true;
437}
438
439
440////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000441bool draw_path(GrContext* context,
442 GrGpu* gpu,
443 const SkPath& path,
444 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000445 bool doAA,
446 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000448 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000449 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000450 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000451 }
452
bsalomon@google.come3d32162012-07-20 13:37:06 +0000453 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000454 return true;
455}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000456
robertphillips@google.com7b112892012-07-31 15:18:21 +0000457// 'rect' enters in device coordinates and leaves in canvas coordinates
458void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
459 GrAssert(NULL != rect);
460
461 rect->fLeft += SkIntToScalar(origin.fX);
462 rect->fTop += SkIntToScalar(origin.fY);
463 rect->fRight += SkIntToScalar(origin.fX);
464 rect->fBottom += SkIntToScalar(origin.fY);
465}
466
robertphillips@google.com72176b22012-05-23 13:19:12 +0000467}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000468
469////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000470bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000471 const SkClipStack::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000472 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000473 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000474 GrAssert(NULL != drawState);
475
476 drawState->setRenderTarget(target->asRenderTarget());
477
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000478 if (NULL != clip->fRect) {
479 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000480 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000481 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000482 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000483 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000484 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000485 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000486 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000487 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000488 *clip->fPath,
489 get_path_fill(*clip->fPath),
490 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000491 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000492 }
493 return true;
494}
495
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000496void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000498 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000499 GrAssert(NULL != drawState);
500
501 // no AA here since it is encoded in the texture
502 drawState->setRenderTarget(target->asRenderTarget());
503
504 GrMatrix sampleM;
505 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000506
bsalomon@google.comb8670992012-07-25 21:27:09 +0000507 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000508 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000509
robertphillips@google.comf105b102012-05-14 12:18:26 +0000510 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
511 SkIntToScalar(target->height()));
512
bsalomon@google.come3d32162012-07-20 13:37:06 +0000513 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000514
tomhudson@google.com676e6602012-07-10 17:21:48 +0000515 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000516}
517
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000518// get a texture to act as a temporary buffer for AA clip boolean operations
519// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000520void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000521 GrAutoScratchTexture* temp) {
522 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000523 // we've already allocated the temp texture
524 return;
525 }
526
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000527 GrTextureDesc desc;
528 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
529 desc.fWidth = bounds.width();
530 desc.fHeight = bounds.height();
531 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000532
robertphillips@google.com2c756812012-05-22 20:28:23 +0000533 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000534}
535
robertphillips@google.comf105b102012-05-14 12:18:26 +0000536
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000537void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000538 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000539 // Since we are setting up the cache we know the last lookup was a miss
540 // Free up the currently cached mask so it can be reused
541 fAACache.reset();
542
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000543 GrTextureDesc desc;
544 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
545 desc.fWidth = bounds.width();
546 desc.fHeight = bounds.height();
547 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000548
549 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000550}
551
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000552////////////////////////////////////////////////////////////////////////////////
553// Shared preamble between gpu and SW-only AA clip mask creation paths.
554// Handles caching, determination of clip mask bound & allocation (if needed)
555// of the result texture
556// 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 +0000557bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000558 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000559 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000560 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561 GrAssert(origDrawState->isClipState());
562
563 GrRenderTarget* rt = origDrawState->getRenderTarget();
564 GrAssert(NULL != rt);
565
robertphillips@google.comf294b772012-04-27 14:29:26 +0000566 // unlike the stencil path the alpha path is not bound to the size of the
567 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000568 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com7b112892012-07-31 15:18:21 +0000569 GrIRect devClipBounds;
570 clipDataIn.getConservativeBounds(rt, &devClipBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000571
572 // need to outset a pixel since the standard bounding box computation
573 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com7b112892012-07-31 15:18:21 +0000574 devClipBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000575
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000576 // TODO: make sure we don't outset if bounds are still 0,0 @ min
577
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000578 if (fAACache.canReuse(*clipDataIn.fClipStack,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000579 devClipBounds.width(),
580 devClipBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000581 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000582 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000583 return true;
584 }
585
robertphillips@google.com7b112892012-07-31 15:18:21 +0000586 this->setupCache(*clipDataIn.fClipStack, devClipBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000587
robertphillips@google.com7b112892012-07-31 15:18:21 +0000588 *devResultBounds = devClipBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000589 return false;
590}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000591
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000592////////////////////////////////////////////////////////////////////////////////
593// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000594bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000595 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000596 GrIRect *devResultBounds) {
597 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000598 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
599
robertphillips@google.com7b112892012-07-31 15:18:21 +0000600 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000601 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000602 return true;
603 }
604
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000605 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
606
robertphillips@google.comf105b102012-05-14 12:18:26 +0000607 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000608 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000609 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000610 return false;
611 }
612
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000613 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
614 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000615
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000616 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000617
robertphillips@google.com7b112892012-07-31 15:18:21 +0000618 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000619 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620 // if we were able to trim down the size of the mask we need to
621 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000622 drawState->viewMatrix()->setTranslate(
robertphillips@google.com7b112892012-07-31 15:18:21 +0000623 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
624 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000625 }
626
627 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000628 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
629
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000630 SkClipStack::Iter iter(*clipDataIn.fClipStack,
631 SkClipStack::Iter::kBottom_IterStart);
632 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000633 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000634 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000635 &firstOp,
636 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000637
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000638 fGpu->clear(NULL,
639 clearToInside ? 0xffffffff : 0x00000000,
640 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000641
robertphillips@google.comf105b102012-05-14 12:18:26 +0000642 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000643 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000644 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000645 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000646
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000647 SkRegion::Op op = clip->fOp;
648 if (first) {
649 first = false;
650 op = firstOp;
651 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000652
653 if (SkRegion::kReplace_Op == op) {
654 // TODO: replace is actually a lot faster then intersection
655 // for this path - refactor the stencil path so it can handle
656 // replace ops and alter GrClip to allow them through
657
658 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000659 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000660
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000661 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000662 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000663
664 } else if (SkRegion::kReverseDifference_Op == op ||
665 SkRegion::kIntersect_Op == op) {
666 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000667 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000668 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000669 continue;
670 }
671
robertphillips@google.com7b112892012-07-31 15:18:21 +0000672 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000673 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000674 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000675 return false;
676 }
677
robertphillips@google.comf294b772012-04-27 14:29:26 +0000678 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000679 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000680
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000681 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000682 this->drawClipShape(temp.texture(), clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000683
684 // TODO: rather than adding these two translations here
685 // compute the bounding box needed to render the texture
686 // into temp
robertphillips@google.com7b112892012-07-31 15:18:21 +0000687 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000688 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
689 // In order for the merge of the temp clip into the accumulator
690 // to work we need to disable the translation
691 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000692 }
693
694 // Now draw into the accumulator using the real operation
695 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000696 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000697 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000698
robertphillips@google.com7b112892012-07-31 15:18:21 +0000699 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000700 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
701 drawState->viewMatrix()->setTranslate(
robertphillips@google.com7b112892012-07-31 15:18:21 +0000702 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
703 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000704 }
705
706 } else {
707 // all the remaining ops can just be directly draw into
708 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000709 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000710 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000711 }
712 }
713
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000714 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000715 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000716 return true;
717}
718
719////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com7b112892012-07-31 15:18:21 +0000720// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000721// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000722bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000723 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000725 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000727 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728 GrAssert(drawState->isClipState());
729
730 GrRenderTarget* rt = drawState->getRenderTarget();
731 GrAssert(NULL != rt);
732
733 // TODO: dynamically attach a SB when needed.
734 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
735 if (NULL == stencilBuffer) {
736 return false;
737 }
738
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000739 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000741 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742
743 // we set the current clip to the bounds so that our recursive
744 // draws are scissored to them. We use the copy of the complex clip
745 // we just stashed on the SB to render from. We set it back after
746 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000747 const GrClipData* oldClipData = fGpu->getClip();
748
robertphillips@google.com7b112892012-07-31 15:18:21 +0000749 // The origin of 'newClipData' is (0, 0) so it is okay to place
750 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000751 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000752 GrClipData newClipData;
753 newClipData.fClipStack = &newClipStack;
754
755 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000757 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
758 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000760 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000761
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000762 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
763 // Add the saveLayer's offset to the view matrix rather than
764 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000765 drawState->viewMatrix()->setTranslate(
766 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000767 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000768 }
769
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000770#if !VISUALIZE_COMPLEX_CLIP
771 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
772#endif
773
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774 int clipBit = stencilBuffer->bits();
775 SkASSERT((clipBit <= 16) &&
776 "Ganesh only handles 16b or smaller stencil buffers");
777 clipBit = (1 << (clipBit-1));
778
robertphillips@google.com7b112892012-07-31 15:18:21 +0000779 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780
781 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000782 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
783
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000784 SkClipStack::Iter iter(*oldClipData->fClipStack,
785 SkClipStack::Iter::kBottom_IterStart);
786 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000787 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000788 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000789 &firstOp,
790 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791
robertphillips@google.com7b112892012-07-31 15:18:21 +0000792 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000793 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000794
795 // walk through each clip element and perform its set op
796 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000797 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798 GrPathFill fill;
799 bool fillInverted;
800 // enabled at bottom of loop
801 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000802 // if the target is MSAA then we want MSAA enabled when the clip is soft
803 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000804 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000805 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
806 } else {
807 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
808 }
809 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000810
811 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000812 // directly to the stencil buffer
813 // with a non-inverted fill rule
814 // without extra passes to
815 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000816
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000817 SkRegion::Op op = clip->fOp;
818 if (first) {
819 first = false;
820 op = firstOp;
821 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000822
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000823 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000824 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000825 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000826 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000827 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000828 fillInverted = false;
829 // there is no point in intersecting a screen filling
830 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000831 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000832 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000833 continue;
834 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000835 } else if (NULL != clip->fPath) {
836 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000837 fillInverted = GrIsFillInverted(fill);
838 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000839 clipPath = clip->fPath;
robertphillips@google.com2c756812012-05-22 20:28:23 +0000840 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000841 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000842 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000843 if (NULL == pr) {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000844 fGpu->setClip(oldClipData); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000845 return false;
846 }
847 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000848 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000849 }
850
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000851 int passes;
852 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
853
854 bool canDrawDirectToClip; // Given the renderer, the element,
855 // fill rule, and set operation can
856 // we render the element directly to
857 // stencil bit used for clipping.
858 canDrawDirectToClip =
859 GrStencilSettings::GetClipPasses(op,
860 canRenderDirectToStencil,
861 clipBit,
862 fillInverted,
863 &passes, stencilSettings);
864
865 // draw the element to the client stencil bits if necessary
866 if (!canDrawDirectToClip) {
867 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
868 kIncClamp_StencilOp,
869 kIncClamp_StencilOp,
870 kAlways_StencilFunc,
871 0xffff,
872 0x0000,
873 0xffff);
874 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000875 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000876 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000877 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000878 } else {
879 if (canRenderDirectToStencil) {
880 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000881 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000882 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000883 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000884 }
885 }
886 }
887
888 // now we modify the clip bit by rendering either the clip
889 // element directly or a bounding rect of the entire clip.
890 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
891 for (int p = 0; p < passes; ++p) {
892 *drawState->stencil() = stencilSettings[p];
893 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000894 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000895 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000896 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000897 } else {
898 SET_RANDOM_COLOR
bsalomon@google.come3d32162012-07-20 13:37:06 +0000899 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000900 }
901 } else {
902 SET_RANDOM_COLOR
robertphillips@google.com7b112892012-07-31 15:18:21 +0000903 // 'devClipBounds' is already in device coordinates so the
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000904 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000905 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000906 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000907 GrRect canvClipBounds;
908 canvClipBounds.set(devClipBounds);
909 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
910 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000911 }
912 }
913 }
914 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000915 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000916 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000917 // set this last because recursive draws may overwrite it back to kNone.
918 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
919 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000920 return true;
921}
922
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000923
bsalomon@google.com411dad02012-06-05 20:24:20 +0000924// mapping of clip-respecting stencil funcs to normal stencil funcs
925// mapping depends on whether stencil-clipping is in effect.
926static const GrStencilFunc
927 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
928 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
929 // In the Clip Funcs
930 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
931 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
932 kLess_StencilFunc, // kLessIfInClip_StencilFunc
933 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
934 // Special in the clip func that forces user's ref to be 0.
935 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
936 // make ref 0 and do normal nequal.
937 },
938 {// Stencil-Clipping is ENABLED
939 // In the Clip Funcs
940 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
941 // eq stencil clip bit, mask
942 // out user bits.
943
944 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
945 // add stencil bit to mask and ref
946
947 kLess_StencilFunc, // kLessIfInClip_StencilFunc
948 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
949 // for both of these we can add
950 // the clip bit to the mask and
951 // ref and compare as normal
952 // Special in the clip func that forces user's ref to be 0.
953 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
954 // make ref have only the clip bit set
955 // and make comparison be less
956 // 10..0 < 1..user_bits..
957 }
958};
959
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960namespace {
961// Sets the settings to clip against the stencil buffer clip while ignoring the
962// client bits.
963const GrStencilSettings& basic_apply_stencil_clip_settings() {
964 // stencil settings to use when clip is in stencil
965 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
966 kKeep_StencilOp,
967 kKeep_StencilOp,
968 kAlwaysIfInClip_StencilFunc,
969 0x0000,
970 0x0000,
971 0x0000);
972 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
973}
974}
975
976void GrClipMaskManager::setGpuStencil() {
977 // We make two copies of the StencilSettings here (except in the early
978 // exit scenario. One copy from draw state to the stack var. Then another
979 // from the stack var to the gpu. We could make this class hold a ptr to
980 // GrGpu's fStencilSettings and eliminate the stack copy here.
981
982 const GrDrawState& drawState = fGpu->getDrawState();
983
984 // use stencil for clipping if clipping is enabled and the clip
985 // has been written into the stencil.
986 GrClipMaskManager::StencilClipMode clipMode;
987 if (this->isClipInStencil() && drawState.isClipState()) {
988 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
989 // We can't be modifying the clip and respecting it at the same time.
990 GrAssert(!drawState.isStateFlagEnabled(
991 GrGpu::kModifyStencilClip_StateBit));
992 } else if (drawState.isStateFlagEnabled(
993 GrGpu::kModifyStencilClip_StateBit)) {
994 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
995 } else {
996 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
997 }
998
999 GrStencilSettings settings;
1000 // The GrGpu client may not be using the stencil buffer but we may need to
1001 // enable it in order to respect a stencil clip.
1002 if (drawState.getStencil().isDisabled()) {
1003 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1004 settings = basic_apply_stencil_clip_settings();
1005 } else {
1006 fGpu->disableStencil();
1007 return;
1008 }
1009 } else {
1010 settings = drawState.getStencil();
1011 }
1012
1013 // TODO: dynamically attach a stencil buffer
1014 int stencilBits = 0;
1015 GrStencilBuffer* stencilBuffer =
1016 drawState.getRenderTarget()->getStencilBuffer();
1017 if (NULL != stencilBuffer) {
1018 stencilBits = stencilBuffer->bits();
1019 }
1020
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001021 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
1022 !settings.usesWrapOp());
1023 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001024 this->adjustStencilParams(&settings, clipMode, stencilBits);
1025 fGpu->setStencilSettings(settings);
1026}
1027
1028void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1029 StencilClipMode mode,
1030 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001031 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001032
1033 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001034 // We assume that this clip manager itself is drawing to the GrGpu and
1035 // has already setup the correct values.
1036 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001037 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001038
bsalomon@google.com411dad02012-06-05 20:24:20 +00001039 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1040 unsigned int userBits = clipBit - 1;
1041
bsalomon@google.coma3201942012-06-21 19:58:20 +00001042 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
1043 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001044
bsalomon@google.coma3201942012-06-21 19:58:20 +00001045 bool finished = false;
1046 while (!finished) {
1047 GrStencilFunc func = settings->func(face);
1048 uint16_t writeMask = settings->writeMask(face);
1049 uint16_t funcMask = settings->funcMask(face);
1050 uint16_t funcRef = settings->funcRef(face);
1051
1052 GrAssert((unsigned) func < kStencilFuncCount);
1053
1054 writeMask &= userBits;
1055
1056 if (func >= kBasicStencilFuncCount) {
1057 int respectClip = kRespectClip_StencilClipMode == mode;
1058 if (respectClip) {
1059 // The GrGpu class should have checked this
1060 GrAssert(this->isClipInStencil());
1061 switch (func) {
1062 case kAlwaysIfInClip_StencilFunc:
1063 funcMask = clipBit;
1064 funcRef = clipBit;
1065 break;
1066 case kEqualIfInClip_StencilFunc:
1067 case kLessIfInClip_StencilFunc:
1068 case kLEqualIfInClip_StencilFunc:
1069 funcMask = (funcMask & userBits) | clipBit;
1070 funcRef = (funcRef & userBits) | clipBit;
1071 break;
1072 case kNonZeroIfInClip_StencilFunc:
1073 funcMask = (funcMask & userBits) | clipBit;
1074 funcRef = clipBit;
1075 break;
1076 default:
1077 GrCrash("Unknown stencil func");
1078 }
1079 } else {
1080 funcMask &= userBits;
1081 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001082 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001083 const GrStencilFunc* table =
1084 gSpecialToBasicStencilFunc[respectClip];
1085 func = table[func - kBasicStencilFuncCount];
1086 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001087 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001088 funcMask &= userBits;
1089 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001090 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001091
1092 settings->setFunc(face, func);
1093 settings->setWriteMask(face, writeMask);
1094 settings->setFuncMask(face, funcMask);
1095 settings->setFuncRef(face, funcRef);
1096
1097 if (GrStencilSettings::kFront_Face == face) {
1098 face = GrStencilSettings::kBack_Face;
1099 finished = !twoSided;
1100 } else {
1101 finished = true;
1102 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001103 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001104 if (!twoSided) {
1105 settings->copyFrontSettingsToBack();
1106 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001107}
1108
1109////////////////////////////////////////////////////////////////////////////////
1110
robertphillips@google.comfa662942012-05-17 12:20:22 +00001111namespace {
1112
1113GrPathFill invert_fill(GrPathFill fill) {
1114 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001115 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1116 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1117 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1118 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1119 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001120 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001121 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1122 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1123 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1124 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1125 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1126 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001127 return gInvertedFillTable[fill];
1128}
1129
1130}
1131
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001132bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001133 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001134 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001135 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001136
robertphillips@google.com7b112892012-07-31 15:18:21 +00001137 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001138 return true;
1139 }
1140
robertphillips@google.comf105b102012-05-14 12:18:26 +00001141 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001142 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001143 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001144 return false;
1145 }
1146
robertphillips@google.com2c756812012-05-22 20:28:23 +00001147 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001148
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001149 GrMatrix matrix;
1150 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
1151 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001152 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001153
robertphillips@google.comfa662942012-05-17 12:20:22 +00001154 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001155 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1156
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001157 SkClipStack::Iter iter(*clipDataIn.fClipStack,
1158 SkClipStack::Iter::kBottom_IterStart);
1159 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001160 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001161 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001162 &firstOp,
1163 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001164
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001165 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001166
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001167 bool first = true;
1168 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001169
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001170 SkRegion::Op op = clip->fOp;
1171 if (first) {
1172 first = false;
1173 op = firstOp;
1174 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001175
1176 if (SkRegion::kIntersect_Op == op ||
1177 SkRegion::kReverseDifference_Op == op) {
1178 // Intersect and reverse difference require modifying pixels
1179 // outside of the geometry that is being "drawn". In both cases
1180 // we erase all the pixels outside of the geometry but
1181 // leave the pixels inside the geometry alone. For reverse
1182 // difference we invert all the pixels before clearing the ones
1183 // outside the geometry.
1184 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001185 SkRect temp;
1186 temp.set(*devResultBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001187
1188 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001189 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001190 }
1191
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001192 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001193
1194 // convert the rect to a path so we can invert the fill
1195 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001196 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001197
1198 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001199 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001200 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001201 } else if (NULL != clip->fPath) {
1202 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001203 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001204 invert_fill(get_path_fill(*clip->fPath)),
1205 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001206 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001207 }
1208
1209 continue;
1210 }
1211
1212 // The other ops (union, xor, diff) only affect pixels inside
1213 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001214 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001215
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001216 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001217 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001218 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001219
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001220 } else if (NULL != clip->fPath) {
1221 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001222 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001223 get_path_fill(*clip->fPath),
1224 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001225 }
1226 }
1227
robertphillips@google.comfa662942012-05-17 12:20:22 +00001228 // Because we are using the scratch texture cache, "accum" may be
1229 // larger than expected and have some cruft in the areas we aren't using.
1230 // Clear it out.
1231
1232 // TODO: need a simpler way to clear the texture - can we combine
1233 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001234 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001235 GrAssert(NULL != drawState);
1236 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001237 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001238 // can't leave the accum bound as a rendertarget
1239 drawState->setRenderTarget(temp);
1240
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001241 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001242
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001243 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001244
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001245 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001246 return true;
1247}
1248
robertphillips@google.comf294b772012-04-27 14:29:26 +00001249////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001250void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001251 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001252}