Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/gpu/GrBatchFontCache.cpp

Issue 1011403004: BitmapTextBatch and BitmapTextBlob (Closed) Base URL: https://skia.googlesource.com/skia.git@dfpr_take_2
Patch Set: tidying Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
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 #include "GrBatchFontCache.h"
9 #include "GrFontAtlasSizes.h"
10 #include "GrGpu.h"
11 #include "GrRectanizer.h"
12 #include "GrSurfacePriv.h"
13 #include "SkString.h"
14
15 #include "SkDistanceFieldGen.h"
16
17 ///////////////////////////////////////////////////////////////////////////////
18
19 static GrBatchAtlas* make_atlas(GrContext* context, GrPixelConfig config,
20 int textureWidth, int textureHeight,
21 int numPlotsX, int numPlotsY) {
22 GrSurfaceDesc desc;
23 desc.fFlags = kNone_GrSurfaceFlags;
24 desc.fWidth = textureWidth;
25 desc.fHeight = textureHeight;
26 desc.fConfig = config;
27
28 // We don't want to flush the context so we claim we're in the middle of flu shing so as to
29 // guarantee we do not recieve a texture with pending IO
30 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox_Scr atchTexMatch, true);
31 return SkNEW_ARGS(GrBatchAtlas, (texture, numPlotsX, numPlotsY));
32 }
33
34 int GrBatchFontCache::MaskFormatToAtlasIndex(GrMaskFormat format) {
35 static const int sAtlasIndices[] = {
bsalomon 2015/03/26 17:06:55 If these are one to one why have two enums? I was
joshualitt 2015/03/26 18:21:43 Acknowledged.
36 GrBatchFontCache::kA8_AtlasType,
37 GrBatchFontCache::k565_AtlasType,
38 GrBatchFontCache::k8888_AtlasType
39 };
40 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_s ize_mismatch);
41
42 SkASSERT(sAtlasIndices[format] < GrBatchFontCache::kAtlasCount);
43 return sAtlasIndices[format];
44 }
45
46 GrMaskFormat GrBatchFontCache::AtlasIndexToMaskFormat(int atlasIndex) {
47 static GrMaskFormat sMaskFormats[] = {
48 kA8_GrMaskFormat,
49 kA565_GrMaskFormat,
50 kARGB_GrMaskFormat,
51 };
52 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sMaskFormats) == kMaskFormatCount, array_si ze_mismatch);
53
54 SkASSERT(sMaskFormats[atlasIndex] < GrBatchFontCache::kAtlasCount);
55 return sMaskFormats[atlasIndex];
56 }
57
58 GrBatchFontCache::GrBatchFontCache(GrContext* context)
59 : fPreserveStrike(NULL) {
60 for (int i = 0; i < kAtlasCount; i++) {
61 GrMaskFormat format = AtlasIndexToMaskFormat(i);
62 GrPixelConfig config = this->getPixelConfig(format);
63
64 if (kA8_GrMaskFormat == format) {
65 fAtlases[i] = make_atlas(context, config,
66 GR_FONT_ATLAS_A8_TEXTURE_WIDTH,
67 GR_FONT_ATLAS_TEXTURE_HEIGHT,
68 GR_FONT_ATLAS_A8_NUM_PLOTS_X,
69 GR_FONT_ATLAS_NUM_PLOTS_Y);
70 } else {
71 fAtlases[i] = make_atlas(context, config,
72 GR_FONT_ATLAS_TEXTURE_WIDTH,
73 GR_FONT_ATLAS_TEXTURE_HEIGHT,
74 GR_FONT_ATLAS_NUM_PLOTS_X,
75 GR_FONT_ATLAS_NUM_PLOTS_Y);
76 }
77
78 fAtlases[i]->registerEvictionCallback(&GrBatchFontCache::HandleEviction, (void*)this);
79 }
80 }
81
82 GrBatchFontCache::~GrBatchFontCache() {
83 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
84 while (!iter.done()) {
85 SkDELETE(&(*iter));
86 ++iter;
87 }
88 for (int i = 0; i < kAtlasCount; ++i) {
89 SkDELETE(fAtlases[i]);
90 }
91 }
92
93 GrBatchTextStrike* GrBatchFontCache::generateStrike(GrFontScaler* scaler) {
94 GrBatchTextStrike* strike = SkNEW_ARGS(GrBatchTextStrike, (this, scaler->get Key()));
95 fCache.add(strike);
96 fStrikeList.addToHead(strike);
97 return strike;
98 }
99
100 void GrBatchFontCache::freeAll() {
101 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
102 while (!iter.done()) {
103 SkDELETE(&(*iter));
104 ++iter;
105 }
106 fCache.rewind();
107 for (int i = 0; i < kAtlasCount; ++i) {
108 SkDELETE(fAtlases[i]);
109 fAtlases[i] = NULL;
110 }
111 }
112
113 void GrBatchFontCache::purgeStrike(GrBatchTextStrike* strike) {
114 fCache.remove(*(strike->fFontScalerKey));
115 fStrikeList.remove(strike);
116 SkDELETE(strike);
117 }
118
119 inline GrBatchAtlas* GrBatchFontCache::getAtlas(GrMaskFormat format) const {
120 int atlasIndex = MaskFormatToAtlasIndex(format);
121 SkASSERT(fAtlases[atlasIndex]);
122 return fAtlases[atlasIndex];
123 }
124
125 bool GrBatchFontCache::hasGlyph(GrGlyph* glyph) {
126 SkASSERT(glyph);
127 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
128 }
129
130 void GrBatchFontCache::refGlyph(GrGlyph* glyph, GrBatchAtlas::BatchToken batchTo ken) {
131 SkASSERT(glyph);
132 SkASSERT(this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID));
133 this->getAtlas(glyph->fMaskFormat)->setLastRefToken(glyph->fID, batchToken);
134 }
135
136 bool GrBatchFontCache::addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::Atlas ID* id,
137 GrBatchTarget* batchTarget,
138 GrMaskFormat format, int width, int height, co nst void* image,
139 SkIPoint16* loc) {
140 fPreserveStrike = strike;
141 return this->getAtlas(format)->addToAtlas(id, batchTarget, width, height, im age, loc);
142 }
143
144 uint32_t GrBatchFontCache::atlasGeneration(GrMaskFormat format) const {
145 return this->getAtlas(format)->atlasGeneration();
146 }
147
148 GrTexture* GrBatchFontCache::getTexture(GrMaskFormat format) {
149 int atlasIndex = MaskFormatToAtlasIndex(format);
150 SkASSERT(fAtlases[atlasIndex]);
151 return fAtlases[atlasIndex]->getTexture();
152 }
153
154 GrPixelConfig GrBatchFontCache::getPixelConfig(GrMaskFormat format) const {
155 static const GrPixelConfig sPixelConfigs[] = {
156 kAlpha_8_GrPixelConfig,
157 kRGB_565_GrPixelConfig,
158 kSkia8888_GrPixelConfig
159 };
160 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sPixelConfigs) == kMaskFormatCount, array_s ize_mismatch);
161
162 return sPixelConfigs[format];
163 }
164
165 void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) {
166 GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr);
167
168 GrBatchStrikeList::Iter plotIter;
169 plotIter.init(fontCache->fStrikeList, GrBatchStrikeList::Iter::kHead_IterSta rt);
170
171 GrBatchTextStrike* strike;
172 while ((strike = plotIter.get())) {
173 // We advance the pointer here because we might delete the strike
174 plotIter.next();
175 strike->removeID(id);
176
177 // clear out any empty strikes. We will preserve the strike whose call to addToAtlas
178 // triggered the eviction
179 if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) {
180 fontCache->purgeStrike(strike);
181 }
182 }
183 }
184
185 #ifdef SK_DEBUG
186 void GrBatchFontCache::validate() const {
187 SkASSERT(fCache.count() == fStrikeList.countEntries());
188 }
189 #endif
190
191 void GrBatchFontCache::dump() const {
192 static int gDumpCount = 0;
193 for (int i = 0; i < kAtlasCount; ++i) {
194 if (fAtlases[i]) {
195 GrTexture* texture = fAtlases[i]->getTexture();
196 if (texture) {
197 SkString filename;
198 #ifdef SK_BUILD_FOR_ANDROID
199 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
200 #else
201 filename.printf("fontcache_%d%d.png", gDumpCount, i);
202 #endif
203 texture->surfacePriv().savePixels(filename.c_str());
204 }
205 }
206 }
207 ++gDumpCount;
208 }
209
210 ///////////////////////////////////////////////////////////////////////////////
211
212 /*
213 The text strike is specific to a given font/style/matrix setup, which is
214 represented by the GrHostFontScaler object we are given in getGlyph().
215
216 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
217 atlas and a position within that texture.
218 */
219
220 GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKe y* key)
221 : fFontScalerKey(SkRef(key))
222 , fPool(9/*start allocations at 512 bytes*/)
223 , fAtlasedGlyphs(0) {
224
225 fBatchFontCache = cache; // no need to ref, it won't go away before we d o
226 }
227
228 GrBatchTextStrike::~GrBatchTextStrike() {
229 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
230 while (!iter.done()) {
231 (*iter).free();
232 ++iter;
233 }
234 }
235
236 GrGlyph* GrBatchTextStrike::generateGlyph(GrGlyph::PackedID packed,
237 GrFontScaler* scaler) {
238 SkIRect bounds;
239 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
240 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) {
241 return NULL;
242 }
243 } else {
244 if (!scaler->getPackedGlyphBounds(packed, &bounds)) {
245 return NULL;
246 }
247 }
248 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(packed);
249
250 GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW);
251 glyph->init(packed, bounds, format);
252 fCache.add(glyph);
253 return glyph;
254 }
255
256 void GrBatchTextStrike::removeID(GrBatchAtlas::AtlasID id) {
257 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
258 while (!iter.done()) {
259 if (id == (*iter).fID) {
260 (*iter).fID = GrBatchAtlas::kInvalidAtlasID;
261 fAtlasedGlyphs--;
262 SkASSERT(fAtlasedGlyphs >= 0);
263 }
264 ++iter;
265 }
266 }
267
268 bool GrBatchTextStrike::glyphTooLargeForAtlas(GrGlyph* glyph) {
269 int width = glyph->fBounds.width();
270 int height = glyph->fBounds.height();
271 bool useDistanceField =
272 (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPa ckedID));
273 int pad = useDistanceField ? 2 * SK_DistanceFieldPad : 0;
274 int plotWidth = (kA8_GrMaskFormat == glyph->fMaskFormat) ? GR_FONT_ATLAS_A8_ PLOT_WIDTH
275 : GR_FONT_ATLAS_PLO T_WIDTH;
276 if (width + pad > plotWidth) {
277 return true;
278 }
279 if (height + pad > GR_FONT_ATLAS_PLOT_HEIGHT) {
280 return true;
281 }
282
283 return false;
284 }
285
286 bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* gly ph,
287 GrFontScaler* scaler) {
288 SkASSERT(glyph);
289 SkASSERT(scaler);
290 SkASSERT(fCache.find(glyph->fPackedID));
291 SkASSERT(NULL == glyph->fPlot);
292
293 SkAutoUnref ar(SkSafeRef(scaler));
294
295 int bytesPerPixel = GrMaskFormatBytesPerPixel(glyph->fMaskFormat);
296
297 size_t size = glyph->fBounds.area() * bytesPerPixel;
298 GrAutoMalloc<1024> storage(size);
299
300 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedI D)) {
301 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(),
302 glyph->height(),
303 storage.get())) {
304 return false;
305 }
306 } else {
307 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(),
308 glyph->height(),
309 glyph->width() * bytesPerPixel,
310 storage.get())) {
311 return false;
312 }
313 }
314
315 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, g lyph->fMaskFormat,
316 glyph->width(), glyph->height(),
317 storage.get(), &glyph->fAtlasLoca tion);
318 if (success) {
319 fAtlasedGlyphs++;
320 }
321 return success;
322 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698