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