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