Flutter Engine
The Flutter Engine
Classes | Public Member Functions | List of all members
skia::textlayout::ParagraphCache Class Reference

#include <ParagraphCache.h>

Classes

struct  Entry
 

Public Member Functions

 ParagraphCache ()
 
 ~ParagraphCache ()
 
void abandon ()
 
void reset ()
 
bool updateParagraph (ParagraphImpl *paragraph)
 
bool findParagraph (ParagraphImpl *paragraph)
 
void setChecker (std::function< void(ParagraphImpl *impl, const char *, bool)> checker)
 
void printStatistics ()
 
void turnOn (bool value)
 
int count ()
 
bool isPossiblyTextEditing (ParagraphImpl *paragraph)
 

Detailed Description

Definition at line 18 of file ParagraphCache.h.

Constructor & Destructor Documentation

◆ ParagraphCache()

skia::textlayout::ParagraphCache::ParagraphCache ( )

Definition at line 241 of file ParagraphCache.cpp.

242 : fChecker([](ParagraphImpl* impl, const char*, bool){ })
243 , fLRUCacheMap(kMaxEntries)
244 , fCacheIsOn(true)
245 , fLastCachedValue(nullptr)
246#ifdef PARAGRAPH_CACHE_STATS
247 , fTotalRequests(0)
248 , fCacheMisses(0)
249 , fHashMisses(0)
250#endif
251{ }

◆ ~ParagraphCache()

skia::textlayout::ParagraphCache::~ParagraphCache ( )

Definition at line 253 of file ParagraphCache.cpp.

253{ }

Member Function Documentation

◆ abandon()

void skia::textlayout::ParagraphCache::abandon ( )

Definition at line 285 of file ParagraphCache.cpp.

285 {
286 this->reset();
287}

◆ count()

int skia::textlayout::ParagraphCache::count ( )
inline

Definition at line 34 of file ParagraphCache.h.

34{ return fLRUCacheMap.count(); }

◆ findParagraph()

bool skia::textlayout::ParagraphCache::findParagraph ( ParagraphImpl paragraph)

Definition at line 300 of file ParagraphCache.cpp.

300 {
301 if (!fCacheIsOn) {
302 return false;
303 }
304#ifdef PARAGRAPH_CACHE_STATS
305 ++fTotalRequests;
306#endif
307 SkAutoMutexExclusive lock(fParagraphMutex);
308 ParagraphCacheKey key(paragraph);
309 std::unique_ptr<Entry>* entry = fLRUCacheMap.find(key);
310
311 if (!entry) {
312 // We have a cache miss
313#ifdef PARAGRAPH_CACHE_STATS
314 ++fCacheMisses;
315#endif
316 fChecker(paragraph, "missingParagraph", true);
317 return false;
318 }
319 updateTo(paragraph, entry->get());
320 fChecker(paragraph, "foundParagraph", true);
321 return true;
322}

◆ isPossiblyTextEditing()

bool skia::textlayout::ParagraphCache::isPossiblyTextEditing ( ParagraphImpl paragraph)

Definition at line 354 of file ParagraphCache.cpp.

354 {
355 if (fLastCachedValue == nullptr) {
356 return false;
357 }
358
359 auto& lastText = fLastCachedValue->fKey.text();
360 auto& text = paragraph->fText;
361
362 if ((lastText.size() < NOCACHE_PREFIX_LENGTH) || (text.size() < NOCACHE_PREFIX_LENGTH)) {
363 // Either last text or the current are too short
364 return false;
365 }
366
367 if (std::strncmp(lastText.c_str(), text.c_str(), NOCACHE_PREFIX_LENGTH) == 0) {
368 // Texts have the same starts
369 return true;
370 }
371
372 if (std::strncmp(lastText.c_str() + lastText.size() - NOCACHE_PREFIX_LENGTH, &text[text.size() - NOCACHE_PREFIX_LENGTH], NOCACHE_PREFIX_LENGTH) == 0) {
373 // Texts have the same ends
374 return true;
375 }
376
377 // It does not look like editing the text
378 return false;
379}
#define NOCACHE_PREFIX_LENGTH
std::u16string text

◆ printStatistics()

void skia::textlayout::ParagraphCache::printStatistics ( )

Definition at line 275 of file ParagraphCache.cpp.

275 {
276 SkDebugf("--- Paragraph Cache ---\n");
277 SkDebugf("Total requests: %d\n", fTotalRequests);
278 SkDebugf("Cache misses: %d\n", fCacheMisses);
279 SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ? 100.f * fCacheMisses / fTotalRequests : 0.f);
280 int cacheHits = fTotalRequests - fCacheMisses;
281 SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f);
282 SkDebugf("---------------------\n");
283}
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1

◆ reset()

void skia::textlayout::ParagraphCache::reset ( )

Definition at line 289 of file ParagraphCache.cpp.

289 {
290 SkAutoMutexExclusive lock(fParagraphMutex);
291#ifdef PARAGRAPH_CACHE_STATS
292 fTotalRequests = 0;
293 fCacheMisses = 0;
294 fHashMisses = 0;
295#endif
296 fLRUCacheMap.reset();
297 fLastCachedValue = nullptr;
298}

◆ setChecker()

void skia::textlayout::ParagraphCache::setChecker ( std::function< void(ParagraphImpl *impl, const char *, bool)>  checker)
inline

Definition at line 29 of file ParagraphCache.h.

29 {
30 fChecker = std::move(checker);
31 }

◆ turnOn()

void skia::textlayout::ParagraphCache::turnOn ( bool  value)
inline

Definition at line 33 of file ParagraphCache.h.

33{ fCacheIsOn = value; }
uint8_t value

◆ updateParagraph()

bool skia::textlayout::ParagraphCache::updateParagraph ( ParagraphImpl paragraph)

Definition at line 324 of file ParagraphCache.cpp.

324 {
325 if (!fCacheIsOn) {
326 return false;
327 }
328#ifdef PARAGRAPH_CACHE_STATS
329 ++fTotalRequests;
330#endif
331 SkAutoMutexExclusive lock(fParagraphMutex);
332
333 ParagraphCacheKey key(paragraph);
334 std::unique_ptr<Entry>* entry = fLRUCacheMap.find(key);
335 if (!entry) {
336 // isTooMuchMemoryWasted(paragraph) not needed for now
337 if (isPossiblyTextEditing(paragraph)) {
338 // Skip this paragraph
339 return false;
340 }
341 ParagraphCacheValue* value = new ParagraphCacheValue(std::move(key), paragraph);
342 fLRUCacheMap.insert(value->fKey, std::make_unique<Entry>(value));
343 fChecker(paragraph, "addedParagraph", true);
344 fLastCachedValue = value;
345 return true;
346 } else {
347 // We do not have to update the paragraph
348 return false;
349 }
350}
bool isPossiblyTextEditing(ParagraphImpl *paragraph)

The documentation for this class was generated from the following files: