Add proper aliased support for SDF text

Previously, when asked to render aliased text with distance fields,
we would get the aliased glyph from the cache and then try to
anti-alias the edge. This change instead grabs the anti-aliased glyph,
then deliberately aliases the edge.

Bug: chromium:707979
Change-Id: I05766af17d7ae58bca27aaffd9e08e5c586e789c
Reviewed-on: https://skia-review.googlesource.com/21728
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
This commit is contained in:
Jim Van Verth 2017-07-06 16:36:55 -04:00 committed by Skia Commit-Bot
parent 24a2ecfa25
commit 90e89b366a
7 changed files with 64 additions and 35 deletions

View File

@ -77,6 +77,8 @@ public:
bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag); bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
bool isGammaCorrect = bool isGammaCorrect =
SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag); SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
bool isAliased =
SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision); varyingHandler->addVarying("TextureCoords", &uv, kHigh_GrSLPrecision);
vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName); vertBuilder->codeAppendf("%s = %s;", uv.vsOut(), dfTexEffect.inTextureCoords()->fName);
@ -158,10 +160,12 @@ public:
fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);"); fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
} }
// The smoothstep falloff compensates for the non-linear sRGB response curve. If we are if (isAliased) {
// doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance fragBuilder->codeAppend("float val = distance > 0 ? 1.0 : 0.0;");
// mapped linearly to coverage, so use a linear step: } else if (isGammaCorrect) {
if (isGammaCorrect) { // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
// doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
// distance mapped linearly to coverage, so use a linear step:
fragBuilder->codeAppend( fragBuilder->codeAppend(
"float val = clamp((distance + afwidth) / (2.0 * afwidth), 0.0, 1.0);"); "float val = clamp((distance + afwidth) / (2.0 * afwidth), 0.0, 1.0);");
} else { } else {

View File

@ -23,6 +23,7 @@ enum GrDistanceFieldEffectFlags {
kBGR_DistanceFieldEffectFlag = 0x08, // lcd display has bgr order kBGR_DistanceFieldEffectFlag = 0x08, // lcd display has bgr order
kPortrait_DistanceFieldEffectFlag = 0x10, // lcd display is in portrait mode (not used yet) kPortrait_DistanceFieldEffectFlag = 0x10, // lcd display is in portrait mode (not used yet)
kGammaCorrect_DistanceFieldEffectFlag = 0x20, // assume gamma-correct output (linear blending) kGammaCorrect_DistanceFieldEffectFlag = 0x20, // assume gamma-correct output (linear blending)
kAliased_DistanceFieldEffectFlag = 0x40, // monochrome output
kInvalid_DistanceFieldEffectFlag = 0x80, // invalid state (for initialization) kInvalid_DistanceFieldEffectFlag = 0x80, // invalid state (for initialization)
@ -31,7 +32,8 @@ enum GrDistanceFieldEffectFlags {
// The subset of the flags relevant to GrDistanceFieldA8TextGeoProc // The subset of the flags relevant to GrDistanceFieldA8TextGeoProc
kNonLCD_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag | kNonLCD_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag |
kScaleOnly_DistanceFieldEffectFlag | kScaleOnly_DistanceFieldEffectFlag |
kGammaCorrect_DistanceFieldEffectFlag, kGammaCorrect_DistanceFieldEffectFlag |
kAliased_DistanceFieldEffectFlag,
// The subset of the flags relevant to GrDistanceFieldLCDTextGeoProc // The subset of the flags relevant to GrDistanceFieldLCDTextGeoProc
kLCD_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag | kLCD_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag |
kScaleOnly_DistanceFieldEffectFlag | kScaleOnly_DistanceFieldEffectFlag |

View File

@ -47,12 +47,14 @@ void GrAtlasTextOp::getProcessorAnalysisInputs(GrProcessorAnalysisColor* color,
color->setToConstant(fColor); color->setToConstant(fColor);
} }
switch (fMaskType) { switch (fMaskType) {
case kGrayscaleDistanceField_MaskType:
case kGrayscaleCoverageMask_MaskType: case kGrayscaleCoverageMask_MaskType:
case kAliasedDistanceField_MaskType:
case kGrayscaleDistanceField_MaskType:
*coverage = GrProcessorAnalysisCoverage::kSingleChannel; *coverage = GrProcessorAnalysisCoverage::kSingleChannel;
break; break;
case kLCDCoverageMask_MaskType: case kLCDCoverageMask_MaskType:
case kLCDDistanceField_MaskType: case kLCDDistanceField_MaskType:
case kLCDBGRDistanceField_MaskType:
*coverage = GrProcessorAnalysisCoverage::kLCD; *coverage = GrProcessorAnalysisCoverage::kLCD;
break; break;
case kColorBitmapMask_MaskType: case kColorBitmapMask_MaskType:
@ -174,10 +176,6 @@ bool GrAtlasTextOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
if (fLuminanceColor != that->fLuminanceColor) { if (fLuminanceColor != that->fLuminanceColor) {
return false; return false;
} }
if (fUseBGR != that->fUseBGR) {
return false;
}
} }
fNumGlyphs += that->numGlyphs(); fNumGlyphs += that->numGlyphs();
@ -221,11 +219,12 @@ sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(const SkMatrix& viewM
uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
flags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0; flags |= viewMatrix.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
flags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0; flags |= fUseGammaCorrectDistanceTable ? kGammaCorrect_DistanceFieldEffectFlag : 0;
flags |= (kAliasedDistanceField_MaskType == fMaskType) ? kAliased_DistanceFieldEffectFlag : 0;
// see if we need to create a new effect // see if we need to create a new effect
if (isLCD) { if (isLCD) {
flags |= kUseLCD_DistanceFieldEffectFlag; flags |= kUseLCD_DistanceFieldEffectFlag;
flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0; flags |= (kLCDBGRDistanceField_MaskType == fMaskType) ? kBGR_DistanceFieldEffectFlag : 0;
float redCorrection = fDistanceAdjustTable->getAdjustment( float redCorrection = fDistanceAdjustTable->getAdjustment(
SkColorGetR(luminanceColor) >> kDistanceAdjustLumShift, SkColorGetR(luminanceColor) >> kDistanceAdjustLumShift,
@ -245,9 +244,12 @@ sk_sp<GrGeometryProcessor> GrAtlasTextOp::setupDfProcessor(const SkMatrix& viewM
this->usesLocalCoords()); this->usesLocalCoords());
} else { } else {
#ifdef SK_GAMMA_APPLY_TO_A8 #ifdef SK_GAMMA_APPLY_TO_A8
U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, luminanceColor); float correction = 0;
float correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift, if (kAliasedDistanceField_MaskType != fMaskType) {
fUseGammaCorrectDistanceTable); U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, luminanceColor);
correction = fDistanceAdjustTable->getAdjustment(lum >> kDistanceAdjustLumShift,
fUseGammaCorrectDistanceTable);
}
return GrDistanceFieldA8TextGeoProc::Make(color, return GrDistanceFieldA8TextGeoProc::Make(color,
viewMatrix, std::move(proxy), viewMatrix, std::move(proxy),
params, correction, flags, params, correction, flags,

View File

@ -57,22 +57,24 @@ public:
op->fGeoCount = 1; op->fGeoCount = 1;
op->fLuminanceColor = 0; op->fLuminanceColor = 0;
op->fFontCache = fontCache; op->fFontCache = fontCache;
op->fUseBGR = false;
return op; return op;
} }
static std::unique_ptr<GrAtlasTextOp> MakeDistanceField( static std::unique_ptr<GrAtlasTextOp> MakeDistanceField(
int glyphCount, GrAtlasGlyphCache* fontCache, int glyphCount, GrAtlasGlyphCache* fontCache,
const GrDistanceFieldAdjustTable* distanceAdjustTable, const GrDistanceFieldAdjustTable* distanceAdjustTable,
bool useGammaCorrectDistanceTable, SkColor luminanceColor, bool isLCD, bool useBGR) { bool useGammaCorrectDistanceTable, SkColor luminanceColor, bool isLCD, bool useBGR,
bool isAntiAliased) {
std::unique_ptr<GrAtlasTextOp> op(new GrAtlasTextOp); std::unique_ptr<GrAtlasTextOp> op(new GrAtlasTextOp);
op->fFontCache = fontCache; op->fFontCache = fontCache;
op->fMaskType = isLCD ? kLCDDistanceField_MaskType : kGrayscaleDistanceField_MaskType; op->fMaskType = !isAntiAliased ? kAliasedDistanceField_MaskType
: isLCD ? (useBGR ? kLCDBGRDistanceField_MaskType
: kLCDDistanceField_MaskType)
: kGrayscaleDistanceField_MaskType;
op->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable)); op->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
op->fUseGammaCorrectDistanceTable = useGammaCorrectDistanceTable; op->fUseGammaCorrectDistanceTable = useGammaCorrectDistanceTable;
op->fLuminanceColor = luminanceColor; op->fLuminanceColor = luminanceColor;
op->fUseBGR = useBGR;
op->fNumGlyphs = glyphCount; op->fNumGlyphs = glyphCount;
op->fGeoCount = 1; op->fGeoCount = 1;
return op; return op;
@ -122,20 +124,26 @@ private:
case kColorBitmapMask_MaskType: case kColorBitmapMask_MaskType:
return kARGB_GrMaskFormat; return kARGB_GrMaskFormat;
case kGrayscaleCoverageMask_MaskType: case kGrayscaleCoverageMask_MaskType:
case kAliasedDistanceField_MaskType:
case kGrayscaleDistanceField_MaskType: case kGrayscaleDistanceField_MaskType:
case kLCDDistanceField_MaskType: case kLCDDistanceField_MaskType:
case kLCDBGRDistanceField_MaskType:
return kA8_GrMaskFormat; return kA8_GrMaskFormat;
} }
return kA8_GrMaskFormat; // suppress warning return kA8_GrMaskFormat; // suppress warning
} }
bool usesDistanceFields() const { bool usesDistanceFields() const {
return kGrayscaleDistanceField_MaskType == fMaskType || return kAliasedDistanceField_MaskType == fMaskType ||
kLCDDistanceField_MaskType == fMaskType; kGrayscaleDistanceField_MaskType == fMaskType ||
kLCDDistanceField_MaskType == fMaskType ||
kLCDBGRDistanceField_MaskType == fMaskType;
} }
bool isLCD() const { bool isLCD() const {
return kLCDCoverageMask_MaskType == fMaskType || kLCDDistanceField_MaskType == fMaskType; return kLCDCoverageMask_MaskType == fMaskType ||
kLCDDistanceField_MaskType == fMaskType ||
kLCDBGRDistanceField_MaskType == fMaskType;
} }
inline void flush(GrLegacyMeshDrawOp::Target* target, FlushInfo* flushInfo) const; inline void flush(GrLegacyMeshDrawOp::Target* target, FlushInfo* flushInfo) const;
@ -164,10 +172,11 @@ private:
kGrayscaleCoverageMask_MaskType, kGrayscaleCoverageMask_MaskType,
kLCDCoverageMask_MaskType, kLCDCoverageMask_MaskType,
kColorBitmapMask_MaskType, kColorBitmapMask_MaskType,
kAliasedDistanceField_MaskType,
kGrayscaleDistanceField_MaskType, kGrayscaleDistanceField_MaskType,
kLCDDistanceField_MaskType, kLCDDistanceField_MaskType,
kLCDBGRDistanceField_MaskType,
} fMaskType; } fMaskType;
bool fUseBGR; // fold this into the enum?
GrAtlasGlyphCache* fFontCache; GrAtlasGlyphCache* fFontCache;

View File

@ -270,7 +270,7 @@ inline std::unique_ptr<GrLegacyMeshDrawOp> GrAtlasTextBlob::makeOp(
bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry()); bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
op = GrAtlasTextOp::MakeDistanceField(glyphCount, cache, distanceAdjustTable, op = GrAtlasTextOp::MakeDistanceField(glyphCount, cache, distanceAdjustTable,
useGammaCorrectDistanceTable, luminanceColor, useGammaCorrectDistanceTable, luminanceColor,
info.hasUseLCDText(), useBGR); info.hasUseLCDText(), useBGR, info.isAntiAliased());
} else { } else {
op = GrAtlasTextOp::MakeBitmap(format, glyphCount, cache); op = GrAtlasTextOp::MakeBitmap(format, glyphCount, cache);
} }

View File

@ -126,10 +126,11 @@ public:
} }
// sets the last subrun of runIndex to use distance field text // sets the last subrun of runIndex to use distance field text
void setSubRunHasDistanceFields(int runIndex, bool hasLCD) { void setSubRunHasDistanceFields(int runIndex, bool hasLCD, bool isAntiAlias) {
Run& run = fRuns[runIndex]; Run& run = fRuns[runIndex];
Run::SubRunInfo& subRun = run.fSubRunInfo.back(); Run::SubRunInfo& subRun = run.fSubRunInfo.back();
subRun.setUseLCDText(hasLCD); subRun.setUseLCDText(hasLCD);
subRun.setAntiAliased(isAntiAlias);
subRun.setDrawAsDistanceFields(); subRun.setDrawAsDistanceFields();
} }
@ -358,8 +359,7 @@ private:
, fGlyphEndIndex(0) , fGlyphEndIndex(0)
, fColor(GrColor_ILLEGAL) , fColor(GrColor_ILLEGAL)
, fMaskFormat(kA8_GrMaskFormat) , fMaskFormat(kA8_GrMaskFormat)
, fDrawAsDistanceFields(false) , fFlags(0) {
, fUseLCDText(false) {
fVertexBounds.setLargestInverted(); fVertexBounds.setLargestInverted();
} }
SubRunInfo(const SubRunInfo& that) SubRunInfo(const SubRunInfo& that)
@ -376,8 +376,7 @@ private:
, fY(that.fY) , fY(that.fY)
, fColor(that.fColor) , fColor(that.fColor)
, fMaskFormat(that.fMaskFormat) , fMaskFormat(that.fMaskFormat)
, fDrawAsDistanceFields(that.fDrawAsDistanceFields) , fFlags(that.fFlags) {
, fUseLCDText(that.fUseLCDText) {
} }
// TODO when this object is more internal, drop the privacy // TODO when this object is more internal, drop the privacy
@ -432,12 +431,24 @@ private:
SkScalar*transX, SkScalar* transY); SkScalar*transX, SkScalar* transY);
// df properties // df properties
void setUseLCDText(bool useLCDText) { fUseLCDText = useLCDText; } void setDrawAsDistanceFields() { fFlags |= kDrawAsSDF_Flag; }
bool hasUseLCDText() const { return fUseLCDText; } bool drawAsDistanceFields() const { return SkToBool(fFlags & kDrawAsSDF_Flag); }
void setDrawAsDistanceFields() { fDrawAsDistanceFields = true; } void setUseLCDText(bool useLCDText) {
bool drawAsDistanceFields() const { return fDrawAsDistanceFields; } fFlags = useLCDText ? fFlags | kUseLCDText_Flag : fFlags & ~kUseLCDText_Flag;
}
bool hasUseLCDText() const { return SkToBool(fFlags & kUseLCDText_Flag); }
void setAntiAliased(bool antiAliased) {
fFlags = antiAliased ? fFlags | kAntiAliased_Flag : fFlags & ~kAntiAliased_Flag;
}
bool isAntiAliased() const { return SkToBool(fFlags & kAntiAliased_Flag); }
private: private:
enum Flag {
kDrawAsSDF_Flag = 0x1,
kUseLCDText_Flag = 0x2,
kAntiAliased_Flag = 0x4
};
GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken; GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
sk_sp<GrAtlasTextStrike> fStrike; sk_sp<GrAtlasTextStrike> fStrike;
SkMatrix fCurrentViewMatrix; SkMatrix fCurrentViewMatrix;
@ -451,8 +462,7 @@ private:
SkScalar fY; SkScalar fY;
GrColor fColor; GrColor fColor;
GrMaskFormat fMaskFormat; GrMaskFormat fMaskFormat;
bool fDrawAsDistanceFields; // df property uint32_t fFlags;
bool fUseLCDText; // df property
}; };
SubRunInfo& push_back() { SubRunInfo& push_back() {

View File

@ -283,6 +283,7 @@ void GrTextUtils::InitDistanceFieldPaint(GrAtlasTextBlob* blob,
SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil); SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize, dfMaskScaleCeil / scaledTextSize); blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize, dfMaskScaleCeil / scaledTextSize);
skPaint->setAntiAlias(true);
skPaint->setLCDRenderText(false); skPaint->setLCDRenderText(false);
skPaint->setAutohinted(false); skPaint->setAutohinted(false);
skPaint->setHinting(SkPaint::kNormal_Hinting); skPaint->setHinting(SkPaint::kNormal_Hinting);
@ -386,7 +387,8 @@ void GrTextUtils::DrawDFPosText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyp
SkPaint dfPaint(paint); SkPaint dfPaint(paint);
GrTextUtils::InitDistanceFieldPaint(blob, &dfPaint, &textRatio, viewMatrix); GrTextUtils::InitDistanceFieldPaint(blob, &dfPaint, &textRatio, viewMatrix);
blob->setHasDistanceField(); blob->setHasDistanceField();
blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText()); blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
paint.skPaint().isAntiAlias());
GrAtlasTextStrike* currStrike = nullptr; GrAtlasTextStrike* currStrike = nullptr;