From b24b4fa12be03807a0c3c710ec5ad102f3a8e145 Mon Sep 17 00:00:00 2001 From: "bungeman@google.com" Date: Tue, 4 Sep 2012 13:49:59 +0000 Subject: [PATCH] Future proof luminance caches. https://codereview.appspot.com/6495071/ git-svn-id: http://skia.googlecode.com/svn/trunk@5382 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkPaint.h | 3 +++ src/core/SkGraphics.cpp | 1 + src/core/SkPaint.cpp | 47 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/include/core/SkPaint.h b/include/core/SkPaint.h index 096344f..50fcdd8 100644 --- a/include/core/SkPaint.h +++ b/include/core/SkPaint.h @@ -951,12 +951,15 @@ private: void (*proc)(const SkDescriptor*, void*), void* context, bool ignoreGamma = false) const; + static void Term(); + enum { kCanonicalTextSizeForPaths = 64 }; friend class SkAutoGlyphCache; friend class SkCanvas; friend class SkDraw; + friend class SkGraphics; // So Term() can be called. friend class SkPDFDevice; friend class SkTextToPathIter; diff --git a/src/core/SkGraphics.cpp b/src/core/SkGraphics.cpp index d71a70e..64f9264 100644 --- a/src/core/SkGraphics.cpp +++ b/src/core/SkGraphics.cpp @@ -122,6 +122,7 @@ void SkGraphics::Init() { void SkGraphics::Term() { PurgeFontCache(); + SkPaint::Term(); } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp index 6ea03b4..4b23a5d 100644 --- a/src/core/SkPaint.cpp +++ b/src/core/SkPaint.cpp @@ -1621,51 +1621,53 @@ void SkScalerContext::MakeRec(const SkPaint& paint, */ SK_DECLARE_STATIC_MUTEX(gMaskGammaCacheMutex); +static SkColorSpaceLuminance* gDeviceLuminance = NULL; +static SkScalar gDeviceGammaExponent = SK_ScalarMin; /** * The caller must hold the gMaskGammaCacheMutex and continue to hold it until - * the returned SkColorSpaceLuminance pointer is refed or forgotten. + * the returned SkColorSpaceLuminance pointer is forgotten. */ static SkColorSpaceLuminance* cachedDeviceLuminance(SkScalar gammaExponent) { - static SkColorSpaceLuminance* gDeviceLuminance = NULL; - static SkScalar gGammaExponent = SK_ScalarMin; - if (gGammaExponent != gammaExponent) { + if (gDeviceGammaExponent != gammaExponent) { + SkDELETE(gDeviceLuminance); if (0 == gammaExponent) { gDeviceLuminance = SkNEW(SkSRGBLuminance); } else { gDeviceLuminance = SkNEW_ARGS(SkGammaLuminance, (gammaExponent)); } - gGammaExponent = gammaExponent; + gDeviceGammaExponent = gammaExponent; } return gDeviceLuminance; } +static SkColorSpaceLuminance* gPaintLuminance = NULL; +static SkScalar gPaintGammaExponent = SK_ScalarMin; /** * The caller must hold the gMaskGammaCacheMutex and continue to hold it until - * the returned SkColorSpaceLuminance pointer is refed or forgotten. + * the returned SkColorSpaceLuminance pointer is forgotten. */ static SkColorSpaceLuminance* cachedPaintLuminance(SkScalar gammaExponent) { - static SkColorSpaceLuminance* gPaintLuminance = NULL; - static SkScalar gGammaExponent = SK_ScalarMin; - if (gGammaExponent != gammaExponent) { + if (gPaintGammaExponent != gammaExponent) { + SkDELETE(gPaintLuminance); if (0 == gammaExponent) { gPaintLuminance = SkNEW(SkSRGBLuminance); } else { gPaintLuminance = SkNEW_ARGS(SkGammaLuminance, (gammaExponent)); } - gGammaExponent = gammaExponent; + gPaintGammaExponent = gammaExponent; } return gPaintLuminance; } +static SkMaskGamma* gMaskGamma = NULL; +static SkScalar gContrast = SK_ScalarMin; +static SkScalar gPaintGamma = SK_ScalarMin; +static SkScalar gDeviceGamma = SK_ScalarMin; /** * The caller must hold the gMaskGammaCacheMutex and continue to hold it until * the returned SkMaskGamma pointer is refed or forgotten. */ static SkMaskGamma* cachedMaskGamma(SkScalar contrast, SkScalar paintGamma, SkScalar deviceGamma) { - static SkMaskGamma* gMaskGamma = NULL; - static SkScalar gContrast = SK_ScalarMin; - static SkScalar gPaintGamma = SK_ScalarMin; - static SkScalar gDeviceGamma = SK_ScalarMin; if (gContrast != contrast || gPaintGamma != paintGamma || gDeviceGamma != deviceGamma) { SkSafeUnref(gMaskGamma); SkColorSpaceLuminance* paintLuminance = cachedPaintLuminance(paintGamma); @@ -1678,6 +1680,23 @@ static SkMaskGamma* cachedMaskGamma(SkScalar contrast, SkScalar paintGamma, SkSc return gMaskGamma; } +/*static*/ void SkPaint::Term() { + SkAutoMutexAcquire ama(gMaskGammaCacheMutex); + + SkSafeUnref(gMaskGamma); + SkDEBUGCODE(gContrast = SK_ScalarMin;) + SkDEBUGCODE(gPaintGamma = SK_ScalarMin;) + SkDEBUGCODE(gDeviceGamma = SK_ScalarMin;) + + SkDELETE(gPaintLuminance); + SkDEBUGCODE(gPaintLuminance = NULL;) + SkDEBUGCODE(gPaintGammaExponent = SK_ScalarMin;) + + SkDELETE(gDeviceLuminance); + SkDEBUGCODE(gDeviceLuminance = NULL;) + SkDEBUGCODE(gDeviceGammaExponent = SK_ScalarMin;) +} + /** * We ensure that the rec is self-consistent and efficient (where possible) */ -- 2.7.4