Future proof luminance caches.
authorbungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 4 Sep 2012 13:49:59 +0000 (13:49 +0000)
committerbungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 4 Sep 2012 13:49:59 +0000 (13:49 +0000)
https://codereview.appspot.com/6495071/

git-svn-id: http://skia.googlecode.com/svn/trunk@5382 2bbb7eff-a529-9590-31e7-b0007b416f81

include/core/SkPaint.h
src/core/SkGraphics.cpp
src/core/SkPaint.cpp

index 096344f..50fcdd8 100644 (file)
@@ -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;
 
index d71a70e..64f9264 100644 (file)
@@ -122,6 +122,7 @@ void SkGraphics::Init() {
 
 void SkGraphics::Term() {
     PurgeFontCache();
+    SkPaint::Term();
 }
 
 ///////////////////////////////////////////////////////////////////////////////
index 6ea03b4..4b23a5d 100644 (file)
@@ -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)
  */