blob: 1b5e5b8803865c8f92e6c1d11bca6a670f62572c [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);
joshualittf6e97e62015-04-10 07:23:29 -070068
69 // Backup the iterator before removing and unrefing the blob
joshualittb7133be2015-04-08 09:08:31 -070070 iter.prev();
joshualittf6e97e62015-04-10 07:23:29 -070071 fBlobList.remove(lruBlob);
joshualitt0db6dfa2015-04-10 07:01:30 -070072 lruBlob->unref();
73 }
74
75 // If we break out of the loop with lruBlob == blob, then we haven't purged enough
76 // use the call back and try to free some more. If we are still overbudget after this,
77 // then this single textblob is over our budget
78 if (lruBlob == blob) {
79 (*fCallback)(fData);
80 }
81
82#ifdef SK_DEBUG
83 if (fPool.size() > kBudget) {
84 SkDebugf("Single textblob is larger than our whole budget");
85 }
86#endif
joshualittb7133be2015-04-08 09:08:31 -070087 }
88 }
89
90 void makeMRU(BitmapTextBlob* blob) {
91 if (fBlobList.head() == blob) {
92 return;
93 }
94
95 fBlobList.remove(blob);
96 fBlobList.addToHead(blob);
97 }
98
99private:
100 // TODO move to SkTextBlob
101 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) {
102 SkTextBlob::RunIterator itCounter(blob);
103 for (; !itCounter.done(); itCounter.next(), (*runCount)++) {
104 *glyphCount += itCounter.glyphCount();
105 }
106 }
107
108 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList;
109
110 // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes in the pool are
111 // based off of the largest cached textblob I have seen in the skps(a couple of kilobytes).
112 static const int kPreAllocSize = 1 << 17;
113 static const int kMinGrowthSize = 1 << 17;
joshualitt0db6dfa2015-04-10 07:01:30 -0700114 static const int kBudget = 1 << 22;
joshualittb7133be2015-04-08 09:08:31 -0700115 BitmapBlobList fBlobList;
116 SkTDynamicHash<BitmapTextBlob, uint32_t> fCache;
117 GrMemoryPool fPool;
joshualitt0db6dfa2015-04-10 07:01:30 -0700118 PFOverBudgetCB fCallback;
119 void* fData;
joshualittb7133be2015-04-08 09:08:31 -0700120};
121
122#endif