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 424103002: Add effect caching to distance field text. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase to ToT Created 6 years, 4 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
« no previous file with comments | « src/gpu/GrDistanceFieldTextContext.h ('k') | src/gpu/effects/GrDistanceFieldTextureEffect.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "SkColorFilter.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 : GrTextContext(context, pro perties) { 59 : GrTextContext(context, pro perties) {
60 #if SK_FORCE_DISTANCEFIELD_FONTS 60 #if SK_FORCE_DISTANCEFIELD_FONTS
61 fEnableDFRendering = true; 61 fEnableDFRendering = true;
62 #else 62 #else
63 fEnableDFRendering = enable; 63 fEnableDFRendering = enable;
64 #endif 64 #endif
65 fStrike = NULL; 65 fStrike = NULL;
66 fGammaTexture = NULL; 66 fGammaTexture = NULL;
67 67
68 fCurrVertex = 0; 68 fCurrVertex = 0;
69 69 fEffectTextureUniqueID = SK_InvalidUniqueID;
70 fEffectColor = GrColor_ILLEGAL;
71 fEffectFlags = 0;
72
70 fVertices = NULL; 73 fVertices = NULL;
71 } 74 }
72 75
73 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { 76 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
74 this->flushGlyphs(); 77 this->flushGlyphs();
75 SkSafeSetNull(fGammaTexture); 78 SkSafeSetNull(fGammaTexture);
76 } 79 }
77 80
78 bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) { 81 bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
79 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) { 82 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
(...skipping 24 matching lines...) Expand all
104 return rec.getFormat() != SkMask::kARGB32_Format; 107 return rec.getFormat() != SkMask::kARGB32_Format;
105 } 108 }
106 109
107 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { 110 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
108 unsigned r = SkColorGetR(c); 111 unsigned r = SkColorGetR(c);
109 unsigned g = SkColorGetG(c); 112 unsigned g = SkColorGetG(c);
110 unsigned b = SkColorGetB(c); 113 unsigned b = SkColorGetB(c);
111 return GrColorPackRGBA(r, g, b, 0xff); 114 return GrColorPackRGBA(r, g, b, 0xff);
112 } 115 }
113 116
117 void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColo r) {
118 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_ FilterMode);
119 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNon e_FilterMode);
120
121 GrTexture* currTexture = fStrike->getTexture();
122 SkASSERT(currTexture);
123 uint32_t textureUniqueID = currTexture->getUniqueID();
124
125 // set up any flags
126 uint32_t flags = 0;
127 flags |= fContext->getMatrix().isSimilarity() ? kSimilarity_DistanceFieldEff ectFlag : 0;
128 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
129 flags |= fUseLCDText && fContext->getMatrix().rectStaysRect() ?
130 kRectToRect_DistanceFieldEffectFlag : 0;
131 bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout ==
132 fDeviceProperties.fGeometry.getLayout();
133 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
134
135 // see if we need to create a new effect
136 if (textureUniqueID != fEffectTextureUniqueID ||
137 filteredColor != fEffectColor ||
138 flags != fEffectFlags) {
139 if (fUseLCDText) {
140 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredCol or);
141 fCachedEffect.reset(GrDistanceFieldLCDTextureEffect::Create(currText ure,
142 params,
143 fGammaTe xture,
144 gammaPar ams,
145 colorNoP reMul,
146 flags));
147 } else {
148 #ifdef SK_GAMMA_APPLY_TO_A8
149 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDevicePropertie s.fGamma,
150 filteredColor);
151 fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(currTexture ,
152 params,
153 fGammaTextu re,
154 gammaParams ,
155 lum/255.f,
156 flags));
157 #else
158 fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(currTexture ,
159 params, fla gs));
160 #endif
161 }
162 fEffectTextureUniqueID = textureUniqueID;
163 fEffectColor = filteredColor;
164 fEffectFlags = flags;
165 }
166
167 }
168
114 void GrDistanceFieldTextContext::flushGlyphs() { 169 void GrDistanceFieldTextContext::flushGlyphs() {
115 if (NULL == fDrawTarget) { 170 if (NULL == fDrawTarget) {
116 return; 171 return;
117 } 172 }
118 173
119 GrDrawState* drawState = fDrawTarget->drawState(); 174 GrDrawState* drawState = fDrawTarget->drawState();
120 GrDrawState::AutoRestoreEffects are(drawState); 175 GrDrawState::AutoRestoreEffects are(drawState);
121 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTa rget()); 176 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTa rget());
122 177
123 if (fCurrVertex > 0) { 178 if (fCurrVertex > 0) {
124 // setup our sampler state for our text texture/atlas 179 // setup our sampler state for our text texture/atlas
125 SkASSERT(SkIsAlign4(fCurrVertex)); 180 SkASSERT(SkIsAlign4(fCurrVertex));
126 GrTexture* currTexture = fStrike->getTexture();
127 SkASSERT(currTexture);
128 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBil erp_FilterMode);
129 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams:: kNone_FilterMode);
130 181
131 // Effects could be stored with one of the cache objects (atlas?) 182 // get our current color
132 int coordsIdx = drawState->hasColorVertexAttribute() ? kGlyphCoordsWithC olorAttributeIndex :
133 kGlyphCoordsNoCol orAttributeIndex;
134 SkColor filteredColor; 183 SkColor filteredColor;
135 SkColorFilter* colorFilter = fSkPaint.getColorFilter(); 184 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
136 if (NULL != colorFilter) { 185 if (NULL != colorFilter) {
137 filteredColor = colorFilter->filterColor(fSkPaint.getColor()); 186 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
138 } else { 187 } else {
139 filteredColor = fSkPaint.getColor(); 188 filteredColor = fSkPaint.getColor();
140 } 189 }
190 this->setupCoverageEffect(filteredColor);
191
192 // Effects could be stored with one of the cache objects (atlas?)
193 int coordsIdx = drawState->hasColorVertexAttribute() ? kGlyphCoordsWithC olorAttributeIndex :
194 kGlyphCoordsNoCol orAttributeIndex;
195 drawState->addCoverageEffect(fCachedEffect.get(), coordsIdx);
196
197 // Set draw state
141 if (fUseLCDText) { 198 if (fUseLCDText) {
142 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredCol or); 199 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredCol or);
143 bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout ==
144 fDeviceProperties.fG eometry.getLayout();
145 drawState->addCoverageEffect(GrDistanceFieldLCDTextureEffect::Create (
146 currTexture,
147 params,
148 fGammaTexture,
149 gammaParams,
150 colorNoPreMul,
151 fContext->getMatrix( ).rectStaysRect() &&
152 fContext->getMatrix( ).isSimilarity(),
153 useBGR),
154 coordsIdx)->unref();
155
156 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || 200 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
157 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || 201 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
158 fPaint.numColorStages()) { 202 fPaint.numColorStages()) {
159 GrPrintf("LCD Text will not draw correctly.\n"); 203 GrPrintf("LCD Text will not draw correctly.\n");
160 } 204 }
161 SkASSERT(!drawState->hasColorVertexAttribute()); 205 SkASSERT(!drawState->hasColorVertexAttribute());
162 // We don't use the GrPaint's color in this case because it's been p remultiplied by 206 // We don't use the GrPaint's color in this case because it's been p remultiplied by
163 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by 207 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
164 // the mask texture color. The end result is that we get 208 // the mask texture color. The end result is that we get
165 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo lor 209 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo lor
166 int a = SkColorGetA(fSkPaint.getColor()); 210 int a = SkColorGetA(fSkPaint.getColor());
167 // paintAlpha 211 // paintAlpha
168 drawState->setColor(SkColorSetARGB(a, a, a, a)); 212 drawState->setColor(SkColorSetARGB(a, a, a, a));
169 // paintColor 213 // paintColor
170 drawState->setBlendConstant(colorNoPreMul); 214 drawState->setBlendConstant(colorNoPreMul);
171 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); 215 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
172 } else { 216 } else {
173 #ifdef SK_GAMMA_APPLY_TO_A8
174 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDevicePropertie s.fGamma,
175 filteredColor);
176 drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create(
177 currTexture, param s,
178 fGammaTexture, gam maParams,
179 lum/255.f,
180 fContext->getMatri x().isSimilarity()),
181 coordsIdx)->unref();
182 #else
183 drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create(
184 currTexture, param s,
185 fContext->getMatri x().isSimilarity()),
186 coordsIdx)->unref();
187 #endif
188 // set back to normal in case we took LCD path previously. 217 // set back to normal in case we took LCD path previously.
189 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen dCoeff()); 218 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen dCoeff());
190 //drawState->setColor(fPaint.getColor()); 219 //drawState->setColor(fPaint.getColor());
191 // We're using per-vertex color. 220 // We're using per-vertex color.
192 SkASSERT(drawState->hasColorVertexAttribute()); 221 SkASSERT(drawState->hasColorVertexAttribute());
193 drawState->setColor(0xFFFFFFFF); 222 drawState->setColor(0xFFFFFFFF);
194 } 223 }
195 int nGlyphs = fCurrVertex / 4; 224 int nGlyphs = fCurrVertex / 4;
196 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); 225 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
197 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, 226 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 SkScalarToFixed(x) - (glyph.fAdvanceX >> a lignShift), 619 SkScalarToFixed(x) - (glyph.fAdvanceX >> a lignShift),
591 SkScalarToFixed(y) - (glyph.fAdvanceY >> a lignShift), 620 SkScalarToFixed(y) - (glyph.fAdvanceY >> a lignShift),
592 fontScaler); 621 fontScaler);
593 } 622 }
594 pos += scalarsPerPosition; 623 pos += scalarsPerPosition;
595 } 624 }
596 } 625 }
597 626
598 this->finish(); 627 this->finish();
599 } 628 }
OLDNEW
« no previous file with comments | « src/gpu/GrDistanceFieldTextContext.h ('k') | src/gpu/effects/GrDistanceFieldTextureEffect.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698