From 0650e811b537f21a3a9d09a953960626cf5cfce4 Mon Sep 17 00:00:00 2001 From: "bsalomon@google.com" Date: Fri, 8 Apr 2011 18:07:53 +0000 Subject: [PATCH] fix use of smooth lines Review URL:http://codereview.appspot.com/4370050/ git-svn-id: http://skia.googlecode.com/svn/trunk@1085 2bbb7eff-a529-9590-31e7-b0007b416f81 --- gpu/include/GrTypes.h | 10 ++++ gpu/src/GrGpuGL.cpp | 106 ++++++++++++++++++++++++++---------------- gpu/src/GrGpuGL.h | 5 ++ 3 files changed, 81 insertions(+), 40 deletions(-) diff --git a/gpu/include/GrTypes.h b/gpu/include/GrTypes.h index 2d8c116a36..3a55c84231 100644 --- a/gpu/include/GrTypes.h +++ b/gpu/include/GrTypes.h @@ -172,6 +172,16 @@ enum GrPrimitiveType { kLineStrip_PrimitiveType }; +static inline bool GrIsPrimTypeLines(GrPrimitiveType type) { + return kLines_PrimitiveType == type || kLineStrip_PrimitiveType == type; +} + +static inline bool GrIsPrimTypeTris(GrPrimitiveType type) { + return kTriangles_PrimitiveType == type || + kTriangleStrip_PrimitiveType == type || + kTriangleFan_PrimitiveType == type; +} + /** * Coeffecients for alpha-blending. */ diff --git a/gpu/src/GrGpuGL.cpp b/gpu/src/GrGpuGL.cpp index a5cac2ef69..cc9aacab4a 100644 --- a/gpu/src/GrGpuGL.cpp +++ b/gpu/src/GrGpuGL.cpp @@ -1526,6 +1526,18 @@ void GrGpuGL::flushStencil() { } } +bool GrGpuGL::useSmoothLines() { + // there is a conflict between using smooth lines and our use of + // premultiplied alpha. Smooth lines tweak the incoming alpha value + // but not in a premul-alpha way. So we only use them when our alpha + // is 0xff. + + // TODO: write a smarter line frag shader. + + return (kAntialias_StateBit & fCurrDrawState.fFlagBits) && + canDisableBlend(); +} + void GrGpuGL::flushAAState(GrPrimitiveType type) { if (GR_GL_SUPPORT_DESKTOP) { // ES doesn't support toggling GL_MULTISAMPLE and doesn't have @@ -1533,13 +1545,12 @@ void GrGpuGL::flushAAState(GrPrimitiveType type) { // we prefer smooth lines over multisampled lines // msaa should be disabled if drawing smooth lines. - if (kLines_PrimitiveType == type) { - if (!fHWAAState.fSmoothLineEnabled && - (kAntialias_StateBit & fCurrDrawState.fFlagBits)) { + if (GrIsPrimTypeLines(type)) { + bool smooth = useSmoothLines(); + if (!fHWAAState.fSmoothLineEnabled && smooth) { GR_GL(Enable(GR_GL_LINE_SMOOTH)); fHWAAState.fSmoothLineEnabled = true; - } else if (fHWAAState.fSmoothLineEnabled && - !(kAntialias_StateBit & fCurrDrawState.fFlagBits)) { + } else if (fHWAAState.fSmoothLineEnabled && !smooth) { GR_GL(Disable(GR_GL_LINE_SMOOTH)); fHWAAState.fSmoothLineEnabled = false; } @@ -1562,6 +1573,54 @@ void GrGpuGL::flushAAState(GrPrimitiveType type) { } } +void GrGpuGL::flushBlend(GrPrimitiveType type) { + if (GrIsPrimTypeLines(type) && useSmoothLines()) { + if (fHWBlendDisabled) { + GR_GL(Enable(GR_GL_BLEND)); + fHWBlendDisabled = false; + } + if (kSA_BlendCoeff != fHWDrawState.fSrcBlend || + kISA_BlendCoeff != fHWDrawState.fDstBlend) { + GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff], + gXfermodeCoeff2Blend[kISA_BlendCoeff])); + fHWDrawState.fSrcBlend = kSA_BlendCoeff; + fHWDrawState.fDstBlend = kISA_BlendCoeff; + } + } else { + bool blendOff = canDisableBlend(); + if (fHWBlendDisabled != blendOff) { + if (blendOff) { + GR_GL(Disable(GR_GL_BLEND)); + } else { + GR_GL(Enable(GR_GL_BLEND)); + } + fHWBlendDisabled = blendOff; + } + if (!blendOff) { + if (fHWDrawState.fSrcBlend != fCurrDrawState.fSrcBlend || + fHWDrawState.fDstBlend != fCurrDrawState.fDstBlend) { + GR_GL(BlendFunc(gXfermodeCoeff2Blend[fCurrDrawState.fSrcBlend], + gXfermodeCoeff2Blend[fCurrDrawState.fDstBlend])); + fHWDrawState.fSrcBlend = fCurrDrawState.fSrcBlend; + fHWDrawState.fDstBlend = fCurrDrawState.fDstBlend; + } + if ((BlendCoefReferencesConstant(fCurrDrawState.fSrcBlend) || + BlendCoefReferencesConstant(fCurrDrawState.fDstBlend)) && + fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) { + + float c[] = { + GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f, + GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f, + GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f, + GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f + }; + GR_GL(BlendColor(c[0], c[1], c[2], c[3])); + fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant; + } + } + } +} + bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) { // GrGpu::setupClipAndFlushState should have already checked this @@ -1638,9 +1697,9 @@ bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) { } flushRenderTarget(); - flushAAState(type); - + flushBlend(type); + if ((fCurrDrawState.fFlagBits & kDither_StateBit) != (fHWDrawState.fFlagBits & kDither_StateBit)) { if (fCurrDrawState.fFlagBits & kDither_StateBit) { @@ -1661,39 +1720,6 @@ bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) { GR_GL(ColorMask(mask, mask, mask, mask)); } - bool blendOff = canDisableBlend(); - if (fHWBlendDisabled != blendOff) { - if (blendOff) { - GR_GL(Disable(GR_GL_BLEND)); - } else { - GR_GL(Enable(GR_GL_BLEND)); - } - fHWBlendDisabled = blendOff; - } - - if (!blendOff) { - if (fHWDrawState.fSrcBlend != fCurrDrawState.fSrcBlend || - fHWDrawState.fDstBlend != fCurrDrawState.fDstBlend) { - GR_GL(BlendFunc(gXfermodeCoeff2Blend[fCurrDrawState.fSrcBlend], - gXfermodeCoeff2Blend[fCurrDrawState.fDstBlend])); - fHWDrawState.fSrcBlend = fCurrDrawState.fSrcBlend; - fHWDrawState.fDstBlend = fCurrDrawState.fDstBlend; - } - if ((BlendCoefReferencesConstant(fCurrDrawState.fSrcBlend) || - BlendCoefReferencesConstant(fCurrDrawState.fDstBlend)) && - fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) { - - float c[] = { - GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f, - GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f, - GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f, - GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f - }; - GR_GL(BlendColor(c[0], c[1], c[2], c[3])); - fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant; - } - } - if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) { switch (fCurrDrawState.fDrawFace) { case kCCW_DrawFace: diff --git a/gpu/src/GrGpuGL.h b/gpu/src/GrGpuGL.h index 6d600e0cf5..1b5f7f7ac7 100644 --- a/gpu/src/GrGpuGL.h +++ b/gpu/src/GrGpuGL.h @@ -151,15 +151,20 @@ private: void setSpareTextureUnit(); + bool useSmoothLines(); + void flushRenderTarget(); void flushStencil(); void flushAAState(GrPrimitiveType type); + void flushBlend(GrPrimitiveType type); + void resolveTextureRenderTarget(GrGLTexture* texture); bool canBeTexture(GrPixelConfig config, GrGLenum* internalFormat, GrGLenum* format, GrGLenum* type); + bool fboInternalFormat(GrPixelConfig config, GrGLenum* format); friend class GrGLVertexBuffer;