blob: cdc13f0ca7d5e5149a6da77f19f9befcb413d2b9 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrClipMaskManager.h"
10#include "GrGpu.h"
11#include "GrRenderTarget.h"
12#include "GrStencilBuffer.h"
13#include "GrPathRenderer.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000014#include "GrPaint.h"
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000015#include "SkRasterClip.h"
robertphillips@google.comfa662942012-05-17 12:20:22 +000016#include "GrAAConvexPathRenderer.h"
17#include "GrAAHairLinePathRenderer.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018#include "GrSWMaskHelper.h"
robertphillips@google.coma72eef32012-05-01 17:22:59 +000019
20//#define GR_AA_CLIP 1
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000021//#define GR_SW_CLIP 1
robertphillips@google.coma72eef32012-05-01 17:22:59 +000022
robertphillips@google.comf294b772012-04-27 14:29:26 +000023////////////////////////////////////////////////////////////////////////////////
robertphillips@google.coma72eef32012-05-01 17:22:59 +000024namespace {
25// set up the draw state to enable the aa clipping mask. Besides setting up the
26// sampler matrix this also alters the vertex layout
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000027void setup_drawstate_aaclip(GrGpu* gpu,
28 GrTexture* result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000029 const GrIRect &bound) {
robertphillips@google.coma72eef32012-05-01 17:22:59 +000030 GrDrawState* drawState = gpu->drawState();
31 GrAssert(drawState);
32
33 static const int maskStage = GrPaint::kTotalStages+1;
34
35 GrMatrix mat;
36 mat.setIDiv(result->width(), result->height());
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000037 mat.preTranslate(SkIntToScalar(-bound.fLeft), SkIntToScalar(-bound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000038 mat.preConcat(drawState->getViewMatrix());
39
40 drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode,
41 GrSamplerState::kNearest_Filter,
42 mat);
43
44 drawState->setTexture(maskStage, result);
robertphillips@google.coma72eef32012-05-01 17:22:59 +000045}
46
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000047bool path_needs_SW_renderer(GrContext* context,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000048 GrGpu* gpu,
49 const SkPath& path,
50 GrPathFill fill,
51 bool doAA) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000052 // last (false) parameter disallows use of the SW path renderer
53 return NULL == context->getPathRenderer(path, fill, gpu, doAA, false);
54}
55
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000056}
57
robertphillips@google.comfa662942012-05-17 12:20:22 +000058/*
59 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
60 * will be used on any element. If so, it returns true to indicate that the
61 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
62 */
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000063bool GrClipMaskManager::useSWOnlyPath(const GrClip& clipIn) {
robertphillips@google.coma3e5c632012-05-22 18:09:26 +000064
65 if (!clipIn.requiresAA()) {
66 // The stencil buffer can handle this case
67 return false;
68 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000069
robertphillips@google.com8a4fc402012-05-24 12:42:24 +000070 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +000071 // a clip gets complex enough it can just be done in SW regardless
72 // of whether it would invoke the GrSoftwarePathRenderer.
73 bool useSW = false;
74
75 for (int i = 0; i < clipIn.getElementCount(); ++i) {
76
77 if (SkRegion::kReplace_Op == clipIn.getOp(i)) {
78 // Everything before a replace op can be ignored so start
79 // afresh w.r.t. determining if any element uses the SW path
80 useSW = false;
81 }
82
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000083 // rects can always be drawn directly w/o using the software path
84 // so only paths need to be checked
85 if (kPath_ClipType == clipIn.getElementType(i) &&
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000086 path_needs_SW_renderer(this->getContext(), fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +000087 clipIn.getPath(i),
88 clipIn.getPathFill(i),
89 clipIn.getDoAA(i))) {
90 useSW = true;
robertphillips@google.comfa662942012-05-17 12:20:22 +000091 }
robertphillips@google.comfa662942012-05-17 12:20:22 +000092 }
93
94 return useSW;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000095}
96
robertphillips@google.comf294b772012-04-27 14:29:26 +000097////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +000098// sort out what kind of clip mask needs to be created: alpha, stencil,
99// scissor, or entirely software
bsalomon@google.coma3201942012-06-21 19:58:20 +0000100bool GrClipMaskManager::setupClipping(const GrClip& clipIn) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000101 fCurrClipMaskType = kNone_ClipMaskType;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000102
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000103 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000104 if (!drawState->isClipState() || clipIn.isEmpty()) {
105 fGpu->disableScissor();
106 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000107 return true;
108 }
109
110 GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000111 // GrDrawTarget should have filtered this for us
112 GrAssert(NULL != rt);
113
bsalomon@google.coma3201942012-06-21 19:58:20 +0000114 GrIRect bounds;
115 GrIRect rtRect;
116 rtRect.setLTRB(0, 0, rt->width(), rt->height());
117 if (clipIn.hasConservativeBounds()) {
118 GrRect softBounds = clipIn.getConservativeBounds();
119 softBounds.roundOut(&bounds);
120 if (!bounds.intersect(rtRect)) {
121 bounds.setEmpty();
122 }
123 if (bounds.isEmpty()) {
124 return false;
125 }
126 } else {
127 bounds = rtRect;
128 }
129
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000130#if GR_SW_CLIP
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000131 // If MSAA is enabled we can do everything in the stencil buffer.
132 // Otherwise check if we should just create the entire clip mask
133 // in software (this will only happen if the clip mask is anti-aliased
134 // and too complex for the gpu to handle in its entirety)
bsalomon@google.coma3201942012-06-21 19:58:20 +0000135 if (0 == rt->numSamples() && this->useSWOnlyPath(clipIn)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000136 // The clip geometry is complex enough that it will be more
137 // efficient to create it entirely in software
138 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000139 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000140 if (this->createSoftwareClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000141 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000142 fGpu->disableScissor();
143 this->setGpuStencil();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000144 return true;
145 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000146
147 // if SW clip mask creation fails fall through to the other
148 // two possible methods (bottoming out at stencil clipping)
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000149 }
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000150#endif // GR_SW_CLIP
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000151
robertphillips@google.comf294b772012-04-27 14:29:26 +0000152#if GR_AA_CLIP
153 // If MSAA is enabled use the (faster) stencil path for AA clipping
154 // otherwise the alpha clip mask is our only option
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000155 if (0 == rt->numSamples() && clipIn.requiresAA()) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000156 // Since we are going to create a destination texture of the correct
157 // size for the mask (rather than being bound by the size of the
158 // render target) we aren't going to use scissoring like the stencil
159 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000160 GrTexture* result = NULL;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000161 GrIRect bound;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000162 if (this->createAlphaClipMask(clipIn, &result, &bound)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000163 setup_drawstate_aaclip(fGpu, result, bound);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000164 fGpu->disableScissor();
165 this->setGpuStencil();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000166 return true;
167 }
168
169 // if alpha clip mask creation fails fall through to the stencil
170 // buffer method
171 }
172#endif // GR_AA_CLIP
173
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000174 // Either a hard (stencil buffer) clip was explicitly requested or
175 // an antialiased clip couldn't be created. In either case, free up
176 // the texture in the antialiased mask cache.
177 // TODO: this may require more investigation. Ganesh performs a lot of
robertphillips@google.coma3e5c632012-05-22 18:09:26 +0000178 // utility draws (e.g., clears, InOrderDrawBuffer playbacks) that hit
179 // the stencil buffer path. These may be "incorrectly" clearing the
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000180 // AA cache.
181 fAACache.reset();
182
bsalomon@google.coma3201942012-06-21 19:58:20 +0000183 // If the clip is a rectangle then just set the scissor. Otherwise, create
184 // a stencil mask.
185 if (clipIn.isRect()) {
186 fGpu->enableScissor(bounds);
187 this->setGpuStencil();
188 return true;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000189 }
190
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000191 // use the stencil clip if we can't represent the clip as a rectangle.
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000192 bool useStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
193 !bounds.isEmpty();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000194
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000195 if (useStencil) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000196 this->createStencilClipMask(clipIn, bounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000197 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000198 // This must occur after createStencilClipMask. That function may change
199 // the scissor. Also, it only guarantees that the stencil mask is correct
200 // within the bounds it was passed, so we must use both stencil and scissor
201 // test to the bounds for the final draw.
202 fGpu->enableScissor(bounds);
203 this->setGpuStencil();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000204 return true;
205}
206
207#define VISUALIZE_COMPLEX_CLIP 0
208
209#if VISUALIZE_COMPLEX_CLIP
210 #include "GrRandom.h"
211 GrRandom gRandom;
212 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
213#else
214 #define SET_RANDOM_COLOR
215#endif
216
217namespace {
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000218/**
219 * Does "container" contain "containee"? If either is empty then
220 * no containment is possible.
221 */
222bool contains(const SkRect& container, const SkIRect& containee) {
223 return !containee.isEmpty() && !container.isEmpty() &&
224 container.fLeft <= SkIntToScalar(containee.fLeft) &&
225 container.fTop <= SkIntToScalar(containee.fTop) &&
226 container.fRight >= SkIntToScalar(containee.fRight) &&
227 container.fBottom >= SkIntToScalar(containee.fBottom);
228}
229
230
robertphillips@google.comf294b772012-04-27 14:29:26 +0000231////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000232// determines how many elements at the head of the clip can be skipped and
233// whether the initial clear should be to the inside- or outside-the-clip value,
234// and what op should be used to draw the first element that isn't skipped.
235int process_initial_clip_elements(const GrClip& clip,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000236 const GrIRect& bounds,
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000237 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000238 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000239
240 // logically before the first element of the clip stack is
241 // processed the clip is entirely open. However, depending on the
242 // first set op we may prefer to clear to 0 for performance. We may
243 // also be able to skip the initial clip paths/rects. We loop until
244 // we cannot skip an element.
245 int curr;
246 bool done = false;
247 *clearToInside = true;
248 int count = clip.getElementCount();
249
250 for (curr = 0; curr < count && !done; ++curr) {
251 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000252 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000253 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000254 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000255 *clearToInside = false;
256 done = true;
257 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000258 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000259 // if this element contains the entire bounds then we
260 // can skip it.
261 if (kRect_ClipType == clip.getElementType(curr)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000262 && contains(clip.getRect(curr), bounds)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000263 break;
264 }
265 // if everything is initially clearToInside then intersect is
266 // same as clear to 0 and treat as a replace. Otherwise,
267 // set stays empty.
268 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000269 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000270 *clearToInside = false;
271 done = true;
272 }
273 break;
274 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000275 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000276 // if everything is initially outside then union is
277 // same as replace. Otherwise, every pixel is still
278 // clearToInside
279 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000280 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000281 done = true;
282 }
283 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000284 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000285 // xor is same as difference or replace both of which
286 // can be 1-pass instead of 2 for xor.
287 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000288 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000289 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000290 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000291 }
292 done = true;
293 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000294 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000295 // if all pixels are clearToInside then we have to process the
296 // difference, otherwise it has no effect and all pixels
297 // remain outside.
298 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000299 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000300 done = true;
301 }
302 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000303 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000304 // if all pixels are clearToInside then reverse difference
305 // produces empty set. Otherise it is same as replace
306 if (*clearToInside) {
307 *clearToInside = false;
308 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000309 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000310 done = true;
311 }
312 break;
313 default:
314 GrCrash("Unknown set op.");
315 }
316 }
317 return done ? curr-1 : count;
318}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000319
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000320}
321
robertphillips@google.comf294b772012-04-27 14:29:26 +0000322
323namespace {
324
325////////////////////////////////////////////////////////////////////////////////
326// set up the OpenGL blend function to perform the specified
327// boolean operation for alpha clip mask creation
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000328void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000329
330 switch (op) {
331 case SkRegion::kReplace_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000332 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000333 break;
334 case SkRegion::kIntersect_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000335 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000336 break;
337 case SkRegion::kUnion_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000338 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000339 break;
340 case SkRegion::kXOR_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000341 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000342 break;
343 case SkRegion::kDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000344 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000345 break;
346 case SkRegion::kReverseDifference_Op:
bsalomon@google.com47059542012-06-06 20:51:20 +0000347 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000348 break;
349 default:
350 GrAssert(false);
351 break;
352 }
353}
354
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000356bool draw_path_in_software(GrContext* context,
357 GrGpu* gpu,
358 const SkPath& path,
359 GrPathFill fill,
360 bool doAA,
361 const GrIRect& resultBounds) {
362
363 GrAutoScratchTexture ast;
364
365 if (!GrSWMaskHelper::DrawToTexture(context, path, resultBounds, fill,
366 &ast, doAA, NULL)) {
367 return false;
368 }
369
370 // TODO: merge this with the similar code in the GrSoftwarePathRenderer.cpp
371 SkAutoTUnref<GrTexture> texture(ast.detach());
372 GrAssert(NULL != texture);
373
374 GrDrawState::StageMask stageMask = 0;
375 GrDrawTarget::AutoDeviceCoordDraw adcd(gpu, stageMask);
376 enum {
377 // the SW path renderer shares this stage with glyph
378 // rendering (kGlyphMaskStage in GrBatchedTextContext)
379 kPathMaskStage = GrPaint::kTotalStages,
380 };
381 GrAssert(NULL == gpu->drawState()->getTexture(kPathMaskStage));
382 gpu->drawState()->setTexture(kPathMaskStage, texture);
383 gpu->drawState()->sampler(kPathMaskStage)->reset();
384 GrScalar w = GrIntToScalar(resultBounds.width());
385 GrScalar h = GrIntToScalar(resultBounds.height());
386 GrRect maskRect = GrRect::MakeWH(w / texture->width(),
387 h / texture->height());
388
389 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
390 srcRects[kPathMaskStage] = &maskRect;
391 stageMask |= 1 << kPathMaskStage;
392 GrRect dstRect = GrRect::MakeWH(
393 SK_Scalar1* resultBounds.width(),
394 SK_Scalar1* resultBounds.height());
395 gpu->drawRect(dstRect, NULL, stageMask, srcRects, NULL);
396 gpu->drawState()->setTexture(kPathMaskStage, NULL);
397 GrAssert(!GrIsFillInverted(fill));
398 return true;
399}
400
401
402////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000403bool draw_path(GrContext* context,
404 GrGpu* gpu,
405 const SkPath& path,
406 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000407 bool doAA,
408 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000409
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000410 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000411 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000412 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000413 }
414
415 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
416 return true;
417}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000418
419}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000420
421////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000422bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000423 const GrClip& clipIn,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000424 int index,
425 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000426 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000427 GrAssert(NULL != drawState);
428
429 drawState->setRenderTarget(target->asRenderTarget());
430
431 if (kRect_ClipType == clipIn.getElementType(index)) {
432 if (clipIn.getDoAA(index)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000433 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000434 clipIn.getRect(index),
435 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000436 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000437 fGpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000438 }
439 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000440 return draw_path(this->getContext(), fGpu,
robertphillips@google.com2c756812012-05-22 20:28:23 +0000441 clipIn.getPath(index),
442 clipIn.getPathFill(index),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000443 clipIn.getDoAA(index),
444 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000445 }
446 return true;
447}
448
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000449void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000450 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000451 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000452 GrAssert(NULL != drawState);
453
454 // no AA here since it is encoded in the texture
455 drawState->setRenderTarget(target->asRenderTarget());
456
457 GrMatrix sampleM;
458 sampleM.setIDiv(texture->width(), texture->height());
459 drawState->setTexture(0, texture);
460
461 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
462 GrSamplerState::kNearest_Filter,
463 sampleM);
464
robertphillips@google.comf105b102012-05-14 12:18:26 +0000465 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
466 SkIntToScalar(target->height()));
467
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000468 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000469
470 drawState->setTexture(0, NULL);
471}
472
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000473// get a texture to act as a temporary buffer for AA clip boolean operations
474// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000475void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000476 GrAutoScratchTexture* temp) {
477 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000478 // we've already allocated the temp texture
479 return;
480 }
481
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000482 GrTextureDesc desc;
483 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
484 desc.fWidth = bounds.width();
485 desc.fHeight = bounds.height();
486 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000487
robertphillips@google.com2c756812012-05-22 20:28:23 +0000488 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000489}
490
robertphillips@google.comf105b102012-05-14 12:18:26 +0000491
492void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000493 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000494 // Since we are setting up the cache we know the last lookup was a miss
495 // Free up the currently cached mask so it can be reused
496 fAACache.reset();
497
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000498 GrTextureDesc desc;
499 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
500 desc.fWidth = bounds.width();
501 desc.fHeight = bounds.height();
502 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000503
504 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000505}
506
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000507////////////////////////////////////////////////////////////////////////////////
508// Shared preamble between gpu and SW-only AA clip mask creation paths.
509// Handles caching, determination of clip mask bound & allocation (if needed)
510// of the result texture
511// Returns true if there is no more work to be done (i.e., we got a cache hit)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000512bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000513 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000514 GrIRect* resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000515 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000516 GrAssert(origDrawState->isClipState());
517
518 GrRenderTarget* rt = origDrawState->getRenderTarget();
519 GrAssert(NULL != rt);
520
521 GrRect rtRect;
522 rtRect.setLTRB(0, 0,
523 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
524
525 // unlike the stencil path the alpha path is not bound to the size of the
526 // render target - determine the minimum size required for the mask
527 GrRect bounds;
528
529 if (clipIn.hasConservativeBounds()) {
530 bounds = clipIn.getConservativeBounds();
531 if (!bounds.intersect(rtRect)) {
532 // the mask will be empty in this case
533 GrAssert(false);
534 bounds.setEmpty();
535 }
536 } else {
537 // still locked to the size of the render target
538 bounds = rtRect;
539 }
540
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000541 GrIRect intBounds;
542 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000543
544 // need to outset a pixel since the standard bounding box computation
545 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000546 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000547
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000548 // TODO: make sure we don't outset if bounds are still 0,0 @ min
549
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000550 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000551 intBounds.width(),
552 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000553 *result = fAACache.getLastMask();
554 fAACache.getLastBound(resultBounds);
555 return true;
556 }
557
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000558 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000559
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000560 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000561 return false;
562}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000563
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000564////////////////////////////////////////////////////////////////////////////////
565// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000566bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000567 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000568 GrIRect *resultBounds) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000569 GrAssert(NULL != resultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000570 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
571
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000572 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000573 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000574 return true;
575 }
576
robertphillips@google.comf105b102012-05-14 12:18:26 +0000577 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000578 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000579 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000580 return false;
581 }
582
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000583 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
584 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000585
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000586 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000587
588 int count = clipIn.getElementCount();
589
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000590 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000591 // if we were able to trim down the size of the mask we need to
592 // offset the paths & rects that will be used to compute it
593 GrMatrix m;
594
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000595 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
596 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000597
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000598 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000599 }
600
601 bool clearToInside;
602 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
603 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000604 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605 &clearToInside,
606 &startOp);
607
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000608 fGpu->clear(NULL,
609 clearToInside ? 0xffffffff : 0x00000000,
610 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000611
robertphillips@google.comf105b102012-05-14 12:18:26 +0000612 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000613
robertphillips@google.comf294b772012-04-27 14:29:26 +0000614 // walk through each clip element and perform its set op
615 for (int c = start; c < count; ++c) {
616
617 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
618
619 if (SkRegion::kReplace_Op == op) {
620 // TODO: replace is actually a lot faster then intersection
621 // for this path - refactor the stencil path so it can handle
622 // replace ops and alter GrClip to allow them through
623
624 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000625 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000627 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000628 this->drawClipShape(accum, clipIn, c, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000629
630 } else if (SkRegion::kReverseDifference_Op == op ||
631 SkRegion::kIntersect_Op == op) {
632 // there is no point in intersecting a screen filling rectangle.
633 if (SkRegion::kIntersect_Op == op &&
634 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000635 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000636 continue;
637 }
638
robertphillips@google.comf105b102012-05-14 12:18:26 +0000639 getTemp(*resultBounds, &temp);
640 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000641 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000642 return false;
643 }
644
robertphillips@google.comf294b772012-04-27 14:29:26 +0000645 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000646 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000648 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000649 this->drawClipShape(temp.texture(), clipIn, c, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000650
651 // TODO: rather than adding these two translations here
652 // compute the bounding box needed to render the texture
653 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000654 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000655 GrMatrix m;
656
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000657 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
658 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000659
660 drawState->preConcatViewMatrix(m);
661 }
662
663 // Now draw into the accumulator using the real operation
664 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000665 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000666 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000667
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000668 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000669 GrMatrix m;
670
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000671 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
672 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000673
674 drawState->preConcatViewMatrix(m);
675 }
676
677 } else {
678 // all the remaining ops can just be directly draw into
679 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000680 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000681 this->drawClipShape(accum, clipIn, c, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000682 }
683 }
684
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000685 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000686 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000687 return true;
688}
689
690////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000691// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000692bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000693 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000695 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000697 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698 GrAssert(drawState->isClipState());
699
700 GrRenderTarget* rt = drawState->getRenderTarget();
701 GrAssert(NULL != rt);
702
703 // TODO: dynamically attach a SB when needed.
704 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
705 if (NULL == stencilBuffer) {
706 return false;
707 }
708
709 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
710
711 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
712
713 // we set the current clip to the bounds so that our recursive
714 // draws are scissored to them. We use the copy of the complex clip
715 // we just stashed on the SB to render from. We set it back after
716 // we finish drawing it into the stencil.
717 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000718 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000720 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
721 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000723 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725#if !VISUALIZE_COMPLEX_CLIP
726 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
727#endif
728
729 int count = clipCopy.getElementCount();
730 int clipBit = stencilBuffer->bits();
731 SkASSERT((clipBit <= 16) &&
732 "Ganesh only handles 16b or smaller stencil buffers");
733 clipBit = (1 << (clipBit-1));
734
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000735 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000736
737 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000738 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739 int start = process_initial_clip_elements(clipCopy,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000740 rtRect,
741 &clearToInside,
742 &startOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743
bsalomon@google.coma3201942012-06-21 19:58:20 +0000744 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000745
746 // walk through each clip element and perform its set op
747 // with the existing clip.
748 for (int c = start; c < count; ++c) {
749 GrPathFill fill;
750 bool fillInverted;
751 // enabled at bottom of loop
752 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000753 // if the target is MSAA then we want MSAA enabled when the clip is soft
754 if (rt->isMultisampled()) {
755 if (clipCopy.getDoAA(c)) {
756 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
757 } else {
758 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
759 }
760 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000761
762 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000763 // directly to the stencil buffer
764 // with a non-inverted fill rule
765 // without extra passes to
766 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000767
robertphillips@google.comf294b772012-04-27 14:29:26 +0000768 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
769
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000770 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000771 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000772 if (kRect_ClipType == clipCopy.getElementType(c)) {
773 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000774 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 fillInverted = false;
776 // there is no point in intersecting a screen filling
777 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000778 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000779 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000780 continue;
781 }
782 } else {
783 fill = clipCopy.getPathFill(c);
784 fillInverted = GrIsFillInverted(fill);
785 fill = GrNonInvertedFill(fill);
786 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000787 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000788 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000789 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000790 if (NULL == pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000791 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000792 return false;
793 }
794 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000795 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000796 }
797
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000798 int passes;
799 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
800
801 bool canDrawDirectToClip; // Given the renderer, the element,
802 // fill rule, and set operation can
803 // we render the element directly to
804 // stencil bit used for clipping.
805 canDrawDirectToClip =
806 GrStencilSettings::GetClipPasses(op,
807 canRenderDirectToStencil,
808 clipBit,
809 fillInverted,
810 &passes, stencilSettings);
811
812 // draw the element to the client stencil bits if necessary
813 if (!canDrawDirectToClip) {
814 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
815 kIncClamp_StencilOp,
816 kIncClamp_StencilOp,
817 kAlways_StencilFunc,
818 0xffff,
819 0x0000,
820 0xffff);
821 SET_RANDOM_COLOR
822 if (kRect_ClipType == clipCopy.getElementType(c)) {
823 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000824 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000825 } else {
826 if (canRenderDirectToStencil) {
827 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000828 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000829 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000830 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000831 }
832 }
833 }
834
835 // now we modify the clip bit by rendering either the clip
836 // element directly or a bounding rect of the entire clip.
837 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
838 for (int p = 0; p < passes; ++p) {
839 *drawState->stencil() = stencilSettings[p];
840 if (canDrawDirectToClip) {
841 if (kRect_ClipType == clipCopy.getElementType(c)) {
842 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000843 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844 } else {
845 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000846 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000847 }
848 } else {
849 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000850 GrRect rect = GrRect::MakeLTRB(
851 SkIntToScalar(bounds.fLeft),
852 SkIntToScalar(bounds.fTop),
853 SkIntToScalar(bounds.fRight),
854 SkIntToScalar(bounds.fBottom));
855 fGpu->drawSimpleRect(rect, NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000856 }
857 }
858 }
859 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000860 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000861 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000862 // set this last because recursive draws may overwrite it back to kNone.
863 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
864 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000865 return true;
866}
867
bsalomon@google.com411dad02012-06-05 20:24:20 +0000868// mapping of clip-respecting stencil funcs to normal stencil funcs
869// mapping depends on whether stencil-clipping is in effect.
870static const GrStencilFunc
871 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
872 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
873 // In the Clip Funcs
874 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
875 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
876 kLess_StencilFunc, // kLessIfInClip_StencilFunc
877 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
878 // Special in the clip func that forces user's ref to be 0.
879 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
880 // make ref 0 and do normal nequal.
881 },
882 {// Stencil-Clipping is ENABLED
883 // In the Clip Funcs
884 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
885 // eq stencil clip bit, mask
886 // out user bits.
887
888 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
889 // add stencil bit to mask and ref
890
891 kLess_StencilFunc, // kLessIfInClip_StencilFunc
892 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
893 // for both of these we can add
894 // the clip bit to the mask and
895 // ref and compare as normal
896 // Special in the clip func that forces user's ref to be 0.
897 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
898 // make ref have only the clip bit set
899 // and make comparison be less
900 // 10..0 < 1..user_bits..
901 }
902};
903
bsalomon@google.coma3201942012-06-21 19:58:20 +0000904namespace {
905// Sets the settings to clip against the stencil buffer clip while ignoring the
906// client bits.
907const GrStencilSettings& basic_apply_stencil_clip_settings() {
908 // stencil settings to use when clip is in stencil
909 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
910 kKeep_StencilOp,
911 kKeep_StencilOp,
912 kAlwaysIfInClip_StencilFunc,
913 0x0000,
914 0x0000,
915 0x0000);
916 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
917}
918}
919
920void GrClipMaskManager::setGpuStencil() {
921 // We make two copies of the StencilSettings here (except in the early
922 // exit scenario. One copy from draw state to the stack var. Then another
923 // from the stack var to the gpu. We could make this class hold a ptr to
924 // GrGpu's fStencilSettings and eliminate the stack copy here.
925
926 const GrDrawState& drawState = fGpu->getDrawState();
927
928 // use stencil for clipping if clipping is enabled and the clip
929 // has been written into the stencil.
930 GrClipMaskManager::StencilClipMode clipMode;
931 if (this->isClipInStencil() && drawState.isClipState()) {
932 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
933 // We can't be modifying the clip and respecting it at the same time.
934 GrAssert(!drawState.isStateFlagEnabled(
935 GrGpu::kModifyStencilClip_StateBit));
936 } else if (drawState.isStateFlagEnabled(
937 GrGpu::kModifyStencilClip_StateBit)) {
938 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
939 } else {
940 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
941 }
942
943 GrStencilSettings settings;
944 // The GrGpu client may not be using the stencil buffer but we may need to
945 // enable it in order to respect a stencil clip.
946 if (drawState.getStencil().isDisabled()) {
947 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
948 settings = basic_apply_stencil_clip_settings();
949 } else {
950 fGpu->disableStencil();
951 return;
952 }
953 } else {
954 settings = drawState.getStencil();
955 }
956
957 // TODO: dynamically attach a stencil buffer
958 int stencilBits = 0;
959 GrStencilBuffer* stencilBuffer =
960 drawState.getRenderTarget()->getStencilBuffer();
961 if (NULL != stencilBuffer) {
962 stencilBits = stencilBuffer->bits();
963 }
964
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000965 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
966 !settings.usesWrapOp());
967 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000968 this->adjustStencilParams(&settings, clipMode, stencilBits);
969 fGpu->setStencilSettings(settings);
970}
971
972void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
973 StencilClipMode mode,
974 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000975 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000976
977 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000978 // We assume that this clip manager itself is drawing to the GrGpu and
979 // has already setup the correct values.
980 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000981 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000982
bsalomon@google.com411dad02012-06-05 20:24:20 +0000983 unsigned int clipBit = (1 << (stencilBitCnt - 1));
984 unsigned int userBits = clipBit - 1;
985
bsalomon@google.coma3201942012-06-21 19:58:20 +0000986 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
987 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000988
bsalomon@google.coma3201942012-06-21 19:58:20 +0000989 bool finished = false;
990 while (!finished) {
991 GrStencilFunc func = settings->func(face);
992 uint16_t writeMask = settings->writeMask(face);
993 uint16_t funcMask = settings->funcMask(face);
994 uint16_t funcRef = settings->funcRef(face);
995
996 GrAssert((unsigned) func < kStencilFuncCount);
997
998 writeMask &= userBits;
999
1000 if (func >= kBasicStencilFuncCount) {
1001 int respectClip = kRespectClip_StencilClipMode == mode;
1002 if (respectClip) {
1003 // The GrGpu class should have checked this
1004 GrAssert(this->isClipInStencil());
1005 switch (func) {
1006 case kAlwaysIfInClip_StencilFunc:
1007 funcMask = clipBit;
1008 funcRef = clipBit;
1009 break;
1010 case kEqualIfInClip_StencilFunc:
1011 case kLessIfInClip_StencilFunc:
1012 case kLEqualIfInClip_StencilFunc:
1013 funcMask = (funcMask & userBits) | clipBit;
1014 funcRef = (funcRef & userBits) | clipBit;
1015 break;
1016 case kNonZeroIfInClip_StencilFunc:
1017 funcMask = (funcMask & userBits) | clipBit;
1018 funcRef = clipBit;
1019 break;
1020 default:
1021 GrCrash("Unknown stencil func");
1022 }
1023 } else {
1024 funcMask &= userBits;
1025 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001026 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001027 const GrStencilFunc* table =
1028 gSpecialToBasicStencilFunc[respectClip];
1029 func = table[func - kBasicStencilFuncCount];
1030 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001031 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001032 funcMask &= userBits;
1033 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001034 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001035
1036 settings->setFunc(face, func);
1037 settings->setWriteMask(face, writeMask);
1038 settings->setFuncMask(face, funcMask);
1039 settings->setFuncRef(face, funcRef);
1040
1041 if (GrStencilSettings::kFront_Face == face) {
1042 face = GrStencilSettings::kBack_Face;
1043 finished = !twoSided;
1044 } else {
1045 finished = true;
1046 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001047 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001048 if (!twoSided) {
1049 settings->copyFrontSettingsToBack();
1050 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001051}
1052
1053////////////////////////////////////////////////////////////////////////////////
1054
robertphillips@google.comfa662942012-05-17 12:20:22 +00001055namespace {
1056
1057GrPathFill invert_fill(GrPathFill fill) {
1058 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001059 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1060 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1061 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1062 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1063 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001064 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001065 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1066 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1067 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1068 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1069 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1070 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001071 return gInvertedFillTable[fill];
1072}
1073
1074}
1075
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001076bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001077 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001078 GrIRect* resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001079 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001080
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001081 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001082 return true;
1083 }
1084
robertphillips@google.comf105b102012-05-14 12:18:26 +00001085 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001086 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001087 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001088 return false;
1089 }
1090
robertphillips@google.com2c756812012-05-22 20:28:23 +00001091 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001092
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001093 helper.init(*resultBounds, NULL);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001094
1095 int count = clipIn.getElementCount();
1096
1097 bool clearToInside;
1098 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
1099 int start = process_initial_clip_elements(clipIn,
1100 *resultBounds,
1101 &clearToInside,
1102 &startOp);
1103
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001104 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001105
1106 for (int i = start; i < count; ++i) {
1107
1108 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
1109
1110 if (SkRegion::kIntersect_Op == op ||
1111 SkRegion::kReverseDifference_Op == op) {
1112 // Intersect and reverse difference require modifying pixels
1113 // outside of the geometry that is being "drawn". In both cases
1114 // we erase all the pixels outside of the geometry but
1115 // leave the pixels inside the geometry alone. For reverse
1116 // difference we invert all the pixels before clearing the ones
1117 // outside the geometry.
1118 if (SkRegion::kReverseDifference_Op == op) {
1119 SkRect temp = SkRect::MakeLTRB(
1120 SkIntToScalar(resultBounds->left()),
1121 SkIntToScalar(resultBounds->top()),
1122 SkIntToScalar(resultBounds->right()),
1123 SkIntToScalar(resultBounds->bottom()));
1124
1125 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001126 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001127 }
1128
1129 if (kRect_ClipType == clipIn.getElementType(i)) {
1130
1131 // convert the rect to a path so we can invert the fill
1132 SkPath temp;
1133 temp.addRect(clipIn.getRect(i));
1134
1135 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com47059542012-06-06 20:51:20 +00001136 kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001137 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001138 } else {
1139 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1140
1141 helper.draw(clipIn.getPath(i),
1142 SkRegion::kReplace_Op,
1143 invert_fill(clipIn.getPathFill(i)),
1144 clipIn.getDoAA(i),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001145 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001146 }
1147
1148 continue;
1149 }
1150
1151 // The other ops (union, xor, diff) only affect pixels inside
1152 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001153 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001154
1155 helper.draw(clipIn.getRect(i),
1156 op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001157 clipIn.getDoAA(i), 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001158
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001159 } else {
1160 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1161
robertphillips@google.comfa662942012-05-17 12:20:22 +00001162 helper.draw(clipIn.getPath(i),
1163 op,
1164 clipIn.getPathFill(i),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001165 clipIn.getDoAA(i), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001166 }
1167 }
1168
robertphillips@google.comfa662942012-05-17 12:20:22 +00001169 // Because we are using the scratch texture cache, "accum" may be
1170 // larger than expected and have some cruft in the areas we aren't using.
1171 // Clear it out.
1172
1173 // TODO: need a simpler way to clear the texture - can we combine
1174 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001175 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001176 GrAssert(NULL != drawState);
1177 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001178 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001179 // can't leave the accum bound as a rendertarget
1180 drawState->setRenderTarget(temp);
1181
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001182 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001183
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001184 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001185
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001186 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001187 return true;
1188}
1189
robertphillips@google.comf294b772012-04-27 14:29:26 +00001190////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001191void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001192 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001193}