SkStrikeCache.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2010 Google Inc.
  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. #ifndef SkStrikeCache_DEFINED
  8. #define SkStrikeCache_DEFINED
  9. #include <unordered_map>
  10. #include <unordered_set>
  11. #include "include/private/SkSpinlock.h"
  12. #include "include/private/SkTemplates.h"
  13. #include "src/core/SkDescriptor.h"
  14. #include "src/core/SkStrike.h"
  15. class SkTraceMemoryDump;
  16. #ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT
  17. #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT 2048
  18. #endif
  19. #ifndef SK_DEFAULT_FONT_CACHE_LIMIT
  20. #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024)
  21. #endif
  22. #ifndef SK_DEFAULT_FONT_CACHE_POINT_SIZE_LIMIT
  23. #define SK_DEFAULT_FONT_CACHE_POINT_SIZE_LIMIT 256
  24. #endif
  25. ///////////////////////////////////////////////////////////////////////////////
  26. class SkStrikePinner {
  27. public:
  28. virtual ~SkStrikePinner() = default;
  29. virtual bool canDelete() = 0;
  30. };
  31. class SkStrikeCache final : public SkStrikeCacheInterface {
  32. class Node;
  33. public:
  34. SkStrikeCache() = default;
  35. ~SkStrikeCache() override;
  36. class ExclusiveStrikePtr {
  37. public:
  38. explicit ExclusiveStrikePtr(Node*);
  39. ExclusiveStrikePtr();
  40. ExclusiveStrikePtr(const ExclusiveStrikePtr&) = delete;
  41. ExclusiveStrikePtr& operator = (const ExclusiveStrikePtr&) = delete;
  42. ExclusiveStrikePtr(ExclusiveStrikePtr&&);
  43. ExclusiveStrikePtr& operator = (ExclusiveStrikePtr&&);
  44. ~ExclusiveStrikePtr();
  45. SkStrike* get() const;
  46. SkStrike* operator -> () const;
  47. SkStrike& operator * () const;
  48. explicit operator bool () const;
  49. friend bool operator == (const ExclusiveStrikePtr&, const ExclusiveStrikePtr&);
  50. friend bool operator == (const ExclusiveStrikePtr&, decltype(nullptr));
  51. friend bool operator == (decltype(nullptr), const ExclusiveStrikePtr&);
  52. private:
  53. Node* fNode;
  54. };
  55. static SkStrikeCache* GlobalStrikeCache();
  56. ExclusiveStrikePtr findStrikeExclusive(const SkDescriptor&);
  57. ExclusiveStrikePtr createStrikeExclusive(
  58. const SkDescriptor& desc,
  59. std::unique_ptr<SkScalerContext> scaler,
  60. SkFontMetrics* maybeMetrics = nullptr,
  61. std::unique_ptr<SkStrikePinner> = nullptr);
  62. ExclusiveStrikePtr findOrCreateStrikeExclusive(
  63. const SkDescriptor& desc,
  64. const SkScalerContextEffects& effects,
  65. const SkTypeface& typeface);
  66. // Routines to find suitable data when working in a remote cache situation. These are
  67. // suitable as substitutes for similar calls in SkScalerContext.
  68. bool desperationSearchForImage(const SkDescriptor& desc,
  69. SkGlyph* glyph,
  70. SkStrike* targetCache);
  71. bool desperationSearchForPath(const SkDescriptor& desc, SkGlyphID glyphID, SkPath* path);
  72. SkScopedStrike findOrCreateScopedStrike(const SkDescriptor& desc,
  73. const SkScalerContextEffects& effects,
  74. const SkTypeface& typeface) override;
  75. static std::unique_ptr<SkScalerContext> CreateScalerContext(
  76. const SkDescriptor&, const SkScalerContextEffects&, const SkTypeface&);
  77. static void PurgeAll();
  78. static void ValidateGlyphCacheDataSize();
  79. static void Dump();
  80. // Dump memory usage statistics of all the attaches caches in the process using the
  81. // SkTraceMemoryDump interface.
  82. static void DumpMemoryStatistics(SkTraceMemoryDump* dump);
  83. void purgeAll(); // does not change budget
  84. int getCacheCountLimit() const;
  85. int setCacheCountLimit(int limit);
  86. int getCacheCountUsed() const;
  87. size_t getCacheSizeLimit() const;
  88. size_t setCacheSizeLimit(size_t limit);
  89. size_t getTotalMemoryUsed() const;
  90. int getCachePointSizeLimit() const;
  91. int setCachePointSizeLimit(int limit);
  92. #ifdef SK_DEBUG
  93. // A simple accounting of what each glyph cache reports and the strike cache total.
  94. void validate() const SK_REQUIRES(fLock);
  95. // Make sure that each glyph cache's memory tracking and actual memory used are in sync.
  96. void validateGlyphCacheDataSize() const;
  97. #else
  98. void validate() const {}
  99. void validateGlyphCacheDataSize() const {}
  100. #endif
  101. private:
  102. Node* findAndDetachStrike(const SkDescriptor&);
  103. Node* createStrike(
  104. const SkDescriptor& desc,
  105. std::unique_ptr<SkScalerContext> scaler,
  106. SkFontMetrics* maybeMetrics = nullptr,
  107. std::unique_ptr<SkStrikePinner> = nullptr);
  108. Node* findOrCreateStrike(
  109. const SkDescriptor& desc,
  110. const SkScalerContextEffects& effects,
  111. const SkTypeface& typeface);
  112. void attachNode(Node* node);
  113. // The following methods can only be called when mutex is already held.
  114. Node* internalGetHead() const SK_REQUIRES(fLock) { return fHead; }
  115. Node* internalGetTail() const SK_REQUIRES(fLock) { return fTail; }
  116. void internalDetachCache(Node*) SK_REQUIRES(fLock);
  117. void internalAttachToHead(Node*) SK_REQUIRES(fLock);
  118. // Checkout budgets, modulated by the specified min-bytes-needed-to-purge,
  119. // and attempt to purge caches to match.
  120. // Returns number of bytes freed.
  121. size_t internalPurge(size_t minBytesNeeded = 0) SK_REQUIRES(fLock);
  122. void forEachStrike(std::function<void(const SkStrike&)> visitor) const;
  123. mutable SkSpinlock fLock;
  124. Node* fHead SK_GUARDED_BY(fLock) {nullptr};
  125. Node* fTail SK_GUARDED_BY(fLock) {nullptr};
  126. size_t fTotalMemoryUsed{0};
  127. size_t fCacheSizeLimit{SK_DEFAULT_FONT_CACHE_LIMIT};
  128. int32_t fCacheCountLimit{SK_DEFAULT_FONT_CACHE_COUNT_LIMIT};
  129. int32_t fCacheCount{0};
  130. int32_t fPointSizeLimit{SK_DEFAULT_FONT_CACHE_POINT_SIZE_LIMIT};
  131. };
  132. using SkExclusiveStrikePtr = SkStrikeCache::ExclusiveStrikePtr;
  133. #endif // SkStrikeCache_DEFINED