blob: 71743a03e999cb1b97d323064ea6cf1ac4b3d6cc [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.comf8d904a2012-07-31 12:18:16 +000023GrClipMaskCache::GrClipMaskCache()
24 : fContext(NULL)
25 , fStack(sizeof(GrClipStackFrame)) {
26 // We need an initial frame to capture the clip state prior to
27 // any pushes
28 SkNEW_PLACEMENT(fStack.push_back(), GrClipStackFrame);
29}
30
31void GrClipMaskCache::push() {
32 SkNEW_PLACEMENT(fStack.push_back(), GrClipStackFrame);
33}
34
robertphillips@google.comf294b772012-04-27 14:29:26 +000035////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000036namespace {
37// set up the draw state to enable the aa clipping mask. Besides setting up the
38// sampler matrix this also alters the vertex layout
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000039void setup_drawstate_aaclip(GrGpu* gpu,
40 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000041 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042 GrDrawState* drawState = gpu->drawState();
43 GrAssert(drawState);
44
45 static const int maskStage = GrPaint::kTotalStages+1;
46
47 GrMatrix mat;
48 mat.setIDiv(result->width(), result->height());
robertphillips@google.com7b112892012-07-31 15:18:21 +000049 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
50 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000051 mat.preConcat(drawState->getViewMatrix());
52
bsalomon@google.comb8670992012-07-25 21:27:09 +000053 drawState->sampler(maskStage)->reset(mat);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000054
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000055 drawState->createTextureEffect(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000056}
57
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000058bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000059 GrGpu* gpu,
60 const SkPath& path,
61 GrPathFill fill,
62 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000063 // last (false) parameter disallows use of the SW path renderer
64 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
65}
66
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000067GrPathFill get_path_fill(const SkPath& path) {
68 switch (path.getFillType()) {
69 case SkPath::kWinding_FillType:
70 return kWinding_GrPathFill;
71 case SkPath::kEvenOdd_FillType:
72 return kEvenOdd_GrPathFill;
73 case SkPath::kInverseWinding_FillType:
74 return kInverseWinding_GrPathFill;
75 case SkPath::kInverseEvenOdd_FillType:
76 return kInverseEvenOdd_GrPathFill;
77 default:
78 GrCrash("Unsupported path fill in clip.");
79 return kWinding_GrPathFill; // suppress warning
80 }
81}
82
robertphillips@google.comb99225c2012-07-24 18:20:10 +000083/**
84 * Does any individual clip in 'clipIn' use anti-aliasing?
85 */
86bool requires_AA(const GrClip& clipIn) {
87
88 GrClip::Iter iter;
89 iter.reset(clipIn, GrClip::Iter::kBottom_IterStart);
90
91 const GrClip::Iter::Clip* clip = NULL;
92 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
93 NULL != clip;
94 clip = iter.next()) {
95
96 if (clip->fDoAA) {
97 return true;
98 }
99 }
100
101 return false;
102}
103
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000104}
105
robertphillips@google.comfa662942012-05-17 12:20:22 +0000106/*
107 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
108 * will be used on any element. If so, it returns true to indicate that the
109 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
110 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000111bool GrClipMaskManager::useSWOnlyPath(const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000112
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000113 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000114 // a clip gets complex enough it can just be done in SW regardless
115 // of whether it would invoke the GrSoftwarePathRenderer.
116 bool useSW = false;
117
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000118 GrClip::Iter iter(clipIn, GrClip::Iter::kBottom_IterStart);
119 const GrClip::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000120
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000121 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
122 NULL != clip;
123 clip = iter.next()) {
124
125 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000126 // Everything before a replace op can be ignored so start
127 // afresh w.r.t. determining if any element uses the SW path
128 useSW = false;
129 }
130
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000131 // rects can always be drawn directly w/o using the software path
132 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000133 if (NULL != clip->fPath &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000134 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000135 *clip->fPath,
136 get_path_fill(*clip->fPath),
137 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000138 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000139 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000140 }
141
142 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000143}
144
robertphillips@google.comf294b772012-04-27 14:29:26 +0000145////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000146// sort out what kind of clip mask needs to be created: alpha, stencil,
147// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000148bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000149 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000150
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000151 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000152 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000153 fGpu->disableScissor();
154 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000155 return true;
156 }
157
158 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000159 // GrDrawTarget should have filtered this for us
160 GrAssert(NULL != rt);
161
robertphillips@google.com7b112892012-07-31 15:18:21 +0000162 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000163 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000164
robertphillips@google.com7b112892012-07-31 15:18:21 +0000165 clipDataIn->getConservativeBounds(rt, &devClipBounds,
166 &isIntersectionOfRects);
167 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000168 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000169 }
170
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000171 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
172 GrAssert(requiresAA == clipDataIn->fClipStack->requiresAA());
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000173
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000174#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000175 // If MSAA is enabled we can do everything in the stencil buffer.
176 // Otherwise check if we should just create the entire clip mask
177 // in software (this will only happen if the clip mask is anti-aliased
178 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000179 if (0 == rt->numSamples() &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000180 requiresAA &&
181 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000182 // The clip geometry is complex enough that it will be more
183 // efficient to create it entirely in software
184 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000185 GrIRect devBound;
186 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
187 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000188 fGpu->disableScissor();
189 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000190 return true;
191 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000192
193 // if SW clip mask creation fails fall through to the other
194 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000195 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000196#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000197
robertphillips@google.comf294b772012-04-27 14:29:26 +0000198#if GR_AA_CLIP
199 // If MSAA is enabled use the (faster) stencil path for AA clipping
200 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000201 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000202 // Since we are going to create a destination texture of the correct
203 // size for the mask (rather than being bound by the size of the
204 // render target) we aren't going to use scissoring like the stencil
205 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000206 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000207 GrIRect devBound;
208 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
209 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000210 fGpu->disableScissor();
211 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000212 return true;
213 }
214
215 // if alpha clip mask creation fails fall through to the stencil
216 // buffer method
217 }
218#endif // GR_AA_CLIP
219
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000220 // Either a hard (stencil buffer) clip was explicitly requested or
221 // an antialiased clip couldn't be created. In either case, free up
222 // the texture in the antialiased mask cache.
223 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000224 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
225 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000226 // AA cache.
227 fAACache.reset();
228
bsalomon@google.coma3201942012-06-21 19:58:20 +0000229 // If the clip is a rectangle then just set the scissor. Otherwise, create
230 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000231 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000232 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000233 this->setGpuStencil();
234 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000235 }
236
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000237 // use the stencil clip if we can't represent the clip as a rectangle.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000238 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
239 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000240
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000241 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000242 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000243 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000244 // This must occur after createStencilClipMask. That function may change
245 // the scissor. Also, it only guarantees that the stencil mask is correct
246 // within the bounds it was passed, so we must use both stencil and scissor
247 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000248 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000249 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000250 return true;
251}
252
253#define VISUALIZE_COMPLEX_CLIP 0
254
255#if VISUALIZE_COMPLEX_CLIP
256 #include "GrRandom.h"
257 GrRandom gRandom;
258 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
259#else
260 #define SET_RANDOM_COLOR
261#endif
262
263namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000264/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000265 * Does "canvContainer" contain "devContainee"? If either is empty then
266 * no containment is possible. "canvContainer" is in canvas coordinates while
267 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000268 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000269 */
robertphillips@google.com7b112892012-07-31 15:18:21 +0000270bool contains(const SkRect& canvContainer,
271 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000272 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000273 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
274 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
275 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
276 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
277 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000278}
279
robertphillips@google.comf294b772012-04-27 14:29:26 +0000280////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000281// determines how many elements at the head of the clip can be skipped and
282// whether the initial clear should be to the inside- or outside-the-clip value,
283// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000284const GrClip::Iter::Clip* process_initial_clip_elements(
285 GrClip::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000286 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000287 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000288 SkRegion::Op* firstOp,
289 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000290
291 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000292
293 // logically before the first element of the clip stack is
294 // processed the clip is entirely open. However, depending on the
295 // first set op we may prefer to clear to 0 for performance. We may
296 // also be able to skip the initial clip paths/rects. We loop until
297 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000298 bool done = false;
299 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000301 const GrClip::Iter::Clip* clip = NULL;
302
303 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
304 NULL != clip && !done;
305 clip = iter->next()) {
306 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000307 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000308 // replace ignores everything previous
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 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000313 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000314 // if this element contains the entire bounds then we
315 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000316 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000317 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000318 break;
319 }
320 // if everything is initially clearToInside then intersect is
321 // same as clear to 0 and treat as a replace. Otherwise,
322 // set stays empty.
323 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000324 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000325 *clearToInside = false;
326 done = true;
327 }
328 break;
329 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000330 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331 // if everything is initially outside then union is
332 // same as replace. Otherwise, every pixel is still
333 // clearToInside
334 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000335 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000336 done = true;
337 }
338 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000339 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000340 // xor is same as difference or replace both of which
341 // can be 1-pass instead of 2 for xor.
342 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000343 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000344 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000345 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000346 }
347 done = true;
348 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000349 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000350 // if all pixels are clearToInside then we have to process the
351 // difference, otherwise it has no effect and all pixels
352 // remain outside.
353 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000354 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000355 done = true;
356 }
357 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000358 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000359 // if all pixels are clearToInside then reverse difference
360 // produces empty set. Otherise it is same as replace
361 if (*clearToInside) {
362 *clearToInside = false;
363 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000364 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000365 done = true;
366 }
367 break;
368 default:
369 GrCrash("Unknown set op.");
370 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000371
372 if (done) {
373 // we need to break out here (rather than letting the test in
374 // the loop do it) since backing up the iterator is very expensive
375 break;
376 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000377 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000378 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000379}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000381}
382
robertphillips@google.comf294b772012-04-27 14:29:26 +0000383namespace {
384
385////////////////////////////////////////////////////////////////////////////////
386// set up the OpenGL blend function to perform the specified
387// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000388void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389
390 switch (op) {
391 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000392 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000393 break;
394 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000395 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000396 break;
397 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000398 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000399 break;
400 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000401 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402 break;
403 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000404 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000405 break;
406 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000407 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000408 break;
409 default:
410 GrAssert(false);
411 break;
412 }
413}
414
robertphillips@google.comf294b772012-04-27 14:29:26 +0000415////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000416bool draw_path_in_software(GrContext* context,
417 GrGpu* gpu,
418 const SkPath& path,
419 GrPathFill fill,
420 bool doAA,
421 const GrIRect& resultBounds) {
422
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000423 SkAutoTUnref<GrTexture> texture(
424 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
425 resultBounds, fill,
426 doAA, NULL));
427 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000428 return false;
429 }
430
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000431 // The ClipMaskManager accumulates the clip mask in the UL corner
432 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000433
bsalomon@google.come3d32162012-07-20 13:37:06 +0000434 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000435
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000436 GrAssert(!GrIsFillInverted(fill));
437 return true;
438}
439
440
441////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000442bool draw_path(GrContext* context,
443 GrGpu* gpu,
444 const SkPath& path,
445 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000446 bool doAA,
447 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000448
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000449 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000450 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000451 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000452 }
453
bsalomon@google.come3d32162012-07-20 13:37:06 +0000454 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000455 return true;
456}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000457
robertphillips@google.com7b112892012-07-31 15:18:21 +0000458// 'rect' enters in device coordinates and leaves in canvas coordinates
459void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
460 GrAssert(NULL != rect);
461
462 rect->fLeft += SkIntToScalar(origin.fX);
463 rect->fTop += SkIntToScalar(origin.fY);
464 rect->fRight += SkIntToScalar(origin.fX);
465 rect->fBottom += SkIntToScalar(origin.fY);
466}
467
robertphillips@google.com72176b22012-05-23 13:19:12 +0000468}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000469
470////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000471bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000472 const GrClip::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000473 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000474 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000475 GrAssert(NULL != drawState);
476
477 drawState->setRenderTarget(target->asRenderTarget());
478
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000479 if (NULL != clip->fRect) {
480 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000481 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000482 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000483 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000484 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000485 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000486 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000487 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000488 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000489 *clip->fPath,
490 get_path_fill(*clip->fPath),
491 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000492 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000493 }
494 return true;
495}
496
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000497void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000498 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000499 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000500 GrAssert(NULL != drawState);
501
502 // no AA here since it is encoded in the texture
503 drawState->setRenderTarget(target->asRenderTarget());
504
505 GrMatrix sampleM;
506 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000507
bsalomon@google.comb8670992012-07-25 21:27:09 +0000508 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000509 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000510
robertphillips@google.comf105b102012-05-14 12:18:26 +0000511 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
512 SkIntToScalar(target->height()));
513
bsalomon@google.come3d32162012-07-20 13:37:06 +0000514 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000515
tomhudson@google.com676e6602012-07-10 17:21:48 +0000516 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000517}
518
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000519// get a texture to act as a temporary buffer for AA clip boolean operations
520// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000521void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000522 GrAutoScratchTexture* temp) {
523 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000524 // we've already allocated the temp texture
525 return;
526 }
527
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000528 GrTextureDesc desc;
529 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
530 desc.fWidth = bounds.width();
531 desc.fHeight = bounds.height();
532 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000533
robertphillips@google.com2c756812012-05-22 20:28:23 +0000534 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000535}
536
robertphillips@google.comf105b102012-05-14 12:18:26 +0000537
538void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000539 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000540 // Since we are setting up the cache we know the last lookup was a miss
541 // Free up the currently cached mask so it can be reused
542 fAACache.reset();
543
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000544 GrTextureDesc desc;
545 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
546 desc.fWidth = bounds.width();
547 desc.fHeight = bounds.height();
548 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000549
550 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000551}
552
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000553////////////////////////////////////////////////////////////////////////////////
554// Shared preamble between gpu and SW-only AA clip mask creation paths.
555// Handles caching, determination of clip mask bound & allocation (if needed)
556// of the result texture
557// Returns true if there is no more work to be done (i.e., we got a cache hit)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000558bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000559 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000560 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000561 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562 GrAssert(origDrawState->isClipState());
563
564 GrRenderTarget* rt = origDrawState->getRenderTarget();
565 GrAssert(NULL != rt);
566
robertphillips@google.comf294b772012-04-27 14:29:26 +0000567 // unlike the stencil path the alpha path is not bound to the size of the
568 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000569 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com7b112892012-07-31 15:18:21 +0000570 GrIRect devClipBounds;
571 clipDataIn.getConservativeBounds(rt, &devClipBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000572
573 // need to outset a pixel since the standard bounding box computation
574 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com7b112892012-07-31 15:18:21 +0000575 devClipBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000576
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000577 // TODO: make sure we don't outset if bounds are still 0,0 @ min
578
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000579 if (fAACache.canReuse(*clipDataIn.fClipStack,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000580 devClipBounds.width(),
581 devClipBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000582 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000583 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000584 return true;
585 }
586
robertphillips@google.com7b112892012-07-31 15:18:21 +0000587 this->setupCache(*clipDataIn.fClipStack, devClipBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000588
robertphillips@google.com7b112892012-07-31 15:18:21 +0000589 *devResultBounds = devClipBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000590 return false;
591}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000592
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000593////////////////////////////////////////////////////////////////////////////////
594// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000595bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000596 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000597 GrIRect *devResultBounds) {
598 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000599 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
600
robertphillips@google.com7b112892012-07-31 15:18:21 +0000601 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000602 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000603 return true;
604 }
605
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000606 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
607
robertphillips@google.comf105b102012-05-14 12:18:26 +0000608 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000609 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000610 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000611 return false;
612 }
613
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000614 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
615 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000616
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000617 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000618
robertphillips@google.com7b112892012-07-31 15:18:21 +0000619 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000620 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000621 // if we were able to trim down the size of the mask we need to
622 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000623 drawState->viewMatrix()->setTranslate(
robertphillips@google.com7b112892012-07-31 15:18:21 +0000624 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
625 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626 }
627
628 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000629 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
630
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000631 GrClip::Iter iter(*clipDataIn.fClipStack,
632 GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000633 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000634 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000635 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000636 &firstOp,
637 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000638
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000639 fGpu->clear(NULL,
640 clearToInside ? 0xffffffff : 0x00000000,
641 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000642
robertphillips@google.comf105b102012-05-14 12:18:26 +0000643 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000644 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000645 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000646 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000648 SkRegion::Op op = clip->fOp;
649 if (first) {
650 first = false;
651 op = firstOp;
652 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000653
654 if (SkRegion::kReplace_Op == op) {
655 // TODO: replace is actually a lot faster then intersection
656 // for this path - refactor the stencil path so it can handle
657 // replace ops and alter GrClip to allow them through
658
659 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000660 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000661
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000662 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000663 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000664
665 } else if (SkRegion::kReverseDifference_Op == op ||
666 SkRegion::kIntersect_Op == op) {
667 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000668 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000669 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000670 continue;
671 }
672
robertphillips@google.com7b112892012-07-31 15:18:21 +0000673 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000674 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000675 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000676 return false;
677 }
678
robertphillips@google.comf294b772012-04-27 14:29:26 +0000679 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000680 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000681
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000682 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000683 this->drawClipShape(temp.texture(), clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684
685 // TODO: rather than adding these two translations here
686 // compute the bounding box needed to render the texture
687 // into temp
robertphillips@google.com7b112892012-07-31 15:18:21 +0000688 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000689 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
690 // In order for the merge of the temp clip into the accumulator
691 // to work we need to disable the translation
692 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000693 }
694
695 // Now draw into the accumulator using the real operation
696 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000697 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000698 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000699
robertphillips@google.com7b112892012-07-31 15:18:21 +0000700 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000701 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
702 drawState->viewMatrix()->setTranslate(
robertphillips@google.com7b112892012-07-31 15:18:21 +0000703 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
704 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000705 }
706
707 } else {
708 // all the remaining ops can just be directly draw into
709 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000710 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000711 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000712 }
713 }
714
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000715 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000716 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000717 return true;
718}
719
720////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com7b112892012-07-31 15:18:21 +0000721// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000722// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000723bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000724 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000726 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000728 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729 GrAssert(drawState->isClipState());
730
731 GrRenderTarget* rt = drawState->getRenderTarget();
732 GrAssert(NULL != rt);
733
734 // TODO: dynamically attach a SB when needed.
735 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
736 if (NULL == stencilBuffer) {
737 return false;
738 }
739
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000740 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000742 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743
744 // we set the current clip to the bounds so that our recursive
745 // draws are scissored to them. We use the copy of the complex clip
746 // we just stashed on the SB to render from. We set it back after
747 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000748 const GrClipData* oldClipData = fGpu->getClip();
749
robertphillips@google.com7b112892012-07-31 15:18:21 +0000750 // The origin of 'newClipData' is (0, 0) so it is okay to place
751 // a device-coordinate bound in 'newClipStack'
752 GrClip newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000753 GrClipData newClipData;
754 newClipData.fClipStack = &newClipStack;
755
756 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000757
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000758 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
759 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000761 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000763 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
764 // Add the saveLayer's offset to the view matrix rather than
765 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000766 drawState->viewMatrix()->setTranslate(
767 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000768 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000769 }
770
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000771#if !VISUALIZE_COMPLEX_CLIP
772 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
773#endif
774
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 int clipBit = stencilBuffer->bits();
776 SkASSERT((clipBit <= 16) &&
777 "Ganesh only handles 16b or smaller stencil buffers");
778 clipBit = (1 << (clipBit-1));
779
robertphillips@google.com7b112892012-07-31 15:18:21 +0000780 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781
782 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000783 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
784
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000785 GrClip::Iter iter(*oldClipData->fClipStack,
786 GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000787 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000788 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000789 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000790 &firstOp,
791 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000792
robertphillips@google.com7b112892012-07-31 15:18:21 +0000793 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000794 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000795
796 // walk through each clip element and perform its set op
797 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000798 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799 GrPathFill fill;
800 bool fillInverted;
801 // enabled at bottom of loop
802 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000803 // if the target is MSAA then we want MSAA enabled when the clip is soft
804 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000805 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000806 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
807 } else {
808 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
809 }
810 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811
812 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000813 // directly to the stencil buffer
814 // with a non-inverted fill rule
815 // without extra passes to
816 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000817
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000818 SkRegion::Op op = clip->fOp;
819 if (first) {
820 first = false;
821 op = firstOp;
822 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000823
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000825 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000826 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000827 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000828 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000829 fillInverted = false;
830 // there is no point in intersecting a screen filling
831 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000832 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000833 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000834 continue;
835 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000836 } else if (NULL != clip->fPath) {
837 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000838 fillInverted = GrIsFillInverted(fill);
839 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000840 clipPath = clip->fPath;
robertphillips@google.com2c756812012-05-22 20:28:23 +0000841 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000842 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000843 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844 if (NULL == pr) {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000845 fGpu->setClip(oldClipData); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000846 return false;
847 }
848 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000849 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000850 }
851
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000852 int passes;
853 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
854
855 bool canDrawDirectToClip; // Given the renderer, the element,
856 // fill rule, and set operation can
857 // we render the element directly to
858 // stencil bit used for clipping.
859 canDrawDirectToClip =
860 GrStencilSettings::GetClipPasses(op,
861 canRenderDirectToStencil,
862 clipBit,
863 fillInverted,
864 &passes, stencilSettings);
865
866 // draw the element to the client stencil bits if necessary
867 if (!canDrawDirectToClip) {
868 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
869 kIncClamp_StencilOp,
870 kIncClamp_StencilOp,
871 kAlways_StencilFunc,
872 0xffff,
873 0x0000,
874 0xffff);
875 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000876 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000877 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000878 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000879 } else {
880 if (canRenderDirectToStencil) {
881 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000882 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000883 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000884 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000885 }
886 }
887 }
888
889 // now we modify the clip bit by rendering either the clip
890 // element directly or a bounding rect of the entire clip.
891 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
892 for (int p = 0; p < passes; ++p) {
893 *drawState->stencil() = stencilSettings[p];
894 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000895 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000896 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000897 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000898 } else {
899 SET_RANDOM_COLOR
bsalomon@google.come3d32162012-07-20 13:37:06 +0000900 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000901 }
902 } else {
903 SET_RANDOM_COLOR
robertphillips@google.com7b112892012-07-31 15:18:21 +0000904 // 'devClipBounds' is already in device coordinates so the
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000905 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000906 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000907 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000908 GrRect canvClipBounds;
909 canvClipBounds.set(devClipBounds);
910 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
911 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000912 }
913 }
914 }
915 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000916 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000917 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000918 // set this last because recursive draws may overwrite it back to kNone.
919 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
920 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000921 return true;
922}
923
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000924
bsalomon@google.com411dad02012-06-05 20:24:20 +0000925// mapping of clip-respecting stencil funcs to normal stencil funcs
926// mapping depends on whether stencil-clipping is in effect.
927static const GrStencilFunc
928 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
929 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
930 // In the Clip Funcs
931 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
932 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
933 kLess_StencilFunc, // kLessIfInClip_StencilFunc
934 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
935 // Special in the clip func that forces user's ref to be 0.
936 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
937 // make ref 0 and do normal nequal.
938 },
939 {// Stencil-Clipping is ENABLED
940 // In the Clip Funcs
941 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
942 // eq stencil clip bit, mask
943 // out user bits.
944
945 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
946 // add stencil bit to mask and ref
947
948 kLess_StencilFunc, // kLessIfInClip_StencilFunc
949 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
950 // for both of these we can add
951 // the clip bit to the mask and
952 // ref and compare as normal
953 // Special in the clip func that forces user's ref to be 0.
954 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
955 // make ref have only the clip bit set
956 // and make comparison be less
957 // 10..0 < 1..user_bits..
958 }
959};
960
bsalomon@google.coma3201942012-06-21 19:58:20 +0000961namespace {
962// Sets the settings to clip against the stencil buffer clip while ignoring the
963// client bits.
964const GrStencilSettings& basic_apply_stencil_clip_settings() {
965 // stencil settings to use when clip is in stencil
966 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
967 kKeep_StencilOp,
968 kKeep_StencilOp,
969 kAlwaysIfInClip_StencilFunc,
970 0x0000,
971 0x0000,
972 0x0000);
973 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
974}
975}
976
977void GrClipMaskManager::setGpuStencil() {
978 // We make two copies of the StencilSettings here (except in the early
979 // exit scenario. One copy from draw state to the stack var. Then another
980 // from the stack var to the gpu. We could make this class hold a ptr to
981 // GrGpu's fStencilSettings and eliminate the stack copy here.
982
983 const GrDrawState& drawState = fGpu->getDrawState();
984
985 // use stencil for clipping if clipping is enabled and the clip
986 // has been written into the stencil.
987 GrClipMaskManager::StencilClipMode clipMode;
988 if (this->isClipInStencil() && drawState.isClipState()) {
989 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
990 // We can't be modifying the clip and respecting it at the same time.
991 GrAssert(!drawState.isStateFlagEnabled(
992 GrGpu::kModifyStencilClip_StateBit));
993 } else if (drawState.isStateFlagEnabled(
994 GrGpu::kModifyStencilClip_StateBit)) {
995 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
996 } else {
997 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
998 }
999
1000 GrStencilSettings settings;
1001 // The GrGpu client may not be using the stencil buffer but we may need to
1002 // enable it in order to respect a stencil clip.
1003 if (drawState.getStencil().isDisabled()) {
1004 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1005 settings = basic_apply_stencil_clip_settings();
1006 } else {
1007 fGpu->disableStencil();
1008 return;
1009 }
1010 } else {
1011 settings = drawState.getStencil();
1012 }
1013
1014 // TODO: dynamically attach a stencil buffer
1015 int stencilBits = 0;
1016 GrStencilBuffer* stencilBuffer =
1017 drawState.getRenderTarget()->getStencilBuffer();
1018 if (NULL != stencilBuffer) {
1019 stencilBits = stencilBuffer->bits();
1020 }
1021
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001022 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
1023 !settings.usesWrapOp());
1024 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001025 this->adjustStencilParams(&settings, clipMode, stencilBits);
1026 fGpu->setStencilSettings(settings);
1027}
1028
1029void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1030 StencilClipMode mode,
1031 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001032 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001033
1034 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001035 // We assume that this clip manager itself is drawing to the GrGpu and
1036 // has already setup the correct values.
1037 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001038 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001039
bsalomon@google.com411dad02012-06-05 20:24:20 +00001040 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1041 unsigned int userBits = clipBit - 1;
1042
bsalomon@google.coma3201942012-06-21 19:58:20 +00001043 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
1044 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001045
bsalomon@google.coma3201942012-06-21 19:58:20 +00001046 bool finished = false;
1047 while (!finished) {
1048 GrStencilFunc func = settings->func(face);
1049 uint16_t writeMask = settings->writeMask(face);
1050 uint16_t funcMask = settings->funcMask(face);
1051 uint16_t funcRef = settings->funcRef(face);
1052
1053 GrAssert((unsigned) func < kStencilFuncCount);
1054
1055 writeMask &= userBits;
1056
1057 if (func >= kBasicStencilFuncCount) {
1058 int respectClip = kRespectClip_StencilClipMode == mode;
1059 if (respectClip) {
1060 // The GrGpu class should have checked this
1061 GrAssert(this->isClipInStencil());
1062 switch (func) {
1063 case kAlwaysIfInClip_StencilFunc:
1064 funcMask = clipBit;
1065 funcRef = clipBit;
1066 break;
1067 case kEqualIfInClip_StencilFunc:
1068 case kLessIfInClip_StencilFunc:
1069 case kLEqualIfInClip_StencilFunc:
1070 funcMask = (funcMask & userBits) | clipBit;
1071 funcRef = (funcRef & userBits) | clipBit;
1072 break;
1073 case kNonZeroIfInClip_StencilFunc:
1074 funcMask = (funcMask & userBits) | clipBit;
1075 funcRef = clipBit;
1076 break;
1077 default:
1078 GrCrash("Unknown stencil func");
1079 }
1080 } else {
1081 funcMask &= userBits;
1082 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001083 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001084 const GrStencilFunc* table =
1085 gSpecialToBasicStencilFunc[respectClip];
1086 func = table[func - kBasicStencilFuncCount];
1087 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001088 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001089 funcMask &= userBits;
1090 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001091 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001092
1093 settings->setFunc(face, func);
1094 settings->setWriteMask(face, writeMask);
1095 settings->setFuncMask(face, funcMask);
1096 settings->setFuncRef(face, funcRef);
1097
1098 if (GrStencilSettings::kFront_Face == face) {
1099 face = GrStencilSettings::kBack_Face;
1100 finished = !twoSided;
1101 } else {
1102 finished = true;
1103 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001104 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001105 if (!twoSided) {
1106 settings->copyFrontSettingsToBack();
1107 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001108}
1109
1110////////////////////////////////////////////////////////////////////////////////
1111
robertphillips@google.comfa662942012-05-17 12:20:22 +00001112namespace {
1113
1114GrPathFill invert_fill(GrPathFill fill) {
1115 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001116 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1117 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1118 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1119 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1120 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001121 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001122 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1123 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1124 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1125 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1126 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1127 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001128 return gInvertedFillTable[fill];
1129}
1130
1131}
1132
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001133bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001134 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001135 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001136 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001137
robertphillips@google.com7b112892012-07-31 15:18:21 +00001138 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001139 return true;
1140 }
1141
robertphillips@google.comf105b102012-05-14 12:18:26 +00001142 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001143 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001144 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001145 return false;
1146 }
1147
robertphillips@google.com2c756812012-05-22 20:28:23 +00001148 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001149
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001150 GrMatrix matrix;
1151 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
1152 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001153 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001154
robertphillips@google.comfa662942012-05-17 12:20:22 +00001155 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001156 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1157
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001158 GrClip::Iter iter(*clipDataIn.fClipStack,
1159 GrClip::Iter::kBottom_IterStart);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001160 const GrClip::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001161 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001162 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001163 &firstOp,
1164 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001165
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001166 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001167
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001168 bool first = true;
1169 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001170
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001171 SkRegion::Op op = clip->fOp;
1172 if (first) {
1173 first = false;
1174 op = firstOp;
1175 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001176
1177 if (SkRegion::kIntersect_Op == op ||
1178 SkRegion::kReverseDifference_Op == op) {
1179 // Intersect and reverse difference require modifying pixels
1180 // outside of the geometry that is being "drawn". In both cases
1181 // we erase all the pixels outside of the geometry but
1182 // leave the pixels inside the geometry alone. For reverse
1183 // difference we invert all the pixels before clearing the ones
1184 // outside the geometry.
1185 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001186 SkRect temp;
1187 temp.set(*devResultBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001188
1189 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001190 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001191 }
1192
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001193 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001194
1195 // convert the rect to a path so we can invert the fill
1196 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001197 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001198
1199 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001200 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001201 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001202 } else if (NULL != clip->fPath) {
1203 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001204 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001205 invert_fill(get_path_fill(*clip->fPath)),
1206 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001207 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001208 }
1209
1210 continue;
1211 }
1212
1213 // The other ops (union, xor, diff) only affect pixels inside
1214 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001215 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001216
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001217 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001218 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001219 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001220
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001221 } else if (NULL != clip->fPath) {
1222 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001223 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001224 get_path_fill(*clip->fPath),
1225 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001226 }
1227 }
1228
robertphillips@google.comfa662942012-05-17 12:20:22 +00001229 // Because we are using the scratch texture cache, "accum" may be
1230 // larger than expected and have some cruft in the areas we aren't using.
1231 // Clear it out.
1232
1233 // TODO: need a simpler way to clear the texture - can we combine
1234 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001235 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001236 GrAssert(NULL != drawState);
1237 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001238 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001239 // can't leave the accum bound as a rendertarget
1240 drawState->setRenderTarget(temp);
1241
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001242 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001243
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001244 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001245
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001246 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001247 return true;
1248}
1249
robertphillips@google.comf294b772012-04-27 14:29:26 +00001250////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001251void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001252 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001253}