1 // Copyright 2019 Google LLC. 2 #ifndef ParagraphCache_DEFINED 3 #define ParagraphCache_DEFINED 4 5 #include "include/private/SkMutex.h" 6 #include "src/core/SkLRUCache.h" 7 #include <functional> // std::function 8 9 #define PARAGRAPH_CACHE_STATS 10 11 namespace skia { 12 namespace textlayout { 13 14 enum InternalState { 15 kUnknown = 0, 16 kShaped = 2, 17 kClusterized = 3, 18 kMarked = 4, 19 kLineBroken = 5, 20 kFormatted = 6, 21 kDrawn = 7 22 }; 23 24 class ParagraphImpl; 25 class ParagraphCacheKey; 26 class ParagraphCacheValue; 27 28 class ParagraphCache { 29 public: 30 ParagraphCache(); 31 ~ParagraphCache(); 32 33 void abandon(); 34 void reset(); 35 bool updateParagraph(ParagraphImpl* paragraph); 36 bool findParagraph(ParagraphImpl* paragraph); 37 38 // For testing setChecker(std::function<void (ParagraphImpl * impl,const char *,bool)> checker)39 void setChecker(std::function<void(ParagraphImpl* impl, const char*, bool)> checker) { 40 fChecker = std::move(checker); 41 } 42 void printStatistics(); turnOn(bool value)43 void turnOn(bool value) { fCacheIsOn = value; } count()44 int count() { return fLRUCacheMap.count(); } 45 46 bool isPossiblyTextEditing(ParagraphImpl* paragraph); 47 48 private: 49 50 struct Entry; 51 void updateFrom(const ParagraphImpl* paragraph, Entry* entry); 52 void updateTo(ParagraphImpl* paragraph, const Entry* entry); 53 54 mutable SkMutex fParagraphMutex; 55 std::function<void(ParagraphImpl* impl, const char*, bool)> fChecker; 56 57 static const int kMaxEntries = 128; 58 59 struct KeyHash { 60 uint32_t operator()(const ParagraphCacheKey& key) const; 61 }; 62 63 SkLRUCache<ParagraphCacheKey, std::unique_ptr<Entry>, KeyHash> fLRUCacheMap; 64 bool fCacheIsOn; 65 ParagraphCacheValue* fLastCachedValue; 66 67 #ifdef PARAGRAPH_CACHE_STATS 68 int fTotalRequests; 69 int fCacheMisses; 70 int fHashMisses; // cache hit but hash table missed 71 #endif 72 }; 73 74 } // namespace textlayout 75 } // namespace skia 76 77 #endif // ParagraphCache_DEFINED 78