OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "GrDistanceFieldTextContext.h" | 8 #include "GrDistanceFieldTextContext.h" |
9 #include "GrAtlas.h" | 9 #include "GrAtlas.h" |
10 #include "GrDrawTarget.h" | 10 #include "GrDrawTarget.h" |
11 #include "GrFontScaler.h" | 11 #include "GrFontScaler.h" |
12 #include "SkGlyphCache.h" | 12 #include "SkGlyphCache.h" |
13 #include "GrIndexBuffer.h" | 13 #include "GrIndexBuffer.h" |
14 #include "GrTextStrike.h" | 14 #include "GrTextStrike.h" |
15 #include "GrTextStrike_impl.h" | 15 #include "GrTextStrike_impl.h" |
16 #include "SkDraw.h" | 16 #include "SkDraw.h" |
17 #include "SkGpuDevice.h" | 17 #include "SkGpuDevice.h" |
18 #include "SkPath.h" | 18 #include "SkPath.h" |
19 #include "SkRTConf.h" | 19 #include "SkRTConf.h" |
20 #include "SkStrokeRec.h" | 20 #include "SkStrokeRec.h" |
21 #include "effects/GrDistanceFieldTextureEffect.h" | 21 #include "effects/GrDistanceFieldTextureEffect.h" |
22 | 22 |
23 static const int kGlyphCoordsAttributeIndex = 1; | 23 static const int kGlyphCoordsAttributeIndex = 1; |
24 | 24 |
25 static const int kBaseDFFontSize = 32; | 25 static const int kBaseDFFontSize = 32; |
26 | 26 |
27 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, | 27 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, |
28 "Dump the contents of the font cache before every purge."); | 28 "Dump the contents of the font cache before every purge."); |
29 | 29 |
30 bool GrDistanceFieldTextContext::CanDraw(const SkPaint& paint, const SkMatrix& c
tm) { | |
31 return !paint.getRasterizer() && !paint.getMaskFilter() && | |
32 paint.getStyle() == SkPaint::kFill_Style && | |
33 !SkDraw::ShouldDrawTextAsPaths(paint, ctm); | |
34 } | |
35 | |
36 GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context, | 30 GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context, |
37 const GrPaint& grPaint, | |
38 const SkPaint& skPaint, | |
39 const SkDeviceProperties&
properties) | 31 const SkDeviceProperties&
properties) |
40 : GrTextContext(context, grPaint, sk
Paint, properties) { | 32 : GrTextContext(context, pro
perties) { |
41 fStrike = NULL; | 33 fStrike = NULL; |
42 | 34 |
43 fCurrTexture = NULL; | 35 fCurrTexture = NULL; |
44 fCurrVertex = 0; | 36 fCurrVertex = 0; |
45 | 37 |
46 fVertices = NULL; | 38 fVertices = NULL; |
47 fMaxVertices = 0; | 39 fMaxVertices = 0; |
48 | |
49 fTextRatio = fSkPaint.getTextSize()/kBaseDFFontSize; | |
50 | |
51 fSkPaint.setTextSize(SkIntToScalar(kBaseDFFontSize)); | |
52 fSkPaint.setLCDRenderText(false); | |
53 fSkPaint.setAutohinted(false); | |
54 fSkPaint.setSubpixelText(false); | |
55 } | 40 } |
56 | 41 |
57 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { | 42 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { |
58 this->flushGlyphs(); | 43 this->flushGlyphs(); |
59 } | 44 } |
60 | 45 |
| 46 bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) { |
| 47 return !paint.getRasterizer() && !paint.getMaskFilter() && |
| 48 paint.getStyle() == SkPaint::kFill_Style && |
| 49 !SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix()); |
| 50 } |
| 51 |
61 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { | 52 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { |
62 unsigned r = SkColorGetR(c); | 53 unsigned r = SkColorGetR(c); |
63 unsigned g = SkColorGetG(c); | 54 unsigned g = SkColorGetG(c); |
64 unsigned b = SkColorGetB(c); | 55 unsigned b = SkColorGetB(c); |
65 return GrColorPackRGBA(r, g, b, 0xff); | 56 return GrColorPackRGBA(r, g, b, 0xff); |
66 } | 57 } |
67 | 58 |
68 void GrDistanceFieldTextContext::flushGlyphs() { | 59 void GrDistanceFieldTextContext::flushGlyphs() { |
69 if (NULL == fDrawTarget) { | 60 if (NULL == fDrawTarget) { |
70 return; | 61 return; |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 sy + height, | 275 sy + height, |
285 2 * sizeof(SkPoint)); | 276 2 * sizeof(SkPoint)); |
286 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixed
X(tx)), | 277 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixed
X(tx)), |
287 SkFixedToFloat(texture->normalizeFixed
Y(ty)), | 278 SkFixedToFloat(texture->normalizeFixed
Y(ty)), |
288 SkFixedToFloat(texture->normalizeFixed
X(tx + tw)), | 279 SkFixedToFloat(texture->normalizeFixed
X(tx + tw)), |
289 SkFixedToFloat(texture->normalizeFixed
Y(ty + th)), | 280 SkFixedToFloat(texture->normalizeFixed
Y(ty + th)), |
290 2 * sizeof(SkPoint)); | 281 2 * sizeof(SkPoint)); |
291 fCurrVertex += 4; | 282 fCurrVertex += 4; |
292 } | 283 } |
293 | 284 |
294 void GrDistanceFieldTextContext::drawText(const char text[], size_t byteLength, | 285 inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint
& skPaint) { |
| 286 GrTextContext::init(paint, skPaint); |
| 287 |
| 288 fStrike = NULL; |
| 289 |
| 290 fCurrTexture = NULL; |
| 291 fCurrVertex = 0; |
| 292 |
| 293 fVertices = NULL; |
| 294 fMaxVertices = 0; |
| 295 |
| 296 fTextRatio = fSkPaint.getTextSize()/kBaseDFFontSize; |
| 297 |
| 298 fSkPaint.setTextSize(SkIntToScalar(kBaseDFFontSize)); |
| 299 fSkPaint.setLCDRenderText(false); |
| 300 fSkPaint.setAutohinted(false); |
| 301 fSkPaint.setSubpixelText(false); |
| 302 } |
| 303 |
| 304 inline void GrDistanceFieldTextContext::finish() { |
| 305 flushGlyphs(); |
| 306 |
| 307 GrTextContext::finish(); |
| 308 } |
| 309 |
| 310 void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& s
kPaint, |
| 311 const char text[], size_t byteLength, |
295 SkScalar x, SkScalar y) { | 312 SkScalar x, SkScalar y) { |
296 SkASSERT(byteLength == 0 || text != NULL); | 313 SkASSERT(byteLength == 0 || text != NULL); |
297 | 314 |
298 // nothing to draw or can't draw | 315 // nothing to draw or can't draw |
299 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/ | 316 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/ |
300 || fSkPaint.getRasterizer()) { | 317 || fSkPaint.getRasterizer()) { |
301 return; | 318 return; |
302 } | 319 } |
303 | 320 |
| 321 this->init(paint, skPaint); |
| 322 |
304 SkScalar sizeRatio = fTextRatio; | 323 SkScalar sizeRatio = fTextRatio; |
305 | 324 |
306 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); | 325 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
307 | 326 |
308 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); | 327 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); |
309 SkGlyphCache* cache = autoCache.getCache(); | 328 SkGlyphCache* cache = autoCache.getCache(); |
310 GrFontScaler* fontScaler = GetGrFontScaler(cache); | 329 GrFontScaler* fontScaler = GetGrFontScaler(cache); |
311 | 330 |
312 // need to measure first | 331 // need to measure first |
313 // TODO - generate positions and pre-load cache as well? | 332 // TODO - generate positions and pre-load cache as well? |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 glyph.getSubXFixed(), | 369 glyph.getSubXFixed(), |
351 glyph.getSubYFixed()), | 370 glyph.getSubYFixed()), |
352 SkFixedFloorToFixed(fx), | 371 SkFixedFloorToFixed(fx), |
353 SkFixedFloorToFixed(fy), | 372 SkFixedFloorToFixed(fy), |
354 fontScaler); | 373 fontScaler); |
355 } | 374 } |
356 | 375 |
357 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale); | 376 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale); |
358 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale); | 377 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale); |
359 } | 378 } |
| 379 |
| 380 this->finish(); |
360 } | 381 } |
361 | 382 |
362 void GrDistanceFieldTextContext::drawPosText(const char text[], size_t byteLengt
h, | 383 void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint
& skPaint, |
| 384 const char text[], size_t byteLengt
h, |
363 const SkScalar pos[], SkScalar cons
tY, | 385 const SkScalar pos[], SkScalar cons
tY, |
364 int scalarsPerPosition) { | 386 int scalarsPerPosition) { |
365 | 387 |
366 SkASSERT(byteLength == 0 || text != NULL); | 388 SkASSERT(byteLength == 0 || text != NULL); |
367 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); | 389 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
368 | 390 |
369 // nothing to draw | 391 // nothing to draw |
370 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/ | 392 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/)
{ |
371 || fSkPaint.getRasterizer()) { | |
372 return; | 393 return; |
373 } | 394 } |
374 | 395 |
| 396 this->init(paint, skPaint); |
| 397 |
375 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); | 398 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
376 | 399 |
377 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); | 400 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); |
378 SkGlyphCache* cache = autoCache.getCache(); | 401 SkGlyphCache* cache = autoCache.getCache(); |
379 GrFontScaler* fontScaler = GetGrFontScaler(cache); | 402 GrFontScaler* fontScaler = GetGrFontScaler(cache); |
380 | 403 |
381 const char* stop = text + byteLength; | 404 const char* stop = text + byteLength; |
382 | 405 |
383 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { | 406 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
384 while (text < stop) { | 407 while (text < stop) { |
(...skipping 28 matching lines...) Expand all Loading... |
413 glyph.getSubYFixed()), | 436 glyph.getSubYFixed()), |
414 SkScalarToFixed(x) - (glyph.fAdvanceX >> a
lignShift) | 437 SkScalarToFixed(x) - (glyph.fAdvanceX >> a
lignShift) |
415 + SK_FixedHalf, //d1g.fHalfSampleX, | 438 + SK_FixedHalf, //d1g.fHalfSampleX, |
416 SkScalarToFixed(y) - (glyph.fAdvanceY >> a
lignShift) | 439 SkScalarToFixed(y) - (glyph.fAdvanceY >> a
lignShift) |
417 + SK_FixedHalf, //d1g.fHalfSampleY, | 440 + SK_FixedHalf, //d1g.fHalfSampleY, |
418 fontScaler); | 441 fontScaler); |
419 } | 442 } |
420 pos += scalarsPerPosition; | 443 pos += scalarsPerPosition; |
421 } | 444 } |
422 } | 445 } |
| 446 |
| 447 this->finish(); |
423 } | 448 } |
OLD | NEW |