expose instance methods on SkTypefaceCache

BUG=
R=bungeman@google.com

Review URL: https://codereview.chromium.org/23067003

git-svn-id: http://skia.googlecode.com/svn/trunk@10691 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2013-08-13 14:59:17 +00:00
parent 2b1155ac0d
commit 03b8781250
2 changed files with 36 additions and 15 deletions

View File

@ -13,6 +13,21 @@
#define TYPEFACE_CACHE_LIMIT 1024 #define TYPEFACE_CACHE_LIMIT 1024
SkTypefaceCache::SkTypefaceCache() {}
SkTypefaceCache::~SkTypefaceCache() {
const Rec* curr = fArray.begin();
const Rec* stop = fArray.end();
while (curr < stop) {
if (curr->fStrong) {
curr->fFace->unref();
} else {
curr->fFace->weak_unref();
}
curr += 1;
}
}
void SkTypefaceCache::add(SkTypeface* face, void SkTypefaceCache::add(SkTypeface* face,
SkTypeface::Style requestedStyle, SkTypeface::Style requestedStyle,
bool strong) { bool strong) {

View File

@ -23,6 +23,9 @@
class SkTypefaceCache { class SkTypefaceCache {
public: public:
SkTypefaceCache();
~SkTypefaceCache();
/** /**
* Callback for FindByProc. Returns true if the given typeface is a match * Callback for FindByProc. Returns true if the given typeface is a match
* for the given context. The passed typeface is owned by the cache and is * for the given context. The passed typeface is owned by the cache and is
@ -30,21 +33,13 @@ public:
*/ */
typedef bool (*FindProc)(SkTypeface*, SkTypeface::Style, void* context); typedef bool (*FindProc)(SkTypeface*, SkTypeface::Style, void* context);
/**
* Helper: returns a unique fontID to pass to the constructor of
* your subclass of SkTypeface
*/
static SkFontID NewFontID();
/** /**
* Add a typeface to the cache. This ref()s the typeface, so that the * Add a typeface to the cache. This ref()s the typeface, so that the
* cache is also an owner. Later, if we need to purge the cache, typefaces * cache is also an owner. Later, if we need to purge the cache, typefaces
* whose refcnt is 1 (meaning only the cache is an owner) will be * whose refcnt is 1 (meaning only the cache is an owner) will be
* unref()ed. * unref()ed.
*/ */
static void Add(SkTypeface*, void add(SkTypeface*, SkTypeface::Style requested, bool strong = true);
SkTypeface::Style requested,
bool strong = true);
/** /**
* Search the cache for a typeface with the specified fontID (uniqueID). * Search the cache for a typeface with the specified fontID (uniqueID).
@ -52,14 +47,14 @@ public:
* is found, return NULL. The reference count is unmodified as it is * is found, return NULL. The reference count is unmodified as it is
* assumed that the stack will contain a ref to the typeface. * assumed that the stack will contain a ref to the typeface.
*/ */
static SkTypeface* FindByID(SkFontID fontID); SkTypeface* findByID(SkFontID findID) const;
/** /**
* Iterate through the cache, calling proc(typeface, ctx) with each * Iterate through the cache, calling proc(typeface, ctx) with each
* typeface. If proc returns true, then we return that typeface (this * typeface. If proc returns true, then we return that typeface (this
* ref()s the typeface). If it never returns true, we return NULL. * ref()s the typeface). If it never returns true, we return NULL.
*/ */
static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx); SkTypeface* findByProcAndRef(FindProc proc, void* ctx) const;
/** /**
* This will unref all of the typefaces in the cache for which the cache * This will unref all of the typefaces in the cache for which the cache
@ -67,6 +62,21 @@ public:
* This function is exposed for clients that explicitly want to purge the * This function is exposed for clients that explicitly want to purge the
* cache (e.g. to look for leaks). * cache (e.g. to look for leaks).
*/ */
void purgeAll();
/**
* Helper: returns a unique fontID to pass to the constructor of
* your subclass of SkTypeface
*/
static SkFontID NewFontID();
// These are static wrappers around a global instance of a cache.
static void Add(SkTypeface*,
SkTypeface::Style requested,
bool strong = true);
static SkTypeface* FindByID(SkFontID fontID);
static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx);
static void PurgeAll(); static void PurgeAll();
/** /**
@ -77,11 +87,7 @@ public:
private: private:
static SkTypefaceCache& Get(); static SkTypefaceCache& Get();
void add(SkTypeface*, SkTypeface::Style requested, bool strong = true);
SkTypeface* findByID(SkFontID findID) const;
SkTypeface* findByProcAndRef(FindProc proc, void* ctx) const;
void purge(int count); void purge(int count);
void purgeAll();
struct Rec { struct Rec {
SkTypeface* fFace; SkTypeface* fFace;