blob: 0d0320e00b81b06294b25fb2c1c0373aed01a813 [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);
joshualitt2a0e9f32015-04-13 06:12:21 -070035 BitmapTextBlob* createBlob(const SkTextBlob* blob, size_t maxVAStride) {
36 int glyphCount = 0;
37 int runCount = 0;
38 BlobGlyphCount(&glyphCount, &runCount, blob);
39 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVAStride);
40 return cacheBlob;
41 }
joshualittb7133be2015-04-08 09:08:31 -070042
43 BitmapTextBlob* createCachedBlob(const SkTextBlob* blob, size_t maxVAStride) {
44 int glyphCount = 0;
45 int runCount = 0;
46 BlobGlyphCount(&glyphCount, &runCount, blob);
47 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVAStride);
48 cacheBlob->fUniqueID = blob->uniqueID();
49 this->add(cacheBlob);
50 return cacheBlob;
51 }
52
53 BitmapTextBlob* find(uint32_t uniqueID) {
54 return fCache.find(uniqueID);
55 }
56
57 void remove(BitmapTextBlob* blob) {
58 fCache.remove(blob->fUniqueID);
59 fBlobList.remove(blob);
60 blob->unref();
61 }
62
63 void add(BitmapTextBlob* blob) {
64 fCache.add(blob);
65 fBlobList.addToHead(blob);
66
67 // If we are overbudget, then unref until we are below budget again
68 if (fPool.size() > kBudget) {
69 BitmapBlobList::Iter iter;
70 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart);
71 BitmapTextBlob* lruBlob = iter.get();
72 SkASSERT(lruBlob);
joshualitt0db6dfa2015-04-10 07:01:30 -070073 while (fPool.size() > kBudget && (lruBlob = iter.get()) && lruBlob != blob) {
joshualittb7133be2015-04-08 09:08:31 -070074 fCache.remove(lruBlob->fUniqueID);
joshualittf6e97e62015-04-10 07:23:29 -070075
76 // Backup the iterator before removing and unrefing the blob
joshualittb7133be2015-04-08 09:08:31 -070077 iter.prev();
joshualittf6e97e62015-04-10 07:23:29 -070078 fBlobList.remove(lruBlob);
joshualitt0db6dfa2015-04-10 07:01:30 -070079 lruBlob->unref();
80 }
81
82 // If we break out of the loop with lruBlob == blob, then we haven't purged enough
83 // use the call back and try to free some more. If we are still overbudget after this,
84 // then this single textblob is over our budget
85 if (lruBlob == blob) {
86 (*fCallback)(fData);
87 }
88
89#ifdef SK_DEBUG
90 if (fPool.size() > kBudget) {
91 SkDebugf("Single textblob is larger than our whole budget");
92 }
93#endif
joshualittb7133be2015-04-08 09:08:31 -070094 }
95 }
96
97 void makeMRU(BitmapTextBlob* blob) {
98 if (fBlobList.head() == blob) {
99 return;
100 }
101
102 fBlobList.remove(blob);
103 fBlobList.addToHead(blob);
104 }
105
106private:
107 // TODO move to SkTextBlob
108 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) {
109 SkTextBlob::RunIterator itCounter(blob);
110 for (; !itCounter.done(); itCounter.next(), (*runCount)++) {
111 *glyphCount += itCounter.glyphCount();
112 }
113 }
114
115 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList;
116
117 // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes in the pool are
118 // based off of the largest cached textblob I have seen in the skps(a couple of kilobytes).
119 static const int kPreAllocSize = 1 << 17;
120 static const int kMinGrowthSize = 1 << 17;
joshualitt0db6dfa2015-04-10 07:01:30 -0700121 static const int kBudget = 1 << 22;
joshualittb7133be2015-04-08 09:08:31 -0700122 BitmapBlobList fBlobList;
123 SkTDynamicHash<BitmapTextBlob, uint32_t> fCache;
124 GrMemoryPool fPool;
joshualitt0db6dfa2015-04-10 07:01:30 -0700125 PFOverBudgetCB fCallback;
126 void* fData;
joshualittb7133be2015-04-08 09:08:31 -0700127};
128
129#endif