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