blob: ea5a2cd6939f931b3d1d4fa59dd2b387b8b10e48 [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;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000150 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000151
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000152 clipDataIn->getConservativeBounds(rt, &bounds, &isIntersectionOfRects);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000153 if (bounds.isEmpty()) {
154 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000155 }
156
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000157 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
158 GrAssert(requiresAA == clipDataIn->fClipStack->requiresAA());
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000159
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000160#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000161 // If MSAA is enabled we can do everything in the stencil buffer.
162 // Otherwise check if we should just create the entire clip mask
163 // in software (this will only happen if the clip mask is anti-aliased
164 // and too complex for the gpu to handle in its entirety)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000165 if (0 == rt->numSamples() &&
166 requiresAA &&
167 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000168 // The clip geometry is complex enough that it will be more
169 // efficient to create it entirely in software
170 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000171 GrIRect bound;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000172 if (this->createSoftwareClipMask(*clipDataIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000173 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000174 fGpu->disableScissor();
175 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000176 return true;
177 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000178
179 // if SW clip mask creation fails fall through to the other
180 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000181 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000182#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000183
robertphillips@google.comf294b772012-04-27 14:29:26 +0000184#if GR_AA_CLIP
185 // If MSAA is enabled use the (faster) stencil path for AA clipping
186 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000187 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000188 // Since we are going to create a destination texture of the correct
189 // size for the mask (rather than being bound by the size of the
190 // render target) we aren't going to use scissoring like the stencil
191 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000192 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000193 GrIRect bound;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000194 if (this->createAlphaClipMask(*clipDataIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000195 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000196 fGpu->disableScissor();
197 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000198 return true;
199 }
200
201 // if alpha clip mask creation fails fall through to the stencil
202 // buffer method
203 }
204#endif // GR_AA_CLIP
205
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000206 // Either a hard (stencil buffer) clip was explicitly requested or
207 // an antialiased clip couldn't be created. In either case, free up
208 // the texture in the antialiased mask cache.
209 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000210 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
211 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000212 // AA cache.
213 fAACache.reset();
214
bsalomon@google.coma3201942012-06-21 19:58:20 +0000215 // If the clip is a rectangle then just set the scissor. Otherwise, create
216 // a stencil mask.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000217 if (clipDataIn->fClipStack->isRect()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000218 fGpu->enableScissor(bounds);
219 this->setGpuStencil();
220 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000221 }
222
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000223 // use the stencil clip if we can't represent the clip as a rectangle.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000224 bool useStencil = !clipDataIn->fClipStack->isEmpty() && !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000225
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000226 if (useStencil) {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000227 this->createStencilClipMask(*clipDataIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000228 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000229 // This must occur after createStencilClipMask. That function may change
230 // the scissor. Also, it only guarantees that the stencil mask is correct
231 // within the bounds it was passed, so we must use both stencil and scissor
232 // test to the bounds for the final draw.
233 fGpu->enableScissor(bounds);
234 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000235 return true;
236}
237
238#define VISUALIZE_COMPLEX_CLIP 0
239
240#if VISUALIZE_COMPLEX_CLIP
241 #include "GrRandom.h"
242 GrRandom gRandom;
243 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
244#else
245 #define SET_RANDOM_COLOR
246#endif
247
248namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000249/**
250 * Does "container" contain "containee"? If either is empty then
251 * no containment is possible.
252 */
253bool contains(const SkRect& container, const SkIRect& containee) {
254 return !containee.isEmpty() && !container.isEmpty() &&
255 container.fLeft <= SkIntToScalar(containee.fLeft) &&
256 container.fTop <= SkIntToScalar(containee.fTop) &&
257 container.fRight >= SkIntToScalar(containee.fRight) &&
258 container.fBottom >= SkIntToScalar(containee.fBottom);
259}
260
261
robertphillips@google.comf294b772012-04-27 14:29:26 +0000262////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000263// determines how many elements at the head of the clip can be skipped and
264// whether the initial clear should be to the inside- or outside-the-clip value,
265// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000266const GrClip::Iter::Clip* process_initial_clip_elements(
267 GrClip::Iter* iter,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000268 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000269 bool* clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000270 SkRegion::Op* firstOp) {
271
272 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000273
274 // logically before the first element of the clip stack is
275 // processed the clip is entirely open. However, depending on the
276 // first set op we may prefer to clear to 0 for performance. We may
277 // also be able to skip the initial clip paths/rects. We loop until
278 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000279 bool done = false;
280 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000281
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000282 const GrClip::Iter::Clip* clip = NULL;
283
284 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
285 NULL != clip && !done;
286 clip = iter->next()) {
287 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000288 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000290 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291 *clearToInside = false;
292 done = true;
293 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000294 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295 // if this element contains the entire bounds then we
296 // can skip it.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000297 if (NULL != clip->fRect && contains(*clip->fRect, bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000298 break;
299 }
300 // if everything is initially clearToInside then intersect is
301 // same as clear to 0 and treat as a replace. Otherwise,
302 // set stays empty.
303 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000304 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000305 *clearToInside = false;
306 done = true;
307 }
308 break;
309 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000310 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000311 // if everything is initially outside then union is
312 // same as replace. Otherwise, every pixel is still
313 // clearToInside
314 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000315 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000316 done = true;
317 }
318 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000319 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320 // xor is same as difference or replace both of which
321 // can be 1-pass instead of 2 for xor.
322 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000323 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000324 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000325 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000326 }
327 done = true;
328 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000329 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000330 // if all pixels are clearToInside then we have to process the
331 // difference, otherwise it has no effect and all pixels
332 // remain outside.
333 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000334 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 done = true;
336 }
337 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000338 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 // if all pixels are clearToInside then reverse difference
340 // produces empty set. Otherise it is same as replace
341 if (*clearToInside) {
342 *clearToInside = false;
343 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000344 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345 done = true;
346 }
347 break;
348 default:
349 GrCrash("Unknown set op.");
350 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000351
352 if (done) {
353 // we need to break out here (rather than letting the test in
354 // the loop do it) since backing up the iterator is very expensive
355 break;
356 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000357 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000358 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000359}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000361}
362
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363
364namespace {
365
366////////////////////////////////////////////////////////////////////////////////
367// set up the OpenGL blend function to perform the specified
368// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000369void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370
371 switch (op) {
372 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000373 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374 break;
375 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000376 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377 break;
378 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000379 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380 break;
381 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000382 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000383 break;
384 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000385 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386 break;
387 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000388 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389 break;
390 default:
391 GrAssert(false);
392 break;
393 }
394}
395
robertphillips@google.comf294b772012-04-27 14:29:26 +0000396////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000397bool draw_path_in_software(GrContext* context,
398 GrGpu* gpu,
399 const SkPath& path,
400 GrPathFill fill,
401 bool doAA,
402 const GrIRect& resultBounds) {
403
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000404 SkAutoTUnref<GrTexture> texture(
405 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
406 resultBounds, fill,
407 doAA, NULL));
408 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000409 return false;
410 }
411
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000412 // The ClipMaskManager accumulates the clip mask in the UL corner
413 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000414
bsalomon@google.come3d32162012-07-20 13:37:06 +0000415 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000416
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000417 GrAssert(!GrIsFillInverted(fill));
418 return true;
419}
420
421
422////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000423bool draw_path(GrContext* context,
424 GrGpu* gpu,
425 const SkPath& path,
426 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000427 bool doAA,
428 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000429
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000430 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000432 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000433 }
434
bsalomon@google.come3d32162012-07-20 13:37:06 +0000435 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000436 return true;
437}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000438
439}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000440
441////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000442bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000443 const GrClip::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000444 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000445 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000446 GrAssert(NULL != drawState);
447
448 drawState->setRenderTarget(target->asRenderTarget());
449
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000450 if (NULL != clip->fRect) {
451 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000452 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000453 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000454 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000455 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000456 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000457 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000458 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000459 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000460 *clip->fPath,
461 get_path_fill(*clip->fPath),
462 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000463 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464 }
465 return true;
466}
467
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000468void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000469 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000470 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000471 GrAssert(NULL != drawState);
472
473 // no AA here since it is encoded in the texture
474 drawState->setRenderTarget(target->asRenderTarget());
475
476 GrMatrix sampleM;
477 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000478
bsalomon@google.comb8670992012-07-25 21:27:09 +0000479 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000480 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000481
robertphillips@google.comf105b102012-05-14 12:18:26 +0000482 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
483 SkIntToScalar(target->height()));
484
bsalomon@google.come3d32162012-07-20 13:37:06 +0000485 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000486
tomhudson@google.com676e6602012-07-10 17:21:48 +0000487 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000488}
489
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000490// get a texture to act as a temporary buffer for AA clip boolean operations
491// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000492void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000493 GrAutoScratchTexture* temp) {
494 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000495 // we've already allocated the temp texture
496 return;
497 }
498
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000499 GrTextureDesc desc;
500 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
501 desc.fWidth = bounds.width();
502 desc.fHeight = bounds.height();
503 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000504
robertphillips@google.com2c756812012-05-22 20:28:23 +0000505 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000506}
507
robertphillips@google.comf105b102012-05-14 12:18:26 +0000508
509void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000510 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000511 // Since we are setting up the cache we know the last lookup was a miss
512 // Free up the currently cached mask so it can be reused
513 fAACache.reset();
514
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000515 GrTextureDesc desc;
516 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
517 desc.fWidth = bounds.width();
518 desc.fHeight = bounds.height();
519 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000520
521 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000522}
523
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000524////////////////////////////////////////////////////////////////////////////////
525// Shared preamble between gpu and SW-only AA clip mask creation paths.
526// Handles caching, determination of clip mask bound & allocation (if needed)
527// of the result texture
528// 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 +0000529bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000530 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000531 GrIRect* resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000532 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000533 GrAssert(origDrawState->isClipState());
534
535 GrRenderTarget* rt = origDrawState->getRenderTarget();
536 GrAssert(NULL != rt);
537
robertphillips@google.comf294b772012-04-27 14:29:26 +0000538 // unlike the stencil path the alpha path is not bound to the size of the
539 // render target - determine the minimum size required for the mask
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000540 GrIRect intBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000541 clipDataIn.getConservativeBounds(rt, &intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000542
543 // need to outset a pixel since the standard bounding box computation
544 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000545 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000546
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000547 // TODO: make sure we don't outset if bounds are still 0,0 @ min
548
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000549 if (fAACache.canReuse(*clipDataIn.fClipStack,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000550 intBounds.width(),
551 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000552 *result = fAACache.getLastMask();
553 fAACache.getLastBound(resultBounds);
554 return true;
555 }
556
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000557 this->setupCache(*clipDataIn.fClipStack, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000558
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000559 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000560 return false;
561}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000563////////////////////////////////////////////////////////////////////////////////
564// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000565bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000566 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000567 GrIRect *resultBounds) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000568 GrAssert(NULL != resultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000569 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
570
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000571 if (this->clipMaskPreamble(clipDataIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000572 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000573 return true;
574 }
575
robertphillips@google.comf105b102012-05-14 12:18:26 +0000576 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000577 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000578 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000579 return false;
580 }
581
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000582 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
583 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000584
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000585 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000587 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000588 // if we were able to trim down the size of the mask we need to
589 // offset the paths & rects that will be used to compute it
590 GrMatrix m;
591
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000592 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
593 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000595 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000596 }
597
598 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000599 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
600
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000601 GrClip::Iter iter(*clipDataIn.fClipStack, GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000602 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
603 *resultBounds,
604 &clearToInside,
605 &firstOp);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000606
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000607 fGpu->clear(NULL,
608 clearToInside ? 0xffffffff : 0x00000000,
609 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000610
robertphillips@google.comf105b102012-05-14 12:18:26 +0000611 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000612 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000614 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000615
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000616 SkRegion::Op op = clip->fOp;
617 if (first) {
618 first = false;
619 op = firstOp;
620 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000621
622 if (SkRegion::kReplace_Op == op) {
623 // TODO: replace is actually a lot faster then intersection
624 // for this path - refactor the stencil path so it can handle
625 // replace ops and alter GrClip to allow them through
626
627 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000628 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000629
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000630 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000631 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632
633 } else if (SkRegion::kReverseDifference_Op == op ||
634 SkRegion::kIntersect_Op == op) {
635 // there is no point in intersecting a screen filling rectangle.
636 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000637 NULL != clip->fRect &&
638 contains(*clip->fRect, *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000639 continue;
640 }
641
robertphillips@google.comf105b102012-05-14 12:18:26 +0000642 getTemp(*resultBounds, &temp);
643 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000644 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000645 return false;
646 }
647
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000649 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000650
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000651 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000652 this->drawClipShape(temp.texture(), clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000653
654 // TODO: rather than adding these two translations here
655 // compute the bounding box needed to render the texture
656 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000657 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000658 GrMatrix m;
659
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000660 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
661 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662
663 drawState->preConcatViewMatrix(m);
664 }
665
666 // Now draw into the accumulator using the real operation
667 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000668 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000669 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000670
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 } else {
681 // all the remaining ops can just be directly draw into
682 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000683 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000684 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000685 }
686 }
687
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000688 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000689 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000690 return true;
691}
692
693////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694// Create a 1-bit clip mask in the stencil buffer
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000695bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000696 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000698 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000699
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000700 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000701 GrAssert(drawState->isClipState());
702
703 GrRenderTarget* rt = drawState->getRenderTarget();
704 GrAssert(NULL != rt);
705
706 // TODO: dynamically attach a SB when needed.
707 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
708 if (NULL == stencilBuffer) {
709 return false;
710 }
711
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000712 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000713
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000714 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715
716 // we set the current clip to the bounds so that our recursive
717 // draws are scissored to them. We use the copy of the complex clip
718 // we just stashed on the SB to render from. We set it back after
719 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000720 const GrClipData* oldClipData = fGpu->getClip();
721
722 GrClip newClipStack(bounds);
723 GrClipData newClipData;
724 newClipData.fClipStack = &newClipStack;
725
726 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000728 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
729 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000731 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000732
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733#if !VISUALIZE_COMPLEX_CLIP
734 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
735#endif
736
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737 int clipBit = stencilBuffer->bits();
738 SkASSERT((clipBit <= 16) &&
739 "Ganesh only handles 16b or smaller stencil buffers");
740 clipBit = (1 << (clipBit-1));
741
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000742 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743
744 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000745 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
746
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000747 GrClip::Iter iter(*oldClipData->fClipStack,
748 GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000749 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000750 rtRect,
751 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000752 &firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753
bsalomon@google.coma3201942012-06-21 19:58:20 +0000754 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000755 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756
757 // walk through each clip element and perform its set op
758 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000759 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760 GrPathFill fill;
761 bool fillInverted;
762 // enabled at bottom of loop
763 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000764 // if the target is MSAA then we want MSAA enabled when the clip is soft
765 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000766 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000767 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
768 } else {
769 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
770 }
771 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772
773 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000774 // directly to the stencil buffer
775 // with a non-inverted fill rule
776 // without extra passes to
777 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000779 SkRegion::Op op = clip->fOp;
780 if (first) {
781 first = false;
782 op = firstOp;
783 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000784
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000786 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000787 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000789 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790 fillInverted = false;
791 // there is no point in intersecting a screen filling
792 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000793 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000794 contains(*clip->fRect, rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000795 continue;
796 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000797 } else if (NULL != clip->fPath) {
798 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799 fillInverted = GrIsFillInverted(fill);
800 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000801 clipPath = clip->fPath;
robertphillips@google.com2c756812012-05-22 20:28:23 +0000802 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000803 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000804 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000805 if (NULL == pr) {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000806 fGpu->setClip(oldClipData); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 return false;
808 }
809 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000810 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 }
812
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 int passes;
814 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
815
816 bool canDrawDirectToClip; // Given the renderer, the element,
817 // fill rule, and set operation can
818 // we render the element directly to
819 // stencil bit used for clipping.
820 canDrawDirectToClip =
821 GrStencilSettings::GetClipPasses(op,
822 canRenderDirectToStencil,
823 clipBit,
824 fillInverted,
825 &passes, stencilSettings);
826
827 // draw the element to the client stencil bits if necessary
828 if (!canDrawDirectToClip) {
829 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
830 kIncClamp_StencilOp,
831 kIncClamp_StencilOp,
832 kAlways_StencilFunc,
833 0xffff,
834 0x0000,
835 0xffff);
836 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000837 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000838 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000839 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000840 } else {
841 if (canRenderDirectToStencil) {
842 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000843 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000845 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000846 }
847 }
848 }
849
850 // now we modify the clip bit by rendering either the clip
851 // element directly or a bounding rect of the entire clip.
852 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
853 for (int p = 0; p < passes; ++p) {
854 *drawState->stencil() = stencilSettings[p];
855 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000856 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000857 SET_RANDOM_COLOR
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 SET_RANDOM_COLOR
bsalomon@google.come3d32162012-07-20 13:37:06 +0000861 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000862 }
863 } else {
864 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000865 GrRect rect = GrRect::MakeLTRB(
866 SkIntToScalar(bounds.fLeft),
867 SkIntToScalar(bounds.fTop),
868 SkIntToScalar(bounds.fRight),
869 SkIntToScalar(bounds.fBottom));
bsalomon@google.come3d32162012-07-20 13:37:06 +0000870 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000871 }
872 }
873 }
874 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000875 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000876 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000877 // set this last because recursive draws may overwrite it back to kNone.
878 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
879 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000880 return true;
881}
882
bsalomon@google.com411dad02012-06-05 20:24:20 +0000883// mapping of clip-respecting stencil funcs to normal stencil funcs
884// mapping depends on whether stencil-clipping is in effect.
885static const GrStencilFunc
886 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
887 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
888 // In the Clip Funcs
889 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
890 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
891 kLess_StencilFunc, // kLessIfInClip_StencilFunc
892 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
893 // Special in the clip func that forces user's ref to be 0.
894 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
895 // make ref 0 and do normal nequal.
896 },
897 {// Stencil-Clipping is ENABLED
898 // In the Clip Funcs
899 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
900 // eq stencil clip bit, mask
901 // out user bits.
902
903 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
904 // add stencil bit to mask and ref
905
906 kLess_StencilFunc, // kLessIfInClip_StencilFunc
907 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
908 // for both of these we can add
909 // the clip bit to the mask and
910 // ref and compare as normal
911 // Special in the clip func that forces user's ref to be 0.
912 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
913 // make ref have only the clip bit set
914 // and make comparison be less
915 // 10..0 < 1..user_bits..
916 }
917};
918
bsalomon@google.coma3201942012-06-21 19:58:20 +0000919namespace {
920// Sets the settings to clip against the stencil buffer clip while ignoring the
921// client bits.
922const GrStencilSettings& basic_apply_stencil_clip_settings() {
923 // stencil settings to use when clip is in stencil
924 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
925 kKeep_StencilOp,
926 kKeep_StencilOp,
927 kAlwaysIfInClip_StencilFunc,
928 0x0000,
929 0x0000,
930 0x0000);
931 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
932}
933}
934
935void GrClipMaskManager::setGpuStencil() {
936 // We make two copies of the StencilSettings here (except in the early
937 // exit scenario. One copy from draw state to the stack var. Then another
938 // from the stack var to the gpu. We could make this class hold a ptr to
939 // GrGpu's fStencilSettings and eliminate the stack copy here.
940
941 const GrDrawState& drawState = fGpu->getDrawState();
942
943 // use stencil for clipping if clipping is enabled and the clip
944 // has been written into the stencil.
945 GrClipMaskManager::StencilClipMode clipMode;
946 if (this->isClipInStencil() && drawState.isClipState()) {
947 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
948 // We can't be modifying the clip and respecting it at the same time.
949 GrAssert(!drawState.isStateFlagEnabled(
950 GrGpu::kModifyStencilClip_StateBit));
951 } else if (drawState.isStateFlagEnabled(
952 GrGpu::kModifyStencilClip_StateBit)) {
953 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
954 } else {
955 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
956 }
957
958 GrStencilSettings settings;
959 // The GrGpu client may not be using the stencil buffer but we may need to
960 // enable it in order to respect a stencil clip.
961 if (drawState.getStencil().isDisabled()) {
962 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
963 settings = basic_apply_stencil_clip_settings();
964 } else {
965 fGpu->disableStencil();
966 return;
967 }
968 } else {
969 settings = drawState.getStencil();
970 }
971
972 // TODO: dynamically attach a stencil buffer
973 int stencilBits = 0;
974 GrStencilBuffer* stencilBuffer =
975 drawState.getRenderTarget()->getStencilBuffer();
976 if (NULL != stencilBuffer) {
977 stencilBits = stencilBuffer->bits();
978 }
979
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000980 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
981 !settings.usesWrapOp());
982 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000983 this->adjustStencilParams(&settings, clipMode, stencilBits);
984 fGpu->setStencilSettings(settings);
985}
986
987void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
988 StencilClipMode mode,
989 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000990 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000991
992 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000993 // We assume that this clip manager itself is drawing to the GrGpu and
994 // has already setup the correct values.
995 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000996 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000997
bsalomon@google.com411dad02012-06-05 20:24:20 +0000998 unsigned int clipBit = (1 << (stencilBitCnt - 1));
999 unsigned int userBits = clipBit - 1;
1000
bsalomon@google.coma3201942012-06-21 19:58:20 +00001001 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
1002 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001003
bsalomon@google.coma3201942012-06-21 19:58:20 +00001004 bool finished = false;
1005 while (!finished) {
1006 GrStencilFunc func = settings->func(face);
1007 uint16_t writeMask = settings->writeMask(face);
1008 uint16_t funcMask = settings->funcMask(face);
1009 uint16_t funcRef = settings->funcRef(face);
1010
1011 GrAssert((unsigned) func < kStencilFuncCount);
1012
1013 writeMask &= userBits;
1014
1015 if (func >= kBasicStencilFuncCount) {
1016 int respectClip = kRespectClip_StencilClipMode == mode;
1017 if (respectClip) {
1018 // The GrGpu class should have checked this
1019 GrAssert(this->isClipInStencil());
1020 switch (func) {
1021 case kAlwaysIfInClip_StencilFunc:
1022 funcMask = clipBit;
1023 funcRef = clipBit;
1024 break;
1025 case kEqualIfInClip_StencilFunc:
1026 case kLessIfInClip_StencilFunc:
1027 case kLEqualIfInClip_StencilFunc:
1028 funcMask = (funcMask & userBits) | clipBit;
1029 funcRef = (funcRef & userBits) | clipBit;
1030 break;
1031 case kNonZeroIfInClip_StencilFunc:
1032 funcMask = (funcMask & userBits) | clipBit;
1033 funcRef = clipBit;
1034 break;
1035 default:
1036 GrCrash("Unknown stencil func");
1037 }
1038 } else {
1039 funcMask &= userBits;
1040 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001041 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001042 const GrStencilFunc* table =
1043 gSpecialToBasicStencilFunc[respectClip];
1044 func = table[func - kBasicStencilFuncCount];
1045 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001046 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001047 funcMask &= userBits;
1048 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001049 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001050
1051 settings->setFunc(face, func);
1052 settings->setWriteMask(face, writeMask);
1053 settings->setFuncMask(face, funcMask);
1054 settings->setFuncRef(face, funcRef);
1055
1056 if (GrStencilSettings::kFront_Face == face) {
1057 face = GrStencilSettings::kBack_Face;
1058 finished = !twoSided;
1059 } else {
1060 finished = true;
1061 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001062 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001063 if (!twoSided) {
1064 settings->copyFrontSettingsToBack();
1065 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001066}
1067
1068////////////////////////////////////////////////////////////////////////////////
1069
robertphillips@google.comfa662942012-05-17 12:20:22 +00001070namespace {
1071
1072GrPathFill invert_fill(GrPathFill fill) {
1073 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001074 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1075 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1076 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1077 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1078 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001079 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001080 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1081 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1082 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1083 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1084 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1085 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001086 return gInvertedFillTable[fill];
1087}
1088
1089}
1090
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001091bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001092 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001093 GrIRect* resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001094 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001095
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001096 if (this->clipMaskPreamble(clipDataIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001097 return true;
1098 }
1099
robertphillips@google.comf105b102012-05-14 12:18:26 +00001100 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001101 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001102 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001103 return false;
1104 }
1105
robertphillips@google.com2c756812012-05-22 20:28:23 +00001106 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001107
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001108 helper.init(*resultBounds, NULL);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001109
robertphillips@google.comfa662942012-05-17 12:20:22 +00001110 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001111 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1112
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001113 GrClip::Iter iter(*clipDataIn.fClipStack, GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001114 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001115 *resultBounds,
1116 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001117 &firstOp);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001118
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001119 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001120
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001121 bool first = true;
1122 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001123
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001124 SkRegion::Op op = clip->fOp;
1125 if (first) {
1126 first = false;
1127 op = firstOp;
1128 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001129
1130 if (SkRegion::kIntersect_Op == op ||
1131 SkRegion::kReverseDifference_Op == op) {
1132 // Intersect and reverse difference require modifying pixels
1133 // outside of the geometry that is being "drawn". In both cases
1134 // we erase all the pixels outside of the geometry but
1135 // leave the pixels inside the geometry alone. For reverse
1136 // difference we invert all the pixels before clearing the ones
1137 // outside the geometry.
1138 if (SkRegion::kReverseDifference_Op == op) {
1139 SkRect temp = SkRect::MakeLTRB(
1140 SkIntToScalar(resultBounds->left()),
1141 SkIntToScalar(resultBounds->top()),
1142 SkIntToScalar(resultBounds->right()),
1143 SkIntToScalar(resultBounds->bottom()));
1144
1145 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001146 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001147 }
1148
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001149 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001150
1151 // convert the rect to a path so we can invert the fill
1152 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001153 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001154
1155 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001156 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001157 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001158 } else if (NULL != clip->fPath) {
1159 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001160 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001161 invert_fill(get_path_fill(*clip->fPath)),
1162 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001163 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001164 }
1165
1166 continue;
1167 }
1168
1169 // The other ops (union, xor, diff) only affect pixels inside
1170 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001171 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001172
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001173 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001174 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001175 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001176
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 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001180 get_path_fill(*clip->fPath),
1181 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001182 }
1183 }
1184
robertphillips@google.comfa662942012-05-17 12:20:22 +00001185 // Because we are using the scratch texture cache, "accum" may be
1186 // larger than expected and have some cruft in the areas we aren't using.
1187 // Clear it out.
1188
1189 // TODO: need a simpler way to clear the texture - can we combine
1190 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001191 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001192 GrAssert(NULL != drawState);
1193 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001194 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001195 // can't leave the accum bound as a rendertarget
1196 drawState->setRenderTarget(temp);
1197
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001198 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001199
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001200 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001201
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001202 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001203 return true;
1204}
1205
robertphillips@google.comf294b772012-04-27 14:29:26 +00001206////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001207void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001208 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001209}