blob: ceba42931970231ade7a727be23a6bf2dddb7082 [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
40 drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode,
41 GrSamplerState::kNearest_Filter,
42 mat);
43
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000044 drawState->createTextureEffect(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045}
46
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000047bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000048 GrGpu* gpu,
49 const SkPath& path,
50 GrPathFill fill,
51 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000052 // last (false) parameter disallows use of the SW path renderer
53 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
54}
55
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000056GrPathFill get_path_fill(const SkPath& path) {
57 switch (path.getFillType()) {
58 case SkPath::kWinding_FillType:
59 return kWinding_GrPathFill;
60 case SkPath::kEvenOdd_FillType:
61 return kEvenOdd_GrPathFill;
62 case SkPath::kInverseWinding_FillType:
63 return kInverseWinding_GrPathFill;
64 case SkPath::kInverseEvenOdd_FillType:
65 return kInverseEvenOdd_GrPathFill;
66 default:
67 GrCrash("Unsupported path fill in clip.");
68 return kWinding_GrPathFill; // suppress warning
69 }
70}
71
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000072}
73
robertphillips@google.comfa662942012-05-17 12:20:22 +000074/*
75 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
76 * will be used on any element. If so, it returns true to indicate that the
77 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
78 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000079bool GrClipMaskManager::useSWOnlyPath(const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000080
81 if (!clipIn.requiresAA()) {
82 // The stencil buffer can handle this case
83 return false;
84 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000085
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000086 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000087 // a clip gets complex enough it can just be done in SW regardless
88 // of whether it would invoke the GrSoftwarePathRenderer.
89 bool useSW = false;
90
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000091 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
92 const GrClip::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +000093
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000094 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
95 NULL != clip;
96 clip = iter.next()) {
97
98 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +000099 // Everything before a replace op can be ignored so start
100 // afresh w.r.t. determining if any element uses the SW path
101 useSW = false;
102 }
103
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000104 // rects can always be drawn directly w/o using the software path
105 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000106 if (NULL != clip->fPath &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000107 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000108 *clip->fPath,
109 get_path_fill(*clip->fPath),
110 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000111 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000112 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000113 }
114
115 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000116}
117
robertphillips@google.comf294b772012-04-27 14:29:26 +0000118////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000119// sort out what kind of clip mask needs to be created: alpha, stencil,
120// scissor, or entirely software
bsalomon@google.coma3201942012-06-21 19:58:20 +0000121bool GrClipMaskManager::setupClipping(const GrClip& clipIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000122 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000123
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000124 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000125 if (!drawState->isClipState() || clipIn.isEmpty()) {
126 fGpu->disableScissor();
127 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000128 return true;
129 }
130
131 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000132 // GrDrawTarget should have filtered this for us
133 GrAssert(NULL != rt);
134
bsalomon@google.coma3201942012-06-21 19:58:20 +0000135 GrIRect bounds;
136 GrIRect rtRect;
137 rtRect.setLTRB(0, 0, rt->width(), rt->height());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000138
139 clipIn.getConservativeBounds().roundOut(&bounds);
140 if (!bounds.intersect(rtRect)) {
141 bounds.setEmpty();
142 }
143 if (bounds.isEmpty()) {
144 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000145 }
146
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000147#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000148 // If MSAA is enabled we can do everything in the stencil buffer.
149 // Otherwise check if we should just create the entire clip mask
150 // in software (this will only happen if the clip mask is anti-aliased
151 // and too complex for the gpu to handle in its entirety)
bsalomon@google.coma3201942012-06-21 19:58:20 +0000152 if (0 == rt->numSamples() && this->useSWOnlyPath(clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000153 // The clip geometry is complex enough that it will be more
154 // efficient to create it entirely in software
155 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000156 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000157 if (this->createSoftwareClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000158 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000159 fGpu->disableScissor();
160 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000161 return true;
162 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000163
164 // if SW clip mask creation fails fall through to the other
165 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000166 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000167#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000168
robertphillips@google.comf294b772012-04-27 14:29:26 +0000169#if GR_AA_CLIP
170 // If MSAA is enabled use the (faster) stencil path for AA clipping
171 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000172 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000173 // Since we are going to create a destination texture of the correct
174 // size for the mask (rather than being bound by the size of the
175 // render target) we aren't going to use scissoring like the stencil
176 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000177 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000178 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000179 if (this->createAlphaClipMask(clipIn, &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.comf294b772012-04-27 14:29:26 +0000183 return true;
184 }
185
186 // if alpha clip mask creation fails fall through to the stencil
187 // buffer method
188 }
189#endif // GR_AA_CLIP
190
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000191 // Either a hard (stencil buffer) clip was explicitly requested or
192 // an antialiased clip couldn't be created. In either case, free up
193 // the texture in the antialiased mask cache.
194 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000195 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
196 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000197 // AA cache.
198 fAACache.reset();
199
bsalomon@google.coma3201942012-06-21 19:58:20 +0000200 // If the clip is a rectangle then just set the scissor. Otherwise, create
201 // a stencil mask.
202 if (clipIn.isRect()) {
203 fGpu->enableScissor(bounds);
204 this->setGpuStencil();
205 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000206 }
207
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000208 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000209 bool useStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
210 !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000211
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000212 if (useStencil) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000213 this->createStencilClipMask(clipIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000214 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000215 // This must occur after createStencilClipMask. That function may change
216 // the scissor. Also, it only guarantees that the stencil mask is correct
217 // within the bounds it was passed, so we must use both stencil and scissor
218 // test to the bounds for the final draw.
219 fGpu->enableScissor(bounds);
220 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000221 return true;
222}
223
224#define VISUALIZE_COMPLEX_CLIP 0
225
226#if VISUALIZE_COMPLEX_CLIP
227 #include "GrRandom.h"
228 GrRandom gRandom;
229 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
230#else
231 #define SET_RANDOM_COLOR
232#endif
233
234namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000235/**
236 * Does "container" contain "containee"? If either is empty then
237 * no containment is possible.
238 */
239bool contains(const SkRect& container, const SkIRect& containee) {
240 return !containee.isEmpty() && !container.isEmpty() &&
241 container.fLeft <= SkIntToScalar(containee.fLeft) &&
242 container.fTop <= SkIntToScalar(containee.fTop) &&
243 container.fRight >= SkIntToScalar(containee.fRight) &&
244 container.fBottom >= SkIntToScalar(containee.fBottom);
245}
246
247
robertphillips@google.comf294b772012-04-27 14:29:26 +0000248////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000249// determines how many elements at the head of the clip can be skipped and
250// whether the initial clear should be to the inside- or outside-the-clip value,
251// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000252const GrClip::Iter::Clip* process_initial_clip_elements(
253 GrClip::Iter* iter,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000254 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000255 bool* clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000256 SkRegion::Op* firstOp) {
257
258 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000259
260 // logically before the first element of the clip stack is
261 // processed the clip is entirely open. However, depending on the
262 // first set op we may prefer to clear to 0 for performance. We may
263 // also be able to skip the initial clip paths/rects. We loop until
264 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000265 bool done = false;
266 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000267
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000268 const GrClip::Iter::Clip* clip = NULL;
269
270 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
271 NULL != clip && !done;
272 clip = iter->next()) {
273 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000274 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000275 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000276 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000277 *clearToInside = false;
278 done = true;
279 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000280 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000281 // if this element contains the entire bounds then we
282 // can skip it.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000283 if (NULL != clip->fRect && contains(*clip->fRect, bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000284 break;
285 }
286 // if everything is initially clearToInside then intersect is
287 // same as clear to 0 and treat as a replace. Otherwise,
288 // set stays empty.
289 if (*clearToInside) {
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 }
294 break;
295 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000296 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000297 // if everything is initially outside then union is
298 // same as replace. Otherwise, every pixel is still
299 // clearToInside
300 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000301 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000302 done = true;
303 }
304 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000305 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000306 // xor is same as difference or replace both of which
307 // can be 1-pass instead of 2 for xor.
308 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000309 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000310 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000311 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000312 }
313 done = true;
314 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000315 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000316 // if all pixels are clearToInside then we have to process the
317 // difference, otherwise it has no effect and all pixels
318 // remain outside.
319 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000320 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000321 done = true;
322 }
323 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000324 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000325 // if all pixels are clearToInside then reverse difference
326 // produces empty set. Otherise it is same as replace
327 if (*clearToInside) {
328 *clearToInside = false;
329 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000330 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331 done = true;
332 }
333 break;
334 default:
335 GrCrash("Unknown set op.");
336 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000337
338 if (done) {
339 // we need to break out here (rather than letting the test in
340 // the loop do it) since backing up the iterator is very expensive
341 break;
342 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000343 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000344 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000346
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000347}
348
robertphillips@google.comf294b772012-04-27 14:29:26 +0000349
350namespace {
351
352////////////////////////////////////////////////////////////////////////////////
353// set up the OpenGL blend function to perform the specified
354// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000355void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000356
357 switch (op) {
358 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000359 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360 break;
361 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000362 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000363 break;
364 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000365 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000366 break;
367 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000368 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369 break;
370 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000371 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000372 break;
373 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000374 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000375 break;
376 default:
377 GrAssert(false);
378 break;
379 }
380}
381
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000383bool draw_path_in_software(GrContext* context,
384 GrGpu* gpu,
385 const SkPath& path,
386 GrPathFill fill,
387 bool doAA,
388 const GrIRect& resultBounds) {
389
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000390 SkAutoTUnref<GrTexture> texture(
391 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
392 resultBounds, fill,
393 doAA, NULL));
394 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000395 return false;
396 }
397
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000398 // The ClipMaskManager accumulates the clip mask in the UL corner
399 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000400
bsalomon@google.come3d32162012-07-20 13:37:06 +0000401 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000402
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000403 GrAssert(!GrIsFillInverted(fill));
404 return true;
405}
406
407
408////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000409bool draw_path(GrContext* context,
410 GrGpu* gpu,
411 const SkPath& path,
412 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000413 bool doAA,
414 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000415
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000416 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000417 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000418 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000419 }
420
bsalomon@google.come3d32162012-07-20 13:37:06 +0000421 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000422 return true;
423}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000424
425}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000426
427////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000428bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000429 const GrClip::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000430 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000431 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000432 GrAssert(NULL != drawState);
433
434 drawState->setRenderTarget(target->asRenderTarget());
435
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000436 if (NULL != clip->fRect) {
437 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000438 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000439 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000440 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000441 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000442 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000443 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000444 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000445 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000446 *clip->fPath,
447 get_path_fill(*clip->fPath),
448 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000449 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000450 }
451 return true;
452}
453
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000454void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000455 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000456 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000457 GrAssert(NULL != drawState);
458
459 // no AA here since it is encoded in the texture
460 drawState->setRenderTarget(target->asRenderTarget());
461
462 GrMatrix sampleM;
463 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000464
465 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
466 GrSamplerState::kNearest_Filter,
467 sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000468 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000469
robertphillips@google.comf105b102012-05-14 12:18:26 +0000470 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
471 SkIntToScalar(target->height()));
472
bsalomon@google.come3d32162012-07-20 13:37:06 +0000473 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000474
tomhudson@google.com676e6602012-07-10 17:21:48 +0000475 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000476}
477
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000478// get a texture to act as a temporary buffer for AA clip boolean operations
479// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000480void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000481 GrAutoScratchTexture* temp) {
482 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000483 // we've already allocated the temp texture
484 return;
485 }
486
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000487 GrTextureDesc desc;
488 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
489 desc.fWidth = bounds.width();
490 desc.fHeight = bounds.height();
491 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000492
robertphillips@google.com2c756812012-05-22 20:28:23 +0000493 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000494}
495
robertphillips@google.comf105b102012-05-14 12:18:26 +0000496
497void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000498 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000499 // Since we are setting up the cache we know the last lookup was a miss
500 // Free up the currently cached mask so it can be reused
501 fAACache.reset();
502
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000503 GrTextureDesc desc;
504 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
505 desc.fWidth = bounds.width();
506 desc.fHeight = bounds.height();
507 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000508
509 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000510}
511
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000512////////////////////////////////////////////////////////////////////////////////
513// Shared preamble between gpu and SW-only AA clip mask creation paths.
514// Handles caching, determination of clip mask bound & allocation (if needed)
515// of the result texture
516// Returns true if there is no more work to be done (i.e., we got a cache hit)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000517bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000518 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000519 GrIRect* resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000520 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000521 GrAssert(origDrawState->isClipState());
522
523 GrRenderTarget* rt = origDrawState->getRenderTarget();
524 GrAssert(NULL != rt);
525
526 GrRect rtRect;
527 rtRect.setLTRB(0, 0,
528 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
529
530 // unlike the stencil path the alpha path is not bound to the size of the
531 // render target - determine the minimum size required for the mask
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000532 GrRect bounds = clipIn.getConservativeBounds();
533 if (!bounds.intersect(rtRect)) {
534 // the mask will be empty in this case
535 GrAssert(false);
536 bounds.setEmpty();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000537 }
538
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000539 GrIRect intBounds;
540 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000541
542 // need to outset a pixel since the standard bounding box computation
543 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000544 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000545
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000546 // TODO: make sure we don't outset if bounds are still 0,0 @ min
547
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000548 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000549 intBounds.width(),
550 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000551 *result = fAACache.getLastMask();
552 fAACache.getLastBound(resultBounds);
553 return true;
554 }
555
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000556 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000557
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000558 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000559 return false;
560}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000562////////////////////////////////////////////////////////////////////////////////
563// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000564bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000565 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000566 GrIRect *resultBounds) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000567 GrAssert(NULL != resultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000568 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
569
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000570 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000571 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000572 return true;
573 }
574
robertphillips@google.comf105b102012-05-14 12:18:26 +0000575 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000576 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000577 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000578 return false;
579 }
580
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000581 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
582 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000583
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000584 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000585
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000586 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000587 // if we were able to trim down the size of the mask we need to
588 // offset the paths & rects that will be used to compute it
589 GrMatrix m;
590
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000591 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
592 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000593
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000594 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000595 }
596
597 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000598 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
599
600 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
601 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
602 *resultBounds,
603 &clearToInside,
604 &firstOp);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000606 fGpu->clear(NULL,
607 clearToInside ? 0xffffffff : 0x00000000,
608 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000609
robertphillips@google.comf105b102012-05-14 12:18:26 +0000610 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000611 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000612 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000613 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000614
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000615 SkRegion::Op op = clip->fOp;
616 if (first) {
617 first = false;
618 op = firstOp;
619 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620
621 if (SkRegion::kReplace_Op == op) {
622 // TODO: replace is actually a lot faster then intersection
623 // for this path - refactor the stencil path so it can handle
624 // replace ops and alter GrClip to allow them through
625
626 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000627 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000629 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000630 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000631
632 } else if (SkRegion::kReverseDifference_Op == op ||
633 SkRegion::kIntersect_Op == op) {
634 // there is no point in intersecting a screen filling rectangle.
635 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000636 NULL != clip->fRect &&
637 contains(*clip->fRect, *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000638 continue;
639 }
640
robertphillips@google.comf105b102012-05-14 12:18:26 +0000641 getTemp(*resultBounds, &temp);
642 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000643 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000644 return false;
645 }
646
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000648 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000649
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000650 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000651 this->drawClipShape(temp.texture(), clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000652
653 // TODO: rather than adding these two translations here
654 // compute the bounding box needed to render the texture
655 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000656 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000657 GrMatrix m;
658
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000659 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
660 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000661
662 drawState->preConcatViewMatrix(m);
663 }
664
665 // Now draw into the accumulator using the real operation
666 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000667 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000668 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000669
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000670 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000671 GrMatrix m;
672
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000673 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
674 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000675
676 drawState->preConcatViewMatrix(m);
677 }
678
679 } else {
680 // all the remaining ops can just be directly draw into
681 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000682 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000683 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684 }
685 }
686
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000687 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000688 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689 return true;
690}
691
692////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000694bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000695 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000697 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000699 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000700 GrAssert(drawState->isClipState());
701
702 GrRenderTarget* rt = drawState->getRenderTarget();
703 GrAssert(NULL != rt);
704
705 // TODO: dynamically attach a SB when needed.
706 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
707 if (NULL == stencilBuffer) {
708 return false;
709 }
710
711 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
712
713 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
714
715 // we set the current clip to the bounds so that our recursive
716 // draws are scissored to them. We use the copy of the complex clip
717 // we just stashed on the SB to render from. We set it back after
718 // we finish drawing it into the stencil.
719 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000720 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000722 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
723 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000725 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727#if !VISUALIZE_COMPLEX_CLIP
728 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
729#endif
730
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000731 int clipBit = stencilBuffer->bits();
732 SkASSERT((clipBit <= 16) &&
733 "Ganesh only handles 16b or smaller stencil buffers");
734 clipBit = (1 << (clipBit-1));
735
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000736 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737
738 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000739 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
740
741 GrClip::Iter iter(clipCopy, GrClip::Iter::kBottom_IterStart);
742 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000743 rtRect,
744 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000745 &firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746
bsalomon@google.coma3201942012-06-21 19:58:20 +0000747 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000748 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749
750 // walk through each clip element and perform its set op
751 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000752 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753 GrPathFill fill;
754 bool fillInverted;
755 // enabled at bottom of loop
756 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000757 // if the target is MSAA then we want MSAA enabled when the clip is soft
758 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000759 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000760 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
761 } else {
762 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
763 }
764 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765
766 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000767 // directly to the stencil buffer
768 // with a non-inverted fill rule
769 // without extra passes to
770 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000771
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000772 SkRegion::Op op = clip->fOp;
773 if (first) {
774 first = false;
775 op = firstOp;
776 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000777
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000779 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000780 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000782 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000783 fillInverted = false;
784 // there is no point in intersecting a screen filling
785 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000786 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000787 contains(*clip->fRect, rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788 continue;
789 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000790 } else if (NULL != clip->fPath) {
791 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000792 fillInverted = GrIsFillInverted(fill);
793 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000794 clipPath = clip->fPath;
robertphillips@google.com2c756812012-05-22 20:28:23 +0000795 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000796 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000797 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798 if (NULL == pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000799 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 return false;
801 }
802 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000803 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000804 }
805
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000806 int passes;
807 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
808
809 bool canDrawDirectToClip; // Given the renderer, the element,
810 // fill rule, and set operation can
811 // we render the element directly to
812 // stencil bit used for clipping.
813 canDrawDirectToClip =
814 GrStencilSettings::GetClipPasses(op,
815 canRenderDirectToStencil,
816 clipBit,
817 fillInverted,
818 &passes, stencilSettings);
819
820 // draw the element to the client stencil bits if necessary
821 if (!canDrawDirectToClip) {
822 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
823 kIncClamp_StencilOp,
824 kIncClamp_StencilOp,
825 kAlways_StencilFunc,
826 0xffff,
827 0x0000,
828 0xffff);
829 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000830 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000832 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000833 } else {
834 if (canRenderDirectToStencil) {
835 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000836 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000837 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000838 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000839 }
840 }
841 }
842
843 // now we modify the clip bit by rendering either the clip
844 // element directly or a bounding rect of the entire clip.
845 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
846 for (int p = 0; p < passes; ++p) {
847 *drawState->stencil() = stencilSettings[p];
848 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000849 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000850 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000851 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000852 } else {
853 SET_RANDOM_COLOR
bsalomon@google.come3d32162012-07-20 13:37:06 +0000854 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000855 }
856 } else {
857 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000858 GrRect rect = GrRect::MakeLTRB(
859 SkIntToScalar(bounds.fLeft),
860 SkIntToScalar(bounds.fTop),
861 SkIntToScalar(bounds.fRight),
862 SkIntToScalar(bounds.fBottom));
bsalomon@google.come3d32162012-07-20 13:37:06 +0000863 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000864 }
865 }
866 }
867 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000868 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000869 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000870 // set this last because recursive draws may overwrite it back to kNone.
871 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
872 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000873 return true;
874}
875
bsalomon@google.com411dad02012-06-05 20:24:20 +0000876// mapping of clip-respecting stencil funcs to normal stencil funcs
877// mapping depends on whether stencil-clipping is in effect.
878static const GrStencilFunc
879 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
880 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
881 // In the Clip Funcs
882 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
883 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
884 kLess_StencilFunc, // kLessIfInClip_StencilFunc
885 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
886 // Special in the clip func that forces user's ref to be 0.
887 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
888 // make ref 0 and do normal nequal.
889 },
890 {// Stencil-Clipping is ENABLED
891 // In the Clip Funcs
892 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
893 // eq stencil clip bit, mask
894 // out user bits.
895
896 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
897 // add stencil bit to mask and ref
898
899 kLess_StencilFunc, // kLessIfInClip_StencilFunc
900 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
901 // for both of these we can add
902 // the clip bit to the mask and
903 // ref and compare as normal
904 // Special in the clip func that forces user's ref to be 0.
905 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
906 // make ref have only the clip bit set
907 // and make comparison be less
908 // 10..0 < 1..user_bits..
909 }
910};
911
bsalomon@google.coma3201942012-06-21 19:58:20 +0000912namespace {
913// Sets the settings to clip against the stencil buffer clip while ignoring the
914// client bits.
915const GrStencilSettings& basic_apply_stencil_clip_settings() {
916 // stencil settings to use when clip is in stencil
917 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
918 kKeep_StencilOp,
919 kKeep_StencilOp,
920 kAlwaysIfInClip_StencilFunc,
921 0x0000,
922 0x0000,
923 0x0000);
924 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
925}
926}
927
928void GrClipMaskManager::setGpuStencil() {
929 // We make two copies of the StencilSettings here (except in the early
930 // exit scenario. One copy from draw state to the stack var. Then another
931 // from the stack var to the gpu. We could make this class hold a ptr to
932 // GrGpu's fStencilSettings and eliminate the stack copy here.
933
934 const GrDrawState& drawState = fGpu->getDrawState();
935
936 // use stencil for clipping if clipping is enabled and the clip
937 // has been written into the stencil.
938 GrClipMaskManager::StencilClipMode clipMode;
939 if (this->isClipInStencil() && drawState.isClipState()) {
940 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
941 // We can't be modifying the clip and respecting it at the same time.
942 GrAssert(!drawState.isStateFlagEnabled(
943 GrGpu::kModifyStencilClip_StateBit));
944 } else if (drawState.isStateFlagEnabled(
945 GrGpu::kModifyStencilClip_StateBit)) {
946 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
947 } else {
948 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
949 }
950
951 GrStencilSettings settings;
952 // The GrGpu client may not be using the stencil buffer but we may need to
953 // enable it in order to respect a stencil clip.
954 if (drawState.getStencil().isDisabled()) {
955 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
956 settings = basic_apply_stencil_clip_settings();
957 } else {
958 fGpu->disableStencil();
959 return;
960 }
961 } else {
962 settings = drawState.getStencil();
963 }
964
965 // TODO: dynamically attach a stencil buffer
966 int stencilBits = 0;
967 GrStencilBuffer* stencilBuffer =
968 drawState.getRenderTarget()->getStencilBuffer();
969 if (NULL != stencilBuffer) {
970 stencilBits = stencilBuffer->bits();
971 }
972
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000973 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
974 !settings.usesWrapOp());
975 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000976 this->adjustStencilParams(&settings, clipMode, stencilBits);
977 fGpu->setStencilSettings(settings);
978}
979
980void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
981 StencilClipMode mode,
982 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000983 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000984
985 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000986 // We assume that this clip manager itself is drawing to the GrGpu and
987 // has already setup the correct values.
988 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000989 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000990
bsalomon@google.com411dad02012-06-05 20:24:20 +0000991 unsigned int clipBit = (1 << (stencilBitCnt - 1));
992 unsigned int userBits = clipBit - 1;
993
bsalomon@google.coma3201942012-06-21 19:58:20 +0000994 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
995 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000996
bsalomon@google.coma3201942012-06-21 19:58:20 +0000997 bool finished = false;
998 while (!finished) {
999 GrStencilFunc func = settings->func(face);
1000 uint16_t writeMask = settings->writeMask(face);
1001 uint16_t funcMask = settings->funcMask(face);
1002 uint16_t funcRef = settings->funcRef(face);
1003
1004 GrAssert((unsigned) func < kStencilFuncCount);
1005
1006 writeMask &= userBits;
1007
1008 if (func >= kBasicStencilFuncCount) {
1009 int respectClip = kRespectClip_StencilClipMode == mode;
1010 if (respectClip) {
1011 // The GrGpu class should have checked this
1012 GrAssert(this->isClipInStencil());
1013 switch (func) {
1014 case kAlwaysIfInClip_StencilFunc:
1015 funcMask = clipBit;
1016 funcRef = clipBit;
1017 break;
1018 case kEqualIfInClip_StencilFunc:
1019 case kLessIfInClip_StencilFunc:
1020 case kLEqualIfInClip_StencilFunc:
1021 funcMask = (funcMask & userBits) | clipBit;
1022 funcRef = (funcRef & userBits) | clipBit;
1023 break;
1024 case kNonZeroIfInClip_StencilFunc:
1025 funcMask = (funcMask & userBits) | clipBit;
1026 funcRef = clipBit;
1027 break;
1028 default:
1029 GrCrash("Unknown stencil func");
1030 }
1031 } else {
1032 funcMask &= userBits;
1033 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001034 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001035 const GrStencilFunc* table =
1036 gSpecialToBasicStencilFunc[respectClip];
1037 func = table[func - kBasicStencilFuncCount];
1038 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001039 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001040 funcMask &= userBits;
1041 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001042 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001043
1044 settings->setFunc(face, func);
1045 settings->setWriteMask(face, writeMask);
1046 settings->setFuncMask(face, funcMask);
1047 settings->setFuncRef(face, funcRef);
1048
1049 if (GrStencilSettings::kFront_Face == face) {
1050 face = GrStencilSettings::kBack_Face;
1051 finished = !twoSided;
1052 } else {
1053 finished = true;
1054 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001055 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001056 if (!twoSided) {
1057 settings->copyFrontSettingsToBack();
1058 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001059}
1060
1061////////////////////////////////////////////////////////////////////////////////
1062
robertphillips@google.comfa662942012-05-17 12:20:22 +00001063namespace {
1064
1065GrPathFill invert_fill(GrPathFill fill) {
1066 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001067 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1068 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1069 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1070 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1071 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001072 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001073 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1074 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1075 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1076 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1077 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1078 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001079 return gInvertedFillTable[fill];
1080}
1081
1082}
1083
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001084bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001085 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001086 GrIRect* resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001087 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001088
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001089 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001090 return true;
1091 }
1092
robertphillips@google.comf105b102012-05-14 12:18:26 +00001093 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001094 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001095 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001096 return false;
1097 }
1098
robertphillips@google.com2c756812012-05-22 20:28:23 +00001099 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001100
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001101 helper.init(*resultBounds, NULL);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001102
robertphillips@google.comfa662942012-05-17 12:20:22 +00001103 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001104 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1105
1106 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
1107 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001108 *resultBounds,
1109 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001110 &firstOp);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001111
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001112 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001113
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001114 bool first = true;
1115 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001116
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001117 SkRegion::Op op = clip->fOp;
1118 if (first) {
1119 first = false;
1120 op = firstOp;
1121 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001122
1123 if (SkRegion::kIntersect_Op == op ||
1124 SkRegion::kReverseDifference_Op == op) {
1125 // Intersect and reverse difference require modifying pixels
1126 // outside of the geometry that is being "drawn". In both cases
1127 // we erase all the pixels outside of the geometry but
1128 // leave the pixels inside the geometry alone. For reverse
1129 // difference we invert all the pixels before clearing the ones
1130 // outside the geometry.
1131 if (SkRegion::kReverseDifference_Op == op) {
1132 SkRect temp = SkRect::MakeLTRB(
1133 SkIntToScalar(resultBounds->left()),
1134 SkIntToScalar(resultBounds->top()),
1135 SkIntToScalar(resultBounds->right()),
1136 SkIntToScalar(resultBounds->bottom()));
1137
1138 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001139 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001140 }
1141
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001142 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001143
1144 // convert the rect to a path so we can invert the fill
1145 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001146 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001147
1148 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001149 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001150 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001151 } else if (NULL != clip->fPath) {
1152 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001153 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001154 invert_fill(get_path_fill(*clip->fPath)),
1155 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001156 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001157 }
1158
1159 continue;
1160 }
1161
1162 // The other ops (union, xor, diff) only affect pixels inside
1163 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001164 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001165
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001166 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001167 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001168 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001169
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001170 } else if (NULL != clip->fPath) {
1171 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001172 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001173 get_path_fill(*clip->fPath),
1174 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001175 }
1176 }
1177
robertphillips@google.comfa662942012-05-17 12:20:22 +00001178 // Because we are using the scratch texture cache, "accum" may be
1179 // larger than expected and have some cruft in the areas we aren't using.
1180 // Clear it out.
1181
1182 // TODO: need a simpler way to clear the texture - can we combine
1183 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001184 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001185 GrAssert(NULL != drawState);
1186 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001187 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001188 // can't leave the accum bound as a rendertarget
1189 drawState->setRenderTarget(temp);
1190
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001191 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001192
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001193 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001194
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001195 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001196 return true;
1197}
1198
robertphillips@google.comf294b772012-04-27 14:29:26 +00001199////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001200void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001201 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001202}