Add flag for SkGpuSurface creation to enable distance fields.
BUG=skia:2173
R=bsalomon@google.com, reed@google.com, bungeman@google.com
Author: jvanverth@google.com
Review URL: https://codereview.chromium.org/261783004
git-svn-id: http://skia.googlecode.com/svn/trunk@14525 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkSurface.h b/include/core/SkSurface.h
index 542c9a0..d049d8c 100644
--- a/include/core/SkSurface.h
+++ b/include/core/SkSurface.h
@@ -56,15 +56,31 @@
}
/**
+ * 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 @@
* 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 7f564de..8042ed3 100644
--- a/include/gpu/SkGpuDevice.h
+++ b/include/gpu/SkGpuDevice.h
@@ -32,6 +32,7 @@
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 238bcca..512420e 100755
--- a/src/gpu/GrDistanceFieldTextContext.cpp
+++ b/src/gpu/GrDistanceFieldTextContext.cpp
@@ -33,15 +33,15 @@
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 @@
}
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 58c0824..3dfffd1 100644
--- a/src/gpu/GrDistanceFieldTextContext.h
+++ b/src/gpu/GrDistanceFieldTextContext.h
@@ -17,7 +17,7 @@
*/
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 @@
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 60b4e7c..714a6da 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -190,7 +190,9 @@
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 6f018bf..a34b774 100644
--- a/src/image/SkSurface_Gpu.cpp
+++ b/src/image/SkSurface_Gpu.cpp
@@ -14,7 +14,7 @@
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 @@
///////////////////////////////////////////////////////////////////////////////
-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 @@
///////////////////////////////////////////////////////////////////////////////
-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 @@
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 @@
return NULL;
}
- return SkNEW_ARGS(SkSurface_Gpu, (tex->asRenderTarget(), true));
+ return SkNEW_ARGS(SkSurface_Gpu, (tex->asRenderTarget(), true, trm));
}