blob: 5e1bd362b7109a4fc77cfbff19bbbe5e73a44d63 [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
23//#define GR_AA_CLIP 1
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000024//#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.come3d32162012-07-20 13:37:06 +0000444 pr->drawPath(path, fill, NULL, 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
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000568 if (fAACache.canReuse(*clipDataIn.fClipStack,
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000569 devResultBounds->width(),
570 devResultBounds->height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000571 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000572 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000573 return true;
574 }
575
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000576 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000577 return false;
578}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000579
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000580////////////////////////////////////////////////////////////////////////////////
581// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000582bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000583 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000584 GrIRect *devResultBounds) {
585 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000586 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
587
robertphillips@google.com7b112892012-07-31 15:18:21 +0000588 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000589 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000590 return true;
591 }
592
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000593 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
594
robertphillips@google.comf105b102012-05-14 12:18:26 +0000595 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000596 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000597 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000598 return false;
599 }
600
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000601 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
602 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000603
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000604 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605
robertphillips@google.com7b112892012-07-31 15:18:21 +0000606 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000607 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000608 // if we were able to trim down the size of the mask we need to
robertphillips@google.comf294b772012-04-27 14:29:26 +0000609 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000610 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000611 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000612 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613 }
614
615 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000616 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
617
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000618 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000619 SkClipStack::Iter::kBottom_IterStart);
620 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000621 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000622 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000623 &firstOp,
624 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000625
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000626 fGpu->clear(NULL,
627 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000628 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000629
robertphillips@google.comf105b102012-05-14 12:18:26 +0000630 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000631 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000632 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000633 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000634
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000635 SkRegion::Op op = clip->fOp;
636 if (first) {
637 first = false;
638 op = firstOp;
639 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640
641 if (SkRegion::kReplace_Op == op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000642 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000643 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000644
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000645 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000646 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647
648 } else if (SkRegion::kReverseDifference_Op == op ||
649 SkRegion::kIntersect_Op == op) {
650 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000651 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000652 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000653 continue;
654 }
655
robertphillips@google.com7b112892012-07-31 15:18:21 +0000656 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000657 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000658 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000659 return false;
660 }
661
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000663 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000664
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000665 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000666 this->drawClipShape(temp.texture(), clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667
668 // TODO: rather than adding these two translations here
669 // compute the bounding box needed to render the texture
670 // into temp
robertphillips@google.com7b112892012-07-31 15:18:21 +0000671 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000672 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
673 // In order for the merge of the temp clip into the accumulator
674 // to work we need to disable the translation
675 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000676 }
677
678 // Now draw into the accumulator using the real operation
679 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000680 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000681 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000682
robertphillips@google.com7b112892012-07-31 15:18:21 +0000683 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000684 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
685 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000686 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000687 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000688 }
689
690 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000691 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000692 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000693 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000694 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000695 }
696 }
697
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000698 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000699 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000700 return true;
701}
702
703////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000704// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000705// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000706bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000707 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000708
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000709 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000711 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 GrAssert(drawState->isClipState());
713
714 GrRenderTarget* rt = drawState->getRenderTarget();
715 GrAssert(NULL != rt);
716
717 // TODO: dynamically attach a SB when needed.
718 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
719 if (NULL == stencilBuffer) {
720 return false;
721 }
722
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000723 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000725 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726
727 // we set the current clip to the bounds so that our recursive
728 // draws are scissored to them. We use the copy of the complex clip
729 // we just stashed on the SB to render from. We set it back after
730 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000731 const GrClipData* oldClipData = fGpu->getClip();
732
robertphillips@google.com7b112892012-07-31 15:18:21 +0000733 // The origin of 'newClipData' is (0, 0) so it is okay to place
734 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000735 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000736 GrClipData newClipData;
737 newClipData.fClipStack = &newClipStack;
738
739 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000741 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
742 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000744 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000746 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000747 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000748 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000749 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000750 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000751 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000752 }
753
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754#if !VISUALIZE_COMPLEX_CLIP
755 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
756#endif
757
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 int clipBit = stencilBuffer->bits();
759 SkASSERT((clipBit <= 16) &&
760 "Ganesh only handles 16b or smaller stencil buffers");
761 clipBit = (1 << (clipBit-1));
762
robertphillips@google.com7b112892012-07-31 15:18:21 +0000763 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000764
765 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000766 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
767
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000768 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000769 SkClipStack::Iter::kBottom_IterStart);
770 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000771 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000772 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000773 &firstOp,
774 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775
robertphillips@google.com7b112892012-07-31 15:18:21 +0000776 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000777 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000778
779 // walk through each clip element and perform its set op
780 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000781 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000783 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000784 // enabled at bottom of loop
785 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000786 // if the target is MSAA then we want MSAA enabled when the clip is soft
787 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000788 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000789 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
790 } else {
791 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
792 }
793 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000794
tomhudson@google.com8afae612012-08-14 15:03:35 +0000795 // Can the clip element be drawn directly to the stencil buffer
796 // with a non-inverted fill rule without extra passes to
797 // resolve in/out status?
798 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000800 SkRegion::Op op = clip->fOp;
801 if (first) {
802 first = false;
803 op = firstOp;
804 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000805
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000806 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000807 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000808 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000810 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 fillInverted = false;
812 // there is no point in intersecting a screen filling
813 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000814 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000815 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000816 continue;
817 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000818 } else {
819 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000820 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000821 fillInverted = GrIsFillInverted(fill);
822 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000823 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000824 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000826 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000827 return false;
828 }
829 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000830 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831 }
832
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000833 int passes;
834 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
835
836 bool canDrawDirectToClip; // Given the renderer, the element,
837 // fill rule, and set operation can
838 // we render the element directly to
839 // stencil bit used for clipping.
840 canDrawDirectToClip =
841 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000842 canRenderDirectToStencil,
843 clipBit,
844 fillInverted,
845 &passes,
846 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000847
848 // draw the element to the client stencil bits if necessary
849 if (!canDrawDirectToClip) {
850 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
851 kIncClamp_StencilOp,
852 kIncClamp_StencilOp,
853 kAlways_StencilFunc,
854 0xffff,
855 0x0000,
856 0xffff);
857 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000858 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000859 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000860 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000861 } else {
862 if (canRenderDirectToStencil) {
863 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000864 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000865 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000866 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000867 }
868 }
869 }
870
871 // now we modify the clip bit by rendering either the clip
872 // element directly or a bounding rect of the entire clip.
873 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
874 for (int p = 0; p < passes; ++p) {
875 *drawState->stencil() = stencilSettings[p];
876 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000877 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000878 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000879 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000880 } else {
881 SET_RANDOM_COLOR
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 }
884 } else {
885 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000886 // 'devClipBounds' is already in device coordinates so the
887 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000888 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000889 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000890 GrRect canvClipBounds;
891 canvClipBounds.set(devClipBounds);
892 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
893 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000894 }
895 }
896 }
897 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000898 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000899 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000900 // set this last because recursive draws may overwrite it back to kNone.
901 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
902 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000903 return true;
904}
905
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000906
bsalomon@google.com411dad02012-06-05 20:24:20 +0000907// mapping of clip-respecting stencil funcs to normal stencil funcs
908// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000909static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000910 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
911 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
912 // In the Clip Funcs
913 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
914 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
915 kLess_StencilFunc, // kLessIfInClip_StencilFunc
916 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
917 // Special in the clip func that forces user's ref to be 0.
918 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
919 // make ref 0 and do normal nequal.
920 },
921 {// Stencil-Clipping is ENABLED
922 // In the Clip Funcs
923 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
924 // eq stencil clip bit, mask
925 // out user bits.
926
927 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
928 // add stencil bit to mask and ref
929
930 kLess_StencilFunc, // kLessIfInClip_StencilFunc
931 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
932 // for both of these we can add
933 // the clip bit to the mask and
934 // ref and compare as normal
935 // Special in the clip func that forces user's ref to be 0.
936 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
937 // make ref have only the clip bit set
938 // and make comparison be less
939 // 10..0 < 1..user_bits..
940 }
941};
942
bsalomon@google.coma3201942012-06-21 19:58:20 +0000943namespace {
944// Sets the settings to clip against the stencil buffer clip while ignoring the
945// client bits.
946const GrStencilSettings& basic_apply_stencil_clip_settings() {
947 // stencil settings to use when clip is in stencil
948 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
949 kKeep_StencilOp,
950 kKeep_StencilOp,
951 kAlwaysIfInClip_StencilFunc,
952 0x0000,
953 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000954 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000955 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
956}
957}
958
959void GrClipMaskManager::setGpuStencil() {
960 // We make two copies of the StencilSettings here (except in the early
961 // exit scenario. One copy from draw state to the stack var. Then another
962 // from the stack var to the gpu. We could make this class hold a ptr to
963 // GrGpu's fStencilSettings and eliminate the stack copy here.
964
965 const GrDrawState& drawState = fGpu->getDrawState();
966
967 // use stencil for clipping if clipping is enabled and the clip
968 // has been written into the stencil.
969 GrClipMaskManager::StencilClipMode clipMode;
970 if (this->isClipInStencil() && drawState.isClipState()) {
971 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
972 // We can't be modifying the clip and respecting it at the same time.
973 GrAssert(!drawState.isStateFlagEnabled(
974 GrGpu::kModifyStencilClip_StateBit));
975 } else if (drawState.isStateFlagEnabled(
976 GrGpu::kModifyStencilClip_StateBit)) {
977 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
978 } else {
979 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
980 }
981
982 GrStencilSettings settings;
983 // The GrGpu client may not be using the stencil buffer but we may need to
984 // enable it in order to respect a stencil clip.
985 if (drawState.getStencil().isDisabled()) {
986 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
987 settings = basic_apply_stencil_clip_settings();
988 } else {
989 fGpu->disableStencil();
990 return;
991 }
992 } else {
993 settings = drawState.getStencil();
994 }
995
996 // TODO: dynamically attach a stencil buffer
997 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000998 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000999 drawState.getRenderTarget()->getStencilBuffer();
1000 if (NULL != stencilBuffer) {
1001 stencilBits = stencilBuffer->bits();
1002 }
1003
bsalomon@google.comf6601872012-08-28 21:11:35 +00001004 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001005 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001006 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001007 this->adjustStencilParams(&settings, clipMode, stencilBits);
1008 fGpu->setStencilSettings(settings);
1009}
1010
1011void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1012 StencilClipMode mode,
1013 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001014 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001015
1016 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001017 // We assume that this clip manager itself is drawing to the GrGpu and
1018 // has already setup the correct values.
1019 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001020 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001021
bsalomon@google.com411dad02012-06-05 20:24:20 +00001022 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1023 unsigned int userBits = clipBit - 1;
1024
bsalomon@google.coma3201942012-06-21 19:58:20 +00001025 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001026 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001027
bsalomon@google.coma3201942012-06-21 19:58:20 +00001028 bool finished = false;
1029 while (!finished) {
1030 GrStencilFunc func = settings->func(face);
1031 uint16_t writeMask = settings->writeMask(face);
1032 uint16_t funcMask = settings->funcMask(face);
1033 uint16_t funcRef = settings->funcRef(face);
1034
1035 GrAssert((unsigned) func < kStencilFuncCount);
1036
1037 writeMask &= userBits;
1038
1039 if (func >= kBasicStencilFuncCount) {
1040 int respectClip = kRespectClip_StencilClipMode == mode;
1041 if (respectClip) {
1042 // The GrGpu class should have checked this
1043 GrAssert(this->isClipInStencil());
1044 switch (func) {
1045 case kAlwaysIfInClip_StencilFunc:
1046 funcMask = clipBit;
1047 funcRef = clipBit;
1048 break;
1049 case kEqualIfInClip_StencilFunc:
1050 case kLessIfInClip_StencilFunc:
1051 case kLEqualIfInClip_StencilFunc:
1052 funcMask = (funcMask & userBits) | clipBit;
1053 funcRef = (funcRef & userBits) | clipBit;
1054 break;
1055 case kNonZeroIfInClip_StencilFunc:
1056 funcMask = (funcMask & userBits) | clipBit;
1057 funcRef = clipBit;
1058 break;
1059 default:
1060 GrCrash("Unknown stencil func");
1061 }
1062 } else {
1063 funcMask &= userBits;
1064 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001065 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001066 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001067 gSpecialToBasicStencilFunc[respectClip];
1068 func = table[func - kBasicStencilFuncCount];
1069 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001070 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001071 funcMask &= userBits;
1072 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001073 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001074
1075 settings->setFunc(face, func);
1076 settings->setWriteMask(face, writeMask);
1077 settings->setFuncMask(face, funcMask);
1078 settings->setFuncRef(face, funcRef);
1079
1080 if (GrStencilSettings::kFront_Face == face) {
1081 face = GrStencilSettings::kBack_Face;
1082 finished = !twoSided;
1083 } else {
1084 finished = true;
1085 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001086 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001087 if (!twoSided) {
1088 settings->copyFrontSettingsToBack();
1089 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001090}
1091
1092////////////////////////////////////////////////////////////////////////////////
1093
robertphillips@google.comfa662942012-05-17 12:20:22 +00001094namespace {
1095
1096GrPathFill invert_fill(GrPathFill fill) {
1097 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001098 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1099 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1100 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1101 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1102 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001103 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001104 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1105 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1106 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1107 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1108 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1109 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001110 return gInvertedFillTable[fill];
1111}
1112
1113}
1114
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001115bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001116 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001117 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001118 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001119
robertphillips@google.com7b112892012-07-31 15:18:21 +00001120 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001121 return true;
1122 }
1123
robertphillips@google.comf105b102012-05-14 12:18:26 +00001124 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001125 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001126 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001127 return false;
1128 }
1129
robertphillips@google.com2c756812012-05-22 20:28:23 +00001130 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001131
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001132 GrMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001133 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001134 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001135 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001136
robertphillips@google.comfa662942012-05-17 12:20:22 +00001137 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001138 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1139
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001140 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001141 SkClipStack::Iter::kBottom_IterStart);
1142 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001143 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001144 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001145 &firstOp,
1146 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001147
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001148 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001149
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001150 bool first = true;
1151 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001152
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001153 SkRegion::Op op = clip->fOp;
1154 if (first) {
1155 first = false;
1156 op = firstOp;
1157 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001158
1159 if (SkRegion::kIntersect_Op == op ||
1160 SkRegion::kReverseDifference_Op == op) {
1161 // Intersect and reverse difference require modifying pixels
1162 // outside of the geometry that is being "drawn". In both cases
1163 // we erase all the pixels outside of the geometry but
1164 // leave the pixels inside the geometry alone. For reverse
1165 // difference we invert all the pixels before clearing the ones
1166 // outside the geometry.
1167 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001168 SkRect temp;
1169 temp.set(*devResultBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001170
1171 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001172 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001173 }
1174
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001175 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001176
1177 // convert the rect to a path so we can invert the fill
1178 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001179 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001180
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001181 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001182 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001183 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001184 } else if (NULL != clip->fPath) {
1185 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001186 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001187 invert_fill(get_path_fill(*clip->fPath)),
1188 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001189 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001190 }
1191
1192 continue;
1193 }
1194
1195 // The other ops (union, xor, diff) only affect pixels inside
1196 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001197 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001198
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001199 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001200 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001201 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001202
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001203 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001204 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001205 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001206 get_path_fill(*clip->fPath),
1207 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001208 }
1209 }
1210
robertphillips@google.comfa662942012-05-17 12:20:22 +00001211 // Because we are using the scratch texture cache, "accum" may be
1212 // larger than expected and have some cruft in the areas we aren't using.
1213 // Clear it out.
1214
1215 // TODO: need a simpler way to clear the texture - can we combine
1216 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001217 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001218 GrAssert(NULL != drawState);
1219 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001220 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001221 // can't leave the accum bound as a rendertarget
1222 drawState->setRenderTarget(temp);
1223
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001224 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001225
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001226 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001227
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001228 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001229 return true;
1230}
1231
robertphillips@google.comf294b772012-04-27 14:29:26 +00001232////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001233void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001234 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001235}