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 "GrBitmapTextContext.h" michael@0: #include "GrAtlas.h" michael@0: #include "GrDrawTarget.h" michael@0: #include "GrFontScaler.h" michael@0: #include "GrIndexBuffer.h" michael@0: #include "GrTextStrike.h" michael@0: #include "GrTextStrike_impl.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkPath.h" michael@0: #include "SkRTConf.h" michael@0: #include "SkStrokeRec.h" michael@0: #include "effects/GrCustomCoordsTextureEffect.h" michael@0: michael@0: #include "SkAutoKern.h" michael@0: #include "SkDraw.h" michael@0: #include "SkGlyphCache.h" michael@0: #include "SkGpuDevice.h" michael@0: #include "SkGr.h" michael@0: michael@0: static const int kGlyphCoordsAttributeIndex = 1; 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: GrBitmapTextContext::GrBitmapTextContext(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: fVertexBounds.setLargestInverted(); michael@0: } michael@0: michael@0: GrBitmapTextContext::~GrBitmapTextContext() { michael@0: this->flushGlyphs(); michael@0: } michael@0: michael@0: bool GrBitmapTextContext::canDraw(const SkPaint& paint) { michael@0: return !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 GrBitmapTextContext::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, SkMatrix::I(), 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::kNone_FilterMode); michael@0: michael@0: // This effect could be stored with one of the cache objects (atlas?) michael@0: drawState->addCoverageEffect( michael@0: GrCustomCoordsTextureEffect::Create(fCurrTexture, params), michael@0: kGlyphCoordsAttributeIndex)->unref(); michael@0: michael@0: if (NULL != fStrike && kARGB_GrMaskFormat == fStrike->getMaskFormat()) { michael@0: drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff()); michael@0: drawState->setColor(0xffffffff); michael@0: } else 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, &fVertexBounds); michael@0: michael@0: fDrawTarget->resetVertexSource(); michael@0: fVertices = NULL; michael@0: fMaxVertices = 0; michael@0: fCurrVertex = 0; michael@0: fVertexBounds.setLargestInverted(); michael@0: SkSafeSetNull(fCurrTexture); michael@0: } michael@0: } michael@0: michael@0: inline void GrBitmapTextContext::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: michael@0: inline void GrBitmapTextContext::finish() { michael@0: flushGlyphs(); michael@0: michael@0: GrTextContext::finish(); michael@0: } michael@0: michael@0: void GrBitmapTextContext::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 michael@0: if (text == NULL || byteLength == 0 /*|| 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, &fContext->getMatrix()); michael@0: SkGlyphCache* cache = autoCache.getCache(); michael@0: GrFontScaler* fontScaler = GetGrFontScaler(cache); michael@0: michael@0: // transform our starting point michael@0: { michael@0: SkPoint loc; michael@0: fContext->getMatrix().mapXY(x, y, &loc); michael@0: x = loc.fX; michael@0: y = loc.fY; michael@0: } michael@0: michael@0: // need to measure first michael@0: if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { michael@0: SkVector stop; michael@0: michael@0: MeasureText(cache, glyphCacheProc, text, byteLength, &stop); michael@0: michael@0: SkScalar stopX = stop.fX; michael@0: SkScalar stopY = stop.fY; michael@0: michael@0: if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { michael@0: stopX = SkScalarHalf(stopX); michael@0: stopY = SkScalarHalf(stopY); michael@0: } michael@0: x -= stopX; michael@0: y -= stopY; michael@0: } michael@0: michael@0: const char* stop = text + byteLength; michael@0: michael@0: SkAutoKern autokern; michael@0: michael@0: SkFixed fxMask = ~0; michael@0: SkFixed fyMask = ~0; michael@0: SkFixed halfSampleX, halfSampleY; michael@0: if (cache->isSubpixel()) { michael@0: halfSampleX = halfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits); michael@0: SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(fContext->getMatrix()); michael@0: if (kX_SkAxisAlignment == baseline) { michael@0: fyMask = 0; michael@0: halfSampleY = SK_FixedHalf; michael@0: } else if (kY_SkAxisAlignment == baseline) { michael@0: fxMask = 0; michael@0: halfSampleX = SK_FixedHalf; michael@0: } michael@0: } else { michael@0: halfSampleX = halfSampleY = SK_FixedHalf; michael@0: } michael@0: michael@0: SkFixed fx = SkScalarToFixed(x) + halfSampleX; michael@0: SkFixed fy = SkScalarToFixed(y) + halfSampleY; michael@0: michael@0: GrContext::AutoMatrix autoMatrix; michael@0: autoMatrix.setIdentity(fContext, &fPaint); michael@0: michael@0: while (text < stop) { michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask); michael@0: michael@0: fx += autokern.adjust(glyph); 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 += glyph.fAdvanceX; michael@0: fy += glyph.fAdvanceY; michael@0: } michael@0: michael@0: this->finish(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Copied from SkDraw michael@0: michael@0: // last parameter is interpreted as SkFixed [x, y] michael@0: // return the fixed position, which may be rounded or not by the caller michael@0: // e.g. subpixel doesn't round michael@0: typedef void (*AlignProc)(const SkPoint&, const SkGlyph&, SkIPoint*); michael@0: michael@0: static void leftAlignProc(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* dst) { michael@0: dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY)); michael@0: } michael@0: michael@0: static void centerAlignProc(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* dst) { michael@0: dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1), michael@0: SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1)); michael@0: } michael@0: michael@0: static void rightAlignProc(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* dst) { michael@0: dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX, michael@0: SkScalarToFixed(loc.fY) - glyph.fAdvanceY); michael@0: } michael@0: michael@0: static AlignProc pick_align_proc(SkPaint::Align align) { michael@0: static const AlignProc gProcs[] = { michael@0: leftAlignProc, centerAlignProc, rightAlignProc michael@0: }; michael@0: michael@0: SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs)); michael@0: michael@0: return gProcs[align]; michael@0: } michael@0: michael@0: class BitmapTextMapState { michael@0: public: michael@0: mutable SkPoint fLoc; michael@0: michael@0: BitmapTextMapState(const SkMatrix& matrix, SkScalar y) michael@0: : fMatrix(matrix), fProc(matrix.getMapXYProc()), fY(y) {} michael@0: michael@0: typedef void (*Proc)(const BitmapTextMapState&, const SkScalar pos[]); michael@0: michael@0: Proc pickProc(int scalarsPerPosition); michael@0: michael@0: private: michael@0: const SkMatrix& fMatrix; michael@0: SkMatrix::MapXYProc fProc; michael@0: SkScalar fY; // ignored by MapXYProc michael@0: // these are only used by Only... procs michael@0: SkScalar fScaleX, fTransX, fTransformedY; michael@0: michael@0: static void MapXProc(const BitmapTextMapState& state, const SkScalar pos[]) { michael@0: state.fProc(state.fMatrix, *pos, state.fY, &state.fLoc); michael@0: } michael@0: michael@0: static void MapXYProc(const BitmapTextMapState& state, const SkScalar pos[]) { michael@0: state.fProc(state.fMatrix, pos[0], pos[1], &state.fLoc); michael@0: } michael@0: michael@0: static void MapOnlyScaleXProc(const BitmapTextMapState& state, michael@0: const SkScalar pos[]) { michael@0: state.fLoc.set(SkScalarMul(state.fScaleX, *pos) + state.fTransX, michael@0: state.fTransformedY); michael@0: } michael@0: michael@0: static void MapOnlyTransXProc(const BitmapTextMapState& state, michael@0: const SkScalar pos[]) { michael@0: state.fLoc.set(*pos + state.fTransX, state.fTransformedY); michael@0: } michael@0: }; michael@0: michael@0: BitmapTextMapState::Proc BitmapTextMapState::pickProc(int scalarsPerPosition) { michael@0: SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); michael@0: michael@0: if (1 == scalarsPerPosition) { michael@0: unsigned mtype = fMatrix.getType(); michael@0: if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) { michael@0: return MapXProc; michael@0: } else { michael@0: fScaleX = fMatrix.getScaleX(); michael@0: fTransX = fMatrix.getTranslateX(); michael@0: fTransformedY = SkScalarMul(fY, fMatrix.getScaleY()) + michael@0: fMatrix.getTranslateY(); michael@0: return (mtype & SkMatrix::kScale_Mask) ? michael@0: MapOnlyScaleXProc : MapOnlyTransXProc; michael@0: } michael@0: } else { michael@0: return MapXYProc; michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void GrBitmapTextContext::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: 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/* || 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, &fContext->getMatrix()); michael@0: SkGlyphCache* cache = autoCache.getCache(); michael@0: GrFontScaler* fontScaler = GetGrFontScaler(cache); michael@0: michael@0: // store original matrix before we reset, so we can use it to transform positions michael@0: SkMatrix ctm = fContext->getMatrix(); michael@0: GrContext::AutoMatrix autoMatrix; michael@0: autoMatrix.setIdentity(fContext, &fPaint); michael@0: michael@0: const char* stop = text + byteLength; michael@0: AlignProc alignProc = pick_align_proc(fSkPaint.getTextAlign()); michael@0: BitmapTextMapState tms(ctm, constY); michael@0: BitmapTextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition); michael@0: SkFixed halfSampleX = 0, halfSampleY = 0; michael@0: michael@0: if (cache->isSubpixel()) { michael@0: // maybe we should skip the rounding if linearText is set michael@0: SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(ctm); michael@0: michael@0: SkFixed fxMask = ~0; michael@0: SkFixed fyMask = ~0; michael@0: if (kX_SkAxisAlignment == baseline) { michael@0: fyMask = 0; michael@0: #ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX michael@0: halfSampleY = SK_FixedHalf; michael@0: #endif michael@0: } else if (kY_SkAxisAlignment == baseline) { michael@0: fxMask = 0; michael@0: #ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX michael@0: halfSampleX = SK_FixedHalf; michael@0: #endif michael@0: } michael@0: michael@0: if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { michael@0: while (text < stop) { michael@0: tmsProc(tms, pos); michael@0: SkFixed fx = SkScalarToFixed(tms.fLoc.fX) + halfSampleX; michael@0: SkFixed fy = SkScalarToFixed(tms.fLoc.fY) + halfSampleY; michael@0: michael@0: const SkGlyph& glyph = glyphCacheProc(cache, &text, michael@0: fx & fxMask, fy & fyMask); 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: pos += scalarsPerPosition; michael@0: } michael@0: } else { michael@0: while (text < stop) { michael@0: const char* currentText = text; michael@0: const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0); michael@0: michael@0: if (metricGlyph.fWidth) { michael@0: SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;) michael@0: SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;) michael@0: michael@0: tmsProc(tms, pos); michael@0: SkIPoint fixedLoc; michael@0: alignProc(tms.fLoc, metricGlyph, &fixedLoc); michael@0: michael@0: SkFixed fx = fixedLoc.fX + halfSampleX; michael@0: SkFixed fy = fixedLoc.fY + halfSampleY; michael@0: michael@0: // have to call again, now that we've been "aligned" michael@0: const SkGlyph& glyph = glyphCacheProc(cache, ¤tText, michael@0: fx & fxMask, fy & fyMask); michael@0: // the assumption is that the metrics haven't changed michael@0: SkASSERT(prevAdvX == glyph.fAdvanceX); michael@0: SkASSERT(prevAdvY == glyph.fAdvanceY); michael@0: SkASSERT(glyph.fWidth); michael@0: 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: pos += scalarsPerPosition; michael@0: } michael@0: } michael@0: } else { // not subpixel 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: tmsProc(tms, pos); michael@0: michael@0: SkFixed fx = SkScalarToFixed(tms.fLoc.fX) + SK_FixedHalf; //halfSampleX; michael@0: SkFixed fy = SkScalarToFixed(tms.fLoc.fY) + SK_FixedHalf; //halfSampleY; 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: pos += scalarsPerPosition; michael@0: } michael@0: } else { 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: tmsProc(tms, pos); michael@0: michael@0: SkIPoint fixedLoc; michael@0: alignProc(tms.fLoc, glyph, &fixedLoc); michael@0: michael@0: SkFixed fx = fixedLoc.fX + SK_FixedHalf; //halfSampleX; michael@0: SkFixed fy = fixedLoc.fY + SK_FixedHalf; //halfSampleY; 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: pos += scalarsPerPosition; michael@0: } michael@0: } michael@0: } michael@0: michael@0: this->finish(); 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 GrBitmapTextContext::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: michael@0: if (NULL == fStrike) { michael@0: fStrike = fContext->getFontCache()->getStrike(scaler, false); 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: 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: // flush any accumulated draws to allow us to free up a plot 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(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)), michael@0: SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop))); 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: // now promote them to fixed (TODO: Rethink using fixed pt). michael@0: width = SkIntToFixed(width); michael@0: height = SkIntToFixed(height); 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: GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); michael@0: GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); michael@0: michael@0: SkRect r; michael@0: r.fLeft = SkFixedToFloat(vx); michael@0: r.fTop = SkFixedToFloat(vy); michael@0: r.fRight = SkFixedToFloat(vx + width); michael@0: r.fBottom = SkFixedToFloat(vy + height); michael@0: michael@0: fVertexBounds.growToInclude(r); michael@0: michael@0: fVertices[2*fCurrVertex].setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, michael@0: 2 * sizeof(SkPoint)); michael@0: fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)), michael@0: SkFixedToFloat(texture->normalizeFixedY(ty)), michael@0: SkFixedToFloat(texture->normalizeFixedX(tx + width)), michael@0: SkFixedToFloat(texture->normalizeFixedY(ty + height)), michael@0: 2 * sizeof(SkPoint)); michael@0: fCurrVertex += 4; michael@0: }