blob: aa0c0249a840fd158bb53698dba0711c1772865e [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
10#include "GrGpu.h"
11#include "GrRenderTarget.h"
12#include "GrStencilBuffer.h"
13#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000014#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000015#include "SkRasterClip.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000016#include "GrAAConvexPathRenderer.h"
17#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000019#include "GrCacheID.h"
20
21GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000022
robertphillips@google.comba998f22012-10-12 11:33:56 +000023#define GR_AA_CLIP 1
24#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000025
robertphillips@google.comf294b772012-04-27 14:29:26 +000026////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000027namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028// set up the draw state to enable the aa clipping mask. Besides setting up the
robertphillips@google.coma72eef32012-05-01 17:22:59 +000029// sampler matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030void setup_drawstate_aaclip(GrGpu* gpu,
31 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000032 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000033 GrDrawState* drawState = gpu->drawState();
34 GrAssert(drawState);
35
36 static const int maskStage = GrPaint::kTotalStages+1;
37
38 GrMatrix mat;
39 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000041 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042 mat.preConcat(drawState->getViewMatrix());
43
bsalomon@google.comb0221772012-10-16 14:16:11 +000044 drawState->createTextureEffect(maskStage, result, mat);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045}
46
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000047bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000048 GrGpu* gpu,
49 const SkPath& path,
50 GrPathFill fill,
51 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000052 // last (false) parameter disallows use of the SW path renderer
53 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
54}
55
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000056GrPathFill get_path_fill(const SkPath& path) {
57 switch (path.getFillType()) {
58 case SkPath::kWinding_FillType:
59 return kWinding_GrPathFill;
60 case SkPath::kEvenOdd_FillType:
61 return kEvenOdd_GrPathFill;
62 case SkPath::kInverseWinding_FillType:
63 return kInverseWinding_GrPathFill;
64 case SkPath::kInverseEvenOdd_FillType:
65 return kInverseEvenOdd_GrPathFill;
66 default:
67 GrCrash("Unsupported path fill in clip.");
68 return kWinding_GrPathFill; // suppress warning
69 }
70}
71
robertphillips@google.comb99225c2012-07-24 18:20:10 +000072/**
73 * Does any individual clip in 'clipIn' use anti-aliasing?
74 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000075bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000076
robertphillips@google.com641f8b12012-07-31 19:15:58 +000077 SkClipStack::Iter iter;
78 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000079
robertphillips@google.com641f8b12012-07-31 19:15:58 +000080 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comb99225c2012-07-24 18:20:10 +000081 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
82 NULL != clip;
83 clip = iter.next()) {
84
85 if (clip->fDoAA) {
86 return true;
87 }
88 }
89
90 return false;
91}
92
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000093}
94
robertphillips@google.comfa662942012-05-17 12:20:22 +000095/*
96 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
97 * will be used on any element. If so, it returns true to indicate that the
98 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
99 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000100bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000101
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000102 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000103 // a clip gets complex enough it can just be done in SW regardless
104 // of whether it would invoke the GrSoftwarePathRenderer.
105 bool useSW = false;
106
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000107 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
108 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000109
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000110 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
111 NULL != clip;
112 clip = iter.next()) {
113
114 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000115 // Everything before a replace op can be ignored so start
116 // afresh w.r.t. determining if any element uses the SW path
117 useSW = false;
118 }
119
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000120 // rects can always be drawn directly w/o using the software path
121 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000122 if (NULL != clip->fPath &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000123 path_needs_SW_renderer(this->getContext(), fGpu,
124 *clip->fPath,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000125 get_path_fill(*clip->fPath),
126 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000127 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000128 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000129 }
130
131 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000132}
133
robertphillips@google.comf294b772012-04-27 14:29:26 +0000134////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000135// sort out what kind of clip mask needs to be created: alpha, stencil,
136// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000137bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000138 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000139
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000140 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000141 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000142 fGpu->disableScissor();
143 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000144 return true;
145 }
146
147 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000148 // GrDrawTarget should have filtered this for us
149 GrAssert(NULL != rt);
150
robertphillips@google.com7b112892012-07-31 15:18:21 +0000151 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000152 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000153
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000154 clipDataIn->getConservativeBounds(rt, &devClipBounds,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000155 &isIntersectionOfRects);
156 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000157 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000158 }
159
bsalomon@google.com100abf42012-09-05 17:40:04 +0000160#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000161 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000162
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000163 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000164 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000165 // in software (this will only happen if the clip mask is anti-aliased
166 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000167 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000168 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000169 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000170 // The clip geometry is complex enough that it will be more
171 // efficient to create it entirely in software
172 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000173 GrIRect devBound;
174 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
175 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000176 fGpu->disableScissor();
177 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000178 return true;
179 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000180
181 // if SW clip mask creation fails fall through to the other
182 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000183 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000184#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000185
robertphillips@google.comf294b772012-04-27 14:29:26 +0000186#if GR_AA_CLIP
187 // If MSAA is enabled use the (faster) stencil path for AA clipping
188 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000189 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000190 // Since we are going to create a destination texture of the correct
191 // size for the mask (rather than being bound by the size of the
192 // render target) we aren't going to use scissoring like the stencil
193 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000194 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000195 GrIRect devBound;
196 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
197 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000198 fGpu->disableScissor();
199 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000200 return true;
201 }
202
203 // if alpha clip mask creation fails fall through to the stencil
204 // buffer method
205 }
206#endif // GR_AA_CLIP
207
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000208 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000209 // an antialiased clip couldn't be created. In either case, free up
210 // the texture in the antialiased mask cache.
211 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000212 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000213 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000214 // AA cache.
215 fAACache.reset();
216
bsalomon@google.coma3201942012-06-21 19:58:20 +0000217 // If the clip is a rectangle then just set the scissor. Otherwise, create
218 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000219 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000220 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000221 this->setGpuStencil();
222 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000223 }
224
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000225 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000226 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000227 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000228
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000229 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000230 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000231 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000232 // This must occur after createStencilClipMask. That function may change
233 // the scissor. Also, it only guarantees that the stencil mask is correct
234 // within the bounds it was passed, so we must use both stencil and scissor
235 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000236 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000237 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000238 return true;
239}
240
241#define VISUALIZE_COMPLEX_CLIP 0
242
243#if VISUALIZE_COMPLEX_CLIP
244 #include "GrRandom.h"
245 GrRandom gRandom;
246 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
247#else
248 #define SET_RANDOM_COLOR
249#endif
250
251namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000252/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000253 * Does "canvContainer" contain "devContainee"? If either is empty then
254 * no containment is possible. "canvContainer" is in canvas coordinates while
255 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000256 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000257 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000258bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000259 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000260 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000261 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000262 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000263 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000264 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000265 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000266}
267
robertphillips@google.comf294b772012-04-27 14:29:26 +0000268////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000269// determines how many elements at the head of the clip can be skipped and
270// whether the initial clear should be to the inside- or outside-the-clip value,
271// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000272const SkClipStack::Iter::Clip* process_initial_clip_elements(
273 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000274 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000275 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000276 SkRegion::Op* firstOp,
277 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000278
279 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000280
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000281 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000282 // processed the clip is entirely open. However, depending on the
283 // first set op we may prefer to clear to 0 for performance. We may
284 // also be able to skip the initial clip paths/rects. We loop until
285 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000286 bool done = false;
287 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000288
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000289 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000290
291 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
292 NULL != clip && !done;
293 clip = iter->next()) {
294 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000295 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000296 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000297 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000298 *clearToInside = false;
299 done = true;
300 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000301 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000302 // if this element contains the entire bounds then we
303 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000304 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000305 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000306 break;
307 }
308 // if everything is initially clearToInside then intersect is
309 // same as clear to 0 and treat as a replace. Otherwise,
310 // set stays empty.
311 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000312 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000313 *clearToInside = false;
314 done = true;
315 }
316 break;
317 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000318 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000319 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000320 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000321 // clearToInside
322 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000323 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000324 done = true;
325 }
326 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000327 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000328 // xor is same as difference or replace both of which
329 // can be 1-pass instead of 2 for xor.
330 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000331 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000332 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000333 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000334 }
335 done = true;
336 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000337 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000338 // if all pixels are clearToInside then we have to process the
339 // difference, otherwise it has no effect and all pixels
340 // remain outside.
341 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000342 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000343 done = true;
344 }
345 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000346 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000347 // if all pixels are clearToInside then reverse difference
348 // produces empty set. Otherise it is same as replace
349 if (*clearToInside) {
350 *clearToInside = false;
351 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000352 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000353 done = true;
354 }
355 break;
356 default:
357 GrCrash("Unknown set op.");
358 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000359
360 if (done) {
361 // we need to break out here (rather than letting the test in
362 // the loop do it) since backing up the iterator is very expensive
363 break;
364 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000365 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000366 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000367}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000368
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000369}
370
robertphillips@google.comf294b772012-04-27 14:29:26 +0000371namespace {
372
373////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000374// set up the OpenGL blend function to perform the specified
375// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000376void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000377
378 switch (op) {
379 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000380 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000381 break;
382 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000383 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000384 break;
385 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000386 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387 break;
388 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000389 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000390 break;
391 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000392 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000393 break;
394 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000395 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000396 break;
397 default:
398 GrAssert(false);
399 break;
400 }
401}
402
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000404bool draw_path_in_software(GrContext* context,
405 GrGpu* gpu,
406 const SkPath& path,
407 GrPathFill fill,
408 bool doAA,
409 const GrIRect& resultBounds) {
410
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000411 SkAutoTUnref<GrTexture> texture(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000412 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
413 resultBounds, fill,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000414 doAA, NULL));
415 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000416 return false;
417 }
418
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000419 // The ClipMaskManager accumulates the clip mask in the UL corner
420 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000421
bsalomon@google.come3d32162012-07-20 13:37:06 +0000422 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000423
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000424 GrAssert(!GrIsFillInverted(fill));
425 return true;
426}
427
428
429////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000430bool draw_path(GrContext* context,
431 GrGpu* gpu,
432 const SkPath& path,
433 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000434 bool doAA,
435 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000436
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000437 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000439 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000440 }
441
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000442 pr->drawPath(path, fill, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000443 return true;
444}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000445
robertphillips@google.com7b112892012-07-31 15:18:21 +0000446// 'rect' enters in device coordinates and leaves in canvas coordinates
447void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
448 GrAssert(NULL != rect);
449
450 rect->fLeft += SkIntToScalar(origin.fX);
451 rect->fTop += SkIntToScalar(origin.fY);
452 rect->fRight += SkIntToScalar(origin.fX);
453 rect->fBottom += SkIntToScalar(origin.fY);
454}
455
robertphillips@google.com72176b22012-05-23 13:19:12 +0000456}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000457
458////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000459bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000460 const SkClipStack::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000461 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000462 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000463 GrAssert(NULL != drawState);
464
465 drawState->setRenderTarget(target->asRenderTarget());
466
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000467 if (NULL != clip->fRect) {
468 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000469 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000470 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000471 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000472 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000473 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000474 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000475 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000476 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000477 *clip->fPath,
478 get_path_fill(*clip->fPath),
479 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000480 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000481 }
482 return true;
483}
484
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000485void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000486 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000487 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000488 GrAssert(NULL != drawState);
489
490 // no AA here since it is encoded in the texture
491 drawState->setRenderTarget(target->asRenderTarget());
492
493 GrMatrix sampleM;
494 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495
bsalomon@google.comb0221772012-10-16 14:16:11 +0000496 drawState->createTextureEffect(0, texture, sampleM);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000498 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
robertphillips@google.comf105b102012-05-14 12:18:26 +0000499 SkIntToScalar(target->height()));
500
bsalomon@google.come3d32162012-07-20 13:37:06 +0000501 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000502
tomhudson@google.com676e6602012-07-10 17:21:48 +0000503 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000504}
505
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000506// get a texture to act as a temporary buffer for AA clip boolean operations
507// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000508void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000509 GrAutoScratchTexture* temp) {
510 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000511 // we've already allocated the temp texture
512 return;
513 }
514
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000515 GrTextureDesc desc;
516 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
517 desc.fWidth = bounds.width();
518 desc.fHeight = bounds.height();
519 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000520
robertphillips@google.com2c756812012-05-22 20:28:23 +0000521 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000522}
523
robertphillips@google.comf105b102012-05-14 12:18:26 +0000524
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000525void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000526 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000527 // Since we are setting up the cache we know the last lookup was a miss
528 // Free up the currently cached mask so it can be reused
529 fAACache.reset();
530
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000531 GrTextureDesc desc;
532 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
533 desc.fWidth = bounds.width();
534 desc.fHeight = bounds.height();
535 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000536
537 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000538}
539
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000540////////////////////////////////////////////////////////////////////////////////
541// Shared preamble between gpu and SW-only AA clip mask creation paths.
542// Handles caching, determination of clip mask bound & allocation (if needed)
543// of the result texture
544// 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 +0000545bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000546 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000547 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000548 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000549 GrAssert(origDrawState->isClipState());
550
551 GrRenderTarget* rt = origDrawState->getRenderTarget();
552 GrAssert(NULL != rt);
553
robertphillips@google.comf294b772012-04-27 14:29:26 +0000554 // unlike the stencil path the alpha path is not bound to the size of the
555 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000556 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000557 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000558
559 // need to outset a pixel since the standard bounding box computation
560 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000561 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000562
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000563 // TODO: make sure we don't outset if bounds are still 0,0 @ min
564
robertphillips@google.comba998f22012-10-12 11:33:56 +0000565 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000566 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000567 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000568 return true;
569 }
570
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000571 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000572 return false;
573}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000574
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000575////////////////////////////////////////////////////////////////////////////////
576// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000577bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000578 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000579 GrIRect *devResultBounds) {
580 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000581 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
582
robertphillips@google.com7b112892012-07-31 15:18:21 +0000583 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000584 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000585 return true;
586 }
587
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000588 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
589
robertphillips@google.comf105b102012-05-14 12:18:26 +0000590 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000591 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000592 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000593 return false;
594 }
595
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000596 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
597 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000598
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000599 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000600
robertphillips@google.com7b112892012-07-31 15:18:21 +0000601 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000602 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000603 // if we were able to trim down the size of the mask we need to
robertphillips@google.comf294b772012-04-27 14:29:26 +0000604 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000605 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000606 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000607 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608 }
609
610 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000611 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
612
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000613 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000614 SkClipStack::Iter::kBottom_IterStart);
615 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000616 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000617 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000618 &firstOp,
619 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000621 fGpu->clear(NULL,
622 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000623 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000624
robertphillips@google.comf105b102012-05-14 12:18:26 +0000625 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000626 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000627 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000628 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000629
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000630 SkRegion::Op op = clip->fOp;
631 if (first) {
632 first = false;
633 op = firstOp;
634 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000635
636 if (SkRegion::kReplace_Op == op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000637 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000638 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000639
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000640 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000641 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000642
643 } else if (SkRegion::kReverseDifference_Op == op ||
644 SkRegion::kIntersect_Op == op) {
645 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000646 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000647 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648 continue;
649 }
650
robertphillips@google.com7b112892012-07-31 15:18:21 +0000651 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000652 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000653 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000654 return false;
655 }
656
robertphillips@google.comf294b772012-04-27 14:29:26 +0000657 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000658 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000659
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000660 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000661 this->drawClipShape(temp.texture(), clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662
663 // TODO: rather than adding these two translations here
664 // compute the bounding box needed to render the texture
665 // into temp
robertphillips@google.com7b112892012-07-31 15:18:21 +0000666 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000667 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
668 // In order for the merge of the temp clip into the accumulator
669 // to work we need to disable the translation
670 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000671 }
672
673 // Now draw into the accumulator using the real operation
674 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000675 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000676 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000677
robertphillips@google.com7b112892012-07-31 15:18:21 +0000678 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000679 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
680 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000681 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000682 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000683 }
684
685 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000686 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000687 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000688 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000689 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000690 }
691 }
692
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000693 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000694 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000695 return true;
696}
697
698////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000699// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000700// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000701bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000702 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000703
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000704 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000705
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000706 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707 GrAssert(drawState->isClipState());
708
709 GrRenderTarget* rt = drawState->getRenderTarget();
710 GrAssert(NULL != rt);
711
712 // TODO: dynamically attach a SB when needed.
713 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
714 if (NULL == stencilBuffer) {
715 return false;
716 }
717
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000718 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000720 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721
722 // we set the current clip to the bounds so that our recursive
723 // draws are scissored to them. We use the copy of the complex clip
724 // we just stashed on the SB to render from. We set it back after
725 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000726 const GrClipData* oldClipData = fGpu->getClip();
727
robertphillips@google.com7b112892012-07-31 15:18:21 +0000728 // The origin of 'newClipData' is (0, 0) so it is okay to place
729 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000730 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000731 GrClipData newClipData;
732 newClipData.fClipStack = &newClipStack;
733
734 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000736 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
737 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000739 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000741 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000742 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000743 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000744 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000745 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000746 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000747 }
748
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749#if !VISUALIZE_COMPLEX_CLIP
750 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
751#endif
752
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753 int clipBit = stencilBuffer->bits();
754 SkASSERT((clipBit <= 16) &&
755 "Ganesh only handles 16b or smaller stencil buffers");
756 clipBit = (1 << (clipBit-1));
757
robertphillips@google.com7b112892012-07-31 15:18:21 +0000758 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759
760 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000761 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
762
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000763 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000764 SkClipStack::Iter::kBottom_IterStart);
765 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000766 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000767 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000768 &firstOp,
769 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000770
robertphillips@google.com7b112892012-07-31 15:18:21 +0000771 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000772 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000773
774 // walk through each clip element and perform its set op
775 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000776 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000777 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000778 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000779 // enabled at bottom of loop
780 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000781 // if the target is MSAA then we want MSAA enabled when the clip is soft
782 if (rt->isMultisampled()) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000783 drawState->setState(GrDrawState::kHWAntialias_StateBit, clip->fDoAA);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000784 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000785
tomhudson@google.com8afae612012-08-14 15:03:35 +0000786 // Can the clip element be drawn directly to the stencil buffer
787 // with a non-inverted fill rule without extra passes to
788 // resolve in/out status?
789 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000791 SkRegion::Op op = clip->fOp;
792 if (first) {
793 first = false;
794 op = firstOp;
795 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000796
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000798 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000799 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000800 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000801 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000802 fillInverted = false;
803 // there is no point in intersecting a screen filling
804 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000805 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000806 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 continue;
808 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000809 } else {
810 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000811 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 fillInverted = GrIsFillInverted(fill);
813 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000814 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000815 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000816 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000817 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000818 return false;
819 }
820 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000821 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 }
823
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 int passes;
825 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
826
827 bool canDrawDirectToClip; // Given the renderer, the element,
828 // fill rule, and set operation can
829 // we render the element directly to
830 // stencil bit used for clipping.
831 canDrawDirectToClip =
832 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000833 canRenderDirectToStencil,
834 clipBit,
835 fillInverted,
836 &passes,
837 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000838
839 // draw the element to the client stencil bits if necessary
840 if (!canDrawDirectToClip) {
841 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
842 kIncClamp_StencilOp,
843 kIncClamp_StencilOp,
844 kAlways_StencilFunc,
845 0xffff,
846 0x0000,
847 0xffff);
848 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000849 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000850 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000851 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000852 } else {
853 if (canRenderDirectToStencil) {
854 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000855 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000857 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000858 }
859 }
860 }
861
862 // now we modify the clip bit by rendering either the clip
863 // element directly or a bounding rect of the entire clip.
864 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
865 for (int p = 0; p < passes; ++p) {
866 *drawState->stencil() = stencilSettings[p];
867 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000868 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000869 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000870 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000871 } else {
872 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000873 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000874 }
875 } else {
876 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000877 // 'devClipBounds' is already in device coordinates so the
878 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000879 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000880 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000881 GrRect canvClipBounds;
882 canvClipBounds.set(devClipBounds);
883 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
884 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000885 }
886 }
887 }
888 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000889 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000890 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000891 // set this last because recursive draws may overwrite it back to kNone.
892 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
893 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000894 return true;
895}
896
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000897
bsalomon@google.com411dad02012-06-05 20:24:20 +0000898// mapping of clip-respecting stencil funcs to normal stencil funcs
899// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000900static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000901 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
902 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
903 // In the Clip Funcs
904 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
905 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
906 kLess_StencilFunc, // kLessIfInClip_StencilFunc
907 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
908 // Special in the clip func that forces user's ref to be 0.
909 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
910 // make ref 0 and do normal nequal.
911 },
912 {// Stencil-Clipping is ENABLED
913 // In the Clip Funcs
914 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
915 // eq stencil clip bit, mask
916 // out user bits.
917
918 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
919 // add stencil bit to mask and ref
920
921 kLess_StencilFunc, // kLessIfInClip_StencilFunc
922 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
923 // for both of these we can add
924 // the clip bit to the mask and
925 // ref and compare as normal
926 // Special in the clip func that forces user's ref to be 0.
927 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
928 // make ref have only the clip bit set
929 // and make comparison be less
930 // 10..0 < 1..user_bits..
931 }
932};
933
bsalomon@google.coma3201942012-06-21 19:58:20 +0000934namespace {
935// Sets the settings to clip against the stencil buffer clip while ignoring the
936// client bits.
937const GrStencilSettings& basic_apply_stencil_clip_settings() {
938 // stencil settings to use when clip is in stencil
939 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
940 kKeep_StencilOp,
941 kKeep_StencilOp,
942 kAlwaysIfInClip_StencilFunc,
943 0x0000,
944 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000945 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000946 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
947}
948}
949
950void GrClipMaskManager::setGpuStencil() {
951 // We make two copies of the StencilSettings here (except in the early
952 // exit scenario. One copy from draw state to the stack var. Then another
953 // from the stack var to the gpu. We could make this class hold a ptr to
954 // GrGpu's fStencilSettings and eliminate the stack copy here.
955
956 const GrDrawState& drawState = fGpu->getDrawState();
957
958 // use stencil for clipping if clipping is enabled and the clip
959 // has been written into the stencil.
960 GrClipMaskManager::StencilClipMode clipMode;
961 if (this->isClipInStencil() && drawState.isClipState()) {
962 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
963 // We can't be modifying the clip and respecting it at the same time.
964 GrAssert(!drawState.isStateFlagEnabled(
965 GrGpu::kModifyStencilClip_StateBit));
966 } else if (drawState.isStateFlagEnabled(
967 GrGpu::kModifyStencilClip_StateBit)) {
968 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
969 } else {
970 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
971 }
972
973 GrStencilSettings settings;
974 // The GrGpu client may not be using the stencil buffer but we may need to
975 // enable it in order to respect a stencil clip.
976 if (drawState.getStencil().isDisabled()) {
977 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
978 settings = basic_apply_stencil_clip_settings();
979 } else {
980 fGpu->disableStencil();
981 return;
982 }
983 } else {
984 settings = drawState.getStencil();
985 }
986
987 // TODO: dynamically attach a stencil buffer
988 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000989 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000990 drawState.getRenderTarget()->getStencilBuffer();
991 if (NULL != stencilBuffer) {
992 stencilBits = stencilBuffer->bits();
993 }
994
bsalomon@google.comf6601872012-08-28 21:11:35 +0000995 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000996 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +0000997 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000998 this->adjustStencilParams(&settings, clipMode, stencilBits);
999 fGpu->setStencilSettings(settings);
1000}
1001
1002void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1003 StencilClipMode mode,
1004 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001005 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001006
1007 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001008 // We assume that this clip manager itself is drawing to the GrGpu and
1009 // has already setup the correct values.
1010 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001011 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001012
bsalomon@google.com411dad02012-06-05 20:24:20 +00001013 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1014 unsigned int userBits = clipBit - 1;
1015
bsalomon@google.coma3201942012-06-21 19:58:20 +00001016 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001017 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001018
bsalomon@google.coma3201942012-06-21 19:58:20 +00001019 bool finished = false;
1020 while (!finished) {
1021 GrStencilFunc func = settings->func(face);
1022 uint16_t writeMask = settings->writeMask(face);
1023 uint16_t funcMask = settings->funcMask(face);
1024 uint16_t funcRef = settings->funcRef(face);
1025
1026 GrAssert((unsigned) func < kStencilFuncCount);
1027
1028 writeMask &= userBits;
1029
1030 if (func >= kBasicStencilFuncCount) {
1031 int respectClip = kRespectClip_StencilClipMode == mode;
1032 if (respectClip) {
1033 // The GrGpu class should have checked this
1034 GrAssert(this->isClipInStencil());
1035 switch (func) {
1036 case kAlwaysIfInClip_StencilFunc:
1037 funcMask = clipBit;
1038 funcRef = clipBit;
1039 break;
1040 case kEqualIfInClip_StencilFunc:
1041 case kLessIfInClip_StencilFunc:
1042 case kLEqualIfInClip_StencilFunc:
1043 funcMask = (funcMask & userBits) | clipBit;
1044 funcRef = (funcRef & userBits) | clipBit;
1045 break;
1046 case kNonZeroIfInClip_StencilFunc:
1047 funcMask = (funcMask & userBits) | clipBit;
1048 funcRef = clipBit;
1049 break;
1050 default:
1051 GrCrash("Unknown stencil func");
1052 }
1053 } else {
1054 funcMask &= userBits;
1055 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001056 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001057 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001058 gSpecialToBasicStencilFunc[respectClip];
1059 func = table[func - kBasicStencilFuncCount];
1060 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001061 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001062 funcMask &= userBits;
1063 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001064 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001065
1066 settings->setFunc(face, func);
1067 settings->setWriteMask(face, writeMask);
1068 settings->setFuncMask(face, funcMask);
1069 settings->setFuncRef(face, funcRef);
1070
1071 if (GrStencilSettings::kFront_Face == face) {
1072 face = GrStencilSettings::kBack_Face;
1073 finished = !twoSided;
1074 } else {
1075 finished = true;
1076 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001077 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001078 if (!twoSided) {
1079 settings->copyFrontSettingsToBack();
1080 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001081}
1082
1083////////////////////////////////////////////////////////////////////////////////
1084
robertphillips@google.comfa662942012-05-17 12:20:22 +00001085namespace {
1086
1087GrPathFill invert_fill(GrPathFill fill) {
1088 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001089 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1090 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1091 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1092 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1093 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001094 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001095 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1096 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1097 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1098 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1099 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1100 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001101 return gInvertedFillTable[fill];
1102}
1103
1104}
1105
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001106bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001107 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001108 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001109 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001110
robertphillips@google.com7b112892012-07-31 15:18:21 +00001111 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001112 return true;
1113 }
1114
robertphillips@google.comf105b102012-05-14 12:18:26 +00001115 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001116 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001117 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001118 return false;
1119 }
1120
robertphillips@google.com2c756812012-05-22 20:28:23 +00001121 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001122
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001123 GrMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001124 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001125 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001126 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001127
robertphillips@google.comfa662942012-05-17 12:20:22 +00001128 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001129 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1130
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001131 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001132 SkClipStack::Iter::kBottom_IterStart);
1133 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001134 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001135 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001136 &firstOp,
1137 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001138
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001139 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001140
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001141 bool first = true;
1142 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001143
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001144 SkRegion::Op op = clip->fOp;
1145 if (first) {
1146 first = false;
1147 op = firstOp;
1148 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001149
1150 if (SkRegion::kIntersect_Op == op ||
1151 SkRegion::kReverseDifference_Op == op) {
1152 // Intersect and reverse difference require modifying pixels
1153 // outside of the geometry that is being "drawn". In both cases
1154 // we erase all the pixels outside of the geometry but
1155 // leave the pixels inside the geometry alone. For reverse
1156 // difference we invert all the pixels before clearing the ones
1157 // outside the geometry.
1158 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001159 SkRect temp;
1160 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001161 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1162 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001163
1164 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001165 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001166 }
1167
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001168 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001169
1170 // convert the rect to a path so we can invert the fill
1171 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001172 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001173
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001174 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001175 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001176 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001177 } else if (NULL != clip->fPath) {
1178 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001179 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001180 invert_fill(get_path_fill(*clip->fPath)),
1181 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001182 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001183 }
1184
1185 continue;
1186 }
1187
1188 // The other ops (union, xor, diff) only affect pixels inside
1189 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001190 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001191
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001192 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001193 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001194 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001195
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001196 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001197 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001198 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001199 get_path_fill(*clip->fPath),
1200 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001201 }
1202 }
1203
robertphillips@google.comfa662942012-05-17 12:20:22 +00001204 // Because we are using the scratch texture cache, "accum" may be
1205 // larger than expected and have some cruft in the areas we aren't using.
1206 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001207 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001208
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001209 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001210
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001211 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001212
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001213 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001214 return true;
1215}
1216
robertphillips@google.comf294b772012-04-27 14:29:26 +00001217////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001218void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001219 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001220}