epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkGlyphCache.h" |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 9 | #include "SkGlyphCache_Globals.h" |
reed@google.com | 77407ca | 2011-11-08 13:48:32 +0000 | [diff] [blame] | 10 | #include "SkGraphics.h" |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 11 | #include "SkLazyPtr.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkPaint.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 13 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkTemplates.h" |
reed@google.com | 6172d67 | 2012-05-17 13:38:03 +0000 | [diff] [blame] | 15 | #include "SkTLS.h" |
reed@google.com | 90808e8 | 2013-03-19 14:44:54 +0000 | [diff] [blame] | 16 | #include "SkTypeface.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
reed@google.com | 8887be0 | 2012-01-25 16:04:18 +0000 | [diff] [blame] | 18 | //#define SPEW_PURGE_STATUS |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | SkGlyphCache_Globals* create_globals() { |
| 23 | return SkNEW_ARGS(SkGlyphCache_Globals, (SkGlyphCache_Globals::kYes_UseMutex)); |
bungeman@google.com | b6ad00b | 2014-04-28 13:58:35 +0000 | [diff] [blame] | 24 | } |
| 25 | |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 26 | } // namespace |
| 27 | |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 28 | SK_DECLARE_STATIC_LAZY_PTR(SkGlyphCache_Globals, globals, create_globals); |
| 29 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 30 | // Returns the shared globals |
| 31 | static SkGlyphCache_Globals& getSharedGlobals() { |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 32 | return *globals.get(); |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Returns the TLS globals (if set), or the shared globals |
| 36 | static SkGlyphCache_Globals& getGlobals() { |
| 37 | SkGlyphCache_Globals* tls = SkGlyphCache_Globals::FindTLS(); |
| 38 | return tls ? *tls : getSharedGlobals(); |
| 39 | } |
| 40 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 43 | #ifdef SK_GLYPHCACHE_TRACK_HASH_STATS |
| 44 | #define RecordHashSuccess() fHashHitCount += 1 |
| 45 | #define RecordHashCollisionIf(pred) do { if (pred) fHashMissCount += 1; } while (0) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | #else |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 47 | #define RecordHashSuccess() (void)0 |
| 48 | #define RecordHashCollisionIf(pred) (void)0 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | #endif |
| 50 | #define RecordHashCollision() RecordHashCollisionIf(true) |
| 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | |
reed@google.com | 6757a3c | 2013-06-19 19:25:36 +0000 | [diff] [blame] | 54 | // so we don't grow our arrays a lot |
| 55 | #define kMinGlyphCount 16 |
| 56 | #define kMinGlyphImageSize (16*2) |
| 57 | #define kMinAllocAmount ((sizeof(SkGlyph) + kMinGlyphImageSize) * kMinGlyphCount) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | |
reed@google.com | 4b60dbe | 2013-07-09 15:29:32 +0000 | [diff] [blame] | 59 | SkGlyphCache::SkGlyphCache(SkTypeface* typeface, const SkDescriptor* desc, SkScalerContext* ctx) |
| 60 | : fScalerContext(ctx), fGlyphAlloc(kMinAllocAmount) { |
reed@google.com | 90808e8 | 2013-03-19 14:44:54 +0000 | [diff] [blame] | 61 | SkASSERT(typeface); |
reed@google.com | 4b60dbe | 2013-07-09 15:29:32 +0000 | [diff] [blame] | 62 | SkASSERT(desc); |
| 63 | SkASSERT(ctx); |
reed@google.com | 90808e8 | 2013-03-19 14:44:54 +0000 | [diff] [blame] | 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | fPrev = fNext = NULL; |
| 66 | |
| 67 | fDesc = desc->copy(); |
reed@google.com | 0a01f5a | 2013-05-08 14:19:08 +0000 | [diff] [blame] | 68 | fScalerContext->getFontMetrics(&fFontMetrics); |
herb | 4c08f16 | 2015-02-02 17:47:31 -0800 | [diff] [blame] | 69 | |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 70 | // init to 0 so that all of the pointers will be null |
| 71 | memset(fGlyphHash, 0, sizeof(fGlyphHash)); |
| 72 | |
reed@google.com | 6757a3c | 2013-06-19 19:25:36 +0000 | [diff] [blame] | 73 | fMemoryUsed = sizeof(*this); |
reed@google.com | 22a0221 | 2011-03-01 21:33:48 +0000 | [diff] [blame] | 74 | |
reed@google.com | 6757a3c | 2013-06-19 19:25:36 +0000 | [diff] [blame] | 75 | fGlyphArray.setReserve(kMinGlyphCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | fAuxProcList = NULL; |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 78 | |
| 79 | #ifdef SK_GLYPHCACHE_TRACK_HASH_STATS |
| 80 | fHashHitCount = fHashMissCount = 0; |
| 81 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | SkGlyphCache::~SkGlyphCache() { |
reed@google.com | 6757a3c | 2013-06-19 19:25:36 +0000 | [diff] [blame] | 85 | #if 0 |
| 86 | { |
| 87 | size_t ptrMem = fGlyphArray.count() * sizeof(SkGlyph*); |
| 88 | size_t glyphAlloc = fGlyphAlloc.totalCapacity(); |
| 89 | size_t glyphHashUsed = 0; |
| 90 | size_t uniHashUsed = 0; |
| 91 | for (int i = 0; i < kHashCount; ++i) { |
| 92 | glyphHashUsed += fGlyphHash[i] ? sizeof(fGlyphHash[0]) : 0; |
| 93 | uniHashUsed += fCharToGlyphHash[i].fID != 0xFFFFFFFF ? sizeof(fCharToGlyphHash[0]) : 0; |
| 94 | } |
| 95 | size_t glyphUsed = fGlyphArray.count() * sizeof(SkGlyph); |
| 96 | size_t imageUsed = 0; |
| 97 | for (int i = 0; i < fGlyphArray.count(); ++i) { |
| 98 | const SkGlyph& g = *fGlyphArray[i]; |
| 99 | if (g.fImage) { |
| 100 | imageUsed += g.fHeight * g.rowBytes(); |
| 101 | } |
| 102 | } |
| 103 | |
herb | f8dd076 | 2015-01-28 14:12:12 -0800 | [diff] [blame] | 104 | SkDebugf("glyphPtrArray,%zu, Alloc,%zu, imageUsed,%zu, glyphUsed,%zu, glyphHashAlloc,%zu, glyphHashUsed,%zu, unicharHashAlloc,%zu, unicharHashUsed,%zu\n", |
| 105 | ptrMem, glyphAlloc, imageUsed, glyphUsed, sizeof(fGlyphHash), glyphHashUsed, sizeof(CharGlyphRec) * kHashCount, uniHashUsed); |
skia.committer@gmail.com | 4d494f0 | 2013-06-20 07:00:59 +0000 | [diff] [blame] | 106 | |
reed@google.com | 6757a3c | 2013-06-19 19:25:36 +0000 | [diff] [blame] | 107 | } |
| 108 | #endif |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 109 | SkGlyph** gptr = fGlyphArray.begin(); |
| 110 | SkGlyph** stop = fGlyphArray.end(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | while (gptr < stop) { |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 112 | SkPath* path = (*gptr)->fPath; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | if (path) { |
| 114 | SkDELETE(path); |
| 115 | } |
| 116 | gptr += 1; |
| 117 | } |
| 118 | SkDescriptor::Free(fDesc); |
| 119 | SkDELETE(fScalerContext); |
| 120 | this->invokeAndRemoveAuxProcs(); |
| 121 | } |
| 122 | |
herb | f8dd076 | 2015-01-28 14:12:12 -0800 | [diff] [blame] | 123 | SkGlyphCache::CharGlyphRec* SkGlyphCache::getCharGlyphRec(uint32_t id) { |
| 124 | if (NULL == fCharToGlyphHash.get()) { |
| 125 | fCharToGlyphHash.reset(kHashCount); |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 126 | // init with 0xFF so that the charCode field will be -1, which is invalid |
| 127 | memset(fCharToGlyphHash.get(), 0xFF, |
herb | f8dd076 | 2015-01-28 14:12:12 -0800 | [diff] [blame] | 128 | sizeof(CharGlyphRec) * kHashCount); |
| 129 | } |
| 130 | |
| 131 | return &fCharToGlyphHash[ID2HashIndex(id)]; |
| 132 | } |
| 133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | /////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| 136 | #ifdef SK_DEBUG |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 137 | #define VALIDATE() AutoValidate av(this) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | #else |
| 139 | #define VALIDATE() |
| 140 | #endif |
| 141 | |
| 142 | uint16_t SkGlyphCache::unicharToGlyph(SkUnichar charCode) { |
| 143 | VALIDATE(); |
| 144 | uint32_t id = SkGlyph::MakeID(charCode); |
herb | f8dd076 | 2015-01-28 14:12:12 -0800 | [diff] [blame] | 145 | const CharGlyphRec& rec = *this->getCharGlyphRec(id); |
reed@google.com | 22a0221 | 2011-03-01 21:33:48 +0000 | [diff] [blame] | 146 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | if (rec.fID == id) { |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 148 | return rec.fGlyph->getGlyphID(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | } else { |
| 150 | return fScalerContext->charToGlyphID(charCode); |
| 151 | } |
| 152 | } |
| 153 | |
reed@android.com | 9d3a985 | 2010-01-08 14:07:42 +0000 | [diff] [blame] | 154 | SkUnichar SkGlyphCache::glyphToUnichar(uint16_t glyphID) { |
| 155 | return fScalerContext->glyphIDToChar(glyphID); |
| 156 | } |
| 157 | |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 158 | unsigned SkGlyphCache::getGlyphCount() { |
| 159 | return fScalerContext->getGlyphCount(); |
| 160 | } |
| 161 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | /////////////////////////////////////////////////////////////////////////////// |
| 163 | |
| 164 | const SkGlyph& SkGlyphCache::getUnicharAdvance(SkUnichar charCode) { |
| 165 | VALIDATE(); |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 166 | uint32_t id = SkGlyph::MakeID(charCode); |
| 167 | CharGlyphRec* rec = this->getCharGlyphRec(id); |
| 168 | |
| 169 | if (rec->fID != id) { |
| 170 | // this ID is based on the UniChar |
| 171 | rec->fID = id; |
| 172 | // this ID is based on the glyph index |
| 173 | id = SkGlyph::MakeID(fScalerContext->charToGlyphID(charCode)); |
| 174 | rec->fGlyph = this->lookupMetrics(id, kJustAdvance_MetricsType); |
| 175 | } |
| 176 | return *rec->fGlyph; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | const SkGlyph& SkGlyphCache::getGlyphIDAdvance(uint16_t glyphID) { |
| 180 | VALIDATE(); |
| 181 | uint32_t id = SkGlyph::MakeID(glyphID); |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 182 | unsigned index = ID2HashIndex(id); |
| 183 | SkGlyph* glyph = fGlyphHash[index]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 185 | if (NULL == glyph || glyph->fID != id) { |
| 186 | glyph = this->lookupMetrics(glyphID, kJustAdvance_MetricsType); |
| 187 | fGlyphHash[index] = glyph; |
| 188 | } |
| 189 | return *glyph; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /////////////////////////////////////////////////////////////////////////////// |
| 193 | |
| 194 | const SkGlyph& SkGlyphCache::getUnicharMetrics(SkUnichar charCode) { |
| 195 | VALIDATE(); |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 196 | uint32_t id = SkGlyph::MakeID(charCode); |
| 197 | CharGlyphRec* rec = this->getCharGlyphRec(id); |
| 198 | |
| 199 | if (rec->fID != id) { |
| 200 | RecordHashCollisionIf(rec->fGlyph != NULL); |
| 201 | // this ID is based on the UniChar |
| 202 | rec->fID = id; |
| 203 | // this ID is based on the glyph index |
| 204 | id = SkGlyph::MakeID(fScalerContext->charToGlyphID(charCode)); |
| 205 | rec->fGlyph = this->lookupMetrics(id, kFull_MetricsType); |
| 206 | } else { |
| 207 | RecordHashSuccess(); |
| 208 | if (rec->fGlyph->isJustAdvance()) { |
| 209 | fScalerContext->getMetrics(rec->fGlyph); |
| 210 | } |
| 211 | } |
| 212 | SkASSERT(rec->fGlyph->isFullMetrics()); |
| 213 | return *rec->fGlyph; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | const SkGlyph& SkGlyphCache::getUnicharMetrics(SkUnichar charCode, |
| 217 | SkFixed x, SkFixed y) { |
| 218 | VALIDATE(); |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 219 | uint32_t id = SkGlyph::MakeID(charCode, x, y); |
| 220 | CharGlyphRec* rec = this->getCharGlyphRec(id); |
| 221 | |
| 222 | if (rec->fID != id) { |
| 223 | RecordHashCollisionIf(rec->fGlyph != NULL); |
| 224 | // this ID is based on the UniChar |
| 225 | rec->fID = id; |
| 226 | // this ID is based on the glyph index |
| 227 | id = SkGlyph::MakeID(fScalerContext->charToGlyphID(charCode), x, y); |
| 228 | rec->fGlyph = this->lookupMetrics(id, kFull_MetricsType); |
| 229 | } else { |
| 230 | RecordHashSuccess(); |
| 231 | if (rec->fGlyph->isJustAdvance()) { |
| 232 | fScalerContext->getMetrics(rec->fGlyph); |
| 233 | } |
| 234 | } |
| 235 | SkASSERT(rec->fGlyph->isFullMetrics()); |
| 236 | return *rec->fGlyph; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | const SkGlyph& SkGlyphCache::getGlyphIDMetrics(uint16_t glyphID) { |
| 240 | VALIDATE(); |
| 241 | uint32_t id = SkGlyph::MakeID(glyphID); |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 242 | unsigned index = ID2HashIndex(id); |
| 243 | SkGlyph* glyph = fGlyphHash[index]; |
| 244 | |
| 245 | if (NULL == glyph || glyph->fID != id) { |
| 246 | RecordHashCollisionIf(glyph != NULL); |
| 247 | glyph = this->lookupMetrics(glyphID, kFull_MetricsType); |
| 248 | fGlyphHash[index] = glyph; |
| 249 | } else { |
| 250 | RecordHashSuccess(); |
| 251 | if (glyph->isJustAdvance()) { |
| 252 | fScalerContext->getMetrics(glyph); |
| 253 | } |
| 254 | } |
| 255 | SkASSERT(glyph->isFullMetrics()); |
| 256 | return *glyph; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 257 | } |
| 258 | |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 259 | const SkGlyph& SkGlyphCache::getGlyphIDMetrics(uint16_t glyphID, SkFixed x, SkFixed y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 260 | VALIDATE(); |
| 261 | uint32_t id = SkGlyph::MakeID(glyphID, x, y); |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 262 | unsigned index = ID2HashIndex(id); |
| 263 | SkGlyph* glyph = fGlyphHash[index]; |
| 264 | |
| 265 | if (NULL == glyph || glyph->fID != id) { |
| 266 | RecordHashCollisionIf(glyph != NULL); |
| 267 | glyph = this->lookupMetrics(id, kFull_MetricsType); |
| 268 | fGlyphHash[index] = glyph; |
| 269 | } else { |
| 270 | RecordHashSuccess(); |
| 271 | if (glyph->isJustAdvance()) { |
| 272 | fScalerContext->getMetrics(glyph); |
| 273 | } |
| 274 | } |
| 275 | SkASSERT(glyph->isFullMetrics()); |
| 276 | return *glyph; |
herb | 4c08f16 | 2015-02-02 17:47:31 -0800 | [diff] [blame] | 277 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 278 | |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 279 | SkGlyph* SkGlyphCache::lookupMetrics(uint32_t id, MetricsType mtype) { |
herb | 4c08f16 | 2015-02-02 17:47:31 -0800 | [diff] [blame] | 280 | SkGlyph* glyph; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 282 | int hi = 0; |
| 283 | int count = fGlyphArray.count(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 285 | if (count) { |
| 286 | SkGlyph** gptr = fGlyphArray.begin(); |
| 287 | int lo = 0; |
| 288 | |
| 289 | hi = count - 1; |
| 290 | while (lo < hi) { |
| 291 | int mid = (hi + lo) >> 1; |
| 292 | if (gptr[mid]->fID < id) { |
| 293 | lo = mid + 1; |
| 294 | } else { |
| 295 | hi = mid; |
| 296 | } |
| 297 | } |
| 298 | glyph = gptr[hi]; |
| 299 | if (glyph->fID == id) { |
| 300 | if (kFull_MetricsType == mtype && glyph->isJustAdvance()) { |
| 301 | fScalerContext->getMetrics(glyph); |
| 302 | } |
| 303 | return glyph; |
| 304 | } |
| 305 | |
| 306 | // check if we need to bump hi before falling though to the allocator |
| 307 | if (glyph->fID < id) { |
| 308 | hi += 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | // not found, but hi tells us where to inser the new glyph |
| 313 | fMemoryUsed += sizeof(SkGlyph); |
| 314 | |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 315 | glyph = (SkGlyph*)fGlyphAlloc.alloc(sizeof(SkGlyph), |
| 316 | SkChunkAlloc::kThrow_AllocFailType); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 317 | glyph->init(id); |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 318 | *fGlyphArray.insert(hi) = glyph; |
reed@google.com | 22a0221 | 2011-03-01 21:33:48 +0000 | [diff] [blame] | 319 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | if (kJustAdvance_MetricsType == mtype) { |
| 321 | fScalerContext->getAdvance(glyph); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 322 | } else { |
| 323 | SkASSERT(kFull_MetricsType == mtype); |
| 324 | fScalerContext->getMetrics(glyph); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | } |
| 326 | |
bsalomon | 9bf4e5b | 2015-02-02 21:06:23 -0800 | [diff] [blame^] | 327 | return glyph; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | const void* SkGlyphCache::findImage(const SkGlyph& glyph) { |
scarybeasts@gmail.com | 17f694b | 2010-10-18 23:29:36 +0000 | [diff] [blame] | 331 | if (glyph.fWidth > 0 && glyph.fWidth < kMaxGlyphWidth) { |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 332 | if (NULL == glyph.fImage) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 333 | size_t size = glyph.computeImageSize(); |
reed@google.com | 6757a3c | 2013-06-19 19:25:36 +0000 | [diff] [blame] | 334 | const_cast<SkGlyph&>(glyph).fImage = fGlyphAlloc.alloc(size, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 335 | SkChunkAlloc::kReturnNil_AllocFailType); |
reed@google.com | 22a0221 | 2011-03-01 21:33:48 +0000 | [diff] [blame] | 336 | // check that alloc() actually succeeded |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 337 | if (glyph.fImage) { |
reed@google.com | 22a0221 | 2011-03-01 21:33:48 +0000 | [diff] [blame] | 338 | fScalerContext->getImage(glyph); |
reed@google.com | 5e2df64 | 2011-09-21 18:42:09 +0000 | [diff] [blame] | 339 | // TODO: the scaler may have changed the maskformat during |
| 340 | // getImage (e.g. from AA or LCD to BW) which means we may have |
| 341 | // overallocated the buffer. Check if the new computedImageSize |
| 342 | // is smaller, and if so, strink the alloc size in fImageAlloc. |
reed@google.com | 22a0221 | 2011-03-01 21:33:48 +0000 | [diff] [blame] | 343 | fMemoryUsed += size; |
| 344 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | return glyph.fImage; |
| 348 | } |
| 349 | |
| 350 | const SkPath* SkGlyphCache::findPath(const SkGlyph& glyph) { |
| 351 | if (glyph.fWidth) { |
| 352 | if (glyph.fPath == NULL) { |
| 353 | const_cast<SkGlyph&>(glyph).fPath = SkNEW(SkPath); |
| 354 | fScalerContext->getPath(glyph, glyph.fPath); |
| 355 | fMemoryUsed += sizeof(SkPath) + |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 356 | glyph.fPath->countPoints() * sizeof(SkPoint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | return glyph.fPath; |
| 360 | } |
| 361 | |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 362 | void SkGlyphCache::dump() const { |
| 363 | const SkTypeface* face = fScalerContext->getTypeface(); |
| 364 | const SkScalerContextRec& rec = fScalerContext->getRec(); |
| 365 | SkMatrix matrix; |
| 366 | rec.getSingleMatrix(&matrix); |
| 367 | matrix.preScale(SkScalarInvert(rec.fTextSize), SkScalarInvert(rec.fTextSize)); |
| 368 | SkString name; |
| 369 | face->getFamilyName(&name); |
| 370 | |
| 371 | SkString msg; |
| 372 | msg.printf("cache typeface:%x %25s:%d size:%2g [%g %g %g %g] lum:%02X devG:%d pntG:%d cntr:%d glyphs:%3d", |
| 373 | face->uniqueID(), name.c_str(), face->style(), rec.fTextSize, |
| 374 | matrix[SkMatrix::kMScaleX], matrix[SkMatrix::kMSkewX], |
| 375 | matrix[SkMatrix::kMSkewY], matrix[SkMatrix::kMScaleY], |
| 376 | rec.fLumBits & 0xFF, rec.fDeviceGamma, rec.fPaintGamma, rec.fContrast, |
| 377 | fGlyphArray.count()); |
| 378 | #ifdef SK_GLYPHCACHE_TRACK_HASH_STATS |
| 379 | const int sum = SkTMax(fHashHitCount + fHashMissCount, 1); // avoid divide-by-zero |
| 380 | msg.appendf(" hash:%2d\n", 100 * fHashHitCount / sum); |
| 381 | #endif |
| 382 | SkDebugf("%s\n", msg.c_str()); |
| 383 | } |
| 384 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 385 | /////////////////////////////////////////////////////////////////////////////// |
| 386 | |
| 387 | bool SkGlyphCache::getAuxProcData(void (*proc)(void*), void** dataPtr) const { |
| 388 | const AuxProcRec* rec = fAuxProcList; |
| 389 | while (rec) { |
| 390 | if (rec->fProc == proc) { |
| 391 | if (dataPtr) { |
| 392 | *dataPtr = rec->fData; |
| 393 | } |
| 394 | return true; |
| 395 | } |
| 396 | rec = rec->fNext; |
| 397 | } |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | void SkGlyphCache::setAuxProc(void (*proc)(void*), void* data) { |
| 402 | if (proc == NULL) { |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | AuxProcRec* rec = fAuxProcList; |
| 407 | while (rec) { |
| 408 | if (rec->fProc == proc) { |
| 409 | rec->fData = data; |
| 410 | return; |
| 411 | } |
| 412 | rec = rec->fNext; |
| 413 | } |
| 414 | // not found, create a new rec |
| 415 | rec = SkNEW(AuxProcRec); |
| 416 | rec->fProc = proc; |
| 417 | rec->fData = data; |
| 418 | rec->fNext = fAuxProcList; |
| 419 | fAuxProcList = rec; |
| 420 | } |
| 421 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | void SkGlyphCache::invokeAndRemoveAuxProcs() { |
| 423 | AuxProcRec* rec = fAuxProcList; |
| 424 | while (rec) { |
| 425 | rec->fProc(rec->fData); |
| 426 | AuxProcRec* next = rec->fNext; |
| 427 | SkDELETE(rec); |
| 428 | rec = next; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /////////////////////////////////////////////////////////////////////////////// |
| 433 | /////////////////////////////////////////////////////////////////////////////// |
| 434 | |
reed@google.com | 5d248bc | 2011-11-17 21:49:02 +0000 | [diff] [blame] | 435 | #include "SkThread.h" |
| 436 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 437 | size_t SkGlyphCache_Globals::setCacheSizeLimit(size_t newLimit) { |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 438 | static const size_t minLimit = 256 * 1024; |
| 439 | if (newLimit < minLimit) { |
| 440 | newLimit = minLimit; |
| 441 | } |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 442 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 443 | SkAutoMutexAcquire ac(fMutex); |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 444 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 445 | size_t prevLimit = fCacheSizeLimit; |
| 446 | fCacheSizeLimit = newLimit; |
| 447 | this->internalPurge(); |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 448 | return prevLimit; |
| 449 | } |
| 450 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 451 | int SkGlyphCache_Globals::setCacheCountLimit(int newCount) { |
| 452 | if (newCount < 0) { |
| 453 | newCount = 0; |
| 454 | } |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 455 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 456 | SkAutoMutexAcquire ac(fMutex); |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 457 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 458 | int prevCount = fCacheCountLimit; |
| 459 | fCacheCountLimit = newCount; |
| 460 | this->internalPurge(); |
| 461 | return prevCount; |
| 462 | } |
| 463 | |
reed@google.com | 26344cf | 2012-06-27 18:23:01 +0000 | [diff] [blame] | 464 | void SkGlyphCache_Globals::purgeAll() { |
| 465 | SkAutoMutexAcquire ac(fMutex); |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 466 | this->internalPurge(fTotalMemoryUsed); |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 467 | } |
| 468 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 469 | /* This guy calls the visitor from within the mutext lock, so the visitor |
| 470 | cannot: |
| 471 | - take too much time |
| 472 | - try to acquire the mutext again |
| 473 | - call a fontscaler (which might call into the cache) |
| 474 | */ |
reed@google.com | 90808e8 | 2013-03-19 14:44:54 +0000 | [diff] [blame] | 475 | SkGlyphCache* SkGlyphCache::VisitCache(SkTypeface* typeface, |
| 476 | const SkDescriptor* desc, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 477 | bool (*proc)(const SkGlyphCache*, void*), |
| 478 | void* context) { |
reed@google.com | 90808e8 | 2013-03-19 14:44:54 +0000 | [diff] [blame] | 479 | if (!typeface) { |
| 480 | typeface = SkTypeface::GetDefaultTypeface(); |
| 481 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 482 | SkASSERT(desc); |
| 483 | |
reed@google.com | 5d248bc | 2011-11-17 21:49:02 +0000 | [diff] [blame] | 484 | SkGlyphCache_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 485 | SkAutoMutexAcquire ac(globals.fMutex); |
| 486 | SkGlyphCache* cache; |
| 487 | bool insideMutex = true; |
| 488 | |
| 489 | globals.validate(); |
| 490 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 491 | for (cache = globals.internalGetHead(); cache != NULL; cache = cache->fNext) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 492 | if (cache->fDesc->equals(*desc)) { |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 493 | globals.internalDetachCache(cache); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 494 | goto FOUND_IT; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* Release the mutex now, before we create a new entry (which might have |
| 499 | side-effects like trying to access the cache/mutex (yikes!) |
| 500 | */ |
| 501 | ac.release(); // release the mutex now |
| 502 | insideMutex = false; // can't use globals anymore |
| 503 | |
reed@google.com | 4b60dbe | 2013-07-09 15:29:32 +0000 | [diff] [blame] | 504 | // Check if we can create a scaler-context before creating the glyphcache. |
| 505 | // If not, we may have exhausted OS/font resources, so try purging the |
| 506 | // cache once and try again. |
| 507 | { |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 508 | // pass true the first time, to notice if the scalercontext failed, |
| 509 | // so we can try the purge. |
| 510 | SkScalerContext* ctx = typeface->createScalerContext(desc, true); |
reed@google.com | 4b60dbe | 2013-07-09 15:29:32 +0000 | [diff] [blame] | 511 | if (!ctx) { |
| 512 | getSharedGlobals().purgeAll(); |
reed@google.com | 84e22d8 | 2013-07-10 15:38:20 +0000 | [diff] [blame] | 513 | ctx = typeface->createScalerContext(desc, false); |
| 514 | SkASSERT(ctx); |
reed@google.com | 4b60dbe | 2013-07-09 15:29:32 +0000 | [diff] [blame] | 515 | } |
| 516 | cache = SkNEW_ARGS(SkGlyphCache, (typeface, desc, ctx)); |
| 517 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 518 | |
| 519 | FOUND_IT: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 520 | |
| 521 | AutoValidate av(cache); |
| 522 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 523 | if (!proc(cache, context)) { // need to reattach |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 524 | if (insideMutex) { |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 525 | globals.internalAttachCacheToHead(cache); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | } else { |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 527 | globals.attachCacheToHead(cache); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 528 | } |
| 529 | cache = NULL; |
| 530 | } |
| 531 | return cache; |
| 532 | } |
| 533 | |
| 534 | void SkGlyphCache::AttachCache(SkGlyphCache* cache) { |
| 535 | SkASSERT(cache); |
| 536 | SkASSERT(cache->fNext == NULL); |
| 537 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 538 | getGlobals().attachCacheToHead(cache); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 539 | } |
| 540 | |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 541 | void SkGlyphCache::Dump() { |
| 542 | SkGlyphCache_Globals& globals = getGlobals(); |
| 543 | SkAutoMutexAcquire ac(globals.fMutex); |
| 544 | SkGlyphCache* cache; |
| 545 | |
| 546 | globals.validate(); |
| 547 | |
| 548 | SkDebugf("SkGlyphCache strikes:%d memory:%d\n", |
| 549 | globals.getCacheCountUsed(), (int)globals.getTotalMemoryUsed()); |
| 550 | |
| 551 | #ifdef SK_GLYPHCACHE_TRACK_HASH_STATS |
| 552 | int hitCount = 0; |
| 553 | int missCount = 0; |
| 554 | #endif |
| 555 | |
| 556 | for (cache = globals.internalGetHead(); cache != NULL; cache = cache->fNext) { |
| 557 | #ifdef SK_GLYPHCACHE_TRACK_HASH_STATS |
| 558 | hitCount += cache->fHashHitCount; |
| 559 | missCount += cache->fHashMissCount; |
| 560 | #endif |
| 561 | cache->dump(); |
| 562 | } |
| 563 | #ifdef SK_GLYPHCACHE_TRACK_HASH_STATS |
| 564 | SkDebugf("Hash hit percent:%2d\n", 100 * hitCount / (hitCount + missCount)); |
| 565 | #endif |
| 566 | } |
| 567 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 568 | /////////////////////////////////////////////////////////////////////////////// |
| 569 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 570 | void SkGlyphCache_Globals::attachCacheToHead(SkGlyphCache* cache) { |
| 571 | SkAutoMutexAcquire ac(fMutex); |
| 572 | |
| 573 | this->validate(); |
| 574 | cache->validate(); |
| 575 | |
| 576 | this->internalAttachCacheToHead(cache); |
| 577 | this->internalPurge(); |
| 578 | } |
| 579 | |
| 580 | SkGlyphCache* SkGlyphCache_Globals::internalGetTail() const { |
| 581 | SkGlyphCache* cache = fHead; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 582 | if (cache) { |
| 583 | while (cache->fNext) { |
| 584 | cache = cache->fNext; |
| 585 | } |
| 586 | } |
| 587 | return cache; |
| 588 | } |
| 589 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 590 | size_t SkGlyphCache_Globals::internalPurge(size_t minBytesNeeded) { |
| 591 | this->validate(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 592 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 593 | size_t bytesNeeded = 0; |
| 594 | if (fTotalMemoryUsed > fCacheSizeLimit) { |
| 595 | bytesNeeded = fTotalMemoryUsed - fCacheSizeLimit; |
| 596 | } |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 597 | bytesNeeded = SkTMax(bytesNeeded, minBytesNeeded); |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 598 | if (bytesNeeded) { |
| 599 | // no small purges! |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 600 | bytesNeeded = SkTMax(bytesNeeded, fTotalMemoryUsed >> 2); |
reed@google.com | eb9a9bf | 2012-04-23 13:43:30 +0000 | [diff] [blame] | 601 | } |
| 602 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 603 | int countNeeded = 0; |
| 604 | if (fCacheCount > fCacheCountLimit) { |
| 605 | countNeeded = fCacheCount - fCacheCountLimit; |
| 606 | // no small purges! |
| 607 | countNeeded = SkMax32(countNeeded, fCacheCount >> 2); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 608 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 609 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 610 | // early exit |
| 611 | if (!countNeeded && !bytesNeeded) { |
| 612 | return 0; |
| 613 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 614 | |
| 615 | size_t bytesFreed = 0; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 616 | int countFreed = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 617 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 618 | // we start at the tail and proceed backwards, as the linklist is in LRU |
| 619 | // order, with unimportant entries at the tail. |
| 620 | SkGlyphCache* cache = this->internalGetTail(); |
| 621 | while (cache != NULL && |
| 622 | (bytesFreed < bytesNeeded || countFreed < countNeeded)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 623 | SkGlyphCache* prev = cache->fPrev; |
| 624 | bytesFreed += cache->fMemoryUsed; |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 625 | countFreed += 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 626 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 627 | this->internalDetachCache(cache); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 628 | SkDELETE(cache); |
| 629 | cache = prev; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 630 | } |
| 631 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 632 | this->validate(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 633 | |
| 634 | #ifdef SPEW_PURGE_STATUS |
commit-bot@chromium.org | 261d015 | 2014-05-12 15:48:38 +0000 | [diff] [blame] | 635 | if (countFreed) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 636 | SkDebugf("purging %dK from font cache [%d entries]\n", |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 637 | (int)(bytesFreed >> 10), countFreed); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 638 | } |
| 639 | #endif |
| 640 | |
| 641 | return bytesFreed; |
| 642 | } |
| 643 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 644 | void SkGlyphCache_Globals::internalAttachCacheToHead(SkGlyphCache* cache) { |
| 645 | SkASSERT(NULL == cache->fPrev && NULL == cache->fNext); |
| 646 | if (fHead) { |
| 647 | fHead->fPrev = cache; |
| 648 | cache->fNext = fHead; |
| 649 | } |
| 650 | fHead = cache; |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 651 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 652 | fCacheCount += 1; |
| 653 | fTotalMemoryUsed += cache->fMemoryUsed; |
| 654 | } |
| 655 | |
| 656 | void SkGlyphCache_Globals::internalDetachCache(SkGlyphCache* cache) { |
| 657 | SkASSERT(fCacheCount > 0); |
| 658 | fCacheCount -= 1; |
| 659 | fTotalMemoryUsed -= cache->fMemoryUsed; |
| 660 | |
| 661 | if (cache->fPrev) { |
| 662 | cache->fPrev->fNext = cache->fNext; |
| 663 | } else { |
| 664 | fHead = cache->fNext; |
| 665 | } |
| 666 | if (cache->fNext) { |
| 667 | cache->fNext->fPrev = cache->fPrev; |
| 668 | } |
| 669 | cache->fPrev = cache->fNext = NULL; |
| 670 | } |
| 671 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 672 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 673 | |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 674 | #ifdef SK_DEBUG |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 675 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 676 | void SkGlyphCache::validate() const { |
djsollen@google.com | 000dea7 | 2012-11-30 16:19:32 +0000 | [diff] [blame] | 677 | #ifdef SK_DEBUG_GLYPH_CACHE |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 678 | int count = fGlyphArray.count(); |
| 679 | for (int i = 0; i < count; i++) { |
| 680 | const SkGlyph* glyph = fGlyphArray[i]; |
| 681 | SkASSERT(glyph); |
| 682 | SkASSERT(fGlyphAlloc.contains(glyph)); |
| 683 | if (glyph->fImage) { |
reed@google.com | 6757a3c | 2013-06-19 19:25:36 +0000 | [diff] [blame] | 684 | SkASSERT(fGlyphAlloc.contains(glyph->fImage)); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 685 | } |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 686 | if (glyph->fDistanceField) { |
| 687 | SkASSERT(fGlyphAlloc.contains(glyph->fDistanceField)); |
| 688 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 689 | } |
djsollen@google.com | 000dea7 | 2012-11-30 16:19:32 +0000 | [diff] [blame] | 690 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 691 | } |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 692 | |
| 693 | void SkGlyphCache_Globals::validate() const { |
| 694 | size_t computedBytes = 0; |
| 695 | int computedCount = 0; |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 696 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 697 | const SkGlyphCache* head = fHead; |
| 698 | while (head != NULL) { |
| 699 | computedBytes += head->fMemoryUsed; |
| 700 | computedCount += 1; |
| 701 | head = head->fNext; |
| 702 | } |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 703 | |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 704 | SkASSERT(fTotalMemoryUsed == computedBytes); |
| 705 | SkASSERT(fCacheCount == computedCount); |
| 706 | } |
| 707 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 708 | #endif |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 709 | |
| 710 | /////////////////////////////////////////////////////////////////////////////// |
| 711 | /////////////////////////////////////////////////////////////////////////////// |
| 712 | |
| 713 | #include "SkTypefaceCache.h" |
| 714 | |
| 715 | size_t SkGraphics::GetFontCacheLimit() { |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 716 | return getSharedGlobals().getCacheSizeLimit(); |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | size_t SkGraphics::SetFontCacheLimit(size_t bytes) { |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 720 | return getSharedGlobals().setCacheSizeLimit(bytes); |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 721 | } |
| 722 | |
reed@google.com | 79a1c34ee | 2012-07-30 13:08:01 +0000 | [diff] [blame] | 723 | size_t SkGraphics::GetFontCacheUsed() { |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 724 | return getSharedGlobals().getTotalMemoryUsed(); |
| 725 | } |
| 726 | |
| 727 | int SkGraphics::GetFontCacheCountLimit() { |
| 728 | return getSharedGlobals().getCacheCountLimit(); |
| 729 | } |
| 730 | |
| 731 | int SkGraphics::SetFontCacheCountLimit(int count) { |
| 732 | return getSharedGlobals().setCacheCountLimit(count); |
| 733 | } |
| 734 | |
| 735 | int SkGraphics::GetFontCacheCountUsed() { |
| 736 | return getSharedGlobals().getCacheCountUsed(); |
reed@google.com | 79a1c34ee | 2012-07-30 13:08:01 +0000 | [diff] [blame] | 737 | } |
| 738 | |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 739 | void SkGraphics::PurgeFontCache() { |
reed@google.com | 26344cf | 2012-06-27 18:23:01 +0000 | [diff] [blame] | 740 | getSharedGlobals().purgeAll(); |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 741 | SkTypefaceCache::PurgeAll(); |
| 742 | } |
| 743 | |
reed@google.com | 6172d67 | 2012-05-17 13:38:03 +0000 | [diff] [blame] | 744 | size_t SkGraphics::GetTLSFontCacheLimit() { |
| 745 | const SkGlyphCache_Globals* tls = SkGlyphCache_Globals::FindTLS(); |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 746 | return tls ? tls->getCacheSizeLimit() : 0; |
reed@google.com | 6172d67 | 2012-05-17 13:38:03 +0000 | [diff] [blame] | 747 | } |
| 748 | |
reed@google.com | 803c67d | 2012-05-17 13:50:36 +0000 | [diff] [blame] | 749 | void SkGraphics::SetTLSFontCacheLimit(size_t bytes) { |
reed@google.com | 6172d67 | 2012-05-17 13:38:03 +0000 | [diff] [blame] | 750 | if (0 == bytes) { |
| 751 | SkGlyphCache_Globals::DeleteTLS(); |
| 752 | } else { |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 753 | SkGlyphCache_Globals::GetTLS().setCacheSizeLimit(bytes); |
reed@google.com | 6172d67 | 2012-05-17 13:38:03 +0000 | [diff] [blame] | 754 | } |
| 755 | } |