Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(630)

Side by Side Diff: src/gpu/GrDistanceFieldTextContext.cpp

Issue 258883002: Gamma correction for distance field text. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add comments for the magic bold factor Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
24 26
25 static const int kGlyphCoordsAttributeIndex = 1; 27 static const int kGlyphCoordsAttributeIndex = 1;
26 28
27 static const int kSmallDFFontSize = 32; 29 static const int kSmallDFFontSize = 32;
28 static const int kSmallDFFontLimit = 32; 30 static const int kSmallDFFontLimit = 32;
29 static const int kMediumDFFontSize = 64; 31 static const int kMediumDFFontSize = 64;
30 static const int kMediumDFFontLimit = 64; 32 static const int kMediumDFFontLimit = 64;
31 static const int kLargeDFFontSize = 128; 33 static const int kLargeDFFontSize = 128;
32 34
33 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, 35 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false,
34 "Dump the contents of the font cache before every purge."); 36 "Dump the contents of the font cache before every purge.");
35 37
38 GrTexture* gGammaTexture = NULL;
bsalomon 2014/05/28 19:53:53 This doesn't seem safe. There can be multiple GrCo
jvanverth1 2014/05/29 18:53:49 Changed to be a member of GrDistanceFieldTextConte
39
36 GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context, 40 GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
37 const SkDeviceProperties& properties, 41 const SkDeviceProperties& properties,
38 bool enable) 42 bool enable)
39 : GrTextContext(context, pro perties) { 43 : GrTextContext(context, pro perties) {
40 #if SK_FORCE_DISTANCEFIELD_FONTS 44 #if SK_FORCE_DISTANCEFIELD_FONTS
41 fEnableDFRendering = true; 45 fEnableDFRendering = true;
42 #else 46 #else
43 fEnableDFRendering = enable; 47 fEnableDFRendering = enable;
44 #endif 48 #endif
45 fStrike = NULL; 49 fStrike = NULL;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 gGammaTexture,
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 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDevicePropertie s.fGamma,
156 filteredColor);
157 drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create(
158 fCurrTexture, para ms,
159 gGammaTexture, gam maParams,
160 lum/255.f,
140 fContext->getMatri x().isSimilarity()), 161 fContext->getMatri x().isSimilarity()),
141 kGlyphCoordsAttributeIndex)->unref(); 162 kGlyphCoordsAttributeIndex)->unref();
142 163
143 // set back to normal in case we took LCD path previously. 164 // set back to normal in case we took LCD path previously.
144 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen dCoeff()); 165 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen dCoeff());
145 drawState->setColor(fPaint.getColor()); 166 drawState->setColor(fPaint.getColor());
146 } 167 }
147 168
148 int nGlyphs = fCurrVertex / 4; 169 int nGlyphs = fCurrVertex / 4;
149 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); 170 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 } else if (fSkPaint.getTextSize() <= kMediumDFFontLimit) { 368 } else if (fSkPaint.getTextSize() <= kMediumDFFontLimit) {
348 fTextRatio = fSkPaint.getTextSize()/kMediumDFFontSize; 369 fTextRatio = fSkPaint.getTextSize()/kMediumDFFontSize;
349 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize)); 370 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
350 } else { 371 } else {
351 fTextRatio = fSkPaint.getTextSize()/kLargeDFFontSize; 372 fTextRatio = fSkPaint.getTextSize()/kLargeDFFontSize;
352 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize)); 373 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
353 } 374 }
354 375
355 fUseLCDText = fSkPaint.isLCDRenderText(); 376 fUseLCDText = fSkPaint.isLCDRenderText();
356 377
357 fSkPaint.setLCDRenderText(false); 378 fSkPaint.setDistanceFieldTextTEMP(true);
358 fSkPaint.setAutohinted(false);
359 fSkPaint.setSubpixelText(true);
360 } 379 }
361 380
362 inline void GrDistanceFieldTextContext::finish() { 381 inline void GrDistanceFieldTextContext::finish() {
363 flushGlyphs(); 382 flushGlyphs();
364 383
365 GrTextContext::finish(); 384 GrTextContext::finish();
366 } 385 }
367 386
387 static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
388 const SkDeviceProperties& deviceProperties) {
389 if (!gGammaTexture) {
reed1 2014/05/28 19:45:17 Seems like we need thread-protection for writing t
jvanverth1 2014/05/29 18:53:49 There should be only one GrDistanceFieldTextContex
390 int width, height;
391 SkScalar contrast = 0.5f;
392 SkScalar paintGamma = deviceProperties.fGamma;
393 SkScalar deviceGamma = deviceProperties.fGamma;
394 void *data = cache->getScalerContext()->getGammaLUTData(width, height, c ontrast,
395 paintGamma, devi ceGamma);
396
397 // TODO: Update this to use the cache rather than directly creating a te xture.
398 GrTextureDesc desc;
399 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
400 desc.fWidth = width;
401 desc.fHeight = height;
402 desc.fConfig = kAlpha_8_GrPixelConfig;
403
404 gGammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
405 if (NULL == gGammaTexture) {
406 return;
407 }
408
409 context->writeTexturePixels(gGammaTexture,
410 0, 0, width, height,
411 gGammaTexture->config(), data, 0,
412 GrContext::kDontFlush_PixelOpsFlag);
413 }
414 }
415
368 void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& s kPaint, 416 void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& s kPaint,
369 const char text[], size_t byteLength, 417 const char text[], size_t byteLength,
370 SkScalar x, SkScalar y) { 418 SkScalar x, SkScalar y) {
371 SkASSERT(byteLength == 0 || text != NULL); 419 SkASSERT(byteLength == 0 || text != NULL);
372 420
373 // nothing to draw or can't draw 421 // nothing to draw or can't draw
374 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/ 422 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
375 || fSkPaint.getRasterizer()) { 423 || fSkPaint.getRasterizer()) {
376 return; 424 return;
377 } 425 }
378 426
379 this->init(paint, skPaint); 427 this->init(paint, skPaint);
380 428
381 SkScalar sizeRatio = fTextRatio; 429 SkScalar sizeRatio = fTextRatio;
382 430
383 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); 431 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
384 432
385 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); 433 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL);
386 SkGlyphCache* cache = autoCache.getCache(); 434 SkGlyphCache* cache = autoCache.getCache();
387 GrFontScaler* fontScaler = GetGrFontScaler(cache); 435 GrFontScaler* fontScaler = GetGrFontScaler(cache);
388 436
437 setup_gamma_texture(fContext, cache, fDeviceProperties);
438
389 // need to measure first 439 // need to measure first
390 // TODO - generate positions and pre-load cache as well? 440 // TODO - generate positions and pre-load cache as well?
391 const char* stop = text + byteLength; 441 const char* stop = text + byteLength;
392 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { 442 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
393 SkFixed stopX = 0; 443 SkFixed stopX = 0;
394 SkFixed stopY = 0; 444 SkFixed stopY = 0;
395 445
396 const char* textPtr = text; 446 const char* textPtr = text;
397 while (textPtr < stop) { 447 while (textPtr < stop) {
398 // don't need x, y here, since all subpixel variants will have the 448 // 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
452 } 502 }
453 503
454 this->init(paint, skPaint); 504 this->init(paint, skPaint);
455 505
456 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); 506 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
457 507
458 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); 508 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL);
459 SkGlyphCache* cache = autoCache.getCache(); 509 SkGlyphCache* cache = autoCache.getCache();
460 GrFontScaler* fontScaler = GetGrFontScaler(cache); 510 GrFontScaler* fontScaler = GetGrFontScaler(cache);
461 511
512 setup_gamma_texture(fContext, cache, fDeviceProperties);
513
462 const char* stop = text + byteLength; 514 const char* stop = text + byteLength;
463 515
464 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { 516 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
465 while (text < stop) { 517 while (text < stop) {
466 // the last 2 parameters are ignored 518 // the last 2 parameters are ignored
467 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); 519 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
468 520
469 if (glyph.fWidth) { 521 if (glyph.fWidth) {
470 SkScalar x = pos[0]; 522 SkScalar x = pos[0];
471 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1]; 523 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
(...skipping 23 matching lines...) Expand all
495 SkScalarToFixed(x) - (glyph.fAdvanceX >> a lignShift), 547 SkScalarToFixed(x) - (glyph.fAdvanceX >> a lignShift),
496 SkScalarToFixed(y) - (glyph.fAdvanceY >> a lignShift), 548 SkScalarToFixed(y) - (glyph.fAdvanceY >> a lignShift),
497 fontScaler); 549 fontScaler);
498 } 550 }
499 pos += scalarsPerPosition; 551 pos += scalarsPerPosition;
500 } 552 }
501 } 553 }
502 554
503 this->finish(); 555 this->finish();
504 } 556 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698