3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "GrClipMaskManager.h"
10 #include "GrAAConvexPathRenderer.h"
11 #include "GrAAHairLinePathRenderer.h"
12 #include "GrAARectRenderer.h"
13 #include "GrDrawTargetCaps.h"
16 #include "GrPathRenderer.h"
17 #include "GrRenderTarget.h"
18 #include "GrStencilBuffer.h"
19 #include "GrSWMaskHelper.h"
20 #include "effects/GrTextureDomain.h"
21 #include "effects/GrConvexPolyEffect.h"
22 #include "effects/GrRRectEffect.h"
23 #include "SkRasterClip.h"
24 #include "SkStrokeRec.h"
29 typedef SkClipStack::Element Element;
31 using namespace GrReducedClip;
33 ////////////////////////////////////////////////////////////////////////////////
35 // set up the draw state to enable the aa clipping mask. Besides setting up the
36 // stage matrix this also alters the vertex layout
37 void setup_drawstate_aaclip(GrGpu* gpu,
39 const SkIRect &devBound) {
40 GrDrawState* drawState = gpu->drawState();
44 // We want to use device coords to compute the texture coordinates. We set our matrix to be
45 // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to
46 // normalized coords. We apply this matrix to the vertex positions rather than local coords.
47 mat.setIDiv(result->width(), result->height());
48 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
49 SkIntToScalar(-devBound.fTop));
50 mat.preConcat(drawState->getViewMatrix());
52 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
53 // This could be a long-lived effect that is cached with the alpha-mask.
54 drawState->addCoverageProcessor(
55 GrTextureDomainEffect::Create(result,
57 GrTextureDomain::MakeTexelDomain(result, domainTexels),
58 GrTextureDomain::kDecal_Mode,
59 GrTextureParams::kNone_FilterMode,
60 kPosition_GrCoordSet))->unref();
63 bool path_needs_SW_renderer(GrContext* context,
65 const SkPath& origPath,
66 const SkStrokeRec& stroke,
68 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
69 SkTCopyOnFirstWrite<SkPath> path(origPath);
70 if (path->isInverseFillType()) {
71 path.writable()->toggleInverseFillType();
73 // last (false) parameter disallows use of the SW path renderer
74 GrPathRendererChain::DrawType type = doAA ?
75 GrPathRendererChain::kColorAntiAlias_DrawType :
76 GrPathRendererChain::kColor_DrawType;
78 return NULL == context->getPathRenderer(*path, stroke, gpu, false, type);
84 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
85 * will be used on any element. If so, it returns true to indicate that the
86 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
88 bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) {
90 // TODO: generalize this function so that when
91 // a clip gets complex enough it can just be done in SW regardless
92 // of whether it would invoke the GrSoftwarePathRenderer.
93 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
95 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
96 const Element* element = iter.get();
97 // rects can always be drawn directly w/o using the software path
98 // Skip rrects once we're drawing them directly.
99 if (Element::kRect_Type != element->getType()) {
101 element->asPath(&path);
102 if (path_needs_SW_renderer(this->getContext(), fGpu, path, stroke, element->isAA())) {
110 bool GrClipMaskManager::installClipEffects(const ElementList& elements,
111 GrDrawState::AutoRestoreEffects* are,
112 const SkVector& clipToRTOffset,
113 const SkRect* drawBounds) {
115 GrDrawState* drawState = fGpu->drawState();
116 SkRect boundsInClipSpace;
118 boundsInClipSpace = *drawBounds;
119 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
123 GrRenderTarget* rt = drawState->getRenderTarget();
124 ElementList::Iter iter(elements);
130 SkRegion::Op op = iter.get()->getOp();
134 case SkRegion::kReplace_Op:
135 SkASSERT(iter.get() == elements.head());
136 // Fallthrough, handled same as intersect.
137 case SkRegion::kIntersect_Op:
139 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
143 case SkRegion::kDifference_Op:
145 // We don't currently have a cheap test for whether a rect is fully outside an
146 // element's primitive, so don't attempt to set skip.
157 GrPrimitiveEdgeType edgeType;
158 if (GR_AA_CLIP && iter.get()->isAA()) {
159 if (rt->isMultisampled()) {
160 // Coverage based AA clips don't place nicely with MSAA.
165 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
168 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
170 SkAutoTUnref<GrFragmentProcessor> fp;
171 switch (iter.get()->getType()) {
172 case SkClipStack::Element::kPath_Type:
173 fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(),
176 case SkClipStack::Element::kRRect_Type: {
177 SkRRect rrect = iter.get()->getRRect();
178 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
179 fp.reset(GrRRectEffect::Create(edgeType, rrect));
182 case SkClipStack::Element::kRect_Type: {
183 SkRect rect = iter.get()->getRect();
184 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
185 fp.reset(GrConvexPolyEffect::Create(edgeType, rect));
193 are->set(fGpu->drawState());
196 fGpu->drawState()->addCoverageProcessor(fp);
212 ////////////////////////////////////////////////////////////////////////////////
213 // sort out what kind of clip mask needs to be created: alpha, stencil,
214 // scissor, or entirely software
215 bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn,
216 GrDrawState::AutoRestoreEffects* are,
217 const SkRect* devBounds) {
218 fCurrClipMaskType = kNone_ClipMaskType;
220 ElementList elements(16);
222 InitialState initialState;
223 SkIRect clipSpaceIBounds;
226 GrDrawState* drawState = fGpu->drawState();
228 const GrRenderTarget* rt = drawState->getRenderTarget();
229 // GrDrawTarget should have filtered this for us
232 bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen();
235 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height());
236 clipSpaceRTIBounds.offset(clipDataIn->fOrigin);
237 ReduceClipStack(*clipDataIn->fClipStack,
244 if (elements.isEmpty()) {
245 if (kAllIn_InitialState == initialState) {
246 ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds;
254 fGpu->disableScissor();
255 this->setGpuStencil();
259 // An element count of 4 was chosen because of the common pattern in Blink of:
264 // when drawing rounded div borders. This could probably be tuned based on a
265 // configuration's relative costs of switching RTs to generate a mask vs
267 if (elements.count() <= 4) {
268 SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX),
269 SkIntToScalar(-clipDataIn->fOrigin.fY) };
270 if (elements.isEmpty() ||
271 (requiresAA && this->installClipEffects(elements, are, clipToRTOffset, devBounds))) {
272 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
273 scissorSpaceIBounds.offset(-clipDataIn->fOrigin);
274 if (NULL == devBounds ||
275 !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
276 fGpu->enableScissor(scissorSpaceIBounds);
278 fGpu->disableScissor();
280 this->setGpuStencil();
286 // If MSAA is enabled we can do everything in the stencil buffer.
287 if (0 == rt->numSamples() && requiresAA) {
288 GrTexture* result = NULL;
290 if (this->useSWOnlyPath(elements)) {
291 // The clip geometry is complex enough that it will be more efficient to create it
292 // entirely in software
293 result = this->createSoftwareClipMask(genID,
298 result = this->createAlphaClipMask(genID,
305 // The mask's top left coord should be pinned to the rounded-out top left corner of
306 // clipSpace bounds. We determine the mask's position WRT to the render target here.
307 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
308 rtSpaceMaskBounds.offset(-clipDataIn->fOrigin);
309 are->set(fGpu->drawState());
310 setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds);
311 fGpu->disableScissor();
312 this->setGpuStencil();
315 // if alpha clip mask creation fails fall through to the non-AA code paths
319 // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't
320 // be created. In either case, free up the texture in the anti-aliased mask cache.
321 // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g.,
322 // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be
323 // "incorrectly" clearing the AA cache.
326 // use the stencil clip if we can't represent the clip as a rectangle.
327 SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin;
328 this->createStencilClipMask(genID,
332 clipSpaceToStencilSpaceOffset);
334 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
335 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
336 // use both stencil and scissor test to the bounds for the final draw.
337 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
338 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
339 fGpu->enableScissor(scissorSpaceIBounds);
340 this->setGpuStencil();
344 #define VISUALIZE_COMPLEX_CLIP 0
346 #if VISUALIZE_COMPLEX_CLIP
347 #include "SkRandom.h"
349 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
351 #define SET_RANDOM_COLOR
356 ////////////////////////////////////////////////////////////////////////////////
357 // set up the OpenGL blend function to perform the specified
358 // boolean operation for alpha clip mask creation
359 void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) {
362 case SkRegion::kReplace_Op:
363 drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
365 case SkRegion::kIntersect_Op:
366 drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
368 case SkRegion::kUnion_Op:
369 drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
371 case SkRegion::kXOR_Op:
372 drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff);
374 case SkRegion::kDifference_Op:
375 drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
377 case SkRegion::kReverseDifference_Op:
378 drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff);
388 ////////////////////////////////////////////////////////////////////////////////
389 bool GrClipMaskManager::drawElement(GrTexture* target,
390 const SkClipStack::Element* element,
391 GrPathRenderer* pr) {
392 GrDrawState* drawState = fGpu->drawState();
394 drawState->setRenderTarget(target->asRenderTarget());
396 // TODO: Draw rrects directly here.
397 switch (element->getType()) {
398 case Element::kEmpty_Type:
399 SkDEBUGFAIL("Should never get here with an empty element.");
401 case Element::kRect_Type:
402 // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers
403 // the entire mask bounds and writes 0 outside the rect.
404 if (element->isAA()) {
405 getContext()->getAARectRenderer()->fillAARect(fGpu,
411 fGpu->drawSimpleRect(element->getRect());
416 element->asPath(&path);
417 if (path.isInverseFillType()) {
418 path.toggleInverseFillType();
420 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
422 GrPathRendererChain::DrawType type;
423 type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType :
424 GrPathRendererChain::kColor_DrawType;
425 pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
430 pr->drawPath(path, stroke, fGpu, element->isAA());
437 bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target,
438 const SkClipStack::Element* element,
439 GrPathRenderer** pr) {
440 GrDrawState* drawState = fGpu->drawState();
441 drawState->setRenderTarget(target->asRenderTarget());
443 if (Element::kRect_Type == element->getType()) {
446 // We shouldn't get here with an empty clip element.
447 SkASSERT(Element::kEmpty_Type != element->getType());
449 element->asPath(&path);
450 if (path.isInverseFillType()) {
451 path.toggleInverseFillType();
453 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
454 GrPathRendererChain::DrawType type = element->isAA() ?
455 GrPathRendererChain::kStencilAndColorAntiAlias_DrawType :
456 GrPathRendererChain::kStencilAndColor_DrawType;
457 *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type);
458 return SkToBool(*pr);
462 void GrClipMaskManager::mergeMask(GrTexture* dstMask,
465 const SkIRect& dstBound,
466 const SkIRect& srcBound) {
467 GrDrawState::AutoViewMatrixRestore avmr;
468 GrDrawState* drawState = fGpu->drawState();
469 SkAssertResult(avmr.setIdentity(drawState));
470 GrDrawState::AutoRestoreEffects are(drawState);
472 drawState->setRenderTarget(dstMask->asRenderTarget());
474 setup_boolean_blendcoeffs(drawState, op);
477 sampleM.setIDiv(srcMask->width(), srcMask->height());
479 drawState->addColorProcessor(
480 GrTextureDomainEffect::Create(srcMask,
482 GrTextureDomain::MakeTexelDomain(srcMask, srcBound),
483 GrTextureDomain::kDecal_Mode,
484 GrTextureParams::kNone_FilterMode))->unref();
485 fGpu->drawSimpleRect(SkRect::Make(dstBound));
488 // get a texture to act as a temporary buffer for AA clip boolean operations
489 // TODO: given the expense of createTexture we may want to just cache this too
490 void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) {
491 if (temp->texture()) {
492 // we've already allocated the temp texture
497 desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit;
499 desc.fHeight = height;
500 desc.fConfig = kAlpha_8_GrPixelConfig;
502 temp->set(this->getContext(), desc);
505 ////////////////////////////////////////////////////////////////////////////////
506 // Return the texture currently in the cache if it exists. Otherwise, return NULL
507 GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID,
508 const SkIRect& clipSpaceIBounds) {
509 bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds);
514 return fAACache.getLastMask();
517 ////////////////////////////////////////////////////////////////////////////////
518 // Allocate a texture in the texture cache. This function returns the texture
519 // allocated (or NULL on error).
520 GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID,
521 const SkIRect& clipSpaceIBounds,
523 // Since we are setting up the cache we should free up the
524 // currently cached mask so it can be reused.
528 desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit;
529 desc.fWidth = clipSpaceIBounds.width();
530 desc.fHeight = clipSpaceIBounds.height();
531 desc.fConfig = kRGBA_8888_GrPixelConfig;
532 if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
533 // We would always like A8 but it isn't supported on all platforms
534 desc.fConfig = kAlpha_8_GrPixelConfig;
537 fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds);
538 return fAACache.getLastMask();
541 ////////////////////////////////////////////////////////////////////////////////
542 // Create a 8-bit clip mask in alpha
543 GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID,
544 InitialState initialState,
545 const ElementList& elements,
546 const SkIRect& clipSpaceIBounds) {
547 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
549 // First, check for cached texture
550 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
552 fCurrClipMaskType = kAlpha_ClipMaskType;
556 // There's no texture in the cache. Let's try to allocate it then.
557 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false);
558 if (NULL == result) {
563 // The top-left of the mask corresponds to the top-left corner of the bounds.
564 SkVector clipToMaskOffset = {
565 SkIntToScalar(-clipSpaceIBounds.fLeft),
566 SkIntToScalar(-clipSpaceIBounds.fTop)
568 // The texture may be larger than necessary, this rect represents the part of the texture
569 // we populate with a rasterization of the clip.
570 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
572 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
574 translate.setTranslate(clipToMaskOffset);
575 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate);
577 GrDrawState* drawState = fGpu->drawState();
579 // We're drawing a coverage mask and want coverage to be run through the blend function.
580 drawState->enableState(GrDrawState::kCoverageDrawing_StateBit);
582 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
583 // clear the part that we care about.
584 fGpu->clear(&maskSpaceIBounds,
585 kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
587 result->asRenderTarget());
589 // When we use the stencil in the below loop it is important to have this clip installed.
590 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
591 // pass must not set values outside of this bounds or stencil values outside the rect won't be
593 GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds);
594 drawState->enableState(GrDrawState::kClip_StateBit);
596 GrAutoScratchTexture temp;
597 // walk through each clip element and perform its set op
598 for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
599 const Element* element = iter.get();
600 SkRegion::Op op = element->getOp();
601 bool invert = element->isInverseFilled();
603 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
604 GrPathRenderer* pr = NULL;
605 bool useTemp = !this->canStencilAndDrawElement(result, element, &pr);
607 // This is the bounds of the clip element in the space of the alpha-mask. The temporary
608 // mask buffer can be substantially larger than the actually clip stack element. We
609 // touch the minimum number of pixels necessary and use decal mode to combine it with
611 SkIRect maskSpaceElementIBounds;
615 maskSpaceElementIBounds = maskSpaceIBounds;
617 SkRect elementBounds = element->getBounds();
618 elementBounds.offset(clipToMaskOffset);
619 elementBounds.roundOut(&maskSpaceElementIBounds);
622 this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp);
623 if (NULL == temp.texture()) {
627 dst = temp.texture();
628 // clear the temp target and set blend to replace
629 fGpu->clear(&maskSpaceElementIBounds,
630 invert ? 0xffffffff : 0x00000000,
632 dst->asRenderTarget());
633 setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op);
636 // draw directly into the result with the stencil set to make the pixels affected
637 // by the clip shape be non-zero.
639 GR_STATIC_CONST_SAME_STENCIL(kStencilInElement,
646 drawState->setStencil(kStencilInElement);
647 setup_boolean_blendcoeffs(drawState, op);
650 drawState->setAlpha(invert ? 0x00 : 0xff);
652 if (!this->drawElement(dst, element, pr)) {
658 // Now draw into the accumulator using the real operation and the temp buffer as a
660 this->mergeMask(result,
664 maskSpaceElementIBounds);
666 // Draw to the exterior pixels (those with a zero stencil value).
667 drawState->setAlpha(invert ? 0xff : 0x00);
668 GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement,
675 drawState->setStencil(kDrawOutsideElement);
676 fGpu->drawSimpleRect(clipSpaceIBounds);
677 drawState->disableStencil();
680 // all the remaining ops can just be directly draw into the accumulation buffer
681 drawState->setAlpha(0xff);
682 setup_boolean_blendcoeffs(drawState, op);
683 this->drawElement(result, element);
687 fCurrClipMaskType = kAlpha_ClipMaskType;
691 ////////////////////////////////////////////////////////////////////////////////
692 // Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
693 // (as opposed to canvas) coordinates
694 bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID,
695 InitialState initialState,
696 const ElementList& elements,
697 const SkIRect& clipSpaceIBounds,
698 const SkIPoint& clipSpaceToStencilOffset) {
700 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
702 GrDrawState* drawState = fGpu->drawState();
703 SkASSERT(drawState->isClipState());
705 GrRenderTarget* rt = drawState->getRenderTarget();
708 // TODO: dynamically attach a SB when needed.
709 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
710 if (NULL == stencilBuffer) {
714 if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
716 stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
718 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
719 SkVector translate = {
720 SkIntToScalar(clipSpaceToStencilOffset.fX),
721 SkIntToScalar(clipSpaceToStencilOffset.fY)
724 matrix.setTranslate(translate);
725 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
726 drawState = fGpu->drawState();
728 drawState->setRenderTarget(rt);
730 // We set the current clip to the bounds so that our recursive draws are scissored to them.
731 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
732 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
733 GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds);
734 drawState->enableState(GrDrawState::kClip_StateBit);
736 #if !VISUALIZE_COMPLEX_CLIP
737 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
740 int clipBit = stencilBuffer->bits();
741 SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers");
742 clipBit = (1 << (clipBit-1));
744 fGpu->clearStencilClip(rt, stencilSpaceIBounds, kAllIn_InitialState == initialState);
746 // walk through each clip element and perform its set op
747 // with the existing clip.
748 for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
749 const Element* element = iter.get();
750 bool fillInverted = false;
751 // enabled at bottom of loop
752 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
753 // if the target is MSAA then we want MSAA enabled when the clip is soft
754 if (rt->isMultisampled()) {
755 drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA());
758 // This will be used to determine whether the clip shape can be rendered into the
759 // stencil with arbitrary stencil settings.
760 GrPathRenderer::StencilSupport stencilSupport;
762 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
764 SkRegion::Op op = element->getOp();
766 GrPathRenderer* pr = NULL;
768 if (Element::kRect_Type == element->getType()) {
769 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
770 fillInverted = false;
772 element->asPath(&clipPath);
773 fillInverted = clipPath.isInverseFillType();
775 clipPath.toggleInverseFillType();
777 pr = this->getContext()->getPathRenderer(clipPath,
781 GrPathRendererChain::kStencilOnly_DrawType,
789 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
791 bool canRenderDirectToStencil =
792 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
793 bool canDrawDirectToClip; // Given the renderer, the element,
794 // fill rule, and set operation can
795 // we render the element directly to
796 // stencil bit used for clipping.
797 canDrawDirectToClip = GrStencilSettings::GetClipPasses(op,
798 canRenderDirectToStencil,
804 // draw the element to the client stencil bits if necessary
805 if (!canDrawDirectToClip) {
806 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
814 if (Element::kRect_Type == element->getType()) {
815 *drawState->stencil() = gDrawToStencil;
816 fGpu->drawSimpleRect(element->getRect());
818 if (!clipPath.isEmpty()) {
819 if (canRenderDirectToStencil) {
820 *drawState->stencil() = gDrawToStencil;
821 pr->drawPath(clipPath, stroke, fGpu, false);
823 pr->stencilPath(clipPath, stroke, fGpu);
829 // now we modify the clip bit by rendering either the clip
830 // element directly or a bounding rect of the entire clip.
831 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
832 for (int p = 0; p < passes; ++p) {
833 *drawState->stencil() = stencilSettings[p];
834 if (canDrawDirectToClip) {
835 if (Element::kRect_Type == element->getType()) {
837 fGpu->drawSimpleRect(element->getRect());
840 pr->drawPath(clipPath, stroke, fGpu, false);
844 // The view matrix is setup to do clip space -> stencil space translation, so
845 // draw rect in clip space.
846 fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds));
851 // set this last because recursive draws may overwrite it back to kNone.
852 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
853 fCurrClipMaskType = kStencil_ClipMaskType;
858 // mapping of clip-respecting stencil funcs to normal stencil funcs
859 // mapping depends on whether stencil-clipping is in effect.
860 static const GrStencilFunc
861 gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = {
862 {// Stencil-Clipping is DISABLED, we are effectively always inside the clip
864 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
865 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
866 kLess_StencilFunc, // kLessIfInClip_StencilFunc
867 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
868 // Special in the clip func that forces user's ref to be 0.
869 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
870 // make ref 0 and do normal nequal.
872 {// Stencil-Clipping is ENABLED
874 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
875 // eq stencil clip bit, mask
878 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
879 // add stencil bit to mask and ref
881 kLess_StencilFunc, // kLessIfInClip_StencilFunc
882 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
883 // for both of these we can add
884 // the clip bit to the mask and
885 // ref and compare as normal
886 // Special in the clip func that forces user's ref to be 0.
887 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
888 // make ref have only the clip bit set
889 // and make comparison be less
890 // 10..0 < 1..user_bits..
895 // Sets the settings to clip against the stencil buffer clip while ignoring the
897 const GrStencilSettings& basic_apply_stencil_clip_settings() {
898 // stencil settings to use when clip is in stencil
899 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
902 kAlwaysIfInClip_StencilFunc,
906 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
910 void GrClipMaskManager::setGpuStencil() {
911 // We make two copies of the StencilSettings here (except in the early
912 // exit scenario. One copy from draw state to the stack var. Then another
913 // from the stack var to the gpu. We could make this class hold a ptr to
914 // GrGpu's fStencilSettings and eliminate the stack copy here.
916 const GrDrawState& drawState = fGpu->getDrawState();
918 // use stencil for clipping if clipping is enabled and the clip
919 // has been written into the stencil.
920 GrClipMaskManager::StencilClipMode clipMode;
921 if (this->isClipInStencil() && drawState.isClipState()) {
922 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
923 // We can't be modifying the clip and respecting it at the same time.
924 SkASSERT(!drawState.isStateFlagEnabled(
925 GrGpu::kModifyStencilClip_StateBit));
926 } else if (drawState.isStateFlagEnabled(
927 GrGpu::kModifyStencilClip_StateBit)) {
928 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
930 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
933 GrStencilSettings settings;
934 // The GrGpu client may not be using the stencil buffer but we may need to
935 // enable it in order to respect a stencil clip.
936 if (drawState.getStencil().isDisabled()) {
937 if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
938 settings = basic_apply_stencil_clip_settings();
940 fGpu->disableStencil();
944 settings = drawState.getStencil();
947 // TODO: dynamically attach a stencil buffer
949 GrStencilBuffer* stencilBuffer =
950 drawState.getRenderTarget()->getStencilBuffer();
952 stencilBits = stencilBuffer->bits();
955 SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp());
956 SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided());
957 this->adjustStencilParams(&settings, clipMode, stencilBits);
958 fGpu->setStencilSettings(settings);
961 void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings,
962 StencilClipMode mode,
964 SkASSERT(stencilBitCnt > 0);
966 if (kModifyClip_StencilClipMode == mode) {
967 // We assume that this clip manager itself is drawing to the GrGpu and
968 // has already setup the correct values.
972 unsigned int clipBit = (1 << (stencilBitCnt - 1));
973 unsigned int userBits = clipBit - 1;
975 GrStencilSettings::Face face = GrStencilSettings::kFront_Face;
976 bool twoSided = fGpu->caps()->twoSidedStencilSupport();
978 bool finished = false;
980 GrStencilFunc func = settings->func(face);
981 uint16_t writeMask = settings->writeMask(face);
982 uint16_t funcMask = settings->funcMask(face);
983 uint16_t funcRef = settings->funcRef(face);
985 SkASSERT((unsigned) func < kStencilFuncCount);
987 writeMask &= userBits;
989 if (func >= kBasicStencilFuncCount) {
990 int respectClip = kRespectClip_StencilClipMode == mode;
992 // The GrGpu class should have checked this
993 SkASSERT(this->isClipInStencil());
995 case kAlwaysIfInClip_StencilFunc:
999 case kEqualIfInClip_StencilFunc:
1000 case kLessIfInClip_StencilFunc:
1001 case kLEqualIfInClip_StencilFunc:
1002 funcMask = (funcMask & userBits) | clipBit;
1003 funcRef = (funcRef & userBits) | clipBit;
1005 case kNonZeroIfInClip_StencilFunc:
1006 funcMask = (funcMask & userBits) | clipBit;
1010 SkFAIL("Unknown stencil func");
1013 funcMask &= userBits;
1014 funcRef &= userBits;
1016 const GrStencilFunc* table =
1017 gSpecialToBasicStencilFunc[respectClip];
1018 func = table[func - kBasicStencilFuncCount];
1019 SkASSERT(func >= 0 && func < kBasicStencilFuncCount);
1021 funcMask &= userBits;
1022 funcRef &= userBits;
1025 settings->setFunc(face, func);
1026 settings->setWriteMask(face, writeMask);
1027 settings->setFuncMask(face, funcMask);
1028 settings->setFuncRef(face, funcRef);
1030 if (GrStencilSettings::kFront_Face == face) {
1031 face = GrStencilSettings::kBack_Face;
1032 finished = !twoSided;
1038 settings->copyFrontSettingsToBack();
1042 ////////////////////////////////////////////////////////////////////////////////
1043 GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID,
1044 GrReducedClip::InitialState initialState,
1045 const GrReducedClip::ElementList& elements,
1046 const SkIRect& clipSpaceIBounds) {
1047 SkASSERT(kNone_ClipMaskType == fCurrClipMaskType);
1049 GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds);
1054 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
1055 // the top left corner of the resulting rect to the top left of the texture.
1056 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
1058 GrSWMaskHelper helper(this->getContext());
1061 matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft),
1062 SkIntToScalar(-clipSpaceIBounds.fTop));
1063 helper.init(maskSpaceIBounds, &matrix, false);
1065 helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00);
1067 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1069 for (ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
1071 const Element* element = iter.get();
1072 SkRegion::Op op = element->getOp();
1074 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
1075 // Intersect and reverse difference require modifying pixels outside of the geometry
1076 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
1077 // but leave the pixels inside the geometry alone. For reverse difference we invert all
1078 // the pixels before clearing the ones outside the geometry.
1079 if (SkRegion::kReverseDifference_Op == op) {
1080 SkRect temp = SkRect::Make(clipSpaceIBounds);
1081 // invert the entire scene
1082 helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF);
1086 element->asPath(&clipPath);
1087 clipPath.toggleInverseFillType();
1088 helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00);
1093 // The other ops (union, xor, diff) only affect pixels inside
1094 // the geometry so they can just be drawn normally
1095 if (Element::kRect_Type == element->getType()) {
1096 helper.draw(element->getRect(), op, element->isAA(), 0xFF);
1099 element->asPath(&path);
1100 helper.draw(path, stroke, op, element->isAA(), 0xFF);
1104 // Allocate clip mask texture
1105 result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true);
1106 if (NULL == result) {
1110 helper.toTexture(result);
1112 fCurrClipMaskType = kAlpha_ClipMaskType;
1116 ////////////////////////////////////////////////////////////////////////////////
1117 void GrClipMaskManager::purgeResources() {
1118 fAACache.purgeResources();
1121 void GrClipMaskManager::setGpu(GrGpu* gpu) {
1123 fAACache.setContext(gpu->getContext());
1126 void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) {
1127 const GrDrawState& drawState = fGpu->getDrawState();
1128 GrClipMaskManager::StencilClipMode clipMode;
1129 if (this->isClipInStencil() && drawState.isClipState()) {
1130 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
1131 // We can't be modifying the clip and respecting it at the same time.
1132 SkASSERT(!drawState.isStateFlagEnabled(
1133 GrGpu::kModifyStencilClip_StateBit));
1134 } else if (drawState.isStateFlagEnabled(
1135 GrGpu::kModifyStencilClip_StateBit)) {
1136 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
1138 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
1141 // TODO: dynamically attach a stencil buffer
1142 int stencilBits = 0;
1143 GrStencilBuffer* stencilBuffer =
1144 drawState.getRenderTarget()->getStencilBuffer();
1145 if (stencilBuffer) {
1146 stencilBits = stencilBuffer->bits();
1147 this->adjustStencilParams(settings, clipMode, stencilBits);