OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrTextBlobCache_DEFINED | 8 #ifndef GrTextBlobCache_DEFINED |
9 #define GrTextBlobCache_DEFINED | 9 #define GrTextBlobCache_DEFINED |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 // creates an uncached blob | 22 // creates an uncached blob |
23 BitmapTextBlob* createBlob(int glyphCount, int runCount, size_t maxVASize); | 23 BitmapTextBlob* createBlob(int glyphCount, int runCount, size_t maxVASize); |
24 BitmapTextBlob* createBlob(const SkTextBlob* blob, size_t maxVAStride) { | 24 BitmapTextBlob* createBlob(const SkTextBlob* blob, size_t maxVAStride) { |
25 int glyphCount = 0; | 25 int glyphCount = 0; |
26 int runCount = 0; | 26 int runCount = 0; |
27 BlobGlyphCount(&glyphCount, &runCount, blob); | 27 BlobGlyphCount(&glyphCount, &runCount, blob); |
28 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVA
Stride); | 28 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVA
Stride); |
29 return cacheBlob; | 29 return cacheBlob; |
30 } | 30 } |
31 | 31 |
32 BitmapTextBlob* createCachedBlob(const SkTextBlob* blob, size_t maxVAStride)
{ | 32 BitmapTextBlob* createCachedBlob(const SkTextBlob* blob, |
| 33 BitmapTextBlob::Key& key, |
| 34 size_t maxVAStride) { |
33 int glyphCount = 0; | 35 int glyphCount = 0; |
34 int runCount = 0; | 36 int runCount = 0; |
35 BlobGlyphCount(&glyphCount, &runCount, blob); | 37 BlobGlyphCount(&glyphCount, &runCount, blob); |
36 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVA
Stride); | 38 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVA
Stride); |
37 cacheBlob->fUniqueID = blob->uniqueID(); | 39 cacheBlob->fKey = key; |
38 this->add(cacheBlob); | 40 this->add(cacheBlob); |
39 return cacheBlob; | 41 return cacheBlob; |
40 } | 42 } |
41 | 43 |
42 BitmapTextBlob* find(uint32_t uniqueID) { | 44 BitmapTextBlob* find(const BitmapTextBlob::Key& key) { |
43 return fCache.find(uniqueID); | 45 return fCache.find(key); |
44 } | 46 } |
45 | 47 |
46 void remove(BitmapTextBlob* blob) { | 48 void remove(BitmapTextBlob* blob) { |
47 fCache.remove(blob->fUniqueID); | 49 fCache.remove(blob->fKey); |
48 fBlobList.remove(blob); | 50 fBlobList.remove(blob); |
49 blob->unref(); | 51 blob->unref(); |
50 } | 52 } |
51 | 53 |
52 void add(BitmapTextBlob* blob) { | 54 void add(BitmapTextBlob* blob) { |
53 fCache.add(blob); | 55 fCache.add(blob); |
54 fBlobList.addToHead(blob); | 56 fBlobList.addToHead(blob); |
55 | 57 |
56 // If we are overbudget, then unref until we are below budget again | 58 // If we are overbudget, then unref until we are below budget again |
57 if (fPool.size() > fBudget) { | 59 if (fPool.size() > fBudget) { |
58 BitmapBlobList::Iter iter; | 60 BitmapBlobList::Iter iter; |
59 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); | 61 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); |
60 BitmapTextBlob* lruBlob = iter.get(); | 62 BitmapTextBlob* lruBlob = iter.get(); |
61 SkASSERT(lruBlob); | 63 SkASSERT(lruBlob); |
62 do { | 64 do { |
63 fCache.remove(lruBlob->fUniqueID); | 65 fCache.remove(lruBlob->fKey); |
64 fBlobList.remove(lruBlob); | 66 fBlobList.remove(lruBlob); |
65 lruBlob->unref(); | 67 lruBlob->unref(); |
66 iter.prev(); | 68 iter.prev(); |
67 } while (fPool.size() > fBudget && (lruBlob = iter.get())); | 69 } while (fPool.size() > fBudget && (lruBlob = iter.get())); |
68 } | 70 } |
69 } | 71 } |
70 | 72 |
71 void makeMRU(BitmapTextBlob* blob) { | 73 void makeMRU(BitmapTextBlob* blob) { |
72 if (fBlobList.head() == blob) { | 74 if (fBlobList.head() == blob) { |
73 return; | 75 return; |
74 } | 76 } |
75 | 77 |
76 fBlobList.remove(blob); | 78 fBlobList.remove(blob); |
77 fBlobList.addToHead(blob); | 79 fBlobList.addToHead(blob); |
78 } | 80 } |
79 | 81 |
80 private: | 82 private: |
81 // TODO move to SkTextBlob | 83 // TODO move to SkTextBlob |
82 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob)
{ | 84 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob)
{ |
83 SkTextBlob::RunIterator itCounter(blob); | 85 SkTextBlob::RunIterator itCounter(blob); |
84 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { | 86 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { |
85 *glyphCount += itCounter.glyphCount(); | 87 *glyphCount += itCounter.glyphCount(); |
86 } | 88 } |
87 } | 89 } |
88 | 90 |
89 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList; | 91 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList; |
90 BitmapBlobList fBlobList; | 92 BitmapBlobList fBlobList; |
91 SkTDynamicHash<BitmapTextBlob, uint32_t> fCache; | 93 SkTDynamicHash<BitmapTextBlob, BitmapTextBlob::Key> fCache; |
92 GrMemoryPool fPool; | 94 GrMemoryPool fPool; |
93 size_t fBudget; | 95 size_t fBudget; |
94 }; | 96 }; |
95 | 97 |
96 #endif | 98 #endif |
OLD | NEW |