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