michael@0: michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrClipMaskManager.h" michael@0: #include "GrAAConvexPathRenderer.h" michael@0: #include "GrAAHairLinePathRenderer.h" michael@0: #include "GrAARectRenderer.h" michael@0: #include "GrDrawTargetCaps.h" michael@0: #include "GrGpu.h" michael@0: #include "GrPaint.h" michael@0: #include "GrPathRenderer.h" michael@0: #include "GrRenderTarget.h" michael@0: #include "GrStencilBuffer.h" michael@0: #include "GrSWMaskHelper.h" michael@0: #include "effects/GrTextureDomain.h" michael@0: #include "effects/GrConvexPolyEffect.h" michael@0: #include "effects/GrRRectEffect.h" michael@0: #include "SkRasterClip.h" michael@0: #include "SkStrokeRec.h" michael@0: #include "SkTLazy.h" michael@0: michael@0: #define GR_AA_CLIP 1 michael@0: michael@0: typedef SkClipStack::Element Element; michael@0: michael@0: using namespace GrReducedClip; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: namespace { michael@0: // set up the draw state to enable the aa clipping mask. Besides setting up the michael@0: // stage matrix this also alters the vertex layout michael@0: void setup_drawstate_aaclip(GrGpu* gpu, michael@0: GrTexture* result, michael@0: const SkIRect &devBound) { michael@0: GrDrawState* drawState = gpu->drawState(); michael@0: SkASSERT(drawState); michael@0: michael@0: SkMatrix mat; michael@0: // We want to use device coords to compute the texture coordinates. We set our matrix to be michael@0: // equal to the view matrix followed by an offset to the devBound, and then a scaling matrix to michael@0: // normalized coords. We apply this matrix to the vertex positions rather than local coords. michael@0: mat.setIDiv(result->width(), result->height()); michael@0: mat.preTranslate(SkIntToScalar(-devBound.fLeft), michael@0: SkIntToScalar(-devBound.fTop)); michael@0: mat.preConcat(drawState->getViewMatrix()); michael@0: michael@0: SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height()); michael@0: // This could be a long-lived effect that is cached with the alpha-mask. michael@0: drawState->addCoverageEffect( michael@0: GrTextureDomainEffect::Create(result, michael@0: mat, michael@0: GrTextureDomain::MakeTexelDomain(result, domainTexels), michael@0: GrTextureDomain::kDecal_Mode, michael@0: GrTextureParams::kNone_FilterMode, michael@0: kPosition_GrCoordSet))->unref(); michael@0: } michael@0: michael@0: bool path_needs_SW_renderer(GrContext* context, michael@0: GrGpu* gpu, michael@0: const SkPath& origPath, michael@0: const SkStrokeRec& stroke, michael@0: bool doAA) { michael@0: // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer michael@0: SkTCopyOnFirstWrite path(origPath); michael@0: if (path->isInverseFillType()) { michael@0: path.writable()->toggleInverseFillType(); michael@0: } michael@0: // last (false) parameter disallows use of the SW path renderer michael@0: GrPathRendererChain::DrawType type = doAA ? michael@0: GrPathRendererChain::kColorAntiAlias_DrawType : michael@0: GrPathRendererChain::kColor_DrawType; michael@0: michael@0: return NULL == context->getPathRenderer(*path, stroke, gpu, false, type); michael@0: } michael@0: michael@0: } michael@0: michael@0: /* michael@0: * This method traverses the clip stack to see if the GrSoftwarePathRenderer michael@0: * will be used on any element. If so, it returns true to indicate that the michael@0: * entire clip should be rendered in SW and then uploaded en masse to the gpu. michael@0: */ michael@0: bool GrClipMaskManager::useSWOnlyPath(const ElementList& elements) { michael@0: michael@0: // TODO: generalize this function so that when michael@0: // a clip gets complex enough it can just be done in SW regardless michael@0: // of whether it would invoke the GrSoftwarePathRenderer. michael@0: SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); michael@0: michael@0: for (ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { michael@0: const Element* element = iter.get(); michael@0: // rects can always be drawn directly w/o using the software path michael@0: // Skip rrects once we're drawing them directly. michael@0: if (Element::kRect_Type != element->getType()) { michael@0: SkPath path; michael@0: element->asPath(&path); michael@0: if (path_needs_SW_renderer(this->getContext(), fGpu, path, stroke, element->isAA())) { michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool GrClipMaskManager::installClipEffects(const ElementList& elements, michael@0: GrDrawState::AutoRestoreEffects* are, michael@0: const SkVector& clipToRTOffset, michael@0: const SkRect* drawBounds) { michael@0: michael@0: GrDrawState* drawState = fGpu->drawState(); michael@0: SkRect boundsInClipSpace; michael@0: if (NULL != drawBounds) { michael@0: boundsInClipSpace = *drawBounds; michael@0: boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY); michael@0: } michael@0: michael@0: are->set(drawState); michael@0: GrRenderTarget* rt = drawState->getRenderTarget(); michael@0: ElementList::Iter iter(elements); michael@0: michael@0: bool setARE = false; michael@0: bool failed = false; michael@0: michael@0: while (NULL != iter.get()) { michael@0: SkRegion::Op op = iter.get()->getOp(); michael@0: bool invert; michael@0: bool skip = false; michael@0: switch (op) { michael@0: case SkRegion::kReplace_Op: michael@0: SkASSERT(iter.get() == elements.head()); michael@0: // Fallthrough, handled same as intersect. michael@0: case SkRegion::kIntersect_Op: michael@0: invert = false; michael@0: if (NULL != drawBounds && iter.get()->contains(boundsInClipSpace)) { michael@0: skip = true; michael@0: } michael@0: break; michael@0: case SkRegion::kDifference_Op: michael@0: invert = true; michael@0: // We don't currently have a cheap test for whether a rect is fully outside an michael@0: // element's primitive, so don't attempt to set skip. michael@0: break; michael@0: default: michael@0: failed = true; michael@0: break; michael@0: } michael@0: if (failed) { michael@0: break; michael@0: } michael@0: michael@0: if (!skip) { michael@0: GrEffectEdgeType edgeType; michael@0: if (GR_AA_CLIP && iter.get()->isAA()) { michael@0: if (rt->isMultisampled()) { michael@0: // Coverage based AA clips don't place nicely with MSAA. michael@0: failed = true; michael@0: break; michael@0: } michael@0: edgeType = invert ? kInverseFillAA_GrEffectEdgeType : kFillAA_GrEffectEdgeType; michael@0: } else { michael@0: edgeType = invert ? kInverseFillBW_GrEffectEdgeType : kFillBW_GrEffectEdgeType; michael@0: } michael@0: SkAutoTUnref effect; michael@0: switch (iter.get()->getType()) { michael@0: case SkClipStack::Element::kPath_Type: michael@0: effect.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(), michael@0: &clipToRTOffset)); michael@0: break; michael@0: case SkClipStack::Element::kRRect_Type: { michael@0: SkRRect rrect = iter.get()->getRRect(); michael@0: rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY); michael@0: effect.reset(GrRRectEffect::Create(edgeType, rrect)); michael@0: break; michael@0: } michael@0: case SkClipStack::Element::kRect_Type: { michael@0: SkRect rect = iter.get()->getRect(); michael@0: rect.offset(clipToRTOffset.fX, clipToRTOffset.fY); michael@0: effect.reset(GrConvexPolyEffect::Create(edgeType, rect)); michael@0: break; michael@0: } michael@0: default: michael@0: break; michael@0: } michael@0: if (effect) { michael@0: if (!setARE) { michael@0: are->set(fGpu->drawState()); michael@0: setARE = true; michael@0: } michael@0: fGpu->drawState()->addCoverageEffect(effect); michael@0: } else { michael@0: failed = true; michael@0: break; michael@0: } michael@0: } michael@0: iter.next(); michael@0: } michael@0: michael@0: if (failed) { michael@0: are->set(NULL); michael@0: } michael@0: michael@0: return !failed; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // sort out what kind of clip mask needs to be created: alpha, stencil, michael@0: // scissor, or entirely software michael@0: bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn, michael@0: GrDrawState::AutoRestoreEffects* are, michael@0: const SkRect* devBounds) { michael@0: fCurrClipMaskType = kNone_ClipMaskType; michael@0: michael@0: ElementList elements(16); michael@0: int32_t genID; michael@0: InitialState initialState; michael@0: SkIRect clipSpaceIBounds; michael@0: bool requiresAA; michael@0: bool isRect = false; michael@0: michael@0: GrDrawState* drawState = fGpu->drawState(); michael@0: michael@0: const GrRenderTarget* rt = drawState->getRenderTarget(); michael@0: // GrDrawTarget should have filtered this for us michael@0: SkASSERT(NULL != rt); michael@0: michael@0: bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen(); michael@0: michael@0: if (!ignoreClip) { michael@0: SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height()); michael@0: clipSpaceRTIBounds.offset(clipDataIn->fOrigin); michael@0: ReduceClipStack(*clipDataIn->fClipStack, michael@0: clipSpaceRTIBounds, michael@0: &elements, michael@0: &genID, michael@0: &initialState, michael@0: &clipSpaceIBounds, michael@0: &requiresAA); michael@0: if (elements.isEmpty()) { michael@0: if (kAllIn_InitialState == initialState) { michael@0: ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds; michael@0: isRect = true; michael@0: } else { michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (ignoreClip) { michael@0: fGpu->disableScissor(); michael@0: this->setGpuStencil(); michael@0: return true; michael@0: } michael@0: michael@0: // An element count of 4 was chosen because of the common pattern in Blink of: michael@0: // isect RR michael@0: // diff RR michael@0: // isect convex_poly michael@0: // isect convex_poly michael@0: // when drawing rounded div borders. This could probably be tuned based on a michael@0: // configuration's relative costs of switching RTs to generate a mask vs michael@0: // longer shaders. michael@0: if (elements.count() <= 4) { michael@0: SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX), michael@0: SkIntToScalar(-clipDataIn->fOrigin.fY) }; michael@0: if (elements.isEmpty() || michael@0: this->installClipEffects(elements, are, clipToRTOffset, devBounds)) { michael@0: SkIRect scissorSpaceIBounds(clipSpaceIBounds); michael@0: scissorSpaceIBounds.offset(-clipDataIn->fOrigin); michael@0: if (NULL == devBounds || michael@0: !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) { michael@0: fGpu->enableScissor(scissorSpaceIBounds); michael@0: } else { michael@0: fGpu->disableScissor(); michael@0: } michael@0: this->setGpuStencil(); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: #if GR_AA_CLIP michael@0: // If MSAA is enabled we can do everything in the stencil buffer. michael@0: if (0 == rt->numSamples() && requiresAA) { michael@0: GrTexture* result = NULL; michael@0: michael@0: if (this->useSWOnlyPath(elements)) { michael@0: // The clip geometry is complex enough that it will be more efficient to create it michael@0: // entirely in software michael@0: result = this->createSoftwareClipMask(genID, michael@0: initialState, michael@0: elements, michael@0: clipSpaceIBounds); michael@0: } else { michael@0: result = this->createAlphaClipMask(genID, michael@0: initialState, michael@0: elements, michael@0: clipSpaceIBounds); michael@0: } michael@0: michael@0: if (NULL != result) { michael@0: // The mask's top left coord should be pinned to the rounded-out top left corner of michael@0: // clipSpace bounds. We determine the mask's position WRT to the render target here. michael@0: SkIRect rtSpaceMaskBounds = clipSpaceIBounds; michael@0: rtSpaceMaskBounds.offset(-clipDataIn->fOrigin); michael@0: are->set(fGpu->drawState()); michael@0: setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds); michael@0: fGpu->disableScissor(); michael@0: this->setGpuStencil(); michael@0: return true; michael@0: } michael@0: // if alpha clip mask creation fails fall through to the non-AA code paths michael@0: } michael@0: #endif // GR_AA_CLIP michael@0: michael@0: // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't michael@0: // be created. In either case, free up the texture in the anti-aliased mask cache. michael@0: // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g., michael@0: // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be michael@0: // "incorrectly" clearing the AA cache. michael@0: fAACache.reset(); michael@0: michael@0: // If the clip is a rectangle then just set the scissor. Otherwise, create michael@0: // a stencil mask. michael@0: if (isRect) { michael@0: SkIRect clipRect = clipSpaceIBounds; michael@0: clipRect.offset(-clipDataIn->fOrigin); michael@0: fGpu->enableScissor(clipRect); michael@0: this->setGpuStencil(); michael@0: return true; michael@0: } michael@0: michael@0: // use the stencil clip if we can't represent the clip as a rectangle. michael@0: SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin; michael@0: this->createStencilClipMask(genID, michael@0: initialState, michael@0: elements, michael@0: clipSpaceIBounds, michael@0: clipSpaceToStencilSpaceOffset); michael@0: michael@0: // This must occur after createStencilClipMask. That function may change the scissor. Also, it michael@0: // only guarantees that the stencil mask is correct within the bounds it was passed, so we must michael@0: // use both stencil and scissor test to the bounds for the final draw. michael@0: SkIRect scissorSpaceIBounds(clipSpaceIBounds); michael@0: scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset); michael@0: fGpu->enableScissor(scissorSpaceIBounds); michael@0: this->setGpuStencil(); michael@0: return true; michael@0: } michael@0: michael@0: #define VISUALIZE_COMPLEX_CLIP 0 michael@0: michael@0: #if VISUALIZE_COMPLEX_CLIP michael@0: #include "SkRandom.h" michael@0: SkRandom gRandom; michael@0: #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU()); michael@0: #else michael@0: #define SET_RANDOM_COLOR michael@0: #endif michael@0: michael@0: namespace { michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // set up the OpenGL blend function to perform the specified michael@0: // boolean operation for alpha clip mask creation michael@0: void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) { michael@0: michael@0: switch (op) { michael@0: case SkRegion::kReplace_Op: michael@0: drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff); michael@0: break; michael@0: case SkRegion::kIntersect_Op: michael@0: drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff); michael@0: break; michael@0: case SkRegion::kUnion_Op: michael@0: drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff); michael@0: break; michael@0: case SkRegion::kXOR_Op: michael@0: drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff); michael@0: break; michael@0: case SkRegion::kDifference_Op: michael@0: drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff); michael@0: break; michael@0: case SkRegion::kReverseDifference_Op: michael@0: drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff); michael@0: break; michael@0: default: michael@0: SkASSERT(false); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: bool GrClipMaskManager::drawElement(GrTexture* target, michael@0: const SkClipStack::Element* element, michael@0: GrPathRenderer* pr) { michael@0: GrDrawState* drawState = fGpu->drawState(); michael@0: michael@0: drawState->setRenderTarget(target->asRenderTarget()); michael@0: michael@0: // TODO: Draw rrects directly here. michael@0: switch (element->getType()) { michael@0: case Element::kEmpty_Type: michael@0: SkDEBUGFAIL("Should never get here with an empty element."); michael@0: break; michael@0: case Element::kRect_Type: michael@0: // TODO: Do rects directly to the accumulator using a aa-rect GrEffect that covers the michael@0: // entire mask bounds and writes 0 outside the rect. michael@0: if (element->isAA()) { michael@0: getContext()->getAARectRenderer()->fillAARect(fGpu, michael@0: fGpu, michael@0: element->getRect(), michael@0: SkMatrix::I(), michael@0: element->getRect(), michael@0: false); michael@0: } else { michael@0: fGpu->drawSimpleRect(element->getRect(), NULL); michael@0: } michael@0: return true; michael@0: default: { michael@0: SkPath path; michael@0: element->asPath(&path); michael@0: if (path.isInverseFillType()) { michael@0: path.toggleInverseFillType(); michael@0: } michael@0: SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); michael@0: if (NULL == pr) { michael@0: GrPathRendererChain::DrawType type; michael@0: type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType : michael@0: GrPathRendererChain::kColor_DrawType; michael@0: pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type); michael@0: } michael@0: if (NULL == pr) { michael@0: return false; michael@0: } michael@0: pr->drawPath(path, stroke, fGpu, element->isAA()); michael@0: break; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target, michael@0: const SkClipStack::Element* element, michael@0: GrPathRenderer** pr) { michael@0: GrDrawState* drawState = fGpu->drawState(); michael@0: drawState->setRenderTarget(target->asRenderTarget()); michael@0: michael@0: if (Element::kRect_Type == element->getType()) { michael@0: return true; michael@0: } else { michael@0: // We shouldn't get here with an empty clip element. michael@0: SkASSERT(Element::kEmpty_Type != element->getType()); michael@0: SkPath path; michael@0: element->asPath(&path); michael@0: if (path.isInverseFillType()) { michael@0: path.toggleInverseFillType(); michael@0: } michael@0: SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); michael@0: GrPathRendererChain::DrawType type = element->isAA() ? michael@0: GrPathRendererChain::kStencilAndColorAntiAlias_DrawType : michael@0: GrPathRendererChain::kStencilAndColor_DrawType; michael@0: *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type); michael@0: return NULL != *pr; michael@0: } michael@0: } michael@0: michael@0: void GrClipMaskManager::mergeMask(GrTexture* dstMask, michael@0: GrTexture* srcMask, michael@0: SkRegion::Op op, michael@0: const SkIRect& dstBound, michael@0: const SkIRect& srcBound) { michael@0: GrDrawState::AutoViewMatrixRestore avmr; michael@0: GrDrawState* drawState = fGpu->drawState(); michael@0: SkAssertResult(avmr.setIdentity(drawState)); michael@0: GrDrawState::AutoRestoreEffects are(drawState); michael@0: michael@0: drawState->setRenderTarget(dstMask->asRenderTarget()); michael@0: michael@0: setup_boolean_blendcoeffs(drawState, op); michael@0: michael@0: SkMatrix sampleM; michael@0: sampleM.setIDiv(srcMask->width(), srcMask->height()); michael@0: michael@0: drawState->addColorEffect( michael@0: GrTextureDomainEffect::Create(srcMask, michael@0: sampleM, michael@0: GrTextureDomain::MakeTexelDomain(srcMask, srcBound), michael@0: GrTextureDomain::kDecal_Mode, michael@0: GrTextureParams::kNone_FilterMode))->unref(); michael@0: fGpu->drawSimpleRect(SkRect::Make(dstBound), NULL); michael@0: } michael@0: michael@0: // get a texture to act as a temporary buffer for AA clip boolean operations michael@0: // TODO: given the expense of createTexture we may want to just cache this too michael@0: void GrClipMaskManager::getTemp(int width, int height, GrAutoScratchTexture* temp) { michael@0: if (NULL != temp->texture()) { michael@0: // we've already allocated the temp texture michael@0: return; michael@0: } michael@0: michael@0: GrTextureDesc desc; michael@0: desc.fFlags = kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit; michael@0: desc.fWidth = width; michael@0: desc.fHeight = height; michael@0: desc.fConfig = kAlpha_8_GrPixelConfig; michael@0: michael@0: temp->set(this->getContext(), desc); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // Handles caching & allocation (if needed) of a clip alpha-mask texture for both the sw-upload michael@0: // or gpu-rendered cases. Returns true if there is no more work to be done (i.e., we got a cache michael@0: // hit) michael@0: bool GrClipMaskManager::getMaskTexture(int32_t elementsGenID, michael@0: const SkIRect& clipSpaceIBounds, michael@0: GrTexture** result, michael@0: bool willUpload) { michael@0: bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds); michael@0: if (!cached) { michael@0: michael@0: // There isn't a suitable entry in the cache so we create a new texture to store the mask. michael@0: // Since we are setting up the cache we know the last lookup was a miss. Free up the michael@0: // currently cached mask so it can be reused. michael@0: fAACache.reset(); michael@0: michael@0: GrTextureDesc desc; michael@0: desc.fFlags = willUpload ? kNone_GrTextureFlags : kRenderTarget_GrTextureFlagBit; michael@0: desc.fWidth = clipSpaceIBounds.width(); michael@0: desc.fHeight = clipSpaceIBounds.height(); michael@0: desc.fConfig = kRGBA_8888_GrPixelConfig; michael@0: if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { michael@0: // We would always like A8 but it isn't supported on all platforms michael@0: desc.fConfig = kAlpha_8_GrPixelConfig; michael@0: } michael@0: michael@0: fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds); michael@0: } michael@0: michael@0: *result = fAACache.getLastMask(); michael@0: return cached; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // Create a 8-bit clip mask in alpha michael@0: GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID, michael@0: InitialState initialState, michael@0: const ElementList& elements, michael@0: const SkIRect& clipSpaceIBounds) { michael@0: SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); michael@0: michael@0: GrTexture* result; michael@0: if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, false)) { michael@0: fCurrClipMaskType = kAlpha_ClipMaskType; michael@0: return result; michael@0: } michael@0: michael@0: if (NULL == result) { michael@0: fAACache.reset(); michael@0: return NULL; michael@0: } michael@0: michael@0: // The top-left of the mask corresponds to the top-left corner of the bounds. michael@0: SkVector clipToMaskOffset = { michael@0: SkIntToScalar(-clipSpaceIBounds.fLeft), michael@0: SkIntToScalar(-clipSpaceIBounds.fTop) michael@0: }; michael@0: // The texture may be larger than necessary, this rect represents the part of the texture michael@0: // we populate with a rasterization of the clip. michael@0: SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); michael@0: michael@0: // Set the matrix so that rendered clip elements are transformed to mask space from clip space. michael@0: SkMatrix translate; michael@0: translate.setTranslate(clipToMaskOffset); michael@0: GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate); michael@0: michael@0: GrDrawState* drawState = fGpu->drawState(); michael@0: michael@0: // We're drawing a coverage mask and want coverage to be run through the blend function. michael@0: drawState->enableState(GrDrawState::kCoverageDrawing_StateBit); michael@0: michael@0: // The scratch texture that we are drawing into can be substantially larger than the mask. Only michael@0: // clear the part that we care about. michael@0: fGpu->clear(&maskSpaceIBounds, michael@0: kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000, michael@0: true, michael@0: result->asRenderTarget()); michael@0: michael@0: // When we use the stencil in the below loop it is important to have this clip installed. michael@0: // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first michael@0: // pass must not set values outside of this bounds or stencil values outside the rect won't be michael@0: // cleared. michael@0: GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds); michael@0: drawState->enableState(GrDrawState::kClip_StateBit); michael@0: michael@0: GrAutoScratchTexture temp; michael@0: // walk through each clip element and perform its set op michael@0: for (ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) { michael@0: const Element* element = iter.get(); michael@0: SkRegion::Op op = element->getOp(); michael@0: bool invert = element->isInverseFilled(); michael@0: michael@0: if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { michael@0: GrPathRenderer* pr = NULL; michael@0: bool useTemp = !this->canStencilAndDrawElement(result, element, &pr); michael@0: GrTexture* dst; michael@0: // This is the bounds of the clip element in the space of the alpha-mask. The temporary michael@0: // mask buffer can be substantially larger than the actually clip stack element. We michael@0: // touch the minimum number of pixels necessary and use decal mode to combine it with michael@0: // the accumulator. michael@0: SkIRect maskSpaceElementIBounds; michael@0: michael@0: if (useTemp) { michael@0: if (invert) { michael@0: maskSpaceElementIBounds = maskSpaceIBounds; michael@0: } else { michael@0: SkRect elementBounds = element->getBounds(); michael@0: elementBounds.offset(clipToMaskOffset); michael@0: elementBounds.roundOut(&maskSpaceElementIBounds); michael@0: } michael@0: michael@0: this->getTemp(maskSpaceIBounds.fRight, maskSpaceIBounds.fBottom, &temp); michael@0: if (NULL == temp.texture()) { michael@0: fAACache.reset(); michael@0: return NULL; michael@0: } michael@0: dst = temp.texture(); michael@0: // clear the temp target and set blend to replace michael@0: fGpu->clear(&maskSpaceElementIBounds, michael@0: invert ? 0xffffffff : 0x00000000, michael@0: true, michael@0: dst->asRenderTarget()); michael@0: setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op); michael@0: michael@0: } else { michael@0: // draw directly into the result with the stencil set to make the pixels affected michael@0: // by the clip shape be non-zero. michael@0: dst = result; michael@0: GR_STATIC_CONST_SAME_STENCIL(kStencilInElement, michael@0: kReplace_StencilOp, michael@0: kReplace_StencilOp, michael@0: kAlways_StencilFunc, michael@0: 0xffff, michael@0: 0xffff, michael@0: 0xffff); michael@0: drawState->setStencil(kStencilInElement); michael@0: setup_boolean_blendcoeffs(drawState, op); michael@0: } michael@0: michael@0: drawState->setAlpha(invert ? 0x00 : 0xff); michael@0: michael@0: if (!this->drawElement(dst, element, pr)) { michael@0: fAACache.reset(); michael@0: return NULL; michael@0: } michael@0: michael@0: if (useTemp) { michael@0: // Now draw into the accumulator using the real operation and the temp buffer as a michael@0: // texture michael@0: this->mergeMask(result, michael@0: temp.texture(), michael@0: op, michael@0: maskSpaceIBounds, michael@0: maskSpaceElementIBounds); michael@0: } else { michael@0: // Draw to the exterior pixels (those with a zero stencil value). michael@0: drawState->setAlpha(invert ? 0xff : 0x00); michael@0: GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement, michael@0: kZero_StencilOp, michael@0: kZero_StencilOp, michael@0: kEqual_StencilFunc, michael@0: 0xffff, michael@0: 0x0000, michael@0: 0xffff); michael@0: drawState->setStencil(kDrawOutsideElement); michael@0: fGpu->drawSimpleRect(clipSpaceIBounds); michael@0: drawState->disableStencil(); michael@0: } michael@0: } else { michael@0: // all the remaining ops can just be directly draw into the accumulation buffer michael@0: drawState->setAlpha(0xff); michael@0: setup_boolean_blendcoeffs(drawState, op); michael@0: this->drawElement(result, element); michael@0: } michael@0: } michael@0: michael@0: fCurrClipMaskType = kAlpha_ClipMaskType; michael@0: return result; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device michael@0: // (as opposed to canvas) coordinates michael@0: bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID, michael@0: InitialState initialState, michael@0: const ElementList& elements, michael@0: const SkIRect& clipSpaceIBounds, michael@0: const SkIPoint& clipSpaceToStencilOffset) { michael@0: michael@0: SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); michael@0: michael@0: GrDrawState* drawState = fGpu->drawState(); michael@0: SkASSERT(drawState->isClipState()); michael@0: michael@0: GrRenderTarget* rt = drawState->getRenderTarget(); michael@0: SkASSERT(NULL != rt); michael@0: michael@0: // TODO: dynamically attach a SB when needed. michael@0: GrStencilBuffer* stencilBuffer = rt->getStencilBuffer(); michael@0: if (NULL == stencilBuffer) { michael@0: return false; michael@0: } michael@0: michael@0: if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) { michael@0: michael@0: stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset); michael@0: michael@0: // Set the matrix so that rendered clip elements are transformed from clip to stencil space. michael@0: SkVector translate = { michael@0: SkIntToScalar(clipSpaceToStencilOffset.fX), michael@0: SkIntToScalar(clipSpaceToStencilOffset.fY) michael@0: }; michael@0: SkMatrix matrix; michael@0: matrix.setTranslate(translate); michael@0: GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix); michael@0: drawState = fGpu->drawState(); michael@0: michael@0: drawState->setRenderTarget(rt); michael@0: michael@0: // We set the current clip to the bounds so that our recursive draws are scissored to them. michael@0: SkIRect stencilSpaceIBounds(clipSpaceIBounds); michael@0: stencilSpaceIBounds.offset(clipSpaceToStencilOffset); michael@0: GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds); michael@0: drawState->enableState(GrDrawState::kClip_StateBit); michael@0: michael@0: #if !VISUALIZE_COMPLEX_CLIP michael@0: drawState->enableState(GrDrawState::kNoColorWrites_StateBit); michael@0: #endif michael@0: michael@0: int clipBit = stencilBuffer->bits(); michael@0: SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers"); michael@0: clipBit = (1 << (clipBit-1)); michael@0: michael@0: fGpu->clearStencilClip(stencilSpaceIBounds, kAllIn_InitialState == initialState); michael@0: michael@0: // walk through each clip element and perform its set op michael@0: // with the existing clip. michael@0: for (ElementList::Iter iter(elements.headIter()); NULL != iter.get(); iter.next()) { michael@0: const Element* element = iter.get(); michael@0: bool fillInverted = false; michael@0: // enabled at bottom of loop michael@0: drawState->disableState(GrGpu::kModifyStencilClip_StateBit); michael@0: // if the target is MSAA then we want MSAA enabled when the clip is soft michael@0: if (rt->isMultisampled()) { michael@0: drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA()); michael@0: } michael@0: michael@0: // This will be used to determine whether the clip shape can be rendered into the michael@0: // stencil with arbitrary stencil settings. michael@0: GrPathRenderer::StencilSupport stencilSupport; michael@0: michael@0: SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); michael@0: michael@0: SkRegion::Op op = element->getOp(); michael@0: michael@0: GrPathRenderer* pr = NULL; michael@0: SkPath clipPath; michael@0: if (Element::kRect_Type == element->getType()) { michael@0: stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; michael@0: fillInverted = false; michael@0: } else { michael@0: element->asPath(&clipPath); michael@0: fillInverted = clipPath.isInverseFillType(); michael@0: if (fillInverted) { michael@0: clipPath.toggleInverseFillType(); michael@0: } michael@0: pr = this->getContext()->getPathRenderer(clipPath, michael@0: stroke, michael@0: fGpu, michael@0: false, michael@0: GrPathRendererChain::kStencilOnly_DrawType, michael@0: &stencilSupport); michael@0: if (NULL == pr) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: int passes; michael@0: GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; michael@0: michael@0: bool canRenderDirectToStencil = michael@0: GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; michael@0: bool canDrawDirectToClip; // Given the renderer, the element, michael@0: // fill rule, and set operation can michael@0: // we render the element directly to michael@0: // stencil bit used for clipping. michael@0: canDrawDirectToClip = GrStencilSettings::GetClipPasses(op, michael@0: canRenderDirectToStencil, michael@0: clipBit, michael@0: fillInverted, michael@0: &passes, michael@0: stencilSettings); michael@0: michael@0: // draw the element to the client stencil bits if necessary michael@0: if (!canDrawDirectToClip) { michael@0: GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil, michael@0: kIncClamp_StencilOp, michael@0: kIncClamp_StencilOp, michael@0: kAlways_StencilFunc, michael@0: 0xffff, michael@0: 0x0000, michael@0: 0xffff); michael@0: SET_RANDOM_COLOR michael@0: if (Element::kRect_Type == element->getType()) { michael@0: *drawState->stencil() = gDrawToStencil; michael@0: fGpu->drawSimpleRect(element->getRect(), NULL); michael@0: } else { michael@0: if (!clipPath.isEmpty()) { michael@0: if (canRenderDirectToStencil) { michael@0: *drawState->stencil() = gDrawToStencil; michael@0: pr->drawPath(clipPath, stroke, fGpu, false); michael@0: } else { michael@0: pr->stencilPath(clipPath, stroke, fGpu); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // now we modify the clip bit by rendering either the clip michael@0: // element directly or a bounding rect of the entire clip. michael@0: drawState->enableState(GrGpu::kModifyStencilClip_StateBit); michael@0: for (int p = 0; p < passes; ++p) { michael@0: *drawState->stencil() = stencilSettings[p]; michael@0: if (canDrawDirectToClip) { michael@0: if (Element::kRect_Type == element->getType()) { michael@0: SET_RANDOM_COLOR michael@0: fGpu->drawSimpleRect(element->getRect(), NULL); michael@0: } else { michael@0: SET_RANDOM_COLOR michael@0: pr->drawPath(clipPath, stroke, fGpu, false); michael@0: } michael@0: } else { michael@0: SET_RANDOM_COLOR michael@0: // The view matrix is setup to do clip space -> stencil space translation, so michael@0: // draw rect in clip space. michael@0: fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds), NULL); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: // set this last because recursive draws may overwrite it back to kNone. michael@0: SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); michael@0: fCurrClipMaskType = kStencil_ClipMaskType; michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // mapping of clip-respecting stencil funcs to normal stencil funcs michael@0: // mapping depends on whether stencil-clipping is in effect. michael@0: static const GrStencilFunc michael@0: gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = { michael@0: {// Stencil-Clipping is DISABLED, we are effectively always inside the clip michael@0: // In the Clip Funcs michael@0: kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc michael@0: kEqual_StencilFunc, // kEqualIfInClip_StencilFunc michael@0: kLess_StencilFunc, // kLessIfInClip_StencilFunc michael@0: kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc michael@0: // Special in the clip func that forces user's ref to be 0. michael@0: kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc michael@0: // make ref 0 and do normal nequal. michael@0: }, michael@0: {// Stencil-Clipping is ENABLED michael@0: // In the Clip Funcs michael@0: kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc michael@0: // eq stencil clip bit, mask michael@0: // out user bits. michael@0: michael@0: kEqual_StencilFunc, // kEqualIfInClip_StencilFunc michael@0: // add stencil bit to mask and ref michael@0: michael@0: kLess_StencilFunc, // kLessIfInClip_StencilFunc michael@0: kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc michael@0: // for both of these we can add michael@0: // the clip bit to the mask and michael@0: // ref and compare as normal michael@0: // Special in the clip func that forces user's ref to be 0. michael@0: kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc michael@0: // make ref have only the clip bit set michael@0: // and make comparison be less michael@0: // 10..0 < 1..user_bits.. michael@0: } michael@0: }; michael@0: michael@0: namespace { michael@0: // Sets the settings to clip against the stencil buffer clip while ignoring the michael@0: // client bits. michael@0: const GrStencilSettings& basic_apply_stencil_clip_settings() { michael@0: // stencil settings to use when clip is in stencil michael@0: GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings, michael@0: kKeep_StencilOp, michael@0: kKeep_StencilOp, michael@0: kAlwaysIfInClip_StencilFunc, michael@0: 0x0000, michael@0: 0x0000, michael@0: 0x0000); michael@0: return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); michael@0: } michael@0: } michael@0: michael@0: void GrClipMaskManager::setGpuStencil() { michael@0: // We make two copies of the StencilSettings here (except in the early michael@0: // exit scenario. One copy from draw state to the stack var. Then another michael@0: // from the stack var to the gpu. We could make this class hold a ptr to michael@0: // GrGpu's fStencilSettings and eliminate the stack copy here. michael@0: michael@0: const GrDrawState& drawState = fGpu->getDrawState(); michael@0: michael@0: // use stencil for clipping if clipping is enabled and the clip michael@0: // has been written into the stencil. michael@0: GrClipMaskManager::StencilClipMode clipMode; michael@0: if (this->isClipInStencil() && drawState.isClipState()) { michael@0: clipMode = GrClipMaskManager::kRespectClip_StencilClipMode; michael@0: // We can't be modifying the clip and respecting it at the same time. michael@0: SkASSERT(!drawState.isStateFlagEnabled( michael@0: GrGpu::kModifyStencilClip_StateBit)); michael@0: } else if (drawState.isStateFlagEnabled( michael@0: GrGpu::kModifyStencilClip_StateBit)) { michael@0: clipMode = GrClipMaskManager::kModifyClip_StencilClipMode; michael@0: } else { michael@0: clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode; michael@0: } michael@0: michael@0: GrStencilSettings settings; michael@0: // The GrGpu client may not be using the stencil buffer but we may need to michael@0: // enable it in order to respect a stencil clip. michael@0: if (drawState.getStencil().isDisabled()) { michael@0: if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) { michael@0: settings = basic_apply_stencil_clip_settings(); michael@0: } else { michael@0: fGpu->disableStencil(); michael@0: return; michael@0: } michael@0: } else { michael@0: settings = drawState.getStencil(); michael@0: } michael@0: michael@0: // TODO: dynamically attach a stencil buffer michael@0: int stencilBits = 0; michael@0: GrStencilBuffer* stencilBuffer = michael@0: drawState.getRenderTarget()->getStencilBuffer(); michael@0: if (NULL != stencilBuffer) { michael@0: stencilBits = stencilBuffer->bits(); michael@0: } michael@0: michael@0: SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp()); michael@0: SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided()); michael@0: this->adjustStencilParams(&settings, clipMode, stencilBits); michael@0: fGpu->setStencilSettings(settings); michael@0: } michael@0: michael@0: void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings, michael@0: StencilClipMode mode, michael@0: int stencilBitCnt) { michael@0: SkASSERT(stencilBitCnt > 0); michael@0: michael@0: if (kModifyClip_StencilClipMode == mode) { michael@0: // We assume that this clip manager itself is drawing to the GrGpu and michael@0: // has already setup the correct values. michael@0: return; michael@0: } michael@0: michael@0: unsigned int clipBit = (1 << (stencilBitCnt - 1)); michael@0: unsigned int userBits = clipBit - 1; michael@0: michael@0: GrStencilSettings::Face face = GrStencilSettings::kFront_Face; michael@0: bool twoSided = fGpu->caps()->twoSidedStencilSupport(); michael@0: michael@0: bool finished = false; michael@0: while (!finished) { michael@0: GrStencilFunc func = settings->func(face); michael@0: uint16_t writeMask = settings->writeMask(face); michael@0: uint16_t funcMask = settings->funcMask(face); michael@0: uint16_t funcRef = settings->funcRef(face); michael@0: michael@0: SkASSERT((unsigned) func < kStencilFuncCount); michael@0: michael@0: writeMask &= userBits; michael@0: michael@0: if (func >= kBasicStencilFuncCount) { michael@0: int respectClip = kRespectClip_StencilClipMode == mode; michael@0: if (respectClip) { michael@0: // The GrGpu class should have checked this michael@0: SkASSERT(this->isClipInStencil()); michael@0: switch (func) { michael@0: case kAlwaysIfInClip_StencilFunc: michael@0: funcMask = clipBit; michael@0: funcRef = clipBit; michael@0: break; michael@0: case kEqualIfInClip_StencilFunc: michael@0: case kLessIfInClip_StencilFunc: michael@0: case kLEqualIfInClip_StencilFunc: michael@0: funcMask = (funcMask & userBits) | clipBit; michael@0: funcRef = (funcRef & userBits) | clipBit; michael@0: break; michael@0: case kNonZeroIfInClip_StencilFunc: michael@0: funcMask = (funcMask & userBits) | clipBit; michael@0: funcRef = clipBit; michael@0: break; michael@0: default: michael@0: GrCrash("Unknown stencil func"); michael@0: } michael@0: } else { michael@0: funcMask &= userBits; michael@0: funcRef &= userBits; michael@0: } michael@0: const GrStencilFunc* table = michael@0: gSpecialToBasicStencilFunc[respectClip]; michael@0: func = table[func - kBasicStencilFuncCount]; michael@0: SkASSERT(func >= 0 && func < kBasicStencilFuncCount); michael@0: } else { michael@0: funcMask &= userBits; michael@0: funcRef &= userBits; michael@0: } michael@0: michael@0: settings->setFunc(face, func); michael@0: settings->setWriteMask(face, writeMask); michael@0: settings->setFuncMask(face, funcMask); michael@0: settings->setFuncRef(face, funcRef); michael@0: michael@0: if (GrStencilSettings::kFront_Face == face) { michael@0: face = GrStencilSettings::kBack_Face; michael@0: finished = !twoSided; michael@0: } else { michael@0: finished = true; michael@0: } michael@0: } michael@0: if (!twoSided) { michael@0: settings->copyFrontSettingsToBack(); michael@0: } michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID, michael@0: GrReducedClip::InitialState initialState, michael@0: const GrReducedClip::ElementList& elements, michael@0: const SkIRect& clipSpaceIBounds) { michael@0: SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); michael@0: michael@0: GrTexture* result; michael@0: if (this->getMaskTexture(elementsGenID, clipSpaceIBounds, &result, true)) { michael@0: return result; michael@0: } michael@0: michael@0: if (NULL == result) { michael@0: fAACache.reset(); michael@0: return NULL; michael@0: } michael@0: michael@0: // The mask texture may be larger than necessary. We round out the clip space bounds and pin michael@0: // the top left corner of the resulting rect to the top left of the texture. michael@0: SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); michael@0: michael@0: GrSWMaskHelper helper(this->getContext()); michael@0: michael@0: SkMatrix matrix; michael@0: matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft), michael@0: SkIntToScalar(-clipSpaceIBounds.fTop)); michael@0: helper.init(maskSpaceIBounds, &matrix); michael@0: michael@0: helper.clear(kAllIn_InitialState == initialState ? 0xFF : 0x00); michael@0: michael@0: SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); michael@0: michael@0: for (ElementList::Iter iter(elements.headIter()) ; NULL != iter.get(); iter.next()) { michael@0: michael@0: const Element* element = iter.get(); michael@0: SkRegion::Op op = element->getOp(); michael@0: michael@0: if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { michael@0: // Intersect and reverse difference require modifying pixels outside of the geometry michael@0: // that is being "drawn". In both cases we erase all the pixels outside of the geometry michael@0: // but leave the pixels inside the geometry alone. For reverse difference we invert all michael@0: // the pixels before clearing the ones outside the geometry. michael@0: if (SkRegion::kReverseDifference_Op == op) { michael@0: SkRect temp = SkRect::Make(clipSpaceIBounds); michael@0: // invert the entire scene michael@0: helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF); michael@0: } michael@0: michael@0: SkPath clipPath; michael@0: element->asPath(&clipPath); michael@0: clipPath.toggleInverseFillType(); michael@0: helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00); michael@0: michael@0: continue; michael@0: } michael@0: michael@0: // The other ops (union, xor, diff) only affect pixels inside michael@0: // the geometry so they can just be drawn normally michael@0: if (Element::kRect_Type == element->getType()) { michael@0: helper.draw(element->getRect(), op, element->isAA(), 0xFF); michael@0: } else { michael@0: SkPath path; michael@0: element->asPath(&path); michael@0: helper.draw(path, stroke, op, element->isAA(), 0xFF); michael@0: } michael@0: } michael@0: michael@0: helper.toTexture(result); michael@0: michael@0: fCurrClipMaskType = kAlpha_ClipMaskType; michael@0: return result; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: void GrClipMaskManager::releaseResources() { michael@0: fAACache.releaseResources(); michael@0: } michael@0: michael@0: void GrClipMaskManager::setGpu(GrGpu* gpu) { michael@0: fGpu = gpu; michael@0: fAACache.setContext(gpu->getContext()); michael@0: } michael@0: michael@0: void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) { michael@0: const GrDrawState& drawState = fGpu->getDrawState(); michael@0: GrClipMaskManager::StencilClipMode clipMode; michael@0: if (this->isClipInStencil() && drawState.isClipState()) { michael@0: clipMode = GrClipMaskManager::kRespectClip_StencilClipMode; michael@0: // We can't be modifying the clip and respecting it at the same time. michael@0: SkASSERT(!drawState.isStateFlagEnabled( michael@0: GrGpu::kModifyStencilClip_StateBit)); michael@0: } else if (drawState.isStateFlagEnabled( michael@0: GrGpu::kModifyStencilClip_StateBit)) { michael@0: clipMode = GrClipMaskManager::kModifyClip_StencilClipMode; michael@0: } else { michael@0: clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode; michael@0: } michael@0: michael@0: // TODO: dynamically attach a stencil buffer michael@0: int stencilBits = 0; michael@0: GrStencilBuffer* stencilBuffer = michael@0: drawState.getRenderTarget()->getStencilBuffer(); michael@0: if (NULL != stencilBuffer) { michael@0: stencilBits = stencilBuffer->bits(); michael@0: this->adjustStencilParams(settings, clipMode, stencilBits); michael@0: } michael@0: }