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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 fontScaler); | 340 fontScaler); |
345 } | 341 } |
346 pos += scalarsPerPosition; | 342 pos += scalarsPerPosition; |
347 } | 343 } |
348 } | 344 } |
349 } | 345 } |
350 | 346 |
351 this->finish(); | 347 this->finish(); |
352 } | 348 } |
353 | 349 |
354 static void* alloc_vertices(GrDrawTarget* drawTarget, int numVertices, GrMaskFor
mat maskFormat) { | 350 static size_t get_vertex_stride(GrMaskFormat maskFormat) { |
| 351 switch (maskFormat) { |
| 352 case kA8_GrMaskFormat: |
| 353 return kGrayTextVASize; |
| 354 case kARGB_GrMaskFormat: |
| 355 return kColorTextVASize; |
| 356 default: |
| 357 return kLCDTextVASize; |
| 358 } |
| 359 } |
| 360 |
| 361 static void set_vertex_attributes(GrDrawState* drawState, GrMaskFormat maskForma
t) { |
| 362 if (kA8_GrMaskFormat == maskFormat) { |
| 363 drawState->setVertexAttribs<gGrayVertexAttribs>( |
| 364 SK_ARRAY_COUNT(gGrayVertexAttribs), kGrayTex
tVASize); |
| 365 } else if (kARGB_GrMaskFormat == maskFormat) { |
| 366 GrDefaultGeoProcFactory::SetAttribs(drawState, |
| 367 GrDefaultGeoProcFactory::kLocalCoord
_GPType); |
| 368 } else { |
| 369 drawState->setVertexAttribs<gLCDVertexAttribs>( |
| 370 SK_ARRAY_COUNT(gLCDVertexAttribs), kLCDTextV
ASize); |
| 371 } |
| 372 } |
| 373 |
| 374 static void* alloc_vertices(GrDrawTarget* drawTarget, |
| 375 int numVertices, |
| 376 GrMaskFormat maskFormat) { |
355 if (numVertices <= 0) { | 377 if (numVertices <= 0) { |
356 return NULL; | 378 return NULL; |
357 } | 379 } |
358 | 380 |
359 // set up attributes | 381 // set up attributes |
360 if (kA8_GrMaskFormat == maskFormat) { | |
361 drawTarget->drawState()->setVertexAttribs<gGrayVertexAttribs>( | |
362 SK_ARRAY_COUNT(gGrayVertexAttribs), kGrayTex
tVASize); | |
363 } else if (kARGB_GrMaskFormat == maskFormat) { | |
364 GrDefaultGeoProcFactory::SetAttribs(drawTarget->drawState(), | |
365 GrDefaultGeoProcFactory::kLocalCoord
_GPType); | |
366 } else { | |
367 drawTarget->drawState()->setVertexAttribs<gLCDVertexAttribs>( | |
368 SK_ARRAY_COUNT(gLCDVertexAttribs), kLCDTextV
ASize); | |
369 } | |
370 void* vertices = NULL; | 382 void* vertices = NULL; |
371 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, | 383 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, |
| 384 get_vertex_stride(mask
Format), |
372 0, | 385 0, |
373 &vertices, | 386 &vertices, |
374 NULL); | 387 NULL); |
375 GrAlwaysAssert(success); | 388 GrAlwaysAssert(success); |
376 return vertices; | 389 return vertices; |
377 } | 390 } |
378 | 391 |
379 void GrBitmapTextContext::appendGlyph(GrGlyph::PackedID packed, | 392 void GrBitmapTextContext::appendGlyph(GrGlyph::PackedID packed, |
380 SkFixed vx, SkFixed vy, | 393 SkFixed vx, SkFixed vy, |
381 GrFontScaler* scaler) { | 394 GrFontScaler* scaler) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); | 508 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); |
496 | 509 |
497 SkRect r; | 510 SkRect r; |
498 r.fLeft = SkFixedToFloat(vx); | 511 r.fLeft = SkFixedToFloat(vx); |
499 r.fTop = SkFixedToFloat(vy); | 512 r.fTop = SkFixedToFloat(vy); |
500 r.fRight = SkFixedToFloat(vx + width); | 513 r.fRight = SkFixedToFloat(vx + width); |
501 r.fBottom = SkFixedToFloat(vy + height); | 514 r.fBottom = SkFixedToFloat(vy + height); |
502 | 515 |
503 fVertexBounds.joinNonEmptyArg(r); | 516 fVertexBounds.joinNonEmptyArg(r); |
504 | 517 |
505 size_t vertSize; | 518 size_t vertSize = get_vertex_stride(fCurrMaskFormat); |
506 switch (fCurrMaskFormat) { | |
507 case kA8_GrMaskFormat: | |
508 vertSize = kGrayTextVASize; | |
509 break; | |
510 case kARGB_GrMaskFormat: | |
511 vertSize = kColorTextVASize; | |
512 default: | |
513 vertSize = kLCDTextVASize; | |
514 } | |
515 | |
516 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride()); | |
517 | 519 |
518 SkPoint* positions = reinterpret_cast<SkPoint*>( | 520 SkPoint* positions = reinterpret_cast<SkPoint*>( |
519 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); | 521 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); |
520 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); | 522 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); |
521 | 523 |
522 // The texture coords are last in both the with and without color vertex lay
outs. | 524 // The texture coords are last in both the with and without color vertex lay
outs. |
523 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( | 525 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( |
524 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint))
; | 526 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint))
; |
525 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx)), | 527 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx)), |
526 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty)), | 528 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty)), |
527 SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx + width)), | 529 SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx + width)), |
528 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty + height)), | 530 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty + height)), |
529 vertSize); | 531 vertSize); |
530 if (kA8_GrMaskFormat == fCurrMaskFormat) { | 532 if (kA8_GrMaskFormat == fCurrMaskFormat) { |
531 if (0xFF == GrColorUnpackA(fPaint.getColor())) { | |
532 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaqu
e_Hint, true); | |
533 } | |
534 // color comes after position. | 533 // color comes after position. |
535 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); | 534 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); |
536 for (int i = 0; i < 4; ++i) { | 535 for (int i = 0; i < 4; ++i) { |
537 *colors = fPaint.getColor(); | 536 *colors = fPaint.getColor(); |
538 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors
) + vertSize); | 537 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors
) + vertSize); |
539 } | 538 } |
540 } | 539 } |
541 fCurrVertex += 4; | 540 fCurrVertex += 4; |
542 } | 541 } |
543 | 542 |
544 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { | 543 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { |
545 unsigned r = SkColorGetR(c); | 544 unsigned r = SkColorGetR(c); |
546 unsigned g = SkColorGetG(c); | 545 unsigned g = SkColorGetG(c); |
547 unsigned b = SkColorGetB(c); | 546 unsigned b = SkColorGetB(c); |
548 return GrColorPackRGBA(r, g, b, 0xff); | 547 return GrColorPackRGBA(r, g, b, 0xff); |
549 } | 548 } |
550 | 549 |
551 void GrBitmapTextContext::flush() { | 550 void GrBitmapTextContext::flush() { |
552 if (NULL == fDrawTarget) { | 551 if (NULL == fDrawTarget) { |
553 return; | 552 return; |
554 } | 553 } |
555 | 554 |
556 GrDrawState* drawState = fDrawTarget->drawState(); | 555 GrDrawState drawState; |
557 GrDrawState::AutoRestoreEffects are(drawState); | 556 drawState.setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); |
558 drawState->setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); | 557 |
| 558 set_vertex_attributes(&drawState, fCurrMaskFormat); |
559 | 559 |
560 if (fCurrVertex > 0) { | 560 if (fCurrVertex > 0) { |
561 // setup our sampler state for our text texture/atlas | 561 // setup our sampler state for our text texture/atlas |
562 SkASSERT(SkIsAlign4(fCurrVertex)); | 562 SkASSERT(SkIsAlign4(fCurrVertex)); |
563 SkASSERT(fCurrTexture); | 563 SkASSERT(fCurrTexture); |
564 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNon
e_FilterMode); | 564 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNon
e_FilterMode); |
565 | 565 |
566 // This effect could be stored with one of the cache objects (atlas?) | 566 // This effect could be stored with one of the cache objects (atlas?) |
567 if (kARGB_GrMaskFormat == fCurrMaskFormat) { | 567 if (kARGB_GrMaskFormat == fCurrMaskFormat) { |
568 drawState->setGeometryProcessor(GrDefaultGeoProcFactory::Create(true
))->unref(); | 568 drawState.setGeometryProcessor(GrDefaultGeoProcFactory::Create(true)
)->unref(); |
569 GrFragmentProcessor* fragProcessor = GrSimpleTextureEffect::Create(f
CurrTexture, | 569 GrFragmentProcessor* fragProcessor = GrSimpleTextureEffect::Create(f
CurrTexture, |
570 S
kMatrix::I(), | 570 S
kMatrix::I(), |
571 p
arams); | 571 p
arams); |
572 drawState->addColorProcessor(fragProcessor)->unref(); | 572 drawState.addColorProcessor(fragProcessor)->unref(); |
573 } else { | 573 } else { |
574 uint32_t textureUniqueID = fCurrTexture->getUniqueID(); | 574 uint32_t textureUniqueID = fCurrTexture->getUniqueID(); |
575 if (textureUniqueID != fEffectTextureUniqueID) { | 575 if (textureUniqueID != fEffectTextureUniqueID) { |
576 fCachedGeometryProcessor.reset(GrCustomCoordsTextureEffect::Crea
te(fCurrTexture, | 576 fCachedGeometryProcessor.reset(GrCustomCoordsTextureEffect::Crea
te(fCurrTexture, |
577
params)); | 577
params)); |
578 fEffectTextureUniqueID = textureUniqueID; | 578 fEffectTextureUniqueID = textureUniqueID; |
579 } | 579 } |
580 | 580 |
581 drawState->setGeometryProcessor(fCachedGeometryProcessor.get()); | 581 drawState.setGeometryProcessor(fCachedGeometryProcessor.get()); |
582 } | 582 } |
583 | 583 |
584 SkASSERT(fStrike); | 584 SkASSERT(fStrike); |
585 switch (fCurrMaskFormat) { | 585 switch (fCurrMaskFormat) { |
586 // Color bitmap text | 586 // Color bitmap text |
587 case kARGB_GrMaskFormat: | 587 case kARGB_GrMaskFormat: |
588 SkASSERT(!drawState->hasColorVertexAttribute()); | 588 SkASSERT(!drawState.hasColorVertexAttribute()); |
589 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDst
BlendCoeff()); | 589 drawState.setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstB
lendCoeff()); |
590 drawState->setAlpha(fSkPaint.getAlpha()); | 590 drawState.setAlpha(fSkPaint.getAlpha()); |
591 break; | 591 break; |
592 // LCD text | 592 // LCD text |
593 case kA888_GrMaskFormat: | 593 case kA888_GrMaskFormat: |
594 case kA565_GrMaskFormat: { | 594 case kA565_GrMaskFormat: { |
595 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || | 595 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
596 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || | 596 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
597 fPaint.numColorStages()) { | 597 fPaint.numColorStages()) { |
598 SkDebugf("LCD Text will not draw correctly.\n"); | 598 SkDebugf("LCD Text will not draw correctly.\n"); |
599 } | 599 } |
600 SkASSERT(!drawState->hasColorVertexAttribute()); | 600 SkASSERT(!drawState.hasColorVertexAttribute()); |
601 // We don't use the GrPaint's color in this case because it's be
en premultiplied by | 601 // We don't use the GrPaint's color in this case because it's be
en premultiplied by |
602 // alpha. Instead we feed in a non-premultiplied color, and mult
iply its alpha by | 602 // alpha. Instead we feed in a non-premultiplied color, and mult
iply its alpha by |
603 // the mask texture color. The end result is that we get | 603 // the mask texture color. The end result is that we get |
604 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*d
stColor | 604 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*d
stColor |
605 int a = SkColorGetA(fSkPaint.getColor()); | 605 int a = SkColorGetA(fSkPaint.getColor()); |
606 // paintAlpha | 606 // paintAlpha |
607 drawState->setColor(SkColorSetARGB(a, a, a, a)); | 607 drawState.setColor(SkColorSetARGB(a, a, a, a)); |
608 // paintColor | 608 // paintColor |
609 drawState->setBlendConstant(skcolor_to_grcolor_nopremultiply(fSk
Paint.getColor())); | 609 drawState.setBlendConstant(skcolor_to_grcolor_nopremultiply(fSkP
aint.getColor())); |
610 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff)
; | 610 drawState.setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
611 break; | 611 break; |
612 } | 612 } |
613 // Grayscale/BW text | 613 // Grayscale/BW text |
614 case kA8_GrMaskFormat: | 614 case kA8_GrMaskFormat: |
| 615 drawState.setHint(GrDrawState::kVertexColorsAreOpaque_Hint, |
| 616 0xFF == GrColorUnpackA(fPaint.getColor())); |
615 // set back to normal in case we took LCD path previously. | 617 // set back to normal in case we took LCD path previously. |
616 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDst
BlendCoeff()); | 618 drawState.setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstB
lendCoeff()); |
617 // We're using per-vertex color. | 619 // We're using per-vertex color. |
618 SkASSERT(drawState->hasColorVertexAttribute()); | 620 SkASSERT(drawState.hasColorVertexAttribute()); |
619 break; | 621 break; |
620 default: | 622 default: |
621 SkFAIL("Unexpected mask format."); | 623 SkFAIL("Unexpected mask format."); |
622 } | 624 } |
623 int nGlyphs = fCurrVertex / kVerticesPerGlyph; | 625 int nGlyphs = fCurrVertex / kVerticesPerGlyph; |
624 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); | 626 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
625 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, | 627 fDrawTarget->drawIndexedInstances(&drawState, |
| 628 kTriangles_GrPrimitiveType, |
626 nGlyphs, | 629 nGlyphs, |
627 kVerticesPerGlyph, kIndicesPerGlyph, &
fVertexBounds); | 630 kVerticesPerGlyph, |
| 631 kIndicesPerGlyph, |
| 632 &fVertexBounds); |
628 | 633 |
629 fDrawTarget->resetVertexSource(); | 634 fDrawTarget->resetVertexSource(); |
630 fVertices = NULL; | 635 fVertices = NULL; |
631 fAllocVertexCount = 0; | 636 fAllocVertexCount = 0; |
632 // reset to be those that are left | 637 // reset to be those that are left |
633 fTotalVertexCount -= fCurrVertex; | 638 fTotalVertexCount -= fCurrVertex; |
634 fCurrVertex = 0; | 639 fCurrVertex = 0; |
635 fVertexBounds.setLargestInverted(); | 640 fVertexBounds.setLargestInverted(); |
636 SkSafeSetNull(fCurrTexture); | 641 SkSafeSetNull(fCurrTexture); |
637 } | 642 } |
638 } | 643 } |
639 | 644 |
640 inline void GrBitmapTextContext::finish() { | 645 inline void GrBitmapTextContext::finish() { |
641 this->flush(); | 646 this->flush(); |
642 fTotalVertexCount = 0; | 647 fTotalVertexCount = 0; |
643 | 648 |
644 GrTextContext::finish(); | 649 GrTextContext::finish(); |
645 } | 650 } |
646 | 651 |
OLD | NEW |