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

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

Issue 41213003: Hook in rough distance field support for fonts (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Port over fix for alpha-blended paint color (may not be needed). Created 7 years, 1 month 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 | Annotate | Revision Log
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 "GrBitmapTextContext.h" 8 #include "GrDistanceFieldTextContext.h"
9 #include "GrAtlas.h" 9 #include "GrAtlas.h"
10 #include "GrDrawTarget.h" 10 #include "GrDrawTarget.h"
11 #include "GrFontScaler.h" 11 #include "GrFontScaler.h"
12 #include "GrIndexBuffer.h" 12 #include "GrIndexBuffer.h"
13 #include "GrTextStrike.h" 13 #include "GrTextStrike.h"
14 #include "GrTextStrike_impl.h" 14 #include "GrTextStrike_impl.h"
15 #include "SkColorPriv.h"
16 #include "SkPath.h" 15 #include "SkPath.h"
17 #include "SkRTConf.h" 16 #include "SkRTConf.h"
18 #include "SkStrokeRec.h" 17 #include "SkStrokeRec.h"
19 #include "effects/GrCustomCoordsTextureEffect.h" 18 #include "effects/GrDistanceFieldTextureEffect.h"
20 19
21 static const int kGlyphCoordsAttributeIndex = 1; 20 static const int kGlyphCoordsAttributeIndex = 1;
22 21
23 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, 22 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false,
24 "Dump the contents of the font cache before every purge."); 23 "Dump the contents of the font cache before every purge.");
25 24
26 GrBitmapTextContext::GrBitmapTextContext(GrContext* context, const GrPaint& pain t,
27 SkColor color) :
28 GrTextContext(context, paint) {
29 fAutoMatrix.setIdentity(fContext, &fPaint);
30 25
26 GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
27 const GrPaint& paint,
28 SkColor color,
29 SkScalar textSize) :
30 GrTextContext(context, paint),
31 fTextSize(textSize) {
31 fSkPaintColor = color; 32 fSkPaintColor = color;
32 33
33 fStrike = NULL; 34 fStrike = NULL;
34 35
35 fCurrTexture = NULL; 36 fCurrTexture = NULL;
36 fCurrVertex = 0; 37 fCurrVertex = 0;
37 38
38 fVertices = NULL; 39 fVertices = NULL;
39 fMaxVertices = 0; 40 fMaxVertices = 0;
40 } 41 }
41 42
42 GrBitmapTextContext::~GrBitmapTextContext() { 43 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
43 this->flushGlyphs(); 44 this->flushGlyphs();
44 } 45 }
45 46
46 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { 47 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
47 unsigned r = SkColorGetR(c); 48 unsigned r = SkColorGetR(c);
48 unsigned g = SkColorGetG(c); 49 unsigned g = SkColorGetG(c);
49 unsigned b = SkColorGetB(c); 50 unsigned b = SkColorGetB(c);
50 return GrColorPackRGBA(r, g, b, 0xff); 51 return GrColorPackRGBA(r, g, b, 0xff);
51 } 52 }
52 53
53 void GrBitmapTextContext::flushGlyphs() { 54 void GrDistanceFieldTextContext::flushGlyphs() {
54 if (NULL == fDrawTarget) { 55 if (NULL == fDrawTarget) {
55 return; 56 return;
56 } 57 }
57 58
58 GrDrawState* drawState = fDrawTarget->drawState(); 59 GrDrawState* drawState = fDrawTarget->drawState();
59 GrDrawState::AutoRestoreEffects are(drawState); 60 GrDrawState::AutoRestoreEffects are(drawState);
60 drawState->setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); 61 SkMatrix transform;
62 transform.reset();
bsalomon 2013/11/04 20:34:53 why reset? also do we need to make a copy of the m
jvanverth1 2013/11/05 14:59:11 No idea why this was this way. Done.
63 transform = fContext->getMatrix();
64 drawState->setFromPaint(fPaint, transform, fContext->getRenderTarget());
61 65
62 if (fCurrVertex > 0) { 66 if (fCurrVertex > 0) {
63 // setup our sampler state for our text texture/atlas 67 // setup our sampler state for our text texture/atlas
64 SkASSERT(GrIsALIGN4(fCurrVertex)); 68 SkASSERT(GrIsALIGN4(fCurrVertex));
65 SkASSERT(fCurrTexture); 69 SkASSERT(fCurrTexture);
66 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNon e_FilterMode); 70 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBil erp_FilterMode);
67 71
68 // This effect could be stored with one of the cache objects (atlas?) 72 // This effect could be stored with one of the cache objects (atlas?)
69 drawState->addCoverageEffect( 73 drawState->addCoverageEffect(
70 GrCustomCoordsTextureEffect::Create(fCurrTexture , params), 74 GrDistanceFieldTextureEffect::Create(fCurrTextur e, params),
71 kGlyphCoordsAttributeIndex)->unref(); 75 kGlyphCoordsAttributeIndex)->unref();
72 76
73 if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { 77 if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) {
74 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || 78 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
75 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || 79 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
76 fPaint.numColorStages()) { 80 fPaint.numColorStages()) {
77 GrPrintf("LCD Text will not draw correctly.\n"); 81 GrPrintf("LCD Text will not draw correctly.\n");
78 } 82 }
79 // We don't use the GrPaint's color in this case because it's been p remultiplied by 83 // We don't use the GrPaint's color in this case because it's been p remultiplied by
80 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by 84 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
81 // the mask texture color. The end result is that we get 85 // the mask texture color. The end result is that we get
82 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo lor 86 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo lor
83 int a = SkColorGetA(fSkPaintColor); 87 int a = SkColorGetA(fSkPaintColor);
84 // paintAlpha 88 // paintAlpha
85 drawState->setColor(SkColorSetARGB(a, a, a, a)); 89 drawState->setColor(SkColorSetARGB(a, a, a, a));
86 // paintColor 90 // paintColor
87 drawState->setBlendConstant(skcolor_to_grcolor_nopremultiply(fSkPain tColor)); 91 drawState->setBlendConstant(skcolor_to_grcolor_nopremultiply(fSkPain tColor));
88 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); 92 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
89 } else { 93 } else {
90 // set back to normal in case we took LCD path previously. 94 // set back to normal in case we took LCD path previously.
91 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen dCoeff()); 95 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen dCoeff());
92 drawState->setColor(fPaint.getColor()); 96 drawState->setColor(fPaint.getColor());
93 } 97 }
94 98
95 int nGlyphs = fCurrVertex / 4; 99 int nGlyphs = fCurrVertex / 4;
96 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); 100 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
97 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, 101 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
98 nGlyphs, 102 nGlyphs,
99 4, 6); 103 4, 6);
100
101 fDrawTarget->resetVertexSource(); 104 fDrawTarget->resetVertexSource();
102 fVertices = NULL; 105 fVertices = NULL;
103 fMaxVertices = 0; 106 fMaxVertices = 0;
104 fCurrVertex = 0; 107 fCurrVertex = 0;
105 SkSafeSetNull(fCurrTexture); 108 SkSafeSetNull(fCurrTexture);
106 } 109 }
107 } 110 }
108 111
109 namespace { 112 namespace {
110 113
111 // position + texture coord 114 // position + texture coord
112 extern const GrVertexAttrib gTextVertexAttribs[] = { 115 extern const GrVertexAttrib gTextVertexAttribs[] = {
113 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }, 116 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
114 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} 117 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
115 }; 118 };
116 119
117 }; 120 };
118 121
119 void GrBitmapTextContext::drawPackedGlyph(GrGlyph::PackedID packed, 122 void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
120 GrFixed vx, GrFixed vy, 123 GrFixed vx, GrFixed vy,
121 GrFontScaler* scaler) { 124 GrFontScaler* scaler) {
122 if (NULL == fDrawTarget) { 125 if (NULL == fDrawTarget) {
123 return; 126 return;
124 } 127 }
125 if (NULL == fStrike) { 128 if (NULL == fStrike) {
126 fStrike = fContext->getFontCache()->getStrike(scaler); 129 fStrike = fContext->getFontCache()->getStrike(scaler, true);
127 } 130 }
128 131
129 GrGlyph* glyph = fStrike->getGlyph(packed, scaler); 132 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
130 if (NULL == glyph || glyph->fBounds.isEmpty()) { 133 if (NULL == glyph || glyph->fBounds.isEmpty()) {
131 return; 134 return;
132 } 135 }
133 136
137 SkScalar sx = SkFixedToScalar(vx);
138 SkScalar sy = SkFixedToScalar(vy);
139 /*
140 // not valid, need to find a different solution for this
134 vx += SkIntToFixed(glyph->fBounds.fLeft); 141 vx += SkIntToFixed(glyph->fBounds.fLeft);
135 vy += SkIntToFixed(glyph->fBounds.fTop); 142 vy += SkIntToFixed(glyph->fBounds.fTop);
136 143
137 // keep them as ints until we've done the clip-test 144 // keep them as ints until we've done the clip-test
138 GrFixed width = glyph->fBounds.width(); 145 GrFixed width = glyph->fBounds.width();
139 GrFixed height = glyph->fBounds.height(); 146 GrFixed height = glyph->fBounds.height();
140 147
141 // check if we clipped out 148 // check if we clipped out
142 if (true || NULL == glyph->fPlot) { 149 if (true || NULL == glyph->fPlot) {
143 int x = vx >> 16; 150 int x = vx >> 16;
144 int y = vy >> 16; 151 int y = vy >> 16;
145 if (fClipRect.quickReject(x, y, x + width, y + height)) { 152 if (fClipRect.quickReject(x, y, x + width, y + height)) {
146 // SkCLZ(3); // so we can set a break-point in the debugger 153 // SkCLZ(3); // so we can set a break-point in the debugger
147 return; 154 return;
148 } 155 }
149 } 156 }
150 157 */
151 if (NULL == glyph->fPlot) { 158 if (NULL == glyph->fPlot) {
152 if (fStrike->getGlyphAtlas(glyph, scaler)) { 159 if (fStrike->getGlyphAtlas(glyph, scaler)) {
153 goto HAS_ATLAS; 160 goto HAS_ATLAS;
154 } 161 }
155 162
156 // try to clear out an unused plot before we flush 163 // try to clear out an unused plot before we flush
157 fContext->getFontCache()->freePlotExceptFor(fStrike); 164 fContext->getFontCache()->freePlotExceptFor(fStrike);
158 if (fStrike->getGlyphAtlas(glyph, scaler)) { 165 if (fStrike->getGlyphAtlas(glyph, scaler)) {
159 goto HAS_ATLAS; 166 goto HAS_ATLAS;
160 } 167 }
(...skipping 20 matching lines...) Expand all
181 if (!scaler->getGlyphPath(glyph->glyphID(), path)) { 188 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
182 // flag the glyph as being dead? 189 // flag the glyph as being dead?
183 delete path; 190 delete path;
184 return; 191 return;
185 } 192 }
186 glyph->fPath = path; 193 glyph->fPath = path;
187 } 194 }
188 195
189 GrContext::AutoMatrix am; 196 GrContext::AutoMatrix am;
190 SkMatrix translate; 197 SkMatrix translate;
191 translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds. fLeft)), 198 translate.setTranslate(sx, sy);
192 SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds. fTop)));
193 GrPaint tmpPaint(fPaint); 199 GrPaint tmpPaint(fPaint);
194 am.setPreConcat(fContext, translate, &tmpPaint); 200 am.setPreConcat(fContext, translate, &tmpPaint);
195 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); 201 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
196 fContext->drawPath(tmpPaint, *glyph->fPath, stroke); 202 fContext->drawPath(tmpPaint, *glyph->fPath, stroke);
197 return; 203 return;
198 } 204 }
199 205
200 HAS_ATLAS: 206 HAS_ATLAS:
201 SkASSERT(glyph->fPlot); 207 SkASSERT(glyph->fPlot);
202 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); 208 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
203 glyph->fPlot->setDrawToken(drawToken); 209 glyph->fPlot->setDrawToken(drawToken);
204 210
205 // now promote them to fixed (TODO: Rethink using fixed pt).
206 width = SkIntToFixed(width);
207 height = SkIntToFixed(height);
208
209 GrTexture* texture = glyph->fPlot->texture(); 211 GrTexture* texture = glyph->fPlot->texture();
210 SkASSERT(texture); 212 SkASSERT(texture);
211 213
212 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { 214 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
213 this->flushGlyphs(); 215 this->flushGlyphs();
214 fCurrTexture = texture; 216 fCurrTexture = texture;
215 fCurrTexture->ref(); 217 fCurrTexture->ref();
216 } 218 }
217 219
218 if (NULL == fVertices) { 220 if (NULL == fVertices) {
(...skipping 21 matching lines...) Expand all
240 fMaxVertices = maxQuadVertices; 242 fMaxVertices = maxQuadVertices;
241 } 243 }
242 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices, 244 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
243 0, 245 0,
244 GrTCast<void**>(& fVertices), 246 GrTCast<void**>(& fVertices),
245 NULL); 247 NULL);
246 GrAlwaysAssert(success); 248 GrAlwaysAssert(success);
247 SkASSERT(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize( )); 249 SkASSERT(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize( ));
248 } 250 }
249 251
252 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft);
253 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop);
254 SkScalar width = SkIntToScalar(glyph->fBounds.width());
255 SkScalar height = SkIntToScalar(glyph->fBounds.height());
256
257 SkScalar scale = fTextSize/32.f;
258 dx *= scale;
259 dy *= scale;
260 sx += dx;
261 sy += dy;
262 width *= scale;
263 height *= scale;
264
250 GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); 265 GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX);
251 GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); 266 GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY);
267 GrFixed tw = SkIntToFixed(glyph->fBounds.width());
268 GrFixed th = SkIntToFixed(glyph->fBounds.height());
252 269
253 fVertices[2*fCurrVertex].setRectFan(SkFixedToFloat(vx), 270 fVertices[2*fCurrVertex].setRectFan(sx,
254 SkFixedToFloat(vy), 271 sy,
255 SkFixedToFloat(vx + width), 272 sx + width,
256 SkFixedToFloat(vy + height), 273 sy + height,
257 2 * sizeof(SkPoint)); 274 2 * sizeof(SkPoint));
258 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixed X(tx)), 275 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToScalar(texture->normalizeFixe dX(tx)),
bsalomon 2013/11/04 20:34:53 In this case I think ToFloat is correct since the
jvanverth1 2013/11/05 14:59:11 Done.
259 SkFixedToFloat(texture->normalizeFixed Y(ty)), 276 SkFixedToScalar(texture->normalizeFixe dY(ty)),
260 SkFixedToFloat(texture->normalizeFixed X(tx + width)), 277 SkFixedToScalar(texture->normalizeFixe dX(tx + tw)),
261 SkFixedToFloat(texture->normalizeFixed Y(ty + height)), 278 SkFixedToScalar(texture->normalizeFixe dY(ty + th)),
262 2 * sizeof(SkPoint)); 279 2 * sizeof(SkPoint));
263 fCurrVertex += 4; 280 fCurrVertex += 4;
264 } 281 }
OLDNEW
« gyp/gpu.gyp ('K') | « src/gpu/GrBitmapTextContext.cpp ('k') | src/gpu/GrTextStrike.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698