blob: c37affa9397bde2c395e44c10f0b8464dd35bd0d [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
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000363 SkAutoTUnref<GrTexture> texture(
364 GrSWMaskHelper::DrawPathMaskToTexture(context, path,
365 resultBounds, fill,
366 doAA, NULL));
367 if (NULL == texture) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000368 return false;
369 }
370
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000371 // The ClipMaskManager accumulates the clip mask in the UL corner
372 GrIRect rect = GrIRect::MakeWH(resultBounds.width(), resultBounds.height());
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000373
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000374 GrSWMaskHelper::DrawToTargetWithPathMask(texture, gpu, 0, rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000375
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000376 GrAssert(!GrIsFillInverted(fill));
377 return true;
378}
379
380
381////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com2c756812012-05-22 20:28:23 +0000382bool draw_path(GrContext* context,
383 GrGpu* gpu,
384 const SkPath& path,
385 GrPathFill fill,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000386 bool doAA,
387 const GrIRect& resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000388
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000389 GrPathRenderer* pr = context->getPathRenderer(path, fill, gpu, doAA, false);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000390 if (NULL == pr) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000391 return draw_path_in_software(context, gpu, path, fill, doAA, resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000392 }
393
394 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
395 return true;
396}
robertphillips@google.com72176b22012-05-23 13:19:12 +0000397
398}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000399
400////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000401bool GrClipMaskManager::drawClipShape(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402 const GrClip& clipIn,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000403 int index,
404 const GrIRect& resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000405 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000406 GrAssert(NULL != drawState);
407
408 drawState->setRenderTarget(target->asRenderTarget());
409
410 if (kRect_ClipType == clipIn.getElementType(index)) {
411 if (clipIn.getDoAA(index)) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000412 getContext()->getAARectRenderer()->fillAARect(fGpu, fGpu,
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000413 clipIn.getRect(index),
414 true);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000415 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000416 fGpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000417 }
418 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000419 return draw_path(this->getContext(), fGpu,
robertphillips@google.com2c756812012-05-22 20:28:23 +0000420 clipIn.getPath(index),
421 clipIn.getPathFill(index),
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000422 clipIn.getDoAA(index),
423 resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000424 }
425 return true;
426}
427
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000428void GrClipMaskManager::drawTexture(GrTexture* target,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000429 GrTexture* texture) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000430 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431 GrAssert(NULL != drawState);
432
433 // no AA here since it is encoded in the texture
434 drawState->setRenderTarget(target->asRenderTarget());
435
436 GrMatrix sampleM;
437 sampleM.setIDiv(texture->width(), texture->height());
438 drawState->setTexture(0, texture);
439
440 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
441 GrSamplerState::kNearest_Filter,
442 sampleM);
443
robertphillips@google.comf105b102012-05-14 12:18:26 +0000444 GrRect rect = GrRect::MakeWH(SkIntToScalar(target->width()),
445 SkIntToScalar(target->height()));
446
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000447 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000448
449 drawState->setTexture(0, NULL);
450}
451
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000452// get a texture to act as a temporary buffer for AA clip boolean operations
453// TODO: given the expense of createTexture we may want to just cache this too
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000454void GrClipMaskManager::getTemp(const GrIRect& bounds,
robertphillips@google.comf105b102012-05-14 12:18:26 +0000455 GrAutoScratchTexture* temp) {
456 if (NULL != temp->texture()) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000457 // we've already allocated the temp texture
458 return;
459 }
460
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000461 GrTextureDesc desc;
462 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
463 desc.fWidth = bounds.width();
464 desc.fHeight = bounds.height();
465 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000466
robertphillips@google.com2c756812012-05-22 20:28:23 +0000467 temp->set(this->getContext(), desc);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000468}
469
robertphillips@google.comf105b102012-05-14 12:18:26 +0000470
471void GrClipMaskManager::setupCache(const GrClip& clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000472 const GrIRect& bounds) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000473 // Since we are setting up the cache we know the last lookup was a miss
474 // Free up the currently cached mask so it can be reused
475 fAACache.reset();
476
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000477 GrTextureDesc desc;
478 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
479 desc.fWidth = bounds.width();
480 desc.fHeight = bounds.height();
481 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comf105b102012-05-14 12:18:26 +0000482
483 fAACache.acquireMask(clipIn, desc, bounds);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000484}
485
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000486////////////////////////////////////////////////////////////////////////////////
487// Shared preamble between gpu and SW-only AA clip mask creation paths.
488// Handles caching, determination of clip mask bound & allocation (if needed)
489// of the result texture
490// 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 +0000491bool GrClipMaskManager::clipMaskPreamble(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000492 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000493 GrIRect* resultBounds) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000494 GrDrawState* origDrawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495 GrAssert(origDrawState->isClipState());
496
497 GrRenderTarget* rt = origDrawState->getRenderTarget();
498 GrAssert(NULL != rt);
499
500 GrRect rtRect;
501 rtRect.setLTRB(0, 0,
502 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
503
504 // unlike the stencil path the alpha path is not bound to the size of the
505 // render target - determine the minimum size required for the mask
506 GrRect bounds;
507
508 if (clipIn.hasConservativeBounds()) {
509 bounds = clipIn.getConservativeBounds();
510 if (!bounds.intersect(rtRect)) {
511 // the mask will be empty in this case
512 GrAssert(false);
513 bounds.setEmpty();
514 }
515 } else {
516 // still locked to the size of the render target
517 bounds = rtRect;
518 }
519
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000520 GrIRect intBounds;
521 bounds.roundOut(&intBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000522
523 // need to outset a pixel since the standard bounding box computation
524 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000525 intBounds.outset(1, 1);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000526
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000527 // TODO: make sure we don't outset if bounds are still 0,0 @ min
528
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000529 if (fAACache.canReuse(clipIn,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000530 intBounds.width(),
531 intBounds.height())) {
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000532 *result = fAACache.getLastMask();
533 fAACache.getLastBound(resultBounds);
534 return true;
535 }
536
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000537 this->setupCache(clipIn, intBounds);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000538
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000539 *resultBounds = intBounds;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000540 return false;
541}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000542
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000543////////////////////////////////////////////////////////////////////////////////
544// Create a 8-bit clip mask in alpha
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000545bool GrClipMaskManager::createAlphaClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000546 GrTexture** result,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000547 GrIRect *resultBounds) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000548 GrAssert(NULL != resultBounds);
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000549 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
550
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000551 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000552 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000553 return true;
554 }
555
robertphillips@google.comf105b102012-05-14 12:18:26 +0000556 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000557 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000558 fAACache.reset();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559 return false;
560 }
561
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000562 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
563 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000564
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000565 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000566
567 int count = clipIn.getElementCount();
568
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000569 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000570 // if we were able to trim down the size of the mask we need to
571 // offset the paths & rects that will be used to compute it
572 GrMatrix m;
573
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000574 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
575 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000576
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000577 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000578 }
579
580 bool clearToInside;
581 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
582 int start = process_initial_clip_elements(clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000583 *resultBounds,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000584 &clearToInside,
585 &startOp);
586
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000587 fGpu->clear(NULL,
588 clearToInside ? 0xffffffff : 0x00000000,
589 accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000590
robertphillips@google.comf105b102012-05-14 12:18:26 +0000591 GrAutoScratchTexture temp;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000592
robertphillips@google.comf294b772012-04-27 14:29:26 +0000593 // walk through each clip element and perform its set op
594 for (int c = start; c < count; ++c) {
595
596 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
597
598 if (SkRegion::kReplace_Op == op) {
599 // TODO: replace is actually a lot faster then intersection
600 // for this path - refactor the stencil path so it can handle
601 // replace ops and alter GrClip to allow them through
602
603 // clear the accumulator and draw the new object directly into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000604 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000606 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000607 this->drawClipShape(accum, clipIn, c, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608
609 } else if (SkRegion::kReverseDifference_Op == op ||
610 SkRegion::kIntersect_Op == op) {
611 // there is no point in intersecting a screen filling rectangle.
612 if (SkRegion::kIntersect_Op == op &&
613 kRect_ClipType == clipIn.getElementType(c) &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000614 contains(clipIn.getRect(c), *resultBounds)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000615 continue;
616 }
617
robertphillips@google.comf105b102012-05-14 12:18:26 +0000618 getTemp(*resultBounds, &temp);
619 if (NULL == temp.texture()) {
robertphillips@google.comf105b102012-05-14 12:18:26 +0000620 fAACache.reset();
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000621 return false;
622 }
623
robertphillips@google.comf294b772012-04-27 14:29:26 +0000624 // clear the temp target & draw into it
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000625 fGpu->clear(NULL, 0x00000000, temp.texture()->asRenderTarget());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000626
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000627 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000628 this->drawClipShape(temp.texture(), clipIn, c, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000629
630 // TODO: rather than adding these two translations here
631 // compute the bounding box needed to render the texture
632 // into temp
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000633 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000634 GrMatrix m;
635
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000636 m.setTranslate(SkIntToScalar(resultBounds->fLeft),
637 SkIntToScalar(resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000638
639 drawState->preConcatViewMatrix(m);
640 }
641
642 // Now draw into the accumulator using the real operation
643 // and the temp buffer as a texture
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000644 setup_boolean_blendcoeffs(drawState, op);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000645 this->drawTexture(accum, temp.texture());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000646
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000647 if (0 != resultBounds->fTop || 0 != resultBounds->fLeft) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000648 GrMatrix m;
649
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000650 m.setTranslate(SkIntToScalar(-resultBounds->fLeft),
651 SkIntToScalar(-resultBounds->fTop));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000652
653 drawState->preConcatViewMatrix(m);
654 }
655
656 } else {
657 // all the remaining ops can just be directly draw into
658 // the accumulation buffer
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000659 setup_boolean_blendcoeffs(drawState, op);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000660 this->drawClipShape(accum, clipIn, c, *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000661 }
662 }
663
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000664 *result = accum;
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000665 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000666 return true;
667}
668
669////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000670// Create a 1-bit clip mask in the stencil buffer
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000671bool GrClipMaskManager::createStencilClipMask(const GrClip& clipIn,
bsalomon@google.coma3201942012-06-21 19:58:20 +0000672 const GrIRect& bounds) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000673
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000674 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000675
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000676 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000677 GrAssert(drawState->isClipState());
678
679 GrRenderTarget* rt = drawState->getRenderTarget();
680 GrAssert(NULL != rt);
681
682 // TODO: dynamically attach a SB when needed.
683 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
684 if (NULL == stencilBuffer) {
685 return false;
686 }
687
688 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
689
690 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
691
692 // we set the current clip to the bounds so that our recursive
693 // draws are scissored to them. We use the copy of the complex clip
694 // we just stashed on the SB to render from. We set it back after
695 // we finish drawing it into the stencil.
696 const GrClip& clipCopy = stencilBuffer->getLastClip();
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000697 fGpu->setClip(GrClip(bounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000699 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
700 drawState = fGpu->drawState();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000701 drawState->setRenderTarget(rt);
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000702 GrDrawTarget::AutoGeometryPush agp(fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000703
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704#if !VISUALIZE_COMPLEX_CLIP
705 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
706#endif
707
708 int count = clipCopy.getElementCount();
709 int clipBit = stencilBuffer->bits();
710 SkASSERT((clipBit <= 16) &&
711 "Ganesh only handles 16b or smaller stencil buffers");
712 clipBit = (1 << (clipBit-1));
713
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000714 GrIRect rtRect = GrIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715
716 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000717 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000718 int start = process_initial_clip_elements(clipCopy,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000719 rtRect,
720 &clearToInside,
721 &startOp);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722
bsalomon@google.coma3201942012-06-21 19:58:20 +0000723 fGpu->clearStencilClip(bounds, clearToInside);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000724
725 // walk through each clip element and perform its set op
726 // with the existing clip.
727 for (int c = start; c < count; ++c) {
728 GrPathFill fill;
729 bool fillInverted;
730 // enabled at bottom of loop
731 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000732 // if the target is MSAA then we want MSAA enabled when the clip is soft
733 if (rt->isMultisampled()) {
734 if (clipCopy.getDoAA(c)) {
735 drawState->enableState(GrDrawState::kHWAntialias_StateBit);
736 } else {
737 drawState->disableState(GrDrawState::kHWAntialias_StateBit);
738 }
739 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000740
741 bool canRenderDirectToStencil; // can the clip element be drawn
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000742 // directly to the stencil buffer
743 // with a non-inverted fill rule
744 // without extra passes to
745 // resolve in/out status.
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746
robertphillips@google.comf294b772012-04-27 14:29:26 +0000747 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
748
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000749 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000750 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000751 if (kRect_ClipType == clipCopy.getElementType(c)) {
752 canRenderDirectToStencil = true;
bsalomon@google.com47059542012-06-06 20:51:20 +0000753 fill = kEvenOdd_GrPathFill;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000754 fillInverted = false;
755 // there is no point in intersecting a screen filling
756 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000757 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000758 contains(clipCopy.getRect(c), rtRect)) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000759 continue;
760 }
761 } else {
762 fill = clipCopy.getPathFill(c);
763 fillInverted = GrIsFillInverted(fill);
764 fill = GrNonInvertedFill(fill);
765 clipPath = &clipCopy.getPath(c);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000766 pr = this->getContext()->getPathRenderer(*clipPath,
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000767 fill, fGpu, false,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000768 true);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000769 if (NULL == pr) {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000770 fGpu->setClip(clipCopy); // restore to the original
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000771 return false;
772 }
773 canRenderDirectToStencil =
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000774 !pr->requiresStencilPass(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000775 }
776
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000777 int passes;
778 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
779
780 bool canDrawDirectToClip; // Given the renderer, the element,
781 // fill rule, and set operation can
782 // we render the element directly to
783 // stencil bit used for clipping.
784 canDrawDirectToClip =
785 GrStencilSettings::GetClipPasses(op,
786 canRenderDirectToStencil,
787 clipBit,
788 fillInverted,
789 &passes, stencilSettings);
790
791 // draw the element to the client stencil bits if necessary
792 if (!canDrawDirectToClip) {
793 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
794 kIncClamp_StencilOp,
795 kIncClamp_StencilOp,
796 kAlways_StencilFunc,
797 0xffff,
798 0x0000,
799 0xffff);
800 SET_RANDOM_COLOR
801 if (kRect_ClipType == clipCopy.getElementType(c)) {
802 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000803 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000804 } else {
805 if (canRenderDirectToStencil) {
806 *drawState->stencil() = gDrawToStencil;
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000807 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000808 } else {
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000809 pr->drawPathToStencil(*clipPath, fill, fGpu);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000810 }
811 }
812 }
813
814 // now we modify the clip bit by rendering either the clip
815 // element directly or a bounding rect of the entire clip.
816 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
817 for (int p = 0; p < passes; ++p) {
818 *drawState->stencil() = stencilSettings[p];
819 if (canDrawDirectToClip) {
820 if (kRect_ClipType == clipCopy.getElementType(c)) {
821 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000822 fGpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000823 } else {
824 SET_RANDOM_COLOR
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000825 pr->drawPath(*clipPath, fill, NULL, fGpu, 0, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000826 }
827 } else {
828 SET_RANDOM_COLOR
bsalomon@google.coma3201942012-06-21 19:58:20 +0000829 GrRect rect = GrRect::MakeLTRB(
830 SkIntToScalar(bounds.fLeft),
831 SkIntToScalar(bounds.fTop),
832 SkIntToScalar(bounds.fRight),
833 SkIntToScalar(bounds.fBottom));
834 fGpu->drawSimpleRect(rect, NULL, 0);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000835 }
836 }
837 }
838 // restore clip
bsalomon@google.com13b85aa2012-06-15 21:09:40 +0000839 fGpu->setClip(clipCopy);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000840 }
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000841 // set this last because recursive draws may overwrite it back to kNone.
842 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
843 fCurrClipMaskType = kStencil_ClipMaskType;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000844 return true;
845}
846
bsalomon@google.com411dad02012-06-05 20:24:20 +0000847// mapping of clip-respecting stencil funcs to normal stencil funcs
848// mapping depends on whether stencil-clipping is in effect.
849static const GrStencilFunc
850 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
851 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
852 // In the Clip Funcs
853 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
854 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
855 kLess_StencilFunc, // kLessIfInClip_StencilFunc
856 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
857 // Special in the clip func that forces user's ref to be 0.
858 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
859 // make ref 0 and do normal nequal.
860 },
861 {// Stencil-Clipping is ENABLED
862 // In the Clip Funcs
863 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
864 // eq stencil clip bit, mask
865 // out user bits.
866
867 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
868 // add stencil bit to mask and ref
869
870 kLess_StencilFunc, // kLessIfInClip_StencilFunc
871 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
872 // for both of these we can add
873 // the clip bit to the mask and
874 // ref and compare as normal
875 // Special in the clip func that forces user's ref to be 0.
876 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
877 // make ref have only the clip bit set
878 // and make comparison be less
879 // 10..0 < 1..user_bits..
880 }
881};
882
bsalomon@google.coma3201942012-06-21 19:58:20 +0000883namespace {
884// Sets the settings to clip against the stencil buffer clip while ignoring the
885// client bits.
886const GrStencilSettings& basic_apply_stencil_clip_settings() {
887 // stencil settings to use when clip is in stencil
888 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
889 kKeep_StencilOp,
890 kKeep_StencilOp,
891 kAlwaysIfInClip_StencilFunc,
892 0x0000,
893 0x0000,
894 0x0000);
895 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
896}
897}
898
899void GrClipMaskManager::setGpuStencil() {
900 // We make two copies of the StencilSettings here (except in the early
901 // exit scenario. One copy from draw state to the stack var. Then another
902 // from the stack var to the gpu. We could make this class hold a ptr to
903 // GrGpu's fStencilSettings and eliminate the stack copy here.
904
905 const GrDrawState& drawState = fGpu->getDrawState();
906
907 // use stencil for clipping if clipping is enabled and the clip
908 // has been written into the stencil.
909 GrClipMaskManager::StencilClipMode clipMode;
910 if (this->isClipInStencil() && drawState.isClipState()) {
911 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
912 // We can't be modifying the clip and respecting it at the same time.
913 GrAssert(!drawState.isStateFlagEnabled(
914 GrGpu::kModifyStencilClip_StateBit));
915 } else if (drawState.isStateFlagEnabled(
916 GrGpu::kModifyStencilClip_StateBit)) {
917 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
918 } else {
919 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
920 }
921
922 GrStencilSettings settings;
923 // The GrGpu client may not be using the stencil buffer but we may need to
924 // enable it in order to respect a stencil clip.
925 if (drawState.getStencil().isDisabled()) {
926 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
927 settings = basic_apply_stencil_clip_settings();
928 } else {
929 fGpu->disableStencil();
930 return;
931 }
932 } else {
933 settings = drawState.getStencil();
934 }
935
936 // TODO: dynamically attach a stencil buffer
937 int stencilBits = 0;
938 GrStencilBuffer* stencilBuffer =
939 drawState.getRenderTarget()->getStencilBuffer();
940 if (NULL != stencilBuffer) {
941 stencilBits = stencilBuffer->bits();
942 }
943
bsalomon@google.com9e553c62012-06-22 12:23:29 +0000944 GrAssert(fGpu->getCaps().fStencilWrapOpsSupport ||
945 !settings.usesWrapOp());
946 GrAssert(fGpu->getCaps().fTwoSidedStencilSupport || !settings.isTwoSided());
bsalomon@google.coma3201942012-06-21 19:58:20 +0000947 this->adjustStencilParams(&settings, clipMode, stencilBits);
948 fGpu->setStencilSettings(settings);
949}
950
951void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
952 StencilClipMode mode,
953 int stencilBitCnt) {
bsalomon@google.com411dad02012-06-05 20:24:20 +0000954 GrAssert(stencilBitCnt > 0);
bsalomon@google.com411dad02012-06-05 20:24:20 +0000955
956 if (kModifyClip_StencilClipMode == mode) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000957 // We assume that this clip manager itself is drawing to the GrGpu and
958 // has already setup the correct values.
959 return;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000960 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000961
bsalomon@google.com411dad02012-06-05 20:24:20 +0000962 unsigned int clipBit = (1 << (stencilBitCnt - 1));
963 unsigned int userBits = clipBit - 1;
964
bsalomon@google.coma3201942012-06-21 19:58:20 +0000965 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
966 bool twoSided = fGpu->getCaps().fTwoSidedStencilSupport;
bsalomon@google.com411dad02012-06-05 20:24:20 +0000967
bsalomon@google.coma3201942012-06-21 19:58:20 +0000968 bool finished = false;
969 while (!finished) {
970 GrStencilFunc func = settings->func(face);
971 uint16_t writeMask = settings->writeMask(face);
972 uint16_t funcMask = settings->funcMask(face);
973 uint16_t funcRef = settings->funcRef(face);
974
975 GrAssert((unsigned) func < kStencilFuncCount);
976
977 writeMask &= userBits;
978
979 if (func >= kBasicStencilFuncCount) {
980 int respectClip = kRespectClip_StencilClipMode == mode;
981 if (respectClip) {
982 // The GrGpu class should have checked this
983 GrAssert(this->isClipInStencil());
984 switch (func) {
985 case kAlwaysIfInClip_StencilFunc:
986 funcMask = clipBit;
987 funcRef = clipBit;
988 break;
989 case kEqualIfInClip_StencilFunc:
990 case kLessIfInClip_StencilFunc:
991 case kLEqualIfInClip_StencilFunc:
992 funcMask = (funcMask & userBits) | clipBit;
993 funcRef = (funcRef & userBits) | clipBit;
994 break;
995 case kNonZeroIfInClip_StencilFunc:
996 funcMask = (funcMask & userBits) | clipBit;
997 funcRef = clipBit;
998 break;
999 default:
1000 GrCrash("Unknown stencil func");
1001 }
1002 } else {
1003 funcMask &= userBits;
1004 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001005 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001006 const GrStencilFunc* table =
1007 gSpecialToBasicStencilFunc[respectClip];
1008 func = table[func - kBasicStencilFuncCount];
1009 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001010 } else {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001011 funcMask &= userBits;
1012 funcRef &= userBits;
bsalomon@google.com411dad02012-06-05 20:24:20 +00001013 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001014
1015 settings->setFunc(face, func);
1016 settings->setWriteMask(face, writeMask);
1017 settings->setFuncMask(face, funcMask);
1018 settings->setFuncRef(face, funcRef);
1019
1020 if (GrStencilSettings::kFront_Face == face) {
1021 face = GrStencilSettings::kBack_Face;
1022 finished = !twoSided;
1023 } else {
1024 finished = true;
1025 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001026 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001027 if (!twoSided) {
1028 settings->copyFrontSettingsToBack();
1029 }
bsalomon@google.com411dad02012-06-05 20:24:20 +00001030}
1031
1032////////////////////////////////////////////////////////////////////////////////
1033
robertphillips@google.comfa662942012-05-17 12:20:22 +00001034namespace {
1035
1036GrPathFill invert_fill(GrPathFill fill) {
1037 static const GrPathFill gInvertedFillTable[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001038 kInverseWinding_GrPathFill, // kWinding_GrPathFill
1039 kInverseEvenOdd_GrPathFill, // kEvenOdd_GrPathFill
1040 kWinding_GrPathFill, // kInverseWinding_GrPathFill
1041 kEvenOdd_GrPathFill, // kInverseEvenOdd_GrPathFill
1042 kHairLine_GrPathFill, // kHairLine_GrPathFill
robertphillips@google.comfa662942012-05-17 12:20:22 +00001043 };
bsalomon@google.com47059542012-06-06 20:51:20 +00001044 GR_STATIC_ASSERT(0 == kWinding_GrPathFill);
1045 GR_STATIC_ASSERT(1 == kEvenOdd_GrPathFill);
1046 GR_STATIC_ASSERT(2 == kInverseWinding_GrPathFill);
1047 GR_STATIC_ASSERT(3 == kInverseEvenOdd_GrPathFill);
1048 GR_STATIC_ASSERT(4 == kHairLine_GrPathFill);
1049 GR_STATIC_ASSERT(5 == kGrPathFillCount);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001050 return gInvertedFillTable[fill];
1051}
1052
1053}
1054
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001055bool GrClipMaskManager::createSoftwareClipMask(const GrClip& clipIn,
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001056 GrTexture** result,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001057 GrIRect* resultBounds) {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001058 GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001059
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001060 if (this->clipMaskPreamble(clipIn, result, resultBounds)) {
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001061 return true;
1062 }
1063
robertphillips@google.comf105b102012-05-14 12:18:26 +00001064 GrTexture* accum = fAACache.getLastMask();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001065 if (NULL == accum) {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001066 fAACache.reset();
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001067 return false;
1068 }
1069
robertphillips@google.com2c756812012-05-22 20:28:23 +00001070 GrSWMaskHelper helper(this->getContext());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001071
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001072 helper.init(*resultBounds, NULL);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001073
1074 int count = clipIn.getElementCount();
1075
1076 bool clearToInside;
1077 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
1078 int start = process_initial_clip_elements(clipIn,
1079 *resultBounds,
1080 &clearToInside,
1081 &startOp);
1082
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001083 helper.clear(clearToInside ? 0xFF : 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001084
1085 for (int i = start; i < count; ++i) {
1086
1087 SkRegion::Op op = (i == start) ? startOp : clipIn.getOp(i);
1088
1089 if (SkRegion::kIntersect_Op == op ||
1090 SkRegion::kReverseDifference_Op == op) {
1091 // Intersect and reverse difference require modifying pixels
1092 // outside of the geometry that is being "drawn". In both cases
1093 // we erase all the pixels outside of the geometry but
1094 // leave the pixels inside the geometry alone. For reverse
1095 // difference we invert all the pixels before clearing the ones
1096 // outside the geometry.
1097 if (SkRegion::kReverseDifference_Op == op) {
1098 SkRect temp = SkRect::MakeLTRB(
1099 SkIntToScalar(resultBounds->left()),
1100 SkIntToScalar(resultBounds->top()),
1101 SkIntToScalar(resultBounds->right()),
1102 SkIntToScalar(resultBounds->bottom()));
1103
1104 // invert the entire scene
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001105 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001106 }
1107
1108 if (kRect_ClipType == clipIn.getElementType(i)) {
1109
1110 // convert the rect to a path so we can invert the fill
1111 SkPath temp;
1112 temp.addRect(clipIn.getRect(i));
1113
1114 helper.draw(temp, SkRegion::kReplace_Op,
bsalomon@google.com47059542012-06-06 20:51:20 +00001115 kInverseEvenOdd_GrPathFill, clipIn.getDoAA(i),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001116 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001117 } else {
1118 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1119
1120 helper.draw(clipIn.getPath(i),
1121 SkRegion::kReplace_Op,
1122 invert_fill(clipIn.getPathFill(i)),
1123 clipIn.getDoAA(i),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001124 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001125 }
1126
1127 continue;
1128 }
1129
1130 // The other ops (union, xor, diff) only affect pixels inside
1131 // the geometry so they can just be drawn normally
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001132 if (kRect_ClipType == clipIn.getElementType(i)) {
robertphillips@google.comfa662942012-05-17 12:20:22 +00001133
1134 helper.draw(clipIn.getRect(i),
1135 op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001136 clipIn.getDoAA(i), 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +00001137
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001138 } else {
1139 GrAssert(kPath_ClipType == clipIn.getElementType(i));
1140
robertphillips@google.comfa662942012-05-17 12:20:22 +00001141 helper.draw(clipIn.getPath(i),
1142 op,
1143 clipIn.getPathFill(i),
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001144 clipIn.getDoAA(i), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001145 }
1146 }
1147
robertphillips@google.comfa662942012-05-17 12:20:22 +00001148 // Because we are using the scratch texture cache, "accum" may be
1149 // larger than expected and have some cruft in the areas we aren't using.
1150 // Clear it out.
1151
1152 // TODO: need a simpler way to clear the texture - can we combine
1153 // the clear and the writePixels (inside toTexture)
bsalomon@google.com13b85aa2012-06-15 21:09:40 +00001154 GrDrawState* drawState = fGpu->drawState();
robertphillips@google.comfa662942012-05-17 12:20:22 +00001155 GrAssert(NULL != drawState);
1156 GrRenderTarget* temp = drawState->getRenderTarget();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +00001157 fGpu->clear(NULL, 0x00000000, accum->asRenderTarget());
robertphillips@google.comfa662942012-05-17 12:20:22 +00001158 // can't leave the accum bound as a rendertarget
1159 drawState->setRenderTarget(temp);
1160
robertphillips@google.com366f1c62012-06-29 21:38:47 +00001161 helper.toTexture(accum, clearToInside ? 0xFF : 0x00);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001162
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001163 *result = accum;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001164
bsalomon@google.comc8f7f472012-06-18 13:44:51 +00001165 fCurrClipMaskType = kAlpha_ClipMaskType;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +00001166 return true;
1167}
1168
robertphillips@google.comf294b772012-04-27 14:29:26 +00001169////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf105b102012-05-14 12:18:26 +00001170void GrClipMaskManager::releaseResources() {
robertphillips@google.comf105b102012-05-14 12:18:26 +00001171 fAACache.releaseResources();
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001172}