blob: de8f323ce67fa02c46716d02fc0b9d14c3ed2dcd [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
10#include "GrGpu.h"
11#include "GrRenderTarget.h"
12#include "GrStencilBuffer.h"
13#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000014#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000015#include "SkRasterClip.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000016#include "GrAAConvexPathRenderer.h"
17#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000019#include "GrCacheID.h"
20
21GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000022
23//#define GR_AA_CLIP 1
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000024//#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000025
robertphillips@google.comf8d904a2012-07-31 12:18:16 +000026GrClipMaskCache::GrClipMaskCache()
27 : fContext(NULL)
28 , fStack(sizeof(GrClipStackFrame)) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029 // We need an initial frame to capture the clip state prior to
robertphillips@google.comf8d904a2012-07-31 12:18:16 +000030 // any pushes
31 SkNEW_PLACEMENT(fStack.push_back(), GrClipStackFrame);
32}
33
34void GrClipMaskCache::push() {
35 SkNEW_PLACEMENT(fStack.push_back(), GrClipStackFrame);
36}
37
robertphillips@google.comf294b772012-04-27 14:29:26 +000038////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040// set up the draw state to enable the aa clipping mask. Besides setting up the
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041// sampler matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042void setup_drawstate_aaclip(GrGpu* gpu,
43 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000044 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045 GrDrawState* drawState = gpu->drawState();
46 GrAssert(drawState);
47
48 static const int maskStage = GrPaint::kTotalStages+1;
49
50 GrMatrix mat;
51 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000053 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000054 mat.preConcat(drawState->getViewMatrix());
55
bsalomon@google.comb8670992012-07-25 21:27:09 +000056 drawState->sampler(maskStage)->reset(mat);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000057
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000058 drawState->createTextureEffect(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000059}
60
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000061bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000062 GrGpu* gpu,
63 const SkPath& path,
64 GrPathFill fill,
65 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000066 // last (false) parameter disallows use of the SW path renderer
67 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
68}
69
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000070GrPathFill get_path_fill(const SkPath& path) {
71 switch (path.getFillType()) {
72 case SkPath::kWinding_FillType:
73 return kWinding_GrPathFill;
74 case SkPath::kEvenOdd_FillType:
75 return kEvenOdd_GrPathFill;
76 case SkPath::kInverseWinding_FillType:
77 return kInverseWinding_GrPathFill;
78 case SkPath::kInverseEvenOdd_FillType:
79 return kInverseEvenOdd_GrPathFill;
80 default:
81 GrCrash("Unsupported path fill in clip.");
82 return kWinding_GrPathFill; // suppress warning
83 }
84}
85
robertphillips@google.comb99225c2012-07-24 18:20:10 +000086/**
87 * Does any individual clip in 'clipIn' use anti-aliasing?
88 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000089bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000090
robertphillips@google.com641f8b12012-07-31 19:15:58 +000091 SkClipStack::Iter iter;
92 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000093
robertphillips@google.com641f8b12012-07-31 19:15:58 +000094 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comb99225c2012-07-24 18:20:10 +000095 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
96 NULL != clip;
97 clip = iter.next()) {
98
99 if (clip->fDoAA) {
100 return true;
101 }
102 }
103
104 return false;
105}
106
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000107}
108
robertphillips@google.comfa662942012-05-17 12:20:22 +0000109/*
110 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
111 * will be used on any element. If so, it returns true to indicate that the
112 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
113 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000114bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000115
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000116 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000117 // a clip gets complex enough it can just be done in SW regardless
118 // of whether it would invoke the GrSoftwarePathRenderer.
119 bool useSW = false;
120
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000121 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
122 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000123
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000124 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
125 NULL != clip;
126 clip = iter.next()) {
127
128 if (SkRegion::kReplace_Op == clip->fOp) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000129 // Everything before a replace op can be ignored so start
130 // afresh w.r.t. determining if any element uses the SW path
131 useSW = false;
132 }
133
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000134 // rects can always be drawn directly w/o using the software path
135 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000136 if (NULL != clip->fPath &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000137 path_needs_SW_renderer(this->getContext(), fGpu,
138 *clip->fPath,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000139 get_path_fill(*clip->fPath),
140 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000141 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000142 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000143 }
144
145 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000146}
147
robertphillips@google.comf294b772012-04-27 14:29:26 +0000148////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000149// sort out what kind of clip mask needs to be created: alpha, stencil,
150// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000151bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000152 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000153
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000154 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000155 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000156 fGpu->disableScissor();
157 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000158 return true;
159 }
160
161 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000162 // GrDrawTarget should have filtered this for us
163 GrAssert(NULL != rt);
164
robertphillips@google.com7b112892012-07-31 15:18:21 +0000165 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000166 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000167
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000168 clipDataIn->getConservativeBounds(rt, &devClipBounds,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000169 &isIntersectionOfRects);
170 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000171 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000172 }
173
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000174 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000175
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000176#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000177 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000178 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000179 // in software (this will only happen if the clip mask is anti-aliased
180 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000181 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000182 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000183 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000184 // The clip geometry is complex enough that it will be more
185 // efficient to create it entirely in software
186 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000187 GrIRect devBound;
188 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
189 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000190 fGpu->disableScissor();
191 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000192 return true;
193 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000194
195 // if SW clip mask creation fails fall through to the other
196 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000197 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000198#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000199
robertphillips@google.comf294b772012-04-27 14:29:26 +0000200#if GR_AA_CLIP
201 // If MSAA is enabled use the (faster) stencil path for AA clipping
202 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000203 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000204 // Since we are going to create a destination texture of the correct
205 // size for the mask (rather than being bound by the size of the
206 // render target) we aren't going to use scissoring like the stencil
207 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000208 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000209 GrIRect devBound;
210 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
211 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000212 fGpu->disableScissor();
213 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000214 return true;
215 }
216
217 // if alpha clip mask creation fails fall through to the stencil
218 // buffer method
219 }
220#endif // GR_AA_CLIP
221
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000222 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000223 // an antialiased clip couldn't be created. In either case, free up
224 // the texture in the antialiased mask cache.
225 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000226 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000227 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000228 // AA cache.
229 fAACache.reset();
230
bsalomon@google.coma3201942012-06-21 19:58:20 +0000231 // If the clip is a rectangle then just set the scissor. Otherwise, create
232 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000233 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000234 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000235 this->setGpuStencil();
236 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000237 }
238
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000239 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000240 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000241 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000242
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000243 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000244 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000245 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000246 // This must occur after createStencilClipMask. That function may change
247 // the scissor. Also, it only guarantees that the stencil mask is correct
248 // within the bounds it was passed, so we must use both stencil and scissor
249 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000250 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000251 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000252 return true;
253}
254
255#define VISUALIZE_COMPLEX_CLIP 0
256
257#if VISUALIZE_COMPLEX_CLIP
258 #include "GrRandom.h"
259 GrRandom gRandom;
260 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
261#else
262 #define SET_RANDOM_COLOR
263#endif
264
265namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000266/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000267 * Does "canvContainer" contain "devContainee"? If either is empty then
268 * no containment is possible. "canvContainer" is in canvas coordinates while
269 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000270 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000271 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000272bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000273 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000274 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000275 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000276 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000277 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000278 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000279 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000280}
281
robertphillips@google.comf294b772012-04-27 14:29:26 +0000282////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000283// determines how many elements at the head of the clip can be skipped and
284// whether the initial clear should be to the inside- or outside-the-clip value,
285// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000286const SkClipStack::Iter::Clip* process_initial_clip_elements(
287 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000288 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000290 SkRegion::Op* firstOp,
291 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000292
293 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000294
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000295 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000296 // processed the clip is entirely open. However, depending on the
297 // first set op we may prefer to clear to 0 for performance. We may
298 // also be able to skip the initial clip paths/rects. We loop until
299 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 bool done = false;
301 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000302
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000303 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000304
305 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
306 NULL != clip && !done;
307 clip = iter->next()) {
308 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000309 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000310 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000311 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000312 *clearToInside = false;
313 done = true;
314 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000315 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000316 // if this element contains the entire bounds then we
317 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000318 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000319 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320 break;
321 }
322 // if everything is initially clearToInside then intersect is
323 // same as clear to 0 and treat as a replace. Otherwise,
324 // set stays empty.
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 *clearToInside = false;
328 done = true;
329 }
330 break;
331 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000332 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000333 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000334 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 // clearToInside
336 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000337 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000338 done = true;
339 }
340 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000341 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000342 // xor is same as difference or replace both of which
343 // can be 1-pass instead of 2 for xor.
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 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000347 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000348 }
349 done = true;
350 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000351 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000352 // if all pixels are clearToInside then we have to process the
353 // difference, otherwise it has no effect and all pixels
354 // remain outside.
355 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000356 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000357 done = true;
358 }
359 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000360 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000361 // if all pixels are clearToInside then reverse difference
362 // produces empty set. Otherise it is same as replace
363 if (*clearToInside) {
364 *clearToInside = false;
365 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000366 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000367 done = true;
368 }
369 break;
370 default:
371 GrCrash("Unknown set op.");
372 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000373
374 if (done) {
375 // we need to break out here (rather than letting the test in
376 // the loop do it) since backing up the iterator is very expensive
377 break;
378 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000379 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000380 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000381}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000383}
384
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385namespace {
386
387////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000388// set up the OpenGL blend function to perform the specified
389// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000390void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391
392 switch (op) {
393 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000394 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000395 break;
396 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000397 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000398 break;
399 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000400 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000401 break;
402 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000403 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000404 break;
405 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000406 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000407 break;
408 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000409 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000410 break;
411 default:
412 GrAssert(false);
413 break;
414 }
415}
416
robertphillips@google.comf294b772012-04-27 14:29:26 +0000417////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000418bool draw_path_in_software(GrContext* context,
419 GrGpu* gpu,
420 const SkPath& path,
421 GrPathFill fill,
422 bool doAA,
423 const GrIRect& resultBounds) {
424
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000425 SkAutoTUnref<GrTexture> texture(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000426 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
427 resultBounds, fill,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000428 doAA, NULL));
429 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000430 return false;
431 }
432
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000433 // The ClipMaskManager accumulates the clip mask in the UL corner
434 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000435
bsalomon@google.come3d32162012-07-20 13:37:06 +0000436 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000437
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000438 GrAssert(!GrIsFillInverted(fill));
439 return true;
440}
441
442
443////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000444bool draw_path(GrContext* context,
445 GrGpu* gpu,
446 const SkPath& path,
447 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000448 bool doAA,
449 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000450
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000451 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000452 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000453 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000454 }
455
bsalomon@google.come3d32162012-07-20 13:37:06 +0000456 pr->drawPath(path, fill, NULL, gpu, doAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000457 return true;
458}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000459
robertphillips@google.com7b112892012-07-31 15:18:21 +0000460// 'rect' enters in device coordinates and leaves in canvas coordinates
461void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
462 GrAssert(NULL != rect);
463
464 rect->fLeft += SkIntToScalar(origin.fX);
465 rect->fTop += SkIntToScalar(origin.fY);
466 rect->fRight += SkIntToScalar(origin.fX);
467 rect->fBottom += SkIntToScalar(origin.fY);
468}
469
robertphillips@google.com72176b22012-05-23 13:19:12 +0000470}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000471
472////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000473bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000474 const SkClipStack::Iter::Clip* clip,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000475 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000476 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000477 GrAssert(NULL != drawState);
478
479 drawState->setRenderTarget(target->asRenderTarget());
480
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000481 if (NULL != clip->fRect) {
482 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000483 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000484 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000485 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000486 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000487 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000488 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000489 } else if (NULL != clip->fPath) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000490 return draw_path(this->getContext(), fGpu,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000491 *clip->fPath,
492 get_path_fill(*clip->fPath),
493 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000494 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495 }
496 return true;
497}
498
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000499void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000500 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000501 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000502 GrAssert(NULL != drawState);
503
504 // no AA here since it is encoded in the texture
505 drawState->setRenderTarget(target->asRenderTarget());
506
507 GrMatrix sampleM;
508 sampleM.setIDiv(texture->width(), texture->height());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000509
bsalomon@google.comb8670992012-07-25 21:27:09 +0000510 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000511 drawState->createTextureEffect(0, texture);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000512
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000513 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
robertphillips@google.comf105b102012-05-14 12:18:26 +0000514 SkIntToScalar(target->height()));
515
bsalomon@google.come3d32162012-07-20 13:37:06 +0000516 fGpu->drawSimpleRect(rect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000517
tomhudson@google.com676e6602012-07-10 17:21:48 +0000518 drawState->disableStage(0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000519}
520
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000521// get a texture to act as a temporary buffer for AA clip boolean operations
522// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000523void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000524 GrAutoScratchTexture* temp) {
525 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000526 // we've already allocated the temp texture
527 return;
528 }
529
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000530 GrTextureDesc desc;
531 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
532 desc.fWidth = bounds.width();
533 desc.fHeight = bounds.height();
534 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000535
robertphillips@google.com2c756812012-05-22 20:28:23 +0000536 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000537}
538
robertphillips@google.comf105b102012-05-14 12:18:26 +0000539
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000540void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000541 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000542 // Since we are setting up the cache we know the last lookup was a miss
543 // Free up the currently cached mask so it can be reused
544 fAACache.reset();
545
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000546 GrTextureDesc desc;
547 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
548 desc.fWidth = bounds.width();
549 desc.fHeight = bounds.height();
550 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000551
552 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000553}
554
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000555////////////////////////////////////////////////////////////////////////////////
556// Shared preamble between gpu and SW-only AA clip mask creation paths.
557// Handles caching, determination of clip mask bound & allocation (if needed)
558// of the result texture
559// 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 +0000560bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000561 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000562 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000563 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000564 GrAssert(origDrawState->isClipState());
565
566 GrRenderTarget* rt = origDrawState->getRenderTarget();
567 GrAssert(NULL != rt);
568
robertphillips@google.comf294b772012-04-27 14:29:26 +0000569 // unlike the stencil path the alpha path is not bound to the size of the
570 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000571 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com7b112892012-07-31 15:18:21 +0000572 GrIRect devClipBounds;
573 clipDataIn.getConservativeBounds(rt, &devClipBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000574
575 // need to outset a pixel since the standard bounding box computation
576 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com7b112892012-07-31 15:18:21 +0000577 devClipBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000578
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000579 // TODO: make sure we don't outset if bounds are still 0,0 @ min
580
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000581 if (fAACache.canReuse(*clipDataIn.fClipStack,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000582 devClipBounds.width(),
583 devClipBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000584 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000585 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000586 return true;
587 }
588
robertphillips@google.com7b112892012-07-31 15:18:21 +0000589 this->setupCache(*clipDataIn.fClipStack, devClipBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000590
robertphillips@google.com7b112892012-07-31 15:18:21 +0000591 *devResultBounds = devClipBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000592 return false;
593}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000594
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000595////////////////////////////////////////////////////////////////////////////////
596// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000597bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000598 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000599 GrIRect *devResultBounds) {
600 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000601 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
602
robertphillips@google.com7b112892012-07-31 15:18:21 +0000603 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000604 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000605 return true;
606 }
607
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000608 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
609
robertphillips@google.comf105b102012-05-14 12:18:26 +0000610 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000611 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000612 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613 return false;
614 }
615
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000616 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
617 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000618
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000619 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000620
robertphillips@google.com7b112892012-07-31 15:18:21 +0000621 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000622 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000623 // if we were able to trim down the size of the mask we need to
robertphillips@google.comf294b772012-04-27 14:29:26 +0000624 // offset the paths & rects that will be used to compute it
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000625 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000626 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000627 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000628 }
629
630 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000631 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
632
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000633 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000634 SkClipStack::Iter::kBottom_IterStart);
635 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000636 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000637 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000638 &firstOp,
639 clipDataIn);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000640
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000641 fGpu->clear(NULL,
642 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000643 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000644
robertphillips@google.comf105b102012-05-14 12:18:26 +0000645 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000646 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647 // walk through each clip element and perform its set op
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000648 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000649
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000650 SkRegion::Op op = clip->fOp;
651 if (first) {
652 first = false;
653 op = firstOp;
654 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000655
656 if (SkRegion::kReplace_Op == op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000657 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000658 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000659
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000660 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000661 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000662
663 } else if (SkRegion::kReverseDifference_Op == op ||
664 SkRegion::kIntersect_Op == op) {
665 // there is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000666 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000667 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000668 continue;
669 }
670
robertphillips@google.com7b112892012-07-31 15:18:21 +0000671 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000672 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000673 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000674 return false;
675 }
676
robertphillips@google.comf294b772012-04-27 14:29:26 +0000677 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000678 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000679
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000680 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000681 this->drawClipShape(temp.texture(), clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000682
683 // TODO: rather than adding these two translations here
684 // compute the bounding box needed to render the texture
685 // into temp
robertphillips@google.com7b112892012-07-31 15:18:21 +0000686 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000687 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
688 // In order for the merge of the temp clip into the accumulator
689 // to work we need to disable the translation
690 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000691 }
692
693 // Now draw into the accumulator using the real operation
694 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000695 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000696 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000697
robertphillips@google.com7b112892012-07-31 15:18:21 +0000698 if (0 != devResultBounds->fTop || 0 != devResultBounds->fLeft ||
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000699 0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
700 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000701 SkIntToScalar(-devResultBounds->fLeft-clipDataIn.fOrigin.fX),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000702 SkIntToScalar(-devResultBounds->fTop-clipDataIn.fOrigin.fY));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000703 }
704
705 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000706 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000707 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000708 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000709 this->drawClipShape(accum, clip, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000710 }
711 }
712
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000713 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000714 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000715 return true;
716}
717
718////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000719// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000720// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000721bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000722 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000724 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000726 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000727 GrAssert(drawState->isClipState());
728
729 GrRenderTarget* rt = drawState->getRenderTarget();
730 GrAssert(NULL != rt);
731
732 // TODO: dynamically attach a SB when needed.
733 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
734 if (NULL == stencilBuffer) {
735 return false;
736 }
737
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000738 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000740 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741
742 // we set the current clip to the bounds so that our recursive
743 // draws are scissored to them. We use the copy of the complex clip
744 // we just stashed on the SB to render from. We set it back after
745 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000746 const GrClipData* oldClipData = fGpu->getClip();
747
robertphillips@google.com7b112892012-07-31 15:18:21 +0000748 // The origin of 'newClipData' is (0, 0) so it is okay to place
749 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000750 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000751 GrClipData newClipData;
752 newClipData.fClipStack = &newClipStack;
753
754 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000756 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
757 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000758 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000759 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000760
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000761 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000762 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000763 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000764 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000765 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000766 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000767 }
768
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000769#if !VISUALIZE_COMPLEX_CLIP
770 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
771#endif
772
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000773 int clipBit = stencilBuffer->bits();
774 SkASSERT((clipBit <= 16) &&
775 "Ganesh only handles 16b or smaller stencil buffers");
776 clipBit = (1 << (clipBit-1));
777
robertphillips@google.com7b112892012-07-31 15:18:21 +0000778 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000779
780 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000781 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
782
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000783 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000784 SkClipStack::Iter::kBottom_IterStart);
785 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000786 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000787 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000788 &firstOp,
789 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790
robertphillips@google.com7b112892012-07-31 15:18:21 +0000791 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000792 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793
794 // walk through each clip element and perform its set op
795 // with the existing clip.
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000796 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000797 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000798 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000799 // enabled at bottom of loop
800 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000801 // if the target is MSAA then we want MSAA enabled when the clip is soft
802 if (rt->isMultisampled()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000803 if (clip->fDoAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000804 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
805 } else {
806 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
807 }
808 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000809
tomhudson@google.com8afae612012-08-14 15:03:35 +0000810 // Can the clip element be drawn directly to the stencil buffer
811 // with a non-inverted fill rule without extra passes to
812 // resolve in/out status?
813 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000814
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000815 SkRegion::Op op = clip->fOp;
816 if (first) {
817 first = false;
818 op = firstOp;
819 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000820
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000821 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000822 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000823 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000825 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000826 fillInverted = false;
827 // there is no point in intersecting a screen filling
828 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000829 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000830 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831 continue;
832 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000833 } else {
834 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000835 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000836 fillInverted = GrIsFillInverted(fill);
837 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000838 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000839 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000840 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000841 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000842 return false;
843 }
844 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000845 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000846 }
847
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000848 int passes;
849 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
850
851 bool canDrawDirectToClip; // Given the renderer, the element,
852 // fill rule, and set operation can
853 // we render the element directly to
854 // stencil bit used for clipping.
855 canDrawDirectToClip =
856 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000857 canRenderDirectToStencil,
858 clipBit,
859 fillInverted,
860 &passes,
861 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000862
863 // draw the element to the client stencil bits if necessary
864 if (!canDrawDirectToClip) {
865 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
866 kIncClamp_StencilOp,
867 kIncClamp_StencilOp,
868 kAlways_StencilFunc,
869 0xffff,
870 0x0000,
871 0xffff);
872 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000873 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000874 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000875 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000876 } else {
877 if (canRenderDirectToStencil) {
878 *drawState->stencil() = gDrawToStencil;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000879 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000880 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000881 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000882 }
883 }
884 }
885
886 // now we modify the clip bit by rendering either the clip
887 // element directly or a bounding rect of the entire clip.
888 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
889 for (int p = 0; p < passes; ++p) {
890 *drawState->stencil() = stencilSettings[p];
891 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000892 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000893 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000894 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000895 } else {
896 SET_RANDOM_COLOR
bsalomon@google.come3d32162012-07-20 13:37:06 +0000897 pr->drawPath(*clipPath, fill, NULL, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000898 }
899 } else {
900 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000901 // 'devClipBounds' is already in device coordinates so the
902 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000903 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000904 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000905 GrRect canvClipBounds;
906 canvClipBounds.set(devClipBounds);
907 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
908 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000909 }
910 }
911 }
912 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000913 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000914 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000915 // set this last because recursive draws may overwrite it back to kNone.
916 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
917 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000918 return true;
919}
920
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000921
bsalomon@google.com411dad02012-06-05 20:24:20 +0000922// mapping of clip-respecting stencil funcs to normal stencil funcs
923// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000924static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000925 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
926 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
927 // In the Clip Funcs
928 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
929 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
930 kLess_StencilFunc, // kLessIfInClip_StencilFunc
931 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
932 // Special in the clip func that forces user's ref to be 0.
933 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
934 // make ref 0 and do normal nequal.
935 },
936 {// Stencil-Clipping is ENABLED
937 // In the Clip Funcs
938 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
939 // eq stencil clip bit, mask
940 // out user bits.
941
942 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
943 // add stencil bit to mask and ref
944
945 kLess_StencilFunc, // kLessIfInClip_StencilFunc
946 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
947 // for both of these we can add
948 // the clip bit to the mask and
949 // ref and compare as normal
950 // Special in the clip func that forces user's ref to be 0.
951 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
952 // make ref have only the clip bit set
953 // and make comparison be less
954 // 10..0 < 1..user_bits..
955 }
956};
957
bsalomon@google.coma3201942012-06-21 19:58:20 +0000958namespace {
959// Sets the settings to clip against the stencil buffer clip while ignoring the
960// client bits.
961const GrStencilSettings& basic_apply_stencil_clip_settings() {
962 // stencil settings to use when clip is in stencil
963 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
964 kKeep_StencilOp,
965 kKeep_StencilOp,
966 kAlwaysIfInClip_StencilFunc,
967 0x0000,
968 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000969 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000970 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
971}
972}
973
974void GrClipMaskManager::setGpuStencil() {
975 // We make two copies of the StencilSettings here (except in the early
976 // exit scenario. One copy from draw state to the stack var. Then another
977 // from the stack var to the gpu. We could make this class hold a ptr to
978 // GrGpu's fStencilSettings and eliminate the stack copy here.
979
980 const GrDrawState& drawState = fGpu->getDrawState();
981
982 // use stencil for clipping if clipping is enabled and the clip
983 // has been written into the stencil.
984 GrClipMaskManager::StencilClipMode clipMode;
985 if (this->isClipInStencil() && drawState.isClipState()) {
986 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
987 // We can't be modifying the clip and respecting it at the same time.
988 GrAssert(!drawState.isStateFlagEnabled(
989 GrGpu::kModifyStencilClip_StateBit));
990 } else if (drawState.isStateFlagEnabled(
991 GrGpu::kModifyStencilClip_StateBit)) {
992 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
993 } else {
994 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
995 }
996
997 GrStencilSettings settings;
998 // The GrGpu client may not be using the stencil buffer but we may need to
999 // enable it in order to respect a stencil clip.
1000 if (drawState.getStencil().isDisabled()) {
1001 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1002 settings = basic_apply_stencil_clip_settings();
1003 } else {
1004 fGpu->disableStencil();
1005 return;
1006 }
1007 } else {
1008 settings = drawState.getStencil();
1009 }
1010
1011 // TODO: dynamically attach a stencil buffer
1012 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001013 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001014 drawState.getRenderTarget()->getStencilBuffer();
1015 if (NULL != stencilBuffer) {
1016 stencilBits = stencilBuffer->bits();
1017 }
1018
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001019 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
1020 !settings.usesWrapOp());
1021 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001022 this->adjustStencilParams(&settings, clipMode, stencilBits);
1023 fGpu->setStencilSettings(settings);
1024}
1025
1026void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1027 StencilClipMode mode,
1028 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001029 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001030
1031 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001032 // We assume that this clip manager itself is drawing to the GrGpu and
1033 // has already setup the correct values.
1034 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001035 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001036
bsalomon@google.com411dad02012-06-05 20:24:20 +00001037 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1038 unsigned int userBits = clipBit - 1;
1039
bsalomon@google.coma3201942012-06-21 19:58:20 +00001040 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
1041 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001042
bsalomon@google.coma3201942012-06-21 19:58:20 +00001043 bool finished = false;
1044 while (!finished) {
1045 GrStencilFunc func = settings->func(face);
1046 uint16_t writeMask = settings->writeMask(face);
1047 uint16_t funcMask = settings->funcMask(face);
1048 uint16_t funcRef = settings->funcRef(face);
1049
1050 GrAssert((unsigned) func < kStencilFuncCount);
1051
1052 writeMask &= userBits;
1053
1054 if (func >= kBasicStencilFuncCount) {
1055 int respectClip = kRespectClip_StencilClipMode == mode;
1056 if (respectClip) {
1057 // The GrGpu class should have checked this
1058 GrAssert(this->isClipInStencil());
1059 switch (func) {
1060 case kAlwaysIfInClip_StencilFunc:
1061 funcMask = clipBit;
1062 funcRef = clipBit;
1063 break;
1064 case kEqualIfInClip_StencilFunc:
1065 case kLessIfInClip_StencilFunc:
1066 case kLEqualIfInClip_StencilFunc:
1067 funcMask = (funcMask & userBits) | clipBit;
1068 funcRef = (funcRef & userBits) | clipBit;
1069 break;
1070 case kNonZeroIfInClip_StencilFunc:
1071 funcMask = (funcMask & userBits) | clipBit;
1072 funcRef = clipBit;
1073 break;
1074 default:
1075 GrCrash("Unknown stencil func");
1076 }
1077 } else {
1078 funcMask &= userBits;
1079 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001080 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001081 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001082 gSpecialToBasicStencilFunc[respectClip];
1083 func = table[func - kBasicStencilFuncCount];
1084 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001085 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001086 funcMask &= userBits;
1087 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001088 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001089
1090 settings->setFunc(face, func);
1091 settings->setWriteMask(face, writeMask);
1092 settings->setFuncMask(face, funcMask);
1093 settings->setFuncRef(face, funcRef);
1094
1095 if (GrStencilSettings::kFront_Face == face) {
1096 face = GrStencilSettings::kBack_Face;
1097 finished = !twoSided;
1098 } else {
1099 finished = true;
1100 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001101 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001102 if (!twoSided) {
1103 settings->copyFrontSettingsToBack();
1104 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001105}
1106
1107////////////////////////////////////////////////////////////////////////////////
1108
robertphillips@google.comfa662942012-05-17 12:20:22 +00001109namespace {
1110
1111GrPathFill invert_fill(GrPathFill fill) {
1112 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001113 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1114 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1115 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1116 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1117 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001118 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001119 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1120 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1121 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1122 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1123 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1124 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001125 return gInvertedFillTable[fill];
1126}
1127
1128}
1129
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001130bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001131 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001132 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001133 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001134
robertphillips@google.com7b112892012-07-31 15:18:21 +00001135 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001136 return true;
1137 }
1138
robertphillips@google.comf105b102012-05-14 12:18:26 +00001139 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001140 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001141 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001142 return false;
1143 }
1144
robertphillips@google.com2c756812012-05-22 20:28:23 +00001145 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001146
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001147 GrMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001148 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001149 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001150 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001151
robertphillips@google.comfa662942012-05-17 12:20:22 +00001152 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001153 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1154
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001155 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001156 SkClipStack::Iter::kBottom_IterStart);
1157 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001158 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001159 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001160 &firstOp,
1161 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001162
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001163 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001164
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001165 bool first = true;
1166 for ( ; NULL != clip; clip = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001167
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001168 SkRegion::Op op = clip->fOp;
1169 if (first) {
1170 first = false;
1171 op = firstOp;
1172 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001173
1174 if (SkRegion::kIntersect_Op == op ||
1175 SkRegion::kReverseDifference_Op == op) {
1176 // Intersect and reverse difference require modifying pixels
1177 // outside of the geometry that is being "drawn". In both cases
1178 // we erase all the pixels outside of the geometry but
1179 // leave the pixels inside the geometry alone. For reverse
1180 // difference we invert all the pixels before clearing the ones
1181 // outside the geometry.
1182 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001183 SkRect temp;
1184 temp.set(*devResultBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001185
1186 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001187 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001188 }
1189
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001190 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001191
1192 // convert the rect to a path so we can invert the fill
1193 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001194 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001195
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001196 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001197 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001198 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001199 } else if (NULL != clip->fPath) {
1200 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001201 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001202 invert_fill(get_path_fill(*clip->fPath)),
1203 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001204 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001205 }
1206
1207 continue;
1208 }
1209
1210 // The other ops (union, xor, diff) only affect pixels inside
1211 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001212 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001213
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001214 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001215 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001216 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001217
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001218 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001219 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001220 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001221 get_path_fill(*clip->fPath),
1222 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001223 }
1224 }
1225
robertphillips@google.comfa662942012-05-17 12:20:22 +00001226 // Because we are using the scratch texture cache, "accum" may be
1227 // larger than expected and have some cruft in the areas we aren't using.
1228 // Clear it out.
1229
1230 // TODO: need a simpler way to clear the texture - can we combine
1231 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001232 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001233 GrAssert(NULL != drawState);
1234 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001235 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001236 // can't leave the accum bound as a rendertarget
1237 drawState->setRenderTarget(temp);
1238
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001239 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001240
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001241 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001242
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001243 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001244 return true;
1245}
1246
robertphillips@google.comf294b772012-04-27 14:29:26 +00001247////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001248void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001249 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001250}