b7b0b3ad53
That makes layout phase 10 times faster (since the shaping takes 90% of it). LRU cache is attached to the FontCollection object and has the same life time. Currently it has hardcoded limit on the entry numbers (128). One the number reached, the least recently used element is removed from the cache to free the space for a new one. Change-Id: I597e334422614e33715d7a9ed13acf7b1f9cd0e4 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/230755 Commit-Queue: Julia Lavrova <jlavrova@google.com> Reviewed-by: Derek Sollenberger <djsollen@google.com> Reviewed-by: Julia Lavrova <jlavrova@google.com>
86 lines
2.0 KiB
C++
86 lines
2.0 KiB
C++
// Copyright 2019 Google LLC.
|
|
#ifndef ParagraphCache_DEFINED
|
|
#define ParagraphCache_DEFINED
|
|
|
|
#include "include/private/SkMutex.h"
|
|
#include "src/core/SkLRUCache.h"
|
|
|
|
#define PARAGRAPH_CACHE_STATS
|
|
|
|
namespace skia {
|
|
namespace textlayout {
|
|
|
|
struct Measurement {
|
|
SkScalar fAlphabeticBaseline;
|
|
SkScalar fIdeographicBaseline;
|
|
SkScalar fHeight;
|
|
SkScalar fWidth;
|
|
SkScalar fMaxIntrinsicWidth;
|
|
SkScalar fMinIntrinsicWidth;
|
|
};
|
|
|
|
enum InternalState {
|
|
kUnknown = 0,
|
|
kShaped = 1,
|
|
kClusterized = 2,
|
|
kMarked = 3,
|
|
kLineBroken = 4,
|
|
kFormatted = 5,
|
|
kDrawn = 6
|
|
};
|
|
|
|
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(); }
|
|
|
|
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;
|
|
|
|
#ifdef PARAGRAPH_CACHE_STATS
|
|
int fTotalRequests;
|
|
int fCacheMisses;
|
|
int fHashMisses; // cache hit but hash table missed
|
|
#endif
|
|
};
|
|
|
|
} // namespace textlayout
|
|
} // namespace skia
|
|
|
|
#endif // ParagraphCache_DEFINED
|