| 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 |
|
| 28 |
// For testing |
| 29 |
void setChecker(std::function<void(ParagraphImpl* impl, const char*, bool)> checker) { |
| 30 |
fChecker = std::move(checker); |
| 31 |
} |
| 32 |
void printStatistics(); |
| 33 |
void turnOn(bool value) { fCacheIsOn = value; } |
| 34 |
int count() { return fLRUCacheMap.count(); } |
| 35 |
|
| 36 |
bool isPossiblyTextEditing(ParagraphImpl* paragraph); |
| 37 |
|
| 38 |
private: |
| 39 |
|
| 40 |
struct Entry; |
| 41 |
void updateFrom(const ParagraphImpl* paragraph, Entry* entry); |
| 42 |
void updateTo(ParagraphImpl* paragraph, const Entry* entry); |
| 43 |
|
| 44 |
mutable SkMutex fParagraphMutex; |
| 45 |
std::function<void(ParagraphImpl* impl, const char*, bool)> fChecker; |
| 46 |
|
| 47 |
static const int kMaxEntries = 128; |
| 48 |
|
| 49 |
struct KeyHash { |
| 50 |
uint32_t operator()(const ParagraphCacheKey& key) const; |
| 51 |
}; |
| 52 |
|
| 53 |
SkLRUCache<ParagraphCacheKey, std::unique_ptr<Entry>, KeyHash> fLRUCacheMap; |
| 54 |
bool fCacheIsOn; |
| 55 |
ParagraphCacheValue* fLastCachedValue; |
| 56 |
|
| 57 |
#ifdef PARAGRAPH_CACHE_STATS |
| 58 |
int fTotalRequests; |
| 59 |
int fCacheMisses; |
| 60 |
int fHashMisses; // cache hit but hash table missed |
| 61 |
#endif |
| 62 |
}; |
| 63 |
|
| 64 |
} // namespace textlayout |
| 65 |
} // namespace skia |
| 66 |
|
| 67 |
#endif // ParagraphCache_DEFINED |