| 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 "GrStrokeInfo.h" | 13 #include "GrStrokeInfo.h" |
| 14 #include "GrTexturePriv.h" | 14 #include "GrTexturePriv.h" |
| 15 #include "GrTextStrike.h" | 15 #include "GrTextStrike.h" |
| 16 #include "GrTextStrike_impl.h" | 16 #include "GrTextStrike_impl.h" |
| 17 #include "effects/GrCustomCoordsTextureEffect.h" | 17 #include "effects/GrCustomCoordsTextureEffect.h" |
| 18 #include "effects/GrSimpleTextureEffect.h" |
| 18 | 19 |
| 19 #include "SkAutoKern.h" | 20 #include "SkAutoKern.h" |
| 20 #include "SkColorPriv.h" | 21 #include "SkColorPriv.h" |
| 21 #include "SkDraw.h" | 22 #include "SkDraw.h" |
| 22 #include "SkDrawProcs.h" | 23 #include "SkDrawProcs.h" |
| 23 #include "SkGlyphCache.h" | 24 #include "SkGlyphCache.h" |
| 24 #include "SkGpuDevice.h" | 25 #include "SkGpuDevice.h" |
| 25 #include "SkGr.h" | 26 #include "SkGr.h" |
| 26 #include "SkPath.h" | 27 #include "SkPath.h" |
| 27 #include "SkRTConf.h" | 28 #include "SkRTConf.h" |
| 28 #include "SkStrokeRec.h" | 29 #include "SkStrokeRec.h" |
| 29 #include "SkTextMapStateProc.h" | 30 #include "SkTextMapStateProc.h" |
| 30 | 31 |
| 31 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, | 32 SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, |
| 32 "Dump the contents of the font cache before every purge."); | 33 "Dump the contents of the font cache before every purge."); |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 // position + texture coord | 36 // position + texture coord |
| 36 extern const GrVertexAttrib gTextVertexAttribs[] = { | 37 extern const GrVertexAttrib gLCDVertexAttribs[] = { |
| 37 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding
}, | 38 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding
}, |
| 38 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttr
ibBinding} | 39 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttr
ibBinding} |
| 39 }; | 40 }; |
| 40 | 41 |
| 41 static const size_t kTextVASize = 2 * sizeof(SkPoint); | 42 static const size_t kLCDTextVASize = 2 * sizeof(SkPoint); |
| 43 |
| 44 // position + local coord |
| 45 extern const GrVertexAttrib gColorVertexAttribs[] = { |
| 46 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding
}, |
| 47 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBindi
ng} |
| 48 }; |
| 49 |
| 50 static const size_t kColorTextVASize = 2 * sizeof(SkPoint); |
| 42 | 51 |
| 43 // position + color + texture coord | 52 // position + color + texture coord |
| 44 extern const GrVertexAttrib gTextVertexWithColorAttribs[] = { | 53 extern const GrVertexAttrib gGrayVertexAttribs[] = { |
| 45 {kVec2f_GrVertexAttribType, 0, kPosition_Gr
VertexAttribBinding}, | 54 {kVec2f_GrVertexAttribType, 0, kPosition_Gr
VertexAttribBinding}, |
| 46 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVer
texAttribBinding}, | 55 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVer
texAttribBinding}, |
| 47 {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kGeometryPro
cessor_GrVertexAttribBinding} | 56 {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kGeometryPro
cessor_GrVertexAttribBinding} |
| 48 }; | 57 }; |
| 49 | 58 |
| 50 static const size_t kTextVAColorSize = 2 * sizeof(SkPoint) + sizeof(GrColor); | 59 static const size_t kGrayTextVASize = 2 * sizeof(SkPoint) + sizeof(GrColor); |
| 51 | 60 |
| 52 static const int kVerticesPerGlyph = 4; | 61 static const int kVerticesPerGlyph = 4; |
| 53 static const int kIndicesPerGlyph = 6; | 62 static const int kIndicesPerGlyph = 6; |
| 54 }; | 63 }; |
| 55 | 64 |
| 56 GrBitmapTextContext::GrBitmapTextContext(GrContext* context, | 65 GrBitmapTextContext::GrBitmapTextContext(GrContext* context, |
| 57 const SkDeviceProperties& properties) | 66 const SkDeviceProperties& properties) |
| 58 : GrTextContext(context, properties) { | 67 : GrTextContext(context, properties) { |
| 59 fStrike = NULL; | 68 fStrike = NULL; |
| 60 | 69 |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 fontScaler); | 347 fontScaler); |
| 339 } | 348 } |
| 340 pos += scalarsPerPosition; | 349 pos += scalarsPerPosition; |
| 341 } | 350 } |
| 342 } | 351 } |
| 343 } | 352 } |
| 344 | 353 |
| 345 this->finish(); | 354 this->finish(); |
| 346 } | 355 } |
| 347 | 356 |
| 348 static void* alloc_vertices(GrDrawTarget* drawTarget, int numVertices, bool useC
olorVerts) { | 357 static void* alloc_vertices(GrDrawTarget* drawTarget, int numVertices, GrMaskFor
mat maskFormat) { |
| 349 if (numVertices <= 0) { | 358 if (numVertices <= 0) { |
| 350 return NULL; | 359 return NULL; |
| 351 } | 360 } |
| 352 | 361 |
| 353 // set up attributes | 362 // set up attributes |
| 354 if (useColorVerts) { | 363 if (kA8_GrMaskFormat == maskFormat) { |
| 355 drawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>( | 364 drawTarget->drawState()->setVertexAttribs<gGrayVertexAttribs>( |
| 356 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
kTextVAColorSize); | 365 SK_ARRAY_COUNT(gGrayVertexAttribs), kGrayTex
tVASize); |
| 366 } else if (kARGB_GrMaskFormat == maskFormat) { |
| 367 drawTarget->drawState()->setVertexAttribs<gColorVertexAttribs>( |
| 368 SK_ARRAY_COUNT(gColorVertexAttribs), kColorT
extVASize); |
| 357 } else { | 369 } else { |
| 358 drawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( | 370 drawTarget->drawState()->setVertexAttribs<gLCDVertexAttribs>( |
| 359 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVAS
ize); | 371 SK_ARRAY_COUNT(gLCDVertexAttribs), kLCDTextV
ASize); |
| 360 } | 372 } |
| 361 void* vertices = NULL; | 373 void* vertices = NULL; |
| 362 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, | 374 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, |
| 363 0, | 375 0, |
| 364 &vertices, | 376 &vertices, |
| 365 NULL); | 377 NULL); |
| 366 GrAlwaysAssert(success); | 378 GrAlwaysAssert(success); |
| 367 return vertices; | 379 return vertices; |
| 368 } | 380 } |
| 369 | 381 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 GrTexture* texture = glyph->fPlot->texture(); | 481 GrTexture* texture = glyph->fPlot->texture(); |
| 470 SkASSERT(texture); | 482 SkASSERT(texture); |
| 471 | 483 |
| 472 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fAllocVerte
xCount) { | 484 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fAllocVerte
xCount) { |
| 473 this->flush(); | 485 this->flush(); |
| 474 fCurrTexture = texture; | 486 fCurrTexture = texture; |
| 475 fCurrTexture->ref(); | 487 fCurrTexture->ref(); |
| 476 fCurrMaskFormat = glyph->fMaskFormat; | 488 fCurrMaskFormat = glyph->fMaskFormat; |
| 477 } | 489 } |
| 478 | 490 |
| 479 bool useColorVerts = kA8_GrMaskFormat == fCurrMaskFormat; | |
| 480 | |
| 481 if (NULL == fVertices) { | 491 if (NULL == fVertices) { |
| 482 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()
->maxQuads(); | 492 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()
->maxQuads(); |
| 483 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices); | 493 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices); |
| 484 fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, useColorVerts
); | 494 fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, fCurrMaskForm
at); |
| 485 } | 495 } |
| 486 | 496 |
| 487 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); | 497 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); |
| 488 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); | 498 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); |
| 489 | 499 |
| 490 SkRect r; | 500 SkRect r; |
| 491 r.fLeft = SkFixedToFloat(vx); | 501 r.fLeft = SkFixedToFloat(vx); |
| 492 r.fTop = SkFixedToFloat(vy); | 502 r.fTop = SkFixedToFloat(vy); |
| 493 r.fRight = SkFixedToFloat(vx + width); | 503 r.fRight = SkFixedToFloat(vx + width); |
| 494 r.fBottom = SkFixedToFloat(vy + height); | 504 r.fBottom = SkFixedToFloat(vy + height); |
| 495 | 505 |
| 496 fVertexBounds.joinNonEmptyArg(r); | 506 fVertexBounds.joinNonEmptyArg(r); |
| 497 | 507 |
| 498 size_t vertSize = useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) : | 508 size_t vertSize; |
| 499 (2 * sizeof(SkPoint)); | 509 switch (fCurrMaskFormat) { |
| 510 case kA8_GrMaskFormat: |
| 511 vertSize = kGrayTextVASize; |
| 512 break; |
| 513 case kARGB_GrMaskFormat: |
| 514 vertSize = kColorTextVASize; |
| 515 default: |
| 516 vertSize = kLCDTextVASize; |
| 517 } |
| 500 | 518 |
| 501 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride()); | 519 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride()); |
| 502 | 520 |
| 503 SkPoint* positions = reinterpret_cast<SkPoint*>( | 521 SkPoint* positions = reinterpret_cast<SkPoint*>( |
| 504 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); | 522 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); |
| 505 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); | 523 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); |
| 506 | 524 |
| 507 // The texture coords are last in both the with and without color vertex lay
outs. | 525 // The texture coords are last in both the with and without color vertex lay
outs. |
| 508 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( | 526 SkPoint* textureCoords = reinterpret_cast<SkPoint*>( |
| 509 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint))
; | 527 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint))
; |
| 510 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx)), | 528 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx)), |
| 511 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty)), | 529 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty)), |
| 512 SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx + width)), | 530 SkFixedToFloat(texture->texturePriv().normalizeFix
edX(tx + width)), |
| 513 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty + height)), | 531 SkFixedToFloat(texture->texturePriv().normalizeFix
edY(ty + height)), |
| 514 vertSize); | 532 vertSize); |
| 515 if (useColorVerts) { | 533 if (kA8_GrMaskFormat == fCurrMaskFormat) { |
| 516 if (0xFF == GrColorUnpackA(fPaint.getColor())) { | 534 if (0xFF == GrColorUnpackA(fPaint.getColor())) { |
| 517 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaqu
e_Hint, true); | 535 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaqu
e_Hint, true); |
| 518 } | 536 } |
| 519 // color comes after position. | 537 // color comes after position. |
| 520 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); | 538 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); |
| 521 for (int i = 0; i < 4; ++i) { | 539 for (int i = 0; i < 4; ++i) { |
| 522 *colors = fPaint.getColor(); | 540 *colors = fPaint.getColor(); |
| 523 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors
) + vertSize); | 541 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors
) + vertSize); |
| 524 } | 542 } |
| 525 } | 543 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 541 GrDrawState* drawState = fDrawTarget->drawState(); | 559 GrDrawState* drawState = fDrawTarget->drawState(); |
| 542 GrDrawState::AutoRestoreEffects are(drawState); | 560 GrDrawState::AutoRestoreEffects are(drawState); |
| 543 drawState->setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); | 561 drawState->setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); |
| 544 | 562 |
| 545 if (fCurrVertex > 0) { | 563 if (fCurrVertex > 0) { |
| 546 // setup our sampler state for our text texture/atlas | 564 // setup our sampler state for our text texture/atlas |
| 547 SkASSERT(SkIsAlign4(fCurrVertex)); | 565 SkASSERT(SkIsAlign4(fCurrVertex)); |
| 548 SkASSERT(fCurrTexture); | 566 SkASSERT(fCurrTexture); |
| 549 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNon
e_FilterMode); | 567 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNon
e_FilterMode); |
| 550 | 568 |
| 551 uint32_t textureUniqueID = fCurrTexture->getUniqueID(); | 569 // This effect could be stored with one of the cache objects (atlas?) |
| 570 if (kARGB_GrMaskFormat == fCurrMaskFormat) { |
| 571 GrFragmentProcessor* fragProcessor = GrSimpleTextureEffect::Create(f
CurrTexture, |
| 572 S
kMatrix::I(), |
| 573 p
arams); |
| 574 drawState->addColorProcessor(fragProcessor)->unref(); |
| 575 } else { |
| 576 uint32_t textureUniqueID = fCurrTexture->getUniqueID(); |
| 577 if (textureUniqueID != fEffectTextureUniqueID) { |
| 578 fCachedGeometryProcessor.reset(GrCustomCoordsTextureEffect::Crea
te(fCurrTexture, |
| 579
params)); |
| 580 fEffectTextureUniqueID = textureUniqueID; |
| 581 } |
| 552 | 582 |
| 553 if (textureUniqueID != fEffectTextureUniqueID) { | 583 drawState->setGeometryProcessor(fCachedGeometryProcessor.get()); |
| 554 fCachedGeometryProcessor.reset(GrCustomCoordsTextureEffect::Create(f
CurrTexture, | |
| 555 p
arams)); | |
| 556 fEffectTextureUniqueID = textureUniqueID; | |
| 557 } | 584 } |
| 558 | 585 |
| 559 // This effect could be stored with one of the cache objects (atlas?) | |
| 560 drawState->setGeometryProcessor(fCachedGeometryProcessor.get()); | |
| 561 SkASSERT(fStrike); | 586 SkASSERT(fStrike); |
| 562 switch (fCurrMaskFormat) { | 587 switch (fCurrMaskFormat) { |
| 563 // Color bitmap text | 588 // Color bitmap text |
| 564 case kARGB_GrMaskFormat: | 589 case kARGB_GrMaskFormat: |
| 565 SkASSERT(!drawState->hasColorVertexAttribute()); | 590 SkASSERT(!drawState->hasColorVertexAttribute()); |
| 566 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDst
BlendCoeff()); | 591 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDst
BlendCoeff()); |
| 567 drawState->setAlpha(fSkPaint.getAlpha()); | 592 drawState->setAlpha(fSkPaint.getAlpha()); |
| 568 break; | 593 break; |
| 569 // LCD text | 594 // LCD text |
| 570 case kA888_GrMaskFormat: | 595 case kA888_GrMaskFormat: |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 } | 639 } |
| 615 } | 640 } |
| 616 | 641 |
| 617 inline void GrBitmapTextContext::finish() { | 642 inline void GrBitmapTextContext::finish() { |
| 618 this->flush(); | 643 this->flush(); |
| 619 fTotalVertexCount = 0; | 644 fTotalVertexCount = 0; |
| 620 | 645 |
| 621 GrTextContext::finish(); | 646 GrTextContext::finish(); |
| 622 } | 647 } |
| 623 | 648 |
| OLD | NEW |