blob: 5dfc6faa35fa4c724504110127a3905657868991 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000010#include "effects/GrTextureDomainEffect.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000011#include "GrGpu.h"
12#include "GrRenderTarget.h"
13#include "GrStencilBuffer.h"
14#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000015#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000016#include "SkRasterClip.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000017#include "SkStroke.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000018#include "GrAAConvexPathRenderer.h"
19#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000021#include "GrCacheID.h"
22
23GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000024
robertphillips@google.comba998f22012-10-12 11:33:56 +000025#define GR_AA_CLIP 1
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +000026#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000027
bsalomon@google.com8182fa02012-12-04 14:06:06 +000028typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000029
30////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000031namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000033// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034void setup_drawstate_aaclip(GrGpu* gpu,
35 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000036 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037 GrDrawState* drawState = gpu->drawState();
38 GrAssert(drawState);
39
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000040 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000041
bsalomon@google.comb9086a02012-11-01 18:02:54 +000042 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000045 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000046 mat.preConcat(drawState->getViewMatrix());
47
bsalomon@google.com08283af2012-10-26 13:01:20 +000048 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000049
50 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
51 drawState->stage(kMaskStage)->setEffect(
52 GrTextureDomainEffect::Create(result,
53 mat,
54 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
55 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000056}
57
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000058bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000059 GrGpu* gpu,
60 const SkPath& path,
sugoi@google.com12b4e272012-12-06 20:13:11 +000061 const SkStroke& stroke,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000062 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000063 // last (false) parameter disallows use of the SW path renderer
sugoi@google.com12b4e272012-12-06 20:13:11 +000064 return NULL == context->getPathRenderer(path, stroke, gpu, doAA, false);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000065}
66
robertphillips@google.comb99225c2012-07-24 18:20:10 +000067/**
68 * Does any individual clip in 'clipIn' use anti-aliasing?
69 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000070bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000071
robertphillips@google.com641f8b12012-07-31 19:15:58 +000072 SkClipStack::Iter iter;
73 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000074
bsalomon@google.com8182fa02012-12-04 14:06:06 +000075 const Element* element = NULL;
76 for (element = iter.skipToTopmost(SkRegion::kReplace_Op);
77 NULL != element;
78 element = iter.next()) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000079
bsalomon@google.com8182fa02012-12-04 14:06:06 +000080 if (element->isAA()) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000081 return true;
82 }
83 }
84
85 return false;
86}
87
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000088}
89
robertphillips@google.comfa662942012-05-17 12:20:22 +000090/*
91 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
92 * will be used on any element. If so, it returns true to indicate that the
93 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
94 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000095bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000096
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000097 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000098 // a clip gets complex enough it can just be done in SW regardless
99 // of whether it would invoke the GrSoftwarePathRenderer.
100 bool useSW = false;
101
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000102 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000103 const Element* element = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104
sugoi@google.com12b4e272012-12-06 20:13:11 +0000105 SkStroke stroke;
106 stroke.setDoFill(true);
107
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000108 for (element = iter.skipToTopmost(SkRegion::kReplace_Op);
109 NULL != element;
110 element = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000111
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000112 // rects can always be drawn directly w/o using the software path
113 // so only paths need to be checked
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000114 if (Element::kPath_Type == element->getType() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000115 path_needs_SW_renderer(this->getContext(), fGpu,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000116 element->getPath(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000117 stroke,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000118 element->isAA())) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000119 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000120 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000121 }
122
123 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000124}
125
robertphillips@google.comf294b772012-04-27 14:29:26 +0000126////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000127// sort out what kind of clip mask needs to be created: alpha, stencil,
128// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000129bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000130 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000131
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000132 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000133 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000134 fGpu->disableScissor();
135 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000136 return true;
137 }
138
139 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000140 // GrDrawTarget should have filtered this for us
141 GrAssert(NULL != rt);
142
robertphillips@google.com7b112892012-07-31 15:18:21 +0000143 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000144 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000145
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000146 clipDataIn->getConservativeBounds(rt, &devClipBounds, &isIntersectionOfRects);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000147 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000148 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000149 }
150
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000151#if GR_SW_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000152 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000153
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000154 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000155 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000156 // in software (this will only happen if the clip mask is anti-aliased
157 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000158 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000159 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000160 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000161 // The clip geometry is complex enough that it will be more
162 // efficient to create it entirely in software
163 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000164 GrIRect devBound;
165 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
166 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000167 fGpu->disableScissor();
168 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000169 return true;
170 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000171
172 // if SW clip mask creation fails fall through to the other
173 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000174 }
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000175#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000176
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000177#if GR_AA_CLIP
robertphillips@google.comf294b772012-04-27 14:29:26 +0000178 // If MSAA is enabled use the (faster) stencil path for AA clipping
179 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000180 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000181 // Since we are going to create a destination texture of the correct
182 // size for the mask (rather than being bound by the size of the
183 // render target) we aren't going to use scissoring like the stencil
184 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000185 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000186 GrIRect devBound;
187 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
188 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000189 fGpu->disableScissor();
190 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000191 return true;
192 }
193
194 // if alpha clip mask creation fails fall through to the stencil
195 // buffer method
196 }
197#endif // GR_AA_CLIP
198
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000199 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000200 // an antialiased clip couldn't be created. In either case, free up
201 // the texture in the antialiased mask cache.
202 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000203 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000204 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000205 // AA cache.
206 fAACache.reset();
207
bsalomon@google.coma3201942012-06-21 19:58:20 +0000208 // If the clip is a rectangle then just set the scissor. Otherwise, create
209 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000210 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000211 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000212 this->setGpuStencil();
213 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000214 }
215
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000216 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000218 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000219
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000220 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000221 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000222 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000223 // This must occur after createStencilClipMask. That function may change
224 // the scissor. Also, it only guarantees that the stencil mask is correct
225 // within the bounds it was passed, so we must use both stencil and scissor
226 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000227 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000228 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000229 return true;
230}
231
232#define VISUALIZE_COMPLEX_CLIP 0
233
234#if VISUALIZE_COMPLEX_CLIP
tfarina@chromium.org223137f2012-11-21 22:38:36 +0000235 #include "SkRandom.h"
236 SkRandom gRandom;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000237 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
238#else
239 #define SET_RANDOM_COLOR
240#endif
241
242namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000243/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000244 * Does "canvContainer" contain "devContainee"? If either is empty then
245 * no containment is possible. "canvContainer" is in canvas coordinates while
246 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000247 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000248 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000249bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000250 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000251 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000252 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000253 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000254 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000255 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000256 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000257}
258
robertphillips@google.comf294b772012-04-27 14:29:26 +0000259////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000260// determines how many elements at the head of the clip can be skipped and
261// whether the initial clear should be to the inside- or outside-the-clip value,
262// and what op should be used to draw the first element that isn't skipped.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000263const SkClipStack::Element* process_initial_clip_elements(
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000264 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000265 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000266 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000267 SkRegion::Op* firstOp,
268 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000269
270 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000271
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000272 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000273 // processed the clip is entirely open. However, depending on the
274 // first set op we may prefer to clear to 0 for performance. We may
275 // also be able to skip the initial clip paths/rects. We loop until
276 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000277 bool done = false;
278 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000279
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000280 const SkClipStack::Element* element = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000281
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000282 for (element = iter->skipToTopmost(SkRegion::kReplace_Op);
283 NULL != element && !done;
284 element = iter->next()) {
285 switch (element->getOp()) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000286 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000287 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000288 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289 *clearToInside = false;
290 done = true;
291 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000292 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000293 // if this element contains the entire bounds then we
294 // can skip it.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000295 if (Element::kRect_Type == element->getType() &&
296 contains(element->getRect(), devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000297 break;
298 }
299 // if everything is initially clearToInside then intersect is
300 // same as clear to 0 and treat as a replace. Otherwise,
301 // set stays empty.
302 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000303 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000304 *clearToInside = false;
305 done = true;
306 }
307 break;
308 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000309 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000310 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000311 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000312 // clearToInside
313 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000314 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000315 done = true;
316 }
317 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000318 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000319 // xor is same as difference or replace both of which
320 // can be 1-pass instead of 2 for xor.
321 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000322 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000323 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000324 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000325 }
326 done = true;
327 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000328 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000329 // if all pixels are clearToInside then we have to process the
330 // difference, otherwise it has no effect and all pixels
331 // remain outside.
332 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000333 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000334 done = true;
335 }
336 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000337 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000338 // if all pixels are clearToInside then reverse difference
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000339 // produces empty set. Otherwise it is same as replace
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000340 if (*clearToInside) {
341 *clearToInside = false;
342 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000343 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000344 done = true;
345 }
346 break;
347 default:
348 GrCrash("Unknown set op.");
349 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000350
351 if (done) {
352 // we need to break out here (rather than letting the test in
353 // the loop do it) since backing up the iterator is very expensive
354 break;
355 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000356 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000357 return element;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000358}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000359
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000360}
361
robertphillips@google.comf294b772012-04-27 14:29:26 +0000362namespace {
363
364////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000365// set up the OpenGL blend function to perform the specified
366// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000367void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000368
369 switch (op) {
370 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000371 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000372 break;
373 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000374 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000375 break;
376 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000377 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000378 break;
379 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000380 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000381 break;
382 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000383 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000384 break;
385 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000386 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000387 break;
388 default:
389 GrAssert(false);
390 break;
391 }
392}
393
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000394////////////////////////////////////////////////////////////////////////////////
395bool draw_path_in_software(GrContext* context,
396 GrGpu* gpu,
397 const SkPath& path,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000398 bool doAA,
399 const GrIRect& resultBounds) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000400 SkStroke stroke;
401 stroke.setDoFill(true);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000402
403 SkAutoTUnref<GrTexture> texture(
404 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000405 stroke,
406 resultBounds,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000407 doAA, NULL));
408 if (NULL == texture) {
409 return false;
410 }
411
412 // The ClipMaskManager accumulates the clip mask in the UL corner
413 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
414
415 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, rect);
416
sugoi@google.com12b4e272012-12-06 20:13:11 +0000417 GrAssert(!path.isInverseFillType());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000418 return true;
419}
420
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000421
422////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000423bool draw_path(GrContext* context,
424 GrGpu* gpu,
425 const SkPath& path,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000426 bool doAA,
427 const GrIRect& resultBounds) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000428 SkStroke stroke;
429 stroke.setDoFill(true);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000430
sugoi@google.com12b4e272012-12-06 20:13:11 +0000431 GrPathRenderer* pr = context->getPathRenderer(path, stroke, gpu, doAA, false);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000432 if (NULL == pr) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000433 return draw_path_in_software(context, gpu, path, doAA, resultBounds);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000434 }
435
sugoi@google.com12b4e272012-12-06 20:13:11 +0000436 pr->drawPath(path, stroke, gpu, doAA);
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000437 return true;
438}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000439
robertphillips@google.com7b112892012-07-31 15:18:21 +0000440// 'rect' enters in device coordinates and leaves in canvas coordinates
441void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
442 GrAssert(NULL != rect);
443
444 rect->fLeft += SkIntToScalar(origin.fX);
445 rect->fTop += SkIntToScalar(origin.fY);
446 rect->fRight += SkIntToScalar(origin.fX);
447 rect->fBottom += SkIntToScalar(origin.fY);
448}
449
robertphillips@google.com72176b22012-05-23 13:19:12 +0000450}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000451
452////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000453bool GrClipMaskManager::drawClipShape(GrTexture* target,
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000454 const SkClipStack::Element* element,
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000455 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000456 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000457 GrAssert(NULL != drawState);
458
459 drawState->setRenderTarget(target->asRenderTarget());
460
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000461 switch (element->getType()) {
462 case Element::kRect_Type:
463 if (element->isAA()) {
464 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu, element->getRect(), true);
465 } else {
466 fGpu->drawSimpleRect(element->getRect(), NULL);
467 }
468 return true;
469 case Element::kPath_Type:
470 return draw_path(this->getContext(), fGpu,
471 element->getPath(),
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000472 element->isAA(),
473 resultBounds);
474 default:
475 // something is wrong if we're trying to draw an empty element.
476 GrCrash("Unexpected element type");
477 return false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000478 }
479 return true;
480}
481
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000482void GrClipMaskManager::mergeMask(GrTexture* dstMask,
483 GrTexture* srcMask,
484 SkRegion::Op op,
485 const GrIRect& dstBound,
486 const GrIRect& srcBound) {
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);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000489 SkMatrix oldMatrix = drawState->getViewMatrix();
490 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000491
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000492 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000493
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000494 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000495
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000496 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000497 sampleM.setIDiv(srcMask->width(), srcMask->height());
498 drawState->stage(0)->setEffect(
499 GrTextureDomainEffect::Create(srcMask,
500 sampleM,
501 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
502 GrTextureDomainEffect::kDecal_WrapMode))->unref();
503 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000504
tomhudson@google.com676e6602012-07-10 17:21:48 +0000505 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000506 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000507}
508
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000509// get a texture to act as a temporary buffer for AA clip boolean operations
510// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000511void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000512 GrAutoScratchTexture* temp) {
513 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000514 // we've already allocated the temp texture
515 return;
516 }
517
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000518 GrTextureDesc desc;
519 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
520 desc.fWidth = bounds.width();
521 desc.fHeight = bounds.height();
522 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000523
robertphillips@google.com2c756812012-05-22 20:28:23 +0000524 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000525}
526
robertphillips@google.comf105b102012-05-14 12:18:26 +0000527
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000528void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000529 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000530 // Since we are setting up the cache we know the last lookup was a miss
531 // Free up the currently cached mask so it can be reused
532 fAACache.reset();
533
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000534 GrTextureDesc desc;
535 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
536 desc.fWidth = bounds.width();
537 desc.fHeight = bounds.height();
538 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000539
540 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000541}
542
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000543////////////////////////////////////////////////////////////////////////////////
544// Shared preamble between gpu and SW-only AA clip mask creation paths.
545// Handles caching, determination of clip mask bound & allocation (if needed)
546// of the result texture
547// Returns true if there is no more work to be done (i.e., we got a cache hit)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000548bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000549 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000550 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000551 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000552 GrAssert(origDrawState->isClipState());
553
554 GrRenderTarget* rt = origDrawState->getRenderTarget();
555 GrAssert(NULL != rt);
556
robertphillips@google.comf294b772012-04-27 14:29:26 +0000557 // unlike the stencil path the alpha path is not bound to the size of the
558 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000559 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000560 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000561
562 // need to outset a pixel since the standard bounding box computation
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000563 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000564 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000565
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000566 // TODO: make sure we don't outset if bounds are still 0,0 @ min
567
robertphillips@google.comba998f22012-10-12 11:33:56 +0000568 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000569 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000570 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000571 return true;
572 }
573
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000574 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000575 return false;
576}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000577
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000578////////////////////////////////////////////////////////////////////////////////
579// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000580bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000581 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000582 GrIRect *devResultBounds) {
583 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000584 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
585
robertphillips@google.com7b112892012-07-31 15:18:21 +0000586 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000587 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000588 return true;
589 }
590
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000591 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
592
robertphillips@google.comf105b102012-05-14 12:18:26 +0000593 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000594 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000595 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000596 return false;
597 }
598
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000599 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
600 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000601
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000602 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000603
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000604 // The mask we generate is translated so that its upper-left corner is at devResultBounds
605 // upper-left corner in device space.
606 GrIRect maskResultBounds = GrIRect::MakeWH(devResultBounds->width(), devResultBounds->height());
607
608 // Set the matrix so that rendered clip elements are transformed from the space of the clip
609 // stack to the alpha-mask. This accounts for both translation due to the clip-origin and the
610 // placement of the mask within the device.
611 SkVector clipToMaskOffset = {
612 SkIntToScalar(-devResultBounds->fLeft - clipDataIn.fOrigin.fX),
613 SkIntToScalar(-devResultBounds->fTop - clipDataIn.fOrigin.fY)
614 };
615 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000616
617 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000618 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
619
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000620 SkClipStack::Iter iter(*clipDataIn.fClipStack,
621 SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000622 const Element* element = process_initial_clip_elements(&iter,
623 *devResultBounds,
624 &clearToInside,
625 &firstOp,
626 clipDataIn);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000627 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
628 // clear the part that we care about.
629 fGpu->clear(&maskResultBounds,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000630 clearToInside ? 0xffffffff : 0x00000000,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000631 accum->asRenderTarget());
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000632 bool accumClearedToZero = !clearToInside;
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000633
robertphillips@google.comf105b102012-05-14 12:18:26 +0000634 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000635 bool first = true;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000636 // walk through each clip element and perform its set op
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000637 for ( ; NULL != element; element = iter.next()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000638
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000639 SkRegion::Op op = element->getOp();
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000640 if (first) {
641 first = false;
642 op = firstOp;
643 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000644
bsalomon@google.com6794a252012-11-08 15:30:53 +0000645 if (SkRegion::kReplace_Op == op) {
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000646 // clear the accumulator and draw the new object directly into it
647 if (!accumClearedToZero) {
648 fGpu->clear(&maskResultBounds, 0x00000000, accum->asRenderTarget());
649 }
650
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000651 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000652 this->drawClipShape(accum, element, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000653
654 } else if (SkRegion::kReverseDifference_Op == op ||
655 SkRegion::kIntersect_Op == op) {
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000656 // there is no point in intersecting a screen filling rectangle.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000657 if (SkRegion::kIntersect_Op == op && Element::kRect_Type == element->getType() &&
658 contains(element->getRect(), *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000659 continue;
660 }
661
robertphillips@google.com7b112892012-07-31 15:18:21 +0000662 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000663 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000664 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000665 return false;
666 }
667
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000668 // this is the bounds of the clip element in the space of the alpha-mask. The temporary
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000669 // mask buffer can be substantially larger than the actually clip stack element. We
670 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000671 // the accumulator
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000672 GrRect elementMaskBounds = element->getBounds();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000673 elementMaskBounds.offset(clipToMaskOffset);
674 GrIRect elementMaskIBounds;
675 elementMaskBounds.roundOut(&elementMaskIBounds);
676
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000677 // clear the temp target & draw into it
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000678 fGpu->clear(&elementMaskIBounds, 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);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000681 this->drawClipShape(temp.texture(), element, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000682
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000683 // Now draw into the accumulator using the real operation
684 // and the temp buffer as a texture
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000685 this->mergeMask(accum, temp.texture(), op, maskResultBounds, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000686 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000687 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000688 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000689 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000690 this->drawClipShape(accum, element, *devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000691 }
bsalomon@google.com2e0c79f2012-11-12 13:38:57 +0000692 accumClearedToZero = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000693 }
694
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000695 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000696 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000697 return true;
698}
699
700////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000701// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000702// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000703bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000704 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000705
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000706 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000708 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000709 GrAssert(drawState->isClipState());
710
711 GrRenderTarget* rt = drawState->getRenderTarget();
712 GrAssert(NULL != rt);
713
714 // TODO: dynamically attach a SB when needed.
715 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
716 if (NULL == stencilBuffer) {
717 return false;
718 }
719
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000720 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000722 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000723
724 // we set the current clip to the bounds so that our recursive
725 // draws are scissored to them. We use the copy of the complex clip
726 // we just stashed on the SB to render from. We set it back after
727 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000728 const GrClipData* oldClipData = fGpu->getClip();
729
robertphillips@google.com7b112892012-07-31 15:18:21 +0000730 // The origin of 'newClipData' is (0, 0) so it is okay to place
731 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000732 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000733 GrClipData newClipData;
734 newClipData.fClipStack = &newClipStack;
735
736 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000738 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
739 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000741 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000742
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000743 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000744 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000745 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000746 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000747 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000748 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000749 }
750
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751#if !VISUALIZE_COMPLEX_CLIP
752 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
753#endif
754
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755 int clipBit = stencilBuffer->bits();
756 SkASSERT((clipBit <= 16) &&
757 "Ganesh only handles 16b or smaller stencil buffers");
758 clipBit = (1 << (clipBit-1));
759
robertphillips@google.com7b112892012-07-31 15:18:21 +0000760 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000761
762 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000763 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
764
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000765 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000766 SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000767 const Element* element = process_initial_clip_elements(&iter,
768 devRTRect,
769 &clearToInside,
770 &firstOp,
771 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772
robertphillips@google.com7b112892012-07-31 15:18:21 +0000773 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000774 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775
776 // walk through each clip element and perform its set op
777 // with the existing clip.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000778 for ( ; NULL != element; element = iter.next()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000779 SkPath::FillType fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000780 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000781 // enabled at bottom of loop
782 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000783 // if the target is MSAA then we want MSAA enabled when the clip is soft
784 if (rt->isMultisampled()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000785 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000786 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787
tomhudson@google.com8afae612012-08-14 15:03:35 +0000788 // Can the clip element be drawn directly to the stencil buffer
789 // with a non-inverted fill rule without extra passes to
790 // resolve in/out status?
791 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000792
sugoi@google.com12b4e272012-12-06 20:13:11 +0000793 SkStroke stroke;
794 stroke.setDoFill(true);
795
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000796 SkRegion::Op op = element->getOp();
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000797 if (first) {
798 first = false;
799 op = firstOp;
800 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000801
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000802 GrPathRenderer* pr = NULL;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000803 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000804 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000805 canRenderDirectToStencil = true;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000806 fill = SkPath::kEvenOdd_FillType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807 fillInverted = false;
808 // there is no point in intersecting a screen filling
809 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000810 if (SkRegion::kIntersect_Op == op &&
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000811 contains(element->getRect(), devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000812 continue;
813 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000814 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000815 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +0000816 clipPath = element->getPath();
817 fill = clipPath.getFillType();
818 fillInverted = clipPath.isInverseFillType();
819 fill = SkPath::NonInverseFill(fill);
820 clipPath.setFillType(fill);
821 pr = this->getContext()->getPathRenderer(clipPath, stroke, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000822 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000823 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824 return false;
825 }
826 canRenderDirectToStencil =
sugoi@google.com12b4e272012-12-06 20:13:11 +0000827 !pr->requiresStencilPass(clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000828 }
829
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000830 int passes;
831 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
832
833 bool canDrawDirectToClip; // Given the renderer, the element,
834 // fill rule, and set operation can
835 // we render the element directly to
836 // stencil bit used for clipping.
837 canDrawDirectToClip =
838 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000839 canRenderDirectToStencil,
840 clipBit,
841 fillInverted,
842 &passes,
843 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844
845 // draw the element to the client stencil bits if necessary
846 if (!canDrawDirectToClip) {
847 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
848 kIncClamp_StencilOp,
849 kIncClamp_StencilOp,
850 kAlways_StencilFunc,
851 0xffff,
852 0x0000,
853 0xffff);
854 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000855 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000857 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000858 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000859 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000860 if (canRenderDirectToStencil) {
861 *drawState->stencil() = gDrawToStencil;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000862 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000863 } else {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000864 pr->drawPathToStencil(clipPath, stroke, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000865 }
866 }
867 }
868
869 // now we modify the clip bit by rendering either the clip
870 // element directly or a bounding rect of the entire clip.
871 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
872 for (int p = 0; p < passes; ++p) {
873 *drawState->stencil() = stencilSettings[p];
874 if (canDrawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000875 if (Element::kRect_Type == element->getType()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000876 SET_RANDOM_COLOR
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000877 fGpu->drawSimpleRect(element->getRect(), NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000878 } else {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000879 GrAssert(Element::kPath_Type == element->getType());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000880 SET_RANDOM_COLOR
sugoi@google.com12b4e272012-12-06 20:13:11 +0000881 pr->drawPath(clipPath, stroke, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000882 }
883 } else {
884 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000885 // 'devClipBounds' is already in device coordinates so the
886 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000887 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000888 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000889 GrRect canvClipBounds;
890 canvClipBounds.set(devClipBounds);
891 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
892 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000893 }
894 }
895 }
896 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000897 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000898 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000899 // set this last because recursive draws may overwrite it back to kNone.
900 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
901 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000902 return true;
903}
904
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000905
bsalomon@google.com411dad02012-06-05 20:24:20 +0000906// mapping of clip-respecting stencil funcs to normal stencil funcs
907// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000908static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000909 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
910 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
911 // In the Clip Funcs
912 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
913 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
914 kLess_StencilFunc, // kLessIfInClip_StencilFunc
915 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
916 // Special in the clip func that forces user's ref to be 0.
917 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
918 // make ref 0 and do normal nequal.
919 },
920 {// Stencil-Clipping is ENABLED
921 // In the Clip Funcs
922 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
923 // eq stencil clip bit, mask
924 // out user bits.
925
926 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
927 // add stencil bit to mask and ref
928
929 kLess_StencilFunc, // kLessIfInClip_StencilFunc
930 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
931 // for both of these we can add
932 // the clip bit to the mask and
933 // ref and compare as normal
934 // Special in the clip func that forces user's ref to be 0.
935 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
936 // make ref have only the clip bit set
937 // and make comparison be less
938 // 10..0 < 1..user_bits..
939 }
940};
941
bsalomon@google.coma3201942012-06-21 19:58:20 +0000942namespace {
943// Sets the settings to clip against the stencil buffer clip while ignoring the
944// client bits.
945const GrStencilSettings& basic_apply_stencil_clip_settings() {
946 // stencil settings to use when clip is in stencil
947 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
948 kKeep_StencilOp,
949 kKeep_StencilOp,
950 kAlwaysIfInClip_StencilFunc,
951 0x0000,
952 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000953 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000954 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
955}
956}
957
958void GrClipMaskManager::setGpuStencil() {
959 // We make two copies of the StencilSettings here (except in the early
960 // exit scenario. One copy from draw state to the stack var. Then another
961 // from the stack var to the gpu. We could make this class hold a ptr to
962 // GrGpu's fStencilSettings and eliminate the stack copy here.
963
964 const GrDrawState& drawState = fGpu->getDrawState();
965
966 // use stencil for clipping if clipping is enabled and the clip
967 // has been written into the stencil.
968 GrClipMaskManager::StencilClipMode clipMode;
969 if (this->isClipInStencil() && drawState.isClipState()) {
970 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
971 // We can't be modifying the clip and respecting it at the same time.
972 GrAssert(!drawState.isStateFlagEnabled(
973 GrGpu::kModifyStencilClip_StateBit));
974 } else if (drawState.isStateFlagEnabled(
975 GrGpu::kModifyStencilClip_StateBit)) {
976 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
977 } else {
978 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
979 }
980
981 GrStencilSettings settings;
982 // The GrGpu client may not be using the stencil buffer but we may need to
983 // enable it in order to respect a stencil clip.
984 if (drawState.getStencil().isDisabled()) {
985 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
986 settings = basic_apply_stencil_clip_settings();
987 } else {
988 fGpu->disableStencil();
989 return;
990 }
991 } else {
992 settings = drawState.getStencil();
993 }
994
995 // TODO: dynamically attach a stencil buffer
996 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000997 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +0000998 drawState.getRenderTarget()->getStencilBuffer();
999 if (NULL != stencilBuffer) {
1000 stencilBits = stencilBuffer->bits();
1001 }
1002
bsalomon@google.comf6601872012-08-28 21:11:35 +00001003 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001004 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001005 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001006 this->adjustStencilParams(&settings, clipMode, stencilBits);
1007 fGpu->setStencilSettings(settings);
1008}
1009
1010void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1011 StencilClipMode mode,
1012 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001013 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001014
1015 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001016 // We assume that this clip manager itself is drawing to the GrGpu and
1017 // has already setup the correct values.
1018 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001019 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001020
bsalomon@google.com411dad02012-06-05 20:24:20 +00001021 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1022 unsigned int userBits = clipBit - 1;
1023
bsalomon@google.coma3201942012-06-21 19:58:20 +00001024 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001025 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001026
bsalomon@google.coma3201942012-06-21 19:58:20 +00001027 bool finished = false;
1028 while (!finished) {
1029 GrStencilFunc func = settings->func(face);
1030 uint16_t writeMask = settings->writeMask(face);
1031 uint16_t funcMask = settings->funcMask(face);
1032 uint16_t funcRef = settings->funcRef(face);
1033
1034 GrAssert((unsigned) func < kStencilFuncCount);
1035
1036 writeMask &= userBits;
1037
1038 if (func >= kBasicStencilFuncCount) {
1039 int respectClip = kRespectClip_StencilClipMode == mode;
1040 if (respectClip) {
1041 // The GrGpu class should have checked this
1042 GrAssert(this->isClipInStencil());
1043 switch (func) {
1044 case kAlwaysIfInClip_StencilFunc:
1045 funcMask = clipBit;
1046 funcRef = clipBit;
1047 break;
1048 case kEqualIfInClip_StencilFunc:
1049 case kLessIfInClip_StencilFunc:
1050 case kLEqualIfInClip_StencilFunc:
1051 funcMask = (funcMask & userBits) | clipBit;
1052 funcRef = (funcRef & userBits) | clipBit;
1053 break;
1054 case kNonZeroIfInClip_StencilFunc:
1055 funcMask = (funcMask & userBits) | clipBit;
1056 funcRef = clipBit;
1057 break;
1058 default:
1059 GrCrash("Unknown stencil func");
1060 }
1061 } else {
1062 funcMask &= userBits;
1063 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001064 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001065 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001066 gSpecialToBasicStencilFunc[respectClip];
1067 func = table[func - kBasicStencilFuncCount];
1068 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001069 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001070 funcMask &= userBits;
1071 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001072 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001073
1074 settings->setFunc(face, func);
1075 settings->setWriteMask(face, writeMask);
1076 settings->setFuncMask(face, funcMask);
1077 settings->setFuncRef(face, funcRef);
1078
1079 if (GrStencilSettings::kFront_Face == face) {
1080 face = GrStencilSettings::kBack_Face;
1081 finished = !twoSided;
1082 } else {
1083 finished = true;
1084 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001085 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001086 if (!twoSided) {
1087 settings->copyFrontSettingsToBack();
1088 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001089}
1090
1091////////////////////////////////////////////////////////////////////////////////
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001092bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001093 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001094 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001095 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001096
robertphillips@google.com7b112892012-07-31 15:18:21 +00001097 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001098 return true;
1099 }
1100
robertphillips@google.comf105b102012-05-14 12:18:26 +00001101 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001102 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001103 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001104 return false;
1105 }
1106
robertphillips@google.com2c756812012-05-22 20:28:23 +00001107 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001108
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001109 SkMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001110 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001111 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001112 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001113
sugoi@google.com12b4e272012-12-06 20:13:11 +00001114 SkStroke stroke;
1115 stroke.setDoFill(true);
1116
robertphillips@google.comfa662942012-05-17 12:20:22 +00001117 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001118 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1119
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001120 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001121 SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001122 const Element* element = process_initial_clip_elements(&iter,
1123 *devResultBounds,
1124 &clearToInside,
1125 &firstOp,
1126 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001127
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001128 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001129
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001130 bool first = true;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001131 for ( ; NULL != element; element = iter.next()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001132
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001133 SkRegion::Op op = element->getOp();
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001134 if (first) {
1135 first = false;
1136 op = firstOp;
1137 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001138
1139 if (SkRegion::kIntersect_Op == op ||
1140 SkRegion::kReverseDifference_Op == op) {
1141 // Intersect and reverse difference require modifying pixels
1142 // outside of the geometry that is being "drawn". In both cases
1143 // we erase all the pixels outside of the geometry but
1144 // leave the pixels inside the geometry alone. For reverse
1145 // difference we invert all the pixels before clearing the ones
1146 // outside the geometry.
1147 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001148 SkRect temp;
1149 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001150 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1151 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001152
1153 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001154 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001155 }
1156
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001157 if (Element::kRect_Type == element->getType()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001158 // convert the rect to a path so we can invert the fill
1159 SkPath temp;
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001160 temp.addRect(element->getRect());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001161 temp.setFillType(SkPath::kInverseEvenOdd_FillType);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001162
sugoi@google.com12b4e272012-12-06 20:13:11 +00001163 helper.draw(temp, stroke, SkRegion::kReplace_Op,
1164 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001165 0x00);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001166 } else {
1167 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001168 SkPath clipPath = element->getPath();
1169 clipPath.toggleInverseFillType();
1170 helper.draw(clipPath, stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001171 SkRegion::kReplace_Op,
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001172 element->isAA(),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001173 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001174 }
1175
1176 continue;
1177 }
1178
1179 // The other ops (union, xor, diff) only affect pixels inside
1180 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001181 if (Element::kRect_Type == element->getType()) {
1182 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1183 } else {
1184 GrAssert(Element::kPath_Type == element->getType());
sugoi@google.com12b4e272012-12-06 20:13:11 +00001185 helper.draw(element->getPath(), stroke, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001186 }
1187 }
1188
robertphillips@google.comfa662942012-05-17 12:20:22 +00001189 // Because we are using the scratch texture cache, "accum" may be
1190 // larger than expected and have some cruft in the areas we aren't using.
1191 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001192 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001193
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001194 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001195
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001196 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001197
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001198 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001199 return true;
1200}
1201
robertphillips@google.comf294b772012-04-27 14:29:26 +00001202////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001203void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001204 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001205}