epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 11 | #include "SkTypefaceCache.h" |
| 12 | #include "SkThread.h" |
| 13 | |
reed@google.com | e9bbc81 | 2013-03-27 20:29:18 +0000 | [diff] [blame] | 14 | #define TYPEFACE_CACHE_LIMIT 1024 |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 15 | |
reed@google.com | 03b8781 | 2013-08-13 14:59:17 +0000 | [diff] [blame] | 16 | SkTypefaceCache::SkTypefaceCache() {} |
| 17 | |
| 18 | SkTypefaceCache::~SkTypefaceCache() { |
| 19 | const Rec* curr = fArray.begin(); |
| 20 | const Rec* stop = fArray.end(); |
| 21 | while (curr < stop) { |
| 22 | if (curr->fStrong) { |
| 23 | curr->fFace->unref(); |
| 24 | } else { |
| 25 | curr->fFace->weak_unref(); |
| 26 | } |
| 27 | curr += 1; |
| 28 | } |
| 29 | } |
| 30 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 31 | void SkTypefaceCache::add(SkTypeface* face, |
| 32 | SkTypeface::Style requestedStyle, |
| 33 | bool strong) { |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 34 | if (fArray.count() >= TYPEFACE_CACHE_LIMIT) { |
| 35 | this->purge(TYPEFACE_CACHE_LIMIT >> 2); |
| 36 | } |
| 37 | |
| 38 | Rec* rec = fArray.append(); |
| 39 | rec->fFace = face; |
| 40 | rec->fRequestedStyle = requestedStyle; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 41 | rec->fStrong = strong; |
| 42 | if (strong) { |
| 43 | face->ref(); |
| 44 | } else { |
| 45 | face->weak_ref(); |
| 46 | } |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | SkTypeface* SkTypefaceCache::findByID(SkFontID fontID) const { |
| 50 | const Rec* curr = fArray.begin(); |
| 51 | const Rec* stop = fArray.end(); |
| 52 | while (curr < stop) { |
| 53 | if (curr->fFace->uniqueID() == fontID) { |
| 54 | return curr->fFace; |
| 55 | } |
| 56 | curr += 1; |
| 57 | } |
| 58 | return NULL; |
| 59 | } |
| 60 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 61 | SkTypeface* SkTypefaceCache::findByProcAndRef(FindProc proc, void* ctx) const { |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 62 | const Rec* curr = fArray.begin(); |
| 63 | const Rec* stop = fArray.end(); |
| 64 | while (curr < stop) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 65 | SkTypeface* currFace = curr->fFace; |
| 66 | if (proc(currFace, curr->fRequestedStyle, ctx)) { |
| 67 | if (curr->fStrong) { |
| 68 | currFace->ref(); |
| 69 | return currFace; |
| 70 | } else if (currFace->try_ref()) { |
| 71 | return currFace; |
| 72 | } else { |
| 73 | //remove currFace from fArray? |
| 74 | } |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 75 | } |
| 76 | curr += 1; |
| 77 | } |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | void SkTypefaceCache::purge(int numToPurge) { |
| 82 | int count = fArray.count(); |
| 83 | int i = 0; |
| 84 | while (i < count) { |
| 85 | SkTypeface* face = fArray[i].fFace; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 86 | bool strong = fArray[i].fStrong; |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 87 | if ((strong && face->unique()) || (!strong && face->weak_expired())) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 88 | if (strong) { |
| 89 | face->unref(); |
| 90 | } else { |
| 91 | face->weak_unref(); |
| 92 | } |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 93 | fArray.remove(i); |
| 94 | --count; |
| 95 | if (--numToPurge == 0) { |
| 96 | return; |
| 97 | } |
| 98 | } else { |
| 99 | ++i; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
reed@google.com | d40da64 | 2011-10-20 13:18:37 +0000 | [diff] [blame] | 104 | void SkTypefaceCache::purgeAll() { |
| 105 | this->purge(fArray.count()); |
reed@google.com | d40da64 | 2011-10-20 13:18:37 +0000 | [diff] [blame] | 106 | } |
| 107 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 108 | /////////////////////////////////////////////////////////////////////////////// |
| 109 | |
| 110 | SkTypefaceCache& SkTypefaceCache::Get() { |
| 111 | static SkTypefaceCache gCache; |
| 112 | return gCache; |
| 113 | } |
| 114 | |
| 115 | SkFontID SkTypefaceCache::NewFontID() { |
| 116 | static int32_t gFontID; |
| 117 | return sk_atomic_inc(&gFontID) + 1; |
| 118 | } |
| 119 | |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 120 | SK_DECLARE_STATIC_MUTEX(gMutex); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 121 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 122 | void SkTypefaceCache::Add(SkTypeface* face, |
| 123 | SkTypeface::Style requestedStyle, |
| 124 | bool strong) { |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 125 | SkAutoMutexAcquire ama(gMutex); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 126 | Get().add(face, requestedStyle, strong); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | SkTypeface* SkTypefaceCache::FindByID(SkFontID fontID) { |
| 130 | SkAutoMutexAcquire ama(gMutex); |
| 131 | return Get().findByID(fontID); |
| 132 | } |
| 133 | |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 134 | SkTypeface* SkTypefaceCache::FindByProcAndRef(FindProc proc, void* ctx) { |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 135 | SkAutoMutexAcquire ama(gMutex); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 136 | SkTypeface* typeface = Get().findByProcAndRef(proc, ctx); |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 137 | return typeface; |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 138 | } |
| 139 | |
reed@google.com | d40da64 | 2011-10-20 13:18:37 +0000 | [diff] [blame] | 140 | void SkTypefaceCache::PurgeAll() { |
bungeman@google.com | ee51d1a | 2012-02-16 12:40:48 +0000 | [diff] [blame] | 141 | SkAutoMutexAcquire ama(gMutex); |
reed@google.com | d40da64 | 2011-10-20 13:18:37 +0000 | [diff] [blame] | 142 | Get().purgeAll(); |
| 143 | } |
| 144 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 145 | /////////////////////////////////////////////////////////////////////////////// |
| 146 | |
| 147 | #ifdef SK_DEBUG |
| 148 | static bool DumpProc(SkTypeface* face, SkTypeface::Style style, void* ctx) { |
| 149 | SkDebugf("SkTypefaceCache: face %p fontID %d style %d refcnt %d\n", |
| 150 | face, face->uniqueID(), style, face->getRefCnt()); |
| 151 | return false; |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | void SkTypefaceCache::Dump() { |
| 156 | #ifdef SK_DEBUG |
| 157 | SkAutoMutexAcquire ama(gMutex); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 158 | (void)Get().findByProcAndRef(DumpProc, NULL); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 159 | #endif |
| 160 | } |