OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkGlyphCache.h" | 8 #include "SkGlyphCache.h" |
9 #include "SkGlyphCache_Globals.h" | 9 #include "SkGlyphCache_Globals.h" |
10 #include "SkGraphics.h" | 10 #include "SkGraphics.h" |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 rec->fProc(rec->fData); | 286 rec->fProc(rec->fData); |
287 AuxProcRec* next = rec->fNext; | 287 AuxProcRec* next = rec->fNext; |
288 delete rec; | 288 delete rec; |
289 rec = next; | 289 rec = next; |
290 } | 290 } |
291 } | 291 } |
292 | 292 |
293 /////////////////////////////////////////////////////////////////////////////// | 293 /////////////////////////////////////////////////////////////////////////////// |
294 /////////////////////////////////////////////////////////////////////////////// | 294 /////////////////////////////////////////////////////////////////////////////// |
295 | 295 |
296 | 296 typedef SkAutoTExclusive<SkSpinlock> Exclusive; |
297 class AutoAcquire { | |
298 public: | |
299 AutoAcquire(SkSpinlock& lock) : fLock(lock) { fLock.acquire(); } | |
300 ~AutoAcquire() { fLock.release(); } | |
301 private: | |
302 SkSpinlock& fLock; | |
303 }; | |
304 | 297 |
305 size_t SkGlyphCache_Globals::setCacheSizeLimit(size_t newLimit) { | 298 size_t SkGlyphCache_Globals::setCacheSizeLimit(size_t newLimit) { |
306 static const size_t minLimit = 256 * 1024; | 299 static const size_t minLimit = 256 * 1024; |
307 if (newLimit < minLimit) { | 300 if (newLimit < minLimit) { |
308 newLimit = minLimit; | 301 newLimit = minLimit; |
309 } | 302 } |
310 | 303 |
311 AutoAcquire ac(fLock); | 304 Exclusive ac(fLock); |
312 | 305 |
313 size_t prevLimit = fCacheSizeLimit; | 306 size_t prevLimit = fCacheSizeLimit; |
314 fCacheSizeLimit = newLimit; | 307 fCacheSizeLimit = newLimit; |
315 this->internalPurge(); | 308 this->internalPurge(); |
316 return prevLimit; | 309 return prevLimit; |
317 } | 310 } |
318 | 311 |
319 int SkGlyphCache_Globals::setCacheCountLimit(int newCount) { | 312 int SkGlyphCache_Globals::setCacheCountLimit(int newCount) { |
320 if (newCount < 0) { | 313 if (newCount < 0) { |
321 newCount = 0; | 314 newCount = 0; |
322 } | 315 } |
323 | 316 |
324 AutoAcquire ac(fLock); | 317 Exclusive ac(fLock); |
325 | 318 |
326 int prevCount = fCacheCountLimit; | 319 int prevCount = fCacheCountLimit; |
327 fCacheCountLimit = newCount; | 320 fCacheCountLimit = newCount; |
328 this->internalPurge(); | 321 this->internalPurge(); |
329 return prevCount; | 322 return prevCount; |
330 } | 323 } |
331 | 324 |
332 void SkGlyphCache_Globals::purgeAll() { | 325 void SkGlyphCache_Globals::purgeAll() { |
333 AutoAcquire ac(fLock); | 326 Exclusive ac(fLock); |
334 this->internalPurge(fTotalMemoryUsed); | 327 this->internalPurge(fTotalMemoryUsed); |
335 } | 328 } |
336 | 329 |
337 /* This guy calls the visitor from within the mutext lock, so the visitor | 330 /* This guy calls the visitor from within the mutext lock, so the visitor |
338 cannot: | 331 cannot: |
339 - take too much time | 332 - take too much time |
340 - try to acquire the mutext again | 333 - try to acquire the mutext again |
341 - call a fontscaler (which might call into the cache) | 334 - call a fontscaler (which might call into the cache) |
342 */ | 335 */ |
343 SkGlyphCache* SkGlyphCache::VisitCache(SkTypeface* typeface, | 336 SkGlyphCache* SkGlyphCache::VisitCache(SkTypeface* typeface, |
344 const SkDescriptor* desc, | 337 const SkDescriptor* desc, |
345 bool (*proc)(const SkGlyphCache*, void*), | 338 bool (*proc)(const SkGlyphCache*, void*), |
346 void* context) { | 339 void* context) { |
347 if (!typeface) { | 340 if (!typeface) { |
348 typeface = SkTypeface::GetDefaultTypeface(); | 341 typeface = SkTypeface::GetDefaultTypeface(); |
349 } | 342 } |
350 SkASSERT(desc); | 343 SkASSERT(desc); |
351 | 344 |
352 SkGlyphCache_Globals& globals = get_globals(); | 345 SkGlyphCache_Globals& globals = get_globals(); |
353 SkGlyphCache* cache; | 346 SkGlyphCache* cache; |
354 | 347 |
355 { | 348 { |
356 AutoAcquire ac(globals.fLock); | 349 Exclusive ac(globals.fLock); |
357 | 350 |
358 globals.validate(); | 351 globals.validate(); |
359 | 352 |
360 for (cache = globals.internalGetHead(); cache != nullptr; cache = cache-
>fNext) { | 353 for (cache = globals.internalGetHead(); cache != nullptr; cache = cache-
>fNext) { |
361 if (cache->fDesc->equals(*desc)) { | 354 if (cache->fDesc->equals(*desc)) { |
362 globals.internalDetachCache(cache); | 355 globals.internalDetachCache(cache); |
363 if (!proc(cache, context)) { | 356 if (!proc(cache, context)) { |
364 globals.internalAttachCacheToHead(cache); | 357 globals.internalAttachCacheToHead(cache); |
365 cache = nullptr; | 358 cache = nullptr; |
366 } | 359 } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 if (dump->getRequestedDetails() == SkTraceMemoryDump::kLight_LevelOfDetail)
{ | 452 if (dump->getRequestedDetails() == SkTraceMemoryDump::kLight_LevelOfDetail)
{ |
460 dump->setMemoryBacking(gGlyphCacheDumpName, "malloc", nullptr); | 453 dump->setMemoryBacking(gGlyphCacheDumpName, "malloc", nullptr); |
461 return; | 454 return; |
462 } | 455 } |
463 | 456 |
464 SkGlyphCache::VisitAll(sk_trace_dump_visitor, dump); | 457 SkGlyphCache::VisitAll(sk_trace_dump_visitor, dump); |
465 } | 458 } |
466 | 459 |
467 void SkGlyphCache::VisitAll(Visitor visitor, void* context) { | 460 void SkGlyphCache::VisitAll(Visitor visitor, void* context) { |
468 SkGlyphCache_Globals& globals = get_globals(); | 461 SkGlyphCache_Globals& globals = get_globals(); |
469 AutoAcquire ac(globals.fLock); | 462 Exclusive ac(globals.fLock); |
470 SkGlyphCache* cache; | 463 SkGlyphCache* cache; |
471 | 464 |
472 globals.validate(); | 465 globals.validate(); |
473 | 466 |
474 for (cache = globals.internalGetHead(); cache != nullptr; cache = cache->fNe
xt) { | 467 for (cache = globals.internalGetHead(); cache != nullptr; cache = cache->fNe
xt) { |
475 visitor(*cache, context); | 468 visitor(*cache, context); |
476 } | 469 } |
477 } | 470 } |
478 | 471 |
479 /////////////////////////////////////////////////////////////////////////////// | 472 /////////////////////////////////////////////////////////////////////////////// |
480 | 473 |
481 void SkGlyphCache_Globals::attachCacheToHead(SkGlyphCache* cache) { | 474 void SkGlyphCache_Globals::attachCacheToHead(SkGlyphCache* cache) { |
482 AutoAcquire ac(fLock); | 475 Exclusive ac(fLock); |
483 | 476 |
484 this->validate(); | 477 this->validate(); |
485 cache->validate(); | 478 cache->validate(); |
486 | 479 |
487 this->internalAttachCacheToHead(cache); | 480 this->internalAttachCacheToHead(cache); |
488 this->internalPurge(); | 481 this->internalPurge(); |
489 } | 482 } |
490 | 483 |
491 SkGlyphCache* SkGlyphCache_Globals::internalGetTail() const { | 484 SkGlyphCache* SkGlyphCache_Globals::internalGetTail() const { |
492 SkGlyphCache* cache = fHead; | 485 SkGlyphCache* cache = fHead; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
646 } | 639 } |
647 | 640 |
648 void SkGraphics::PurgeFontCache() { | 641 void SkGraphics::PurgeFontCache() { |
649 get_globals().purgeAll(); | 642 get_globals().purgeAll(); |
650 SkTypefaceCache::PurgeAll(); | 643 SkTypefaceCache::PurgeAll(); |
651 } | 644 } |
652 | 645 |
653 // TODO(herb): clean up TLS apis. | 646 // TODO(herb): clean up TLS apis. |
654 size_t SkGraphics::GetTLSFontCacheLimit() { return 0; } | 647 size_t SkGraphics::GetTLSFontCacheLimit() { return 0; } |
655 void SkGraphics::SetTLSFontCacheLimit(size_t bytes) { } | 648 void SkGraphics::SetTLSFontCacheLimit(size_t bytes) { } |
OLD | NEW |