blob: ff78bf9d8cc46688e3af2a2d14b41e73cb4a6969 [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"
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000010#include "effects/GrTextureDomainEffect.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000011#include "GrGpu.h"
12#include "GrRenderTarget.h"
13#include "GrStencilBuffer.h"
14#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000015#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000016#include "SkRasterClip.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000017#include "GrAAConvexPathRenderer.h"
18#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000020#include "GrCacheID.h"
21
22GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000023
robertphillips@google.comba998f22012-10-12 11:33:56 +000024#define GR_AA_CLIP 1
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +000025#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000026
bsalomon@google.com8182fa02012-12-04 14:06:06 +000027typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000028
29////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000030namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000032// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033void setup_drawstate_aaclip(GrGpu* gpu,
34 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000035 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000036 GrDrawState* drawState = gpu->drawState();
37 GrAssert(drawState);
38
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000039 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040
bsalomon@google.comb9086a02012-11-01 18:02:54 +000041 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000044 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045 mat.preConcat(drawState->getViewMatrix());
46
bsalomon@google.com08283af2012-10-26 13:01:20 +000047 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000048
49 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
50 drawState->stage(kMaskStage)->setEffect(
51 GrTextureDomainEffect::Create(result,
52 mat,
53 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
54 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000055}
56
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000057bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000058 GrGpu* gpu,
59 const SkPath& path,
60 GrPathFill fill,
61 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000062 // last (false) parameter disallows use of the SW path renderer
63 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
64}
65
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000066GrPathFill get_path_fill(const SkPath& path) {
67 switch (path.getFillType()) {
68 case SkPath::kWinding_FillType:
69 return kWinding_GrPathFill;
70 case SkPath::kEvenOdd_FillType:
71 return kEvenOdd_GrPathFill;
72 case SkPath::kInverseWinding_FillType:
73 return kInverseWinding_GrPathFill;
74 case SkPath::kInverseEvenOdd_FillType:
75 return kInverseEvenOdd_GrPathFill;
76 default:
77 GrCrash("Unsupported path fill in clip.");
78 return kWinding_GrPathFill; // suppress warning
79 }
80}
81
robertphillips@google.comb99225c2012-07-24 18:20:10 +000082/**
83 * Does any individual clip in 'clipIn' use anti-aliasing?
84 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000085bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000086
robertphillips@google.com641f8b12012-07-31 19:15:58 +000087 SkClipStack::Iter iter;
88 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000089
bsalomon@google.com8182fa02012-12-04 14:06:06 +000090 const Element* element = NULL;
91 for (element = iter.skipToTopmost(SkRegion::kReplace_Op);
92 NULL != element;
93 element = iter.next()) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000094
bsalomon@google.com8182fa02012-12-04 14:06:06 +000095 if (element->isAA()) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000096 return true;
97 }
98 }
99
100 return false;
101}
102
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000103}
104
robertphillips@google.comfa662942012-05-17 12:20:22 +0000105/*
106 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
107 * will be used on any element. If so, it returns true to indicate that the
108 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
109 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000110bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000111
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000112 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000113 // a clip gets complex enough it can just be done in SW regardless
114 // of whether it would invoke the GrSoftwarePathRenderer.
115 bool useSW = false;
116
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000117 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000118 const Element* element = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000119
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000120 for (element = iter.skipToTopmost(SkRegion::kReplace_Op);
121 NULL != element;
122 element = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000123
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000124 // rects can always be drawn directly w/o using the software path
125 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000126 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000127 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000128 element->getPath(),
129 get_path_fill(element->getPath()),
130 element->isAA())) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000131 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000132 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000133 }
134
135 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000136}
137
robertphillips@google.comf294b772012-04-27 14:29:26 +0000138////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000139// sort out what kind of clip mask needs to be created: alpha, stencil,
140// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000141bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000142 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000143
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000144 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000145 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000146 fGpu->disableScissor();
147 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000148 return true;
149 }
150
151 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000152 // GrDrawTarget should have filtered this for us
153 GrAssert(NULL != rt);
154
robertphillips@google.com7b112892012-07-31 15:18:21 +0000155 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000156 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000157
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000158 clipDataIn->getConservativeBounds(rt, &devClipBounds, &isIntersectionOfRects);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000159 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000160 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000161 }
162
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000163#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000164 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000165
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000166 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000167 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000168 // in software (this will only happen if the clip mask is anti-aliased
169 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000170 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000171 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000172 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000173 // The clip geometry is complex enough that it will be more
174 // efficient to create it entirely in software
175 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000176 GrIRect devBound;
177 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
178 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000179 fGpu->disableScissor();
180 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000181 return true;
182 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000183
184 // if SW clip mask creation fails fall through to the other
185 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000186 }
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000187#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000188
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000189#if GR_AA_CLIP
robertphillips@google.comf294b772012-04-27 14:29:26 +0000190 // If MSAA is enabled use the (faster) stencil path for AA clipping
191 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000192 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000193 // Since we are going to create a destination texture of the correct
194 // size for the mask (rather than being bound by the size of the
195 // render target) we aren't going to use scissoring like the stencil
196 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000197 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000198 GrIRect devBound;
199 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
200 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000201 fGpu->disableScissor();
202 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000203 return true;
204 }
205
206 // if alpha clip mask creation fails fall through to the stencil
207 // buffer method
208 }
209#endif // GR_AA_CLIP
210
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000211 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000212 // an antialiased clip couldn't be created. In either case, free up
213 // the texture in the antialiased mask cache.
214 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000215 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000216 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000217 // AA cache.
218 fAACache.reset();
219
bsalomon@google.coma3201942012-06-21 19:58:20 +0000220 // If the clip is a rectangle then just set the scissor. Otherwise, create
221 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000222 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000223 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000224 this->setGpuStencil();
225 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000226 }
227
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000228 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000229 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000230 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000231
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000232 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000233 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000234 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000235 // This must occur after createStencilClipMask. That function may change
236 // the scissor. Also, it only guarantees that the stencil mask is correct
237 // within the bounds it was passed, so we must use both stencil and scissor
238 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000239 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000240 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000241 return true;
242}
243
244#define VISUALIZE_COMPLEX_CLIP 0
245
246#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000247 #include "SkRandom.h"
248 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000249 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
250#else
251 #define SET_RANDOM_COLOR
252#endif
253
254namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000255/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000256 * Does "canvContainer" contain "devContainee"? If either is empty then
257 * no containment is possible. "canvContainer" is in canvas coordinates while
258 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000259 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000260 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000261bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000262 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000263 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000264 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000265 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000266 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000267 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000268 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000269}
270
robertphillips@google.comf294b772012-04-27 14:29:26 +0000271////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000272// determines how many elements at the head of the clip can be skipped and
273// whether the initial clear should be to the inside- or outside-the-clip value,
274// and what op should be used to draw the first element that isn't skipped.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000275const SkClipStack::Element* process_initial_clip_elements(
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000276 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000277 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000278 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000279 SkRegion::Op* firstOp,
280 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000281
282 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000283
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000284 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000285 // processed the clip is entirely open. However, depending on the
286 // first set op we may prefer to clear to 0 for performance. We may
287 // also be able to skip the initial clip paths/rects. We loop until
288 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289 bool done = false;
290 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000292 const SkClipStack::Element* element = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000293
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000294 for (element = iter->skipToTopmost(SkRegion::kReplace_Op);
295 NULL != element && !done;
296 element = iter->next()) {
297 switch (element->getOp()) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000298 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000299 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000300 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000301 *clearToInside = false;
302 done = true;
303 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000304 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000305 // if this element contains the entire bounds then we
306 // can skip it.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000307 if (Element::kRect_Type == element->getType() &&
308 contains(element->getRect(), devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000309 break;
310 }
311 // if everything is initially clearToInside then intersect is
312 // same as clear to 0 and treat as a replace. Otherwise,
313 // set stays empty.
314 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000315 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000316 *clearToInside = false;
317 done = true;
318 }
319 break;
320 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000321 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000322 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000323 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000324 // clearToInside
325 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000326 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000327 done = true;
328 }
329 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000330 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000331 // xor is same as difference or replace both of which
332 // can be 1-pass instead of 2 for xor.
333 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000334 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000336 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000337 }
338 done = true;
339 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000340 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000341 // if all pixels are clearToInside then we have to process the
342 // difference, otherwise it has no effect and all pixels
343 // remain outside.
344 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000345 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000346 done = true;
347 }
348 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000349 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000350 // if all pixels are clearToInside then reverse difference
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000351 // produces empty set. Otherwise it is same as replace
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000352 if (*clearToInside) {
353 *clearToInside = false;
354 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000355 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000356 done = true;
357 }
358 break;
359 default:
360 GrCrash("Unknown set op.");
361 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000362
363 if (done) {
364 // we need to break out here (rather than letting the test in
365 // the loop do it) since backing up the iterator is very expensive
366 break;
367 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000368 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000369 return element;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000370}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000372}
373
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374namespace {
375
376////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000377// set up the OpenGL blend function to perform the specified
378// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000379void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000380
381 switch (op) {
382 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000383 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000384 break;
385 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000386 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387 break;
388 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000389 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000390 break;
391 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000392 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000393 break;
394 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000395 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000396 break;
397 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000398 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000399 break;
400 default:
401 GrAssert(false);
402 break;
403 }
404}
405
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000406////////////////////////////////////////////////////////////////////////////////
407bool draw_path_in_software(GrContext* context,
408 GrGpu* gpu,
409 const SkPath& path,
410 GrPathFill fill,
411 bool doAA,
412 const GrIRect& resultBounds) {
413
414 SkAutoTUnref<GrTexture> texture(
415 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
416 resultBounds, fill,
417 doAA, NULL));
418 if (NULL == texture) {
419 return false;
420 }
421
422 // The ClipMaskManager accumulates the clip mask in the UL corner
423 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
424
425 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
426
427 GrAssert(!GrIsFillInverted(fill));
428 return true;
429}
430
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000431
432////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000433bool draw_path(GrContext* context,
434 GrGpu* gpu,
435 const SkPath& path,
436 GrPathFill fill,
437 bool doAA,
438 const GrIRect& resultBounds) {
439
440 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
441 if (NULL == pr) {
442 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
443 }
444
445 pr->drawPath(path, fill, gpu, doAA);
446 return true;
447}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000448
robertphillips@google.com7b112892012-07-31 15:18:21 +0000449// 'rect' enters in device coordinates and leaves in canvas coordinates
450void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
451 GrAssert(NULL != rect);
452
453 rect->fLeft += SkIntToScalar(origin.fX);
454 rect->fTop += SkIntToScalar(origin.fY);
455 rect->fRight += SkIntToScalar(origin.fX);
456 rect->fBottom += SkIntToScalar(origin.fY);
457}
458
robertphillips@google.com72176b22012-05-23 13:19:12 +0000459}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000460
461////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000462bool GrClipMaskManager::drawClipShape(GrTexture* target,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000463 const SkClipStack::Element* element,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000464 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000465 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000466 GrAssert(NULL != drawState);
467
468 drawState->setRenderTarget(target->asRenderTarget());
469
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000470 switch (element->getType()) {
471 case Element::kRect_Type:
472 if (element->isAA()) {
473 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu, element->getRect(), true);
474 } else {
475 fGpu->drawSimpleRect(element->getRect(), NULL);
476 }
477 return true;
478 case Element::kPath_Type:
479 return draw_path(this->getContext(), fGpu,
480 element->getPath(),
481 get_path_fill(element->getPath()),
482 element->isAA(),
483 resultBounds);
484 default:
485 // something is wrong if we're trying to draw an empty element.
486 GrCrash("Unexpected element type");
487 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000488 }
489 return true;
490}
491
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000492void GrClipMaskManager::mergeMask(GrTexture* dstMask,
493 GrTexture* srcMask,
494 SkRegion::Op op,
495 const GrIRect& dstBound,
496 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000497 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000498 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000499 SkMatrix oldMatrix = drawState->getViewMatrix();
500 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000501
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000502 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000503
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000504 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000505
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000506 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000507 sampleM.setIDiv(srcMask->width(), srcMask->height());
508 drawState->stage(0)->setEffect(
509 GrTextureDomainEffect::Create(srcMask,
510 sampleM,
511 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
512 GrTextureDomainEffect::kDecal_WrapMode))->unref();
513 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000514
tomhudson@google.com676e6602012-07-10 17:21:48 +0000515 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000516 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000517}
518
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000519// get a texture to act as a temporary buffer for AA clip boolean operations
520// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000521void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000522 GrAutoScratchTexture* temp) {
523 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000524 // we've already allocated the temp texture
525 return;
526 }
527
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000528 GrTextureDesc desc;
529 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
530 desc.fWidth = bounds.width();
531 desc.fHeight = bounds.height();
532 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000533
robertphillips@google.com2c756812012-05-22 20:28:23 +0000534 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000535}
536
robertphillips@google.comf105b102012-05-14 12:18:26 +0000537
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000538void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000539 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000540 // Since we are setting up the cache we know the last lookup was a miss
541 // Free up the currently cached mask so it can be reused
542 fAACache.reset();
543
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000544 GrTextureDesc desc;
545 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
546 desc.fWidth = bounds.width();
547 desc.fHeight = bounds.height();
548 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000549
550 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000551}
552
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000553////////////////////////////////////////////////////////////////////////////////
554// Shared preamble between gpu and SW-only AA clip mask creation paths.
555// Handles caching, determination of clip mask bound & allocation (if needed)
556// of the result texture
557// Returns true if there is no more work to be done (i.e., we got a cache hit)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000558bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000559 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000560 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000561 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562 GrAssert(origDrawState->isClipState());
563
564 GrRenderTarget* rt = origDrawState->getRenderTarget();
565 GrAssert(NULL != rt);
566
robertphillips@google.comf294b772012-04-27 14:29:26 +0000567 // unlike the stencil path the alpha path is not bound to the size of the
568 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000569 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000570 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000571
572 // need to outset a pixel since the standard bounding box computation
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000573 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000574 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000575
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000576 // TODO: make sure we don't outset if bounds are still 0,0 @ min
577
robertphillips@google.comba998f22012-10-12 11:33:56 +0000578 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000579 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000580 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000581 return true;
582 }
583
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000584 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000585 return false;
586}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000587
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000588////////////////////////////////////////////////////////////////////////////////
589// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000590bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000591 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000592 GrIRect *devResultBounds) {
593 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000594 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
595
robertphillips@google.com7b112892012-07-31 15:18:21 +0000596 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000597 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000598 return true;
599 }
600
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000601 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
602
robertphillips@google.comf105b102012-05-14 12:18:26 +0000603 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000604 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000605 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000606 return false;
607 }
608
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000609 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
610 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000611
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000612 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000614 // The mask we generate is translated so that its upper-left corner is at devResultBounds
615 // upper-left corner in device space.
616 GrIRect maskResultBounds = GrIRect::MakeWH(devResultBounds->width(), devResultBounds->height());
617
618 // Set the matrix so that rendered clip elements are transformed from the space of the clip
619 // stack to the alpha-mask. This accounts for both translation due to the clip-origin and the
620 // placement of the mask within the device.
621 SkVector clipToMaskOffset = {
622 SkIntToScalar(-devResultBounds->fLeft - clipDataIn.fOrigin.fX),
623 SkIntToScalar(-devResultBounds->fTop - clipDataIn.fOrigin.fY)
624 };
625 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626
627 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000628 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
629
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000630 SkClipStack::Iter iter(*clipDataIn.fClipStack,
631 SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000632 const Element* element = process_initial_clip_elements(&iter,
633 *devResultBounds,
634 &clearToInside,
635 &firstOp,
636 clipDataIn);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000637 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
638 // clear the part that we care about.
639 fGpu->clear(&maskResultBounds,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000640 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000641 accum->asRenderTarget());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000642 bool accumClearedToZero = !clearToInside;
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000643
robertphillips@google.comf105b102012-05-14 12:18:26 +0000644 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000645 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000646 // walk through each clip element and perform its set op
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000647 for ( ; NULL != element; element = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000649 SkRegion::Op op = element->getOp();
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000650 if (first) {
651 first = false;
652 op = firstOp;
653 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000654
bsalomon@google.com6794a252012-11-08 15:30:53 +0000655 if (SkRegion::kReplace_Op == op) {
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000656 // clear the accumulator and draw the new object directly into it
657 if (!accumClearedToZero) {
658 fGpu->clear(&maskResultBounds, 0x00000000, accum->asRenderTarget());
659 }
660
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000661 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000662 this->drawClipShape(accum, element, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000663
664 } else if (SkRegion::kReverseDifference_Op == op ||
665 SkRegion::kIntersect_Op == op) {
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000666 // there is no point in intersecting a screen filling rectangle.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000667 if (SkRegion::kIntersect_Op == op && Element::kRect_Type == element->getType() &&
668 contains(element->getRect(), *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000669 continue;
670 }
671
robertphillips@google.com7b112892012-07-31 15:18:21 +0000672 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000673 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000674 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000675 return false;
676 }
677
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000678 // this is the bounds of the clip element in the space of the alpha-mask. The temporary
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000679 // mask buffer can be substantially larger than the actually clip stack element. We
680 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000681 // the accumulator
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000682 GrRect elementMaskBounds = element->getBounds();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000683 elementMaskBounds.offset(clipToMaskOffset);
684 GrIRect elementMaskIBounds;
685 elementMaskBounds.roundOut(&elementMaskIBounds);
686
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000687 // clear the temp target & draw into it
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000688 fGpu->clear(&elementMaskIBounds, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000689
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000690 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000691 this->drawClipShape(temp.texture(), element, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000692
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000693 // Now draw into the accumulator using the real operation
694 // and the temp buffer as a texture
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000695 this->mergeMask(accum, temp.texture(), op, maskResultBounds, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000696 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000697 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000698 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000699 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000700 this->drawClipShape(accum, element, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000701 }
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000702 accumClearedToZero = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000703 }
704
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000705 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000706 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000707 return true;
708}
709
710////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000711// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000712// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000713bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000714 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000716 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000717
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000718 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719 GrAssert(drawState->isClipState());
720
721 GrRenderTarget* rt = drawState->getRenderTarget();
722 GrAssert(NULL != rt);
723
724 // TODO: dynamically attach a SB when needed.
725 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
726 if (NULL == stencilBuffer) {
727 return false;
728 }
729
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000730 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000731
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000732 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733
734 // we set the current clip to the bounds so that our recursive
735 // draws are scissored to them. We use the copy of the complex clip
736 // we just stashed on the SB to render from. We set it back after
737 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000738 const GrClipData* oldClipData = fGpu->getClip();
739
robertphillips@google.com7b112892012-07-31 15:18:21 +0000740 // The origin of 'newClipData' is (0, 0) so it is okay to place
741 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000742 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000743 GrClipData newClipData;
744 newClipData.fClipStack = &newClipStack;
745
746 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000747
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000748 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
749 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000750 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000751 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000752
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000753 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000754 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000755 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000756 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000757 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000758 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000759 }
760
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000761#if !VISUALIZE_COMPLEX_CLIP
762 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
763#endif
764
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000765 int clipBit = stencilBuffer->bits();
766 SkASSERT((clipBit <= 16) &&
767 "Ganesh only handles 16b or smaller stencil buffers");
768 clipBit = (1 << (clipBit-1));
769
robertphillips@google.com7b112892012-07-31 15:18:21 +0000770 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000771
772 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000773 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
774
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000775 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000776 SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000777 const Element* element = process_initial_clip_elements(&iter,
778 devRTRect,
779 &clearToInside,
780 &firstOp,
781 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000782
robertphillips@google.com7b112892012-07-31 15:18:21 +0000783 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000784 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785
786 // walk through each clip element and perform its set op
787 // with the existing clip.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000788 for ( ; NULL != element; element = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000789 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000790 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000791 // enabled at bottom of loop
792 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000793 // if the target is MSAA then we want MSAA enabled when the clip is soft
794 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000795 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000796 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797
tomhudson@google.com8afae612012-08-14 15:03:35 +0000798 // Can the clip element be drawn directly to the stencil buffer
799 // with a non-inverted fill rule without extra passes to
800 // resolve in/out status?
801 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000802
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000803 SkRegion::Op op = element->getOp();
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000804 if (first) {
805 first = false;
806 op = firstOp;
807 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000808
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000810 const SkPath* clipPath = NULL;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000811 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000813 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000814 fillInverted = false;
815 // there is no point in intersecting a screen filling
816 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000817 if (SkRegion::kIntersect_Op == op &&
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000818 contains(element->getRect(), devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819 continue;
820 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000821 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000822 GrAssert(Element::kPath_Type == element->getType());
823 clipPath = &element->getPath();
824 fill = get_path_fill(*clipPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 fillInverted = GrIsFillInverted(fill);
826 fill = GrNonInvertedFill(fill);
tomhudson@google.com8afae612012-08-14 15:03:35 +0000827 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000828 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000829 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830 return false;
831 }
832 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000833 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000834 }
835
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000836 int passes;
837 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
838
839 bool canDrawDirectToClip; // Given the renderer, the element,
840 // fill rule, and set operation can
841 // we render the element directly to
842 // stencil bit used for clipping.
843 canDrawDirectToClip =
844 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000845 canRenderDirectToStencil,
846 clipBit,
847 fillInverted,
848 &passes,
849 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000850
851 // draw the element to the client stencil bits if necessary
852 if (!canDrawDirectToClip) {
853 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
854 kIncClamp_StencilOp,
855 kIncClamp_StencilOp,
856 kAlways_StencilFunc,
857 0xffff,
858 0x0000,
859 0xffff);
860 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000861 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000862 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000863 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000864 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000865 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000866 if (canRenderDirectToStencil) {
867 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000868 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000869 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000870 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000871 }
872 }
873 }
874
875 // now we modify the clip bit by rendering either the clip
876 // element directly or a bounding rect of the entire clip.
877 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
878 for (int p = 0; p < passes; ++p) {
879 *drawState->stencil() = stencilSettings[p];
880 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000881 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000882 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000883 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000884 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000885 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000886 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000887 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000888 }
889 } else {
890 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000891 // 'devClipBounds' is already in device coordinates so the
892 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000893 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000894 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000895 GrRect canvClipBounds;
896 canvClipBounds.set(devClipBounds);
897 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
898 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000899 }
900 }
901 }
902 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000903 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000904 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000905 // set this last because recursive draws may overwrite it back to kNone.
906 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
907 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000908 return true;
909}
910
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000911
bsalomon@google.com411dad02012-06-05 20:24:20 +0000912// mapping of clip-respecting stencil funcs to normal stencil funcs
913// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000914static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000915 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
916 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
917 // In the Clip Funcs
918 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
919 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
920 kLess_StencilFunc, // kLessIfInClip_StencilFunc
921 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
922 // Special in the clip func that forces user's ref to be 0.
923 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
924 // make ref 0 and do normal nequal.
925 },
926 {// Stencil-Clipping is ENABLED
927 // In the Clip Funcs
928 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
929 // eq stencil clip bit, mask
930 // out user bits.
931
932 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
933 // add stencil bit to mask and ref
934
935 kLess_StencilFunc, // kLessIfInClip_StencilFunc
936 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
937 // for both of these we can add
938 // the clip bit to the mask and
939 // ref and compare as normal
940 // Special in the clip func that forces user's ref to be 0.
941 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
942 // make ref have only the clip bit set
943 // and make comparison be less
944 // 10..0 < 1..user_bits..
945 }
946};
947
bsalomon@google.coma3201942012-06-21 19:58:20 +0000948namespace {
949// Sets the settings to clip against the stencil buffer clip while ignoring the
950// client bits.
951const GrStencilSettings& basic_apply_stencil_clip_settings() {
952 // stencil settings to use when clip is in stencil
953 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
954 kKeep_StencilOp,
955 kKeep_StencilOp,
956 kAlwaysIfInClip_StencilFunc,
957 0x0000,
958 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000959 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000960 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
961}
962}
963
964void GrClipMaskManager::setGpuStencil() {
965 // We make two copies of the StencilSettings here (except in the early
966 // exit scenario. One copy from draw state to the stack var. Then another
967 // from the stack var to the gpu. We could make this class hold a ptr to
968 // GrGpu's fStencilSettings and eliminate the stack copy here.
969
970 const GrDrawState& drawState = fGpu->getDrawState();
971
972 // use stencil for clipping if clipping is enabled and the clip
973 // has been written into the stencil.
974 GrClipMaskManager::StencilClipMode clipMode;
975 if (this->isClipInStencil() && drawState.isClipState()) {
976 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
977 // We can't be modifying the clip and respecting it at the same time.
978 GrAssert(!drawState.isStateFlagEnabled(
979 GrGpu::kModifyStencilClip_StateBit));
980 } else if (drawState.isStateFlagEnabled(
981 GrGpu::kModifyStencilClip_StateBit)) {
982 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
983 } else {
984 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
985 }
986
987 GrStencilSettings settings;
988 // The GrGpu client may not be using the stencil buffer but we may need to
989 // enable it in order to respect a stencil clip.
990 if (drawState.getStencil().isDisabled()) {
991 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
992 settings = basic_apply_stencil_clip_settings();
993 } else {
994 fGpu->disableStencil();
995 return;
996 }
997 } else {
998 settings = drawState.getStencil();
999 }
1000
1001 // TODO: dynamically attach a stencil buffer
1002 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001003 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001004 drawState.getRenderTarget()->getStencilBuffer();
1005 if (NULL != stencilBuffer) {
1006 stencilBits = stencilBuffer->bits();
1007 }
1008
bsalomon@google.comf6601872012-08-28 21:11:35 +00001009 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001010 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001011 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001012 this->adjustStencilParams(&settings, clipMode, stencilBits);
1013 fGpu->setStencilSettings(settings);
1014}
1015
1016void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1017 StencilClipMode mode,
1018 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001019 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001020
1021 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001022 // We assume that this clip manager itself is drawing to the GrGpu and
1023 // has already setup the correct values.
1024 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001025 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001026
bsalomon@google.com411dad02012-06-05 20:24:20 +00001027 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1028 unsigned int userBits = clipBit - 1;
1029
bsalomon@google.coma3201942012-06-21 19:58:20 +00001030 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001031 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001032
bsalomon@google.coma3201942012-06-21 19:58:20 +00001033 bool finished = false;
1034 while (!finished) {
1035 GrStencilFunc func = settings->func(face);
1036 uint16_t writeMask = settings->writeMask(face);
1037 uint16_t funcMask = settings->funcMask(face);
1038 uint16_t funcRef = settings->funcRef(face);
1039
1040 GrAssert((unsigned) func < kStencilFuncCount);
1041
1042 writeMask &= userBits;
1043
1044 if (func >= kBasicStencilFuncCount) {
1045 int respectClip = kRespectClip_StencilClipMode == mode;
1046 if (respectClip) {
1047 // The GrGpu class should have checked this
1048 GrAssert(this->isClipInStencil());
1049 switch (func) {
1050 case kAlwaysIfInClip_StencilFunc:
1051 funcMask = clipBit;
1052 funcRef = clipBit;
1053 break;
1054 case kEqualIfInClip_StencilFunc:
1055 case kLessIfInClip_StencilFunc:
1056 case kLEqualIfInClip_StencilFunc:
1057 funcMask = (funcMask & userBits) | clipBit;
1058 funcRef = (funcRef & userBits) | clipBit;
1059 break;
1060 case kNonZeroIfInClip_StencilFunc:
1061 funcMask = (funcMask & userBits) | clipBit;
1062 funcRef = clipBit;
1063 break;
1064 default:
1065 GrCrash("Unknown stencil func");
1066 }
1067 } else {
1068 funcMask &= userBits;
1069 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001070 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001071 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001072 gSpecialToBasicStencilFunc[respectClip];
1073 func = table[func - kBasicStencilFuncCount];
1074 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001075 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001076 funcMask &= userBits;
1077 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001078 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001079
1080 settings->setFunc(face, func);
1081 settings->setWriteMask(face, writeMask);
1082 settings->setFuncMask(face, funcMask);
1083 settings->setFuncRef(face, funcRef);
1084
1085 if (GrStencilSettings::kFront_Face == face) {
1086 face = GrStencilSettings::kBack_Face;
1087 finished = !twoSided;
1088 } else {
1089 finished = true;
1090 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001091 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001092 if (!twoSided) {
1093 settings->copyFrontSettingsToBack();
1094 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001095}
1096
1097////////////////////////////////////////////////////////////////////////////////
1098
robertphillips@google.comfa662942012-05-17 12:20:22 +00001099namespace {
1100
1101GrPathFill invert_fill(GrPathFill fill) {
1102 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001103 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1104 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1105 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1106 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1107 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001108 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001109 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1110 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1111 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1112 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1113 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1114 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001115 return gInvertedFillTable[fill];
1116}
1117
1118}
1119
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001120bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001121 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001122 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001123 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001124
robertphillips@google.com7b112892012-07-31 15:18:21 +00001125 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001126 return true;
1127 }
1128
robertphillips@google.comf105b102012-05-14 12:18:26 +00001129 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001130 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001131 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001132 return false;
1133 }
1134
robertphillips@google.com2c756812012-05-22 20:28:23 +00001135 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001136
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001137 SkMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001138 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001139 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001140 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001141
robertphillips@google.comfa662942012-05-17 12:20:22 +00001142 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001143 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1144
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001145 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001146 SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001147 const Element* element = process_initial_clip_elements(&iter,
1148 *devResultBounds,
1149 &clearToInside,
1150 &firstOp,
1151 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001152
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001153 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001154
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001155 bool first = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001156 for ( ; NULL != element; element = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001157
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001158 SkRegion::Op op = element->getOp();
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001159 if (first) {
1160 first = false;
1161 op = firstOp;
1162 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001163
1164 if (SkRegion::kIntersect_Op == op ||
1165 SkRegion::kReverseDifference_Op == op) {
1166 // Intersect and reverse difference require modifying pixels
1167 // outside of the geometry that is being "drawn". In both cases
1168 // we erase all the pixels outside of the geometry but
1169 // leave the pixels inside the geometry alone. For reverse
1170 // difference we invert all the pixels before clearing the ones
1171 // outside the geometry.
1172 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001173 SkRect temp;
1174 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001175 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1176 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001177
1178 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001179 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001180 }
1181
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001182 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001183 // convert the rect to a path so we can invert the fill
1184 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001185 temp.addRect(element->getRect());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001186
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001187 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001188 kInverseEvenOdd_GrPathFill, element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001189 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001190 } else {
1191 GrAssert(Element::kPath_Type == element->getType());
1192 helper.draw(element->getPath(),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001193 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001194 invert_fill(get_path_fill(element->getPath())),
1195 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001196 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001197 }
1198
1199 continue;
1200 }
1201
1202 // The other ops (union, xor, diff) only affect pixels inside
1203 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001204 if (Element::kRect_Type == element->getType()) {
1205 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1206 } else {
1207 GrAssert(Element::kPath_Type == element->getType());
1208 helper.draw(element->getPath(),
robertphillips@google.comfa662942012-05-17 12:20:22 +00001209 op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001210 get_path_fill(element->getPath()),
1211 element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001212 }
1213 }
1214
robertphillips@google.comfa662942012-05-17 12:20:22 +00001215 // Because we are using the scratch texture cache, "accum" may be
1216 // larger than expected and have some cruft in the areas we aren't using.
1217 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001218 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001219
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001220 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001221
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001222 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001223
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001224 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001225 return true;
1226}
1227
robertphillips@google.comf294b772012-04-27 14:29:26 +00001228////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001229void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001230 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001231}