blob: d7287ea0ec6d69a40d782f18673b4b2ef5ecf408 [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.comf294b772012-04-27 14:29:26 +000023////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000024namespace {
25// set up the draw state to enable the aa clipping mask. Besides setting up the
26// sampler matrix this also alters the vertex layout
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000027void setup_drawstate_aaclip(GrGpu* gpu,
28 GrTexture* result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000029 const GrIRect &bound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000030 GrDrawState* drawState = gpu->drawState();
31 GrAssert(drawState);
32
33 static const int maskStage = GrPaint::kTotalStages+1;
34
35 GrMatrix mat;
36 mat.setIDiv(result->width(), result->height());
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000037 mat.preTranslate(SkIntToScalar(-bound.fLeft), SkIntToScalar(-bound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000038 mat.preConcat(drawState->getViewMatrix());
39
bsalomon@google.comb8670992012-07-25 21:27:09 +000040 drawState->sampler(maskStage)->reset(mat);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000042 drawState->createTextureEffect(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043}
44
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000045bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000046 GrGpu* gpu,
47 const SkPath& path,
48 GrPathFill fill,
49 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000050 // last (false) parameter disallows use of the SW path renderer
51 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
52}
53
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000054GrPathFill get_path_fill(const SkPath& path) {
55 switch (path.getFillType()) {
56 case SkPath::kWinding_FillType:
57 return kWinding_GrPathFill;
58 case SkPath::kEvenOdd_FillType:
59 return kEvenOdd_GrPathFill;
60 case SkPath::kInverseWinding_FillType:
61 return kInverseWinding_GrPathFill;
62 case SkPath::kInverseEvenOdd_FillType:
63 return kInverseEvenOdd_GrPathFill;
64 default:
65 GrCrash("Unsupported path fill in clip.");
66 return kWinding_GrPathFill; // suppress warning
67 }
68}
69
robertphillips@google.comb99225c2012-07-24 18:20:10 +000070/**
71 * Does any individual clip in 'clipIn' use anti-aliasing?
72 */
73bool requires_AA(const GrClip& clipIn) {
74
75 GrClip::Iter iter;
76 iter.reset(clipIn, GrClip::Iter::kBottom_IterStart);
77
78 const GrClip::Iter::Clip* clip = NULL;
79 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
80 NULL != clip;
81 clip = iter.next()) {
82
83 if (clip->fDoAA) {
84 return true;
85 }
86 }
87
88 return false;
89}
90
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000091}
92
robertphillips@google.comfa662942012-05-17 12:20:22 +000093/*
94 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
95 * will be used on any element. If so, it returns true to indicate that the
96 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
97 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000098bool GrClipMaskManager::useSWOnlyPath(const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000099
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000100 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000101 // a clip gets complex enough it can just be done in SW regardless
102 // of whether it would invoke the GrSoftwarePathRenderer.
103 bool useSW = false;
104
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000105 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
106 const GrClip::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000107
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000108 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
109 NULL != clip;
110 clip = iter.next()) {
111
112 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000113 // Everything before a replace op can be ignored so start
114 // afresh w.r.t. determining if any element uses the SW path
115 useSW = false;
116 }
117
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000118 // rects can always be drawn directly w/o using the software path
119 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000120 if (NULL != clip->fPath &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000121 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000122 *clip->fPath,
123 get_path_fill(*clip->fPath),
124 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000125 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000126 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000127 }
128
129 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000130}
131
robertphillips@google.comf294b772012-04-27 14:29:26 +0000132////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000133// sort out what kind of clip mask needs to be created: alpha, stencil,
134// scissor, or entirely software
bsalomon@google.coma3201942012-06-21 19:58:20 +0000135bool GrClipMaskManager::setupClipping(const GrClip& clipIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000136 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000137
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000138 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000139 if (!drawState->isClipState() || clipIn.isEmpty()) {
140 fGpu->disableScissor();
141 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000142 return true;
143 }
144
145 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000146 // GrDrawTarget should have filtered this for us
147 GrAssert(NULL != rt);
148
bsalomon@google.coma3201942012-06-21 19:58:20 +0000149 GrIRect bounds;
150 GrIRect rtRect;
151 rtRect.setLTRB(0, 0, rt->width(), rt->height());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000152
153 clipIn.getConservativeBounds().roundOut(&bounds);
154 if (!bounds.intersect(rtRect)) {
155 bounds.setEmpty();
156 }
157 if (bounds.isEmpty()) {
158 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000159 }
160
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000161 bool requiresAA = requires_AA(clipIn);
162 GrAssert(requiresAA == clipIn.requiresAA());
163
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000164#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000165 // If MSAA is enabled we can do everything in the stencil buffer.
166 // Otherwise check if we should just create the entire clip mask
167 // in software (this will only happen if the clip mask is anti-aliased
168 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000169 if (0 == rt->numSamples() && requiresAA && this->useSWOnlyPath(clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000170 // The clip geometry is complex enough that it will be more
171 // efficient to create it entirely in software
172 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000173 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000174 if (this->createSoftwareClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000175 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000176 fGpu->disableScissor();
177 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000178 return true;
179 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000180
181 // if SW clip mask creation fails fall through to the other
182 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000183 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000184#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000185
robertphillips@google.comf294b772012-04-27 14:29:26 +0000186#if GR_AA_CLIP
187 // If MSAA is enabled use the (faster) stencil path for AA clipping
188 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000189 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000190 // Since we are going to create a destination texture of the correct
191 // size for the mask (rather than being bound by the size of the
192 // render target) we aren't going to use scissoring like the stencil
193 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000194 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000195 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000196 if (this->createAlphaClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000197 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000198 fGpu->disableScissor();
199 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000200 return true;
201 }
202
203 // if alpha clip mask creation fails fall through to the stencil
204 // buffer method
205 }
206#endif // GR_AA_CLIP
207
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000208 // Either a hard (stencil buffer) clip was explicitly requested or
209 // an antialiased clip couldn't be created. In either case, free up
210 // the texture in the antialiased mask cache.
211 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000212 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
213 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000214 // AA cache.
215 fAACache.reset();
216
bsalomon@google.coma3201942012-06-21 19:58:20 +0000217 // If the clip is a rectangle then just set the scissor. Otherwise, create
218 // a stencil mask.
219 if (clipIn.isRect()) {
220 fGpu->enableScissor(bounds);
221 this->setGpuStencil();
222 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000223 }
224
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000225 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000226 bool useStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
227 !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000228
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000229 if (useStencil) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000230 this->createStencilClipMask(clipIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000231 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000232 // This must occur after createStencilClipMask. That function may change
233 // the scissor. Also, it only guarantees that the stencil mask is correct
234 // within the bounds it was passed, so we must use both stencil and scissor
235 // test to the bounds for the final draw.
236 fGpu->enableScissor(bounds);
237 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000238 return true;
239}
240
241#define VISUALIZE_COMPLEX_CLIP 0
242
243#if VISUALIZE_COMPLEX_CLIP
244 #include "GrRandom.h"
245 GrRandom gRandom;
246 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
247#else
248 #define SET_RANDOM_COLOR
249#endif
250
251namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000252/**
253 * Does "container" contain "containee"? If either is empty then
254 * no containment is possible.
255 */
256bool contains(const SkRect& container, const SkIRect& containee) {
257 return !containee.isEmpty() && !container.isEmpty() &&
258 container.fLeft <= SkIntToScalar(containee.fLeft) &&
259 container.fTop <= SkIntToScalar(containee.fTop) &&
260 container.fRight >= SkIntToScalar(containee.fRight) &&
261 container.fBottom >= SkIntToScalar(containee.fBottom);
262}
263
264
robertphillips@google.comf294b772012-04-27 14:29:26 +0000265////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000266// determines how many elements at the head of the clip can be skipped and
267// whether the initial clear should be to the inside- or outside-the-clip value,
268// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000269const GrClip::Iter::Clip* process_initial_clip_elements(
270 GrClip::Iter* iter,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000271 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000272 bool* clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000273 SkRegion::Op* firstOp) {
274
275 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000276
277 // logically before the first element of the clip stack is
278 // processed the clip is entirely open. However, depending on the
279 // first set op we may prefer to clear to 0 for performance. We may
280 // also be able to skip the initial clip paths/rects. We loop until
281 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000282 bool done = false;
283 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000284
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000285 const GrClip::Iter::Clip* clip = NULL;
286
287 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
288 NULL != clip && !done;
289 clip = iter->next()) {
290 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000291 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000292 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000293 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000294 *clearToInside = false;
295 done = true;
296 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000297 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000298 // if this element contains the entire bounds then we
299 // can skip it.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000300 if (NULL != clip->fRect && contains(*clip->fRect, bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000301 break;
302 }
303 // if everything is initially clearToInside then intersect is
304 // same as clear to 0 and treat as a replace. Otherwise,
305 // set stays empty.
306 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000307 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000308 *clearToInside = false;
309 done = true;
310 }
311 break;
312 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000313 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000314 // if everything is initially outside then union is
315 // same as replace. Otherwise, every pixel is still
316 // clearToInside
317 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000318 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000319 done = true;
320 }
321 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000322 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000323 // xor is same as difference or replace both of which
324 // can be 1-pass instead of 2 for xor.
325 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000326 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000327 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000328 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000329 }
330 done = true;
331 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000332 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000333 // if all pixels are clearToInside then we have to process the
334 // difference, otherwise it has no effect and all pixels
335 // remain outside.
336 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000337 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000338 done = true;
339 }
340 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000341 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000342 // if all pixels are clearToInside then reverse difference
343 // produces empty set. Otherise it is same as replace
344 if (*clearToInside) {
345 *clearToInside = false;
346 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000347 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000348 done = true;
349 }
350 break;
351 default:
352 GrCrash("Unknown set op.");
353 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000354
355 if (done) {
356 // we need to break out here (rather than letting the test in
357 // the loop do it) since backing up the iterator is very expensive
358 break;
359 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000360 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000361 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000362}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000364}
365
robertphillips@google.comf294b772012-04-27 14:29:26 +0000366
367namespace {
368
369////////////////////////////////////////////////////////////////////////////////
370// set up the OpenGL blend function to perform the specified
371// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000372void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000373
374 switch (op) {
375 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000376 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 break;
378 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000379 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380 break;
381 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000382 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000383 break;
384 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000385 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386 break;
387 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000388 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389 break;
390 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000391 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392 break;
393 default:
394 GrAssert(false);
395 break;
396 }
397}
398
robertphillips@google.comf294b772012-04-27 14:29:26 +0000399////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000400bool draw_path_in_software(GrContext* context,
401 GrGpu* gpu,
402 const SkPath& path,
403 GrPathFill fill,
404 bool doAA,
405 const GrIRect& resultBounds) {
406
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000407 SkAutoTUnref<GrTexture> texture(
408 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
409 resultBounds, fill,
410 doAA, NULL));
411 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000412 return false;
413 }
414
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000415 // The ClipMaskManager accumulates the clip mask in the UL corner
416 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000417
bsalomon@google.come3d32162012-07-20 13:37:06 +0000418 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000419
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000420 GrAssert(!GrIsFillInverted(fill));
421 return true;
422}
423
424
425////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000426bool draw_path(GrContext* context,
427 GrGpu* gpu,
428 const SkPath& path,
429 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000430 bool doAA,
431 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000432
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000433 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000434 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000435 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000436 }
437
bsalomon@google.come3d32162012-07-20 13:37:06 +0000438 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000439 return true;
440}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000441
442}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000443
444////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000445bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000446 const GrClip::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000447 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000448 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000449 GrAssert(NULL != drawState);
450
451 drawState->setRenderTarget(target->asRenderTarget());
452
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000453 if (NULL != clip->fRect) {
454 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000455 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000456 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000457 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000458 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000459 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000460 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000461 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000462 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000463 *clip->fPath,
464 get_path_fill(*clip->fPath),
465 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000466 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000467 }
468 return true;
469}
470
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000471void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000472 GrTexture* texture) {
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 // no AA here since it is encoded in the texture
477 drawState->setRenderTarget(target->asRenderTarget());
478
479 GrMatrix sampleM;
480 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000481
bsalomon@google.comb8670992012-07-25 21:27:09 +0000482 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000483 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000484
robertphillips@google.comf105b102012-05-14 12:18:26 +0000485 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
486 SkIntToScalar(target->height()));
487
bsalomon@google.come3d32162012-07-20 13:37:06 +0000488 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000489
tomhudson@google.com676e6602012-07-10 17:21:48 +0000490 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000491}
492
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000493// get a texture to act as a temporary buffer for AA clip boolean operations
494// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000495void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000496 GrAutoScratchTexture* temp) {
497 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000498 // we've already allocated the temp texture
499 return;
500 }
501
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000502 GrTextureDesc desc;
503 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
504 desc.fWidth = bounds.width();
505 desc.fHeight = bounds.height();
506 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000507
robertphillips@google.com2c756812012-05-22 20:28:23 +0000508 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000509}
510
robertphillips@google.comf105b102012-05-14 12:18:26 +0000511
512void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000513 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000514 // Since we are setting up the cache we know the last lookup was a miss
515 // Free up the currently cached mask so it can be reused
516 fAACache.reset();
517
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000518 GrTextureDesc desc;
519 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
520 desc.fWidth = bounds.width();
521 desc.fHeight = bounds.height();
522 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000523
524 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000525}
526
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000527////////////////////////////////////////////////////////////////////////////////
528// Shared preamble between gpu and SW-only AA clip mask creation paths.
529// Handles caching, determination of clip mask bound & allocation (if needed)
530// of the result texture
531// Returns true if there is no more work to be done (i.e., we got a cache hit)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000532bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000533 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000534 GrIRect* resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000535 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000536 GrAssert(origDrawState->isClipState());
537
538 GrRenderTarget* rt = origDrawState->getRenderTarget();
539 GrAssert(NULL != rt);
540
541 GrRect rtRect;
542 rtRect.setLTRB(0, 0,
543 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
544
545 // unlike the stencil path the alpha path is not bound to the size of the
546 // render target - determine the minimum size required for the mask
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000547 GrRect bounds = clipIn.getConservativeBounds();
548 if (!bounds.intersect(rtRect)) {
549 // the mask will be empty in this case
550 GrAssert(false);
551 bounds.setEmpty();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000552 }
553
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000554 GrIRect intBounds;
555 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000556
557 // need to outset a pixel since the standard bounding box computation
558 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000559 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000561 // TODO: make sure we don't outset if bounds are still 0,0 @ min
562
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000563 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000564 intBounds.width(),
565 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000566 *result = fAACache.getLastMask();
567 fAACache.getLastBound(resultBounds);
568 return true;
569 }
570
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000571 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000572
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000573 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000574 return false;
575}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000576
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000577////////////////////////////////////////////////////////////////////////////////
578// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000579bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000580 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000581 GrIRect *resultBounds) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000582 GrAssert(NULL != resultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000583 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
584
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000585 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000586 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000587 return true;
588 }
589
robertphillips@google.comf105b102012-05-14 12:18:26 +0000590 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000591 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000592 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000593 return false;
594 }
595
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000596 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
597 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000598
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000599 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000600
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000601 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000602 // if we were able to trim down the size of the mask we need to
603 // offset the paths & rects that will be used to compute it
604 GrMatrix m;
605
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000606 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
607 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000609 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000610 }
611
612 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000613 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
614
615 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
616 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
617 *resultBounds,
618 &clearToInside,
619 &firstOp);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000621 fGpu->clear(NULL,
622 clearToInside ? 0xffffffff : 0x00000000,
623 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000624
robertphillips@google.comf105b102012-05-14 12:18:26 +0000625 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000626 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000627 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000628 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000629
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000630 SkRegion::Op op = clip->fOp;
631 if (first) {
632 first = false;
633 op = firstOp;
634 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000635
636 if (SkRegion::kReplace_Op == op) {
637 // TODO: replace is actually a lot faster then intersection
638 // for this path - refactor the stencil path so it can handle
639 // replace ops and alter GrClip to allow them through
640
641 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000642 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000643
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000644 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000645 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000646
647 } else if (SkRegion::kReverseDifference_Op == op ||
648 SkRegion::kIntersect_Op == op) {
649 // there is no point in intersecting a screen filling rectangle.
650 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000651 NULL != clip->fRect &&
652 contains(*clip->fRect, *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000653 continue;
654 }
655
robertphillips@google.comf105b102012-05-14 12:18:26 +0000656 getTemp(*resultBounds, &temp);
657 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000658 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000659 return false;
660 }
661
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000663 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000664
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000665 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000666 this->drawClipShape(temp.texture(), clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667
668 // TODO: rather than adding these two translations here
669 // compute the bounding box needed to render the texture
670 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000671 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000672 GrMatrix m;
673
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000674 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
675 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000676
677 drawState->preConcatViewMatrix(m);
678 }
679
680 // Now draw into the accumulator using the real operation
681 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000682 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000683 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000685 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000686 GrMatrix m;
687
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000688 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
689 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000690
691 drawState->preConcatViewMatrix(m);
692 }
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.com1e945b72012-04-16 18:03:03 +0000708// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000709bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000710 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000712 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000714 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715 GrAssert(drawState->isClipState());
716
717 GrRenderTarget* rt = drawState->getRenderTarget();
718 GrAssert(NULL != rt);
719
720 // TODO: dynamically attach a SB when needed.
721 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
722 if (NULL == stencilBuffer) {
723 return false;
724 }
725
726 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
727
728 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
729
730 // we set the current clip to the bounds so that our recursive
731 // draws are scissored to them. We use the copy of the complex clip
732 // we just stashed on the SB to render from. We set it back after
733 // we finish drawing it into the stencil.
734 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000735 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000737 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
738 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000740 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742#if !VISUALIZE_COMPLEX_CLIP
743 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
744#endif
745
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746 int clipBit = stencilBuffer->bits();
747 SkASSERT((clipBit <= 16) &&
748 "Ganesh only handles 16b or smaller stencil buffers");
749 clipBit = (1 << (clipBit-1));
750
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000751 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752
753 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000754 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
755
756 GrClip::Iter iter(clipCopy, GrClip::Iter::kBottom_IterStart);
757 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000758 rtRect,
759 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000760 &firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000761
bsalomon@google.coma3201942012-06-21 19:58:20 +0000762 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000763 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764
765 // walk through each clip element and perform its set op
766 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000767 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768 GrPathFill fill;
769 bool fillInverted;
770 // enabled at bottom of loop
771 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000772 // if the target is MSAA then we want MSAA enabled when the clip is soft
773 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000774 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000775 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
776 } else {
777 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
778 }
779 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780
781 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000782 // directly to the stencil buffer
783 // with a non-inverted fill rule
784 // without extra passes to
785 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000786
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000787 SkRegion::Op op = clip->fOp;
788 if (first) {
789 first = false;
790 op = firstOp;
791 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000792
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000794 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000795 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000796 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000797 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798 fillInverted = false;
799 // there is no point in intersecting a screen filling
800 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000801 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000802 contains(*clip->fRect, rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803 continue;
804 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000805 } else if (NULL != clip->fPath) {
806 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 fillInverted = GrIsFillInverted(fill);
808 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000809 clipPath = clip->fPath;
robertphillips@google.com2c756812012-05-22 20:28:23 +0000810 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000811 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000812 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 if (NULL == pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000814 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000815 return false;
816 }
817 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000818 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 }
820
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000821 int passes;
822 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
823
824 bool canDrawDirectToClip; // Given the renderer, the element,
825 // fill rule, and set operation can
826 // we render the element directly to
827 // stencil bit used for clipping.
828 canDrawDirectToClip =
829 GrStencilSettings::GetClipPasses(op,
830 canRenderDirectToStencil,
831 clipBit,
832 fillInverted,
833 &passes, stencilSettings);
834
835 // draw the element to the client stencil bits if necessary
836 if (!canDrawDirectToClip) {
837 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
838 kIncClamp_StencilOp,
839 kIncClamp_StencilOp,
840 kAlways_StencilFunc,
841 0xffff,
842 0x0000,
843 0xffff);
844 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000845 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000846 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000847 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000848 } else {
849 if (canRenderDirectToStencil) {
850 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000851 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000852 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000853 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000854 }
855 }
856 }
857
858 // now we modify the clip bit by rendering either the clip
859 // element directly or a bounding rect of the entire clip.
860 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
861 for (int p = 0; p < passes; ++p) {
862 *drawState->stencil() = stencilSettings[p];
863 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000864 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000865 SET_RANDOM_COLOR
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 SET_RANDOM_COLOR
bsalomon@google.come3d32162012-07-20 13:37:06 +0000869 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000870 }
871 } else {
872 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000873 GrRect rect = GrRect::MakeLTRB(
874 SkIntToScalar(bounds.fLeft),
875 SkIntToScalar(bounds.fTop),
876 SkIntToScalar(bounds.fRight),
877 SkIntToScalar(bounds.fBottom));
bsalomon@google.come3d32162012-07-20 13:37:06 +0000878 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000879 }
880 }
881 }
882 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000883 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000884 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000885 // set this last because recursive draws may overwrite it back to kNone.
886 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
887 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000888 return true;
889}
890
bsalomon@google.com411dad02012-06-05 20:24:20 +0000891// mapping of clip-respecting stencil funcs to normal stencil funcs
892// mapping depends on whether stencil-clipping is in effect.
893static const GrStencilFunc
894 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
895 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
896 // In the Clip Funcs
897 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
898 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
899 kLess_StencilFunc, // kLessIfInClip_StencilFunc
900 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
901 // Special in the clip func that forces user's ref to be 0.
902 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
903 // make ref 0 and do normal nequal.
904 },
905 {// Stencil-Clipping is ENABLED
906 // In the Clip Funcs
907 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
908 // eq stencil clip bit, mask
909 // out user bits.
910
911 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
912 // add stencil bit to mask and ref
913
914 kLess_StencilFunc, // kLessIfInClip_StencilFunc
915 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
916 // for both of these we can add
917 // the clip bit to the mask and
918 // ref and compare as normal
919 // Special in the clip func that forces user's ref to be 0.
920 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
921 // make ref have only the clip bit set
922 // and make comparison be less
923 // 10..0 < 1..user_bits..
924 }
925};
926
bsalomon@google.coma3201942012-06-21 19:58:20 +0000927namespace {
928// Sets the settings to clip against the stencil buffer clip while ignoring the
929// client bits.
930const GrStencilSettings& basic_apply_stencil_clip_settings() {
931 // stencil settings to use when clip is in stencil
932 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
933 kKeep_StencilOp,
934 kKeep_StencilOp,
935 kAlwaysIfInClip_StencilFunc,
936 0x0000,
937 0x0000,
938 0x0000);
939 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
940}
941}
942
943void GrClipMaskManager::setGpuStencil() {
944 // We make two copies of the StencilSettings here (except in the early
945 // exit scenario. One copy from draw state to the stack var. Then another
946 // from the stack var to the gpu. We could make this class hold a ptr to
947 // GrGpu's fStencilSettings and eliminate the stack copy here.
948
949 const GrDrawState& drawState = fGpu->getDrawState();
950
951 // use stencil for clipping if clipping is enabled and the clip
952 // has been written into the stencil.
953 GrClipMaskManager::StencilClipMode clipMode;
954 if (this->isClipInStencil() && drawState.isClipState()) {
955 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
956 // We can't be modifying the clip and respecting it at the same time.
957 GrAssert(!drawState.isStateFlagEnabled(
958 GrGpu::kModifyStencilClip_StateBit));
959 } else if (drawState.isStateFlagEnabled(
960 GrGpu::kModifyStencilClip_StateBit)) {
961 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
962 } else {
963 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
964 }
965
966 GrStencilSettings settings;
967 // The GrGpu client may not be using the stencil buffer but we may need to
968 // enable it in order to respect a stencil clip.
969 if (drawState.getStencil().isDisabled()) {
970 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
971 settings = basic_apply_stencil_clip_settings();
972 } else {
973 fGpu->disableStencil();
974 return;
975 }
976 } else {
977 settings = drawState.getStencil();
978 }
979
980 // TODO: dynamically attach a stencil buffer
981 int stencilBits = 0;
982 GrStencilBuffer* stencilBuffer =
983 drawState.getRenderTarget()->getStencilBuffer();
984 if (NULL != stencilBuffer) {
985 stencilBits = stencilBuffer->bits();
986 }
987
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000988 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
989 !settings.usesWrapOp());
990 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000991 this->adjustStencilParams(&settings, clipMode, stencilBits);
992 fGpu->setStencilSettings(settings);
993}
994
995void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
996 StencilClipMode mode,
997 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000998 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000999
1000 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001001 // We assume that this clip manager itself is drawing to the GrGpu and
1002 // has already setup the correct values.
1003 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001004 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001005
bsalomon@google.com411dad02012-06-05 20:24:20 +00001006 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1007 unsigned int userBits = clipBit - 1;
1008
bsalomon@google.coma3201942012-06-21 19:58:20 +00001009 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
1010 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001011
bsalomon@google.coma3201942012-06-21 19:58:20 +00001012 bool finished = false;
1013 while (!finished) {
1014 GrStencilFunc func = settings->func(face);
1015 uint16_t writeMask = settings->writeMask(face);
1016 uint16_t funcMask = settings->funcMask(face);
1017 uint16_t funcRef = settings->funcRef(face);
1018
1019 GrAssert((unsigned) func < kStencilFuncCount);
1020
1021 writeMask &= userBits;
1022
1023 if (func >= kBasicStencilFuncCount) {
1024 int respectClip = kRespectClip_StencilClipMode == mode;
1025 if (respectClip) {
1026 // The GrGpu class should have checked this
1027 GrAssert(this->isClipInStencil());
1028 switch (func) {
1029 case kAlwaysIfInClip_StencilFunc:
1030 funcMask = clipBit;
1031 funcRef = clipBit;
1032 break;
1033 case kEqualIfInClip_StencilFunc:
1034 case kLessIfInClip_StencilFunc:
1035 case kLEqualIfInClip_StencilFunc:
1036 funcMask = (funcMask & userBits) | clipBit;
1037 funcRef = (funcRef & userBits) | clipBit;
1038 break;
1039 case kNonZeroIfInClip_StencilFunc:
1040 funcMask = (funcMask & userBits) | clipBit;
1041 funcRef = clipBit;
1042 break;
1043 default:
1044 GrCrash("Unknown stencil func");
1045 }
1046 } else {
1047 funcMask &= userBits;
1048 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001049 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001050 const GrStencilFunc* table =
1051 gSpecialToBasicStencilFunc[respectClip];
1052 func = table[func - kBasicStencilFuncCount];
1053 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001054 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001055 funcMask &= userBits;
1056 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001057 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001058
1059 settings->setFunc(face, func);
1060 settings->setWriteMask(face, writeMask);
1061 settings->setFuncMask(face, funcMask);
1062 settings->setFuncRef(face, funcRef);
1063
1064 if (GrStencilSettings::kFront_Face == face) {
1065 face = GrStencilSettings::kBack_Face;
1066 finished = !twoSided;
1067 } else {
1068 finished = true;
1069 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001070 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001071 if (!twoSided) {
1072 settings->copyFrontSettingsToBack();
1073 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001074}
1075
1076////////////////////////////////////////////////////////////////////////////////
1077
robertphillips@google.comfa662942012-05-17 12:20:22 +00001078namespace {
1079
1080GrPathFill invert_fill(GrPathFill fill) {
1081 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001082 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1083 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1084 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1085 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1086 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001087 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001088 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1089 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1090 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1091 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1092 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1093 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001094 return gInvertedFillTable[fill];
1095}
1096
1097}
1098
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001099bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001100 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001101 GrIRect* resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001102 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001103
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001104 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001105 return true;
1106 }
1107
robertphillips@google.comf105b102012-05-14 12:18:26 +00001108 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001109 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001110 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001111 return false;
1112 }
1113
robertphillips@google.com2c756812012-05-22 20:28:23 +00001114 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001115
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001116 helper.init(*resultBounds, NULL);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001117
robertphillips@google.comfa662942012-05-17 12:20:22 +00001118 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001119 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1120
1121 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
1122 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001123 *resultBounds,
1124 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001125 &firstOp);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001126
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001127 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001128
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001129 bool first = true;
1130 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001131
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001132 SkRegion::Op op = clip->fOp;
1133 if (first) {
1134 first = false;
1135 op = firstOp;
1136 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001137
1138 if (SkRegion::kIntersect_Op == op ||
1139 SkRegion::kReverseDifference_Op == op) {
1140 // Intersect and reverse difference require modifying pixels
1141 // outside of the geometry that is being "drawn". In both cases
1142 // we erase all the pixels outside of the geometry but
1143 // leave the pixels inside the geometry alone. For reverse
1144 // difference we invert all the pixels before clearing the ones
1145 // outside the geometry.
1146 if (SkRegion::kReverseDifference_Op == op) {
1147 SkRect temp = SkRect::MakeLTRB(
1148 SkIntToScalar(resultBounds->left()),
1149 SkIntToScalar(resultBounds->top()),
1150 SkIntToScalar(resultBounds->right()),
1151 SkIntToScalar(resultBounds->bottom()));
1152
1153 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001154 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001155 }
1156
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001157 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001158
1159 // convert the rect to a path so we can invert the fill
1160 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001161 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001162
1163 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001164 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001165 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001166 } else if (NULL != clip->fPath) {
1167 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001168 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001169 invert_fill(get_path_fill(*clip->fPath)),
1170 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001171 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001172 }
1173
1174 continue;
1175 }
1176
1177 // The other ops (union, xor, diff) only affect pixels inside
1178 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001179 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001180
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001181 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001182 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001183 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001184
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001185 } else if (NULL != clip->fPath) {
1186 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001187 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001188 get_path_fill(*clip->fPath),
1189 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001190 }
1191 }
1192
robertphillips@google.comfa662942012-05-17 12:20:22 +00001193 // Because we are using the scratch texture cache, "accum" may be
1194 // larger than expected and have some cruft in the areas we aren't using.
1195 // Clear it out.
1196
1197 // TODO: need a simpler way to clear the texture - can we combine
1198 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001199 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001200 GrAssert(NULL != drawState);
1201 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001202 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001203 // can't leave the accum bound as a rendertarget
1204 drawState->setRenderTarget(temp);
1205
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001206 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001207
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001208 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001209
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001210 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001211 return true;
1212}
1213
robertphillips@google.comf294b772012-04-27 14:29:26 +00001214////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001215void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001216 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001217}