blob: a3c853773770432ba37c620fa38307607cbb6238 [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
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000135bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
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();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000139 if (!drawState->isClipState() || clipDataIn->fClipStack->isEmpty()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000140 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
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000153
154 GrRect conservativeBounds = clipDataIn->fClipStack->getConservativeBounds();
155
156 conservativeBounds.roundOut(&bounds);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000157 if (!bounds.intersect(rtRect)) {
158 bounds.setEmpty();
159 }
160 if (bounds.isEmpty()) {
161 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000162 }
163
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000164 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
165 GrAssert(requiresAA == clipDataIn->fClipStack->requiresAA());
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000166
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000167#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000168 // If MSAA is enabled we can do everything in the stencil buffer.
169 // Otherwise check if we should just create the entire clip mask
170 // in software (this will only happen if the clip mask is anti-aliased
171 // and too complex for the gpu to handle in its entirety)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000172 if (0 == rt->numSamples() &&
173 requiresAA &&
174 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000175 // The clip geometry is complex enough that it will be more
176 // efficient to create it entirely in software
177 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000178 GrIRect bound;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000179 if (this->createSoftwareClipMask(*clipDataIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000180 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000181 fGpu->disableScissor();
182 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000183 return true;
184 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000185
186 // if SW clip mask creation fails fall through to the other
187 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000188 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000189#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000190
robertphillips@google.comf294b772012-04-27 14:29:26 +0000191#if GR_AA_CLIP
192 // If MSAA is enabled use the (faster) stencil path for AA clipping
193 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000194 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000195 // Since we are going to create a destination texture of the correct
196 // size for the mask (rather than being bound by the size of the
197 // render target) we aren't going to use scissoring like the stencil
198 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000199 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000200 GrIRect bound;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000201 if (this->createAlphaClipMask(*clipDataIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000202 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000203 fGpu->disableScissor();
204 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000205 return true;
206 }
207
208 // if alpha clip mask creation fails fall through to the stencil
209 // buffer method
210 }
211#endif // GR_AA_CLIP
212
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000213 // Either a hard (stencil buffer) clip was explicitly requested or
214 // an antialiased clip couldn't be created. In either case, free up
215 // the texture in the antialiased mask cache.
216 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000217 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
218 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000219 // AA cache.
220 fAACache.reset();
221
bsalomon@google.coma3201942012-06-21 19:58:20 +0000222 // If the clip is a rectangle then just set the scissor. Otherwise, create
223 // a stencil mask.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000224 if (clipDataIn->fClipStack->isRect()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000225 fGpu->enableScissor(bounds);
226 this->setGpuStencil();
227 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000228 }
229
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000230 // use the stencil clip if we can't represent the clip as a rectangle.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000231 bool useStencil = !clipDataIn->fClipStack->isEmpty() && !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000232
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000233 if (useStencil) {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000234 this->createStencilClipMask(*clipDataIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000235 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000236 // This must occur after createStencilClipMask. That function may change
237 // the scissor. Also, it only guarantees that the stencil mask is correct
238 // within the bounds it was passed, so we must use both stencil and scissor
239 // test to the bounds for the final draw.
240 fGpu->enableScissor(bounds);
241 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000242 return true;
243}
244
245#define VISUALIZE_COMPLEX_CLIP 0
246
247#if VISUALIZE_COMPLEX_CLIP
248 #include "GrRandom.h"
249 GrRandom gRandom;
250 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
251#else
252 #define SET_RANDOM_COLOR
253#endif
254
255namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000256/**
257 * Does "container" contain "containee"? If either is empty then
258 * no containment is possible.
259 */
260bool contains(const SkRect& container, const SkIRect& containee) {
261 return !containee.isEmpty() && !container.isEmpty() &&
262 container.fLeft <= SkIntToScalar(containee.fLeft) &&
263 container.fTop <= SkIntToScalar(containee.fTop) &&
264 container.fRight >= SkIntToScalar(containee.fRight) &&
265 container.fBottom >= SkIntToScalar(containee.fBottom);
266}
267
268
robertphillips@google.comf294b772012-04-27 14:29:26 +0000269////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000270// determines how many elements at the head of the clip can be skipped and
271// whether the initial clear should be to the inside- or outside-the-clip value,
272// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000273const GrClip::Iter::Clip* process_initial_clip_elements(
274 GrClip::Iter* iter,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000275 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000276 bool* clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000277 SkRegion::Op* firstOp) {
278
279 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000280
281 // logically before the first element of the clip stack is
282 // processed the clip is entirely open. However, depending on the
283 // first set op we may prefer to clear to 0 for performance. We may
284 // also be able to skip the initial clip paths/rects. We loop until
285 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000286 bool done = false;
287 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000288
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000289 const GrClip::Iter::Clip* clip = NULL;
290
291 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
292 NULL != clip && !done;
293 clip = iter->next()) {
294 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000295 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000296 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000297 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000298 *clearToInside = false;
299 done = true;
300 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000301 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000302 // if this element contains the entire bounds then we
303 // can skip it.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000304 if (NULL != clip->fRect && contains(*clip->fRect, bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000305 break;
306 }
307 // if everything is initially clearToInside then intersect is
308 // same as clear to 0 and treat as a replace. Otherwise,
309 // set stays empty.
310 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000311 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000312 *clearToInside = false;
313 done = true;
314 }
315 break;
316 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000317 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000318 // if everything is initially outside then union is
319 // same as replace. Otherwise, every pixel is still
320 // clearToInside
321 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000322 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000323 done = true;
324 }
325 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000326 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000327 // xor is same as difference or replace both of which
328 // can be 1-pass instead of 2 for xor.
329 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000330 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000332 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000333 }
334 done = true;
335 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000336 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000337 // if all pixels are clearToInside then we have to process the
338 // difference, otherwise it has no effect and all pixels
339 // remain outside.
340 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000341 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000342 done = true;
343 }
344 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000345 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000346 // if all pixels are clearToInside then reverse difference
347 // produces empty set. Otherise it is same as replace
348 if (*clearToInside) {
349 *clearToInside = false;
350 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000351 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000352 done = true;
353 }
354 break;
355 default:
356 GrCrash("Unknown set op.");
357 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000358
359 if (done) {
360 // we need to break out here (rather than letting the test in
361 // the loop do it) since backing up the iterator is very expensive
362 break;
363 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000364 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000365 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000366}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000367
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000368}
369
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370
371namespace {
372
373////////////////////////////////////////////////////////////////////////////////
374// set up the OpenGL blend function to perform the specified
375// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000376void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377
378 switch (op) {
379 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000380 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000381 break;
382 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000383 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000384 break;
385 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000386 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387 break;
388 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000389 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000390 break;
391 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000392 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000393 break;
394 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000395 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000396 break;
397 default:
398 GrAssert(false);
399 break;
400 }
401}
402
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000404bool draw_path_in_software(GrContext* context,
405 GrGpu* gpu,
406 const SkPath& path,
407 GrPathFill fill,
408 bool doAA,
409 const GrIRect& resultBounds) {
410
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000411 SkAutoTUnref<GrTexture> texture(
412 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
413 resultBounds, fill,
414 doAA, NULL));
415 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000416 return false;
417 }
418
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000419 // The ClipMaskManager accumulates the clip mask in the UL corner
420 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000421
bsalomon@google.come3d32162012-07-20 13:37:06 +0000422 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000423
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000424 GrAssert(!GrIsFillInverted(fill));
425 return true;
426}
427
428
429////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000430bool draw_path(GrContext* context,
431 GrGpu* gpu,
432 const SkPath& path,
433 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000434 bool doAA,
435 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000436
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000437 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000439 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000440 }
441
bsalomon@google.come3d32162012-07-20 13:37:06 +0000442 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000443 return true;
444}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000445
446}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000447
448////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000449bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000450 const GrClip::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000451 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000452 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000453 GrAssert(NULL != drawState);
454
455 drawState->setRenderTarget(target->asRenderTarget());
456
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000457 if (NULL != clip->fRect) {
458 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000459 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000460 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000461 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000462 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000463 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000465 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000466 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000467 *clip->fPath,
468 get_path_fill(*clip->fPath),
469 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000470 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000471 }
472 return true;
473}
474
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000475void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000476 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000477 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000478 GrAssert(NULL != drawState);
479
480 // no AA here since it is encoded in the texture
481 drawState->setRenderTarget(target->asRenderTarget());
482
483 GrMatrix sampleM;
484 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000485
bsalomon@google.comb8670992012-07-25 21:27:09 +0000486 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000487 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000488
robertphillips@google.comf105b102012-05-14 12:18:26 +0000489 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
490 SkIntToScalar(target->height()));
491
bsalomon@google.come3d32162012-07-20 13:37:06 +0000492 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000493
tomhudson@google.com676e6602012-07-10 17:21:48 +0000494 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495}
496
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000497// get a texture to act as a temporary buffer for AA clip boolean operations
498// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000499void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000500 GrAutoScratchTexture* temp) {
501 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000502 // we've already allocated the temp texture
503 return;
504 }
505
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000506 GrTextureDesc desc;
507 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
508 desc.fWidth = bounds.width();
509 desc.fHeight = bounds.height();
510 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000511
robertphillips@google.com2c756812012-05-22 20:28:23 +0000512 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000513}
514
robertphillips@google.comf105b102012-05-14 12:18:26 +0000515
516void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000517 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000518 // Since we are setting up the cache we know the last lookup was a miss
519 // Free up the currently cached mask so it can be reused
520 fAACache.reset();
521
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000522 GrTextureDesc desc;
523 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
524 desc.fWidth = bounds.width();
525 desc.fHeight = bounds.height();
526 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000527
528 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000529}
530
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000531////////////////////////////////////////////////////////////////////////////////
532// Shared preamble between gpu and SW-only AA clip mask creation paths.
533// Handles caching, determination of clip mask bound & allocation (if needed)
534// of the result texture
535// 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 +0000536bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000537 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000538 GrIRect* resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000539 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000540 GrAssert(origDrawState->isClipState());
541
542 GrRenderTarget* rt = origDrawState->getRenderTarget();
543 GrAssert(NULL != rt);
544
545 GrRect rtRect;
546 rtRect.setLTRB(0, 0,
547 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
548
549 // unlike the stencil path the alpha path is not bound to the size of the
550 // render target - determine the minimum size required for the mask
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000551 GrRect bounds = clipDataIn.fClipStack->getConservativeBounds();
552
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000553 if (!bounds.intersect(rtRect)) {
554 // the mask will be empty in this case
555 GrAssert(false);
556 bounds.setEmpty();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000557 }
558
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000559 GrIRect intBounds;
560 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561
562 // need to outset a pixel since the standard bounding box computation
563 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000564 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000565
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000566 // TODO: make sure we don't outset if bounds are still 0,0 @ min
567
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000568 if (fAACache.canReuse(*clipDataIn.fClipStack,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000569 intBounds.width(),
570 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000571 *result = fAACache.getLastMask();
572 fAACache.getLastBound(resultBounds);
573 return true;
574 }
575
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000576 this->setupCache(*clipDataIn.fClipStack, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000577
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000578 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000579 return false;
580}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000581
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000582////////////////////////////////////////////////////////////////////////////////
583// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000584bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000585 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000586 GrIRect *resultBounds) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000587 GrAssert(NULL != resultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000588 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
589
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000590 if (this->clipMaskPreamble(clipDataIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000591 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000592 return true;
593 }
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.com6b70a7b2012-05-11 15:32:48 +0000606 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000607 // if we were able to trim down the size of the mask we need to
608 // offset the paths & rects that will be used to compute it
609 GrMatrix m;
610
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000611 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
612 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000614 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000615 }
616
617 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000618 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
619
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000620 GrClip::Iter iter(*clipDataIn.fClipStack, GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000621 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
622 *resultBounds,
623 &clearToInside,
624 &firstOp);
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.
655 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000656 NULL != clip->fRect &&
657 contains(*clip->fRect, *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000658 continue;
659 }
660
robertphillips@google.comf105b102012-05-14 12:18:26 +0000661 getTemp(*resultBounds, &temp);
662 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000663 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000664 return false;
665 }
666
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000668 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000669
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000670 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000671 this->drawClipShape(temp.texture(), clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000672
673 // TODO: rather than adding these two translations here
674 // compute the bounding box needed to render the texture
675 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000676 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000677 GrMatrix m;
678
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000679 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
680 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000681
682 drawState->preConcatViewMatrix(m);
683 }
684
685 // Now draw into the accumulator using the real operation
686 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000687 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000688 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000690 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000691 GrMatrix m;
692
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000693 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
694 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000695
696 drawState->preConcatViewMatrix(m);
697 }
698
699 } else {
700 // all the remaining ops can just be directly draw into
701 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000702 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000703 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000704 }
705 }
706
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000707 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000708 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000709 return true;
710}
711
712////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713// Create a 1-bit clip mask in the stencil buffer
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000714bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000715 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000717 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000719 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000720 GrAssert(drawState->isClipState());
721
722 GrRenderTarget* rt = drawState->getRenderTarget();
723 GrAssert(NULL != rt);
724
725 // TODO: dynamically attach a SB when needed.
726 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
727 if (NULL == stencilBuffer) {
728 return false;
729 }
730
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000731 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000733 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734
735 // we set the current clip to the bounds so that our recursive
736 // draws are scissored to them. We use the copy of the complex clip
737 // we just stashed on the SB to render from. We set it back after
738 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000739 const GrClipData* oldClipData = fGpu->getClip();
740
741 GrClip newClipStack(bounds);
742 GrClipData newClipData;
743 newClipData.fClipStack = &newClipStack;
744
745 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000747 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
748 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000750 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752#if !VISUALIZE_COMPLEX_CLIP
753 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
754#endif
755
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756 int clipBit = stencilBuffer->bits();
757 SkASSERT((clipBit <= 16) &&
758 "Ganesh only handles 16b or smaller stencil buffers");
759 clipBit = (1 << (clipBit-1));
760
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000761 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762
763 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000764 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
765
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000766 GrClip::Iter iter(*oldClipData->fClipStack,
767 GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000768 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000769 rtRect,
770 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000771 &firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772
bsalomon@google.coma3201942012-06-21 19:58:20 +0000773 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000774 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775
776 // walk through each clip element and perform its set op
777 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000778 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000779 GrPathFill fill;
780 bool fillInverted;
781 // enabled at bottom of loop
782 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000783 // if the target is MSAA then we want MSAA enabled when the clip is soft
784 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000785 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000786 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
787 } else {
788 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
789 }
790 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791
792 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000793 // directly to the stencil buffer
794 // with a non-inverted fill rule
795 // without extra passes to
796 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000798 SkRegion::Op op = clip->fOp;
799 if (first) {
800 first = false;
801 op = firstOp;
802 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000803
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000804 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000805 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000806 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000808 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 fillInverted = false;
810 // there is no point in intersecting a screen filling
811 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000812 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000813 contains(*clip->fRect, rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000814 continue;
815 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000816 } else if (NULL != clip->fPath) {
817 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 fillInverted = GrIsFillInverted(fill);
819 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000820 clipPath = clip->fPath;
robertphillips@google.com2c756812012-05-22 20:28:23 +0000821 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000822 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000823 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 if (NULL == pr) {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000825 fGpu->setClip(oldClipData); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000826 return false;
827 }
828 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000829 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830 }
831
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000832 int passes;
833 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
834
835 bool canDrawDirectToClip; // Given the renderer, the element,
836 // fill rule, and set operation can
837 // we render the element directly to
838 // stencil bit used for clipping.
839 canDrawDirectToClip =
840 GrStencilSettings::GetClipPasses(op,
841 canRenderDirectToStencil,
842 clipBit,
843 fillInverted,
844 &passes, stencilSettings);
845
846 // draw the element to the client stencil bits if necessary
847 if (!canDrawDirectToClip) {
848 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
849 kIncClamp_StencilOp,
850 kIncClamp_StencilOp,
851 kAlways_StencilFunc,
852 0xffff,
853 0x0000,
854 0xffff);
855 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000856 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000857 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000858 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000859 } else {
860 if (canRenderDirectToStencil) {
861 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000862 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000863 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000864 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000865 }
866 }
867 }
868
869 // now we modify the clip bit by rendering either the clip
870 // element directly or a bounding rect of the entire clip.
871 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
872 for (int p = 0; p < passes; ++p) {
873 *drawState->stencil() = stencilSettings[p];
874 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000875 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000876 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000877 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000878 } else {
879 SET_RANDOM_COLOR
bsalomon@google.come3d32162012-07-20 13:37:06 +0000880 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000881 }
882 } else {
883 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000884 GrRect rect = GrRect::MakeLTRB(
885 SkIntToScalar(bounds.fLeft),
886 SkIntToScalar(bounds.fTop),
887 SkIntToScalar(bounds.fRight),
888 SkIntToScalar(bounds.fBottom));
bsalomon@google.come3d32162012-07-20 13:37:06 +0000889 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000890 }
891 }
892 }
893 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000894 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000895 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000896 // set this last because recursive draws may overwrite it back to kNone.
897 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
898 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000899 return true;
900}
901
bsalomon@google.com411dad02012-06-05 20:24:20 +0000902// mapping of clip-respecting stencil funcs to normal stencil funcs
903// mapping depends on whether stencil-clipping is in effect.
904static const GrStencilFunc
905 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
906 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
907 // In the Clip Funcs
908 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
909 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
910 kLess_StencilFunc, // kLessIfInClip_StencilFunc
911 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
912 // Special in the clip func that forces user's ref to be 0.
913 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
914 // make ref 0 and do normal nequal.
915 },
916 {// Stencil-Clipping is ENABLED
917 // In the Clip Funcs
918 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
919 // eq stencil clip bit, mask
920 // out user bits.
921
922 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
923 // add stencil bit to mask and ref
924
925 kLess_StencilFunc, // kLessIfInClip_StencilFunc
926 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
927 // for both of these we can add
928 // the clip bit to the mask and
929 // ref and compare as normal
930 // Special in the clip func that forces user's ref to be 0.
931 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
932 // make ref have only the clip bit set
933 // and make comparison be less
934 // 10..0 < 1..user_bits..
935 }
936};
937
bsalomon@google.coma3201942012-06-21 19:58:20 +0000938namespace {
939// Sets the settings to clip against the stencil buffer clip while ignoring the
940// client bits.
941const GrStencilSettings& basic_apply_stencil_clip_settings() {
942 // stencil settings to use when clip is in stencil
943 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
944 kKeep_StencilOp,
945 kKeep_StencilOp,
946 kAlwaysIfInClip_StencilFunc,
947 0x0000,
948 0x0000,
949 0x0000);
950 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
951}
952}
953
954void GrClipMaskManager::setGpuStencil() {
955 // We make two copies of the StencilSettings here (except in the early
956 // exit scenario. One copy from draw state to the stack var. Then another
957 // from the stack var to the gpu. We could make this class hold a ptr to
958 // GrGpu's fStencilSettings and eliminate the stack copy here.
959
960 const GrDrawState& drawState = fGpu->getDrawState();
961
962 // use stencil for clipping if clipping is enabled and the clip
963 // has been written into the stencil.
964 GrClipMaskManager::StencilClipMode clipMode;
965 if (this->isClipInStencil() && drawState.isClipState()) {
966 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
967 // We can't be modifying the clip and respecting it at the same time.
968 GrAssert(!drawState.isStateFlagEnabled(
969 GrGpu::kModifyStencilClip_StateBit));
970 } else if (drawState.isStateFlagEnabled(
971 GrGpu::kModifyStencilClip_StateBit)) {
972 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
973 } else {
974 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
975 }
976
977 GrStencilSettings settings;
978 // The GrGpu client may not be using the stencil buffer but we may need to
979 // enable it in order to respect a stencil clip.
980 if (drawState.getStencil().isDisabled()) {
981 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
982 settings = basic_apply_stencil_clip_settings();
983 } else {
984 fGpu->disableStencil();
985 return;
986 }
987 } else {
988 settings = drawState.getStencil();
989 }
990
991 // TODO: dynamically attach a stencil buffer
992 int stencilBits = 0;
993 GrStencilBuffer* stencilBuffer =
994 drawState.getRenderTarget()->getStencilBuffer();
995 if (NULL != stencilBuffer) {
996 stencilBits = stencilBuffer->bits();
997 }
998
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000999 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
1000 !settings.usesWrapOp());
1001 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001002 this->adjustStencilParams(&settings, clipMode, stencilBits);
1003 fGpu->setStencilSettings(settings);
1004}
1005
1006void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1007 StencilClipMode mode,
1008 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001009 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001010
1011 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001012 // We assume that this clip manager itself is drawing to the GrGpu and
1013 // has already setup the correct values.
1014 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001015 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001016
bsalomon@google.com411dad02012-06-05 20:24:20 +00001017 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1018 unsigned int userBits = clipBit - 1;
1019
bsalomon@google.coma3201942012-06-21 19:58:20 +00001020 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
1021 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001022
bsalomon@google.coma3201942012-06-21 19:58:20 +00001023 bool finished = false;
1024 while (!finished) {
1025 GrStencilFunc func = settings->func(face);
1026 uint16_t writeMask = settings->writeMask(face);
1027 uint16_t funcMask = settings->funcMask(face);
1028 uint16_t funcRef = settings->funcRef(face);
1029
1030 GrAssert((unsigned) func < kStencilFuncCount);
1031
1032 writeMask &= userBits;
1033
1034 if (func >= kBasicStencilFuncCount) {
1035 int respectClip = kRespectClip_StencilClipMode == mode;
1036 if (respectClip) {
1037 // The GrGpu class should have checked this
1038 GrAssert(this->isClipInStencil());
1039 switch (func) {
1040 case kAlwaysIfInClip_StencilFunc:
1041 funcMask = clipBit;
1042 funcRef = clipBit;
1043 break;
1044 case kEqualIfInClip_StencilFunc:
1045 case kLessIfInClip_StencilFunc:
1046 case kLEqualIfInClip_StencilFunc:
1047 funcMask = (funcMask & userBits) | clipBit;
1048 funcRef = (funcRef & userBits) | clipBit;
1049 break;
1050 case kNonZeroIfInClip_StencilFunc:
1051 funcMask = (funcMask & userBits) | clipBit;
1052 funcRef = clipBit;
1053 break;
1054 default:
1055 GrCrash("Unknown stencil func");
1056 }
1057 } else {
1058 funcMask &= userBits;
1059 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001060 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001061 const GrStencilFunc* table =
1062 gSpecialToBasicStencilFunc[respectClip];
1063 func = table[func - kBasicStencilFuncCount];
1064 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001065 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001066 funcMask &= userBits;
1067 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001068 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001069
1070 settings->setFunc(face, func);
1071 settings->setWriteMask(face, writeMask);
1072 settings->setFuncMask(face, funcMask);
1073 settings->setFuncRef(face, funcRef);
1074
1075 if (GrStencilSettings::kFront_Face == face) {
1076 face = GrStencilSettings::kBack_Face;
1077 finished = !twoSided;
1078 } else {
1079 finished = true;
1080 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001081 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001082 if (!twoSided) {
1083 settings->copyFrontSettingsToBack();
1084 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001085}
1086
1087////////////////////////////////////////////////////////////////////////////////
1088
robertphillips@google.comfa662942012-05-17 12:20:22 +00001089namespace {
1090
1091GrPathFill invert_fill(GrPathFill fill) {
1092 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001093 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1094 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1095 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1096 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1097 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001098 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001099 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1100 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1101 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1102 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1103 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1104 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001105 return gInvertedFillTable[fill];
1106}
1107
1108}
1109
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001110bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001111 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001112 GrIRect* resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001113 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001114
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001115 if (this->clipMaskPreamble(clipDataIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001116 return true;
1117 }
1118
robertphillips@google.comf105b102012-05-14 12:18:26 +00001119 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001120 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001121 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001122 return false;
1123 }
1124
robertphillips@google.com2c756812012-05-22 20:28:23 +00001125 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001126
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001127 helper.init(*resultBounds, NULL);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001128
robertphillips@google.comfa662942012-05-17 12:20:22 +00001129 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001130 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1131
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001132 GrClip::Iter iter(*clipDataIn.fClipStack, GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001133 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001134 *resultBounds,
1135 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001136 &firstOp);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001137
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001138 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001139
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001140 bool first = true;
1141 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001142
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001143 SkRegion::Op op = clip->fOp;
1144 if (first) {
1145 first = false;
1146 op = firstOp;
1147 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001148
1149 if (SkRegion::kIntersect_Op == op ||
1150 SkRegion::kReverseDifference_Op == op) {
1151 // Intersect and reverse difference require modifying pixels
1152 // outside of the geometry that is being "drawn". In both cases
1153 // we erase all the pixels outside of the geometry but
1154 // leave the pixels inside the geometry alone. For reverse
1155 // difference we invert all the pixels before clearing the ones
1156 // outside the geometry.
1157 if (SkRegion::kReverseDifference_Op == op) {
1158 SkRect temp = SkRect::MakeLTRB(
1159 SkIntToScalar(resultBounds->left()),
1160 SkIntToScalar(resultBounds->top()),
1161 SkIntToScalar(resultBounds->right()),
1162 SkIntToScalar(resultBounds->bottom()));
1163
1164 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001165 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001166 }
1167
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001168 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001169
1170 // convert the rect to a path so we can invert the fill
1171 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001172 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001173
1174 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001175 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001176 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001177 } else if (NULL != clip->fPath) {
1178 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001179 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001180 invert_fill(get_path_fill(*clip->fPath)),
1181 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001182 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001183 }
1184
1185 continue;
1186 }
1187
1188 // The other ops (union, xor, diff) only affect pixels inside
1189 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001190 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001191
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001192 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001193 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001194 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001195
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001196 } else if (NULL != clip->fPath) {
1197 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001198 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001199 get_path_fill(*clip->fPath),
1200 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001201 }
1202 }
1203
robertphillips@google.comfa662942012-05-17 12:20:22 +00001204 // Because we are using the scratch texture cache, "accum" may be
1205 // larger than expected and have some cruft in the areas we aren't using.
1206 // Clear it out.
1207
1208 // TODO: need a simpler way to clear the texture - can we combine
1209 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001210 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001211 GrAssert(NULL != drawState);
1212 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001213 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001214 // can't leave the accum bound as a rendertarget
1215 drawState->setRenderTarget(temp);
1216
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001217 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001218
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001219 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001220
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001221 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001222 return true;
1223}
1224
robertphillips@google.comf294b772012-04-27 14:29:26 +00001225////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001226void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001227 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001228}