blob: 6cd02bbc68dc0a61473a47847cc58e92311a7234 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000010#include "effects/GrTextureDomainEffect.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000011#include "GrGpu.h"
12#include "GrRenderTarget.h"
13#include "GrStencilBuffer.h"
14#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000015#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000016#include "SkRasterClip.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000017#include "GrAAConvexPathRenderer.h"
18#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
robertphillips@google.com46a86002012-08-08 10:42:44 +000020#include "GrCacheID.h"
21
22GR_DEFINE_RESOURCE_CACHE_DOMAIN(GrClipMaskManager, GetAlphaMaskDomain)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000023
robertphillips@google.comba998f22012-10-12 11:33:56 +000024#define GR_AA_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000025
robertphillips@google.comf294b772012-04-27 14:29:26 +000026////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000027namespace {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000029// stage matrix this also alters the vertex layout
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030void setup_drawstate_aaclip(GrGpu* gpu,
31 GrTexture* result,
robertphillips@google.com7b112892012-07-31 15:18:21 +000032 const GrIRect &devBound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000033 GrDrawState* drawState = gpu->drawState();
34 GrAssert(drawState);
35
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000036 static const int kMaskStage = GrPaint::kTotalStages+1;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000037
bsalomon@google.comb9086a02012-11-01 18:02:54 +000038 SkMatrix mat;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000039 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000041 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000042 mat.preConcat(drawState->getViewMatrix());
43
bsalomon@google.com08283af2012-10-26 13:01:20 +000044 drawState->stage(kMaskStage)->reset();
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000045
46 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
47 drawState->stage(kMaskStage)->setEffect(
48 GrTextureDomainEffect::Create(result,
49 mat,
50 GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
51 GrTextureDomainEffect::kDecal_WrapMode))->unref();
robertphillips@google.coma72eef32012-05-01 17:22:59 +000052}
53
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000054bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000055 GrGpu* gpu,
56 const SkPath& path,
57 GrPathFill fill,
58 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000059 // last (false) parameter disallows use of the SW path renderer
60 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
61}
62
robertphillips@google.coma6f11c42012-07-23 17:39:44 +000063GrPathFill get_path_fill(const SkPath& path) {
64 switch (path.getFillType()) {
65 case SkPath::kWinding_FillType:
66 return kWinding_GrPathFill;
67 case SkPath::kEvenOdd_FillType:
68 return kEvenOdd_GrPathFill;
69 case SkPath::kInverseWinding_FillType:
70 return kInverseWinding_GrPathFill;
71 case SkPath::kInverseEvenOdd_FillType:
72 return kInverseEvenOdd_GrPathFill;
73 default:
74 GrCrash("Unsupported path fill in clip.");
75 return kWinding_GrPathFill; // suppress warning
76 }
77}
78
robertphillips@google.comb99225c2012-07-24 18:20:10 +000079/**
80 * Does any individual clip in 'clipIn' use anti-aliasing?
81 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +000082bool requires_AA(const SkClipStack& clipIn) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000083
robertphillips@google.com641f8b12012-07-31 19:15:58 +000084 SkClipStack::Iter iter;
85 iter.reset(clipIn, SkClipStack::Iter::kBottom_IterStart);
robertphillips@google.comb99225c2012-07-24 18:20:10 +000086
robertphillips@google.com641f8b12012-07-31 19:15:58 +000087 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comb99225c2012-07-24 18:20:10 +000088 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
89 NULL != clip;
bsalomon@google.come8ca6c62012-11-07 21:19:10 +000090 clip = iter.nextCombined()) {
robertphillips@google.comb99225c2012-07-24 18:20:10 +000091
92 if (clip->fDoAA) {
93 return true;
94 }
95 }
96
97 return false;
98}
99
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000100}
101
robertphillips@google.comfa662942012-05-17 12:20:22 +0000102/*
103 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
104 * will be used on any element. If so, it returns true to indicate that the
105 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
106 */
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000107bool GrClipMaskManager::useSWOnlyPath(const SkClipStack& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000108
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000109 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000110 // a clip gets complex enough it can just be done in SW regardless
111 // of whether it would invoke the GrSoftwarePathRenderer.
112 bool useSW = false;
113
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000114 SkClipStack::Iter iter(clipIn, SkClipStack::Iter::kBottom_IterStart);
115 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000116
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000117 for (clip = iter.skipToTopmost(SkRegion::kReplace_Op);
118 NULL != clip;
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000119 clip = iter.nextCombined()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000120
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000121 // rects can always be drawn directly w/o using the software path
122 // so only paths need to be checked
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000123 if (NULL != clip->fPath &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000124 path_needs_SW_renderer(this->getContext(), fGpu,
125 *clip->fPath,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000126 get_path_fill(*clip->fPath),
127 clip->fDoAA)) {
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000128 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000129 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000130 }
131
132 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000133}
134
robertphillips@google.comf294b772012-04-27 14:29:26 +0000135////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000136// sort out what kind of clip mask needs to be created: alpha, stencil,
137// scissor, or entirely software
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000138bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000139 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000140
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000141 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000142 if (!drawState->isClipState() || clipDataIn->fClipStack->isWideOpen()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000143 fGpu->disableScissor();
144 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000145 return true;
146 }
147
148 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000149 // GrDrawTarget should have filtered this for us
150 GrAssert(NULL != rt);
151
robertphillips@google.com7b112892012-07-31 15:18:21 +0000152 GrIRect devClipBounds;
robertphillips@google.come4d69c02012-07-26 21:37:40 +0000153 bool isIntersectionOfRects = false;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000154
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000155 clipDataIn->getConservativeBounds(rt, &devClipBounds, &isIntersectionOfRects);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000156 if (devClipBounds.isEmpty()) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000157 return false;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000158 }
159
bsalomon@google.com6794a252012-11-08 15:30:53 +0000160#if GR_AA_CLIP
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000161 bool requiresAA = requires_AA(*clipDataIn->fClipStack);
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000162
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000163 // If MSAA is enabled we can do everything in the stencil buffer.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000164 // Otherwise check if we should just create the entire clip mask
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000165 // in software (this will only happen if the clip mask is anti-aliased
166 // and too complex for the gpu to handle in its entirety)
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000167 if (0 == rt->numSamples() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000168 requiresAA &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000169 this->useSWOnlyPath(*clipDataIn->fClipStack)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000170 // The clip geometry is complex enough that it will be more
171 // efficient to create it entirely in software
172 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000173 GrIRect devBound;
174 if (this->createSoftwareClipMask(*clipDataIn, &result, &devBound)) {
175 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000176 fGpu->disableScissor();
177 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000178 return true;
179 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000180
181 // if SW clip mask creation fails fall through to the other
182 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000183 }
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000184
robertphillips@google.comf294b772012-04-27 14:29:26 +0000185 // If MSAA is enabled use the (faster) stencil path for AA clipping
186 // otherwise the alpha clip mask is our only option
robertphillips@google.comb99225c2012-07-24 18:20:10 +0000187 if (0 == rt->numSamples() && requiresAA) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000188 // Since we are going to create a destination texture of the correct
189 // size for the mask (rather than being bound by the size of the
190 // render target) we aren't going to use scissoring like the stencil
191 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000192 GrTexture* result = NULL;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000193 GrIRect devBound;
194 if (this->createAlphaClipMask(*clipDataIn, &result, &devBound)) {
195 setup_drawstate_aaclip(fGpu, result, devBound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000196 fGpu->disableScissor();
197 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000198 return true;
199 }
200
201 // if alpha clip mask creation fails fall through to the stencil
202 // buffer method
203 }
204#endif // GR_AA_CLIP
205
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000206 // Either a hard (stencil buffer) clip was explicitly requested or
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000207 // an antialiased clip couldn't be created. In either case, free up
208 // the texture in the antialiased mask cache.
209 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000210 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000211 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000212 // AA cache.
213 fAACache.reset();
214
bsalomon@google.coma3201942012-06-21 19:58:20 +0000215 // If the clip is a rectangle then just set the scissor. Otherwise, create
216 // a stencil mask.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000217 if (isIntersectionOfRects) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000218 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000219 this->setGpuStencil();
220 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000221 }
222
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000223 // use the stencil clip if we can't represent the clip as a rectangle.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000224 bool useStencil = !clipDataIn->fClipStack->isWideOpen() &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000225 !devClipBounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000226
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000227 if (useStencil) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000228 this->createStencilClipMask(*clipDataIn, devClipBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000229 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000230 // This must occur after createStencilClipMask. That function may change
231 // the scissor. Also, it only guarantees that the stencil mask is correct
232 // within the bounds it was passed, so we must use both stencil and scissor
233 // test to the bounds for the final draw.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000234 fGpu->enableScissor(devClipBounds);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000235 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000236 return true;
237}
238
239#define VISUALIZE_COMPLEX_CLIP 0
240
241#if VISUALIZE_COMPLEX_CLIP
242 #include "GrRandom.h"
243 GrRandom gRandom;
244 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
245#else
246 #define SET_RANDOM_COLOR
247#endif
248
249namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000250/**
robertphillips@google.com7b112892012-07-31 15:18:21 +0000251 * Does "canvContainer" contain "devContainee"? If either is empty then
252 * no containment is possible. "canvContainer" is in canvas coordinates while
253 * "devContainee" is in device coordiates. "origin" provides the mapping between
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000254 * the two.
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000255 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000256bool contains(const SkRect& canvContainer,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000257 const SkIRect& devContainee,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000258 const SkIPoint& origin) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000259 return !devContainee.isEmpty() && !canvContainer.isEmpty() &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000260 canvContainer.fLeft <= SkIntToScalar(devContainee.fLeft+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000261 canvContainer.fTop <= SkIntToScalar(devContainee.fTop+origin.fY) &&
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000262 canvContainer.fRight >= SkIntToScalar(devContainee.fRight+origin.fX) &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000263 canvContainer.fBottom >= SkIntToScalar(devContainee.fBottom+origin.fY);
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000264}
265
robertphillips@google.comf294b772012-04-27 14:29:26 +0000266////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000267// determines how many elements at the head of the clip can be skipped and
268// whether the initial clear should be to the inside- or outside-the-clip value,
269// and what op should be used to draw the first element that isn't skipped.
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000270const SkClipStack::Iter::Clip* process_initial_clip_elements(
271 SkClipStack::Iter* iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000272 const GrIRect& devBounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000273 bool* clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000274 SkRegion::Op* firstOp,
275 const GrClipData& clipData) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000276
277 GrAssert(NULL != iter && NULL != clearToInside && NULL != firstOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000278
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000279 // logically before the first element of the clip stack is
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000280 // processed the clip is entirely open. However, depending on the
281 // first set op we may prefer to clear to 0 for performance. We may
282 // also be able to skip the initial clip paths/rects. We loop until
283 // we cannot skip an element.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000284 bool done = false;
285 *clearToInside = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000286
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000287 const SkClipStack::Iter::Clip* clip = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000288
289 for (clip = iter->skipToTopmost(SkRegion::kReplace_Op);
290 NULL != clip && !done;
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000291 clip = iter->nextCombined()) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000292 switch (clip->fOp) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000293 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000294 // replace ignores everything previous
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000295 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000296 *clearToInside = false;
297 done = true;
298 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000299 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 // if this element contains the entire bounds then we
301 // can skip it.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000302 if (NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000303 contains(*clip->fRect, devBounds, clipData.fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000304 break;
305 }
306 // if everything is initially clearToInside then intersect is
307 // same as clear to 0 and treat as a replace. Otherwise,
308 // set stays empty.
309 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000310 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000311 *clearToInside = false;
312 done = true;
313 }
314 break;
315 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000316 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000317 // if everything is initially outside then union is
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000318 // same as replace. Otherwise, every pixel is still
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000319 // clearToInside
320 if (!*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000321 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000322 done = true;
323 }
324 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000325 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000326 // xor is same as difference or replace both of which
327 // can be 1-pass instead of 2 for xor.
328 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000329 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000330 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000331 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000332 }
333 done = true;
334 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000335 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000336 // if all pixels are clearToInside then we have to process the
337 // difference, otherwise it has no effect and all pixels
338 // remain outside.
339 if (*clearToInside) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000340 *firstOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000341 done = true;
342 }
343 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000344 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345 // if all pixels are clearToInside then reverse difference
bsalomon@google.com6794a252012-11-08 15:30:53 +0000346 // produces empty set. Otherwise it is same as replace
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000347 if (*clearToInside) {
348 *clearToInside = false;
349 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000350 *firstOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000351 done = true;
352 }
353 break;
354 default:
355 GrCrash("Unknown set op.");
356 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000357
358 if (done) {
359 // we need to break out here (rather than letting the test in
360 // the loop do it) since backing up the iterator is very expensive
361 break;
362 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000363 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000364 return clip;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000365}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000366
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000367}
368
robertphillips@google.comf294b772012-04-27 14:29:26 +0000369namespace {
370
371////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000372// set up the OpenGL blend function to perform the specified
373// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000374void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000375
376 switch (op) {
377 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000378 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000379 break;
380 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000381 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382 break;
383 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000384 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000385 break;
386 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000387 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388 break;
389 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000390 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000391 break;
392 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000393 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000394 break;
395 default:
396 GrAssert(false);
397 break;
398 }
399}
400
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000401
402////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com72176b22012-05-23 13:19:12 +0000403
robertphillips@google.com7b112892012-07-31 15:18:21 +0000404// 'rect' enters in device coordinates and leaves in canvas coordinates
405void device_to_canvas(SkRect* rect, const SkIPoint& origin) {
406 GrAssert(NULL != rect);
407
408 rect->fLeft += SkIntToScalar(origin.fX);
409 rect->fTop += SkIntToScalar(origin.fY);
410 rect->fRight += SkIntToScalar(origin.fX);
411 rect->fBottom += SkIntToScalar(origin.fY);
412}
413
robertphillips@google.com72176b22012-05-23 13:19:12 +0000414}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000415
416////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000417bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000418 const SkClipStack::Iter::Clip* clip,
bsalomon@google.com6794a252012-11-08 15:30:53 +0000419 const SkIRect& clipRect) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000420 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000421 GrAssert(NULL != drawState);
422
423 drawState->setRenderTarget(target->asRenderTarget());
424
bsalomon@google.com6794a252012-11-08 15:30:53 +0000425 GrDrawTarget::AutoClipRestore acr(fGpu);
426 SkClipStack rectStack(clipRect);
427 GrClipData cd;
428 cd.fClipStack = &rectStack;
429 cd.fOrigin.setZero();
430 fGpu->setClip(&cd);
431
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000432 if (NULL != clip->fRect) {
433 if (clip->fDoAA) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000434 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000435 *clip->fRect,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000436 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437 } else {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000438 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000439 }
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000440 } else if (NULL != clip->fPath) {
bsalomon@google.com6794a252012-11-08 15:30:53 +0000441
442 GrPathRenderer* pr = this->getContext()->getPathRenderer(*clip->fPath,
443 get_path_fill(*clip->fPath),
444 fGpu,
445 clip->fDoAA,
446 false);
447 if (NULL == pr) {
448 GrAssert(false); // We should have done the whole clip-stack in SW.
449 return false;
450 }
451
452 pr->drawPath(*clip->fPath, get_path_fill(*clip->fPath), fGpu, clip->fDoAA);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000453 }
454 return true;
455}
456
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000457void GrClipMaskManager::mergeMask(GrTexture* dstMask,
458 GrTexture* srcMask,
459 SkRegion::Op op,
460 const GrIRect& dstBound,
461 const GrIRect& srcBound) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000462 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000463 GrAssert(NULL != drawState);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000464 SkMatrix oldMatrix = drawState->getViewMatrix();
465 drawState->viewMatrix()->reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000466
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000467 drawState->setRenderTarget(dstMask->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000468
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000469 setup_boolean_blendcoeffs(drawState, op);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000470
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000471 SkMatrix sampleM;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000472 sampleM.setIDiv(srcMask->width(), srcMask->height());
473 drawState->stage(0)->setEffect(
474 GrTextureDomainEffect::Create(srcMask,
475 sampleM,
476 GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),
477 GrTextureDomainEffect::kDecal_WrapMode))->unref();
478 fGpu->drawSimpleRect(SkRect::MakeFromIRect(dstBound), NULL);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000479
tomhudson@google.com676e6602012-07-10 17:21:48 +0000480 drawState->disableStage(0);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000481 drawState->setViewMatrix(oldMatrix);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000482}
483
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000484// get a texture to act as a temporary buffer for AA clip boolean operations
485// TODO: given the expense of createTexture we may want to just cache this too
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000486void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000487 GrAutoScratchTexture* temp) {
488 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000489 // we've already allocated the temp texture
490 return;
491 }
492
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000493 GrTextureDesc desc;
494 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
495 desc.fWidth = bounds.width();
496 desc.fHeight = bounds.height();
497 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000498
robertphillips@google.com2c756812012-05-22 20:28:23 +0000499 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000500}
501
robertphillips@google.comf105b102012-05-14 12:18:26 +0000502
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000503void GrClipMaskManager::setupCache(const SkClipStack& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000504 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000505 // Since we are setting up the cache we know the last lookup was a miss
506 // Free up the currently cached mask so it can be reused
507 fAACache.reset();
508
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000509 GrTextureDesc desc;
510 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
511 desc.fWidth = bounds.width();
512 desc.fHeight = bounds.height();
513 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000514
515 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000516}
517
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000518////////////////////////////////////////////////////////////////////////////////
519// Shared preamble between gpu and SW-only AA clip mask creation paths.
520// Handles caching, determination of clip mask bound & allocation (if needed)
521// of the result texture
522// 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 +0000523bool GrClipMaskManager::clipMaskPreamble(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000524 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000525 GrIRect* devResultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000526 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000527 GrAssert(origDrawState->isClipState());
528
529 GrRenderTarget* rt = origDrawState->getRenderTarget();
530 GrAssert(NULL != rt);
531
robertphillips@google.comf294b772012-04-27 14:29:26 +0000532 // unlike the stencil path the alpha path is not bound to the size of the
533 // render target - determine the minimum size required for the mask
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000534 // Note: intBounds is in device (as opposed to canvas) coordinates
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000535 clipDataIn.getConservativeBounds(rt, devResultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000536
537 // need to outset a pixel since the standard bounding box computation
bsalomon@google.com6794a252012-11-08 15:30:53 +0000538 // path doesn't leave any room for anti-aliasing (esp. w.r.t. rects)
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000539 devResultBounds->outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000540
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000541 // TODO: make sure we don't outset if bounds are still 0,0 @ min
542
robertphillips@google.comba998f22012-10-12 11:33:56 +0000543 if (fAACache.canReuse(*clipDataIn.fClipStack, *devResultBounds)) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000544 *result = fAACache.getLastMask();
robertphillips@google.com7b112892012-07-31 15:18:21 +0000545 fAACache.getLastBound(devResultBounds);
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000546 return true;
547 }
548
robertphillips@google.com9cb5adf2012-08-30 11:05:08 +0000549 this->setupCache(*clipDataIn.fClipStack, *devResultBounds);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000550 return false;
551}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000552
bsalomon@google.com6794a252012-11-08 15:30:53 +0000553namespace {
554// Does a look-ahead to check whether we can bound earlier mask generation by forthcoming
555// intersections.
556void get_current_mask_bounds(const SkIRect& maskResultBounds,
557 const SkClipStack::Iter& curr,
558 const SkVector& clipToMaskOffset,
559 SkRect* currMaskBounds) {
560 SkClipStack::Iter isectIter(curr);
561 const SkClipStack::Iter::Clip* clip = isectIter.skipToNext(SkRegion::kIntersect_Op);
562 if (NULL != clip) {
563 *currMaskBounds = clip->getBounds();
564 while (NULL != (clip = isectIter.next()) &&
565 SkRegion::kIntersect_Op == clip->fOp &&
566 !clip->isInverseFilled()) {
567 currMaskBounds->intersect(clip->getBounds());
568 }
569 currMaskBounds->offset(clipToMaskOffset);
570 currMaskBounds->intersect(SkRect::MakeFromIRect(maskResultBounds));
571 } else {
572 *currMaskBounds = SkRect::MakeFromIRect(maskResultBounds);
573 }
574}
575}
576
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000577////////////////////////////////////////////////////////////////////////////////
578// Create a 8-bit clip mask in alpha
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000579bool GrClipMaskManager::createAlphaClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000580 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000581 GrIRect *devResultBounds) {
582 GrAssert(NULL != devResultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000583 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
584
robertphillips@google.com7b112892012-07-31 15:18:21 +0000585 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000586 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000587 return true;
588 }
589
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000590 // Note: 'resultBounds' is in device (as opposed to canvas) coordinates
591
robertphillips@google.comf105b102012-05-14 12:18:26 +0000592 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000593 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000594 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000595 return false;
596 }
597
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000598 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
599 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000600
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000601 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000602
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000603 // The mask we generate is translated so that its upper-left corner is at devResultBounds
604 // upper-left corner in device space.
605 GrIRect maskResultBounds = GrIRect::MakeWH(devResultBounds->width(), devResultBounds->height());
606
607 // Set the matrix so that rendered clip elements are transformed from the space of the clip
608 // stack to the alpha-mask. This accounts for both translation due to the clip-origin and the
609 // placement of the mask within the device.
610 SkVector clipToMaskOffset = {
611 SkIntToScalar(-devResultBounds->fLeft - clipDataIn.fOrigin.fX),
612 SkIntToScalar(-devResultBounds->fTop - clipDataIn.fOrigin.fY)
613 };
614 drawState->viewMatrix()->setTranslate(clipToMaskOffset);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000615
616 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000617 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
618
bsalomon@google.com6794a252012-11-08 15:30:53 +0000619 SkClipStack::Iter iter(*clipDataIn.fClipStack, SkClipStack::Iter::kBottom_IterStart);
620 iter.skipToTopmost(SkRegion::kReplace_Op);
621
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000622 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000623 *devResultBounds,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000624 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000625 &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.com6794a252012-11-08 15:30:53 +0000632 GR_DEBUGCODE(bool accumClearedToZero = !clearToInside;)
robertphillips@google.comf294b772012-04-27 14:29:26 +0000633
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000634
robertphillips@google.comf105b102012-05-14 12:18:26 +0000635 GrAutoScratchTexture temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000636 bool first = true;
bsalomon@google.com6794a252012-11-08 15:30:53 +0000637
638 // We bound mask-generation to both the known bounds of the final result as well as the bounds
639 // of the next run of intersections. This limits the amount of clearing and merging we have
640 // to do and can also allow us to skip intersect-rects entirely.
641 SkRect currMaskBounds;
642 SkIRect currMaskIBounds;
643 get_current_mask_bounds(maskResultBounds, iter, clipToMaskOffset, &currMaskBounds);
644 // currMaskBounds is used to reduce the number of pixels touched. It also allows us to skip
645 // non-AA intersect rects since we've clipped earlier elements to those rects already.
646 // We round out here which is consistent with how non-AA rects are handled.
647 currMaskBounds.roundOut(&currMaskIBounds);
648
649 // used to know when we're through with a run of intersect ops.
650 SkRegion::Op prevOp = SkRegion::kReplace_Op;
651
robertphillips@google.comf294b772012-04-27 14:29:26 +0000652 // walk through each clip element and perform its set op
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000653 for ( ; NULL != clip; clip = iter.nextCombined()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000654
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000655 SkRegion::Op op = clip->fOp;
656 if (first) {
657 first = false;
658 op = firstOp;
659 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000660
bsalomon@google.com6794a252012-11-08 15:30:53 +0000661 if (op == SkRegion::kIntersect_Op) {
662 // When we encounter an intersect we may have already fully accounted for it by applying
663 // currMaskBounds to the accumulated mask.
664 if (NULL != clip->fRect &&
665 (!clip->fDoAA || clip->fRect->contains(currMaskBounds))) {
666 prevOp = SkRegion::kIntersect_Op;
667 continue;
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000668 }
bsalomon@google.com6794a252012-11-08 15:30:53 +0000669 } else if (prevOp == SkRegion::kIntersect_Op) {
670 // We've finished a run of intersects, update currMaskBounds based on the next run.
671 get_current_mask_bounds(maskResultBounds, iter, clipToMaskOffset, &currMaskBounds);
672 currMaskBounds.roundOut(&currMaskIBounds);
673 }
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000674
bsalomon@google.com6794a252012-11-08 15:30:53 +0000675 prevOp = op;
676
677 if (SkRegion::kReplace_Op == op) {
678 // The accumulator should already be cleared. There can only be one replace since we
679 // skip to the last one. Moreover, process_initial_clip_elements will have set
680 // clearToInside to false before the initial clear.
681 GrAssert(accumClearedToZero);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000682 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com6794a252012-11-08 15:30:53 +0000683 this->drawClipShape(accum, clip, currMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000684
685 } else if (SkRegion::kReverseDifference_Op == op ||
686 SkRegion::kIntersect_Op == op) {
bsalomon@google.com6794a252012-11-08 15:30:53 +0000687 // There is no point in intersecting a screen filling rectangle.
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000688 if (SkRegion::kIntersect_Op == op && NULL != clip->fRect &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000689 contains(*clip->fRect, *devResultBounds, clipDataIn.fOrigin)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000690 continue;
691 }
692
robertphillips@google.com7b112892012-07-31 15:18:21 +0000693 getTemp(*devResultBounds, &temp);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000694 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000695 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000696 return false;
697 }
698
bsalomon@google.com6794a252012-11-08 15:30:53 +0000699 // 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 +0000700 // mask buffer can be substantially larger than the actually clip stack element. We
701 // touch the minimum number of pixels necessary and use decal mode to combine it with
bsalomon@google.com6794a252012-11-08 15:30:53 +0000702 // the accumulator.
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000703 GrRect elementMaskBounds = clip->getBounds();
704 elementMaskBounds.offset(clipToMaskOffset);
bsalomon@google.com6794a252012-11-08 15:30:53 +0000705 elementMaskBounds.intersect(currMaskBounds);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000706 GrIRect elementMaskIBounds;
707 elementMaskBounds.roundOut(&elementMaskIBounds);
708
bsalomon@google.com6794a252012-11-08 15:30:53 +0000709 // Clear the temp target & draw into it
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000710 fGpu->clear(&elementMaskIBounds, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000711
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000712 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
bsalomon@google.com6794a252012-11-08 15:30:53 +0000713 this->drawClipShape(temp.texture(), clip, currMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000714
bsalomon@google.com6794a252012-11-08 15:30:53 +0000715 // Now draw into the accumulator using the real operation and the temp buffer as a
716 // texture
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000717 this->mergeMask(accum, temp.texture(), op, maskResultBounds, elementMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000718 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000719 // all the remaining ops can just be directly draw into
robertphillips@google.comf294b772012-04-27 14:29:26 +0000720 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000721 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com6794a252012-11-08 15:30:53 +0000722 this->drawClipShape(accum, clip, currMaskIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000723 }
bsalomon@google.com6794a252012-11-08 15:30:53 +0000724 GR_DEBUGCODE(accumClearedToZero = false;)
robertphillips@google.comf294b772012-04-27 14:29:26 +0000725 }
726
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000727 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000728 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000729 return true;
730}
731
732////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000733// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000734// (as opposed to canvas) coordinates
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000735bool GrClipMaskManager::createStencilClipMask(const GrClipData& clipDataIn,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000736 const GrIRect& devClipBounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000738 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000740 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741 GrAssert(drawState->isClipState());
742
743 GrRenderTarget* rt = drawState->getRenderTarget();
744 GrAssert(NULL != rt);
745
746 // TODO: dynamically attach a SB when needed.
747 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
748 if (NULL == stencilBuffer) {
749 return false;
750 }
751
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000752 if (stencilBuffer->mustRenderClip(clipDataIn, rt->width(), rt->height())) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000753
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000754 stencilBuffer->setLastClip(clipDataIn, rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000755
756 // we set the current clip to the bounds so that our recursive
757 // draws are scissored to them. We use the copy of the complex clip
758 // we just stashed on the SB to render from. We set it back after
759 // we finish drawing it into the stencil.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000760 const GrClipData* oldClipData = fGpu->getClip();
761
robertphillips@google.com7b112892012-07-31 15:18:21 +0000762 // The origin of 'newClipData' is (0, 0) so it is okay to place
763 // a device-coordinate bound in 'newClipStack'
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000764 SkClipStack newClipStack(devClipBounds);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000765 GrClipData newClipData;
766 newClipData.fClipStack = &newClipStack;
767
768 fGpu->setClip(&newClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000769
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000770 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
771 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000773 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000774
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000775 if (0 != clipDataIn.fOrigin.fX || 0 != clipDataIn.fOrigin.fY) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000776 // Add the saveLayer's offset to the view matrix rather than
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000777 // offset each individual draw
robertphillips@google.com7b112892012-07-31 15:18:21 +0000778 drawState->viewMatrix()->setTranslate(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000779 SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000780 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000781 }
782
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000783#if !VISUALIZE_COMPLEX_CLIP
784 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
785#endif
786
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000787 int clipBit = stencilBuffer->bits();
788 SkASSERT((clipBit <= 16) &&
789 "Ganesh only handles 16b or smaller stencil buffers");
790 clipBit = (1 << (clipBit-1));
791
robertphillips@google.com7b112892012-07-31 15:18:21 +0000792 GrIRect devRTRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000793
794 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000795 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
796
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000797 SkClipStack::Iter iter(*oldClipData->fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000798 SkClipStack::Iter::kBottom_IterStart);
799 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000800 devRTRect,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000801 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000802 &firstOp,
803 clipDataIn);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000804
robertphillips@google.com7b112892012-07-31 15:18:21 +0000805 fGpu->clearStencilClip(devClipBounds, clearToInside);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000806 bool first = true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000807
808 // walk through each clip element and perform its set op
809 // with the existing clip.
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000810 for ( ; NULL != clip; clip = iter.nextCombined()) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000811 GrPathFill fill;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000812 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000813 // enabled at bottom of loop
814 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000815 // if the target is MSAA then we want MSAA enabled when the clip is soft
816 if (rt->isMultisampled()) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000817 drawState->setState(GrDrawState::kHWAntialias_StateBit, clip->fDoAA);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000818 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000819
tomhudson@google.com8afae612012-08-14 15:03:35 +0000820 // Can the clip element be drawn directly to the stencil buffer
821 // with a non-inverted fill rule without extra passes to
822 // resolve in/out status?
823 bool canRenderDirectToStencil = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000824
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000825 SkRegion::Op op = clip->fOp;
826 if (first) {
827 first = false;
828 op = firstOp;
829 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000830
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000832 const SkPath* clipPath = NULL;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000833 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000834 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000835 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000836 fillInverted = false;
837 // there is no point in intersecting a screen filling
838 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000839 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com7b112892012-07-31 15:18:21 +0000840 contains(*clip->fRect, devRTRect, oldClipData->fOrigin)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000841 continue;
842 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000843 } else {
844 GrAssert(NULL != clip->fPath);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000845 fill = get_path_fill(*clip->fPath);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000846 fillInverted = GrIsFillInverted(fill);
847 fill = GrNonInvertedFill(fill);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000848 clipPath = clip->fPath;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000849 pr = this->getContext()->getPathRenderer(*clipPath, fill, fGpu, false, true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000850 if (NULL == pr) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000851 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000852 return false;
853 }
854 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000855 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 }
857
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000858 int passes;
859 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
860
861 bool canDrawDirectToClip; // Given the renderer, the element,
862 // fill rule, and set operation can
863 // we render the element directly to
864 // stencil bit used for clipping.
865 canDrawDirectToClip =
866 GrStencilSettings::GetClipPasses(op,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000867 canRenderDirectToStencil,
868 clipBit,
869 fillInverted,
870 &passes,
871 stencilSettings);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000872
873 // draw the element to the client stencil bits if necessary
874 if (!canDrawDirectToClip) {
875 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
876 kIncClamp_StencilOp,
877 kIncClamp_StencilOp,
878 kAlways_StencilFunc,
879 0xffff,
880 0x0000,
881 0xffff);
882 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000883 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000884 *drawState->stencil() = gDrawToStencil;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000885 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000886 } else {
887 if (canRenderDirectToStencil) {
888 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000889 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000890 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000891 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000892 }
893 }
894 }
895
896 // now we modify the clip bit by rendering either the clip
897 // element directly or a bounding rect of the entire clip.
898 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
899 for (int p = 0; p < passes; ++p) {
900 *drawState->stencil() = stencilSettings[p];
901 if (canDrawDirectToClip) {
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000902 if (NULL != clip->fRect) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000903 SET_RANDOM_COLOR
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000904 fGpu->drawSimpleRect(*clip->fRect, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000905 } else {
906 SET_RANDOM_COLOR
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000907 pr->drawPath(*clipPath, fill, fGpu, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000908 }
909 } else {
910 SET_RANDOM_COLOR
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000911 // 'devClipBounds' is already in device coordinates so the
912 // translation in the view matrix is inappropriate.
robertphillips@google.com7b112892012-07-31 15:18:21 +0000913 // Convert it to canvas space so the drawn rect will
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000914 // be in the correct location
robertphillips@google.com7b112892012-07-31 15:18:21 +0000915 GrRect canvClipBounds;
916 canvClipBounds.set(devClipBounds);
917 device_to_canvas(&canvClipBounds, clipDataIn.fOrigin);
918 fGpu->drawSimpleRect(canvClipBounds, NULL);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000919 }
920 }
921 }
922 // restore clip
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000923 fGpu->setClip(oldClipData);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000924 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000925 // set this last because recursive draws may overwrite it back to kNone.
926 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
927 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000928 return true;
929}
930
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000931
bsalomon@google.com411dad02012-06-05 20:24:20 +0000932// mapping of clip-respecting stencil funcs to normal stencil funcs
933// mapping depends on whether stencil-clipping is in effect.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000934static const GrStencilFunc
bsalomon@google.com411dad02012-06-05 20:24:20 +0000935 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
936 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
937 // In the Clip Funcs
938 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
939 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
940 kLess_StencilFunc, // kLessIfInClip_StencilFunc
941 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
942 // Special in the clip func that forces user's ref to be 0.
943 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
944 // make ref 0 and do normal nequal.
945 },
946 {// Stencil-Clipping is ENABLED
947 // In the Clip Funcs
948 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
949 // eq stencil clip bit, mask
950 // out user bits.
951
952 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
953 // add stencil bit to mask and ref
954
955 kLess_StencilFunc, // kLessIfInClip_StencilFunc
956 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
957 // for both of these we can add
958 // the clip bit to the mask and
959 // ref and compare as normal
960 // Special in the clip func that forces user's ref to be 0.
961 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
962 // make ref have only the clip bit set
963 // and make comparison be less
964 // 10..0 < 1..user_bits..
965 }
966};
967
bsalomon@google.coma3201942012-06-21 19:58:20 +0000968namespace {
969// Sets the settings to clip against the stencil buffer clip while ignoring the
970// client bits.
971const GrStencilSettings& basic_apply_stencil_clip_settings() {
972 // stencil settings to use when clip is in stencil
973 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
974 kKeep_StencilOp,
975 kKeep_StencilOp,
976 kAlwaysIfInClip_StencilFunc,
977 0x0000,
978 0x0000,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000979 0x0000);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000980 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
981}
982}
983
984void GrClipMaskManager::setGpuStencil() {
985 // We make two copies of the StencilSettings here (except in the early
986 // exit scenario. One copy from draw state to the stack var. Then another
987 // from the stack var to the gpu. We could make this class hold a ptr to
988 // GrGpu's fStencilSettings and eliminate the stack copy here.
989
990 const GrDrawState& drawState = fGpu->getDrawState();
991
992 // use stencil for clipping if clipping is enabled and the clip
993 // has been written into the stencil.
994 GrClipMaskManager::StencilClipMode clipMode;
995 if (this->isClipInStencil() && drawState.isClipState()) {
996 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
997 // We can't be modifying the clip and respecting it at the same time.
998 GrAssert(!drawState.isStateFlagEnabled(
999 GrGpu::kModifyStencilClip_StateBit));
1000 } else if (drawState.isStateFlagEnabled(
1001 GrGpu::kModifyStencilClip_StateBit)) {
1002 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1003 } else {
1004 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1005 }
1006
1007 GrStencilSettings settings;
1008 // The GrGpu client may not be using the stencil buffer but we may need to
1009 // enable it in order to respect a stencil clip.
1010 if (drawState.getStencil().isDisabled()) {
1011 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1012 settings = basic_apply_stencil_clip_settings();
1013 } else {
1014 fGpu->disableStencil();
1015 return;
1016 }
1017 } else {
1018 settings = drawState.getStencil();
1019 }
1020
1021 // TODO: dynamically attach a stencil buffer
1022 int stencilBits = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001023 GrStencilBuffer* stencilBuffer =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001024 drawState.getRenderTarget()->getStencilBuffer();
1025 if (NULL != stencilBuffer) {
1026 stencilBits = stencilBuffer->bits();
1027 }
1028
bsalomon@google.comf6601872012-08-28 21:11:35 +00001029 GrAssert(fGpu->getCaps().stencilWrapOpsSupport() ||
bsalomon@google.com9e553c62012-06-22 12:23:29 +00001030 !settings.usesWrapOp());
bsalomon@google.comf6601872012-08-28 21:11:35 +00001031 GrAssert(fGpu->getCaps().twoSidedStencilSupport() || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001032 this->adjustStencilParams(&settings, clipMode, stencilBits);
1033 fGpu->setStencilSettings(settings);
1034}
1035
1036void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
1037 StencilClipMode mode,
1038 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001039 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001040
1041 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001042 // We assume that this clip manager itself is drawing to the GrGpu and
1043 // has already setup the correct values.
1044 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001045 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001046
bsalomon@google.com411dad02012-06-05 20:24:20 +00001047 unsigned int clipBit = (1 << (stencilBitCnt - 1));
1048 unsigned int userBits = clipBit - 1;
1049
bsalomon@google.coma3201942012-06-21 19:58:20 +00001050 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
bsalomon@google.comf6601872012-08-28 21:11:35 +00001051 bool twoSided = fGpu->getCaps().twoSidedStencilSupport();
bsalomon@google.com411dad02012-06-05 20:24:20 +00001052
bsalomon@google.coma3201942012-06-21 19:58:20 +00001053 bool finished = false;
1054 while (!finished) {
1055 GrStencilFunc func = settings->func(face);
1056 uint16_t writeMask = settings->writeMask(face);
1057 uint16_t funcMask = settings->funcMask(face);
1058 uint16_t funcRef = settings->funcRef(face);
1059
1060 GrAssert((unsigned) func < kStencilFuncCount);
1061
1062 writeMask &= userBits;
1063
1064 if (func >= kBasicStencilFuncCount) {
1065 int respectClip = kRespectClip_StencilClipMode == mode;
1066 if (respectClip) {
1067 // The GrGpu class should have checked this
1068 GrAssert(this->isClipInStencil());
1069 switch (func) {
1070 case kAlwaysIfInClip_StencilFunc:
1071 funcMask = clipBit;
1072 funcRef = clipBit;
1073 break;
1074 case kEqualIfInClip_StencilFunc:
1075 case kLessIfInClip_StencilFunc:
1076 case kLEqualIfInClip_StencilFunc:
1077 funcMask = (funcMask & userBits) | clipBit;
1078 funcRef = (funcRef & userBits) | clipBit;
1079 break;
1080 case kNonZeroIfInClip_StencilFunc:
1081 funcMask = (funcMask & userBits) | clipBit;
1082 funcRef = clipBit;
1083 break;
1084 default:
1085 GrCrash("Unknown stencil func");
1086 }
1087 } else {
1088 funcMask &= userBits;
1089 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001090 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001091 const GrStencilFunc* table =
bsalomon@google.coma3201942012-06-21 19:58:20 +00001092 gSpecialToBasicStencilFunc[respectClip];
1093 func = table[func - kBasicStencilFuncCount];
1094 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001095 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001096 funcMask &= userBits;
1097 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001098 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001099
1100 settings->setFunc(face, func);
1101 settings->setWriteMask(face, writeMask);
1102 settings->setFuncMask(face, funcMask);
1103 settings->setFuncRef(face, funcRef);
1104
1105 if (GrStencilSettings::kFront_Face == face) {
1106 face = GrStencilSettings::kBack_Face;
1107 finished = !twoSided;
1108 } else {
1109 finished = true;
1110 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001111 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001112 if (!twoSided) {
1113 settings->copyFrontSettingsToBack();
1114 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001115}
1116
1117////////////////////////////////////////////////////////////////////////////////
1118
robertphillips@google.comfa662942012-05-17 12:20:22 +00001119namespace {
1120
1121GrPathFill invert_fill(GrPathFill fill) {
1122 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001123 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1124 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1125 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1126 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1127 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001128 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001129 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1130 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1131 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1132 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1133 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1134 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001135 return gInvertedFillTable[fill];
1136}
1137
1138}
1139
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001140bool GrClipMaskManager::createSoftwareClipMask(const GrClipData& clipDataIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001141 GrTexture** result,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001142 GrIRect* devResultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001143 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001144
robertphillips@google.com7b112892012-07-31 15:18:21 +00001145 if (this->clipMaskPreamble(clipDataIn, result, devResultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001146 return true;
1147 }
1148
robertphillips@google.comf105b102012-05-14 12:18:26 +00001149 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001150 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001151 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001152 return false;
1153 }
1154
robertphillips@google.com2c756812012-05-22 20:28:23 +00001155 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001156
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001157 SkMatrix matrix;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001158 matrix.setTranslate(SkIntToScalar(-clipDataIn.fOrigin.fX),
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001159 SkIntToScalar(-clipDataIn.fOrigin.fY));
robertphillips@google.com7b112892012-07-31 15:18:21 +00001160 helper.init(*devResultBounds, &matrix);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001161
robertphillips@google.comfa662942012-05-17 12:20:22 +00001162 bool clearToInside;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001163 SkRegion::Op firstOp = SkRegion::kReplace_Op; // suppress warning
1164
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001165 SkClipStack::Iter iter(*clipDataIn.fClipStack,
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001166 SkClipStack::Iter::kBottom_IterStart);
1167 const SkClipStack::Iter::Clip* clip = process_initial_clip_elements(&iter,
robertphillips@google.com7b112892012-07-31 15:18:21 +00001168 *devResultBounds,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001169 &clearToInside,
robertphillips@google.comf8d904a2012-07-31 12:18:16 +00001170 &firstOp,
1171 clipDataIn);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001172
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001173 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001174
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001175 bool first = true;
bsalomon@google.come8ca6c62012-11-07 21:19:10 +00001176 for ( ; NULL != clip; clip = iter.nextCombined()) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001177
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001178 SkRegion::Op op = clip->fOp;
1179 if (first) {
1180 first = false;
1181 op = firstOp;
1182 }
robertphillips@google.comfa662942012-05-17 12:20:22 +00001183
1184 if (SkRegion::kIntersect_Op == op ||
1185 SkRegion::kReverseDifference_Op == op) {
1186 // Intersect and reverse difference require modifying pixels
1187 // outside of the geometry that is being "drawn". In both cases
1188 // we erase all the pixels outside of the geometry but
1189 // leave the pixels inside the geometry alone. For reverse
1190 // difference we invert all the pixels before clearing the ones
1191 // outside the geometry.
1192 if (SkRegion::kReverseDifference_Op == op) {
robertphillips@google.com7b112892012-07-31 15:18:21 +00001193 SkRect temp;
1194 temp.set(*devResultBounds);
robertphillips@google.comba998f22012-10-12 11:33:56 +00001195 temp.offset(SkIntToScalar(clipDataIn.fOrigin.fX),
1196 SkIntToScalar(clipDataIn.fOrigin.fX));
robertphillips@google.comfa662942012-05-17 12:20:22 +00001197
1198 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001199 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001200 }
1201
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001202 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001203
1204 // convert the rect to a path so we can invert the fill
1205 SkPath temp;
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001206 temp.addRect(*clip->fRect);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001207
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001208 helper.draw(temp, SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001209 kInverseEvenOdd_GrPathFill, clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001210 0x00);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001211 } else if (NULL != clip->fPath) {
1212 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001213 SkRegion::kReplace_Op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001214 invert_fill(get_path_fill(*clip->fPath)),
1215 clip->fDoAA,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001216 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001217 }
1218
1219 continue;
1220 }
1221
1222 // The other ops (union, xor, diff) only affect pixels inside
1223 // the geometry so they can just be drawn normally
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001224 if (NULL != clip->fRect) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001225
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001226 helper.draw(*clip->fRect,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001227 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001228 clip->fDoAA, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001229
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001230 } else if (NULL != clip->fPath) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001231 helper.draw(*clip->fPath,
robertphillips@google.comfa662942012-05-17 12:20:22 +00001232 op,
robertphillips@google.coma6f11c42012-07-23 17:39:44 +00001233 get_path_fill(*clip->fPath),
1234 clip->fDoAA, 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001235 }
1236 }
1237
robertphillips@google.comfa662942012-05-17 12:20:22 +00001238 // Because we are using the scratch texture cache, "accum" may be
1239 // larger than expected and have some cruft in the areas we aren't using.
1240 // Clear it out.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001241 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001242
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001243 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001244
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001245 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001246
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001247 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001248 return true;
1249}
1250
robertphillips@google.comf294b772012-04-27 14:29:26 +00001251////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001252void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001253 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001254}