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" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 GrBitmapTextContext::GrBitmapTextContext(GrContext* context, | 52 GrBitmapTextContext::GrBitmapTextContext(GrContext* context, |
53 const SkDeviceProperties& properties) | 53 const SkDeviceProperties& properties) |
54 : GrTextContext(context, properties) { | 54 : GrTextContext(context, properties) { |
55 fStrike = NULL; | 55 fStrike = NULL; |
56 | 56 |
57 fCurrTexture = NULL; | 57 fCurrTexture = NULL; |
58 fCurrVertex = 0; | 58 fCurrVertex = 0; |
59 fEffectTextureUniqueID = SK_InvalidUniqueID; | 59 fEffectTextureUniqueID = SK_InvalidUniqueID; |
60 | 60 |
61 fVertices = NULL; | 61 fVertices = NULL; |
| 62 fMaxVertices = 0; |
62 | 63 |
63 fVertexBounds.setLargestInverted(); | 64 fVertexBounds.setLargestInverted(); |
64 } | 65 } |
65 | 66 |
66 GrBitmapTextContext::~GrBitmapTextContext() { | 67 GrBitmapTextContext::~GrBitmapTextContext() { |
67 this->flushGlyphs(); | 68 this->flushGlyphs(); |
68 } | 69 } |
69 | 70 |
70 bool GrBitmapTextContext::canDraw(const SkPaint& paint) { | 71 bool GrBitmapTextContext::canDraw(const SkPaint& paint) { |
71 return !SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix()); | 72 return !SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix()); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 SkFAIL("Unexepected mask format."); | 147 SkFAIL("Unexepected mask format."); |
147 } | 148 } |
148 int nGlyphs = fCurrVertex / 4; | 149 int nGlyphs = fCurrVertex / 4; |
149 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); | 150 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
150 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, | 151 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, |
151 nGlyphs, | 152 nGlyphs, |
152 4, 6, &fVertexBounds); | 153 4, 6, &fVertexBounds); |
153 | 154 |
154 fDrawTarget->resetVertexSource(); | 155 fDrawTarget->resetVertexSource(); |
155 fVertices = NULL; | 156 fVertices = NULL; |
| 157 fMaxVertices = 0; |
156 fCurrVertex = 0; | 158 fCurrVertex = 0; |
157 fVertexBounds.setLargestInverted(); | 159 fVertexBounds.setLargestInverted(); |
158 SkSafeSetNull(fCurrTexture); | 160 SkSafeSetNull(fCurrTexture); |
159 } | 161 } |
160 } | 162 } |
161 | 163 |
162 inline void GrBitmapTextContext::init(const GrPaint& paint, const SkPaint& skPai
nt) { | 164 inline void GrBitmapTextContext::init(const GrPaint& paint, const SkPaint& skPai
nt) { |
163 GrTextContext::init(paint, skPaint); | 165 GrTextContext::init(paint, skPaint); |
164 | 166 |
165 fStrike = NULL; | 167 fStrike = NULL; |
166 | 168 |
167 fCurrTexture = NULL; | 169 fCurrTexture = NULL; |
168 fCurrVertex = 0; | 170 fCurrVertex = 0; |
169 | 171 |
170 fVertices = NULL; | 172 fVertices = NULL; |
| 173 fMaxVertices = 0; |
171 } | 174 } |
172 | 175 |
173 inline void GrBitmapTextContext::finish() { | 176 inline void GrBitmapTextContext::finish() { |
174 this->flushGlyphs(); | 177 this->flushGlyphs(); |
175 | 178 |
176 GrTextContext::finish(); | 179 GrTextContext::finish(); |
177 } | 180 } |
178 | 181 |
179 void GrBitmapTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint, | 182 void GrBitmapTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint, |
180 const char text[], size_t byteLength, | 183 const char text[], size_t byteLength, |
181 SkScalar x, SkScalar y) { | 184 SkScalar x, SkScalar y) { |
182 SkASSERT(byteLength == 0 || text != NULL); | 185 SkASSERT(byteLength == 0 || text != NULL); |
183 | 186 |
184 // nothing to draw | 187 // nothing to draw |
185 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { | 188 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { |
186 return; | 189 return; |
187 } | 190 } |
188 | 191 |
189 this->init(paint, skPaint); | 192 this->init(paint, skPaint); |
190 | 193 |
191 if (NULL == fDrawTarget) { | |
192 return; | |
193 } | |
194 | |
195 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); | 194 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
196 | 195 |
197 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMa
trix()); | 196 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMa
trix()); |
198 SkGlyphCache* cache = autoCache.getCache(); | 197 SkGlyphCache* cache = autoCache.getCache(); |
199 GrFontScaler* fontScaler = GetGrFontScaler(cache); | 198 GrFontScaler* fontScaler = GetGrFontScaler(cache); |
200 if (NULL == fStrike) { | |
201 fStrike = fContext->getFontCache()->getStrike(fontScaler, false); | |
202 } | |
203 | 199 |
204 // transform our starting point | 200 // transform our starting point |
205 { | 201 { |
206 SkPoint loc; | 202 SkPoint loc; |
207 fContext->getMatrix().mapXY(x, y, &loc); | 203 fContext->getMatrix().mapXY(x, y, &loc); |
208 x = loc.fX; | 204 x = loc.fX; |
209 y = loc.fY; | 205 y = loc.fY; |
210 } | 206 } |
211 | 207 |
212 // need to measure first | 208 // need to measure first |
213 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { | 209 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
214 SkVector stop; | 210 SkVector stop; |
215 | 211 |
216 MeasureText(cache, glyphCacheProc, text, byteLength, &stop); | 212 MeasureText(cache, glyphCacheProc, text, byteLength, &stop); |
217 | 213 |
218 SkScalar stopX = stop.fX; | 214 SkScalar stopX = stop.fX; |
219 SkScalar stopY = stop.fY; | 215 SkScalar stopY = stop.fY; |
220 | 216 |
221 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { | 217 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { |
222 stopX = SkScalarHalf(stopX); | 218 stopX = SkScalarHalf(stopX); |
223 stopY = SkScalarHalf(stopY); | 219 stopY = SkScalarHalf(stopY); |
224 } | 220 } |
225 x -= stopX; | 221 x -= stopX; |
226 y -= stopY; | 222 y -= stopY; |
227 } | 223 } |
228 | 224 |
229 const char* stop = text + byteLength; | 225 const char* stop = text + byteLength; |
230 | 226 |
231 // allocate vertices | |
232 SkASSERT(NULL == fVertices); | |
233 bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat(); | |
234 if (useColorVerts) { | |
235 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>( | |
236 SK_ARRAY_COUNT(gTextVertexWi
thColorAttribs)); | |
237 } else { | |
238 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( | |
239 SK_ARRAY_COUNT(gTextVertexAt
tribs)); | |
240 } | |
241 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL); | |
242 bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs, | |
243 0, | |
244 &fVertices, | |
245 NULL); | |
246 GrAlwaysAssert(success); | |
247 | |
248 SkAutoKern autokern; | 227 SkAutoKern autokern; |
249 | 228 |
250 SkFixed fxMask = ~0; | 229 SkFixed fxMask = ~0; |
251 SkFixed fyMask = ~0; | 230 SkFixed fyMask = ~0; |
252 SkFixed halfSampleX, halfSampleY; | 231 SkFixed halfSampleX, halfSampleY; |
253 if (cache->isSubpixel()) { | 232 if (cache->isSubpixel()) { |
254 halfSampleX = halfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits); | 233 halfSampleX = halfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits); |
255 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(fContext->getM
atrix()); | 234 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(fContext->getM
atrix()); |
256 if (kX_SkAxisAlignment == baseline) { | 235 if (kX_SkAxisAlignment == baseline) { |
257 fyMask = 0; | 236 fyMask = 0; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 SkASSERT(byteLength == 0 || text != NULL); | 277 SkASSERT(byteLength == 0 || text != NULL); |
299 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); | 278 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
300 | 279 |
301 // nothing to draw | 280 // nothing to draw |
302 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { | 281 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { |
303 return; | 282 return; |
304 } | 283 } |
305 | 284 |
306 this->init(paint, skPaint); | 285 this->init(paint, skPaint); |
307 | 286 |
308 if (NULL == fDrawTarget) { | |
309 return; | |
310 } | |
311 | |
312 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); | 287 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
313 | 288 |
314 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMa
trix()); | 289 SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMa
trix()); |
315 SkGlyphCache* cache = autoCache.getCache(); | 290 SkGlyphCache* cache = autoCache.getCache(); |
316 GrFontScaler* fontScaler = GetGrFontScaler(cache); | 291 GrFontScaler* fontScaler = GetGrFontScaler(cache); |
317 | |
318 if (NULL == fStrike) { | |
319 fStrike = fContext->getFontCache()->getStrike(fontScaler, false); | |
320 } | |
321 | 292 |
322 // store original matrix before we reset, so we can use it to transform posi
tions | 293 // store original matrix before we reset, so we can use it to transform posi
tions |
323 SkMatrix ctm = fContext->getMatrix(); | 294 SkMatrix ctm = fContext->getMatrix(); |
324 GrContext::AutoMatrix autoMatrix; | 295 GrContext::AutoMatrix autoMatrix; |
325 autoMatrix.setIdentity(fContext, &fPaint); | 296 autoMatrix.setIdentity(fContext, &fPaint); |
326 | 297 |
327 // allocate vertices | |
328 SkASSERT(NULL == fVertices); | |
329 bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat(); | |
330 if (useColorVerts) { | |
331 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>( | |
332 SK_ARRAY_COUNT(gTextVertexWi
thColorAttribs)); | |
333 } else { | |
334 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( | |
335 SK_ARRAY_COUNT(gTextVertexAt
tribs)); | |
336 } | |
337 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL); | |
338 bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs, | |
339 0, | |
340 &fVertices, | |
341 NULL); | |
342 GrAlwaysAssert(success); | |
343 | |
344 const char* stop = text + byteLength; | 298 const char* stop = text + byteLength; |
345 SkTextAlignProc alignProc(fSkPaint.getTextAlign()); | 299 SkTextAlignProc alignProc(fSkPaint.getTextAlign()); |
346 SkTextMapStateProc tmsProc(ctm, constY, scalarsPerPosition); | 300 SkTextMapStateProc tmsProc(ctm, constY, scalarsPerPosition); |
347 SkFixed halfSampleX = 0, halfSampleY = 0; | 301 SkFixed halfSampleX = 0, halfSampleY = 0; |
348 | 302 |
349 if (cache->isSubpixel()) { | 303 if (cache->isSubpixel()) { |
350 // maybe we should skip the rounding if linearText is set | 304 // maybe we should skip the rounding if linearText is set |
351 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(ctm); | 305 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(ctm); |
352 | 306 |
353 SkFixed fxMask = ~0; | 307 SkFixed fxMask = ~0; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 } | 419 } |
466 } | 420 } |
467 } | 421 } |
468 | 422 |
469 this->finish(); | 423 this->finish(); |
470 } | 424 } |
471 | 425 |
472 void GrBitmapTextContext::drawPackedGlyph(GrGlyph::PackedID packed, | 426 void GrBitmapTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
473 SkFixed vx, SkFixed vy, | 427 SkFixed vx, SkFixed vy, |
474 GrFontScaler* scaler) { | 428 GrFontScaler* scaler) { |
| 429 if (NULL == fDrawTarget) { |
| 430 return; |
| 431 } |
| 432 |
| 433 if (NULL == fStrike) { |
| 434 fStrike = fContext->getFontCache()->getStrike(scaler, false); |
| 435 } |
| 436 |
475 GrGlyph* glyph = fStrike->getGlyph(packed, scaler); | 437 GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
476 if (NULL == glyph || glyph->fBounds.isEmpty()) { | 438 if (NULL == glyph || glyph->fBounds.isEmpty()) { |
477 return; | 439 return; |
478 } | 440 } |
479 | 441 |
480 vx += SkIntToFixed(glyph->fBounds.fLeft); | 442 vx += SkIntToFixed(glyph->fBounds.fLeft); |
481 vy += SkIntToFixed(glyph->fBounds.fTop); | 443 vy += SkIntToFixed(glyph->fBounds.fTop); |
482 | 444 |
483 // keep them as ints until we've done the clip-test | 445 // keep them as ints until we've done the clip-test |
484 SkFixed width = glyph->fBounds.width(); | 446 SkFixed width = glyph->fBounds.width(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); | 509 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); |
548 glyph->fPlot->setDrawToken(drawToken); | 510 glyph->fPlot->setDrawToken(drawToken); |
549 | 511 |
550 // now promote them to fixed (TODO: Rethink using fixed pt). | 512 // now promote them to fixed (TODO: Rethink using fixed pt). |
551 width = SkIntToFixed(width); | 513 width = SkIntToFixed(width); |
552 height = SkIntToFixed(height); | 514 height = SkIntToFixed(height); |
553 | 515 |
554 GrTexture* texture = glyph->fPlot->texture(); | 516 GrTexture* texture = glyph->fPlot->texture(); |
555 SkASSERT(texture); | 517 SkASSERT(texture); |
556 | 518 |
557 if (fCurrTexture != texture) { | 519 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { |
558 this->flushGlyphs(); | 520 this->flushGlyphs(); |
559 fCurrTexture = texture; | 521 fCurrTexture = texture; |
560 fCurrTexture->ref(); | 522 fCurrTexture->ref(); |
561 } | 523 } |
562 | 524 |
| 525 bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat(); |
| 526 |
| 527 if (NULL == fVertices) { |
| 528 // If we need to reserve vertices allow the draw target to suggest |
| 529 // a number of verts to reserve and whether to perform a flush. |
| 530 fMaxVertices = kMinRequestedVerts; |
| 531 if (useColorVerts) { |
| 532 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttri
bs>( |
| 533 SK_ARRAY_COUNT(gTextVertexWithColorAttribs)); |
| 534 } else { |
| 535 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( |
| 536 SK_ARRAY_COUNT(gTextVertexAttribs)); |
| 537 } |
| 538 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL); |
| 539 if (flush) { |
| 540 this->flushGlyphs(); |
| 541 fContext->flush(); |
| 542 if (useColorVerts) { |
| 543 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorA
ttribs>( |
| 544 SK_ARRAY_COUNT(gTextVertexWithColorAttribs)); |
| 545 } else { |
| 546 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( |
| 547 SK_ARRAY_COUNT(gTextVertexAttribs)); |
| 548 } |
| 549 } |
| 550 fMaxVertices = kDefaultRequestedVerts; |
| 551 // ignore return, no point in flushing again. |
| 552 fDrawTarget->geometryHints(&fMaxVertices, NULL); |
| 553 |
| 554 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads(); |
| 555 if (fMaxVertices < kMinRequestedVerts) { |
| 556 fMaxVertices = kDefaultRequestedVerts; |
| 557 } else if (fMaxVertices > maxQuadVertices) { |
| 558 // don't exceed the limit of the index buffer |
| 559 fMaxVertices = maxQuadVertices; |
| 560 } |
| 561 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices, |
| 562 0, |
| 563 &fVertices, |
| 564 NULL); |
| 565 GrAlwaysAssert(success); |
| 566 } |
| 567 |
563 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); | 568 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); |
564 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); | 569 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); |
565 | 570 |
566 SkRect r; | 571 SkRect r; |
567 r.fLeft = SkFixedToFloat(vx); | 572 r.fLeft = SkFixedToFloat(vx); |
568 r.fTop = SkFixedToFloat(vy); | 573 r.fTop = SkFixedToFloat(vy); |
569 r.fRight = SkFixedToFloat(vx + width); | 574 r.fRight = SkFixedToFloat(vx + width); |
570 r.fBottom = SkFixedToFloat(vy + height); | 575 r.fBottom = SkFixedToFloat(vy + height); |
571 | 576 |
572 fVertexBounds.growToInclude(r); | 577 fVertexBounds.growToInclude(r); |
573 | 578 |
574 bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat(); | |
575 size_t vertSize = useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) : | 579 size_t vertSize = useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) : |
576 (2 * sizeof(SkPoint)); | 580 (2 * sizeof(SkPoint)); |
577 | 581 |
578 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexSize()); | 582 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexSize()); |
579 | 583 |
580 SkPoint* positions = reinterpret_cast<SkPoint*>( | 584 SkPoint* positions = reinterpret_cast<SkPoint*>( |
581 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); | 585 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); |
582 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); | 586 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); |
583 | 587 |
584 // The texture coords are last in both the with and without color vertex lay
outs. | 588 // The texture coords are last in both the with and without color vertex lay
outs. |
585 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( | 589 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( |
586 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint))
; | 590 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint))
; |
587 textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)), | 591 textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)), |
588 SkFixedToFloat(texture->normalizeFixedY(ty)), | 592 SkFixedToFloat(texture->normalizeFixedY(ty)), |
589 SkFixedToFloat(texture->normalizeFixedX(tx + width
)), | 593 SkFixedToFloat(texture->normalizeFixedX(tx + width
)), |
590 SkFixedToFloat(texture->normalizeFixedY(ty + heigh
t)), | 594 SkFixedToFloat(texture->normalizeFixedY(ty + heigh
t)), |
591 vertSize); | 595 vertSize); |
592 if (useColorVerts) { | 596 if (useColorVerts) { |
593 // color comes after position. | 597 // color comes after position. |
594 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); | 598 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); |
595 for (int i = 0; i < 4; ++i) { | 599 for (int i = 0; i < 4; ++i) { |
596 *colors = fPaint.getColor(); | 600 *colors = fPaint.getColor(); |
597 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors
) + vertSize); | 601 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors
) + vertSize); |
598 } | 602 } |
599 } | 603 } |
600 fCurrVertex += 4; | 604 fCurrVertex += 4; |
601 } | 605 } |
OLD | NEW |