blob: 4deebf421857ec6fa50e2760a8908342cd60d50d [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.comb99225c2012-07-24 18:20:10 +000072/**
73 * Does any individual clip in 'clipIn' use anti-aliasing?
74 */
75bool requires_AA(const GrClip& clipIn) {
76
77 GrClip::Iter iter;
78 iter.reset(clipIn, GrClip::Iter::kBottom_IterStart);
79
80 const GrClip::Iter::Clip* clip = NULL;
81 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
82 NULL != clip;
83 clip = iter.next()) {
84
85 if (clip->fDoAA) {
86 return true;
87 }
88 }
89
90 return false;
91}
92
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000093}
94
robertphillips@google.comfa662942012-05-17 12:20:22 +000095/*
96 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
97 * will be used on any element. If so, it returns true to indicate that the
98 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
99 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000100bool GrClipMaskManager::useSWOnlyPath(const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000101
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000102 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000103 // a clip gets complex enough it can just be done in SW regardless
104 // of whether it would invoke the GrSoftwarePathRenderer.
105 bool useSW = false;
106
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000107 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
108 const GrClip::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000109
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000110 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
111 NULL != clip;
112 clip = iter.next()) {
113
114 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000115 // Everything before a replace op can be ignored so start
116 // afresh w.r.t. determining if any element uses the SW path
117 useSW = false;
118 }
119
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000120 // rects can always be drawn directly w/o using the software path
121 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000122 if (NULL != clip->fPath &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000123 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000124 *clip->fPath,
125 get_path_fill(*clip->fPath),
126 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000127 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000128 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000129 }
130
131 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000132}
133
robertphillips@google.comf294b772012-04-27 14:29:26 +0000134////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000135// sort out what kind of clip mask needs to be created: alpha, stencil,
136// scissor, or entirely software
bsalomon@google.coma3201942012-06-21 19:58:20 +0000137bool GrClipMaskManager::setupClipping(const GrClip& clipIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000138 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000139
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000140 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000141 if (!drawState->isClipState() || clipIn.isEmpty()) {
142 fGpu->disableScissor();
143 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000144 return true;
145 }
146
147 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000148 // GrDrawTarget should have filtered this for us
149 GrAssert(NULL != rt);
150
bsalomon@google.coma3201942012-06-21 19:58:20 +0000151 GrIRect bounds;
152 GrIRect rtRect;
153 rtRect.setLTRB(0, 0, rt->width(), rt->height());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000154
155 clipIn.getConservativeBounds().roundOut(&bounds);
156 if (!bounds.intersect(rtRect)) {
157 bounds.setEmpty();
158 }
159 if (bounds.isEmpty()) {
160 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000161 }
162
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000163 bool requiresAA = requires_AA(clipIn);
164 GrAssert(requiresAA == clipIn.requiresAA());
165
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000166#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000167 // If MSAA is enabled we can do everything in the stencil buffer.
168 // Otherwise check if we should just create the entire clip mask
169 // in software (this will only happen if the clip mask is anti-aliased
170 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000171 if (0 == rt->numSamples() && requiresAA && this->useSWOnlyPath(clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000172 // The clip geometry is complex enough that it will be more
173 // efficient to create it entirely in software
174 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000175 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000176 if (this->createSoftwareClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000177 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000178 fGpu->disableScissor();
179 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000180 return true;
181 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000182
183 // if SW clip mask creation fails fall through to the other
184 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000185 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000186#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000187
robertphillips@google.comf294b772012-04-27 14:29:26 +0000188#if GR_AA_CLIP
189 // If MSAA is enabled use the (faster) stencil path for AA clipping
190 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000191 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000192 // Since we are going to create a destination texture of the correct
193 // size for the mask (rather than being bound by the size of the
194 // render target) we aren't going to use scissoring like the stencil
195 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000196 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000197 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000198 if (this->createAlphaClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000199 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000200 fGpu->disableScissor();
201 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000202 return true;
203 }
204
205 // if alpha clip mask creation fails fall through to the stencil
206 // buffer method
207 }
208#endif // GR_AA_CLIP
209
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000210 // Either a hard (stencil buffer) clip was explicitly requested or
211 // an antialiased clip couldn't be created. In either case, free up
212 // the texture in the antialiased mask cache.
213 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000214 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
215 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000216 // AA cache.
217 fAACache.reset();
218
bsalomon@google.coma3201942012-06-21 19:58:20 +0000219 // If the clip is a rectangle then just set the scissor. Otherwise, create
220 // a stencil mask.
221 if (clipIn.isRect()) {
222 fGpu->enableScissor(bounds);
223 this->setGpuStencil();
224 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000225 }
226
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000227 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000228 bool useStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
229 !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000230
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000231 if (useStencil) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000232 this->createStencilClipMask(clipIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000233 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000234 // This must occur after createStencilClipMask. That function may change
235 // the scissor. Also, it only guarantees that the stencil mask is correct
236 // within the bounds it was passed, so we must use both stencil and scissor
237 // test to the bounds for the final draw.
238 fGpu->enableScissor(bounds);
239 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000240 return true;
241}
242
243#define VISUALIZE_COMPLEX_CLIP 0
244
245#if VISUALIZE_COMPLEX_CLIP
246 #include "GrRandom.h"
247 GrRandom gRandom;
248 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
249#else
250 #define SET_RANDOM_COLOR
251#endif
252
253namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000254/**
255 * Does "container" contain "containee"? If either is empty then
256 * no containment is possible.
257 */
258bool contains(const SkRect& container, const SkIRect& containee) {
259 return !containee.isEmpty() && !container.isEmpty() &&
260 container.fLeft <= SkIntToScalar(containee.fLeft) &&
261 container.fTop <= SkIntToScalar(containee.fTop) &&
262 container.fRight >= SkIntToScalar(containee.fRight) &&
263 container.fBottom >= SkIntToScalar(containee.fBottom);
264}
265
266
robertphillips@google.comf294b772012-04-27 14:29:26 +0000267////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000268// determines how many elements at the head of the clip can be skipped and
269// whether the initial clear should be to the inside- or outside-the-clip value,
270// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000271const GrClip::Iter::Clip* process_initial_clip_elements(
272 GrClip::Iter* iter,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000273 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000274 bool* clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000275 SkRegion::Op* firstOp) {
276
277 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000278
279 // logically before the first element of the clip stack is
280 // processed the clip is entirely open. However, depending on the
281 // first set op we may prefer to clear to 0 for performance. We may
282 // also be able to skip the initial clip paths/rects. We loop until
283 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000284 bool done = false;
285 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000286
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000287 const GrClip::Iter::Clip* clip = NULL;
288
289 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
290 NULL != clip && !done;
291 clip = iter->next()) {
292 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000293 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000294 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000295 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000296 *clearToInside = false;
297 done = true;
298 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000299 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 // if this element contains the entire bounds then we
301 // can skip it.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000302 if (NULL != clip->fRect && contains(*clip->fRect, bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000303 break;
304 }
305 // if everything is initially clearToInside then intersect is
306 // same as clear to 0 and treat as a replace. Otherwise,
307 // set stays empty.
308 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000309 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000310 *clearToInside = false;
311 done = true;
312 }
313 break;
314 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000315 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000316 // if everything is initially outside then union is
317 // same as replace. Otherwise, every pixel is still
318 // clearToInside
319 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000320 *firstOp = SkRegion::kReplace_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::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000325 // xor is same as difference or replace both of which
326 // can be 1-pass instead of 2 for xor.
327 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000328 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000329 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000330 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331 }
332 done = true;
333 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000334 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 // if all pixels are clearToInside then we have to process the
336 // difference, otherwise it has no effect and all pixels
337 // remain outside.
338 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000339 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000340 done = true;
341 }
342 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000343 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000344 // if all pixels are clearToInside then reverse difference
345 // produces empty set. Otherise it is same as replace
346 if (*clearToInside) {
347 *clearToInside = false;
348 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000349 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000350 done = true;
351 }
352 break;
353 default:
354 GrCrash("Unknown set op.");
355 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000356
357 if (done) {
358 // we need to break out here (rather than letting the test in
359 // the loop do it) since backing up the iterator is very expensive
360 break;
361 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000362 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000363 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000364}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000366}
367
robertphillips@google.comf294b772012-04-27 14:29:26 +0000368
369namespace {
370
371////////////////////////////////////////////////////////////////////////////////
372// set up the OpenGL blend function to perform the specified
373// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000374void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000375
376 switch (op) {
377 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000378 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000379 break;
380 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000381 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382 break;
383 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000384 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385 break;
386 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000387 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388 break;
389 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000390 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391 break;
392 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000393 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000394 break;
395 default:
396 GrAssert(false);
397 break;
398 }
399}
400
robertphillips@google.comf294b772012-04-27 14:29:26 +0000401////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000402bool draw_path_in_software(GrContext* context,
403 GrGpu* gpu,
404 const SkPath& path,
405 GrPathFill fill,
406 bool doAA,
407 const GrIRect& resultBounds) {
408
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000409 SkAutoTUnref<GrTexture> texture(
410 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
411 resultBounds, fill,
412 doAA, NULL));
413 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000414 return false;
415 }
416
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000417 // The ClipMaskManager accumulates the clip mask in the UL corner
418 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000419
bsalomon@google.come3d32162012-07-20 13:37:06 +0000420 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000421
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000422 GrAssert(!GrIsFillInverted(fill));
423 return true;
424}
425
426
427////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000428bool draw_path(GrContext* context,
429 GrGpu* gpu,
430 const SkPath& path,
431 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000432 bool doAA,
433 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000434
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000435 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000436 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000437 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438 }
439
bsalomon@google.come3d32162012-07-20 13:37:06 +0000440 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000441 return true;
442}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000443
444}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000445
446////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000447bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000448 const GrClip::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000449 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000450 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000451 GrAssert(NULL != drawState);
452
453 drawState->setRenderTarget(target->asRenderTarget());
454
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000455 if (NULL != clip->fRect) {
456 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000457 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000458 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000459 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000460 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000461 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000462 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000463 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000464 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000465 *clip->fPath,
466 get_path_fill(*clip->fPath),
467 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000468 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000469 }
470 return true;
471}
472
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000473void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000474 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000475 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000476 GrAssert(NULL != drawState);
477
478 // no AA here since it is encoded in the texture
479 drawState->setRenderTarget(target->asRenderTarget());
480
481 GrMatrix sampleM;
482 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000483
484 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
485 GrSamplerState::kNearest_Filter,
486 sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000487 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000488
robertphillips@google.comf105b102012-05-14 12:18:26 +0000489 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
490 SkIntToScalar(target->height()));
491
bsalomon@google.come3d32162012-07-20 13:37:06 +0000492 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000493
tomhudson@google.com676e6602012-07-10 17:21:48 +0000494 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495}
496
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000497// get a texture to act as a temporary buffer for AA clip boolean operations
498// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000499void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000500 GrAutoScratchTexture* temp) {
501 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000502 // we've already allocated the temp texture
503 return;
504 }
505
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000506 GrTextureDesc desc;
507 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
508 desc.fWidth = bounds.width();
509 desc.fHeight = bounds.height();
510 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000511
robertphillips@google.com2c756812012-05-22 20:28:23 +0000512 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000513}
514
robertphillips@google.comf105b102012-05-14 12:18:26 +0000515
516void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000517 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000518 // Since we are setting up the cache we know the last lookup was a miss
519 // Free up the currently cached mask so it can be reused
520 fAACache.reset();
521
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000522 GrTextureDesc desc;
523 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
524 desc.fWidth = bounds.width();
525 desc.fHeight = bounds.height();
526 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000527
528 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000529}
530
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000531////////////////////////////////////////////////////////////////////////////////
532// Shared preamble between gpu and SW-only AA clip mask creation paths.
533// Handles caching, determination of clip mask bound & allocation (if needed)
534// of the result texture
535// Returns true if there is no more work to be done (i.e., we got a cache hit)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000536bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000537 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000538 GrIRect* resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000539 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000540 GrAssert(origDrawState->isClipState());
541
542 GrRenderTarget* rt = origDrawState->getRenderTarget();
543 GrAssert(NULL != rt);
544
545 GrRect rtRect;
546 rtRect.setLTRB(0, 0,
547 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
548
549 // unlike the stencil path the alpha path is not bound to the size of the
550 // render target - determine the minimum size required for the mask
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000551 GrRect bounds = clipIn.getConservativeBounds();
552 if (!bounds.intersect(rtRect)) {
553 // the mask will be empty in this case
554 GrAssert(false);
555 bounds.setEmpty();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000556 }
557
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000558 GrIRect intBounds;
559 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000560
561 // need to outset a pixel since the standard bounding box computation
562 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000563 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000564
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000565 // TODO: make sure we don't outset if bounds are still 0,0 @ min
566
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000567 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000568 intBounds.width(),
569 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000570 *result = fAACache.getLastMask();
571 fAACache.getLastBound(resultBounds);
572 return true;
573 }
574
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000575 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000576
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000577 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000578 return false;
579}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000580
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000581////////////////////////////////////////////////////////////////////////////////
582// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000583bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000584 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000585 GrIRect *resultBounds) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000586 GrAssert(NULL != resultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000587 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
588
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000589 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000590 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000591 return true;
592 }
593
robertphillips@google.comf105b102012-05-14 12:18:26 +0000594 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000595 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000596 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597 return false;
598 }
599
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000600 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
601 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000602
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000603 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000604
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000605 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000606 // if we were able to trim down the size of the mask we need to
607 // offset the paths & rects that will be used to compute it
608 GrMatrix m;
609
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000610 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
611 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000612
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000613 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000614 }
615
616 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000617 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
618
619 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
620 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
621 *resultBounds,
622 &clearToInside,
623 &firstOp);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000624
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000625 fGpu->clear(NULL,
626 clearToInside ? 0xffffffff : 0x00000000,
627 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628
robertphillips@google.comf105b102012-05-14 12:18:26 +0000629 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000630 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000631 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000632 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000633
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000634 SkRegion::Op op = clip->fOp;
635 if (first) {
636 first = false;
637 op = firstOp;
638 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000639
640 if (SkRegion::kReplace_Op == op) {
641 // TODO: replace is actually a lot faster then intersection
642 // for this path - refactor the stencil path so it can handle
643 // replace ops and alter GrClip to allow them through
644
645 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000646 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000648 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000649 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000650
651 } else if (SkRegion::kReverseDifference_Op == op ||
652 SkRegion::kIntersect_Op == op) {
653 // there is no point in intersecting a screen filling rectangle.
654 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000655 NULL != clip->fRect &&
656 contains(*clip->fRect, *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000657 continue;
658 }
659
robertphillips@google.comf105b102012-05-14 12:18:26 +0000660 getTemp(*resultBounds, &temp);
661 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000662 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000663 return false;
664 }
665
robertphillips@google.comf294b772012-04-27 14:29:26 +0000666 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000667 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000668
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000669 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000670 this->drawClipShape(temp.texture(), clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000671
672 // TODO: rather than adding these two translations here
673 // compute the bounding box needed to render the texture
674 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000675 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000676 GrMatrix m;
677
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000678 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
679 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000680
681 drawState->preConcatViewMatrix(m);
682 }
683
684 // Now draw into the accumulator using the real operation
685 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000686 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000687 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000688
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000689 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000690 GrMatrix m;
691
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000692 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
693 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000694
695 drawState->preConcatViewMatrix(m);
696 }
697
698 } else {
699 // all the remaining ops can just be directly draw into
700 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000701 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000702 this->drawClipShape(accum, clip, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000703 }
704 }
705
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000706 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000707 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000708 return true;
709}
710
711////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000713bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000714 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000716 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000718 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719 GrAssert(drawState->isClipState());
720
721 GrRenderTarget* rt = drawState->getRenderTarget();
722 GrAssert(NULL != rt);
723
724 // TODO: dynamically attach a SB when needed.
725 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
726 if (NULL == stencilBuffer) {
727 return false;
728 }
729
730 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
731
732 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
733
734 // we set the current clip to the bounds so that our recursive
735 // draws are scissored to them. We use the copy of the complex clip
736 // we just stashed on the SB to render from. We set it back after
737 // we finish drawing it into the stencil.
738 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000739 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000741 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
742 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000744 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746#if !VISUALIZE_COMPLEX_CLIP
747 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
748#endif
749
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750 int clipBit = stencilBuffer->bits();
751 SkASSERT((clipBit <= 16) &&
752 "Ganesh only handles 16b or smaller stencil buffers");
753 clipBit = (1 << (clipBit-1));
754
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000755 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756
757 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000758 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
759
760 GrClip::Iter iter(clipCopy, GrClip::Iter::kBottom_IterStart);
761 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000762 rtRect,
763 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000764 &firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765
bsalomon@google.coma3201942012-06-21 19:58:20 +0000766 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000767 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000768
769 // walk through each clip element and perform its set op
770 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000771 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772 GrPathFill fill;
773 bool fillInverted;
774 // enabled at bottom of loop
775 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000776 // if the target is MSAA then we want MSAA enabled when the clip is soft
777 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000778 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000779 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
780 } else {
781 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
782 }
783 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000784
785 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000786 // directly to the stencil buffer
787 // with a non-inverted fill rule
788 // without extra passes to
789 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000791 SkRegion::Op op = clip->fOp;
792 if (first) {
793 first = false;
794 op = firstOp;
795 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000796
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000798 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000799 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000801 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000802 fillInverted = false;
803 // there is no point in intersecting a screen filling
804 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000805 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000806 contains(*clip->fRect, rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 continue;
808 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000809 } else if (NULL != clip->fPath) {
810 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 fillInverted = GrIsFillInverted(fill);
812 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000813 clipPath = clip->fPath;
robertphillips@google.com2c756812012-05-22 20:28:23 +0000814 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000815 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000816 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000817 if (NULL == pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000818 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 return false;
820 }
821 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000822 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000823 }
824
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 int passes;
826 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
827
828 bool canDrawDirectToClip; // Given the renderer, the element,
829 // fill rule, and set operation can
830 // we render the element directly to
831 // stencil bit used for clipping.
832 canDrawDirectToClip =
833 GrStencilSettings::GetClipPasses(op,
834 canRenderDirectToStencil,
835 clipBit,
836 fillInverted,
837 &passes, stencilSettings);
838
839 // draw the element to the client stencil bits if necessary
840 if (!canDrawDirectToClip) {
841 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
842 kIncClamp_StencilOp,
843 kIncClamp_StencilOp,
844 kAlways_StencilFunc,
845 0xffff,
846 0x0000,
847 0xffff);
848 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000849 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000850 *drawState->stencil() = gDrawToStencil;
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 if (canRenderDirectToStencil) {
854 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000855 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000857 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000858 }
859 }
860 }
861
862 // now we modify the clip bit by rendering either the clip
863 // element directly or a bounding rect of the entire clip.
864 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
865 for (int p = 0; p < passes; ++p) {
866 *drawState->stencil() = stencilSettings[p];
867 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000868 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000869 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000870 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000871 } else {
872 SET_RANDOM_COLOR
bsalomon@google.come3d32162012-07-20 13:37:06 +0000873 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000874 }
875 } else {
876 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000877 GrRect rect = GrRect::MakeLTRB(
878 SkIntToScalar(bounds.fLeft),
879 SkIntToScalar(bounds.fTop),
880 SkIntToScalar(bounds.fRight),
881 SkIntToScalar(bounds.fBottom));
bsalomon@google.come3d32162012-07-20 13:37:06 +0000882 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000883 }
884 }
885 }
886 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000887 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000888 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000889 // set this last because recursive draws may overwrite it back to kNone.
890 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
891 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000892 return true;
893}
894
bsalomon@google.com411dad02012-06-05 20:24:20 +0000895// mapping of clip-respecting stencil funcs to normal stencil funcs
896// mapping depends on whether stencil-clipping is in effect.
897static const GrStencilFunc
898 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
899 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
900 // In the Clip Funcs
901 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
902 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
903 kLess_StencilFunc, // kLessIfInClip_StencilFunc
904 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
905 // Special in the clip func that forces user's ref to be 0.
906 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
907 // make ref 0 and do normal nequal.
908 },
909 {// Stencil-Clipping is ENABLED
910 // In the Clip Funcs
911 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
912 // eq stencil clip bit, mask
913 // out user bits.
914
915 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
916 // add stencil bit to mask and ref
917
918 kLess_StencilFunc, // kLessIfInClip_StencilFunc
919 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
920 // for both of these we can add
921 // the clip bit to the mask and
922 // ref and compare as normal
923 // Special in the clip func that forces user's ref to be 0.
924 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
925 // make ref have only the clip bit set
926 // and make comparison be less
927 // 10..0 < 1..user_bits..
928 }
929};
930
bsalomon@google.coma3201942012-06-21 19:58:20 +0000931namespace {
932// Sets the settings to clip against the stencil buffer clip while ignoring the
933// client bits.
934const GrStencilSettings& basic_apply_stencil_clip_settings() {
935 // stencil settings to use when clip is in stencil
936 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
937 kKeep_StencilOp,
938 kKeep_StencilOp,
939 kAlwaysIfInClip_StencilFunc,
940 0x0000,
941 0x0000,
942 0x0000);
943 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
944}
945}
946
947void GrClipMaskManager::setGpuStencil() {
948 // We make two copies of the StencilSettings here (except in the early
949 // exit scenario. One copy from draw state to the stack var. Then another
950 // from the stack var to the gpu. We could make this class hold a ptr to
951 // GrGpu's fStencilSettings and eliminate the stack copy here.
952
953 const GrDrawState& drawState = fGpu->getDrawState();
954
955 // use stencil for clipping if clipping is enabled and the clip
956 // has been written into the stencil.
957 GrClipMaskManager::StencilClipMode clipMode;
958 if (this->isClipInStencil() && drawState.isClipState()) {
959 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
960 // We can't be modifying the clip and respecting it at the same time.
961 GrAssert(!drawState.isStateFlagEnabled(
962 GrGpu::kModifyStencilClip_StateBit));
963 } else if (drawState.isStateFlagEnabled(
964 GrGpu::kModifyStencilClip_StateBit)) {
965 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
966 } else {
967 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
968 }
969
970 GrStencilSettings settings;
971 // The GrGpu client may not be using the stencil buffer but we may need to
972 // enable it in order to respect a stencil clip.
973 if (drawState.getStencil().isDisabled()) {
974 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
975 settings = basic_apply_stencil_clip_settings();
976 } else {
977 fGpu->disableStencil();
978 return;
979 }
980 } else {
981 settings = drawState.getStencil();
982 }
983
984 // TODO: dynamically attach a stencil buffer
985 int stencilBits = 0;
986 GrStencilBuffer* stencilBuffer =
987 drawState.getRenderTarget()->getStencilBuffer();
988 if (NULL != stencilBuffer) {
989 stencilBits = stencilBuffer->bits();
990 }
991
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000992 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
993 !settings.usesWrapOp());
994 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000995 this->adjustStencilParams(&settings, clipMode, stencilBits);
996 fGpu->setStencilSettings(settings);
997}
998
999void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1000 StencilClipMode mode,
1001 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001002 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001003
1004 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001005 // We assume that this clip manager itself is drawing to the GrGpu and
1006 // has already setup the correct values.
1007 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001008 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001009
bsalomon@google.com411dad02012-06-05 20:24:20 +00001010 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1011 unsigned int userBits = clipBit - 1;
1012
bsalomon@google.coma3201942012-06-21 19:58:20 +00001013 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
1014 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001015
bsalomon@google.coma3201942012-06-21 19:58:20 +00001016 bool finished = false;
1017 while (!finished) {
1018 GrStencilFunc func = settings->func(face);
1019 uint16_t writeMask = settings->writeMask(face);
1020 uint16_t funcMask = settings->funcMask(face);
1021 uint16_t funcRef = settings->funcRef(face);
1022
1023 GrAssert((unsigned) func < kStencilFuncCount);
1024
1025 writeMask &= userBits;
1026
1027 if (func >= kBasicStencilFuncCount) {
1028 int respectClip = kRespectClip_StencilClipMode == mode;
1029 if (respectClip) {
1030 // The GrGpu class should have checked this
1031 GrAssert(this->isClipInStencil());
1032 switch (func) {
1033 case kAlwaysIfInClip_StencilFunc:
1034 funcMask = clipBit;
1035 funcRef = clipBit;
1036 break;
1037 case kEqualIfInClip_StencilFunc:
1038 case kLessIfInClip_StencilFunc:
1039 case kLEqualIfInClip_StencilFunc:
1040 funcMask = (funcMask & userBits) | clipBit;
1041 funcRef = (funcRef & userBits) | clipBit;
1042 break;
1043 case kNonZeroIfInClip_StencilFunc:
1044 funcMask = (funcMask & userBits) | clipBit;
1045 funcRef = clipBit;
1046 break;
1047 default:
1048 GrCrash("Unknown stencil func");
1049 }
1050 } else {
1051 funcMask &= userBits;
1052 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001053 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001054 const GrStencilFunc* table =
1055 gSpecialToBasicStencilFunc[respectClip];
1056 func = table[func - kBasicStencilFuncCount];
1057 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001058 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001059 funcMask &= userBits;
1060 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001061 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001062
1063 settings->setFunc(face, func);
1064 settings->setWriteMask(face, writeMask);
1065 settings->setFuncMask(face, funcMask);
1066 settings->setFuncRef(face, funcRef);
1067
1068 if (GrStencilSettings::kFront_Face == face) {
1069 face = GrStencilSettings::kBack_Face;
1070 finished = !twoSided;
1071 } else {
1072 finished = true;
1073 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001074 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001075 if (!twoSided) {
1076 settings->copyFrontSettingsToBack();
1077 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001078}
1079
1080////////////////////////////////////////////////////////////////////////////////
1081
robertphillips@google.comfa662942012-05-17 12:20:22 +00001082namespace {
1083
1084GrPathFill invert_fill(GrPathFill fill) {
1085 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001086 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1087 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1088 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1089 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1090 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001091 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001092 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1093 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1094 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1095 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1096 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1097 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001098 return gInvertedFillTable[fill];
1099}
1100
1101}
1102
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001103bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001104 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001105 GrIRect* resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001106 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001107
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001108 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001109 return true;
1110 }
1111
robertphillips@google.comf105b102012-05-14 12:18:26 +00001112 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001113 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001114 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001115 return false;
1116 }
1117
robertphillips@google.com2c756812012-05-22 20:28:23 +00001118 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001119
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001120 helper.init(*resultBounds, NULL);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001121
robertphillips@google.comfa662942012-05-17 12:20:22 +00001122 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001123 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1124
1125 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
1126 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001127 *resultBounds,
1128 &clearToInside,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001129 &firstOp);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001130
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001131 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001132
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001133 bool first = true;
1134 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001135
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001136 SkRegion::Op op = clip->fOp;
1137 if (first) {
1138 first = false;
1139 op = firstOp;
1140 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001141
1142 if (SkRegion::kIntersect_Op == op ||
1143 SkRegion::kReverseDifference_Op == op) {
1144 // Intersect and reverse difference require modifying pixels
1145 // outside of the geometry that is being "drawn". In both cases
1146 // we erase all the pixels outside of the geometry but
1147 // leave the pixels inside the geometry alone. For reverse
1148 // difference we invert all the pixels before clearing the ones
1149 // outside the geometry.
1150 if (SkRegion::kReverseDifference_Op == op) {
1151 SkRect temp = SkRect::MakeLTRB(
1152 SkIntToScalar(resultBounds->left()),
1153 SkIntToScalar(resultBounds->top()),
1154 SkIntToScalar(resultBounds->right()),
1155 SkIntToScalar(resultBounds->bottom()));
1156
1157 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001158 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001159 }
1160
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001161 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001162
1163 // convert the rect to a path so we can invert the fill
1164 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001165 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001166
1167 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001168 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001169 0x00);
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 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001173 invert_fill(get_path_fill(*clip->fPath)),
1174 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001175 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001176 }
1177
1178 continue;
1179 }
1180
1181 // The other ops (union, xor, diff) only affect pixels inside
1182 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001183 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001184
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001185 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001186 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001187 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001188
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001189 } else if (NULL != clip->fPath) {
1190 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001191 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001192 get_path_fill(*clip->fPath),
1193 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001194 }
1195 }
1196
robertphillips@google.comfa662942012-05-17 12:20:22 +00001197 // Because we are using the scratch texture cache, "accum" may be
1198 // larger than expected and have some cruft in the areas we aren't using.
1199 // Clear it out.
1200
1201 // TODO: need a simpler way to clear the texture - can we combine
1202 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001203 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001204 GrAssert(NULL != drawState);
1205 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001206 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001207 // can't leave the accum bound as a rendertarget
1208 drawState->setRenderTarget(temp);
1209
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001210 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001211
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001212 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001213
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001214 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001215 return true;
1216}
1217
robertphillips@google.comf294b772012-04-27 14:29:26 +00001218////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001219void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001220 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001221}