michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrDistanceFieldTextContext.h" michael@0: #include "GrAtlas.h" michael@0: #include "GrDrawTarget.h" michael@0: #include "GrDrawTargetCaps.h" michael@0: #include "GrFontScaler.h" michael@0: #include "SkGlyphCache.h" michael@0: #include "GrIndexBuffer.h" michael@0: #include "GrTextStrike.h" michael@0: #include "GrTextStrike_impl.h" michael@0: #include "SkDraw.h" michael@0: #include "SkGpuDevice.h" michael@0: #include "SkPath.h" michael@0: #include "SkRTConf.h" michael@0: #include "SkStrokeRec.h" michael@0: #include "effects/GrDistanceFieldTextureEffect.h" michael@0: michael@0: static const int kGlyphCoordsAttributeIndex = 1; michael@0: michael@0: static const int kBaseDFFontSize = 32; michael@0: michael@0: SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, michael@0: "Dump the contents of the font cache before every purge."); michael@0: michael@0: #if SK_FORCE_DISTANCEFIELD_FONTS michael@0: static const bool kForceDistanceFieldFonts = true; michael@0: #else michael@0: static const bool kForceDistanceFieldFonts = false; michael@0: #endif michael@0: michael@0: GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context, michael@0: const SkDeviceProperties& properties) michael@0: : GrTextContext(context, properties) { michael@0: fStrike = NULL; michael@0: michael@0: fCurrTexture = NULL; michael@0: fCurrVertex = 0; michael@0: michael@0: fVertices = NULL; michael@0: fMaxVertices = 0; michael@0: } michael@0: michael@0: GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { michael@0: this->flushGlyphs(); michael@0: } michael@0: michael@0: bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) { michael@0: return (kForceDistanceFieldFonts || paint.isDistanceFieldTextTEMP()) && michael@0: !paint.getRasterizer() && !paint.getMaskFilter() && michael@0: paint.getStyle() == SkPaint::kFill_Style && michael@0: fContext->getTextTarget()->caps()->shaderDerivativeSupport() && michael@0: !SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix()); michael@0: } michael@0: michael@0: static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { michael@0: unsigned r = SkColorGetR(c); michael@0: unsigned g = SkColorGetG(c); michael@0: unsigned b = SkColorGetB(c); michael@0: return GrColorPackRGBA(r, g, b, 0xff); michael@0: } michael@0: michael@0: void GrDistanceFieldTextContext::flushGlyphs() { michael@0: if (NULL == fDrawTarget) { michael@0: return; michael@0: } michael@0: michael@0: GrDrawState* drawState = fDrawTarget->drawState(); michael@0: GrDrawState::AutoRestoreEffects are(drawState); michael@0: drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget()); michael@0: michael@0: if (fCurrVertex > 0) { michael@0: // setup our sampler state for our text texture/atlas michael@0: SkASSERT(GrIsALIGN4(fCurrVertex)); michael@0: SkASSERT(fCurrTexture); michael@0: GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode); michael@0: michael@0: // This effect could be stored with one of the cache objects (atlas?) michael@0: SkISize size = fStrike->getAtlasSize(); michael@0: drawState->addCoverageEffect( michael@0: GrDistanceFieldTextureEffect::Create(fCurrTexture, params, size), michael@0: kGlyphCoordsAttributeIndex)->unref(); michael@0: michael@0: if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { michael@0: if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || michael@0: kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || michael@0: fPaint.numColorStages()) { michael@0: GrPrintf("LCD Text will not draw correctly.\n"); michael@0: } michael@0: // We don't use the GrPaint's color in this case because it's been premultiplied by michael@0: // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by michael@0: // the mask texture color. The end result is that we get michael@0: // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor michael@0: int a = SkColorGetA(fSkPaint.getColor()); michael@0: // paintAlpha michael@0: drawState->setColor(SkColorSetARGB(a, a, a, a)); michael@0: // paintColor michael@0: drawState->setBlendConstant(skcolor_to_grcolor_nopremultiply(fSkPaint.getColor())); michael@0: drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); michael@0: } else { michael@0: // set back to normal in case we took LCD path previously. michael@0: drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff()); michael@0: drawState->setColor(fPaint.getColor()); michael@0: } michael@0: michael@0: int nGlyphs = fCurrVertex / 4; michael@0: fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); michael@0: fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, michael@0: nGlyphs, michael@0: 4, 6); michael@0: fDrawTarget->resetVertexSource(); michael@0: fVertices = NULL; michael@0: fMaxVertices = 0; michael@0: fCurrVertex = 0; michael@0: SkSafeSetNull(fCurrTexture); michael@0: } michael@0: } michael@0: michael@0: namespace { michael@0: michael@0: // position + texture coord michael@0: extern const GrVertexAttrib gTextVertexAttribs[] = { michael@0: {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, michael@0: {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} michael@0: }; michael@0: michael@0: }; michael@0: michael@0: void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed, michael@0: GrFixed vx, GrFixed vy, michael@0: GrFontScaler* scaler) { michael@0: if (NULL == fDrawTarget) { michael@0: return; michael@0: } michael@0: if (NULL == fStrike) { michael@0: fStrike = fContext->getFontCache()->getStrike(scaler, true); michael@0: } michael@0: michael@0: GrGlyph* glyph = fStrike->getGlyph(packed, scaler); michael@0: if (NULL == glyph || glyph->fBounds.isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: SkScalar sx = SkFixedToScalar(vx); michael@0: SkScalar sy = SkFixedToScalar(vy); michael@0: /* michael@0: // not valid, need to find a different solution for this michael@0: vx += SkIntToFixed(glyph->fBounds.fLeft); michael@0: vy += SkIntToFixed(glyph->fBounds.fTop); michael@0: michael@0: // keep them as ints until we've done the clip-test michael@0: GrFixed width = glyph->fBounds.width(); michael@0: GrFixed height = glyph->fBounds.height(); michael@0: michael@0: // check if we clipped out michael@0: if (true || NULL == glyph->fPlot) { michael@0: int x = vx >> 16; michael@0: int y = vy >> 16; michael@0: if (fClipRect.quickReject(x, y, x + width, y + height)) { michael@0: // SkCLZ(3); // so we can set a break-point in the debugger michael@0: return; michael@0: } michael@0: } michael@0: */ michael@0: if (NULL == glyph->fPlot) { michael@0: if (fStrike->addGlyphToAtlas(glyph, scaler)) { michael@0: goto HAS_ATLAS; michael@0: } michael@0: michael@0: // try to clear out an unused plot before we flush michael@0: if (fContext->getFontCache()->freeUnusedPlot(fStrike) && michael@0: fStrike->addGlyphToAtlas(glyph, scaler)) { michael@0: goto HAS_ATLAS; michael@0: } michael@0: michael@0: if (c_DumpFontCache) { michael@0: #ifdef SK_DEVELOPER michael@0: fContext->getFontCache()->dump(); michael@0: #endif michael@0: } michael@0: michael@0: // before we purge the cache, we must flush any accumulated draws michael@0: this->flushGlyphs(); michael@0: fContext->flush(); michael@0: michael@0: // we should have an unused plot now michael@0: if (fContext->getFontCache()->freeUnusedPlot(fStrike) && michael@0: fStrike->addGlyphToAtlas(glyph, scaler)) { michael@0: goto HAS_ATLAS; michael@0: } michael@0: michael@0: if (NULL == glyph->fPath) { michael@0: SkPath* path = SkNEW(SkPath); michael@0: if (!scaler->getGlyphPath(glyph->glyphID(), path)) { michael@0: // flag the glyph as being dead? michael@0: delete path; michael@0: return; michael@0: } michael@0: glyph->fPath = path; michael@0: } michael@0: michael@0: GrContext::AutoMatrix am; michael@0: SkMatrix translate; michael@0: translate.setTranslate(sx, sy); michael@0: GrPaint tmpPaint(fPaint); michael@0: am.setPreConcat(fContext, translate, &tmpPaint); michael@0: SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); michael@0: fContext->drawPath(tmpPaint, *glyph->fPath, stroke); michael@0: return; michael@0: } michael@0: michael@0: HAS_ATLAS: michael@0: SkASSERT(glyph->fPlot); michael@0: GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); michael@0: glyph->fPlot->setDrawToken(drawToken); michael@0: michael@0: GrTexture* texture = glyph->fPlot->texture(); michael@0: SkASSERT(texture); michael@0: michael@0: if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { michael@0: this->flushGlyphs(); michael@0: fCurrTexture = texture; michael@0: fCurrTexture->ref(); michael@0: } michael@0: michael@0: if (NULL == fVertices) { michael@0: // If we need to reserve vertices allow the draw target to suggest michael@0: // a number of verts to reserve and whether to perform a flush. michael@0: fMaxVertices = kMinRequestedVerts; michael@0: fDrawTarget->drawState()->setVertexAttribs( michael@0: SK_ARRAY_COUNT(gTextVertexAttribs)); michael@0: bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL); michael@0: if (flush) { michael@0: this->flushGlyphs(); michael@0: fContext->flush(); michael@0: fDrawTarget->drawState()->setVertexAttribs( michael@0: SK_ARRAY_COUNT(gTextVertexAttribs)); michael@0: } michael@0: fMaxVertices = kDefaultRequestedVerts; michael@0: // ignore return, no point in flushing again. michael@0: fDrawTarget->geometryHints(&fMaxVertices, NULL); michael@0: michael@0: int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads(); michael@0: if (fMaxVertices < kMinRequestedVerts) { michael@0: fMaxVertices = kDefaultRequestedVerts; michael@0: } else if (fMaxVertices > maxQuadVertices) { michael@0: // don't exceed the limit of the index buffer michael@0: fMaxVertices = maxQuadVertices; michael@0: } michael@0: bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices, michael@0: 0, michael@0: GrTCast(&fVertices), michael@0: NULL); michael@0: GrAlwaysAssert(success); michael@0: SkASSERT(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize()); michael@0: } michael@0: michael@0: SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft); michael@0: SkScalar dy = SkIntToScalar(glyph->fBounds.fTop); michael@0: SkScalar width = SkIntToScalar(glyph->fBounds.width()); michael@0: SkScalar height = SkIntToScalar(glyph->fBounds.height()); michael@0: michael@0: SkScalar scale = fTextRatio; michael@0: dx *= scale; michael@0: dy *= scale; michael@0: sx += dx; michael@0: sy += dy; michael@0: width *= scale; michael@0: height *= scale; michael@0: michael@0: GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); michael@0: GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); michael@0: GrFixed tw = SkIntToFixed(glyph->fBounds.width()); michael@0: GrFixed th = SkIntToFixed(glyph->fBounds.height()); michael@0: michael@0: static const size_t kVertexSize = 2 * sizeof(SkPoint); michael@0: fVertices[2*fCurrVertex].setRectFan(sx, michael@0: sy, michael@0: sx + width, michael@0: sy + height, michael@0: kVertexSize); michael@0: fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)), michael@0: SkFixedToFloat(texture->normalizeFixedY(ty)), michael@0: SkFixedToFloat(texture->normalizeFixedX(tx + tw)), michael@0: SkFixedToFloat(texture->normalizeFixedY(ty + th)), michael@0: kVertexSize); michael@0: fCurrVertex += 4; michael@0: } michael@0: michael@0: inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) { michael@0: GrTextContext::init(paint, skPaint); michael@0: michael@0: fStrike = NULL; michael@0: michael@0: fCurrTexture = NULL; michael@0: fCurrVertex = 0; michael@0: michael@0: fVertices = NULL; michael@0: fMaxVertices = 0; michael@0: michael@0: fTextRatio = fSkPaint.getTextSize()/kBaseDFFontSize; michael@0: michael@0: fSkPaint.setTextSize(SkIntToScalar(kBaseDFFontSize)); michael@0: fSkPaint.setLCDRenderText(false); michael@0: fSkPaint.setAutohinted(false); michael@0: fSkPaint.setSubpixelText(true); michael@0: } michael@0: michael@0: inline void GrDistanceFieldTextContext::finish() { michael@0: flushGlyphs(); michael@0: michael@0: GrTextContext::finish(); michael@0: } michael@0: michael@0: void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint, michael@0: const char text[], size_t byteLength, michael@0: SkScalar x, SkScalar y) { michael@0: SkASSERT(byteLength == 0 || text != NULL); michael@0: michael@0: // nothing to draw or can't draw michael@0: if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/ michael@0: || fSkPaint.getRasterizer()) { michael@0: return; michael@0: } michael@0: michael@0: this->init(paint, skPaint); michael@0: michael@0: SkScalar sizeRatio = fTextRatio; michael@0: michael@0: SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); michael@0: michael@0: SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); michael@0: SkGlyphCache* cache = autoCache.getCache(); michael@0: GrFontScaler* fontScaler = GetGrFontScaler(cache); michael@0: michael@0: // need to measure first michael@0: // TODO - generate positions and pre-load cache as well? michael@0: const char* stop = text + byteLength; michael@0: if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { michael@0: SkFixed stopX = 0; michael@0: SkFixed stopY = 0; michael@0: michael@0: const char* textPtr = text; michael@0: while (textPtr < stop) { michael@0: // don't need x, y here, since all subpixel variants will have the michael@0: // same advance michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0); michael@0: michael@0: stopX += glyph.fAdvanceX; michael@0: stopY += glyph.fAdvanceY; michael@0: } michael@0: SkASSERT(textPtr == stop); michael@0: michael@0: SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio; michael@0: SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio; michael@0: michael@0: if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { michael@0: alignX = SkScalarHalf(alignX); michael@0: alignY = SkScalarHalf(alignY); michael@0: } michael@0: michael@0: x -= alignX; michael@0: y -= alignY; michael@0: } michael@0: michael@0: SkFixed fx = SkScalarToFixed(x) + SK_FixedHalf; michael@0: SkFixed fy = SkScalarToFixed(y) + SK_FixedHalf; michael@0: SkFixed fixedScale = SkScalarToFixed(sizeRatio); michael@0: while (text < stop) { michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, fx, fy); michael@0: michael@0: if (glyph.fWidth) { michael@0: this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), michael@0: glyph.getSubXFixed(), michael@0: glyph.getSubYFixed()), michael@0: SkFixedFloorToFixed(fx), michael@0: SkFixedFloorToFixed(fy), michael@0: fontScaler); michael@0: } michael@0: michael@0: fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale); michael@0: fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale); michael@0: } michael@0: michael@0: this->finish(); michael@0: } michael@0: michael@0: void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint, michael@0: const char text[], size_t byteLength, michael@0: const SkScalar pos[], SkScalar constY, michael@0: int scalarsPerPosition) { michael@0: michael@0: SkASSERT(byteLength == 0 || text != NULL); michael@0: SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); michael@0: michael@0: // nothing to draw michael@0: if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) { michael@0: return; michael@0: } michael@0: michael@0: this->init(paint, skPaint); michael@0: michael@0: SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); michael@0: michael@0: SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, NULL); michael@0: SkGlyphCache* cache = autoCache.getCache(); michael@0: GrFontScaler* fontScaler = GetGrFontScaler(cache); michael@0: michael@0: const char* stop = text + byteLength; michael@0: michael@0: if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { michael@0: while (text < stop) { michael@0: // the last 2 parameters are ignored michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); michael@0: michael@0: if (glyph.fWidth) { michael@0: SkScalar x = pos[0]; michael@0: SkScalar y = scalarsPerPosition == 1 ? constY : pos[1]; michael@0: michael@0: this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), michael@0: glyph.getSubXFixed(), michael@0: glyph.getSubYFixed()), michael@0: SkScalarToFixed(x) + SK_FixedHalf, //d1g.fHalfSampleX, michael@0: SkScalarToFixed(y) + SK_FixedHalf, //d1g.fHalfSampleY, michael@0: fontScaler); michael@0: } michael@0: pos += scalarsPerPosition; michael@0: } michael@0: } else { michael@0: int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0; michael@0: while (text < stop) { michael@0: // the last 2 parameters are ignored michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); michael@0: michael@0: if (glyph.fWidth) { michael@0: SkScalar x = pos[0]; michael@0: SkScalar y = scalarsPerPosition == 1 ? constY : pos[1]; michael@0: michael@0: this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), michael@0: glyph.getSubXFixed(), michael@0: glyph.getSubYFixed()), michael@0: SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift) michael@0: + SK_FixedHalf, //d1g.fHalfSampleX, michael@0: SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift) michael@0: + SK_FixedHalf, //d1g.fHalfSampleY, michael@0: fontScaler); michael@0: } michael@0: pos += scalarsPerPosition; michael@0: } michael@0: } michael@0: michael@0: this->finish(); michael@0: }