• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 bool operator==(const ParagraphCacheKey& a, const ParagraphCacheKey& b);
29 
30 class ParagraphCache {
31 public:
32     ParagraphCache();
33     ~ParagraphCache();
34 
35     void abandon();
36     void reset();
37     bool updateParagraph(ParagraphImpl* paragraph);
38     bool findParagraph(ParagraphImpl* paragraph);
39 
40     // For testing
setChecker(std::function<void (ParagraphImpl * impl,const char *,bool)> checker)41     void setChecker(std::function<void(ParagraphImpl* impl, const char*, bool)> checker) {
42         fChecker = std::move(checker);
43     }
44     void printStatistics();
turnOn(bool value)45     void turnOn(bool value) { fCacheIsOn = value; }
count()46     int count() { return fLRUCacheMap.count(); }
47 
48     bool isPossiblyTextEditing(ParagraphImpl* paragraph);
49 
50  private:
51 
52     struct Entry;
53     void updateFrom(const ParagraphImpl* paragraph, Entry* entry);
54     void updateTo(ParagraphImpl* paragraph, const Entry* entry);
55 
56      mutable SkMutex fParagraphMutex;
57      std::function<void(ParagraphImpl* impl, const char*, bool)> fChecker;
58 
59     static const int kMaxEntries = 128;
60 
61     struct KeyHash {
62         uint32_t mix(uint32_t hash, uint32_t data) const;
63         uint32_t operator()(const ParagraphCacheKey& key) const;
64     };
65 
66     SkLRUCache<ParagraphCacheKey, std::unique_ptr<Entry>, KeyHash> fLRUCacheMap;
67     bool fCacheIsOn;
68     ParagraphCacheValue* fLastCachedValue;
69 
70 #ifdef PARAGRAPH_CACHE_STATS
71     int fTotalRequests;
72     int fCacheMisses;
73     int fHashMisses; // cache hit but hash table missed
74 #endif
75 };
76 
77 }  // namespace textlayout
78 }  // namespace skia
79 
80 #endif  // ParagraphCache_DEFINED
81