diff --git a/include/core/SkSurface.h b/include/core/SkSurface.h index 542c9a08f..d049d8c04 100644 --- a/include/core/SkSurface.h +++ b/include/core/SkSurface.h @@ -55,16 +55,32 @@ public: return NewRaster(SkImageInfo::MakeN32Premul(width, height)); } + /** + * Text rendering modes that can be passed to NewRenderTarget* + */ + enum TextRenderMode { + /** + * This will use the standard text rendering method + */ + kStandard_TextRenderMode, + /** + * This will use signed distance fields for text rendering when possible + */ + kDistanceField_TextRenderMode, + }; + /** * Return a new surface using the specified render target. */ - static SkSurface* NewRenderTargetDirect(GrRenderTarget*); + static SkSurface* NewRenderTargetDirect(GrRenderTarget*, + TextRenderMode trm = kStandard_TextRenderMode); /** * Return a new surface whose contents will be drawn to an offscreen * render target, allocated by the surface. */ - static SkSurface* NewRenderTarget(GrContext*, const SkImageInfo&, int sampleCount = 0); + static SkSurface* NewRenderTarget(GrContext*, const SkImageInfo&, int sampleCount = 0, + TextRenderMode trm = kStandard_TextRenderMode); /** * Return a new surface whose contents will be drawn to an offscreen @@ -78,7 +94,8 @@ public: * Note: Scratch textures count against the GrContext's cached resource * budget. */ - static SkSurface* NewScratchRenderTarget(GrContext*, const SkImageInfo&, int sampleCount = 0); + static SkSurface* NewScratchRenderTarget(GrContext*, const SkImageInfo&, int sampleCount = 0, + TextRenderMode trm = kStandard_TextRenderMode); int width() const { return fWidth; } int height() const { return fHeight; } diff --git a/include/gpu/SkGpuDevice.h b/include/gpu/SkGpuDevice.h index 7f564dedd..8042ed30f 100644 --- a/include/gpu/SkGpuDevice.h +++ b/include/gpu/SkGpuDevice.h @@ -32,6 +32,7 @@ public: enum Flags { kNeedClear_Flag = 1 << 0, //!< Surface requires an initial clear kCached_Flag = 1 << 1, //!< Surface is cached and needs to be unlocked when released + kDFFonts_Flag = 1 << 2, //!< Surface should render fonts using signed distance fields }; /** diff --git a/src/gpu/GrDistanceFieldTextContext.cpp b/src/gpu/GrDistanceFieldTextContext.cpp index 238bcca46..512420eb6 100755 --- a/src/gpu/GrDistanceFieldTextContext.cpp +++ b/src/gpu/GrDistanceFieldTextContext.cpp @@ -33,15 +33,15 @@ static const int kLargeDFFontSize = 128; SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, "Dump the contents of the font cache before every purge."); -#if SK_FORCE_DISTANCEFIELD_FONTS -static const bool kForceDistanceFieldFonts = true; -#else -static const bool kForceDistanceFieldFonts = false; -#endif - GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context, - const SkDeviceProperties& properties) + const SkDeviceProperties& properties, + bool enable) : GrTextContext(context, properties) { +#if SK_FORCE_DISTANCEFIELD_FONTS + fEnableDFRendering = true; +#else + fEnableDFRendering = enable; +#endif fStrike = NULL; fCurrTexture = NULL; @@ -56,7 +56,7 @@ GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { } bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) { - if (!kForceDistanceFieldFonts && !paint.isDistanceFieldTextTEMP()) { + if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) { return false; } diff --git a/src/gpu/GrDistanceFieldTextContext.h b/src/gpu/GrDistanceFieldTextContext.h index 58c0824e7..3dfffd1c1 100644 --- a/src/gpu/GrDistanceFieldTextContext.h +++ b/src/gpu/GrDistanceFieldTextContext.h @@ -17,7 +17,7 @@ class GrTextStrike; */ class GrDistanceFieldTextContext : public GrTextContext { public: - GrDistanceFieldTextContext(GrContext*, const SkDeviceProperties&); + GrDistanceFieldTextContext(GrContext*, const SkDeviceProperties&, bool enable); virtual ~GrDistanceFieldTextContext(); virtual void drawText(const GrPaint&, const SkPaint&, const char text[], size_t byteLength, @@ -33,6 +33,7 @@ private: GrTextStrike* fStrike; SkScalar fTextRatio; bool fUseLCDText; + bool fEnableDFRendering; void init(const GrPaint&, const SkPaint&); void drawPackedGlyph(GrGlyph::PackedID, SkFixed left, SkFixed top, GrFontScaler*); diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp index 60b4e7c40..714a6da65 100644 --- a/src/gpu/SkGpuDevice.cpp +++ b/src/gpu/SkGpuDevice.cpp @@ -190,7 +190,9 @@ void SkGpuDevice::initFromRenderTarget(GrContext* context, fContext = context; fContext->ref(); - fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties)); + bool useDFFonts = !!(flags & kDFFonts_Flag); + fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties, + useDFFonts)); fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties)); fRenderTarget = NULL; diff --git a/src/image/SkSurface_Gpu.cpp b/src/image/SkSurface_Gpu.cpp index 6f018bf25..a34b77439 100644 --- a/src/image/SkSurface_Gpu.cpp +++ b/src/image/SkSurface_Gpu.cpp @@ -14,7 +14,7 @@ class SkSurface_Gpu : public SkSurface_Base { public: SK_DECLARE_INST_COUNT(SkSurface_Gpu) - SkSurface_Gpu(GrRenderTarget*, bool cached); + SkSurface_Gpu(GrRenderTarget*, bool cached, TextRenderMode trm); virtual ~SkSurface_Gpu(); virtual SkCanvas* onNewCanvas() SK_OVERRIDE; @@ -33,9 +33,12 @@ private: /////////////////////////////////////////////////////////////////////////////// -SkSurface_Gpu::SkSurface_Gpu(GrRenderTarget* renderTarget, bool cached) +SkSurface_Gpu::SkSurface_Gpu(GrRenderTarget* renderTarget, bool cached, TextRenderMode trm) : INHERITED(renderTarget->width(), renderTarget->height()) { - fDevice = SkGpuDevice::Create(renderTarget, cached ? SkGpuDevice::kCached_Flag : 0); + int flags = 0; + flags |= cached ? SkGpuDevice::kCached_Flag : 0; + flags |= (kDistanceField_TextRenderMode == trm) ? SkGpuDevice::kDFFonts_Flag : 0; + fDevice = SkGpuDevice::Create(renderTarget, flags); if (kRGB_565_GrPixelConfig != renderTarget->config()) { fDevice->clear(0x0); @@ -98,14 +101,15 @@ void SkSurface_Gpu::onDiscard() { /////////////////////////////////////////////////////////////////////////////// -SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target) { +SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget* target, TextRenderMode trm) { if (NULL == target) { return NULL; } - return SkNEW_ARGS(SkSurface_Gpu, (target, false)); + return SkNEW_ARGS(SkSurface_Gpu, (target, false, trm)); } -SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, const SkImageInfo& info, int sampleCount) { +SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, const SkImageInfo& info, int sampleCount, + TextRenderMode trm) { if (NULL == ctx) { return NULL; } @@ -122,10 +126,11 @@ SkSurface* SkSurface::NewRenderTarget(GrContext* ctx, const SkImageInfo& info, i return NULL; } - return SkNEW_ARGS(SkSurface_Gpu, (tex->asRenderTarget(), false)); + return SkNEW_ARGS(SkSurface_Gpu, (tex->asRenderTarget(), false, trm)); } -SkSurface* SkSurface::NewScratchRenderTarget(GrContext* ctx, const SkImageInfo& info, int sampleCount) { +SkSurface* SkSurface::NewScratchRenderTarget(GrContext* ctx, const SkImageInfo& info, + int sampleCount, TextRenderMode trm) { if (NULL == ctx) { return NULL; } @@ -143,5 +148,5 @@ SkSurface* SkSurface::NewScratchRenderTarget(GrContext* ctx, const SkImageInfo& return NULL; } - return SkNEW_ARGS(SkSurface_Gpu, (tex->asRenderTarget(), true)); + return SkNEW_ARGS(SkSurface_Gpu, (tex->asRenderTarget(), true, trm)); }