blob: 7672f5a5993bfcf5eb2c8b34f9288ac6847044d4 [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,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000343 GrColor color) {
344 GrDrawState* drawState = gpu->drawState();
345 GrAssert(NULL != drawState);
346
347 // zap entire target to specified color
348 drawState->setRenderTarget(target->asRenderTarget());
349 gpu->clear(NULL, color);
350}
351
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000352// get a texture to act as a temporary buffer for AA clip boolean operations
353// TODO: given the expense of createTexture we may want to just cache this too
354void needTemp(GrGpu *gpu, const GrTextureDesc& desc, GrTexture** temp) {
355 if (NULL != *temp) {
356 // we've already allocated the temp texture
357 return;
358 }
359
360 *temp = gpu->createTexture(desc, NULL, 0);
361}
362
363}
364
365void GrClipMaskManager::getAccum(GrGpu* gpu,
366 const GrTextureDesc& desc,
367 GrTexture** accum) {
368 GrAssert(NULL == *accum);
369
370 // since we are getting an accumulator we know our cache is shot. See
371 // if we can reuse the texture stored in the cache
372 if (fAACache.getLastMaskWidth() >= desc.fWidth &&
373 fAACache.getLastMaskHeight() >= desc.fHeight) {
374 // we can just reuse the existing texture
375 *accum = fAACache.detachLastMask();
376 fAACache.reset();
377 } else {
378 *accum = gpu->createTexture(desc, NULL, 0);
379 }
380
381 GrAssert(1 == (*accum)->getRefCnt());
robertphillips@google.comf294b772012-04-27 14:29:26 +0000382}
383
384////////////////////////////////////////////////////////////////////////////////
385// Create a 8-bit clip mask in alpha
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000386bool GrClipMaskManager::createAlphaClipMask(GrGpu* gpu,
387 const GrClip& clipIn,
388 GrTexture** result,
389 GrRect *resultBounds) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000390
391 GrDrawState* origDrawState = gpu->drawState();
392 GrAssert(origDrawState->isClipState());
393
394 GrRenderTarget* rt = origDrawState->getRenderTarget();
395 GrAssert(NULL != rt);
396
397 GrRect rtRect;
398 rtRect.setLTRB(0, 0,
399 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
400
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000401
robertphillips@google.comf294b772012-04-27 14:29:26 +0000402 // unlike the stencil path the alpha path is not bound to the size of the
403 // render target - determine the minimum size required for the mask
404 GrRect bounds;
405
406 if (clipIn.hasConservativeBounds()) {
407 bounds = clipIn.getConservativeBounds();
408 if (!bounds.intersect(rtRect)) {
409 // the mask will be empty in this case
410 GrAssert(false);
411 bounds.setEmpty();
412 }
413 } else {
414 // still locked to the size of the render target
415 bounds = rtRect;
416 }
417
418 bounds.roundOut();
419
420 // need to outset a pixel since the standard bounding box computation
421 // path doesn't leave any room for antialiasing (esp. w.r.t. rects)
422 bounds.outset(SkIntToScalar(1), SkIntToScalar(1));
423
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000424 // TODO: make sure we don't outset if bounds are still 0,0 @ min
425
robertphillips@google.comf294b772012-04-27 14:29:26 +0000426 GrAssert(SkScalarIsInt(bounds.width()));
427 GrAssert(SkScalarIsInt(bounds.height()));
428
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000429 if (fAACache.canReuse(clipIn,
430 SkScalarCeilToInt(bounds.width()),
431 SkScalarCeilToInt(bounds.height()))) {
432 *result = fAACache.getLastMask();
433 fAACache.getLastBound(resultBounds);
434 return true;
435 }
436
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000437 const GrTextureDesc desc = {
robertphillips@google.com180bc882012-05-03 18:03:05 +0000438 kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000439 SkScalarCeilToInt(bounds.width()),
440 SkScalarCeilToInt(bounds.height()),
441 kAlpha_8_GrPixelConfig,
442 0 // samples
443 };
444
445 GrRect newRTBounds;
446 newRTBounds.setLTRB(0, 0, bounds.width(), bounds.height());
447
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000448 GrTexture* accum = NULL, *temp = NULL;
449
450 getAccum(gpu, desc, &accum);
451 if (NULL == accum) {
robertphillips@google.comf294b772012-04-27 14:29:26 +0000452 fClipMaskInAlpha = false;
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000453 SkSafeUnref(accum);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000454 return false;
455 }
456
457 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
458 GrDrawState* drawState = gpu->drawState();
459
460 GrDrawTarget::AutoGeometryPush agp(gpu);
461
462 int count = clipIn.getElementCount();
463
464 if (0 != bounds.fTop || 0 != bounds.fLeft) {
465 // if we were able to trim down the size of the mask we need to
466 // offset the paths & rects that will be used to compute it
467 GrMatrix m;
468
469 m.setTranslate(-bounds.fLeft, -bounds.fTop);
470
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000471 drawState->setViewMatrix(m);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000472 }
473
474 bool clearToInside;
475 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
476 int start = process_initial_clip_elements(clipIn,
477 bounds,
478 &clearToInside,
479 &startOp);
480
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000481 clear(gpu, accum, clearToInside ? 0xffffffff : 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000482
483 // walk through each clip element and perform its set op
484 for (int c = start; c < count; ++c) {
485
486 SkRegion::Op op = (c == start) ? startOp : clipIn.getOp(c);
487
488 if (SkRegion::kReplace_Op == op) {
489 // TODO: replace is actually a lot faster then intersection
490 // for this path - refactor the stencil path so it can handle
491 // replace ops and alter GrClip to allow them through
492
493 // clear the accumulator and draw the new object directly into it
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000494 clear(gpu, accum, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495
496 setUpBooleanBlendCoeffs(drawState, op);
497 this->drawClipShape(gpu, accum, clipIn, c);
498
499 } else if (SkRegion::kReverseDifference_Op == op ||
500 SkRegion::kIntersect_Op == op) {
501 // there is no point in intersecting a screen filling rectangle.
502 if (SkRegion::kIntersect_Op == op &&
503 kRect_ClipType == clipIn.getElementType(c) &&
504 clipIn.getRect(c).contains(bounds)) {
505 continue;
506 }
507
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000508 needTemp(gpu, desc, &temp);
509 if (NULL == temp) {
510 fClipMaskInAlpha = false;
511 SkSafeUnref(accum);
512 return false;
513 }
514
robertphillips@google.comf294b772012-04-27 14:29:26 +0000515 // clear the temp target & draw into it
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000516 clear(gpu, temp, 0x00000000);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000517
518 setUpBooleanBlendCoeffs(drawState, SkRegion::kReplace_Op);
519 this->drawClipShape(gpu, temp, clipIn, c);
520
521 // TODO: rather than adding these two translations here
522 // compute the bounding box needed to render the texture
523 // into temp
524 if (0 != bounds.fTop || 0 != bounds.fLeft) {
525 GrMatrix m;
526
527 m.setTranslate(bounds.fLeft, bounds.fTop);
528
529 drawState->preConcatViewMatrix(m);
530 }
531
532 // Now draw into the accumulator using the real operation
533 // and the temp buffer as a texture
534 setUpBooleanBlendCoeffs(drawState, op);
535 this->drawTexture(gpu, accum, newRTBounds, temp);
536
537 if (0 != bounds.fTop || 0 != bounds.fLeft) {
538 GrMatrix m;
539
540 m.setTranslate(-bounds.fLeft, -bounds.fTop);
541
542 drawState->preConcatViewMatrix(m);
543 }
544
545 } else {
546 // all the remaining ops can just be directly draw into
547 // the accumulation buffer
548 setUpBooleanBlendCoeffs(drawState, op);
549 this->drawClipShape(gpu, accum, clipIn, c);
550 }
551 }
552
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000553 fAACache.set(clipIn, accum, bounds);
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000554 *result = accum;
555 *resultBounds = bounds;
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000556 SkSafeUnref(accum); // fAACache still has a ref to accum
557 SkSafeUnref(temp);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000558
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559 return true;
560}
561
562////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000563// Create a 1-bit clip mask in the stencil buffer
564bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu,
565 const GrClip& clipIn,
566 const GrRect& bounds,
567 ScissoringSettings* scissorSettings) {
568
569 GrAssert(fClipMaskInStencil);
570
571 GrDrawState* drawState = gpu->drawState();
572 GrAssert(drawState->isClipState());
573
574 GrRenderTarget* rt = drawState->getRenderTarget();
575 GrAssert(NULL != rt);
576
577 // TODO: dynamically attach a SB when needed.
578 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
579 if (NULL == stencilBuffer) {
580 return false;
581 }
582
583 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
584
585 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
586
587 // we set the current clip to the bounds so that our recursive
588 // draws are scissored to them. We use the copy of the complex clip
589 // we just stashed on the SB to render from. We set it back after
590 // we finish drawing it into the stencil.
591 const GrClip& clipCopy = stencilBuffer->getLastClip();
592 gpu->setClip(GrClip(bounds));
593
594 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
595 drawState = gpu->drawState();
596 drawState->setRenderTarget(rt);
597 GrDrawTarget::AutoGeometryPush agp(gpu);
598
599 gpu->disableScissor();
600#if !VISUALIZE_COMPLEX_CLIP
601 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
602#endif
603
604 int count = clipCopy.getElementCount();
605 int clipBit = stencilBuffer->bits();
606 SkASSERT((clipBit <= 16) &&
607 "Ganesh only handles 16b or smaller stencil buffers");
608 clipBit = (1 << (clipBit-1));
609
610 GrRect rtRect;
611 rtRect.setLTRB(0, 0,
612 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
613
614 bool clearToInside;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000615 SkRegion::Op startOp = SkRegion::kReplace_Op; // suppress warning
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000616 int start = process_initial_clip_elements(clipCopy,
617 rtRect,
618 &clearToInside,
619 &startOp);
620
621 gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
622
623 // walk through each clip element and perform its set op
624 // with the existing clip.
625 for (int c = start; c < count; ++c) {
626 GrPathFill fill;
627 bool fillInverted;
628 // enabled at bottom of loop
629 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
630
631 bool canRenderDirectToStencil; // can the clip element be drawn
632 // directly to the stencil buffer
633 // with a non-inverted fill rule
634 // without extra passes to
635 // resolve in/out status.
636
robertphillips@google.comf294b772012-04-27 14:29:26 +0000637 SkRegion::Op op = (c == start) ? startOp : clipCopy.getOp(c);
638
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639 GrPathRenderer* pr = NULL;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000640 const SkPath* clipPath = NULL;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641 if (kRect_ClipType == clipCopy.getElementType(c)) {
642 canRenderDirectToStencil = true;
643 fill = kEvenOdd_PathFill;
644 fillInverted = false;
645 // there is no point in intersecting a screen filling
646 // rectangle.
robertphillips@google.comf294b772012-04-27 14:29:26 +0000647 if (SkRegion::kIntersect_Op == op &&
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000648 clipCopy.getRect(c).contains(rtRect)) {
649 continue;
650 }
651 } else {
652 fill = clipCopy.getPathFill(c);
653 fillInverted = GrIsFillInverted(fill);
654 fill = GrNonInvertedFill(fill);
655 clipPath = &clipCopy.getPath(c);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000656 pr = this->getClipPathRenderer(gpu, *clipPath, fill, false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000657 if (NULL == pr) {
658 fClipMaskInStencil = false;
659 gpu->setClip(clipCopy); // restore to the original
660 return false;
661 }
662 canRenderDirectToStencil =
663 !pr->requiresStencilPass(*clipPath, fill, gpu);
664 }
665
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000666 int passes;
667 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
668
669 bool canDrawDirectToClip; // Given the renderer, the element,
670 // fill rule, and set operation can
671 // we render the element directly to
672 // stencil bit used for clipping.
673 canDrawDirectToClip =
674 GrStencilSettings::GetClipPasses(op,
675 canRenderDirectToStencil,
676 clipBit,
677 fillInverted,
678 &passes, stencilSettings);
679
680 // draw the element to the client stencil bits if necessary
681 if (!canDrawDirectToClip) {
682 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
683 kIncClamp_StencilOp,
684 kIncClamp_StencilOp,
685 kAlways_StencilFunc,
686 0xffff,
687 0x0000,
688 0xffff);
689 SET_RANDOM_COLOR
690 if (kRect_ClipType == clipCopy.getElementType(c)) {
691 *drawState->stencil() = gDrawToStencil;
692 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
693 } else {
694 if (canRenderDirectToStencil) {
695 *drawState->stencil() = gDrawToStencil;
696 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
697 } else {
698 pr->drawPathToStencil(*clipPath, fill, gpu);
699 }
700 }
701 }
702
703 // now we modify the clip bit by rendering either the clip
704 // element directly or a bounding rect of the entire clip.
705 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
706 for (int p = 0; p < passes; ++p) {
707 *drawState->stencil() = stencilSettings[p];
708 if (canDrawDirectToClip) {
709 if (kRect_ClipType == clipCopy.getElementType(c)) {
710 SET_RANDOM_COLOR
711 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
712 } else {
713 SET_RANDOM_COLOR
714 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
715 }
716 } else {
717 SET_RANDOM_COLOR
718 gpu->drawSimpleRect(bounds, NULL, 0);
719 }
720 }
721 }
722 // restore clip
723 gpu->setClip(clipCopy);
724 // recusive draws would have disabled this since they drew with
725 // the clip bounds as clip.
726 fClipMaskInStencil = true;
727 }
728
729 return true;
730}
731
robertphillips@google.comf294b772012-04-27 14:29:26 +0000732////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733GrPathRenderer* GrClipMaskManager::getClipPathRenderer(GrGpu* gpu,
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000734 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000735 GrPathFill fill,
736 bool antiAlias) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737 if (NULL == fPathRendererChain) {
738 fPathRendererChain =
739 new GrPathRendererChain(gpu->getContext(),
robertphillips@google.comf294b772012-04-27 14:29:26 +0000740 GrPathRendererChain::kNone_UsageFlag);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000741 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000742 return fPathRendererChain->getPathRenderer(path, fill, gpu, antiAlias);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000743}
744
robertphillips@google.comf294b772012-04-27 14:29:26 +0000745////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000746void GrClipMaskManager::freeResources() {
747 // in case path renderer has any GrResources, start from scratch
748 GrSafeSetNull(fPathRendererChain);
749}