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

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

Issue 1050633002: Revert of BitmapTextBatch and BitmapTextBlob (Closed) Base URL: https://skia.googlesource.com/skia.git@dfpr_take_2
Patch Set: Created 5 years, 8 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
« no previous file with comments | « src/gpu/GrBatchFontCache.h ('k') | src/gpu/GrBitmapTextContext.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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[] = {
36 kA8_GrMaskFormat,
37 kA565_GrMaskFormat,
38 kARGB_GrMaskFormat,
39 };
40 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_s ize_mismatch);
41
42 SkASSERT(sAtlasIndices[format] < kMaskFormatCount);
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] < kMaskFormatCount);
55 return sMaskFormats[atlasIndex];
56 }
57
58 GrBatchFontCache::GrBatchFontCache()
59 : fPreserveStrike(NULL) {
60 }
61
62 void GrBatchFontCache::init(GrContext* context) {
63 for (int i = 0; i < kMaskFormatCount; i++) {
64 GrMaskFormat format = AtlasIndexToMaskFormat(i);
65 GrPixelConfig config = this->getPixelConfig(format);
66
67 if (kA8_GrMaskFormat == format) {
68 fAtlases[i] = make_atlas(context, config,
69 GR_FONT_ATLAS_A8_TEXTURE_WIDTH,
70 GR_FONT_ATLAS_TEXTURE_HEIGHT,
71 GR_FONT_ATLAS_A8_NUM_PLOTS_X,
72 GR_FONT_ATLAS_NUM_PLOTS_Y);
73 } else {
74 fAtlases[i] = make_atlas(context, config,
75 GR_FONT_ATLAS_TEXTURE_WIDTH,
76 GR_FONT_ATLAS_TEXTURE_HEIGHT,
77 GR_FONT_ATLAS_NUM_PLOTS_X,
78 GR_FONT_ATLAS_NUM_PLOTS_Y);
79 }
80
81 fAtlases[i]->registerEvictionCallback(&GrBatchFontCache::HandleEviction, (void*)this);
82 }
83 }
84
85 GrBatchFontCache::~GrBatchFontCache() {
86 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
87 while (!iter.done()) {
88 SkDELETE(&(*iter));
89 ++iter;
90 }
91 for (int i = 0; i < kMaskFormatCount; ++i) {
92 SkDELETE(fAtlases[i]);
93 }
94 }
95
96 GrBatchTextStrike* GrBatchFontCache::generateStrike(GrFontScaler* scaler) {
97 GrBatchTextStrike* strike = SkNEW_ARGS(GrBatchTextStrike, (this, scaler->get Key()));
98 fCache.add(strike);
99 return strike;
100 }
101
102 void GrBatchFontCache::freeAll() {
103 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
104 while (!iter.done()) {
105 SkDELETE(&(*iter));
106 ++iter;
107 }
108 fCache.rewind();
109 for (int i = 0; i < kMaskFormatCount; ++i) {
110 SkDELETE(fAtlases[i]);
111 fAtlases[i] = NULL;
112 }
113 }
114
115 inline GrBatchAtlas* GrBatchFontCache::getAtlas(GrMaskFormat format) const {
116 int atlasIndex = MaskFormatToAtlasIndex(format);
117 SkASSERT(fAtlases[atlasIndex]);
118 return fAtlases[atlasIndex];
119 }
120
121 bool GrBatchFontCache::hasGlyph(GrGlyph* glyph) {
122 SkASSERT(glyph);
123 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
124 }
125
126 void GrBatchFontCache::setGlyphRefToken(GrGlyph* glyph, GrBatchAtlas::BatchToken batchToken) {
127 SkASSERT(glyph);
128 SkASSERT(this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID));
129 this->getAtlas(glyph->fMaskFormat)->setLastRefToken(glyph->fID, batchToken);
130 }
131
132 bool GrBatchFontCache::addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::Atlas ID* id,
133 GrBatchTarget* batchTarget,
134 GrMaskFormat format, int width, int height, co nst void* image,
135 SkIPoint16* loc) {
136 fPreserveStrike = strike;
137 return this->getAtlas(format)->addToAtlas(id, batchTarget, width, height, im age, loc);
138 }
139
140 uint64_t GrBatchFontCache::atlasGeneration(GrMaskFormat format) const {
141 return this->getAtlas(format)->atlasGeneration();
142 }
143
144 GrTexture* GrBatchFontCache::getTexture(GrMaskFormat format) {
145 int atlasIndex = MaskFormatToAtlasIndex(format);
146 SkASSERT(fAtlases[atlasIndex]);
147 return fAtlases[atlasIndex]->getTexture();
148 }
149
150 GrPixelConfig GrBatchFontCache::getPixelConfig(GrMaskFormat format) const {
151 static const GrPixelConfig kPixelConfigs[] = {
152 kAlpha_8_GrPixelConfig,
153 kRGB_565_GrPixelConfig,
154 kSkia8888_GrPixelConfig
155 };
156 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(kPixelConfigs) == kMaskFormatCount, array_s ize_mismatch);
157
158 return kPixelConfigs[format];
159 }
160
161 void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) {
162 GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr);
163
164 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fontCache->fCac he);
165 for (; !iter.done(); ++iter) {
166 GrBatchTextStrike* strike = &*iter;
167 strike->removeID(id);
168
169 // clear out any empty strikes. We will preserve the strike whose call to addToAtlas
170 // triggered the eviction
171 if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) {
172 fontCache->fCache.remove(*(strike->fFontScalerKey));
173 SkDELETE(strike);
174 }
175 }
176 }
177
178 void GrBatchFontCache::dump() const {
179 static int gDumpCount = 0;
180 for (int i = 0; i < kMaskFormatCount; ++i) {
181 if (fAtlases[i]) {
182 GrTexture* texture = fAtlases[i]->getTexture();
183 if (texture) {
184 SkString filename;
185 #ifdef SK_BUILD_FOR_ANDROID
186 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
187 #else
188 filename.printf("fontcache_%d%d.png", gDumpCount, i);
189 #endif
190 texture->surfacePriv().savePixels(filename.c_str());
191 }
192 }
193 }
194 ++gDumpCount;
195 }
196
197 ///////////////////////////////////////////////////////////////////////////////
198
199 /*
200 The text strike is specific to a given font/style/matrix setup, which is
201 represented by the GrHostFontScaler object we are given in getGlyph().
202
203 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
204 atlas and a position within that texture.
205 */
206
207 GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKe y* key)
208 : fFontScalerKey(SkRef(key))
209 , fPool(9/*start allocations at 512 bytes*/)
210 , fAtlasedGlyphs(0) {
211
212 fBatchFontCache = cache; // no need to ref, it won't go away before we d o
213 }
214
215 GrBatchTextStrike::~GrBatchTextStrike() {
216 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
217 while (!iter.done()) {
218 (*iter).free();
219 ++iter;
220 }
221 }
222
223 GrGlyph* GrBatchTextStrike::generateGlyph(GrGlyph::PackedID packed,
224 GrFontScaler* scaler) {
225 SkIRect bounds;
226 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
227 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) {
228 return NULL;
229 }
230 } else {
231 if (!scaler->getPackedGlyphBounds(packed, &bounds)) {
232 return NULL;
233 }
234 }
235 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(packed);
236
237 GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW);
238 glyph->init(packed, bounds, format);
239 fCache.add(glyph);
240 return glyph;
241 }
242
243 void GrBatchTextStrike::removeID(GrBatchAtlas::AtlasID id) {
244 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
245 while (!iter.done()) {
246 if (id == (*iter).fID) {
247 (*iter).fID = GrBatchAtlas::kInvalidAtlasID;
248 fAtlasedGlyphs--;
249 SkASSERT(fAtlasedGlyphs >= 0);
250 }
251 ++iter;
252 }
253 }
254
255 bool GrBatchTextStrike::glyphTooLargeForAtlas(GrGlyph* glyph) {
256 int width = glyph->fBounds.width();
257 int height = glyph->fBounds.height();
258 bool useDistanceField =
259 (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPa ckedID));
260 int pad = useDistanceField ? 2 * SK_DistanceFieldPad : 0;
261 int plotWidth = (kA8_GrMaskFormat == glyph->fMaskFormat) ? GR_FONT_ATLAS_A8_ PLOT_WIDTH
262 : GR_FONT_ATLAS_PLO T_WIDTH;
263 if (width + pad > plotWidth) {
264 return true;
265 }
266 if (height + pad > GR_FONT_ATLAS_PLOT_HEIGHT) {
267 return true;
268 }
269
270 return false;
271 }
272
273 bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* gly ph,
274 GrFontScaler* scaler) {
275 SkASSERT(glyph);
276 SkASSERT(scaler);
277 SkASSERT(fCache.find(glyph->fPackedID));
278 SkASSERT(NULL == glyph->fPlot);
279
280 SkAutoUnref ar(SkSafeRef(scaler));
281
282 int bytesPerPixel = GrMaskFormatBytesPerPixel(glyph->fMaskFormat);
283
284 size_t size = glyph->fBounds.area() * bytesPerPixel;
285 GrAutoMalloc<1024> storage(size);
286
287 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedI D)) {
288 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(),
289 glyph->height(),
290 storage.get())) {
291 return false;
292 }
293 } else {
294 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(),
295 glyph->height(),
296 glyph->width() * bytesPerPixel,
297 storage.get())) {
298 return false;
299 }
300 }
301
302 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, g lyph->fMaskFormat,
303 glyph->width(), glyph->height(),
304 storage.get(), &glyph->fAtlasLoca tion);
305 if (success) {
306 fAtlasedGlyphs++;
307 }
308 return success;
309 }
OLDNEW
« no previous file with comments | « src/gpu/GrBatchFontCache.h ('k') | src/gpu/GrBitmapTextContext.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698