blob: f4b07214045cb9b2e070bce7f8338c13fb673b2b [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"
15
16//#define GR_AA_CLIP 1
17
robertphillips@google.comf294b772012-04-27 14:29:26 +000018////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +000019void ScissoringSettings::setupScissoring(GrGpu* gpu) {
20 if (!fEnableScissoring) {
21 gpu->disableScissor();
22 return;
23 }
24
25 gpu->enableScissoring(fScissorRect);
26}
27
robertphillips@google.coma72eef32012-05-01 17:22:59 +000028namespace {
29// set up the draw state to enable the aa clipping mask. Besides setting up the
30// sampler matrix this also alters the vertex layout
31void setupDrawStateAAClip(GrGpu* gpu, GrTexture* result, const GrRect &bound) {
32 GrDrawState* drawState = gpu->drawState();
33 GrAssert(drawState);
34
35 static const int maskStage = GrPaint::kTotalStages+1;
36
37 GrMatrix mat;
38 mat.setIDiv(result->width(), result->height());
39 mat.preTranslate(-bound.fLeft, -bound.fTop);
40 mat.preConcat(drawState->getViewMatrix());
41
42 drawState->sampler(maskStage)->reset(GrSamplerState::kClamp_WrapMode,
43 GrSamplerState::kNearest_Filter,
44 mat);
45
46 drawState->setTexture(maskStage, result);
47
48 // The AA clipping determination happens long after the geometry has
49 // been set up to draw. Here we directly enable the AA clip mask stage
50 gpu->addToVertexLayout(
51 GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(maskStage));
52}
53
54}
55
robertphillips@google.comf294b772012-04-27 14:29:26 +000056////////////////////////////////////////////////////////////////////////////////
57// sort out what kind of clip mask needs to be created: alpha, stencil
58// or scissor
robertphillips@google.com1e945b72012-04-16 18:03:03 +000059bool GrClipMaskManager::createClipMask(GrGpu* gpu,
60 const GrClip& clipIn,
61 ScissoringSettings* scissorSettings) {
62
63 GrAssert(scissorSettings);
64
65 scissorSettings->fEnableScissoring = false;
66 fClipMaskInStencil = false;
robertphillips@google.comf294b772012-04-27 14:29:26 +000067 fClipMaskInAlpha = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000068
69 GrDrawState* drawState = gpu->drawState();
70 if (!drawState->isClipState()) {
71 return true;
72 }
73
74 GrRenderTarget* rt = drawState->getRenderTarget();
75
76 // GrDrawTarget should have filtered this for us
77 GrAssert(NULL != rt);
78
robertphillips@google.comf294b772012-04-27 14:29:26 +000079#if GR_AA_CLIP
80 // If MSAA is enabled use the (faster) stencil path for AA clipping
81 // otherwise the alpha clip mask is our only option
82 if (clipIn.requiresAA() && 0 == rt->numSamples()) {
83 // Since we are going to create a destination texture of the correct
84 // size for the mask (rather than being bound by the size of the
85 // render target) we aren't going to use scissoring like the stencil
86 // path does (see scissorSettings below)
robertphillips@google.coma72eef32012-05-01 17:22:59 +000087 GrTexture* result = NULL;
88 GrRect bound;
89 if (this->createAlphaClipMask(gpu, clipIn, &result, &bound)) {
robertphillips@google.comf294b772012-04-27 14:29:26 +000090 fClipMaskInAlpha = true;
robertphillips@google.coma72eef32012-05-01 17:22:59 +000091
92 setupDrawStateAAClip(gpu, result, bound);
robertphillips@google.comf294b772012-04-27 14:29:26 +000093 return true;
94 }
95
96 // if alpha clip mask creation fails fall through to the stencil
97 // buffer method
98 }
99#endif // GR_AA_CLIP
100
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000101 GrRect bounds;
102 GrRect rtRect;
103 rtRect.setLTRB(0, 0,
104 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
105 if (clipIn.hasConservativeBounds()) {
106 bounds = clipIn.getConservativeBounds();
107 if (!bounds.intersect(rtRect)) {
108 bounds.setEmpty();
109 }
110 } else {
111 bounds = rtRect;
112 }
113
114 bounds.roundOut(&scissorSettings->fScissorRect);
115 if (scissorSettings->fScissorRect.isEmpty()) {
116 scissorSettings->fScissorRect.setLTRB(0,0,0,0);
117 // TODO: I think we can do an early exit here - after refactoring try:
118 // set fEnableScissoring to true but leave fClipMaskInStencil false
119 // and return - everything is going to be scissored away anyway!
120 }
121 scissorSettings->fEnableScissoring = true;
122
123 // use the stencil clip if we can't represent the clip as a rectangle.
124 fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
125 !bounds.isEmpty();
126
127 if (fClipMaskInStencil) {
128 return this->createStencilClipMask(gpu, clipIn, bounds, scissorSettings);
129 }
130
131 return true;
132}
133
134#define VISUALIZE_COMPLEX_CLIP 0
135
136#if VISUALIZE_COMPLEX_CLIP
137 #include "GrRandom.h"
138 GrRandom gRandom;
139 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
140#else
141 #define SET_RANDOM_COLOR
142#endif
143
144namespace {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000145////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000146// determines how many elements at the head of the clip can be skipped and
147// whether the initial clear should be to the inside- or outside-the-clip value,
148// and what op should be used to draw the first element that isn't skipped.
149int process_initial_clip_elements(const GrClip& clip,
150 const GrRect& bounds,
151 bool* clearToInside,
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000152 SkRegion::Op* startOp) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000153
154 // logically before the first element of the clip stack is
155 // processed the clip is entirely open. However, depending on the
156 // first set op we may prefer to clear to 0 for performance. We may
157 // also be able to skip the initial clip paths/rects. We loop until
158 // we cannot skip an element.
159 int curr;
160 bool done = false;
161 *clearToInside = true;
162 int count = clip.getElementCount();
163
164 for (curr = 0; curr < count && !done; ++curr) {
165 switch (clip.getOp(curr)) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000166 case SkRegion::kReplace_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000167 // replace ignores everything previous
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000168 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000169 *clearToInside = false;
170 done = true;
171 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000172 case SkRegion::kIntersect_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000173 // if this element contains the entire bounds then we
174 // can skip it.
175 if (kRect_ClipType == clip.getElementType(curr)
176 && clip.getRect(curr).contains(bounds)) {
177 break;
178 }
179 // if everything is initially clearToInside then intersect is
180 // same as clear to 0 and treat as a replace. Otherwise,
181 // set stays empty.
182 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000183 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000184 *clearToInside = false;
185 done = true;
186 }
187 break;
188 // we can skip a leading union.
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000189 case SkRegion::kUnion_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000190 // if everything is initially outside then union is
191 // same as replace. Otherwise, every pixel is still
192 // clearToInside
193 if (!*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000194 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000195 done = true;
196 }
197 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000198 case SkRegion::kXOR_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000199 // xor is same as difference or replace both of which
200 // can be 1-pass instead of 2 for xor.
201 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000202 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000203 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000204 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000205 }
206 done = true;
207 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000208 case SkRegion::kDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000209 // if all pixels are clearToInside then we have to process the
210 // difference, otherwise it has no effect and all pixels
211 // remain outside.
212 if (*clearToInside) {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000213 *startOp = SkRegion::kDifference_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000214 done = true;
215 }
216 break;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000217 case SkRegion::kReverseDifference_Op:
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000218 // if all pixels are clearToInside then reverse difference
219 // produces empty set. Otherise it is same as replace
220 if (*clearToInside) {
221 *clearToInside = false;
222 } else {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000223 *startOp = SkRegion::kReplace_Op;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000224 done = true;
225 }
226 break;
227 default:
228 GrCrash("Unknown set op.");
229 }
230 }
231 return done ? curr-1 : count;
232}
robertphillips@google.comf294b772012-04-27 14:29:26 +0000233
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000234}
235
robertphillips@google.comf294b772012-04-27 14:29:26 +0000236
237namespace {
238
239////////////////////////////////////////////////////////////////////////////////
240// set up the OpenGL blend function to perform the specified
241// boolean operation for alpha clip mask creation
242void setUpBooleanBlendCoeffs(GrDrawState* drawState, SkRegion::Op op) {
243
244 switch (op) {
245 case SkRegion::kReplace_Op:
246 drawState->setBlendFunc(kOne_BlendCoeff, kZero_BlendCoeff);
247 break;
248 case SkRegion::kIntersect_Op:
249 drawState->setBlendFunc(kDC_BlendCoeff, kZero_BlendCoeff);
250 break;
251 case SkRegion::kUnion_Op:
252 drawState->setBlendFunc(kOne_BlendCoeff, kISC_BlendCoeff);
253 break;
254 case SkRegion::kXOR_Op:
255 drawState->setBlendFunc(kIDC_BlendCoeff, kISC_BlendCoeff);
256 break;
257 case SkRegion::kDifference_Op:
258 drawState->setBlendFunc(kZero_BlendCoeff, kISC_BlendCoeff);
259 break;
260 case SkRegion::kReverseDifference_Op:
261 drawState->setBlendFunc(kIDC_BlendCoeff, kZero_BlendCoeff);
262 break;
263 default:
264 GrAssert(false);
265 break;
266 }
267}
268
269}
270
271////////////////////////////////////////////////////////////////////////////////
272bool GrClipMaskManager::drawPath(GrGpu* gpu,
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000273 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000274 GrPathFill fill,
275 bool doAA) {
276
277 GrPathRenderer* pr = this->getClipPathRenderer(gpu, path, fill, doAA);
278 if (NULL == pr) {
279 return false;
280 }
281
282 pr->drawPath(path, fill, NULL, gpu, 0, doAA);
283 return true;
284}
285
286////////////////////////////////////////////////////////////////////////////////
287bool GrClipMaskManager::drawClipShape(GrGpu* gpu,
288 GrTexture* target,
289 const GrClip& clipIn,
290 int index) {
291 GrDrawState* drawState = gpu->drawState();
292 GrAssert(NULL != drawState);
293
294 drawState->setRenderTarget(target->asRenderTarget());
295
296 if (kRect_ClipType == clipIn.getElementType(index)) {
297 if (clipIn.getDoAA(index)) {
298 // convert the rect to a path for AA
299 SkPath temp;
300 temp.addRect(clipIn.getRect(index));
301
302 return this->drawPath(gpu, temp,
303 kEvenOdd_PathFill, clipIn.getDoAA(index));
304 } else {
305 gpu->drawSimpleRect(clipIn.getRect(index), NULL, 0);
306 }
307 } else {
308 return this->drawPath(gpu,
309 clipIn.getPath(index),
310 clipIn.getPathFill(index),
311 clipIn.getDoAA(index));
312 }
313 return true;
314}
315
316void GrClipMaskManager::drawTexture(GrGpu* gpu,
317 GrTexture* target,
318 const GrRect& rect,
319 GrTexture* texture) {
320 GrDrawState* drawState = gpu->drawState();
321 GrAssert(NULL != drawState);
322
323 // no AA here since it is encoded in the texture
324 drawState->setRenderTarget(target->asRenderTarget());
325
326 GrMatrix sampleM;
327 sampleM.setIDiv(texture->width(), texture->height());
328 drawState->setTexture(0, texture);
329
330 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
331 GrSamplerState::kNearest_Filter,
332 sampleM);
333
334 gpu->drawSimpleRect(rect, NULL, 1 << 0);
335
336 drawState->setTexture(0, NULL);
337}
338
339namespace {
340
341void clear(GrGpu* gpu,
342 GrTexture* target,
343 const GrRect& bounds,
344 GrColor color) {
345 GrDrawState* drawState = gpu->drawState();
346 GrAssert(NULL != drawState);
347
348 // zap entire target to specified color
349 drawState->setRenderTarget(target->asRenderTarget());
350 gpu->clear(NULL, color);
351}
352
353}
354
355////////////////////////////////////////////////////////////////////////////////
356// Create a 8-bit clip mask in alpha
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000357bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu,
358 const GrClip& clipIn,
359 GrTexture** result,
360 GrRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361
362 GrDrawState* origDrawState = gpu->drawState();
363 GrAssert(origDrawState->isClipState());
364
365 GrRenderTarget* rt = origDrawState->getRenderTarget();
366 GrAssert(NULL != rt);
367
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000368 if (fAACache.canReuse(clipIn, rt->width(), rt->height())) {
369 *result = fAACache.getLastMask();
370 *resultBounds = fAACache.getLastBound();
371 return true;
372 }
373
robertphillips@google.comf294b772012-04-27 14:29:26 +0000374 GrRect rtRect;
375 rtRect.setLTRB(0, 0,
376 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
377
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000378
robertphillips@google.comf294b772012-04-27 14:29:26 +0000379 // unlike the stencil path the alpha path is not bound to the size of the
380 // render target - determine the minimum size required for the mask
381 GrRect bounds;
382
383 if (clipIn.hasConservativeBounds()) {
384 bounds = clipIn.getConservativeBounds();
385 if (!bounds.intersect(rtRect)) {
386 // the mask will be empty in this case
387 GrAssert(false);
388 bounds.setEmpty();
389 }
390 } else {
391 // still locked to the size of the render target
392 bounds = rtRect;
393 }
394
395 bounds.roundOut();
396
397 // need to outset a pixel since the standard bounding box computation
398 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
399 bounds.outset(SkIntToScalar(1), SkIntToScalar(1));
400
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000401 // TODO: make sure we don't outset if bounds are still 0,0 @ min
402
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403 GrAssert(SkScalarIsInt(bounds.width()));
404 GrAssert(SkScalarIsInt(bounds.height()));
405
406 GrTextureDesc desc = {
robertphillips@google.com180bc882012-05-03 18:03:05 +0000407 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000408 SkScalarCeilToInt(bounds.width()),
409 SkScalarCeilToInt(bounds.height()),
410 kAlpha_8_GrPixelConfig,
411 0 // samples
412 };
413
414 GrRect newRTBounds;
415 newRTBounds.setLTRB(0, 0, bounds.width(), bounds.height());
416
417 GrTexture* accum = gpu->createTexture(desc, NULL, 0);
418 GrTexture* temp = gpu->createTexture(desc, NULL, 0);
419 if (NULL == accum || NULL == temp) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000420 fClipMaskInAlpha = false;
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000421 SkSafeUnref(accum);
422 SkSafeUnref(temp);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000423 return false;
424 }
425
426 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
427 GrDrawState* drawState = gpu->drawState();
428
429 GrDrawTarget::AutoGeometryPush agp(gpu);
430
431 int count = clipIn.getElementCount();
432
433 if (0 != bounds.fTop || 0 != bounds.fLeft) {
434 // if we were able to trim down the size of the mask we need to
435 // offset the paths & rects that will be used to compute it
436 GrMatrix m;
437
438 m.setTranslate(-bounds.fLeft, -bounds.fTop);
439
440 drawState->preConcatViewMatrix(m);
441 }
442
443 bool clearToInside;
444 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
445 int start = process_initial_clip_elements(clipIn,
446 bounds,
447 &clearToInside,
448 &startOp);
449
450 clear(gpu, accum, newRTBounds, clearToInside ? 0xffffffff : 0x00000000);
451
452 // walk through each clip element and perform its set op
453 for (int c = start; c < count; ++c) {
454
455 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
456
457 if (SkRegion::kReplace_Op == op) {
458 // TODO: replace is actually a lot faster then intersection
459 // for this path - refactor the stencil path so it can handle
460 // replace ops and alter GrClip to allow them through
461
462 // clear the accumulator and draw the new object directly into it
463 clear(gpu, accum, newRTBounds, 0x00000000);
464
465 setUpBooleanBlendCoeffs(drawState, op);
466 this->drawClipShape(gpu, accum, clipIn, c);
467
468 } else if (SkRegion::kReverseDifference_Op == op ||
469 SkRegion::kIntersect_Op == op) {
470 // there is no point in intersecting a screen filling rectangle.
471 if (SkRegion::kIntersect_Op == op &&
472 kRect_ClipType == clipIn.getElementType(c) &&
473 clipIn.getRect(c).contains(bounds)) {
474 continue;
475 }
476
477 // clear the temp target & draw into it
478 clear(gpu, temp, newRTBounds, 0x00000000);
479
480 setUpBooleanBlendCoeffs(drawState, SkRegion::kReplace_Op);
481 this->drawClipShape(gpu, temp, clipIn, c);
482
483 // TODO: rather than adding these two translations here
484 // compute the bounding box needed to render the texture
485 // into temp
486 if (0 != bounds.fTop || 0 != bounds.fLeft) {
487 GrMatrix m;
488
489 m.setTranslate(bounds.fLeft, bounds.fTop);
490
491 drawState->preConcatViewMatrix(m);
492 }
493
494 // Now draw into the accumulator using the real operation
495 // and the temp buffer as a texture
496 setUpBooleanBlendCoeffs(drawState, op);
497 this->drawTexture(gpu, accum, newRTBounds, temp);
498
499 if (0 != bounds.fTop || 0 != bounds.fLeft) {
500 GrMatrix m;
501
502 m.setTranslate(-bounds.fLeft, -bounds.fTop);
503
504 drawState->preConcatViewMatrix(m);
505 }
506
507 } else {
508 // all the remaining ops can just be directly draw into
509 // the accumulation buffer
510 setUpBooleanBlendCoeffs(drawState, op);
511 this->drawClipShape(gpu, accum, clipIn, c);
512 }
513 }
514
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000515 fAACache.set(clipIn, rt->width(), rt->height(), accum, bounds);
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000516 *result = accum;
517 *resultBounds = bounds;
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000518 SkSafeUnref(accum); // fAACache still has a ref to accum
519 SkSafeUnref(temp);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000520 return true;
521}
522
523////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000524// Create a 1-bit clip mask in the stencil buffer
525bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu,
526 const GrClip& clipIn,
527 const GrRect& bounds,
528 ScissoringSettings* scissorSettings) {
529
530 GrAssert(fClipMaskInStencil);
531
532 GrDrawState* drawState = gpu->drawState();
533 GrAssert(drawState->isClipState());
534
535 GrRenderTarget* rt = drawState->getRenderTarget();
536 GrAssert(NULL != rt);
537
538 // TODO: dynamically attach a SB when needed.
539 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
540 if (NULL == stencilBuffer) {
541 return false;
542 }
543
544 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
545
546 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
547
548 // we set the current clip to the bounds so that our recursive
549 // draws are scissored to them. We use the copy of the complex clip
550 // we just stashed on the SB to render from. We set it back after
551 // we finish drawing it into the stencil.
552 const GrClip& clipCopy = stencilBuffer->getLastClip();
553 gpu->setClip(GrClip(bounds));
554
555 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
556 drawState = gpu->drawState();
557 drawState->setRenderTarget(rt);
558 GrDrawTarget::AutoGeometryPush agp(gpu);
559
560 gpu->disableScissor();
561#if !VISUALIZE_COMPLEX_CLIP
562 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
563#endif
564
565 int count = clipCopy.getElementCount();
566 int clipBit = stencilBuffer->bits();
567 SkASSERT((clipBit <= 16) &&
568 "Ganesh only handles 16b or smaller stencil buffers");
569 clipBit = (1 << (clipBit-1));
570
571 GrRect rtRect;
572 rtRect.setLTRB(0, 0,
573 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
574
575 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000576 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000577 int start = process_initial_clip_elements(clipCopy,
578 rtRect,
579 &clearToInside,
580 &startOp);
581
582 gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
583
584 // walk through each clip element and perform its set op
585 // with the existing clip.
586 for (int c = start; c < count; ++c) {
587 GrPathFill fill;
588 bool fillInverted;
589 // enabled at bottom of loop
590 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
591
592 bool canRenderDirectToStencil; // can the clip element be drawn
593 // directly to the stencil buffer
594 // with a non-inverted fill rule
595 // without extra passes to
596 // resolve in/out status.
597
robertphillips@google.comf294b772012-04-27 14:29:26 +0000598 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
599
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000600 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000601 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000602 if (kRect_ClipType == clipCopy.getElementType(c)) {
603 canRenderDirectToStencil = true;
604 fill = kEvenOdd_PathFill;
605 fillInverted = false;
606 // there is no point in intersecting a screen filling
607 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000608 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609 clipCopy.getRect(c).contains(rtRect)) {
610 continue;
611 }
612 } else {
613 fill = clipCopy.getPathFill(c);
614 fillInverted = GrIsFillInverted(fill);
615 fill = GrNonInvertedFill(fill);
616 clipPath = &clipCopy.getPath(c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000617 pr = this->getClipPathRenderer(gpu, *clipPath, fill, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000618 if (NULL == pr) {
619 fClipMaskInStencil = false;
620 gpu->setClip(clipCopy); // restore to the original
621 return false;
622 }
623 canRenderDirectToStencil =
624 !pr->requiresStencilPass(*clipPath, fill, gpu);
625 }
626
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000627 int passes;
628 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
629
630 bool canDrawDirectToClip; // Given the renderer, the element,
631 // fill rule, and set operation can
632 // we render the element directly to
633 // stencil bit used for clipping.
634 canDrawDirectToClip =
635 GrStencilSettings::GetClipPasses(op,
636 canRenderDirectToStencil,
637 clipBit,
638 fillInverted,
639 &passes, stencilSettings);
640
641 // draw the element to the client stencil bits if necessary
642 if (!canDrawDirectToClip) {
643 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
644 kIncClamp_StencilOp,
645 kIncClamp_StencilOp,
646 kAlways_StencilFunc,
647 0xffff,
648 0x0000,
649 0xffff);
650 SET_RANDOM_COLOR
651 if (kRect_ClipType == clipCopy.getElementType(c)) {
652 *drawState->stencil() = gDrawToStencil;
653 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
654 } else {
655 if (canRenderDirectToStencil) {
656 *drawState->stencil() = gDrawToStencil;
657 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
658 } else {
659 pr->drawPathToStencil(*clipPath, fill, gpu);
660 }
661 }
662 }
663
664 // now we modify the clip bit by rendering either the clip
665 // element directly or a bounding rect of the entire clip.
666 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
667 for (int p = 0; p < passes; ++p) {
668 *drawState->stencil() = stencilSettings[p];
669 if (canDrawDirectToClip) {
670 if (kRect_ClipType == clipCopy.getElementType(c)) {
671 SET_RANDOM_COLOR
672 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
673 } else {
674 SET_RANDOM_COLOR
675 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
676 }
677 } else {
678 SET_RANDOM_COLOR
679 gpu->drawSimpleRect(bounds, NULL, 0);
680 }
681 }
682 }
683 // restore clip
684 gpu->setClip(clipCopy);
685 // recusive draws would have disabled this since they drew with
686 // the clip bounds as clip.
687 fClipMaskInStencil = true;
688 }
689
690 return true;
691}
692
robertphillips@google.comf294b772012-04-27 14:29:26 +0000693////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000694GrPathRenderer* GrClipMaskManager::getClipPathRenderer(GrGpu* gpu,
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000695 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000696 GrPathFill fill,
697 bool antiAlias) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000698 if (NULL == fPathRendererChain) {
699 fPathRendererChain =
700 new GrPathRendererChain(gpu->getContext(),
robertphillips@google.comf294b772012-04-27 14:29:26 +0000701 GrPathRendererChain::kNone_UsageFlag);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000702 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000703 return fPathRendererChain->getPathRenderer(path, fill, gpu, antiAlias);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000704}
705
robertphillips@google.comf294b772012-04-27 14:29:26 +0000706////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707void GrClipMaskManager::freeResources() {
708 // in case path renderer has any GrResources, start from scratch
709 GrSafeSetNull(fPathRendererChain);
710}