3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #include "SkTypefaceCache.h"
14 #define TYPEFACE_CACHE_LIMIT 1024
16 SkTypefaceCache::SkTypefaceCache() {}
18 SkTypefaceCache::~SkTypefaceCache() {
19 const Rec* curr = fArray.begin();
20 const Rec* stop = fArray.end();
27 void SkTypefaceCache::add(SkTypeface* face, const SkFontStyle& requestedStyle) {
28 if (fArray.count() >= TYPEFACE_CACHE_LIMIT) {
29 this->purge(TYPEFACE_CACHE_LIMIT >> 2);
32 Rec* rec = fArray.append();
33 rec->fFace = SkRef(face);
34 rec->fRequestedStyle = requestedStyle;
37 SkTypeface* SkTypefaceCache::findByID(SkFontID fontID) const {
38 const Rec* curr = fArray.begin();
39 const Rec* stop = fArray.end();
41 if (curr->fFace->uniqueID() == fontID) {
49 SkTypeface* SkTypefaceCache::findByProcAndRef(FindProc proc, void* ctx) const {
50 const Rec* curr = fArray.begin();
51 const Rec* stop = fArray.end();
53 SkTypeface* currFace = curr->fFace;
54 if (proc(currFace, curr->fRequestedStyle, ctx)) {
55 return SkRef(currFace);
62 void SkTypefaceCache::purge(int numToPurge) {
63 int count = fArray.count();
66 SkTypeface* face = fArray[i].fFace;
71 if (--numToPurge == 0) {
80 void SkTypefaceCache::purgeAll() {
81 this->purge(fArray.count());
84 ///////////////////////////////////////////////////////////////////////////////
86 SkTypefaceCache& SkTypefaceCache::Get() {
87 static SkTypefaceCache gCache;
91 SkFontID SkTypefaceCache::NewFontID() {
92 static int32_t gFontID;
93 return sk_atomic_inc(&gFontID) + 1;
96 SK_DECLARE_STATIC_MUTEX(gMutex);
98 void SkTypefaceCache::Add(SkTypeface* face, const SkFontStyle& requestedStyle) {
99 SkAutoMutexAcquire ama(gMutex);
100 Get().add(face, requestedStyle);
103 SkTypeface* SkTypefaceCache::FindByID(SkFontID fontID) {
104 SkAutoMutexAcquire ama(gMutex);
105 return Get().findByID(fontID);
108 SkTypeface* SkTypefaceCache::FindByProcAndRef(FindProc proc, void* ctx) {
109 SkAutoMutexAcquire ama(gMutex);
110 SkTypeface* typeface = Get().findByProcAndRef(proc, ctx);
114 void SkTypefaceCache::PurgeAll() {
115 SkAutoMutexAcquire ama(gMutex);
119 ///////////////////////////////////////////////////////////////////////////////
122 static bool DumpProc(SkTypeface* face, const SkFontStyle& s, void* ctx) {
123 SkDebugf("SkTypefaceCache: face %p fontID %d weight %d width %d style %d refcnt %d\n",
124 face, face->uniqueID(), s.weight(), s.width(), s.slant(), face->getRefCnt());
129 void SkTypefaceCache::Dump() {
131 SkAutoMutexAcquire ama(gMutex);
132 (void)Get().findByProcAndRef(DumpProc, NULL);