OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 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 #include "GrAtlas.h" | 8 #include "GrAtlas.h" |
9 #include "GrGpu.h" | 9 #include "GrGpu.h" |
10 #include "GrRectanizer.h" | 10 #include "GrRectanizer.h" |
11 #include "GrTextStrike.h" | 11 #include "GrTextStrike.h" |
12 #include "GrTextStrike_impl.h" | 12 #include "GrTextStrike_impl.h" |
13 #include "SkString.h" | 13 #include "SkString.h" |
14 | 14 |
15 #if SK_DISTANCEFIELD_FONTS | |
16 #include "SkDistanceFieldGen.h" | 15 #include "SkDistanceFieldGen.h" |
17 #endif | |
18 | 16 |
19 /////////////////////////////////////////////////////////////////////////////// | 17 /////////////////////////////////////////////////////////////////////////////// |
20 | 18 |
21 #define FONT_CACHE_STATS 0 | 19 #define FONT_CACHE_STATS 0 |
22 #if FONT_CACHE_STATS | 20 #if FONT_CACHE_STATS |
23 static int g_PurgeCount = 0; | 21 static int g_PurgeCount = 0; |
24 #endif | 22 #endif |
25 | 23 |
26 GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { | 24 GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { |
27 gpu->ref(); | 25 gpu->ref(); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 ++gDumpCount; | 189 ++gDumpCount; |
192 } | 190 } |
193 #endif | 191 #endif |
194 | 192 |
195 /////////////////////////////////////////////////////////////////////////////// | 193 /////////////////////////////////////////////////////////////////////////////// |
196 | 194 |
197 #ifdef SK_DEBUG | 195 #ifdef SK_DEBUG |
198 static int gCounter; | 196 static int gCounter; |
199 #endif | 197 #endif |
200 | 198 |
201 #if SK_DISTANCEFIELD_FONTS | |
202 // this acts as the max magnitude for the distance field, | 199 // this acts as the max magnitude for the distance field, |
203 // as well as the pad we need around the glyph | 200 // as well as the pad we need around the glyph |
204 #define DISTANCE_FIELD_RANGE 4 | 201 #define DISTANCE_FIELD_RANGE 4 |
205 #endif | |
206 | 202 |
207 /* | 203 /* |
208 The text strike is specific to a given font/style/matrix setup, which is | 204 The text strike is specific to a given font/style/matrix setup, which is |
209 represented by the GrHostFontScaler object we are given in getGlyph(). | 205 represented by the GrHostFontScaler object we are given in getGlyph(). |
210 | 206 |
211 We map a 32bit glyphID to a GrGlyph record, which in turn points to a | 207 We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
212 atlas and a position within that texture. | 208 atlas and a position within that texture. |
213 */ | 209 */ |
214 | 210 |
215 GrTextStrike::GrTextStrike(GrFontCache* cache, const GrKey* key, | 211 GrTextStrike::GrTextStrike(GrFontCache* cache, const GrKey* key, |
(...skipping 28 matching lines...) Expand all Loading... |
244 } | 240 } |
245 | 241 |
246 GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, | 242 GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, |
247 GrFontScaler* scaler) { | 243 GrFontScaler* scaler) { |
248 SkIRect bounds; | 244 SkIRect bounds; |
249 if (!scaler->getPackedGlyphBounds(packed, &bounds)) { | 245 if (!scaler->getPackedGlyphBounds(packed, &bounds)) { |
250 return NULL; | 246 return NULL; |
251 } | 247 } |
252 | 248 |
253 GrGlyph* glyph = fPool.alloc(); | 249 GrGlyph* glyph = fPool.alloc(); |
254 #if SK_DISTANCEFIELD_FONTS | |
255 // expand bounds to hold full distance field data | 250 // expand bounds to hold full distance field data |
256 if (fUseDistanceField) { | 251 if (fUseDistanceField) { |
257 bounds.fLeft -= DISTANCE_FIELD_RANGE; | 252 bounds.fLeft -= DISTANCE_FIELD_RANGE; |
258 bounds.fRight += DISTANCE_FIELD_RANGE; | 253 bounds.fRight += DISTANCE_FIELD_RANGE; |
259 bounds.fTop -= DISTANCE_FIELD_RANGE; | 254 bounds.fTop -= DISTANCE_FIELD_RANGE; |
260 bounds.fBottom += DISTANCE_FIELD_RANGE; | 255 bounds.fBottom += DISTANCE_FIELD_RANGE; |
261 } | 256 } |
262 #endif | |
263 glyph->init(packed, bounds); | 257 glyph->init(packed, bounds); |
264 fCache.insert(packed, glyph); | 258 fCache.insert(packed, glyph); |
265 return glyph; | 259 return glyph; |
266 } | 260 } |
267 | 261 |
268 void GrTextStrike::removePlot(const GrPlot* plot) { | 262 void GrTextStrike::removePlot(const GrPlot* plot) { |
269 SkTDArray<GrGlyph*>& glyphArray = fCache.getArray(); | 263 SkTDArray<GrGlyph*>& glyphArray = fCache.getArray(); |
270 for (int i = 0; i < glyphArray.count(); ++i) { | 264 for (int i = 0; i < glyphArray.count(); ++i) { |
271 if (plot == glyphArray[i]->fPlot) { | 265 if (plot == glyphArray[i]->fPlot) { |
272 glyphArray[i]->fPlot = NULL; | 266 glyphArray[i]->fPlot = NULL; |
(...skipping 13 matching lines...) Expand all Loading... |
286 SkASSERT(glyph); | 280 SkASSERT(glyph); |
287 SkASSERT(scaler); | 281 SkASSERT(scaler); |
288 SkASSERT(fCache.contains(glyph)); | 282 SkASSERT(fCache.contains(glyph)); |
289 SkASSERT(NULL == glyph->fPlot); | 283 SkASSERT(NULL == glyph->fPlot); |
290 | 284 |
291 SkAutoRef ar(scaler); | 285 SkAutoRef ar(scaler); |
292 | 286 |
293 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); | 287 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); |
294 | 288 |
295 GrPlot* plot; | 289 GrPlot* plot; |
296 #if SK_DISTANCEFIELD_FONTS | |
297 if (fUseDistanceField) { | 290 if (fUseDistanceField) { |
298 // we've already expanded the glyph dimensions to match the final size | 291 // we've already expanded the glyph dimensions to match the final size |
299 // but must shrink back down to get the packed glyph data | 292 // but must shrink back down to get the packed glyph data |
300 int dfWidth = glyph->width(); | 293 int dfWidth = glyph->width(); |
301 int dfHeight = glyph->height(); | 294 int dfHeight = glyph->height(); |
302 int width = dfWidth - 2*DISTANCE_FIELD_RANGE; | 295 int width = dfWidth - 2*DISTANCE_FIELD_RANGE; |
303 int height = dfHeight - 2*DISTANCE_FIELD_RANGE; | 296 int height = dfHeight - 2*DISTANCE_FIELD_RANGE; |
304 int stride = width*bytesPerPixel; | 297 int stride = width*bytesPerPixel; |
305 | 298 |
306 size_t size = width * height * bytesPerPixel; | 299 size_t size = width * height * bytesPerPixel; |
(...skipping 27 matching lines...) Expand all Loading... |
334 dfPtr += dfStride; | 327 dfPtr += dfStride; |
335 ptr += stride; | 328 ptr += stride; |
336 } | 329 } |
337 } | 330 } |
338 | 331 |
339 // copy to atlas | 332 // copy to atlas |
340 plot = fAtlasMgr->addToAtlas(&fAtlas, dfWidth, dfHeight, dfStorage.get()
, | 333 plot = fAtlasMgr->addToAtlas(&fAtlas, dfWidth, dfHeight, dfStorage.get()
, |
341 &glyph->fAtlasLocation); | 334 &glyph->fAtlasLocation); |
342 | 335 |
343 } else { | 336 } else { |
344 #endif | |
345 size_t size = glyph->fBounds.area() * bytesPerPixel; | 337 size_t size = glyph->fBounds.area() * bytesPerPixel; |
346 SkAutoSMalloc<1024> storage(size); | 338 SkAutoSMalloc<1024> storage(size); |
347 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), | 339 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), |
348 glyph->height(), | 340 glyph->height(), |
349 glyph->width() * bytesPerPixel, | 341 glyph->width() * bytesPerPixel, |
350 storage.get())) { | 342 storage.get())) { |
351 return false; | 343 return false; |
352 } | 344 } |
353 | 345 |
354 plot = fAtlasMgr->addToAtlas(&fAtlas, glyph->width(), | 346 plot = fAtlasMgr->addToAtlas(&fAtlas, glyph->width(), |
355 glyph->height(), storage.get(), | 347 glyph->height(), storage.get(), |
356 &glyph->fAtlasLocation); | 348 &glyph->fAtlasLocation); |
357 #if SK_DISTANCEFIELD_FONTS | |
358 } | 349 } |
359 #endif | |
360 | 350 |
361 if (NULL == plot) { | 351 if (NULL == plot) { |
362 return false; | 352 return false; |
363 } | 353 } |
364 | 354 |
365 glyph->fPlot = plot; | 355 glyph->fPlot = plot; |
366 return true; | 356 return true; |
367 } | 357 } |
OLD | NEW |