blob: 97c102f66b573055db44258379a7da75313531c9 [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.com6623fcd2012-05-15 16:47:23 +000041 const GrIRect &bound) {
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.com6623fcd2012-05-15 16:47:23 +000049 mat.preTranslate(SkIntToScalar(-bound.fLeft), SkIntToScalar(-bound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000050 mat.preConcat(drawState->getViewMatrix());
51
bsalomon@google.comb8670992012-07-25 21:27:09 +000052 drawState->sampler(maskStage)->reset(mat);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000053
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000054 drawState->createTextureEffect(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000055}
56
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000057bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000058 GrGpu* gpu,
59 const SkPath& path,
60 GrPathFill fill,
61 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000062 // last (false) parameter disallows use of the SW path renderer
63 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
64}
65
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000066GrPathFill get_path_fill(const SkPath& path) {
67 switch (path.getFillType()) {
68 case SkPath::kWinding_FillType:
69 return kWinding_GrPathFill;
70 case SkPath::kEvenOdd_FillType:
71 return kEvenOdd_GrPathFill;
72 case SkPath::kInverseWinding_FillType:
73 return kInverseWinding_GrPathFill;
74 case SkPath::kInverseEvenOdd_FillType:
75 return kInverseEvenOdd_GrPathFill;
76 default:
77 GrCrash("Unsupported path fill in clip.");
78 return kWinding_GrPathFill; // suppress warning
79 }
80}
81
robertphillips@google.comb99225c2012-07-24 18:20:10 +000082/**
83 * Does any individual clip in 'clipIn' use anti-aliasing?
84 */
85bool requires_AA(const GrClip& clipIn) {
86
87 GrClip::Iter iter;
88 iter.reset(clipIn, GrClip::Iter::kBottom_IterStart);
89
90 const GrClip::Iter::Clip* clip = NULL;
91 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
92 NULL != clip;
93 clip = iter.next()) {
94
95 if (clip->fDoAA) {
96 return true;
97 }
98 }
99
100 return false;
101}
102
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000103}
104
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105/*
106 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
107 * will be used on any element. If so, it returns true to indicate that the
108 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
109 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000110bool GrClipMaskManager::useSWOnlyPath(const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000111
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000112 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000113 // a clip gets complex enough it can just be done in SW regardless
114 // of whether it would invoke the GrSoftwarePathRenderer.
115 bool useSW = false;
116
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000117 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
118 const GrClip::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000119
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000120 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
121 NULL != clip;
122 clip = iter.next()) {
123
124 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000125 // Everything before a replace op can be ignored so start
126 // afresh w.r.t. determining if any element uses the SW path
127 useSW = false;
128 }
129
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000130 // rects can always be drawn directly w/o using the software path
131 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000132 if (NULL != clip->fPath &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000133 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000134 *clip->fPath,
135 get_path_fill(*clip->fPath),
136 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000137 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000138 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000139 }
140
141 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000142}
143
robertphillips@google.comf294b772012-04-27 14:29:26 +0000144////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000145// sort out what kind of clip mask needs to be created: alpha, stencil,
146// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000147bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000148 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000149
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000150 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000151 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000152 fGpu->disableScissor();
153 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000154 return true;
155 }
156
157 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000158 // GrDrawTarget should have filtered this for us
159 GrAssert(NULL != rt);
160
bsalomon@google.coma3201942012-06-21 19:58:20 +0000161 GrIRect bounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000162 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000163
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000164 clipDataIn->getConservativeBounds(rt, &bounds, &isIntersectionOfRects);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000165 if (bounds.isEmpty()) {
166 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000167 }
168
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000169 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
170 GrAssert(requiresAA == clipDataIn->fClipStack->requiresAA());
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000171
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000172#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000173 // If MSAA is enabled we can do everything in the stencil buffer.
174 // Otherwise check if we should just create the entire clip mask
175 // in software (this will only happen if the clip mask is anti-aliased
176 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000177 if (0 == rt->numSamples() &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000178 requiresAA &&
179 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000180 // The clip geometry is complex enough that it will be more
181 // efficient to create it entirely in software
182 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000183 GrIRect bound;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000184 if (this->createSoftwareClipMask(*clipDataIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000185 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000186 fGpu->disableScissor();
187 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000188 return true;
189 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000190
191 // if SW clip mask creation fails fall through to the other
192 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000193 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000194#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000195
robertphillips@google.comf294b772012-04-27 14:29:26 +0000196#if GR_AA_CLIP
197 // If MSAA is enabled use the (faster) stencil path for AA clipping
198 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000199 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000200 // Since we are going to create a destination texture of the correct
201 // size for the mask (rather than being bound by the size of the
202 // render target) we aren't going to use scissoring like the stencil
203 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000204 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000205 GrIRect bound;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000206 if (this->createAlphaClipMask(*clipDataIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000207 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000208 fGpu->disableScissor();
209 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000210 return true;
211 }
212
213 // if alpha clip mask creation fails fall through to the stencil
214 // buffer method
215 }
216#endif // GR_AA_CLIP
217
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000218 // Either a hard (stencil buffer) clip was explicitly requested or
219 // an antialiased clip couldn't be created. In either case, free up
220 // the texture in the antialiased mask cache.
221 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000222 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
223 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000224 // AA cache.
225 fAACache.reset();
226
bsalomon@google.coma3201942012-06-21 19:58:20 +0000227 // If the clip is a rectangle then just set the scissor. Otherwise, create
228 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000229 if (isIntersectionOfRects) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000230 fGpu->enableScissor(bounds);
231 this->setGpuStencil();
232 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000233 }
234
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000235 // use the stencil clip if we can't represent the clip as a rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000236 bool useStencil = !clipDataIn->fClipStack->isWideOpen() && !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000237
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000238 if (useStencil) {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000239 this->createStencilClipMask(*clipDataIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000240 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000241 // This must occur after createStencilClipMask. That function may change
242 // the scissor. Also, it only guarantees that the stencil mask is correct
243 // within the bounds it was passed, so we must use both stencil and scissor
244 // test to the bounds for the final draw.
245 fGpu->enableScissor(bounds);
246 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000247 return true;
248}
249
250#define VISUALIZE_COMPLEX_CLIP 0
251
252#if VISUALIZE_COMPLEX_CLIP
253 #include "GrRandom.h"
254 GrRandom gRandom;
255 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
256#else
257 #define SET_RANDOM_COLOR
258#endif
259
260namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000261/**
262 * Does "container" contain "containee"? If either is empty then
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000263 * no containment is possible. "container" is in canvas coordinates while
264 * "containee" is in device coordiates. "origin" provides the mapping between
265 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000266 */
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000267bool contains(const SkRect& container,
268 const SkIRect& containee,
269 const SkIPoint& origin) {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000270 return !containee.isEmpty() && !container.isEmpty() &&
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000271 container.fLeft <= SkIntToScalar(containee.fLeft+origin.fX) &&
272 container.fTop <= SkIntToScalar(containee.fTop+origin.fY) &&
273 container.fRight >= SkIntToScalar(containee.fRight+origin.fX) &&
274 container.fBottom >= SkIntToScalar(containee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000275}
276
robertphillips@google.comf294b772012-04-27 14:29:26 +0000277////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000278// determines how many elements at the head of the clip can be skipped and
279// whether the initial clear should be to the inside- or outside-the-clip value,
280// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000281const GrClip::Iter::Clip* process_initial_clip_elements(
282 GrClip::Iter* iter,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000283 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000284 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000285 SkRegion::Op* firstOp,
286 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000287
288 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289
290 // logically before the first element of the clip stack is
291 // processed the clip is entirely open. However, depending on the
292 // first set op we may prefer to clear to 0 for performance. We may
293 // also be able to skip the initial clip paths/rects. We loop until
294 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295 bool done = false;
296 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000297
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000298 const GrClip::Iter::Clip* clip = NULL;
299
300 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
301 NULL != clip && !done;
302 clip = iter->next()) {
303 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000304 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000305 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000306 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000307 *clearToInside = false;
308 done = true;
309 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000310 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000311 // if this element contains the entire bounds then we
312 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000313 if (NULL != clip->fRect &&
314 contains(*clip->fRect, bounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000315 break;
316 }
317 // if everything is initially clearToInside then intersect is
318 // same as clear to 0 and treat as a replace. Otherwise,
319 // set stays empty.
320 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000321 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000322 *clearToInside = false;
323 done = true;
324 }
325 break;
326 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000327 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000328 // if everything is initially outside then union is
329 // same as replace. Otherwise, every pixel is still
330 // clearToInside
331 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000332 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000333 done = true;
334 }
335 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000336 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000337 // xor is same as difference or replace both of which
338 // can be 1-pass instead of 2 for xor.
339 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000340 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000341 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000342 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000343 }
344 done = true;
345 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000346 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000347 // if all pixels are clearToInside then we have to process the
348 // difference, otherwise it has no effect and all pixels
349 // remain outside.
350 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000351 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000352 done = true;
353 }
354 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000355 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000356 // if all pixels are clearToInside then reverse difference
357 // produces empty set. Otherise it is same as replace
358 if (*clearToInside) {
359 *clearToInside = false;
360 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000361 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000362 done = true;
363 }
364 break;
365 default:
366 GrCrash("Unknown set op.");
367 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000368
369 if (done) {
370 // we need to break out here (rather than letting the test in
371 // the loop do it) since backing up the iterator is very expensive
372 break;
373 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000374 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000375 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000376}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000378}
379
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380namespace {
381
382////////////////////////////////////////////////////////////////////////////////
383// set up the OpenGL blend function to perform the specified
384// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000385void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386
387 switch (op) {
388 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000389 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000390 break;
391 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000392 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000393 break;
394 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000395 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000396 break;
397 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000398 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000399 break;
400 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000401 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402 break;
403 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000404 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000405 break;
406 default:
407 GrAssert(false);
408 break;
409 }
410}
411
robertphillips@google.comf294b772012-04-27 14:29:26 +0000412////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000413bool draw_path_in_software(GrContext* context,
414 GrGpu* gpu,
415 const SkPath& path,
416 GrPathFill fill,
417 bool doAA,
418 const GrIRect& resultBounds) {
419
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000420 SkAutoTUnref<GrTexture> texture(
421 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
422 resultBounds, fill,
423 doAA, NULL));
424 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000425 return false;
426 }
427
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000428 // The ClipMaskManager accumulates the clip mask in the UL corner
429 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000430
bsalomon@google.come3d32162012-07-20 13:37:06 +0000431 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000432
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000433 GrAssert(!GrIsFillInverted(fill));
434 return true;
435}
436
437
438////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000439bool draw_path(GrContext* context,
440 GrGpu* gpu,
441 const SkPath& path,
442 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000443 bool doAA,
444 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000445
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000446 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000448 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000449 }
450
bsalomon@google.come3d32162012-07-20 13:37:06 +0000451 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000452 return true;
453}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000454
455}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000456
457////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000458bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000459 const GrClip::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000460 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000461 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000462 GrAssert(NULL != drawState);
463
464 drawState->setRenderTarget(target->asRenderTarget());
465
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000466 if (NULL != clip->fRect) {
467 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000468 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000469 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000470 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000471 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000472 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000473 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000474 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000475 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000476 *clip->fPath,
477 get_path_fill(*clip->fPath),
478 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000479 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000480 }
481 return true;
482}
483
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000484void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000485 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000486 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000487 GrAssert(NULL != drawState);
488
489 // no AA here since it is encoded in the texture
490 drawState->setRenderTarget(target->asRenderTarget());
491
492 GrMatrix sampleM;
493 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000494
bsalomon@google.comb8670992012-07-25 21:27:09 +0000495 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000496 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497
robertphillips@google.comf105b102012-05-14 12:18:26 +0000498 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
499 SkIntToScalar(target->height()));
500
bsalomon@google.come3d32162012-07-20 13:37:06 +0000501 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000502
tomhudson@google.com676e6602012-07-10 17:21:48 +0000503 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000504}
505
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000506// get a texture to act as a temporary buffer for AA clip boolean operations
507// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000508void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000509 GrAutoScratchTexture* temp) {
510 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000511 // we've already allocated the temp texture
512 return;
513 }
514
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000515 GrTextureDesc desc;
516 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
517 desc.fWidth = bounds.width();
518 desc.fHeight = bounds.height();
519 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000520
robertphillips@google.com2c756812012-05-22 20:28:23 +0000521 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000522}
523
robertphillips@google.comf105b102012-05-14 12:18:26 +0000524
525void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000526 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000527 // Since we are setting up the cache we know the last lookup was a miss
528 // Free up the currently cached mask so it can be reused
529 fAACache.reset();
530
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000531 GrTextureDesc desc;
532 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
533 desc.fWidth = bounds.width();
534 desc.fHeight = bounds.height();
535 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000536
537 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000538}
539
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000540////////////////////////////////////////////////////////////////////////////////
541// Shared preamble between gpu and SW-only AA clip mask creation paths.
542// Handles caching, determination of clip mask bound & allocation (if needed)
543// of the result texture
544// 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 +0000545bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000546 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000547 GrIRect* resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000548 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000549 GrAssert(origDrawState->isClipState());
550
551 GrRenderTarget* rt = origDrawState->getRenderTarget();
552 GrAssert(NULL != rt);
553
robertphillips@google.comf294b772012-04-27 14:29:26 +0000554 // unlike the stencil path the alpha path is not bound to the size of the
555 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000556 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000557 GrIRect intBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000558 clipDataIn.getConservativeBounds(rt, &intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559
560 // need to outset a pixel since the standard bounding box computation
561 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000562 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000564 // TODO: make sure we don't outset if bounds are still 0,0 @ min
565
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000566 if (fAACache.canReuse(*clipDataIn.fClipStack,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000567 intBounds.width(),
568 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000569 *result = fAACache.getLastMask();
570 fAACache.getLastBound(resultBounds);
571 return true;
572 }
573
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000574 this->setupCache(*clipDataIn.fClipStack, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000575
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000576 *resultBounds = intBounds;
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.com6623fcd2012-05-15 16:47:23 +0000584 GrIRect *resultBounds) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000585 GrAssert(NULL != resultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000586 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
587
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000588 if (this->clipMaskPreamble(clipDataIn, result, resultBounds)) {
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.comf8d904a2012-07-31 12:18:16 +0000606 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft ||
607 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608 // if we were able to trim down the size of the mask we need to
609 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000610 drawState->viewMatrix()->setTranslate(
611 SkIntToScalar(-resultBounds->fLeft-clipDataIn.fOrigin.fX),
612 SkIntToScalar(-resultBounds->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
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000618 GrClip::Iter iter(*clipDataIn.fClipStack,
619 GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000620 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
621 *resultBounds,
622 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000623 &firstOp,
624 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000625
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000626 fGpu->clear(NULL,
627 clearToInside ? 0xffffffff : 0x00000000,
628 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) {
642 // TODO: replace is actually a lot faster then intersection
643 // for this path - refactor the stencil path so it can handle
644 // replace ops and alter GrClip to allow them through
645
646 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000647 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000649 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000650 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000651
652 } else if (SkRegion::kReverseDifference_Op == op ||
653 SkRegion::kIntersect_Op == op) {
654 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000655 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
656 contains(*clip->fRect, *resultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000657 continue;
658 }
659
robertphillips@google.comf105b102012-05-14 12:18:26 +0000660 getTemp(*resultBounds, &temp);
661 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000662 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000663 return false;
664 }
665
robertphillips@google.comf294b772012-04-27 14:29:26 +0000666 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000667 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000668
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000669 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000670 this->drawClipShape(temp.texture(), clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000671
672 // TODO: rather than adding these two translations here
673 // compute the bounding box needed to render the texture
674 // into temp
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000675 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft ||
676 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
677 // In order for the merge of the temp clip into the accumulator
678 // to work we need to disable the translation
679 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000680 }
681
682 // Now draw into the accumulator using the real operation
683 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000684 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000685 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000686
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000687 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft ||
688 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
689 drawState->viewMatrix()->setTranslate(
690 SkIntToScalar(-resultBounds->fLeft-clipDataIn.fOrigin.fX),
691 SkIntToScalar(-resultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000692 }
693
694 } else {
695 // all the remaining ops can just be directly draw into
696 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000697 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000698 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000699 }
700 }
701
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000702 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000703 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000704 return true;
705}
706
707////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000708// Create a 1-bit clip mask in the stencil buffer. 'bounds' are in device
709// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000710bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000711 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000713 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000715 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716 GrAssert(drawState->isClipState());
717
718 GrRenderTarget* rt = drawState->getRenderTarget();
719 GrAssert(NULL != rt);
720
721 // TODO: dynamically attach a SB when needed.
722 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
723 if (NULL == stencilBuffer) {
724 return false;
725 }
726
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000727 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000729 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730
731 // we set the current clip to the bounds so that our recursive
732 // draws are scissored to them. We use the copy of the complex clip
733 // we just stashed on the SB to render from. We set it back after
734 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000735 const GrClipData* oldClipData = fGpu->getClip();
736
737 GrClip newClipStack(bounds);
738 GrClipData newClipData;
739 newClipData.fClipStack = &newClipStack;
740
741 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000743 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
744 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000746 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000747
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000748 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
749 // Add the saveLayer's offset to the view matrix rather than
750 // offset each individual draw
751 GrMatrix m;
752
753 m.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
754 SkIntToScalar(-clipDataIn.fOrigin.fY));
755
756 drawState->setViewMatrix(m);
757 }
758
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759#if !VISUALIZE_COMPLEX_CLIP
760 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
761#endif
762
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000763 int clipBit = stencilBuffer->bits();
764 SkASSERT((clipBit <= 16) &&
765 "Ganesh only handles 16b or smaller stencil buffers");
766 clipBit = (1 << (clipBit-1));
767
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000768 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000769
770 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000771 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
772
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000773 GrClip::Iter iter(*oldClipData->fClipStack,
774 GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000775 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000776 rtRect,
777 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000778 &firstOp,
779 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780
bsalomon@google.coma3201942012-06-21 19:58:20 +0000781 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000782 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000783
784 // walk through each clip element and perform its set op
785 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000786 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787 GrPathFill fill;
788 bool fillInverted;
789 // enabled at bottom of loop
790 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000791 // if the target is MSAA then we want MSAA enabled when the clip is soft
792 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000793 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000794 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
795 } else {
796 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
797 }
798 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799
800 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000801 // directly to the stencil buffer
802 // with a non-inverted fill rule
803 // without extra passes to
804 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000805
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000806 SkRegion::Op op = clip->fOp;
807 if (first) {
808 first = false;
809 op = firstOp;
810 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000811
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000813 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000814 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000815 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000816 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000817 fillInverted = false;
818 // there is no point in intersecting a screen filling
819 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000820 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000821 contains(*clip->fRect, rtRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 continue;
823 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000824 } else if (NULL != clip->fPath) {
825 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000826 fillInverted = GrIsFillInverted(fill);
827 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000828 clipPath = clip->fPath;
robertphillips@google.com2c756812012-05-22 20:28:23 +0000829 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000830 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000831 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000832 if (NULL == pr) {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000833 fGpu->setClip(oldClipData); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000834 return false;
835 }
836 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000837 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000838 }
839
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000840 int passes;
841 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
842
843 bool canDrawDirectToClip; // Given the renderer, the element,
844 // fill rule, and set operation can
845 // we render the element directly to
846 // stencil bit used for clipping.
847 canDrawDirectToClip =
848 GrStencilSettings::GetClipPasses(op,
849 canRenderDirectToStencil,
850 clipBit,
851 fillInverted,
852 &passes, stencilSettings);
853
854 // draw the element to the client stencil bits if necessary
855 if (!canDrawDirectToClip) {
856 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
857 kIncClamp_StencilOp,
858 kIncClamp_StencilOp,
859 kAlways_StencilFunc,
860 0xffff,
861 0x0000,
862 0xffff);
863 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000864 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000865 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000866 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000867 } else {
868 if (canRenderDirectToStencil) {
869 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000870 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000871 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000872 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000873 }
874 }
875 }
876
877 // now we modify the clip bit by rendering either the clip
878 // element directly or a bounding rect of the entire clip.
879 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
880 for (int p = 0; p < passes; ++p) {
881 *drawState->stencil() = stencilSettings[p];
882 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000883 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000884 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000885 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000886 } else {
887 SET_RANDOM_COLOR
bsalomon@google.come3d32162012-07-20 13:37:06 +0000888 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000889 }
890 } else {
891 SET_RANDOM_COLOR
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000892 // 'bounds' is already in device coordinates so the
893 // translation in the view matrix is inappropriate.
894 // Apply the inverse translation so the drawn rect will
895 // be in the correct location
bsalomon@google.coma3201942012-06-21 19:58:20 +0000896 GrRect rect = GrRect::MakeLTRB(
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000897 SkIntToScalar(bounds.fLeft+clipDataIn.fOrigin.fX),
898 SkIntToScalar(bounds.fTop+clipDataIn.fOrigin.fY),
899 SkIntToScalar(bounds.fRight+clipDataIn.fOrigin.fX),
900 SkIntToScalar(bounds.fBottom+clipDataIn.fOrigin.fY));
bsalomon@google.come3d32162012-07-20 13:37:06 +0000901 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000902 }
903 }
904 }
905 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000906 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000907 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000908 // set this last because recursive draws may overwrite it back to kNone.
909 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
910 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000911 return true;
912}
913
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000914
bsalomon@google.com411dad02012-06-05 20:24:20 +0000915// mapping of clip-respecting stencil funcs to normal stencil funcs
916// mapping depends on whether stencil-clipping is in effect.
917static const GrStencilFunc
918 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
919 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
920 // In the Clip Funcs
921 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
922 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
923 kLess_StencilFunc, // kLessIfInClip_StencilFunc
924 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
925 // Special in the clip func that forces user's ref to be 0.
926 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
927 // make ref 0 and do normal nequal.
928 },
929 {// Stencil-Clipping is ENABLED
930 // In the Clip Funcs
931 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
932 // eq stencil clip bit, mask
933 // out user bits.
934
935 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
936 // add stencil bit to mask and ref
937
938 kLess_StencilFunc, // kLessIfInClip_StencilFunc
939 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
940 // for both of these we can add
941 // the clip bit to the mask and
942 // ref and compare as normal
943 // Special in the clip func that forces user's ref to be 0.
944 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
945 // make ref have only the clip bit set
946 // and make comparison be less
947 // 10..0 < 1..user_bits..
948 }
949};
950
bsalomon@google.coma3201942012-06-21 19:58:20 +0000951namespace {
952// Sets the settings to clip against the stencil buffer clip while ignoring the
953// client bits.
954const GrStencilSettings& basic_apply_stencil_clip_settings() {
955 // stencil settings to use when clip is in stencil
956 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
957 kKeep_StencilOp,
958 kKeep_StencilOp,
959 kAlwaysIfInClip_StencilFunc,
960 0x0000,
961 0x0000,
962 0x0000);
963 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
964}
965}
966
967void GrClipMaskManager::setGpuStencil() {
968 // We make two copies of the StencilSettings here (except in the early
969 // exit scenario. One copy from draw state to the stack var. Then another
970 // from the stack var to the gpu. We could make this class hold a ptr to
971 // GrGpu's fStencilSettings and eliminate the stack copy here.
972
973 const GrDrawState& drawState = fGpu->getDrawState();
974
975 // use stencil for clipping if clipping is enabled and the clip
976 // has been written into the stencil.
977 GrClipMaskManager::StencilClipMode clipMode;
978 if (this->isClipInStencil() && drawState.isClipState()) {
979 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
980 // We can't be modifying the clip and respecting it at the same time.
981 GrAssert(!drawState.isStateFlagEnabled(
982 GrGpu::kModifyStencilClip_StateBit));
983 } else if (drawState.isStateFlagEnabled(
984 GrGpu::kModifyStencilClip_StateBit)) {
985 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
986 } else {
987 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
988 }
989
990 GrStencilSettings settings;
991 // The GrGpu client may not be using the stencil buffer but we may need to
992 // enable it in order to respect a stencil clip.
993 if (drawState.getStencil().isDisabled()) {
994 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
995 settings = basic_apply_stencil_clip_settings();
996 } else {
997 fGpu->disableStencil();
998 return;
999 }
1000 } else {
1001 settings = drawState.getStencil();
1002 }
1003
1004 // TODO: dynamically attach a stencil buffer
1005 int stencilBits = 0;
1006 GrStencilBuffer* stencilBuffer =
1007 drawState.getRenderTarget()->getStencilBuffer();
1008 if (NULL != stencilBuffer) {
1009 stencilBits = stencilBuffer->bits();
1010 }
1011
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001012 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
1013 !settings.usesWrapOp());
1014 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001015 this->adjustStencilParams(&settings, clipMode, stencilBits);
1016 fGpu->setStencilSettings(settings);
1017}
1018
1019void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1020 StencilClipMode mode,
1021 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001022 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001023
1024 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001025 // We assume that this clip manager itself is drawing to the GrGpu and
1026 // has already setup the correct values.
1027 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001028 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001029
bsalomon@google.com411dad02012-06-05 20:24:20 +00001030 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1031 unsigned int userBits = clipBit - 1;
1032
bsalomon@google.coma3201942012-06-21 19:58:20 +00001033 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
1034 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001035
bsalomon@google.coma3201942012-06-21 19:58:20 +00001036 bool finished = false;
1037 while (!finished) {
1038 GrStencilFunc func = settings->func(face);
1039 uint16_t writeMask = settings->writeMask(face);
1040 uint16_t funcMask = settings->funcMask(face);
1041 uint16_t funcRef = settings->funcRef(face);
1042
1043 GrAssert((unsigned) func < kStencilFuncCount);
1044
1045 writeMask &= userBits;
1046
1047 if (func >= kBasicStencilFuncCount) {
1048 int respectClip = kRespectClip_StencilClipMode == mode;
1049 if (respectClip) {
1050 // The GrGpu class should have checked this
1051 GrAssert(this->isClipInStencil());
1052 switch (func) {
1053 case kAlwaysIfInClip_StencilFunc:
1054 funcMask = clipBit;
1055 funcRef = clipBit;
1056 break;
1057 case kEqualIfInClip_StencilFunc:
1058 case kLessIfInClip_StencilFunc:
1059 case kLEqualIfInClip_StencilFunc:
1060 funcMask = (funcMask & userBits) | clipBit;
1061 funcRef = (funcRef & userBits) | clipBit;
1062 break;
1063 case kNonZeroIfInClip_StencilFunc:
1064 funcMask = (funcMask & userBits) | clipBit;
1065 funcRef = clipBit;
1066 break;
1067 default:
1068 GrCrash("Unknown stencil func");
1069 }
1070 } else {
1071 funcMask &= userBits;
1072 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001073 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001074 const GrStencilFunc* table =
1075 gSpecialToBasicStencilFunc[respectClip];
1076 func = table[func - kBasicStencilFuncCount];
1077 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001078 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001079 funcMask &= userBits;
1080 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001081 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001082
1083 settings->setFunc(face, func);
1084 settings->setWriteMask(face, writeMask);
1085 settings->setFuncMask(face, funcMask);
1086 settings->setFuncRef(face, funcRef);
1087
1088 if (GrStencilSettings::kFront_Face == face) {
1089 face = GrStencilSettings::kBack_Face;
1090 finished = !twoSided;
1091 } else {
1092 finished = true;
1093 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001094 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001095 if (!twoSided) {
1096 settings->copyFrontSettingsToBack();
1097 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001098}
1099
1100////////////////////////////////////////////////////////////////////////////////
1101
robertphillips@google.comfa662942012-05-17 12:20:22 +00001102namespace {
1103
1104GrPathFill invert_fill(GrPathFill fill) {
1105 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001106 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1107 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1108 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1109 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1110 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001111 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001112 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1113 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1114 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1115 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1116 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1117 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001118 return gInvertedFillTable[fill];
1119}
1120
1121}
1122
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001123bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001124 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001125 GrIRect* resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001126 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001127
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001128 if (this->clipMaskPreamble(clipDataIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001129 return true;
1130 }
1131
robertphillips@google.comf105b102012-05-14 12:18:26 +00001132 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001133 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001134 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001135 return false;
1136 }
1137
robertphillips@google.com2c756812012-05-22 20:28:23 +00001138 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001139
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001140 GrMatrix matrix;
1141 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
1142 SkIntToScalar(-clipDataIn.fOrigin.fY));
1143 helper.init(*resultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001144
robertphillips@google.comfa662942012-05-17 12:20:22 +00001145 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001146 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1147
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001148 GrClip::Iter iter(*clipDataIn.fClipStack,
1149 GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001150 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001151 *resultBounds,
1152 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001153 &firstOp,
1154 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001155
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001156 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001157
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001158 bool first = true;
1159 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001160
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001161 SkRegion::Op op = clip->fOp;
1162 if (first) {
1163 first = false;
1164 op = firstOp;
1165 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001166
1167 if (SkRegion::kIntersect_Op == op ||
1168 SkRegion::kReverseDifference_Op == op) {
1169 // Intersect and reverse difference require modifying pixels
1170 // outside of the geometry that is being "drawn". In both cases
1171 // we erase all the pixels outside of the geometry but
1172 // leave the pixels inside the geometry alone. For reverse
1173 // difference we invert all the pixels before clearing the ones
1174 // outside the geometry.
1175 if (SkRegion::kReverseDifference_Op == op) {
1176 SkRect temp = SkRect::MakeLTRB(
1177 SkIntToScalar(resultBounds->left()),
1178 SkIntToScalar(resultBounds->top()),
1179 SkIntToScalar(resultBounds->right()),
1180 SkIntToScalar(resultBounds->bottom()));
1181
1182 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001183 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001184 }
1185
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001186 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001187
1188 // convert the rect to a path so we can invert the fill
1189 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001190 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001191
1192 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001193 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001194 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001195 } else if (NULL != clip->fPath) {
1196 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001197 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001198 invert_fill(get_path_fill(*clip->fPath)),
1199 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001200 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001201 }
1202
1203 continue;
1204 }
1205
1206 // The other ops (union, xor, diff) only affect pixels inside
1207 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001208 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001209
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001210 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001211 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001212 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001213
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001214 } else if (NULL != clip->fPath) {
1215 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001216 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001217 get_path_fill(*clip->fPath),
1218 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001219 }
1220 }
1221
robertphillips@google.comfa662942012-05-17 12:20:22 +00001222 // Because we are using the scratch texture cache, "accum" may be
1223 // larger than expected and have some cruft in the areas we aren't using.
1224 // Clear it out.
1225
1226 // TODO: need a simpler way to clear the texture - can we combine
1227 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001228 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001229 GrAssert(NULL != drawState);
1230 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001231 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001232 // can't leave the accum bound as a rendertarget
1233 drawState->setRenderTarget(temp);
1234
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001235 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001236
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001237 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001238
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001239 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001240 return true;
1241}
1242
robertphillips@google.comf294b772012-04-27 14:29:26 +00001243////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001244void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001245 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001246}