OLD | NEW |
| (Empty) |
1 | |
2 /* | |
3 * Copyright 2010 Google Inc. | |
4 * | |
5 * Use of this source code is governed by a BSD-style license that can be | |
6 * found in the LICENSE file. | |
7 */ | |
8 | |
9 | |
10 | |
11 #ifndef GrTextStrike_DEFINED | |
12 #define GrTextStrike_DEFINED | |
13 | |
14 #include "GrAtlas.h" | |
15 #include "GrDrawTarget.h" | |
16 #include "GrFontScaler.h" | |
17 #include "GrGlyph.h" | |
18 #include "SkTDynamicHash.h" | |
19 #include "SkVarAlloc.h" | |
20 | |
21 class GrFontCache; | |
22 class GrGpu; | |
23 class GrFontPurgeListener; | |
24 | |
25 /** | |
26 * The textstrike maps a hostfontscaler instance to a dictionary of | |
27 * glyphid->strike | |
28 */ | |
29 class GrTextStrike { | |
30 public: | |
31 GrTextStrike(GrFontCache*, const GrFontDescKey* fontScalerKey); | |
32 ~GrTextStrike(); | |
33 | |
34 const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; } | |
35 GrFontCache* getFontCache() const { return fFontCache; } | |
36 | |
37 inline GrGlyph* getGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler) { | |
38 GrGlyph* glyph = fCache.find(packed); | |
39 if (NULL == glyph) { | |
40 glyph = this->generateGlyph(packed, scaler); | |
41 } | |
42 return glyph; | |
43 } | |
44 | |
45 // returns true if glyph (or glyph+padding for distance field) | |
46 // is too large to ever fit in texture atlas subregions (GrPlots) | |
47 bool glyphTooLargeForAtlas(GrGlyph*); | |
48 // returns true if glyph successfully added to texture atlas, false otherwis
e | |
49 bool addGlyphToAtlas(GrGlyph*, GrFontScaler*); | |
50 | |
51 // testing | |
52 int countGlyphs() const { return fCache.count(); } | |
53 | |
54 // remove any references to this plot | |
55 void removePlot(const GrPlot* plot); | |
56 | |
57 static const GrFontDescKey& GetKey(const GrTextStrike& ts) { | |
58 return *(ts.fFontScalerKey); | |
59 } | |
60 static uint32_t Hash(const GrFontDescKey& key) { | |
61 return key.getHash(); | |
62 } | |
63 | |
64 public: | |
65 // for easy removal from list | |
66 GrTextStrike* fPrev; | |
67 GrTextStrike* fNext; | |
68 | |
69 private: | |
70 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache; | |
71 const GrFontDescKey* fFontScalerKey; | |
72 SkVarAlloc fPool; | |
73 | |
74 GrFontCache* fFontCache; | |
75 | |
76 GrAtlas::ClientPlotUsage fPlotUsage; | |
77 | |
78 GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler); | |
79 | |
80 friend class GrFontCache; | |
81 }; | |
82 | |
83 class GrFontCache { | |
84 public: | |
85 GrFontCache(GrGpu*); | |
86 ~GrFontCache(); | |
87 | |
88 inline GrTextStrike* getStrike(GrFontScaler* scaler) { | |
89 this->validate(); | |
90 | |
91 GrTextStrike* strike = fCache.find(*(scaler->getKey())); | |
92 if (NULL == strike) { | |
93 strike = this->generateStrike(scaler); | |
94 } else if (strike->fPrev) { | |
95 // Need to put the strike at the head of its dllist, since that is h
ow | |
96 // we age the strikes for purging (we purge from the back of the lis
t) | |
97 this->detachStrikeFromList(strike); | |
98 // attach at the head | |
99 fHead->fPrev = strike; | |
100 strike->fNext = fHead; | |
101 strike->fPrev = NULL; | |
102 fHead = strike; | |
103 } | |
104 this->validate(); | |
105 return strike; | |
106 } | |
107 | |
108 // add to texture atlas that matches this format | |
109 GrPlot* addToAtlas(GrMaskFormat format, GrAtlas::ClientPlotUsage* usage, | |
110 int width, int height, const void* image, | |
111 SkIPoint16* loc); | |
112 | |
113 void freeAll(); | |
114 | |
115 // make an unused plot available for this glyph | |
116 bool freeUnusedPlot(GrTextStrike* preserveStrike, const GrGlyph* glyph); | |
117 | |
118 // testing | |
119 int countStrikes() const { return fCache.count(); } | |
120 GrTextStrike* getHeadStrike() const { return fHead; } | |
121 | |
122 void updateTextures() { | |
123 for (int i = 0; i < kAtlasCount; ++i) { | |
124 if (fAtlases[i]) { | |
125 fAtlases[i]->uploadPlotsToTexture(); | |
126 } | |
127 } | |
128 } | |
129 | |
130 #ifdef SK_DEBUG | |
131 void validate() const; | |
132 #else | |
133 void validate() const {} | |
134 #endif | |
135 | |
136 void dump() const; | |
137 | |
138 enum AtlasType { | |
139 kA8_AtlasType, //!< 1-byte per pixel | |
140 k565_AtlasType, //!< 2-bytes per pixel | |
141 k8888_AtlasType, //!< 4-bytes per pixel | |
142 | |
143 kLast_AtlasType = k8888_AtlasType | |
144 }; | |
145 static const int kAtlasCount = kLast_AtlasType + 1; | |
146 | |
147 private: | |
148 friend class GrFontPurgeListener; | |
149 | |
150 SkTDynamicHash<GrTextStrike, GrFontDescKey> fCache; | |
151 // for LRU | |
152 GrTextStrike* fHead; | |
153 GrTextStrike* fTail; | |
154 | |
155 GrGpu* fGpu; | |
156 GrAtlas* fAtlases[kAtlasCount]; | |
157 | |
158 GrTextStrike* generateStrike(GrFontScaler*); | |
159 | |
160 inline void detachStrikeFromList(GrTextStrike* strike) { | |
161 if (strike->fPrev) { | |
162 SkASSERT(fHead != strike); | |
163 strike->fPrev->fNext = strike->fNext; | |
164 } else { | |
165 SkASSERT(fHead == strike); | |
166 fHead = strike->fNext; | |
167 } | |
168 | |
169 if (strike->fNext) { | |
170 SkASSERT(fTail != strike); | |
171 strike->fNext->fPrev = strike->fPrev; | |
172 } else { | |
173 SkASSERT(fTail == strike); | |
174 fTail = strike->fPrev; | |
175 } | |
176 } | |
177 | |
178 void purgeStrike(GrTextStrike* strike); | |
179 }; | |
180 | |
181 #endif | |
OLD | NEW |