blob: 9ce4ec7af2553c0b291eee588aad44be91f9ffe9 [file] [log] [blame]
joshualittb7133be2015-04-08 09:08:31 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrTextBlobCache_DEFINED
9#define GrTextBlobCache_DEFINED
10
11#include "GrAtlasTextContext.h"
12#include "SkTDynamicHash.h"
13#include "SkTextBlob.h"
14
15class GrTextBlobCache {
16public:
17 typedef GrAtlasTextContext::BitmapTextBlob BitmapTextBlob;
18
joshualitt0db6dfa2015-04-10 07:01:30 -070019 /**
20 * The callback function used by the cache when it is still over budget after a purge. The
21 * passed in 'data' is the same 'data' handed to setOverbudgetCallback.
22 */
23 typedef void (*PFOverBudgetCB)(void* data);
24
25 GrTextBlobCache(PFOverBudgetCB cb, void* data)
26 : fPool(kPreAllocSize, kMinGrowthSize)
27 , fCallback(cb)
28 , fData(data) {
29 SkASSERT(cb && data);
30 }
joshualittb7133be2015-04-08 09:08:31 -070031 ~GrTextBlobCache();
32
33 // creates an uncached blob
34 BitmapTextBlob* createBlob(int glyphCount, int runCount, size_t maxVASize);
35
36 BitmapTextBlob* createCachedBlob(const SkTextBlob* blob, size_t maxVAStride) {
37 int glyphCount = 0;
38 int runCount = 0;
39 BlobGlyphCount(&glyphCount, &runCount, blob);
40 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVAStride);
41 cacheBlob->fUniqueID = blob->uniqueID();
42 this->add(cacheBlob);
43 return cacheBlob;
44 }
45
46 BitmapTextBlob* find(uint32_t uniqueID) {
47 return fCache.find(uniqueID);
48 }
49
50 void remove(BitmapTextBlob* blob) {
51 fCache.remove(blob->fUniqueID);
52 fBlobList.remove(blob);
53 blob->unref();
54 }
55
56 void add(BitmapTextBlob* blob) {
57 fCache.add(blob);
58 fBlobList.addToHead(blob);
59
60 // If we are overbudget, then unref until we are below budget again
61 if (fPool.size() > kBudget) {
62 BitmapBlobList::Iter iter;
63 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart);
64 BitmapTextBlob* lruBlob = iter.get();
65 SkASSERT(lruBlob);
joshualitt0db6dfa2015-04-10 07:01:30 -070066 while (fPool.size() > kBudget && (lruBlob = iter.get()) && lruBlob != blob) {
joshualittb7133be2015-04-08 09:08:31 -070067 fCache.remove(lruBlob->fUniqueID);
68 fBlobList.remove(lruBlob);
joshualittb7133be2015-04-08 09:08:31 -070069 iter.prev();
joshualitt0db6dfa2015-04-10 07:01:30 -070070 lruBlob->unref();
71 }
72
73 // If we break out of the loop with lruBlob == blob, then we haven't purged enough
74 // use the call back and try to free some more. If we are still overbudget after this,
75 // then this single textblob is over our budget
76 if (lruBlob == blob) {
77 (*fCallback)(fData);
78 }
79
80#ifdef SK_DEBUG
81 if (fPool.size() > kBudget) {
82 SkDebugf("Single textblob is larger than our whole budget");
83 }
84#endif
joshualittb7133be2015-04-08 09:08:31 -070085 }
86 }
87
88 void makeMRU(BitmapTextBlob* blob) {
89 if (fBlobList.head() == blob) {
90 return;
91 }
92
93 fBlobList.remove(blob);
94 fBlobList.addToHead(blob);
95 }
96
97private:
98 // TODO move to SkTextBlob
99 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) {
100 SkTextBlob::RunIterator itCounter(blob);
101 for (; !itCounter.done(); itCounter.next(), (*runCount)++) {
102 *glyphCount += itCounter.glyphCount();
103 }
104 }
105
106 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList;
107
108 // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes in the pool are
109 // based off of the largest cached textblob I have seen in the skps(a couple of kilobytes).
110 static const int kPreAllocSize = 1 << 17;
111 static const int kMinGrowthSize = 1 << 17;
joshualitt0db6dfa2015-04-10 07:01:30 -0700112 static const int kBudget = 1 << 22;
joshualittb7133be2015-04-08 09:08:31 -0700113 BitmapBlobList fBlobList;
114 SkTDynamicHash<BitmapTextBlob, uint32_t> fCache;
115 GrMemoryPool fPool;
joshualitt0db6dfa2015-04-10 07:01:30 -0700116 PFOverBudgetCB fCallback;
117 void* fData;
joshualittb7133be2015-04-08 09:08:31 -0700118};
119
120#endif