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 "GrDistanceFieldTextContext.h" | 8 #include "GrDistanceFieldTextContext.h" |
9 #include "GrAtlas.h" | 9 #include "GrAtlas.h" |
10 #include "GrBitmapTextContext.h" | 10 #include "GrBitmapTextContext.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 const SkDevicePro
perties& props, | 89 const SkDevicePro
perties& props, |
90 bool enable) { | 90 bool enable) { |
91 GrDistanceFieldTextContext* textContext = SkNEW_ARGS(GrDistanceFieldTextCont
ext, | 91 GrDistanceFieldTextContext* textContext = SkNEW_ARGS(GrDistanceFieldTextCont
ext, |
92 (context, props, enable
)); | 92 (context, props, enable
)); |
93 textContext->fFallbackTextContext = GrBitmapTextContext::Create(context, pro
ps); | 93 textContext->fFallbackTextContext = GrBitmapTextContext::Create(context, pro
ps); |
94 | 94 |
95 return textContext; | 95 return textContext; |
96 } | 96 } |
97 | 97 |
98 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { | 98 GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { |
99 this->finish(); | |
100 SkSafeSetNull(fGammaTexture); | 99 SkSafeSetNull(fGammaTexture); |
101 } | 100 } |
102 | 101 |
103 bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) { | 102 bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) { |
104 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) { | 103 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) { |
105 return false; | 104 return false; |
106 } | 105 } |
107 | 106 |
108 // rasterizers and mask filters modify alpha, which doesn't | 107 // rasterizers and mask filters modify alpha, which doesn't |
109 // translate well to distance | 108 // translate well to distance |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 374 } |
376 } | 375 } |
377 | 376 |
378 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { | 377 static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { |
379 unsigned r = SkColorGetR(c); | 378 unsigned r = SkColorGetR(c); |
380 unsigned g = SkColorGetG(c); | 379 unsigned g = SkColorGetG(c); |
381 unsigned b = SkColorGetB(c); | 380 unsigned b = SkColorGetB(c); |
382 return GrColorPackRGBA(r, g, b, 0xff); | 381 return GrColorPackRGBA(r, g, b, 0xff); |
383 } | 382 } |
384 | 383 |
385 static void* alloc_vertices(GrDrawTarget* drawTarget, int numVertices, bool useC
olorVerts) { | 384 static size_t get_vertex_stride(bool useColorVerts) { |
| 385 return useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) : |
| 386 (2 * sizeof(SkPoint)); |
| 387 } |
| 388 |
| 389 static void* alloc_vertices(GrDrawTarget* drawTarget, |
| 390 int numVertices, |
| 391 bool useColorVerts) { |
386 if (numVertices <= 0) { | 392 if (numVertices <= 0) { |
387 return NULL; | 393 return NULL; |
388 } | 394 } |
389 | 395 |
390 // set up attributes | |
391 if (useColorVerts) { | |
392 drawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>( | |
393 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
kTextVAColorSize); | |
394 } else { | |
395 drawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( | |
396 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVAS
ize); | |
397 } | |
398 void* vertices = NULL; | 396 void* vertices = NULL; |
399 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, | 397 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, |
| 398 get_vertex_stride(useC
olorVerts), |
400 0, | 399 0, |
401 &vertices, | 400 &vertices, |
402 NULL); | 401 NULL); |
403 GrAlwaysAssert(success); | 402 GrAlwaysAssert(success); |
404 return vertices; | 403 return vertices; |
405 } | 404 } |
406 | 405 |
407 void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColo
r) { | 406 void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColo
r) { |
408 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_
FilterMode); | 407 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_
FilterMode); |
409 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNon
e_FilterMode); | 408 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNon
e_FilterMode); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 this->flush(); | 570 this->flush(); |
572 fCurrTexture = texture; | 571 fCurrTexture = texture; |
573 fCurrTexture->ref(); | 572 fCurrTexture->ref(); |
574 } | 573 } |
575 | 574 |
576 bool useColorVerts = !fUseLCDText; | 575 bool useColorVerts = !fUseLCDText; |
577 | 576 |
578 if (NULL == fVertices) { | 577 if (NULL == fVertices) { |
579 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()
->maxQuads(); | 578 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()
->maxQuads(); |
580 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices); | 579 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices); |
581 fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, useColorVerts
); | 580 fVertices = alloc_vertices(fDrawTarget, |
| 581 fAllocVertexCount, |
| 582 useColorVerts); |
582 } | 583 } |
583 | 584 |
584 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset); | 585 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset); |
585 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset); | 586 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset); |
586 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset); | 587 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset); |
587 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset)
; | 588 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset)
; |
588 | 589 |
589 fVertexBounds.joinNonEmptyArg(glyphRect); | 590 fVertexBounds.joinNonEmptyArg(glyphRect); |
590 | 591 |
591 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint)) | 592 size_t vertSize = get_vertex_stride(useColorVerts); |
592 : (2 * sizeof(SkPoint) + sizeof(GrColor)); | |
593 | |
594 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride()); | |
595 | 593 |
596 SkPoint* positions = reinterpret_cast<SkPoint*>( | 594 SkPoint* positions = reinterpret_cast<SkPoint*>( |
597 reinterpret_cast<intptr_t>(fVertices) + vertSize
* fCurrVertex); | 595 reinterpret_cast<intptr_t>(fVertices) + vertSize
* fCurrVertex); |
598 positions->setRectFan(glyphRect.fLeft, glyphRect.fTop, glyphRect.fRight, gly
phRect.fBottom, | 596 positions->setRectFan(glyphRect.fLeft, glyphRect.fTop, glyphRect.fRight, gly
phRect.fBottom, |
599 vertSize); | 597 vertSize); |
600 | 598 |
601 // The texture coords are last in both the with and without color vertex lay
outs. | 599 // The texture coords are last in both the with and without color vertex lay
outs. |
602 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( | 600 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( |
603 reinterpret_cast<intptr_t>(positions) + vertSize
- sizeof(SkPoint)); | 601 reinterpret_cast<intptr_t>(positions) + vertSize
- sizeof(SkPoint)); |
604 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx)), | 602 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx)), |
605 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty)), | 603 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty)), |
606 SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx + tw)), | 604 SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx + tw)), |
607 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty + th)), | 605 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty + th)), |
608 vertSize); | 606 vertSize); |
609 if (useColorVerts) { | 607 if (useColorVerts) { |
610 if (0xFF == GrColorUnpackA(fPaint.getColor())) { | |
611 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaqu
e_Hint, true); | |
612 } | |
613 // color comes after position. | 608 // color comes after position. |
614 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); | 609 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); |
615 for (int i = 0; i < 4; ++i) { | 610 for (int i = 0; i < 4; ++i) { |
616 *colors = fPaint.getColor(); | 611 *colors = fPaint.getColor(); |
617 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(color
s) + vertSize); | 612 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(color
s) + vertSize); |
618 } | 613 } |
619 } | 614 } |
620 | 615 |
621 fCurrVertex += 4; | 616 fCurrVertex += 4; |
622 | 617 |
623 return true; | 618 return true; |
624 } | 619 } |
625 | 620 |
| 621 // We use color vertices if we aren't drawing LCD text |
| 622 static void set_vertex_attributes(GrDrawState* drawState, bool useColorVerts) { |
| 623 // set up attributes |
| 624 if (useColorVerts) { |
| 625 drawState->setVertexAttribs<gTextVertexWithColorAttribs>( |
| 626 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
kTextVAColorSize); |
| 627 } else { |
| 628 drawState->setVertexAttribs<gTextVertexAttribs>( |
| 629 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVAS
ize); |
| 630 } |
| 631 } |
| 632 |
626 void GrDistanceFieldTextContext::flush() { | 633 void GrDistanceFieldTextContext::flush() { |
627 if (NULL == fDrawTarget) { | 634 if (NULL == fDrawTarget) { |
628 return; | 635 return; |
629 } | 636 } |
630 | 637 |
631 GrDrawState* drawState = fDrawTarget->drawState(); | 638 GrDrawState drawState; |
632 GrDrawState::AutoRestoreEffects are(drawState); | 639 drawState.setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTar
get()); |
633 | 640 bool useColorVerts = !fUseLCDText; |
634 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTa
rget()); | 641 set_vertex_attributes(&drawState, useColorVerts); |
635 | 642 |
636 if (fCurrVertex > 0) { | 643 if (fCurrVertex > 0) { |
637 // setup our sampler state for our text texture/atlas | 644 // setup our sampler state for our text texture/atlas |
638 SkASSERT(SkIsAlign4(fCurrVertex)); | 645 SkASSERT(SkIsAlign4(fCurrVertex)); |
639 | 646 |
640 // get our current color | 647 // get our current color |
641 SkColor filteredColor; | 648 SkColor filteredColor; |
642 SkColorFilter* colorFilter = fSkPaint.getColorFilter(); | 649 SkColorFilter* colorFilter = fSkPaint.getColorFilter(); |
643 if (colorFilter) { | 650 if (colorFilter) { |
644 filteredColor = colorFilter->filterColor(fSkPaint.getColor()); | 651 filteredColor = colorFilter->filterColor(fSkPaint.getColor()); |
645 } else { | 652 } else { |
646 filteredColor = fSkPaint.getColor(); | 653 filteredColor = fSkPaint.getColor(); |
647 } | 654 } |
648 this->setupCoverageEffect(filteredColor); | 655 this->setupCoverageEffect(filteredColor); |
649 | 656 |
650 // Effects could be stored with one of the cache objects (atlas?) | 657 // Effects could be stored with one of the cache objects (atlas?) |
651 drawState->setGeometryProcessor(fCachedGeometryProcessor.get()); | 658 drawState.setGeometryProcessor(fCachedGeometryProcessor.get()); |
652 | 659 |
653 // Set draw state | 660 // Set draw state |
654 if (fUseLCDText) { | 661 if (fUseLCDText) { |
655 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredCol
or); | 662 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredCol
or); |
656 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || | 663 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
657 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || | 664 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
658 fPaint.numColorStages()) { | 665 fPaint.numColorStages()) { |
659 SkDebugf("LCD Text will not draw correctly.\n"); | 666 SkDebugf("LCD Text will not draw correctly.\n"); |
660 } | 667 } |
661 SkASSERT(!drawState->hasColorVertexAttribute()); | 668 SkASSERT(!drawState.hasColorVertexAttribute()); |
662 // We don't use the GrPaint's color in this case because it's been p
remultiplied by | 669 // We don't use the GrPaint's color in this case because it's been p
remultiplied by |
663 // alpha. Instead we feed in a non-premultiplied color, and multiply
its alpha by | 670 // alpha. Instead we feed in a non-premultiplied color, and multiply
its alpha by |
664 // the mask texture color. The end result is that we get | 671 // the mask texture color. The end result is that we get |
665 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo
lor | 672 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstCo
lor |
666 int a = SkColorGetA(fSkPaint.getColor()); | 673 int a = SkColorGetA(fSkPaint.getColor()); |
667 // paintAlpha | 674 // paintAlpha |
668 drawState->setColor(SkColorSetARGB(a, a, a, a)); | 675 drawState.setColor(SkColorSetARGB(a, a, a, a)); |
669 // paintColor | 676 // paintColor |
670 drawState->setBlendConstant(colorNoPreMul); | 677 drawState.setBlendConstant(colorNoPreMul); |
671 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); | 678 drawState.setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
672 } else { | 679 } else { |
| 680 if (0xFF == GrColorUnpackA(fPaint.getColor())) { |
| 681 drawState.setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true
); |
| 682 } |
673 // set back to normal in case we took LCD path previously. | 683 // set back to normal in case we took LCD path previously. |
674 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlen
dCoeff()); | 684 drawState.setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlend
Coeff()); |
675 // We're using per-vertex color. | 685 // We're using per-vertex color. |
676 SkASSERT(drawState->hasColorVertexAttribute()); | 686 SkASSERT(drawState.hasColorVertexAttribute()); |
677 } | 687 } |
678 int nGlyphs = fCurrVertex / kVerticesPerGlyph; | 688 int nGlyphs = fCurrVertex / kVerticesPerGlyph; |
679 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); | 689 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
680 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, | 690 fDrawTarget->drawIndexedInstances(&drawState, |
| 691 kTriangles_GrPrimitiveType, |
681 nGlyphs, | 692 nGlyphs, |
682 kVerticesPerGlyph, kIndicesPerGlyph, &
fVertexBounds); | 693 kVerticesPerGlyph, |
| 694 kIndicesPerGlyph, |
| 695 &fVertexBounds); |
683 fDrawTarget->resetVertexSource(); | 696 fDrawTarget->resetVertexSource(); |
684 fVertices = NULL; | 697 fVertices = NULL; |
685 fTotalVertexCount -= fCurrVertex; | 698 fTotalVertexCount -= fCurrVertex; |
686 fCurrVertex = 0; | 699 fCurrVertex = 0; |
687 SkSafeSetNull(fCurrTexture); | 700 SkSafeSetNull(fCurrTexture); |
688 fVertexBounds.setLargestInverted(); | 701 fVertexBounds.setLargestInverted(); |
689 } | 702 } |
690 } | 703 } |
691 | 704 |
692 inline void GrDistanceFieldTextContext::finish() { | 705 inline void GrDistanceFieldTextContext::finish() { |
693 this->flush(); | 706 this->flush(); |
694 fTotalVertexCount = 0; | 707 fTotalVertexCount = 0; |
695 | 708 |
696 GrTextContext::finish(); | 709 GrTextContext::finish(); |
697 } | 710 } |
698 | 711 |
OLD | NEW |