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 "SkColorFilter.h" | |
11 #include "GrDrawTarget.h" | 10 #include "GrDrawTarget.h" |
12 #include "GrDrawTargetCaps.h" | 11 #include "GrDrawTargetCaps.h" |
13 #include "GrFontScaler.h" | 12 #include "GrFontScaler.h" |
14 #include "SkGlyphCache.h" | 13 #include "SkGlyphCache.h" |
15 #include "GrGpu.h" | |
16 #include "GrIndexBuffer.h" | 14 #include "GrIndexBuffer.h" |
17 #include "GrTextStrike.h" | 15 #include "GrTextStrike.h" |
18 #include "GrTextStrike_impl.h" | 16 #include "GrTextStrike_impl.h" |
19 #include "SkDistanceFieldGen.h" | 17 #include "SkDistanceFieldGen.h" |
20 #include "SkDraw.h" | 18 #include "SkDraw.h" |
21 #include "SkGpuDevice.h" | 19 #include "SkGpuDevice.h" |
22 #include "SkPath.h" | 20 #include "SkPath.h" |
23 #include "SkRTConf.h" | 21 #include "SkRTConf.h" |
24 #include "SkStrokeRec.h" | 22 #include "SkStrokeRec.h" |
25 #include "effects/GrDistanceFieldTextureEffect.h" | 23 #include "effects/GrDistanceFieldTextureEffect.h" |
(...skipping 12 matching lines...) Expand all Loading... |
38 GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context, | 36 GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context, |
39 const SkDeviceProperties&
properties, | 37 const SkDeviceProperties&
properties, |
40 bool enable) | 38 bool enable) |
41 : GrTextContext(context, pro
perties) { | 39 : GrTextContext(context, pro
perties) { |
42 #if SK_FORCE_DISTANCEFIELD_FONTS | 40 #if SK_FORCE_DISTANCEFIELD_FONTS |
43 fEnableDFRendering = true; | 41 fEnableDFRendering = true; |
44 #else | 42 #else |
45 fEnableDFRendering = enable; | 43 fEnableDFRendering = enable; |
46 #endif | 44 #endif |
47 fStrike = NULL; | 45 fStrike = NULL; |
48 fGammaTexture = NULL; | |
49 | 46 |
50 fCurrTexture = NULL; | 47 fCurrTexture = NULL; |
51 fCurrVertex = 0; | 48 fCurrVertex = 0; |
52 | 49 |
53 fVertices = NULL; | 50 fVertices = NULL; |
54 fMaxVertices = 0; | 51 fMaxVertices = 0; |
55 } | 52 } |
56 | 53 |
57 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { | 54 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { |
58 this->flushGlyphs(); | 55 this->flushGlyphs(); |
59 SkSafeSetNull(fGammaTexture); | |
60 } | 56 } |
61 | 57 |
62 bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) { | 58 bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) { |
63 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) { | 59 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) { |
64 return false; | 60 return false; |
65 } | 61 } |
66 | 62 |
67 // rasterizers and mask filters modify alpha, which doesn't | 63 // rasterizers and mask filters modify alpha, which doesn't |
68 // translate well to distance | 64 // translate well to distance |
69 if (paint.getRasterizer() || paint.getMaskFilter() || | 65 if (paint.getRasterizer() || paint.getMaskFilter() || |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 GrDrawState::AutoRestoreEffects are(drawState); | 100 GrDrawState::AutoRestoreEffects are(drawState); |
105 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTa
rget()); | 101 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTa
rget()); |
106 | 102 |
107 if (fCurrVertex > 0) { | 103 if (fCurrVertex > 0) { |
108 fContext->getFontCache()->updateTextures(); | 104 fContext->getFontCache()->updateTextures(); |
109 | 105 |
110 // setup our sampler state for our text texture/atlas | 106 // setup our sampler state for our text texture/atlas |
111 SkASSERT(SkIsAlign4(fCurrVertex)); | 107 SkASSERT(SkIsAlign4(fCurrVertex)); |
112 SkASSERT(fCurrTexture); | 108 SkASSERT(fCurrTexture); |
113 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBil
erp_FilterMode); | 109 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBil
erp_FilterMode); |
114 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::
kNone_FilterMode); | |
115 | 110 |
116 // Effects could be stored with one of the cache objects (atlas?) | 111 // Effects could be stored with one of the cache objects (atlas?) |
117 SkColor filteredColor; | |
118 SkColorFilter* colorFilter = fSkPaint.getColorFilter(); | |
119 if (NULL != colorFilter) { | |
120 filteredColor = colorFilter->filterColor(fSkPaint.getColor()); | |
121 } else { | |
122 filteredColor = fSkPaint.getColor(); | |
123 } | |
124 if (fUseLCDText) { | 112 if (fUseLCDText) { |
125 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredCol
or); | |
126 bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout == | 113 bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout == |
127 fDeviceProperties.fG
eometry.getLayout(); | 114 fDeviceProperties.fG
eometry.getLayout(); |
128 drawState->addCoverageEffect(GrDistanceFieldLCDTextureEffect::Create
( | 115 drawState->addCoverageEffect(GrDistanceFieldLCDTextureEffect::Create
( |
129 fCurrTexture, | 116 fCurrTexture, |
130 params, | 117 params, |
131 fGammaTexture, | |
132 gammaParams, | |
133 colorNoPreMul, | |
134 fContext->getMatrix(
).rectStaysRect() && | 118 fContext->getMatrix(
).rectStaysRect() && |
135 fContext->getMatrix(
).isSimilarity(), | 119 fContext->getMatrix(
).isSimilarity(), |
136 useBGR), | 120 useBGR), |
137 kGlyphCoordsAttributeIndex)->unref(); | 121 kGlyphCoordsAttributeIndex)->unref(); |
138 | 122 |
139 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || | 123 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
140 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || | 124 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
141 fPaint.numColorStages()) { | 125 fPaint.numColorStages()) { |
142 GrPrintf("LCD Text will not draw correctly.\n"); | 126 GrPrintf("LCD Text will not draw correctly.\n"); |
143 } | 127 } |
144 // We don't use the GrPaint's color in this case because it's been p
remultiplied by | 128 // We don't use the GrPaint's color in this case because it's been p
remultiplied by |
145 // alpha. Instead we feed in a non-premultiplied color, and multiply
its alpha by | 129 // alpha. Instead we feed in a non-premultiplied color, and multiply
its alpha by |
146 // the mask texture color. The end result is that we get | 130 // the mask texture color. The end result is that we get |
147 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo
lor | 131 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo
lor |
148 int a = SkColorGetA(fSkPaint.getColor()); | 132 int a = SkColorGetA(fSkPaint.getColor()); |
149 // paintAlpha | 133 // paintAlpha |
150 drawState->setColor(SkColorSetARGB(a, a, a, a)); | 134 drawState->setColor(SkColorSetARGB(a, a, a, a)); |
151 // paintColor | 135 // paintColor |
152 drawState->setBlendConstant(colorNoPreMul); | 136 drawState->setBlendConstant(skcolor_to_grcolor_nopremultiply(fSkPain
t.getColor())); |
153 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); | 137 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
154 } else { | 138 } else { |
155 #ifdef SK_GAMMA_APPLY_TO_A8 | 139 drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create(fC
urrTexture, params, |
156 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDevicePropertie
s.fGamma, | |
157 filteredColor); | |
158 drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create( | |
159 fCurrTexture, para
ms, | |
160 fGammaTexture, gam
maParams, | |
161 lum/255.f, | |
162 fContext->getMatri
x().isSimilarity()), | 140 fContext->getMatri
x().isSimilarity()), |
163 kGlyphCoordsAttributeIndex)->unref(); | 141 kGlyphCoordsAttributeIndex)->unref(); |
164 #else | 142 |
165 drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create( | |
166 fCurrTexture, para
ms, | |
167 fContext->getMatri
x().isSimilarity()), | |
168 kGlyphCoordsAttributeIndex)->unref(); | |
169 #endif | |
170 // set back to normal in case we took LCD path previously. | 143 // set back to normal in case we took LCD path previously. |
171 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen
dCoeff()); | 144 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen
dCoeff()); |
172 drawState->setColor(fPaint.getColor()); | 145 drawState->setColor(fPaint.getColor()); |
173 } | 146 } |
174 | 147 |
175 int nGlyphs = fCurrVertex / 4; | 148 int nGlyphs = fCurrVertex / 4; |
176 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); | 149 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
177 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, | 150 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, |
178 nGlyphs, | 151 nGlyphs, |
179 4, 6); | 152 4, 6); |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize)); | 349 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize)); |
377 } else { | 350 } else { |
378 fTextRatio = fSkPaint.getTextSize()/kLargeDFFontSize; | 351 fTextRatio = fSkPaint.getTextSize()/kLargeDFFontSize; |
379 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize)); | 352 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize)); |
380 } | 353 } |
381 | 354 |
382 fUseLCDText = fSkPaint.isLCDRenderText(); | 355 fUseLCDText = fSkPaint.isLCDRenderText(); |
383 | 356 |
384 fSkPaint.setLCDRenderText(false); | 357 fSkPaint.setLCDRenderText(false); |
385 fSkPaint.setAutohinted(false); | 358 fSkPaint.setAutohinted(false); |
386 fSkPaint.setHinting(SkPaint::kNormal_Hinting); | |
387 fSkPaint.setSubpixelText(true); | 359 fSkPaint.setSubpixelText(true); |
388 | |
389 } | 360 } |
390 | 361 |
391 inline void GrDistanceFieldTextContext::finish() { | 362 inline void GrDistanceFieldTextContext::finish() { |
392 flushGlyphs(); | 363 flushGlyphs(); |
393 | 364 |
394 GrTextContext::finish(); | 365 GrTextContext::finish(); |
395 } | 366 } |
396 | 367 |
397 static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache, | |
398 const SkDeviceProperties& deviceProperties, | |
399 GrTexture** gammaTexture) { | |
400 if (NULL == *gammaTexture) { | |
401 int width, height; | |
402 size_t size; | |
403 | |
404 #ifdef SK_GAMMA_CONTRAST | |
405 SkScalar contrast = SK_GAMMA_CONTRAST; | |
406 #else | |
407 SkScalar contrast = 0.5f; | |
408 #endif | |
409 SkScalar paintGamma = deviceProperties.fGamma; | |
410 SkScalar deviceGamma = deviceProperties.fGamma; | |
411 | |
412 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamm
a, | |
413 &width, &height); | |
414 | |
415 SkAutoTArray<uint8_t> data((int)size); | |
416 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data
.get()); | |
417 | |
418 // TODO: Update this to use the cache rather than directly creating a te
xture. | |
419 GrTextureDesc desc; | |
420 desc.fFlags = kDynamicUpdate_GrTextureFlagBit; | |
421 desc.fWidth = width; | |
422 desc.fHeight = height; | |
423 desc.fConfig = kAlpha_8_GrPixelConfig; | |
424 | |
425 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0); | |
426 if (NULL == *gammaTexture) { | |
427 return; | |
428 } | |
429 | |
430 context->writeTexturePixels(*gammaTexture, | |
431 0, 0, width, height, | |
432 (*gammaTexture)->config(), data.get(), 0, | |
433 GrContext::kDontFlush_PixelOpsFlag); | |
434 } | |
435 } | |
436 | |
437 void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& s
kPaint, | 368 void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& s
kPaint, |
438 const char text[], size_t byteLength, | 369 const char text[], size_t byteLength, |
439 SkScalar x, SkScalar y) { | 370 SkScalar x, SkScalar y) { |
440 SkASSERT(byteLength == 0 || text != NULL); | 371 SkASSERT(byteLength == 0 || text != NULL); |
441 | 372 |
442 // nothing to draw or can't draw | 373 // nothing to draw or can't draw |
443 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/ | 374 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/ |
444 || fSkPaint.getRasterizer()) { | 375 || fSkPaint.getRasterizer()) { |
445 return; | 376 return; |
446 } | 377 } |
447 | 378 |
448 this->init(paint, skPaint); | 379 this->init(paint, skPaint); |
449 | 380 |
450 SkScalar sizeRatio = fTextRatio; | 381 SkScalar sizeRatio = fTextRatio; |
451 | 382 |
452 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); | 383 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
453 | 384 |
454 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL); | 385 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); |
455 SkGlyphCache* cache = autoCache.getCache(); | 386 SkGlyphCache* cache = autoCache.getCache(); |
456 GrFontScaler* fontScaler = GetGrFontScaler(cache); | 387 GrFontScaler* fontScaler = GetGrFontScaler(cache); |
457 | |
458 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture); | |
459 | 388 |
460 // need to measure first | 389 // need to measure first |
461 // TODO - generate positions and pre-load cache as well? | 390 // TODO - generate positions and pre-load cache as well? |
462 const char* stop = text + byteLength; | 391 const char* stop = text + byteLength; |
463 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { | 392 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
464 SkFixed stopX = 0; | 393 SkFixed stopX = 0; |
465 SkFixed stopY = 0; | 394 SkFixed stopY = 0; |
466 | 395 |
467 const char* textPtr = text; | 396 const char* textPtr = text; |
468 while (textPtr < stop) { | 397 while (textPtr < stop) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 | 448 |
520 // nothing to draw | 449 // nothing to draw |
521 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/)
{ | 450 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/)
{ |
522 return; | 451 return; |
523 } | 452 } |
524 | 453 |
525 this->init(paint, skPaint); | 454 this->init(paint, skPaint); |
526 | 455 |
527 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); | 456 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
528 | 457 |
529 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL); | 458 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); |
530 SkGlyphCache* cache = autoCache.getCache(); | 459 SkGlyphCache* cache = autoCache.getCache(); |
531 GrFontScaler* fontScaler = GetGrFontScaler(cache); | 460 GrFontScaler* fontScaler = GetGrFontScaler(cache); |
532 | |
533 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture); | |
534 | 461 |
535 const char* stop = text + byteLength; | 462 const char* stop = text + byteLength; |
536 | 463 |
537 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { | 464 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
538 while (text < stop) { | 465 while (text < stop) { |
539 // the last 2 parameters are ignored | 466 // the last 2 parameters are ignored |
540 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); | 467 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
541 | 468 |
542 if (glyph.fWidth) { | 469 if (glyph.fWidth) { |
543 SkScalar x = pos[0]; | 470 SkScalar x = pos[0]; |
(...skipping 24 matching lines...) Expand all Loading... |
568 SkScalarToFixed(x) - (glyph.fAdvanceX >> a
lignShift), | 495 SkScalarToFixed(x) - (glyph.fAdvanceX >> a
lignShift), |
569 SkScalarToFixed(y) - (glyph.fAdvanceY >> a
lignShift), | 496 SkScalarToFixed(y) - (glyph.fAdvanceY >> a
lignShift), |
570 fontScaler); | 497 fontScaler); |
571 } | 498 } |
572 pos += scalarsPerPosition; | 499 pos += scalarsPerPosition; |
573 } | 500 } |
574 } | 501 } |
575 | 502 |
576 this->finish(); | 503 this->finish(); |
577 } | 504 } |
OLD | NEW |