• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC.
2 #ifndef ParagraphCache_DEFINED
3 #define ParagraphCache_DEFINED
4 
5 #include "include/private/base/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 class ParagraphImpl;
15 class ParagraphCacheKey;
16 class ParagraphCacheValue;
17 
18 class ParagraphCache {
19 public:
20     ParagraphCache();
21     ~ParagraphCache();
22 
23     void abandon();
24     void reset();
25     bool updateParagraph(ParagraphImpl* paragraph);
26     bool findParagraph(ParagraphImpl* paragraph);
27 #ifdef ENABLE_TEXT_ENHANCE
28     void SetStoredLayout(ParagraphImpl& paragraph);
29     bool GetStoredLayout(ParagraphImpl& paragraph);
30 #endif
31 
32     // For testing
setChecker(std::function<void (ParagraphImpl * impl,const char *,bool)> checker)33     void setChecker(std::function<void(ParagraphImpl* impl, const char*, bool)> checker) {
34         fChecker = std::move(checker);
35     }
36     void printStatistics();
turnOn(bool value)37     void turnOn(bool value) { fCacheIsOn = value; }
count()38     int count() { return fLRUCacheMap.count(); }
39 
40     bool isPossiblyTextEditing(ParagraphImpl* paragraph);
41 
42  private:
43 
44     struct Entry;
45     void updateFrom(const ParagraphImpl* paragraph, Entry* entry);
46     void updateTo(ParagraphImpl* paragraph, const Entry* entry);
47 
48 #ifdef ENABLE_TEXT_ENHANCE
49     bool useCachedLayout(const ParagraphImpl& paragraph, const ParagraphCacheValue* value);
50     void SetStoredLayoutImpl(ParagraphImpl& paragraph, ParagraphCacheValue* value);
51     ParagraphCacheValue* cacheLayout(ParagraphImpl* paragraph);
52     bool canBeCached(ParagraphImpl* paragraph) const;
53 #endif
54 
55      mutable SkMutex fParagraphMutex;
56      std::function<void(ParagraphImpl* impl, const char*, bool)> fChecker;
57 
58     static const int kMaxEntries = 128;
59 
60     struct KeyHash {
61         uint32_t operator()(const ParagraphCacheKey& key) const;
62     };
63 
64     SkLRUCache<ParagraphCacheKey, std::unique_ptr<Entry>, KeyHash> fLRUCacheMap;
65     bool fCacheIsOn;
66     ParagraphCacheValue* fLastCachedValue;
67 
68 #ifdef PARAGRAPH_CACHE_STATS
69     int fTotalRequests;
70     int fCacheMisses;
71     int fHashMisses; // cache hit but hash table missed
72 #endif
73 };
74 
75 }  // namespace textlayout
76 }  // namespace skia
77 
78 #endif  // ParagraphCache_DEFINED
79