diff options
author | Herb Derby <herb@google.com> | 2018-05-09 16:36:11 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-06-05 20:11:39 +0000 |
commit | 4225b3220ef4bf50f0d9403f812ea94d50c4ee59 (patch) | |
tree | a03d6ff5708e6c41fdc4b1edb8f5200d08d7dc63 /src/gpu/text/GrTextContext.cpp | |
parent | f105dc71e4200db5a57a1e28b8bca2b3689debdb (diff) |
Have draw(Text|PosText|PosTextH) use a single entry on the device
Handle the positioning of drawText at the canvas layer. Simplify
the code by removing similar implementations.
Change-Id: I8b711783435072f560e29fca1dd934fa2e345ed2
Reviewed-on: https://skia-review.googlesource.com/127131
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Herb Derby <herb@google.com>
Diffstat (limited to 'src/gpu/text/GrTextContext.cpp')
-rw-r--r-- | src/gpu/text/GrTextContext.cpp | 227 |
1 files changed, 26 insertions, 201 deletions
diff --git a/src/gpu/text/GrTextContext.cpp b/src/gpu/text/GrTextContext.cpp index f662fa9c12..72926fe280 100644 --- a/src/gpu/text/GrTextContext.cpp +++ b/src/gpu/text/GrTextContext.cpp @@ -17,6 +17,7 @@ #include "SkFindAndPlaceGlyph.h" #include "SkGr.h" #include "SkGraphics.h" +#include "SkGlyphRun.h" #include "SkMakeUnique.h" #include "SkMaskFilterBase.h" #include "SkPaintPriv.h" @@ -209,11 +210,17 @@ void GrTextContext::regenerateTextBlob(GrTextBlob* cacheBlob, shaderCaps.supportsDistanceFieldText(), fOptions)) { switch (it.positioning()) { case SkTextBlob::kDefault_Positioning: { - this->drawDFText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags, - viewMatrix, (const char*)it.glyphs(), textLen, x + offset.x(), - y + offset.y()); + auto origin = SkPoint::Make(x + offset.x(), y + offset.y()); + auto glyphRun = + SkGlyphRun::MakeFromDrawText(runPaint.skPaint(), + (const char*)it.glyphs(), textLen, origin); + + this->drawDFPosText(cacheBlob, run, glyphCache, props, runPaint, + scalerContextFlags, viewMatrix, (const char*)it.glyphs(), + textLen, glyphRun.getPositions(), 2, SkPoint::Make(0,0)); break; } + case SkTextBlob::kHorizontal_Positioning: { SkPoint dfOffset = SkPoint::Make(x, y + offset.y()); this->drawDFPosText(cacheBlob, run, glyphCache, props, runPaint, @@ -231,11 +238,17 @@ void GrTextContext::regenerateTextBlob(GrTextBlob* cacheBlob, } } else { switch (it.positioning()) { - case SkTextBlob::kDefault_Positioning: - DrawBmpText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags, - viewMatrix, (const char*)it.glyphs(), textLen, x + offset.x(), - y + offset.y()); + case SkTextBlob::kDefault_Positioning: { + auto origin = SkPoint::Make(x + offset.x(), y + offset.y()); + auto glyphRun = + SkGlyphRun::MakeFromDrawText( + runPaint.skPaint(), (const char*) it.glyphs(), textLen, origin); + + this->DrawBmpPosText(cacheBlob, run, glyphCache, props, runPaint, + scalerContextFlags, viewMatrix, (const char*) it.glyphs(), + textLen, glyphRun.getPositions(), 2, SkPoint::Make(0, 0)); break; + } case SkTextBlob::kHorizontal_Positioning: DrawBmpPosText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags, viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 1, @@ -252,35 +265,6 @@ void GrTextContext::regenerateTextBlob(GrTextBlob* cacheBlob, } inline sk_sp<GrTextBlob> -GrTextContext::makeDrawTextBlob(GrTextBlobCache* blobCache, - GrGlyphCache* glyphCache, - const GrShaderCaps& shaderCaps, - const GrTextUtils::Paint& paint, - SkScalerContextFlags scalerContextFlags, - const SkMatrix& viewMatrix, - const SkSurfaceProps& props, - const char text[], size_t byteLength, - SkScalar x, SkScalar y) const { - int glyphCount = paint.skPaint().countText(text, byteLength); - if (!glyphCount) { - return nullptr; - } - sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1); - blob->initThrowawayBlob(viewMatrix, x, y); - blob->setRunPaintFlags(0, paint.skPaint().getFlags()); - - if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(), - fOptions)) { - this->drawDFText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix, - text, byteLength, x, y); - } else { - DrawBmpText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix, text, - byteLength, x, y); - } - return blob; -} - -inline sk_sp<GrTextBlob> GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache, GrGlyphCache* glyphCache, const GrShaderCaps& shaderCaps, @@ -311,29 +295,6 @@ GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache, return blob; } -void GrTextContext::drawText(GrContext* context, GrTextUtils::Target* target, - const GrClip& clip, const SkPaint& skPaint, - const SkMatrix& viewMatrix, const SkSurfaceProps& props, - const char text[], size_t byteLength, SkScalar x, SkScalar y, - const SkIRect& regionClipBounds) { - if (context->contextPriv().abandoned()) { - return; - } - - auto glyphCache = context->contextPriv().getGlyphCache(); - auto textBlobCache = context->contextPriv().getTextBlobCache(); - - GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo()); - sk_sp<GrTextBlob> blob(this->makeDrawTextBlob( - textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint, - ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text, - byteLength, x, y)); - if (blob) { - blob->flush(target, props, fDistanceAdjustTable.get(), paint, - clip, viewMatrix, regionClipBounds, x, y); - } -} - void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target, const GrClip& clip, const SkPaint& skPaint, const SkMatrix& viewMatrix, const SkSurfaceProps& props, @@ -358,41 +319,6 @@ void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target, } } -void GrTextContext::DrawBmpText(GrTextBlob* blob, int runIndex, - GrGlyphCache* glyphCache, const SkSurfaceProps& props, - const GrTextUtils::Paint& paint, - SkScalerContextFlags scalerContextFlags, - const SkMatrix& viewMatrix, const char text[], - size_t byteLength, SkScalar x, SkScalar y) { - SkASSERT(byteLength == 0 || text != nullptr); - - // nothing to draw - if (text == nullptr || byteLength == 0) { - return; - } - - // Ensure the blob is set for bitmaptext - blob->setHasBitmap(); - - if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) { - DrawBmpTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags, viewMatrix, - text, byteLength, x, y); - return; - } - - sk_sp<GrTextStrike> currStrike; - auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix); - SkFindAndPlaceGlyph::ProcessText(paint.skPaint().getTextEncoding(), text, byteLength, {x, y}, - viewMatrix, paint.skPaint().getTextAlign(), cache.get(), - [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { - position += rounding; - BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, - glyph, SkScalarFloorToScalar(position.fX), - SkScalarFloorToScalar(position.fY), - paint.filteredPremulColor(), cache.get(), - SK_Scalar1, false); - }); -} void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props, @@ -432,43 +358,6 @@ void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex, }); } -void GrTextContext::DrawBmpTextAsPaths(GrTextBlob* blob, int runIndex, - GrGlyphCache* glyphCache, - const SkSurfaceProps& props, - const GrTextUtils::Paint& origPaint, - SkScalerContextFlags scalerContextFlags, - const SkMatrix& viewMatrix, const char text[], - size_t byteLength, SkScalar x, SkScalar y) { - // nothing to draw - if (text == nullptr || byteLength == 0) { - return; - } - - // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache. - SkPaint pathPaint(origPaint); - pathPaint.setStyle(SkPaint::kFill_Style); - pathPaint.setPathEffect(nullptr); - - GrTextUtils::PathTextIter iter(text, byteLength, pathPaint, true); - FallbackTextHelper fallbackTextHelper(viewMatrix, pathPaint, glyphCache, iter.getPathScale()); - - const SkGlyph* iterGlyph; - const SkPath* iterPath; - SkScalar xpos = 0; - const char* lastText = text; - while (iter.next(&iterGlyph, &iterPath, &xpos)) { - if (iterGlyph) { - SkPoint pos = SkPoint::Make(xpos + x, y); - fallbackTextHelper.appendText(*iterGlyph, iter.getText() - lastText, lastText, pos); - } else if (iterPath) { - blob->appendPathGlyph(runIndex, *iterPath, xpos + x, y, iter.getPathScale(), false); - } - lastText = iter.getText(); - } - - fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags); -} - void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props, @@ -678,74 +567,6 @@ void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob, *flags = SkScalerContextFlags::kNone; } -void GrTextContext::drawDFText(GrTextBlob* blob, int runIndex, - GrGlyphCache* glyphCache, const SkSurfaceProps& props, - const GrTextUtils::Paint& paint, - SkScalerContextFlags scalerContextFlags, - const SkMatrix& viewMatrix, const char text[], - size_t byteLength, SkScalar x, SkScalar y) const { - SkASSERT(byteLength == 0 || text != nullptr); - - // nothing to draw - if (text == nullptr || byteLength == 0) { - return; - } - - const SkPaint& skPaint = paint.skPaint(); - SkPaint::GlyphCacheProc glyphCacheProc = - SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true); - - SkTArray<SkScalar> positions; - - const char* textPtr = text; - SkScalar stopX = 0; - SkScalar stopY = 0; - - SkAutoDescriptor desc; - SkScalerContextEffects effects; - // We apply the fake-gamma by altering the distance in the shader, so we ignore the - // passed-in scaler context flags. (It's only used when we fall-back to bitmap text). - SkScalerContext::CreateDescriptorAndEffectsUsingPaint( - skPaint, &props, SkScalerContextFlags::kNone, nullptr, &desc, &effects); - auto typeface = SkPaintPriv::GetTypefaceOrDefault(skPaint); - - { - auto origPaintCache = - SkStrikeCache::FindOrCreateStrikeExclusive(*desc.getDesc(), effects, *typeface); - - const char* stop = text + byteLength; - while (textPtr < stop) { - // don't need x, y here, since all subpixel variants will have the - // same advance - const SkGlyph& glyph = glyphCacheProc(origPaintCache.get(), &textPtr); - - positions.push_back(stopX); - positions.push_back(stopY); - - stopX += SkFloatToScalar(glyph.fAdvanceX); - stopY += SkFloatToScalar(glyph.fAdvanceY); - } - SkASSERT(textPtr == stop); - } - - // now adjust starting point depending on alignment - SkScalar alignX = stopX; - SkScalar alignY = stopY; - if (skPaint.getTextAlign() == SkPaint::kCenter_Align) { - alignX = SkScalarHalf(alignX); - alignY = SkScalarHalf(alignY); - } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) { - alignX = 0; - alignY = 0; - } - x -= alignX; - y -= alignY; - SkPoint offset = SkPoint::Make(x, y); - - this->drawDFPosText(blob, runIndex, glyphCache, props, paint, scalerContextFlags, - viewMatrix, text, byteLength, positions.begin(), 2, offset); -} - void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props, const GrTextUtils::Paint& paint, @@ -936,11 +757,15 @@ std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to // test the text op with this unit test, that is okay. - sk_sp<GrTextBlob> blob(textContext->makeDrawTextBlob( + + auto origin = SkPoint::Make(x, y); + auto glyphRun = SkGlyphRun::MakeFromDrawText(skPaint, text, textLen, origin); + + sk_sp<GrTextBlob> blob(textContext->makeDrawPosTextBlob( context->contextPriv().getTextBlobCache(), glyphCache, *context->contextPriv().caps()->shaderCaps(), utilsPaint, GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps, text, - static_cast<size_t>(textLen), SkIntToScalar(x), SkIntToScalar(y))); + static_cast<size_t>(textLen), glyphRun.getPositions(), 2, origin)); return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps, textContext->dfAdjustTable(), rtc->textTarget()); |