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, GrFontScaler*); | |
38 // returns true if glyph (or glyph+padding for distance field) | |
39 // is too large to ever fit in texture atlas subregions (GrPlots) | |
40 bool glyphTooLargeForAtlas(GrGlyph*); | |
41 // returns true if glyph successfully added to texture atlas, false otherwis
e | |
42 bool addGlyphToAtlas(GrGlyph*, GrFontScaler*); | |
43 | |
44 // testing | |
45 int countGlyphs() const { return fCache.count(); } | |
46 | |
47 // remove any references to this plot | |
48 void removePlot(const GrPlot* plot); | |
49 | |
50 static const GrFontDescKey& GetKey(const GrTextStrike& ts) { | |
51 return *(ts.fFontScalerKey); | |
52 } | |
53 static uint32_t Hash(const GrFontDescKey& key) { | |
54 return key.getHash(); | |
55 } | |
56 | |
57 public: | |
58 // for easy removal from list | |
59 GrTextStrike* fPrev; | |
60 GrTextStrike* fNext; | |
61 | |
62 private: | |
63 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache; | |
64 const GrFontDescKey* fFontScalerKey; | |
65 SkVarAlloc fPool; | |
66 | |
67 GrFontCache* fFontCache; | |
68 bool fUseDistanceField; | |
69 | |
70 GrAtlas::ClientPlotUsage fPlotUsage; | |
71 | |
72 GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler); | |
73 | |
74 friend class GrFontCache; | |
75 }; | |
76 | |
77 class GrFontCache { | |
78 public: | |
79 GrFontCache(GrGpu*); | |
80 ~GrFontCache(); | |
81 | |
82 inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField); | |
83 | |
84 // add to texture atlas that matches this format | |
85 GrPlot* addToAtlas(GrMaskFormat format, GrAtlas::ClientPlotUsage* usage, | |
86 int width, int height, const void* image, | |
87 SkIPoint16* loc); | |
88 | |
89 void freeAll(); | |
90 | |
91 // make an unused plot available for this glyph | |
92 bool freeUnusedPlot(GrTextStrike* preserveStrike, const GrGlyph* glyph); | |
93 | |
94 // testing | |
95 int countStrikes() const { return fCache.count(); } | |
96 GrTextStrike* getHeadStrike() const { return fHead; } | |
97 | |
98 void updateTextures() { | |
99 for (int i = 0; i < kAtlasCount; ++i) { | |
100 if (fAtlases[i]) { | |
101 fAtlases[i]->uploadPlotsToTexture(); | |
102 } | |
103 } | |
104 } | |
105 | |
106 #ifdef SK_DEBUG | |
107 void validate() const; | |
108 #else | |
109 void validate() const {} | |
110 #endif | |
111 | |
112 void dump() const; | |
113 | |
114 enum AtlasType { | |
115 kA8_AtlasType, //!< 1-byte per pixel | |
116 k565_AtlasType, //!< 2-bytes per pixel | |
117 k8888_AtlasType, //!< 4-bytes per pixel | |
118 | |
119 kLast_AtlasType = k8888_AtlasType | |
120 }; | |
121 static const int kAtlasCount = kLast_AtlasType + 1; | |
122 | |
123 private: | |
124 friend class GrFontPurgeListener; | |
125 | |
126 SkTDynamicHash<GrTextStrike, GrFontDescKey> fCache; | |
127 // for LRU | |
128 GrTextStrike* fHead; | |
129 GrTextStrike* fTail; | |
130 | |
131 GrGpu* fGpu; | |
132 GrAtlas* fAtlases[kAtlasCount]; | |
133 | |
134 GrTextStrike* generateStrike(GrFontScaler*); | |
135 inline void detachStrikeFromList(GrTextStrike*); | |
136 void purgeStrike(GrTextStrike* strike); | |
137 }; | |
138 | |
139 #endif | |
OLD | NEW |