change typeface::purgeall to not assume that the array is empty, as it may
authorreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 16 Dec 2011 17:56:23 +0000 (17:56 +0000)
committerreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 16 Dec 2011 17:56:23 +0000 (17:56 +0000)
contain typefaces that still are referenced.

change SkTypeface::UniqueID to internally hold onto a reference to the default
typeface, so that it will always be available if the font cache tries to resolve
it. Then general rule being: if we have a fontID specified by SkPaint, then its
associated typeface object must be alive.

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

src/core/SkTypeface.cpp
src/core/SkTypefaceCache.cpp

index 268fcc9..e3b49e6 100644 (file)
@@ -34,25 +34,25 @@ SkTypeface::~SkTypeface() {
 
 ///////////////////////////////////////////////////////////////////////////////
 
-uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
-    if (face) {
-        return face->uniqueID();
+static SkTypeface* get_default_typeface() {
+    // we keep a reference to this guy for all time, since if we return its
+    // fontID, the font cache may later on ask to resolve that back into a
+    // typeface object.
+    static SkTypeface* gDefaultTypeface;
+
+    if (NULL == gDefaultTypeface) {
+        gDefaultTypeface =
+        SkFontHost::CreateTypeface(NULL, NULL, NULL, 0,
+                                   SkTypeface::kNormal);
     }
+    return gDefaultTypeface;
+}
 
-    // We cache the default fontID, assuming it will not change during a boot
-    // The initial value of 0 is fine, since a typeface's uniqueID should not
-    // be zero.
-    static uint32_t gDefaultFontID;
-
-    if (0 == gDefaultFontID) {
-        SkTypeface* defaultFace =
-                SkFontHost::CreateTypeface(NULL, NULL, NULL, 0,
-                                           SkTypeface::kNormal);
-        SkASSERT(defaultFace);
-        gDefaultFontID = defaultFace->uniqueID();
-        defaultFace->unref();
+uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
+    if (NULL == face) {
+        face = get_default_typeface();
     }
-    return gDefaultFontID;
+    return face->uniqueID();
 }
 
 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
index 8d86005..5a0b36c 100644 (file)
@@ -68,7 +68,6 @@ void SkTypefaceCache::purge(int numToPurge) {
 
 void SkTypefaceCache::purgeAll() {
     this->purge(fArray.count());
-    fArray.reset();
 }
 
 ///////////////////////////////////////////////////////////////////////////////