2020-03-27 19:40:37 +00:00
|
|
|
// Copyright 2019 Google LLC.
|
2019-07-30 17:32:08 +00:00
|
|
|
#ifndef ParagraphCache_DEFINED
|
|
|
|
#define ParagraphCache_DEFINED
|
|
|
|
|
|
|
|
#include "include/private/SkMutex.h"
|
|
|
|
#include "src/core/SkLRUCache.h"
|
2019-08-27 13:12:15 +00:00
|
|
|
#include <functional> // std::function
|
2019-07-30 17:32:08 +00:00
|
|
|
|
|
|
|
#define PARAGRAPH_CACHE_STATS
|
|
|
|
|
|
|
|
namespace skia {
|
|
|
|
namespace textlayout {
|
|
|
|
|
|
|
|
enum InternalState {
|
|
|
|
kUnknown = 0,
|
2020-05-27 14:38:35 +00:00
|
|
|
kShaped = 2,
|
|
|
|
kClusterized = 3,
|
|
|
|
kMarked = 4,
|
|
|
|
kLineBroken = 5,
|
|
|
|
kFormatted = 6,
|
|
|
|
kDrawn = 7
|
2019-07-30 17:32:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ParagraphImpl;
|
|
|
|
class ParagraphCacheKey;
|
|
|
|
class ParagraphCacheValue;
|
|
|
|
|
|
|
|
bool operator==(const ParagraphCacheKey& a, const ParagraphCacheKey& b);
|
|
|
|
|
|
|
|
class ParagraphCache {
|
|
|
|
public:
|
|
|
|
ParagraphCache();
|
|
|
|
~ParagraphCache();
|
|
|
|
|
|
|
|
void abandon();
|
|
|
|
void reset();
|
|
|
|
bool updateParagraph(ParagraphImpl* paragraph);
|
|
|
|
bool findParagraph(ParagraphImpl* paragraph);
|
|
|
|
|
|
|
|
// For testing
|
|
|
|
void setChecker(std::function<void(ParagraphImpl* impl, const char*, bool)> checker) {
|
|
|
|
fChecker = std::move(checker);
|
|
|
|
}
|
|
|
|
void printStatistics();
|
|
|
|
void turnOn(bool value) { fCacheIsOn = value; }
|
|
|
|
int count() { return fLRUCacheMap.count(); }
|
|
|
|
|
2020-11-17 19:42:46 +00:00
|
|
|
bool isPossiblyTextEditing(ParagraphImpl* paragraph);
|
|
|
|
|
2019-07-30 17:32:08 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
struct Entry;
|
|
|
|
void updateFrom(const ParagraphImpl* paragraph, Entry* entry);
|
|
|
|
void updateTo(ParagraphImpl* paragraph, const Entry* entry);
|
|
|
|
|
|
|
|
mutable SkMutex fParagraphMutex;
|
|
|
|
std::function<void(ParagraphImpl* impl, const char*, bool)> fChecker;
|
|
|
|
|
|
|
|
static const int kMaxEntries = 128;
|
|
|
|
|
|
|
|
struct KeyHash {
|
|
|
|
uint32_t mix(uint32_t hash, uint32_t data) const;
|
|
|
|
uint32_t operator()(const ParagraphCacheKey& key) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
SkLRUCache<ParagraphCacheKey, std::unique_ptr<Entry>, KeyHash> fLRUCacheMap;
|
|
|
|
bool fCacheIsOn;
|
2020-11-17 19:42:46 +00:00
|
|
|
ParagraphCacheValue* fLastCachedValue;
|
2019-07-30 17:32:08 +00:00
|
|
|
|
|
|
|
#ifdef PARAGRAPH_CACHE_STATS
|
|
|
|
int fTotalRequests;
|
|
|
|
int fCacheMisses;
|
|
|
|
int fHashMisses; // cache hit but hash table missed
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace textlayout
|
|
|
|
} // namespace skia
|
|
|
|
|
|
|
|
#endif // ParagraphCache_DEFINED
|