blob: 322fba146a55c2bdd5bc98c74a1ca16ad4896deb [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.com46a86002012-08-08 10:42:44 +000019#include "GrCacheID.h"
20
21GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000022
robertphillips@google.comba998f22012-10-12 11:33:56 +000023#define GR_AA_CLIP 1
24#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000025
robertphillips@google.comf294b772012-04-27 14:29:26 +000026////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000027namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028// set up the draw state to enable the aa clipping mask. Besides setting up the
robertphillips@google.coma72eef32012-05-01 17:22:59 +000029// sampler matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030void setup_drawstate_aaclip(GrGpu* gpu,
31 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000032 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000033 GrDrawState* drawState = gpu->drawState();
34 GrAssert(drawState);
35
36 static const int maskStage = GrPaint::kTotalStages+1;
37
38 GrMatrix mat;
39 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000041 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042 mat.preConcat(drawState->getViewMatrix());
43
bsalomon@google.comb8670992012-07-25 21:27:09 +000044 drawState->sampler(maskStage)->reset(mat);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000046 drawState->createTextureEffect(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000047}
48
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000049bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000050 GrGpu* gpu,
51 const SkPath& path,
52 GrPathFill fill,
53 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000054 // last (false) parameter disallows use of the SW path renderer
55 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
56}
57
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000058GrPathFill get_path_fill(const SkPath& path) {
59 switch (path.getFillType()) {
60 case SkPath::kWinding_FillType:
61 return kWinding_GrPathFill;
62 case SkPath::kEvenOdd_FillType:
63 return kEvenOdd_GrPathFill;
64 case SkPath::kInverseWinding_FillType:
65 return kInverseWinding_GrPathFill;
66 case SkPath::kInverseEvenOdd_FillType:
67 return kInverseEvenOdd_GrPathFill;
68 default:
69 GrCrash("Unsupported path fill in clip.");
70 return kWinding_GrPathFill; // suppress warning
71 }
72}
73
robertphillips@google.comb99225c2012-07-24 18:20:10 +000074/**
75 * Does any individual clip in 'clipIn' use anti-aliasing?
76 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000077bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000078
robertphillips@google.com641f8b12012-07-31 19:15:58 +000079 SkClipStack::Iter iter;
80 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000081
robertphillips@google.com641f8b12012-07-31 19:15:58 +000082 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comb99225c2012-07-24 18:20:10 +000083 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
84 NULL != clip;
85 clip = iter.next()) {
86
87 if (clip->fDoAA) {
88 return true;
89 }
90 }
91
92 return false;
93}
94
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000095}
96
robertphillips@google.comfa662942012-05-17 12:20:22 +000097/*
98 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
99 * will be used on any element. If so, it returns true to indicate that the
100 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
101 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000102bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000103
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000104 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105 // a clip gets complex enough it can just be done in SW regardless
106 // of whether it would invoke the GrSoftwarePathRenderer.
107 bool useSW = false;
108
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000109 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
110 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000111
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000112 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
113 NULL != clip;
114 clip = iter.next()) {
115
116 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000117 // Everything before a replace op can be ignored so start
118 // afresh w.r.t. determining if any element uses the SW path
119 useSW = false;
120 }
121
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000122 // rects can always be drawn directly w/o using the software path
123 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000124 if (NULL != clip->fPath &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000125 path_needs_SW_renderer(this->getContext(), fGpu,
126 *clip->fPath,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000127 get_path_fill(*clip->fPath),
128 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000129 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000130 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000131 }
132
133 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000134}
135
robertphillips@google.comf294b772012-04-27 14:29:26 +0000136////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000137// sort out what kind of clip mask needs to be created: alpha, stencil,
138// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000139bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000140 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000141
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000142 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000143 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000144 fGpu->disableScissor();
145 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000146 return true;
147 }
148
149 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000150 // GrDrawTarget should have filtered this for us
151 GrAssert(NULL != rt);
152
robertphillips@google.com7b112892012-07-31 15:18:21 +0000153 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000154 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000155
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000156 clipDataIn->getConservativeBounds(rt, &devClipBounds,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000157 &isIntersectionOfRects);
158 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000159 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000160 }
161
bsalomon@google.com100abf42012-09-05 17:40:04 +0000162#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000163 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000164
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000165 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000166 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000167 // in software (this will only happen if the clip mask is anti-aliased
168 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000169 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000170 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000171 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
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.com7b112892012-07-31 15:18:21 +0000175 GrIRect devBound;
176 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
177 setup_drawstate_aaclip(fGpu, result, devBound);
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.com7b112892012-07-31 15:18:21 +0000197 GrIRect devBound;
198 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
199 setup_drawstate_aaclip(fGpu, result, devBound);
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
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000211 // 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
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000215 // 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.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000221 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000222 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000223 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.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000228 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000229 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000230
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000231 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000232 this->createStencilClipMask(*clipDataIn, devClipBounds);
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.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000238 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000239 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/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000255 * Does "canvContainer" contain "devContainee"? If either is empty then
256 * no containment is possible. "canvContainer" is in canvas coordinates while
257 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000258 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000259 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000260bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000261 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000262 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000263 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000264 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000265 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000266 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000267 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000268}
269
robertphillips@google.comf294b772012-04-27 14:29:26 +0000270////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000271// determines how many elements at the head of the clip can be skipped and
272// whether the initial clear should be to the inside- or outside-the-clip value,
273// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000274const SkClipStack::Iter::Clip* process_initial_clip_elements(
275 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000276 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000277 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000278 SkRegion::Op* firstOp,
279 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000280
281 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000282
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000283 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000284 // processed the clip is entirely open. However, depending on the
285 // first set op we may prefer to clear to 0 for performance. We may
286 // also be able to skip the initial clip paths/rects. We loop until
287 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000288 bool done = false;
289 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000290
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000291 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000292
293 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
294 NULL != clip && !done;
295 clip = iter->next()) {
296 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000297 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000298 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000299 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 *clearToInside = false;
301 done = true;
302 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000303 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000304 // if this element contains the entire bounds then we
305 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000306 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000307 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000308 break;
309 }
310 // if everything is initially clearToInside then intersect is
311 // same as clear to 0 and treat as a replace. Otherwise,
312 // set stays empty.
313 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000314 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000315 *clearToInside = false;
316 done = true;
317 }
318 break;
319 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000320 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000321 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000322 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000323 // clearToInside
324 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000325 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000326 done = true;
327 }
328 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000329 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000330 // xor is same as difference or replace both of which
331 // can be 1-pass instead of 2 for xor.
332 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000333 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000334 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000335 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000336 }
337 done = true;
338 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000339 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000340 // if all pixels are clearToInside then we have to process the
341 // difference, otherwise it has no effect and all pixels
342 // remain outside.
343 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000344 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345 done = true;
346 }
347 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000348 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000349 // if all pixels are clearToInside then reverse difference
350 // produces empty set. Otherise it is same as replace
351 if (*clearToInside) {
352 *clearToInside = false;
353 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000354 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000355 done = true;
356 }
357 break;
358 default:
359 GrCrash("Unknown set op.");
360 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000361
362 if (done) {
363 // we need to break out here (rather than letting the test in
364 // the loop do it) since backing up the iterator is very expensive
365 break;
366 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000367 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000368 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000369}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000370
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000371}
372
robertphillips@google.comf294b772012-04-27 14:29:26 +0000373namespace {
374
375////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000376// set up the OpenGL blend function to perform the specified
377// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000378void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000379
380 switch (op) {
381 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000382 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000383 break;
384 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000385 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000386 break;
387 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000388 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000389 break;
390 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000391 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392 break;
393 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000394 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000395 break;
396 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000397 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000398 break;
399 default:
400 GrAssert(false);
401 break;
402 }
403}
404
robertphillips@google.comf294b772012-04-27 14:29:26 +0000405////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000406bool draw_path_in_software(GrContext* context,
407 GrGpu* gpu,
408 const SkPath& path,
409 GrPathFill fill,
410 bool doAA,
411 const GrIRect& resultBounds) {
412
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000413 SkAutoTUnref<GrTexture> texture(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000414 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
415 resultBounds, fill,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000416 doAA, NULL));
417 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000418 return false;
419 }
420
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000421 // The ClipMaskManager accumulates the clip mask in the UL corner
422 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000423
bsalomon@google.come3d32162012-07-20 13:37:06 +0000424 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000425
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000426 GrAssert(!GrIsFillInverted(fill));
427 return true;
428}
429
430
431////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000432bool draw_path(GrContext* context,
433 GrGpu* gpu,
434 const SkPath& path,
435 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000436 bool doAA,
437 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000439 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000440 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000441 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000442 }
443
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000444 pr->drawPath(path, fill, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000445 return true;
446}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000447
robertphillips@google.com7b112892012-07-31 15:18:21 +0000448// 'rect' enters in device coordinates and leaves in canvas coordinates
449void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
450 GrAssert(NULL != rect);
451
452 rect->fLeft += SkIntToScalar(origin.fX);
453 rect->fTop += SkIntToScalar(origin.fY);
454 rect->fRight += SkIntToScalar(origin.fX);
455 rect->fBottom += SkIntToScalar(origin.fY);
456}
457
robertphillips@google.com72176b22012-05-23 13:19:12 +0000458}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000459
460////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000461bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000462 const SkClipStack::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000463 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000464 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000465 GrAssert(NULL != drawState);
466
467 drawState->setRenderTarget(target->asRenderTarget());
468
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000469 if (NULL != clip->fRect) {
470 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000471 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000472 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000473 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000474 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000475 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000476 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000477 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000478 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000479 *clip->fPath,
480 get_path_fill(*clip->fPath),
481 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000482 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000483 }
484 return true;
485}
486
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000487void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000488 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000489 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000490 GrAssert(NULL != drawState);
491
492 // no AA here since it is encoded in the texture
493 drawState->setRenderTarget(target->asRenderTarget());
494
495 GrMatrix sampleM;
496 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497
bsalomon@google.comb8670992012-07-25 21:27:09 +0000498 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000499 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000500
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000501 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
robertphillips@google.comf105b102012-05-14 12:18:26 +0000502 SkIntToScalar(target->height()));
503
bsalomon@google.come3d32162012-07-20 13:37:06 +0000504 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000505
tomhudson@google.com676e6602012-07-10 17:21:48 +0000506 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000507}
508
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000509// get a texture to act as a temporary buffer for AA clip boolean operations
510// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000511void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000512 GrAutoScratchTexture* temp) {
513 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000514 // we've already allocated the temp texture
515 return;
516 }
517
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000518 GrTextureDesc desc;
519 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
520 desc.fWidth = bounds.width();
521 desc.fHeight = bounds.height();
522 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000523
robertphillips@google.com2c756812012-05-22 20:28:23 +0000524 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000525}
526
robertphillips@google.comf105b102012-05-14 12:18:26 +0000527
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000528void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000529 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000530 // Since we are setting up the cache we know the last lookup was a miss
531 // Free up the currently cached mask so it can be reused
532 fAACache.reset();
533
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000534 GrTextureDesc desc;
535 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
536 desc.fWidth = bounds.width();
537 desc.fHeight = bounds.height();
538 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000539
540 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000541}
542
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000543////////////////////////////////////////////////////////////////////////////////
544// Shared preamble between gpu and SW-only AA clip mask creation paths.
545// Handles caching, determination of clip mask bound & allocation (if needed)
546// of the result texture
547// 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 +0000548bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000549 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000550 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000551 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000552 GrAssert(origDrawState->isClipState());
553
554 GrRenderTarget* rt = origDrawState->getRenderTarget();
555 GrAssert(NULL != rt);
556
robertphillips@google.comf294b772012-04-27 14:29:26 +0000557 // unlike the stencil path the alpha path is not bound to the size of the
558 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000559 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000560 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561
562 // need to outset a pixel since the standard bounding box computation
563 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000564 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000565
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000566 // TODO: make sure we don't outset if bounds are still 0,0 @ min
567
robertphillips@google.comba998f22012-10-12 11:33:56 +0000568 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000569 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000570 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000571 return true;
572 }
573
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000574 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000575 return false;
576}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000577
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000578////////////////////////////////////////////////////////////////////////////////
579// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000580bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000581 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000582 GrIRect *devResultBounds) {
583 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000584 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
585
robertphillips@google.com7b112892012-07-31 15:18:21 +0000586 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000587 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000588 return true;
589 }
590
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000591 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
592
robertphillips@google.comf105b102012-05-14 12:18:26 +0000593 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000594 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000595 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000596 return false;
597 }
598
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000599 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
600 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000601
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000602 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000603
robertphillips@google.com7b112892012-07-31 15:18:21 +0000604 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000605 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000606 // if we were able to trim down the size of the mask we need to
robertphillips@google.comf294b772012-04-27 14:29:26 +0000607 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000608 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000609 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000610 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000611 }
612
613 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000614 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
615
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000616 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000617 SkClipStack::Iter::kBottom_IterStart);
618 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000619 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000620 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000621 &firstOp,
622 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000623
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000624 fGpu->clear(NULL,
625 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000626 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000627
robertphillips@google.comf105b102012-05-14 12:18:26 +0000628 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000629 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000630 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000631 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000633 SkRegion::Op op = clip->fOp;
634 if (first) {
635 first = false;
636 op = firstOp;
637 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000638
639 if (SkRegion::kReplace_Op == op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000641 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000642
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000643 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000644 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000645
646 } else if (SkRegion::kReverseDifference_Op == op ||
647 SkRegion::kIntersect_Op == op) {
648 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000649 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000650 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000651 continue;
652 }
653
robertphillips@google.com7b112892012-07-31 15:18:21 +0000654 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000655 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000656 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000657 return false;
658 }
659
robertphillips@google.comf294b772012-04-27 14:29:26 +0000660 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000661 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000663 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000664 this->drawClipShape(temp.texture(), clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000665
666 // TODO: rather than adding these two translations here
667 // compute the bounding box needed to render the texture
668 // into temp
robertphillips@google.com7b112892012-07-31 15:18:21 +0000669 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000670 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
671 // In order for the merge of the temp clip into the accumulator
672 // to work we need to disable the translation
673 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000674 }
675
676 // Now draw into the accumulator using the real operation
677 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000678 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000679 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000680
robertphillips@google.com7b112892012-07-31 15:18:21 +0000681 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000682 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
683 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000684 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000685 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000686 }
687
688 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000689 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000690 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000691 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000692 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000693 }
694 }
695
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000696 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000697 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000698 return true;
699}
700
701////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000702// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000703// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000704bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000705 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000707 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000709 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710 GrAssert(drawState->isClipState());
711
712 GrRenderTarget* rt = drawState->getRenderTarget();
713 GrAssert(NULL != rt);
714
715 // TODO: dynamically attach a SB when needed.
716 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
717 if (NULL == stencilBuffer) {
718 return false;
719 }
720
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000721 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000723 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724
725 // we set the current clip to the bounds so that our recursive
726 // draws are scissored to them. We use the copy of the complex clip
727 // we just stashed on the SB to render from. We set it back after
728 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000729 const GrClipData* oldClipData = fGpu->getClip();
730
robertphillips@google.com7b112892012-07-31 15:18:21 +0000731 // The origin of 'newClipData' is (0, 0) so it is okay to place
732 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000733 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000734 GrClipData newClipData;
735 newClipData.fClipStack = &newClipStack;
736
737 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000739 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
740 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000742 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000744 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000745 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000746 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000747 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000748 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000749 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000750 }
751
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752#if !VISUALIZE_COMPLEX_CLIP
753 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
754#endif
755
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000756 int clipBit = stencilBuffer->bits();
757 SkASSERT((clipBit <= 16) &&
758 "Ganesh only handles 16b or smaller stencil buffers");
759 clipBit = (1 << (clipBit-1));
760
robertphillips@google.com7b112892012-07-31 15:18:21 +0000761 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000762
763 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000764 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
765
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000766 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000767 SkClipStack::Iter::kBottom_IterStart);
768 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000769 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000770 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000771 &firstOp,
772 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000773
robertphillips@google.com7b112892012-07-31 15:18:21 +0000774 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000775 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000776
777 // walk through each clip element and perform its set op
778 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000779 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000781 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782 // enabled at bottom of loop
783 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000784 // if the target is MSAA then we want MSAA enabled when the clip is soft
785 if (rt->isMultisampled()) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000786 drawState->setState(GrDrawState::kHWAntialias_StateBit, clip->fDoAA);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000787 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000788
tomhudson@google.com8afae612012-08-14 15:03:35 +0000789 // Can the clip element be drawn directly to the stencil buffer
790 // with a non-inverted fill rule without extra passes to
791 // resolve in/out status?
792 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000794 SkRegion::Op op = clip->fOp;
795 if (first) {
796 first = false;
797 op = firstOp;
798 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000799
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000801 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000802 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000803 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000804 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000805 fillInverted = false;
806 // there is no point in intersecting a screen filling
807 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000808 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000809 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000810 continue;
811 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000812 } else {
813 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000814 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000815 fillInverted = GrIsFillInverted(fill);
816 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000817 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000818 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000820 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000821 return false;
822 }
823 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000824 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 }
826
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000827 int passes;
828 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
829
830 bool canDrawDirectToClip; // Given the renderer, the element,
831 // fill rule, and set operation can
832 // we render the element directly to
833 // stencil bit used for clipping.
834 canDrawDirectToClip =
835 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000836 canRenderDirectToStencil,
837 clipBit,
838 fillInverted,
839 &passes,
840 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000841
842 // draw the element to the client stencil bits if necessary
843 if (!canDrawDirectToClip) {
844 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
845 kIncClamp_StencilOp,
846 kIncClamp_StencilOp,
847 kAlways_StencilFunc,
848 0xffff,
849 0x0000,
850 0xffff);
851 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000852 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000853 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000854 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000855 } else {
856 if (canRenderDirectToStencil) {
857 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000858 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000859 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000860 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000861 }
862 }
863 }
864
865 // now we modify the clip bit by rendering either the clip
866 // element directly or a bounding rect of the entire clip.
867 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
868 for (int p = 0; p < passes; ++p) {
869 *drawState->stencil() = stencilSettings[p];
870 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000871 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000872 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000873 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000874 } else {
875 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000876 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000877 }
878 } else {
879 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000880 // 'devClipBounds' is already in device coordinates so the
881 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000882 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000883 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000884 GrRect canvClipBounds;
885 canvClipBounds.set(devClipBounds);
886 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
887 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000888 }
889 }
890 }
891 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000892 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000893 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000894 // set this last because recursive draws may overwrite it back to kNone.
895 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
896 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000897 return true;
898}
899
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000900
bsalomon@google.com411dad02012-06-05 20:24:20 +0000901// mapping of clip-respecting stencil funcs to normal stencil funcs
902// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000903static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000904 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
905 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
906 // In the Clip Funcs
907 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
908 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
909 kLess_StencilFunc, // kLessIfInClip_StencilFunc
910 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
911 // Special in the clip func that forces user's ref to be 0.
912 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
913 // make ref 0 and do normal nequal.
914 },
915 {// Stencil-Clipping is ENABLED
916 // In the Clip Funcs
917 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
918 // eq stencil clip bit, mask
919 // out user bits.
920
921 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
922 // add stencil bit to mask and ref
923
924 kLess_StencilFunc, // kLessIfInClip_StencilFunc
925 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
926 // for both of these we can add
927 // the clip bit to the mask and
928 // ref and compare as normal
929 // Special in the clip func that forces user's ref to be 0.
930 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
931 // make ref have only the clip bit set
932 // and make comparison be less
933 // 10..0 < 1..user_bits..
934 }
935};
936
bsalomon@google.coma3201942012-06-21 19:58:20 +0000937namespace {
938// Sets the settings to clip against the stencil buffer clip while ignoring the
939// client bits.
940const GrStencilSettings& basic_apply_stencil_clip_settings() {
941 // stencil settings to use when clip is in stencil
942 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
943 kKeep_StencilOp,
944 kKeep_StencilOp,
945 kAlwaysIfInClip_StencilFunc,
946 0x0000,
947 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000948 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000949 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
950}
951}
952
953void GrClipMaskManager::setGpuStencil() {
954 // We make two copies of the StencilSettings here (except in the early
955 // exit scenario. One copy from draw state to the stack var. Then another
956 // from the stack var to the gpu. We could make this class hold a ptr to
957 // GrGpu's fStencilSettings and eliminate the stack copy here.
958
959 const GrDrawState& drawState = fGpu->getDrawState();
960
961 // use stencil for clipping if clipping is enabled and the clip
962 // has been written into the stencil.
963 GrClipMaskManager::StencilClipMode clipMode;
964 if (this->isClipInStencil() && drawState.isClipState()) {
965 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
966 // We can't be modifying the clip and respecting it at the same time.
967 GrAssert(!drawState.isStateFlagEnabled(
968 GrGpu::kModifyStencilClip_StateBit));
969 } else if (drawState.isStateFlagEnabled(
970 GrGpu::kModifyStencilClip_StateBit)) {
971 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
972 } else {
973 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
974 }
975
976 GrStencilSettings settings;
977 // The GrGpu client may not be using the stencil buffer but we may need to
978 // enable it in order to respect a stencil clip.
979 if (drawState.getStencil().isDisabled()) {
980 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
981 settings = basic_apply_stencil_clip_settings();
982 } else {
983 fGpu->disableStencil();
984 return;
985 }
986 } else {
987 settings = drawState.getStencil();
988 }
989
990 // TODO: dynamically attach a stencil buffer
991 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000992 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000993 drawState.getRenderTarget()->getStencilBuffer();
994 if (NULL != stencilBuffer) {
995 stencilBits = stencilBuffer->bits();
996 }
997
bsalomon@google.comf6601872012-08-28 21:11:35 +0000998 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000999 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001000 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001001 this->adjustStencilParams(&settings, clipMode, stencilBits);
1002 fGpu->setStencilSettings(settings);
1003}
1004
1005void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1006 StencilClipMode mode,
1007 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001008 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001009
1010 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001011 // We assume that this clip manager itself is drawing to the GrGpu and
1012 // has already setup the correct values.
1013 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001014 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001015
bsalomon@google.com411dad02012-06-05 20:24:20 +00001016 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1017 unsigned int userBits = clipBit - 1;
1018
bsalomon@google.coma3201942012-06-21 19:58:20 +00001019 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001020 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001021
bsalomon@google.coma3201942012-06-21 19:58:20 +00001022 bool finished = false;
1023 while (!finished) {
1024 GrStencilFunc func = settings->func(face);
1025 uint16_t writeMask = settings->writeMask(face);
1026 uint16_t funcMask = settings->funcMask(face);
1027 uint16_t funcRef = settings->funcRef(face);
1028
1029 GrAssert((unsigned) func < kStencilFuncCount);
1030
1031 writeMask &= userBits;
1032
1033 if (func >= kBasicStencilFuncCount) {
1034 int respectClip = kRespectClip_StencilClipMode == mode;
1035 if (respectClip) {
1036 // The GrGpu class should have checked this
1037 GrAssert(this->isClipInStencil());
1038 switch (func) {
1039 case kAlwaysIfInClip_StencilFunc:
1040 funcMask = clipBit;
1041 funcRef = clipBit;
1042 break;
1043 case kEqualIfInClip_StencilFunc:
1044 case kLessIfInClip_StencilFunc:
1045 case kLEqualIfInClip_StencilFunc:
1046 funcMask = (funcMask & userBits) | clipBit;
1047 funcRef = (funcRef & userBits) | clipBit;
1048 break;
1049 case kNonZeroIfInClip_StencilFunc:
1050 funcMask = (funcMask & userBits) | clipBit;
1051 funcRef = clipBit;
1052 break;
1053 default:
1054 GrCrash("Unknown stencil func");
1055 }
1056 } else {
1057 funcMask &= userBits;
1058 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001059 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001060 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001061 gSpecialToBasicStencilFunc[respectClip];
1062 func = table[func - kBasicStencilFuncCount];
1063 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001064 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001065 funcMask &= userBits;
1066 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001067 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001068
1069 settings->setFunc(face, func);
1070 settings->setWriteMask(face, writeMask);
1071 settings->setFuncMask(face, funcMask);
1072 settings->setFuncRef(face, funcRef);
1073
1074 if (GrStencilSettings::kFront_Face == face) {
1075 face = GrStencilSettings::kBack_Face;
1076 finished = !twoSided;
1077 } else {
1078 finished = true;
1079 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001080 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001081 if (!twoSided) {
1082 settings->copyFrontSettingsToBack();
1083 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001084}
1085
1086////////////////////////////////////////////////////////////////////////////////
1087
robertphillips@google.comfa662942012-05-17 12:20:22 +00001088namespace {
1089
1090GrPathFill invert_fill(GrPathFill fill) {
1091 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001092 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1093 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1094 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1095 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1096 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001097 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001098 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1099 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1100 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1101 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1102 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1103 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001104 return gInvertedFillTable[fill];
1105}
1106
1107}
1108
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001109bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001110 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001111 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001112 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001113
robertphillips@google.com7b112892012-07-31 15:18:21 +00001114 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001115 return true;
1116 }
1117
robertphillips@google.comf105b102012-05-14 12:18:26 +00001118 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001119 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001120 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001121 return false;
1122 }
1123
robertphillips@google.com2c756812012-05-22 20:28:23 +00001124 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001125
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001126 GrMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001127 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001128 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001129 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001130
robertphillips@google.comfa662942012-05-17 12:20:22 +00001131 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001132 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1133
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001134 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001135 SkClipStack::Iter::kBottom_IterStart);
1136 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001137 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001138 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001139 &firstOp,
1140 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001141
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001142 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001143
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001144 bool first = true;
1145 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001146
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001147 SkRegion::Op op = clip->fOp;
1148 if (first) {
1149 first = false;
1150 op = firstOp;
1151 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001152
1153 if (SkRegion::kIntersect_Op == op ||
1154 SkRegion::kReverseDifference_Op == op) {
1155 // Intersect and reverse difference require modifying pixels
1156 // outside of the geometry that is being "drawn". In both cases
1157 // we erase all the pixels outside of the geometry but
1158 // leave the pixels inside the geometry alone. For reverse
1159 // difference we invert all the pixels before clearing the ones
1160 // outside the geometry.
1161 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001162 SkRect temp;
1163 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001164 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1165 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001166
1167 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001168 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001169 }
1170
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001171 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001172
1173 // convert the rect to a path so we can invert the fill
1174 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001175 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001176
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001177 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001178 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001179 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001180 } else if (NULL != clip->fPath) {
1181 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001182 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001183 invert_fill(get_path_fill(*clip->fPath)),
1184 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001185 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001186 }
1187
1188 continue;
1189 }
1190
1191 // The other ops (union, xor, diff) only affect pixels inside
1192 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001193 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001194
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001195 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001196 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001197 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001198
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001199 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001200 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001201 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001202 get_path_fill(*clip->fPath),
1203 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001204 }
1205 }
1206
robertphillips@google.comfa662942012-05-17 12:20:22 +00001207 // Because we are using the scratch texture cache, "accum" may be
1208 // larger than expected and have some cruft in the areas we aren't using.
1209 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001210 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001211
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001212 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001213
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001214 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001215
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001216 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001217 return true;
1218}
1219
robertphillips@google.comf294b772012-04-27 14:29:26 +00001220////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001221void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001222 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001223}