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 "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 textRatio) |
| 30 : GrTextContext(context, pa
int) |
| 31 , fTextRatio(textRatio) { |
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 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTa
rget()); |
61 | 62 |
62 if (fCurrVertex > 0) { | 63 if (fCurrVertex > 0) { |
63 // setup our sampler state for our text texture/atlas | 64 // setup our sampler state for our text texture/atlas |
64 SkASSERT(GrIsALIGN4(fCurrVertex)); | 65 SkASSERT(GrIsALIGN4(fCurrVertex)); |
65 SkASSERT(fCurrTexture); | 66 SkASSERT(fCurrTexture); |
66 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNon
e_FilterMode); | 67 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBil
erp_FilterMode); |
67 | 68 |
68 // This effect could be stored with one of the cache objects (atlas?) | 69 // This effect could be stored with one of the cache objects (atlas?) |
69 drawState->addCoverageEffect( | 70 drawState->addCoverageEffect( |
70 GrCustomCoordsTextureEffect::Create(fCurrTexture
, params), | 71 GrDistanceFieldTextureEffect::Create(fCurrTextur
e, params), |
71 kGlyphCoordsAttributeIndex)->unref(); | 72 kGlyphCoordsAttributeIndex)->unref(); |
72 | 73 |
73 if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { | 74 if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { |
74 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || | 75 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
75 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || | 76 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
76 fPaint.numColorStages()) { | 77 fPaint.numColorStages()) { |
77 GrPrintf("LCD Text will not draw correctly.\n"); | 78 GrPrintf("LCD Text will not draw correctly.\n"); |
78 } | 79 } |
79 // We don't use the GrPaint's color in this case because it's been p
remultiplied by | 80 // 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 | 81 // 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 | 82 // the mask texture color. The end result is that we get |
82 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo
lor | 83 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo
lor |
83 int a = SkColorGetA(fSkPaintColor); | 84 int a = SkColorGetA(fSkPaintColor); |
84 // paintAlpha | 85 // paintAlpha |
85 drawState->setColor(SkColorSetARGB(a, a, a, a)); | 86 drawState->setColor(SkColorSetARGB(a, a, a, a)); |
86 // paintColor | 87 // paintColor |
87 drawState->setBlendConstant(skcolor_to_grcolor_nopremultiply(fSkPain
tColor)); | 88 drawState->setBlendConstant(skcolor_to_grcolor_nopremultiply(fSkPain
tColor)); |
88 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); | 89 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
89 } else { | 90 } else { |
90 // set back to normal in case we took LCD path previously. | 91 // set back to normal in case we took LCD path previously. |
91 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen
dCoeff()); | 92 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen
dCoeff()); |
92 drawState->setColor(fPaint.getColor()); | 93 drawState->setColor(fPaint.getColor()); |
93 } | 94 } |
94 | 95 |
95 int nGlyphs = fCurrVertex / 4; | 96 int nGlyphs = fCurrVertex / 4; |
96 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); | 97 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
97 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, | 98 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, |
98 nGlyphs, | 99 nGlyphs, |
99 4, 6); | 100 4, 6); |
100 | |
101 fDrawTarget->resetVertexSource(); | 101 fDrawTarget->resetVertexSource(); |
102 fVertices = NULL; | 102 fVertices = NULL; |
103 fMaxVertices = 0; | 103 fMaxVertices = 0; |
104 fCurrVertex = 0; | 104 fCurrVertex = 0; |
105 SkSafeSetNull(fCurrTexture); | 105 SkSafeSetNull(fCurrTexture); |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 namespace { | 109 namespace { |
110 | 110 |
111 // position + texture coord | 111 // position + texture coord |
112 extern const GrVertexAttrib gTextVertexAttribs[] = { | 112 extern const GrVertexAttrib gTextVertexAttribs[] = { |
113 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding
}, | 113 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding
}, |
114 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} | 114 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
115 }; | 115 }; |
116 | 116 |
117 }; | 117 }; |
118 | 118 |
119 void GrBitmapTextContext::drawPackedGlyph(GrGlyph::PackedID packed, | 119 void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
120 GrFixed vx, GrFixed vy, | 120 GrFixed vx, GrFixed vy, |
121 GrFontScaler* scaler) { | 121 GrFontScaler* scaler) { |
122 if (NULL == fDrawTarget) { | 122 if (NULL == fDrawTarget) { |
123 return; | 123 return; |
124 } | 124 } |
125 if (NULL == fStrike) { | 125 if (NULL == fStrike) { |
126 fStrike = fContext->getFontCache()->getStrike(scaler); | 126 fStrike = fContext->getFontCache()->getStrike(scaler, true); |
127 } | 127 } |
128 | 128 |
129 GrGlyph* glyph = fStrike->getGlyph(packed, scaler); | 129 GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
130 if (NULL == glyph || glyph->fBounds.isEmpty()) { | 130 if (NULL == glyph || glyph->fBounds.isEmpty()) { |
131 return; | 131 return; |
132 } | 132 } |
133 | 133 |
| 134 SkScalar sx = SkFixedToScalar(vx); |
| 135 SkScalar sy = SkFixedToScalar(vy); |
| 136 /* |
| 137 // not valid, need to find a different solution for this |
134 vx += SkIntToFixed(glyph->fBounds.fLeft); | 138 vx += SkIntToFixed(glyph->fBounds.fLeft); |
135 vy += SkIntToFixed(glyph->fBounds.fTop); | 139 vy += SkIntToFixed(glyph->fBounds.fTop); |
136 | 140 |
137 // keep them as ints until we've done the clip-test | 141 // keep them as ints until we've done the clip-test |
138 GrFixed width = glyph->fBounds.width(); | 142 GrFixed width = glyph->fBounds.width(); |
139 GrFixed height = glyph->fBounds.height(); | 143 GrFixed height = glyph->fBounds.height(); |
140 | 144 |
141 // check if we clipped out | 145 // check if we clipped out |
142 if (true || NULL == glyph->fPlot) { | 146 if (true || NULL == glyph->fPlot) { |
143 int x = vx >> 16; | 147 int x = vx >> 16; |
144 int y = vy >> 16; | 148 int y = vy >> 16; |
145 if (fClipRect.quickReject(x, y, x + width, y + height)) { | 149 if (fClipRect.quickReject(x, y, x + width, y + height)) { |
146 // SkCLZ(3); // so we can set a break-point in the debugger | 150 // SkCLZ(3); // so we can set a break-point in the debugger |
147 return; | 151 return; |
148 } | 152 } |
149 } | 153 } |
150 | 154 */ |
151 if (NULL == glyph->fPlot) { | 155 if (NULL == glyph->fPlot) { |
152 if (fStrike->getGlyphAtlas(glyph, scaler)) { | 156 if (fStrike->getGlyphAtlas(glyph, scaler)) { |
153 goto HAS_ATLAS; | 157 goto HAS_ATLAS; |
154 } | 158 } |
155 | 159 |
156 // try to clear out an unused plot before we flush | 160 // try to clear out an unused plot before we flush |
157 fContext->getFontCache()->freePlotExceptFor(fStrike); | 161 fContext->getFontCache()->freePlotExceptFor(fStrike); |
158 if (fStrike->getGlyphAtlas(glyph, scaler)) { | 162 if (fStrike->getGlyphAtlas(glyph, scaler)) { |
159 goto HAS_ATLAS; | 163 goto HAS_ATLAS; |
160 } | 164 } |
(...skipping 20 matching lines...) Expand all Loading... |
181 if (!scaler->getGlyphPath(glyph->glyphID(), path)) { | 185 if (!scaler->getGlyphPath(glyph->glyphID(), path)) { |
182 // flag the glyph as being dead? | 186 // flag the glyph as being dead? |
183 delete path; | 187 delete path; |
184 return; | 188 return; |
185 } | 189 } |
186 glyph->fPath = path; | 190 glyph->fPath = path; |
187 } | 191 } |
188 | 192 |
189 GrContext::AutoMatrix am; | 193 GrContext::AutoMatrix am; |
190 SkMatrix translate; | 194 SkMatrix translate; |
191 translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.
fLeft)), | 195 translate.setTranslate(sx, sy); |
192 SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.
fTop))); | |
193 GrPaint tmpPaint(fPaint); | 196 GrPaint tmpPaint(fPaint); |
194 am.setPreConcat(fContext, translate, &tmpPaint); | 197 am.setPreConcat(fContext, translate, &tmpPaint); |
195 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); | 198 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
196 fContext->drawPath(tmpPaint, *glyph->fPath, stroke); | 199 fContext->drawPath(tmpPaint, *glyph->fPath, stroke); |
197 return; | 200 return; |
198 } | 201 } |
199 | 202 |
200 HAS_ATLAS: | 203 HAS_ATLAS: |
201 SkASSERT(glyph->fPlot); | 204 SkASSERT(glyph->fPlot); |
202 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); | 205 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); |
203 glyph->fPlot->setDrawToken(drawToken); | 206 glyph->fPlot->setDrawToken(drawToken); |
204 | 207 |
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(); | 208 GrTexture* texture = glyph->fPlot->texture(); |
210 SkASSERT(texture); | 209 SkASSERT(texture); |
211 | 210 |
212 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { | 211 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { |
213 this->flushGlyphs(); | 212 this->flushGlyphs(); |
214 fCurrTexture = texture; | 213 fCurrTexture = texture; |
215 fCurrTexture->ref(); | 214 fCurrTexture->ref(); |
216 } | 215 } |
217 | 216 |
218 if (NULL == fVertices) { | 217 if (NULL == fVertices) { |
(...skipping 21 matching lines...) Expand all Loading... |
240 fMaxVertices = maxQuadVertices; | 239 fMaxVertices = maxQuadVertices; |
241 } | 240 } |
242 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices, | 241 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices, |
243 0, | 242 0, |
244 GrTCast<void**>(&
fVertices), | 243 GrTCast<void**>(&
fVertices), |
245 NULL); | 244 NULL); |
246 GrAlwaysAssert(success); | 245 GrAlwaysAssert(success); |
247 SkASSERT(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize(
)); | 246 SkASSERT(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize(
)); |
248 } | 247 } |
249 | 248 |
| 249 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft); |
| 250 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop); |
| 251 SkScalar width = SkIntToScalar(glyph->fBounds.width()); |
| 252 SkScalar height = SkIntToScalar(glyph->fBounds.height()); |
| 253 |
| 254 SkScalar scale = fTextRatio; |
| 255 dx *= scale; |
| 256 dy *= scale; |
| 257 sx += dx; |
| 258 sy += dy; |
| 259 width *= scale; |
| 260 height *= scale; |
| 261 |
250 GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); | 262 GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); |
251 GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); | 263 GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); |
| 264 GrFixed tw = SkIntToFixed(glyph->fBounds.width()); |
| 265 GrFixed th = SkIntToFixed(glyph->fBounds.height()); |
252 | 266 |
253 fVertices[2*fCurrVertex].setRectFan(SkFixedToFloat(vx), | 267 fVertices[2*fCurrVertex].setRectFan(sx, |
254 SkFixedToFloat(vy), | 268 sy, |
255 SkFixedToFloat(vx + width), | 269 sx + width, |
256 SkFixedToFloat(vy + height), | 270 sy + height, |
257 2 * sizeof(SkPoint)); | 271 2 * sizeof(SkPoint)); |
258 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixed
X(tx)), | 272 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixed
X(tx)), |
259 SkFixedToFloat(texture->normalizeFixed
Y(ty)), | 273 SkFixedToFloat(texture->normalizeFixed
Y(ty)), |
260 SkFixedToFloat(texture->normalizeFixed
X(tx + width)), | 274 SkFixedToFloat(texture->normalizeFixed
X(tx + tw)), |
261 SkFixedToFloat(texture->normalizeFixed
Y(ty + height)), | 275 SkFixedToFloat(texture->normalizeFixed
Y(ty + th)), |
262 2 * sizeof(SkPoint)); | 276 2 * sizeof(SkPoint)); |
263 fCurrVertex += 4; | 277 fCurrVertex += 4; |
264 } | 278 } |
OLD | NEW |