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 "GrDefaultGeoProcFactory.h" | 10 #include "GrDefaultGeoProcFactory.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 fTotalVertexCount = 0; | 73 fTotalVertexCount = 0; |
74 | 74 |
75 fVertexBounds.setLargestInverted(); | 75 fVertexBounds.setLargestInverted(); |
76 } | 76 } |
77 | 77 |
78 GrBitmapTextContext* GrBitmapTextContext::Create(GrContext* context, | 78 GrBitmapTextContext* GrBitmapTextContext::Create(GrContext* context, |
79 const SkDeviceProperties& props
) { | 79 const SkDeviceProperties& props
) { |
80 return SkNEW_ARGS(GrBitmapTextContext, (context, props)); | 80 return SkNEW_ARGS(GrBitmapTextContext, (context, props)); |
81 } | 81 } |
82 | 82 |
83 GrBitmapTextContext::~GrBitmapTextContext() { | |
84 this->finish(); | |
85 } | |
86 | |
87 bool GrBitmapTextContext::canDraw(const SkPaint& paint) { | 83 bool GrBitmapTextContext::canDraw(const SkPaint& paint) { |
88 return !SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix()); | 84 return !SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix()); |
89 } | 85 } |
90 | 86 |
91 inline void GrBitmapTextContext::init(const GrPaint& paint, const SkPaint& skPai
nt) { | 87 inline void GrBitmapTextContext::init(const GrPaint& paint, const SkPaint& skPai
nt) { |
92 GrTextContext::init(paint, skPaint); | 88 GrTextContext::init(paint, skPaint); |
93 | 89 |
94 fStrike = NULL; | 90 fStrike = NULL; |
95 | 91 |
96 fCurrTexture = NULL; | 92 fCurrTexture = NULL; |
97 fCurrVertex = 0; | 93 fCurrVertex = 0; |
98 | 94 |
99 fVertices = NULL; | 95 fVertices = NULL; |
100 fAllocVertexCount = 0; | 96 fAllocVertexCount = 0; |
101 fTotalVertexCount = 0; | 97 fTotalVertexCount = 0; |
102 } | 98 } |
103 | 99 |
104 void GrBitmapTextContext::onDrawText(const GrPaint& paint, const SkPaint& skPain
t, | 100 void GrBitmapTextContext::onDrawText(const GrPaint& paint, const SkPaint& skPain
t, |
105 const char text[], size_t byteLength, | 101 const char text[], size_t byteLength, |
106 SkScalar x, SkScalar y) { | 102 SkScalar x, SkScalar y) { |
107 SkASSERT(byteLength == 0 || text != NULL); | 103 SkASSERT(byteLength == 0 || text != NULL); |
108 | 104 |
109 // nothing to draw | 105 // nothing to draw |
110 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { | 106 if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { |
111 return; | 107 return; |
112 } | 108 } |
113 | 109 |
114 this->init(paint, skPaint); | 110 this->init(paint, skPaint); |
115 | 111 |
116 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); | 112 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } | 185 } |
190 | 186 |
191 fx += glyph.fAdvanceX; | 187 fx += glyph.fAdvanceX; |
192 fy += glyph.fAdvanceY; | 188 fy += glyph.fAdvanceY; |
193 } | 189 } |
194 | 190 |
195 this->finish(); | 191 this->finish(); |
196 } | 192 } |
197 | 193 |
198 void GrBitmapTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skP
aint, | 194 void GrBitmapTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skP
aint, |
199 const char text[], size_t byteLength, | 195 const char text[], size_t byteLength, |
200 const SkScalar pos[], int scalarsPerPositi
on, | 196 const SkScalar pos[], int scalarsPerPosi
tion, |
201 const SkPoint& offset) { | 197 const SkPoint& offset) { |
202 SkASSERT(byteLength == 0 || text != NULL); | 198 SkASSERT(byteLength == 0 || text != NULL); |
203 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); | 199 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
204 | 200 |
205 // nothing to draw | 201 // nothing to draw |
206 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { | 202 if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { |
207 return; | 203 return; |
208 } | 204 } |
209 | 205 |
210 this->init(paint, skPaint); | 206 this->init(paint, skPaint); |
211 | 207 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 fontScaler); | 336 fontScaler); |
341 } | 337 } |
342 pos += scalarsPerPosition; | 338 pos += scalarsPerPosition; |
343 } | 339 } |
344 } | 340 } |
345 } | 341 } |
346 | 342 |
347 this->finish(); | 343 this->finish(); |
348 } | 344 } |
349 | 345 |
350 static void* alloc_vertices(GrDrawTarget* drawTarget, int numVertices, GrMaskFor
mat maskFormat) { | 346 static size_t get_vertex_stride(GrMaskFormat maskFormat) { |
| 347 switch (maskFormat) { |
| 348 case kA8_GrMaskFormat: |
| 349 return kGrayTextVASize; |
| 350 case kARGB_GrMaskFormat: |
| 351 return kColorTextVASize; |
| 352 default: |
| 353 return kLCDTextVASize; |
| 354 } |
| 355 } |
| 356 |
| 357 static void set_vertex_attributes(GrDrawState* drawState, GrMaskFormat maskForma
t) { |
| 358 if (kA8_GrMaskFormat == maskFormat) { |
| 359 drawState->setVertexAttribs<gGrayVertexAttribs>( |
| 360 SK_ARRAY_COUNT(gGrayVertexAttribs), kGrayTex
tVASize); |
| 361 } else if (kARGB_GrMaskFormat == maskFormat) { |
| 362 GrDefaultGeoProcFactory::SetAttribs(drawState, |
| 363 GrDefaultGeoProcFactory::kLocalCoord
_GPType); |
| 364 } else { |
| 365 drawState->setVertexAttribs<gLCDVertexAttribs>( |
| 366 SK_ARRAY_COUNT(gLCDVertexAttribs), kLCDTextV
ASize); |
| 367 } |
| 368 } |
| 369 |
| 370 static void* alloc_vertices(GrDrawTarget* drawTarget, |
| 371 int numVertices, |
| 372 GrMaskFormat maskFormat) { |
351 if (numVertices <= 0) { | 373 if (numVertices <= 0) { |
352 return NULL; | 374 return NULL; |
353 } | 375 } |
354 | 376 |
355 // set up attributes | 377 // set up attributes |
356 if (kA8_GrMaskFormat == maskFormat) { | |
357 drawTarget->drawState()->setVertexAttribs<gGrayVertexAttribs>( | |
358 SK_ARRAY_COUNT(gGrayVertexAttribs), kGrayTex
tVASize); | |
359 } else if (kARGB_GrMaskFormat == maskFormat) { | |
360 GrDefaultGeoProcFactory::SetAttribs(drawTarget->drawState(), | |
361 GrDefaultGeoProcFactory::kLocalCoord
_GPType); | |
362 } else { | |
363 drawTarget->drawState()->setVertexAttribs<gLCDVertexAttribs>( | |
364 SK_ARRAY_COUNT(gLCDVertexAttribs), kLCDTextV
ASize); | |
365 } | |
366 void* vertices = NULL; | 378 void* vertices = NULL; |
367 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, | 379 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, |
| 380 get_vertex_stride(mask
Format), |
368 0, | 381 0, |
369 &vertices, | 382 &vertices, |
370 NULL); | 383 NULL); |
371 GrAlwaysAssert(success); | 384 GrAlwaysAssert(success); |
372 return vertices; | 385 return vertices; |
373 } | 386 } |
374 | 387 |
375 void GrBitmapTextContext::appendGlyph(GrGlyph::PackedID packed, | 388 void GrBitmapTextContext::appendGlyph(GrGlyph::PackedID packed, |
376 SkFixed vx, SkFixed vy, | 389 SkFixed vx, SkFixed vy, |
377 GrFontScaler* scaler) { | 390 GrFontScaler* scaler) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); | 504 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); |
492 | 505 |
493 SkRect r; | 506 SkRect r; |
494 r.fLeft = SkFixedToFloat(vx); | 507 r.fLeft = SkFixedToFloat(vx); |
495 r.fTop = SkFixedToFloat(vy); | 508 r.fTop = SkFixedToFloat(vy); |
496 r.fRight = SkFixedToFloat(vx + width); | 509 r.fRight = SkFixedToFloat(vx + width); |
497 r.fBottom = SkFixedToFloat(vy + height); | 510 r.fBottom = SkFixedToFloat(vy + height); |
498 | 511 |
499 fVertexBounds.joinNonEmptyArg(r); | 512 fVertexBounds.joinNonEmptyArg(r); |
500 | 513 |
501 size_t vertSize; | 514 size_t vertSize = get_vertex_stride(fCurrMaskFormat); |
502 switch (fCurrMaskFormat) { | |
503 case kA8_GrMaskFormat: | |
504 vertSize = kGrayTextVASize; | |
505 break; | |
506 case kARGB_GrMaskFormat: | |
507 vertSize = kColorTextVASize; | |
508 default: | |
509 vertSize = kLCDTextVASize; | |
510 } | |
511 | |
512 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride()); | |
513 | 515 |
514 SkPoint* positions = reinterpret_cast<SkPoint*>( | 516 SkPoint* positions = reinterpret_cast<SkPoint*>( |
515 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); | 517 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); |
516 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); | 518 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); |
517 | 519 |
518 // The texture coords are last in both the with and without color vertex lay
outs. | 520 // The texture coords are last in both the with and without color vertex lay
outs. |
519 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( | 521 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( |
520 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint))
; | 522 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint))
; |
521 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx)), | 523 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx)), |
522 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty)), | 524 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty)), |
523 SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx + width)), | 525 SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx + width)), |
524 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty + height)), | 526 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty + height)), |
525 vertSize); | 527 vertSize); |
526 if (kA8_GrMaskFormat == fCurrMaskFormat) { | 528 if (kA8_GrMaskFormat == fCurrMaskFormat) { |
527 if (0xFF == GrColorUnpackA(fPaint.getColor())) { | |
528 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaqu
e_Hint, true); | |
529 } | |
530 // color comes after position. | 529 // color comes after position. |
531 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); | 530 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); |
532 for (int i = 0; i < 4; ++i) { | 531 for (int i = 0; i < 4; ++i) { |
533 *colors = fPaint.getColor(); | 532 *colors = fPaint.getColor(); |
534 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors
) + vertSize); | 533 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors
) + vertSize); |
535 } | 534 } |
536 } | 535 } |
537 fCurrVertex += 4; | 536 fCurrVertex += 4; |
538 } | 537 } |
539 | 538 |
540 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { | 539 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { |
541 unsigned r = SkColorGetR(c); | 540 unsigned r = SkColorGetR(c); |
542 unsigned g = SkColorGetG(c); | 541 unsigned g = SkColorGetG(c); |
543 unsigned b = SkColorGetB(c); | 542 unsigned b = SkColorGetB(c); |
544 return GrColorPackRGBA(r, g, b, 0xff); | 543 return GrColorPackRGBA(r, g, b, 0xff); |
545 } | 544 } |
546 | 545 |
547 void GrBitmapTextContext::flush() { | 546 void GrBitmapTextContext::flush() { |
548 if (NULL == fDrawTarget) { | 547 if (NULL == fDrawTarget) { |
549 return; | 548 return; |
550 } | 549 } |
551 | 550 |
552 GrDrawState* drawState = fDrawTarget->drawState(); | 551 GrDrawState drawState; |
553 GrDrawState::AutoRestoreEffects are(drawState); | 552 drawState.setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); |
554 drawState->setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); | 553 |
| 554 set_vertex_attributes(&drawState, fCurrMaskFormat); |
555 | 555 |
556 if (fCurrVertex > 0) { | 556 if (fCurrVertex > 0) { |
557 // setup our sampler state for our text texture/atlas | 557 // setup our sampler state for our text texture/atlas |
558 SkASSERT(SkIsAlign4(fCurrVertex)); | 558 SkASSERT(SkIsAlign4(fCurrVertex)); |
559 SkASSERT(fCurrTexture); | 559 SkASSERT(fCurrTexture); |
560 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNon
e_FilterMode); | 560 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNon
e_FilterMode); |
561 | 561 |
562 // This effect could be stored with one of the cache objects (atlas?) | 562 // This effect could be stored with one of the cache objects (atlas?) |
563 if (kARGB_GrMaskFormat == fCurrMaskFormat) { | 563 if (kARGB_GrMaskFormat == fCurrMaskFormat) { |
564 drawState->setGeometryProcessor(GrDefaultGeoProcFactory::Create(true
))->unref(); | 564 drawState.setGeometryProcessor(GrDefaultGeoProcFactory::Create(true)
)->unref(); |
565 GrFragmentProcessor* fragProcessor = GrSimpleTextureEffect::Create(f
CurrTexture, | 565 GrFragmentProcessor* fragProcessor = GrSimpleTextureEffect::Create(f
CurrTexture, |
566 S
kMatrix::I(), | 566 S
kMatrix::I(), |
567 p
arams); | 567 p
arams); |
568 drawState->addColorProcessor(fragProcessor)->unref(); | 568 drawState.addColorProcessor(fragProcessor)->unref(); |
569 } else { | 569 } else { |
570 uint32_t textureUniqueID = fCurrTexture->getUniqueID(); | 570 uint32_t textureUniqueID = fCurrTexture->getUniqueID(); |
571 if (textureUniqueID != fEffectTextureUniqueID) { | 571 if (textureUniqueID != fEffectTextureUniqueID) { |
572 fCachedGeometryProcessor.reset(GrCustomCoordsTextureEffect::Crea
te(fCurrTexture, | 572 fCachedGeometryProcessor.reset(GrCustomCoordsTextureEffect::Crea
te(fCurrTexture, |
573
params)); | 573
params)); |
574 fEffectTextureUniqueID = textureUniqueID; | 574 fEffectTextureUniqueID = textureUniqueID; |
575 } | 575 } |
576 | 576 |
577 drawState->setGeometryProcessor(fCachedGeometryProcessor.get()); | 577 drawState.setGeometryProcessor(fCachedGeometryProcessor.get()); |
578 } | 578 } |
579 | 579 |
580 SkASSERT(fStrike); | 580 SkASSERT(fStrike); |
581 switch (fCurrMaskFormat) { | 581 switch (fCurrMaskFormat) { |
582 // Color bitmap text | 582 // Color bitmap text |
583 case kARGB_GrMaskFormat: | 583 case kARGB_GrMaskFormat: |
584 SkASSERT(!drawState->hasColorVertexAttribute()); | 584 SkASSERT(!drawState.hasColorVertexAttribute()); |
585 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDst
BlendCoeff()); | 585 drawState.setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstB
lendCoeff()); |
586 drawState->setAlpha(fSkPaint.getAlpha()); | 586 drawState.setAlpha(fSkPaint.getAlpha()); |
587 break; | 587 break; |
588 // LCD text | 588 // LCD text |
589 case kA565_GrMaskFormat: { | 589 case kA565_GrMaskFormat: { |
590 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || | 590 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
591 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || | 591 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
592 fPaint.numColorStages()) { | 592 fPaint.numColorStages()) { |
593 SkDebugf("LCD Text will not draw correctly.\n"); | 593 SkDebugf("LCD Text will not draw correctly.\n"); |
594 } | 594 } |
595 SkASSERT(!drawState->hasColorVertexAttribute()); | 595 SkASSERT(!drawState.hasColorVertexAttribute()); |
596 // We don't use the GrPaint's color in this case because it's be
en premultiplied by | 596 // We don't use the GrPaint's color in this case because it's be
en premultiplied by |
597 // alpha. Instead we feed in a non-premultiplied color, and mult
iply its alpha by | 597 // alpha. Instead we feed in a non-premultiplied color, and mult
iply its alpha by |
598 // the mask texture color. The end result is that we get | 598 // the mask texture color. The end result is that we get |
599 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*d
stColor | 599 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*d
stColor |
600 int a = SkColorGetA(fSkPaint.getColor()); | 600 int a = SkColorGetA(fSkPaint.getColor()); |
601 // paintAlpha | 601 // paintAlpha |
602 drawState->setColor(SkColorSetARGB(a, a, a, a)); | 602 drawState.setColor(SkColorSetARGB(a, a, a, a)); |
603 // paintColor | 603 // paintColor |
604 drawState->setBlendConstant(skcolor_to_grcolor_nopremultiply(fSk
Paint.getColor())); | 604 drawState.setBlendConstant(skcolor_to_grcolor_nopremultiply(fSkP
aint.getColor())); |
605 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff)
; | 605 drawState.setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
606 break; | 606 break; |
607 } | 607 } |
608 // Grayscale/BW text | 608 // Grayscale/BW text |
609 case kA8_GrMaskFormat: | 609 case kA8_GrMaskFormat: |
| 610 drawState.setHint(GrDrawState::kVertexColorsAreOpaque_Hint, |
| 611 0xFF == GrColorUnpackA(fPaint.getColor())); |
610 // set back to normal in case we took LCD path previously. | 612 // set back to normal in case we took LCD path previously. |
611 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDst
BlendCoeff()); | 613 drawState.setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstB
lendCoeff()); |
612 // We're using per-vertex color. | 614 // We're using per-vertex color. |
613 SkASSERT(drawState->hasColorVertexAttribute()); | 615 SkASSERT(drawState.hasColorVertexAttribute()); |
614 break; | 616 break; |
615 default: | 617 default: |
616 SkFAIL("Unexpected mask format."); | 618 SkFAIL("Unexpected mask format."); |
617 } | 619 } |
618 int nGlyphs = fCurrVertex / kVerticesPerGlyph; | 620 int nGlyphs = fCurrVertex / kVerticesPerGlyph; |
619 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); | 621 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
620 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, | 622 fDrawTarget->drawIndexedInstances(&drawState, |
| 623 kTriangles_GrPrimitiveType, |
621 nGlyphs, | 624 nGlyphs, |
622 kVerticesPerGlyph, kIndicesPerGlyph, &
fVertexBounds); | 625 kVerticesPerGlyph, |
| 626 kIndicesPerGlyph, |
| 627 &fVertexBounds); |
623 | 628 |
624 fDrawTarget->resetVertexSource(); | 629 fDrawTarget->resetVertexSource(); |
625 fVertices = NULL; | 630 fVertices = NULL; |
626 fAllocVertexCount = 0; | 631 fAllocVertexCount = 0; |
627 // reset to be those that are left | 632 // reset to be those that are left |
628 fTotalVertexCount -= fCurrVertex; | 633 fTotalVertexCount -= fCurrVertex; |
629 fCurrVertex = 0; | 634 fCurrVertex = 0; |
630 fVertexBounds.setLargestInverted(); | 635 fVertexBounds.setLargestInverted(); |
631 SkSafeSetNull(fCurrTexture); | 636 SkSafeSetNull(fCurrTexture); |
632 } | 637 } |
633 } | 638 } |
634 | 639 |
635 inline void GrBitmapTextContext::finish() { | 640 inline void GrBitmapTextContext::finish() { |
636 this->flush(); | 641 this->flush(); |
637 fTotalVertexCount = 0; | 642 fTotalVertexCount = 0; |
638 | 643 |
639 GrTextContext::finish(); | 644 GrTextContext::finish(); |
640 } | 645 } |
641 | 646 |
OLD | NEW |