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