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 "GrBitmapTextContext.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" | 15 #include "SkColorPriv.h" |
16 #include "SkPath.h" | 16 #include "SkPath.h" |
17 #include "SkRTConf.h" | 17 #include "SkRTConf.h" |
18 #include "SkStrokeRec.h" | 18 #include "SkStrokeRec.h" |
19 #include "effects/GrCustomCoordsTextureEffect.h" | 19 #include "effects/GrCustomCoordsTextureEffect.h" |
20 | 20 |
| 21 #include "SkAutoKern.h" |
| 22 #include "SkGlyphCache.h" |
| 23 #include "SkGpuDevice.h" |
| 24 #include "SkGr.h" |
| 25 |
21 static const int kGlyphCoordsAttributeIndex = 1; | 26 static const int kGlyphCoordsAttributeIndex = 1; |
22 | 27 |
23 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, | 28 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, |
24 "Dump the contents of the font cache before every purge."); | 29 "Dump the contents of the font cache before every purge."); |
25 | 30 |
26 GrBitmapTextContext::GrBitmapTextContext(GrContext* context, const GrPaint& pain
t, | 31 GrBitmapTextContext::GrBitmapTextContext(GrContext* context, |
27 const SkPaint& skPaint) : | 32 const GrPaint& grPaint, |
28 GrTextContext(context, paint, skPaint)
{ | 33 const SkPaint& skPaint, |
29 fAutoMatrix.setIdentity(fContext, &fPaint); | 34 const SkDeviceProperties& properties) |
30 | 35 : GrTextContext(context, grPaint, skPaint
, properties) { |
31 fStrike = NULL; | 36 fStrike = NULL; |
32 | 37 |
33 fCurrTexture = NULL; | 38 fCurrTexture = NULL; |
34 fCurrVertex = 0; | 39 fCurrVertex = 0; |
35 | 40 |
36 fVertices = NULL; | 41 fVertices = NULL; |
37 fMaxVertices = 0; | 42 fMaxVertices = 0; |
38 } | 43 } |
39 | 44 |
40 GrBitmapTextContext::~GrBitmapTextContext() { | 45 GrBitmapTextContext::~GrBitmapTextContext() { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 4, 6); | 105 4, 6); |
101 | 106 |
102 fDrawTarget->resetVertexSource(); | 107 fDrawTarget->resetVertexSource(); |
103 fVertices = NULL; | 108 fVertices = NULL; |
104 fMaxVertices = 0; | 109 fMaxVertices = 0; |
105 fCurrVertex = 0; | 110 fCurrVertex = 0; |
106 SkSafeSetNull(fCurrTexture); | 111 SkSafeSetNull(fCurrTexture); |
107 } | 112 } |
108 } | 113 } |
109 | 114 |
| 115 void GrBitmapTextContext::drawText(const char text[], size_t byteLength, |
| 116 SkScalar x, SkScalar y) { |
| 117 SkASSERT(byteLength == 0 || text != NULL); |
| 118 |
| 119 // nothing to draw |
| 120 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { |
| 121 return; |
| 122 } |
| 123 |
| 124 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 125 |
| 126 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMa
trix()); |
| 127 SkGlyphCache* cache = autoCache.getCache(); |
| 128 GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 129 |
| 130 // transform our starting point |
| 131 { |
| 132 SkPoint loc; |
| 133 fContext->getMatrix().mapXY(x, y, &loc); |
| 134 x = loc.fX; |
| 135 y = loc.fY; |
| 136 } |
| 137 |
| 138 // need to measure first |
| 139 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
| 140 SkVector stop; |
| 141 |
| 142 MeasureText(cache, glyphCacheProc, text, byteLength, &stop); |
| 143 |
| 144 SkScalar stopX = stop.fX; |
| 145 SkScalar stopY = stop.fY; |
| 146 |
| 147 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 148 stopX = SkScalarHalf(stopX); |
| 149 stopY = SkScalarHalf(stopY); |
| 150 } |
| 151 x -= stopX; |
| 152 y -= stopY; |
| 153 } |
| 154 |
| 155 const char* stop = text + byteLength; |
| 156 |
| 157 SkAutoKern autokern; |
| 158 |
| 159 SkFixed fxMask = ~0; |
| 160 SkFixed fyMask = ~0; |
| 161 SkFixed halfSampleX, halfSampleY; |
| 162 if (cache->isSubpixel()) { |
| 163 halfSampleX = halfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits); |
| 164 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(fContext->getM
atrix()); |
| 165 if (kX_SkAxisAlignment == baseline) { |
| 166 fyMask = 0; |
| 167 halfSampleY = SK_FixedHalf; |
| 168 } else if (kY_SkAxisAlignment == baseline) { |
| 169 fxMask = 0; |
| 170 halfSampleX = SK_FixedHalf; |
| 171 } |
| 172 } else { |
| 173 halfSampleX = halfSampleY = SK_FixedHalf; |
| 174 } |
| 175 |
| 176 SkFixed fx = SkScalarToFixed(x) + halfSampleX; |
| 177 SkFixed fy = SkScalarToFixed(y) + halfSampleY; |
| 178 |
| 179 GrContext::AutoMatrix autoMatrix; |
| 180 autoMatrix.setIdentity(fContext, &fPaint); |
| 181 |
| 182 while (text < stop) { |
| 183 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fy
Mask); |
| 184 |
| 185 fx += autokern.adjust(glyph); |
| 186 |
| 187 if (glyph.fWidth) { |
| 188 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 189 glyph.getSubXFixed(), |
| 190 glyph.getSubYFixed()), |
| 191 SkFixedFloorToFixed(fx), |
| 192 SkFixedFloorToFixed(fy), |
| 193 fontScaler); |
| 194 } |
| 195 |
| 196 fx += glyph.fAdvanceX; |
| 197 fy += glyph.fAdvanceY; |
| 198 } |
| 199 } |
| 200 |
| 201 /////////////////////////////////////////////////////////////////////////////// |
| 202 // Copied from SkDraw |
| 203 |
| 204 // last parameter is interpreted as SkFixed [x, y] |
| 205 // return the fixed position, which may be rounded or not by the caller |
| 206 // e.g. subpixel doesn't round |
| 207 typedef void (*AlignProc)(const SkPoint&, const SkGlyph&, SkIPoint*); |
| 208 |
| 209 static void leftAlignProc(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* ds
t) { |
| 210 dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY)); |
| 211 } |
| 212 |
| 213 static void centerAlignProc(const SkPoint& loc, const SkGlyph& glyph, SkIPoint*
dst) { |
| 214 dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1), |
| 215 SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1)); |
| 216 } |
| 217 |
| 218 static void rightAlignProc(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* d
st) { |
| 219 dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX, |
| 220 SkScalarToFixed(loc.fY) - glyph.fAdvanceY); |
| 221 } |
| 222 |
| 223 static AlignProc pick_align_proc(SkPaint::Align align) { |
| 224 static const AlignProc gProcs[] = { |
| 225 leftAlignProc, centerAlignProc, rightAlignProc |
| 226 }; |
| 227 |
| 228 SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs)); |
| 229 |
| 230 return gProcs[align]; |
| 231 } |
| 232 |
| 233 class BitmapTextMapState { |
| 234 public: |
| 235 mutable SkPoint fLoc; |
| 236 |
| 237 BitmapTextMapState(const SkMatrix& matrix, SkScalar y) |
| 238 : fMatrix(matrix), fProc(matrix.getMapXYProc()), fY(y) {} |
| 239 |
| 240 typedef void (*Proc)(const BitmapTextMapState&, const SkScalar pos[]); |
| 241 |
| 242 Proc pickProc(int scalarsPerPosition); |
| 243 |
| 244 private: |
| 245 const SkMatrix& fMatrix; |
| 246 SkMatrix::MapXYProc fProc; |
| 247 SkScalar fY; // ignored by MapXYProc |
| 248 // these are only used by Only... procs |
| 249 SkScalar fScaleX, fTransX, fTransformedY; |
| 250 |
| 251 static void MapXProc(const BitmapTextMapState& state, const SkScalar pos[])
{ |
| 252 state.fProc(state.fMatrix, *pos, state.fY, &state.fLoc); |
| 253 } |
| 254 |
| 255 static void MapXYProc(const BitmapTextMapState& state, const SkScalar pos[])
{ |
| 256 state.fProc(state.fMatrix, pos[0], pos[1], &state.fLoc); |
| 257 } |
| 258 |
| 259 static void MapOnlyScaleXProc(const BitmapTextMapState& state, |
| 260 const SkScalar pos[]) { |
| 261 state.fLoc.set(SkScalarMul(state.fScaleX, *pos) + state.fTransX, |
| 262 state.fTransformedY); |
| 263 } |
| 264 |
| 265 static void MapOnlyTransXProc(const BitmapTextMapState& state, |
| 266 const SkScalar pos[]) { |
| 267 state.fLoc.set(*pos + state.fTransX, state.fTransformedY); |
| 268 } |
| 269 }; |
| 270 |
| 271 BitmapTextMapState::Proc BitmapTextMapState::pickProc(int scalarsPerPosition) { |
| 272 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 273 |
| 274 if (1 == scalarsPerPosition) { |
| 275 unsigned mtype = fMatrix.getType(); |
| 276 if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) { |
| 277 return MapXProc; |
| 278 } else { |
| 279 fScaleX = fMatrix.getScaleX(); |
| 280 fTransX = fMatrix.getTranslateX(); |
| 281 fTransformedY = SkScalarMul(fY, fMatrix.getScaleY()) + |
| 282 fMatrix.getTranslateY(); |
| 283 return (mtype & SkMatrix::kScale_Mask) ? |
| 284 MapOnlyScaleXProc : MapOnlyTransXProc; |
| 285 } |
| 286 } else { |
| 287 return MapXYProc; |
| 288 } |
| 289 } |
| 290 |
| 291 /////////////////////////////////////////////////////////////////////////////// |
| 292 |
| 293 void GrBitmapTextContext::drawPosText(const char text[], size_t byteLength, |
| 294 const SkScalar pos[], SkScalar constY, |
| 295 int scalarsPerPosition) { |
| 296 SkASSERT(byteLength == 0 || text != NULL); |
| 297 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 298 |
| 299 // nothing to draw |
| 300 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { |
| 301 return; |
| 302 } |
| 303 |
| 304 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 305 |
| 306 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMa
trix()); |
| 307 SkGlyphCache* cache = autoCache.getCache(); |
| 308 GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 309 |
| 310 // store original matrix before we reset, so we can use it to transform posi
tions |
| 311 SkMatrix ctm = fContext->getMatrix(); |
| 312 GrContext::AutoMatrix autoMatrix; |
| 313 autoMatrix.setIdentity(fContext, &fPaint); |
| 314 |
| 315 const char* stop = text + byteLength; |
| 316 AlignProc alignProc = pick_align_proc(fSkPaint.getTextAlign()); |
| 317 BitmapTextMapState tms(ctm, constY); |
| 318 BitmapTextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition); |
| 319 SkFixed halfSampleX = 0, halfSampleY = 0; |
| 320 |
| 321 if (cache->isSubpixel()) { |
| 322 // maybe we should skip the rounding if linearText is set |
| 323 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(ctm); |
| 324 |
| 325 SkFixed fxMask = ~0; |
| 326 SkFixed fyMask = ~0; |
| 327 if (kX_SkAxisAlignment == baseline) { |
| 328 fyMask = 0; |
| 329 #ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX |
| 330 halfSampleY = SK_FixedHalf; |
| 331 #endif |
| 332 } else if (kY_SkAxisAlignment == baseline) { |
| 333 fxMask = 0; |
| 334 #ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX |
| 335 halfSampleX = SK_FixedHalf; |
| 336 #endif |
| 337 } |
| 338 |
| 339 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
| 340 while (text < stop) { |
| 341 tmsProc(tms, pos); |
| 342 SkFixed fx = SkScalarToFixed(tms.fLoc.fX) + halfSampleX; |
| 343 SkFixed fy = SkScalarToFixed(tms.fLoc.fY) + halfSampleY; |
| 344 |
| 345 const SkGlyph& glyph = glyphCacheProc(cache, &text, |
| 346 fx & fxMask, fy & fyMask); |
| 347 |
| 348 if (glyph.fWidth) { |
| 349 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 350 glyph.getSubXFixed(), |
| 351 glyph.getSubYFixed()), |
| 352 SkFixedFloorToFixed(fx), |
| 353 SkFixedFloorToFixed(fy), |
| 354 fontScaler); |
| 355 } |
| 356 pos += scalarsPerPosition; |
| 357 } |
| 358 } else { |
| 359 while (text < stop) { |
| 360 const char* currentText = text; |
| 361 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0); |
| 362 |
| 363 if (metricGlyph.fWidth) { |
| 364 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;) |
| 365 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;) |
| 366 |
| 367 tmsProc(tms, pos); |
| 368 SkIPoint fixedLoc; |
| 369 alignProc(tms.fLoc, metricGlyph, &fixedLoc); |
| 370 |
| 371 SkFixed fx = fixedLoc.fX + halfSampleX; |
| 372 SkFixed fy = fixedLoc.fY + halfSampleY; |
| 373 |
| 374 // have to call again, now that we've been "aligned" |
| 375 const SkGlyph& glyph = glyphCacheProc(cache, ¤tText, |
| 376 fx & fxMask, fy & fyMa
sk); |
| 377 // the assumption is that the metrics haven't changed |
| 378 SkASSERT(prevAdvX == glyph.fAdvanceX); |
| 379 SkASSERT(prevAdvY == glyph.fAdvanceY); |
| 380 SkASSERT(glyph.fWidth); |
| 381 |
| 382 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 383 glyph.getSubXFixed(), |
| 384 glyph.getSubYFixed()), |
| 385 SkFixedFloorToFixed(fx), |
| 386 SkFixedFloorToFixed(fy), |
| 387 fontScaler); |
| 388 } |
| 389 pos += scalarsPerPosition; |
| 390 } |
| 391 } |
| 392 } else { // not subpixel |
| 393 |
| 394 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
| 395 while (text < stop) { |
| 396 // the last 2 parameters are ignored |
| 397 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 398 |
| 399 if (glyph.fWidth) { |
| 400 tmsProc(tms, pos); |
| 401 |
| 402 SkFixed fx = SkScalarToFixed(tms.fLoc.fX) + SK_FixedHalf; //
halfSampleX; |
| 403 SkFixed fy = SkScalarToFixed(tms.fLoc.fY) + SK_FixedHalf; //
halfSampleY; |
| 404 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 405 glyph.getSubXFixed(), |
| 406 glyph.getSubYFixed()), |
| 407 SkFixedFloorToFixed(fx), |
| 408 SkFixedFloorToFixed(fy), |
| 409 fontScaler); |
| 410 } |
| 411 pos += scalarsPerPosition; |
| 412 } |
| 413 } else { |
| 414 while (text < stop) { |
| 415 // the last 2 parameters are ignored |
| 416 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 417 |
| 418 if (glyph.fWidth) { |
| 419 tmsProc(tms, pos); |
| 420 |
| 421 SkIPoint fixedLoc; |
| 422 alignProc(tms.fLoc, glyph, &fixedLoc); |
| 423 |
| 424 SkFixed fx = fixedLoc.fX + SK_FixedHalf; //halfSampleX; |
| 425 SkFixed fy = fixedLoc.fY + SK_FixedHalf; //halfSampleY; |
| 426 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 427 glyph.getSubXFixed(), |
| 428 glyph.getSubYFixed()), |
| 429 SkFixedFloorToFixed(fx), |
| 430 SkFixedFloorToFixed(fy), |
| 431 fontScaler); |
| 432 } |
| 433 pos += scalarsPerPosition; |
| 434 } |
| 435 } |
| 436 } |
| 437 } |
| 438 |
110 namespace { | 439 namespace { |
111 | 440 |
112 // position + texture coord | 441 // position + texture coord |
113 extern const GrVertexAttrib gTextVertexAttribs[] = { | 442 extern const GrVertexAttrib gTextVertexAttribs[] = { |
114 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding
}, | 443 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding
}, |
115 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} | 444 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
116 }; | 445 }; |
117 | 446 |
118 }; | 447 }; |
119 | 448 |
120 void GrBitmapTextContext::drawPackedGlyph(GrGlyph::PackedID packed, | 449 void GrBitmapTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
121 GrFixed vx, GrFixed vy, | 450 GrFixed vx, GrFixed vy, |
122 GrFontScaler* scaler) { | 451 GrFontScaler* scaler) { |
123 if (NULL == fDrawTarget) { | 452 if (NULL == fDrawTarget) { |
124 return; | 453 return; |
125 } | 454 } |
| 455 |
126 if (NULL == fStrike) { | 456 if (NULL == fStrike) { |
127 #if SK_DISTANCEFIELD_FONTS | 457 #if SK_DISTANCEFIELD_FONTS |
128 fStrike = fContext->getFontCache()->getStrike(scaler, false); | 458 fStrike = fContext->getFontCache()->getStrike(scaler, false); |
129 #else | 459 #else |
130 fStrike = fContext->getFontCache()->getStrike(scaler); | 460 fStrike = fContext->getFontCache()->getStrike(scaler); |
131 #endif | 461 #endif |
132 } | 462 } |
133 | 463 |
134 GrGlyph* glyph = fStrike->getGlyph(packed, scaler); | 464 GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
135 if (NULL == glyph || glyph->fBounds.isEmpty()) { | 465 if (NULL == glyph || glyph->fBounds.isEmpty()) { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 SkFixedToFloat(vx + width), | 590 SkFixedToFloat(vx + width), |
261 SkFixedToFloat(vy + height), | 591 SkFixedToFloat(vy + height), |
262 2 * sizeof(SkPoint)); | 592 2 * sizeof(SkPoint)); |
263 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixed
X(tx)), | 593 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixed
X(tx)), |
264 SkFixedToFloat(texture->normalizeFixed
Y(ty)), | 594 SkFixedToFloat(texture->normalizeFixed
Y(ty)), |
265 SkFixedToFloat(texture->normalizeFixed
X(tx + width)), | 595 SkFixedToFloat(texture->normalizeFixed
X(tx + width)), |
266 SkFixedToFloat(texture->normalizeFixed
Y(ty + height)), | 596 SkFixedToFloat(texture->normalizeFixed
Y(ty + height)), |
267 2 * sizeof(SkPoint)); | 597 2 * sizeof(SkPoint)); |
268 fCurrVertex += 4; | 598 fCurrVertex += 4; |
269 } | 599 } |
OLD | NEW |