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

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: Unref gamma texture 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"
(...skipping 12 matching lines...) Expand all
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
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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 } else if (fSkPaint.getTextSize() <= kMediumDFFontLimit) { 374 } else if (fSkPaint.getTextSize() <= kMediumDFFontLimit) {
348 fTextRatio = fSkPaint.getTextSize()/kMediumDFFontSize; 375 fTextRatio = fSkPaint.getTextSize()/kMediumDFFontSize;
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.setDistanceFieldTextTEMP(true);
358 fSkPaint.setAutohinted(false);
359 fSkPaint.setSubpixelText(true);
360 } 385 }
361 386
362 inline void GrDistanceFieldTextContext::finish() { 387 inline void GrDistanceFieldTextContext::finish() {
363 flushGlyphs(); 388 flushGlyphs();
364 389
365 GrTextContext::finish(); 390 GrTextContext::finish();
366 } 391 }
367 392
393 static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
394 const SkDeviceProperties& deviceProperties,
395 GrTexture** gammaTexture) {
396 if (NULL == *gammaTexture) {
397 int width, height;
398 size_t size;
399
400 #ifdef SK_GAMMA_CONTRAST
401 SkScalar contrast = SK_GAMMA_CONTRAST;
402 #else
403 SkScalar contrast = 0.5f;
404 #endif
405 SkScalar paintGamma = deviceProperties.fGamma;
406 SkScalar deviceGamma = deviceProperties.fGamma;
407
408 size = cache->getScalerContext()->getGammaLUTSize(contrast, paintGamma, deviceGamma,
409 &width, &height);
410
411 SkAutoTArray<uint8_t> data((int)size);
412 cache->getScalerContext()->getGammaLUTData(contrast, paintGamma, deviceG amma, data.get());
413
414 // TODO: Update this to use the cache rather than directly creating a te xture.
415 GrTextureDesc desc;
416 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
417 desc.fWidth = width;
418 desc.fHeight = height;
419 desc.fConfig = kAlpha_8_GrPixelConfig;
420
421 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
422 if (NULL == *gammaTexture) {
423 return;
424 }
425
426 context->writeTexturePixels(*gammaTexture,
427 0, 0, width, height,
428 (*gammaTexture)->config(), data.get(), 0,
429 GrContext::kDontFlush_PixelOpsFlag);
430 }
431 }
432
368 void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& s kPaint, 433 void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& s kPaint,
369 const char text[], size_t byteLength, 434 const char text[], size_t byteLength,
370 SkScalar x, SkScalar y) { 435 SkScalar x, SkScalar y) {
371 SkASSERT(byteLength == 0 || text != NULL); 436 SkASSERT(byteLength == 0 || text != NULL);
372 437
373 // nothing to draw or can't draw 438 // nothing to draw or can't draw
374 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/ 439 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
375 || fSkPaint.getRasterizer()) { 440 || fSkPaint.getRasterizer()) {
376 return; 441 return;
377 } 442 }
378 443
379 this->init(paint, skPaint); 444 this->init(paint, skPaint);
380 445
381 SkScalar sizeRatio = fTextRatio; 446 SkScalar sizeRatio = fTextRatio;
382 447
383 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); 448 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
384 449
385 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); 450 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL);
386 SkGlyphCache* cache = autoCache.getCache(); 451 SkGlyphCache* cache = autoCache.getCache();
387 GrFontScaler* fontScaler = GetGrFontScaler(cache); 452 GrFontScaler* fontScaler = GetGrFontScaler(cache);
388 453
454 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
455
389 // need to measure first 456 // need to measure first
390 // TODO - generate positions and pre-load cache as well? 457 // TODO - generate positions and pre-load cache as well?
391 const char* stop = text + byteLength; 458 const char* stop = text + byteLength;
392 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { 459 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
393 SkFixed stopX = 0; 460 SkFixed stopX = 0;
394 SkFixed stopY = 0; 461 SkFixed stopY = 0;
395 462
396 const char* textPtr = text; 463 const char* textPtr = text;
397 while (textPtr < stop) { 464 while (textPtr < stop) {
398 // don't need x, y here, since all subpixel variants will have the 465 // 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 } 519 }
453 520
454 this->init(paint, skPaint); 521 this->init(paint, skPaint);
455 522
456 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); 523 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
457 524
458 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); 525 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL);
459 SkGlyphCache* cache = autoCache.getCache(); 526 SkGlyphCache* cache = autoCache.getCache();
460 GrFontScaler* fontScaler = GetGrFontScaler(cache); 527 GrFontScaler* fontScaler = GetGrFontScaler(cache);
461 528
529 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
530
462 const char* stop = text + byteLength; 531 const char* stop = text + byteLength;
463 532
464 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { 533 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
465 while (text < stop) { 534 while (text < stop) {
466 // the last 2 parameters are ignored 535 // the last 2 parameters are ignored
467 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); 536 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
468 537
469 if (glyph.fWidth) { 538 if (glyph.fWidth) {
470 SkScalar x = pos[0]; 539 SkScalar x = pos[0];
471 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1]; 540 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
(...skipping 23 matching lines...) Expand all
495 SkScalarToFixed(x) - (glyph.fAdvanceX >> a lignShift), 564 SkScalarToFixed(x) - (glyph.fAdvanceX >> a lignShift),
496 SkScalarToFixed(y) - (glyph.fAdvanceY >> a lignShift), 565 SkScalarToFixed(y) - (glyph.fAdvanceY >> a lignShift),
497 fontScaler); 566 fontScaler);
498 } 567 }
499 pos += scalarsPerPosition; 568 pos += scalarsPerPosition;
500 } 569 }
501 } 570 }
502 571
503 this->finish(); 572 this->finish();
504 } 573 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698