GrTextContext.cpp revision eb6879f50a5564eeb981ec5616b55bf685eb76fc
1/* 2 * Copyright 2010 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 9 10#include "GrTextContext.h" 11#include "GrAtlas.h" 12#include "GrContext.h" 13#include "GrDrawTarget.h" 14#include "GrFontScaler.h" 15#include "GrIndexBuffer.h" 16#include "GrTextStrike.h" 17#include "GrTextStrike_impl.h" 18#include "SkPath.h" 19#include "SkStrokeRec.h" 20 21static const int kGlyphCoordsAttributeIndex = 1; 22 23void GrTextContext::flushGlyphs() { 24 if (NULL == fDrawTarget) { 25 return; 26 } 27 28 GrDrawState* drawState = fDrawTarget->drawState(); 29 GrDrawState::AutoRestoreEffects are(drawState); 30 drawState->setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); 31 32 if (fCurrVertex > 0) { 33 // setup our sampler state for our text texture/atlas 34 GrAssert(GrIsALIGN4(fCurrVertex)); 35 GrAssert(fCurrTexture); 36 GrTextureParams params(SkShader::kRepeat_TileMode, false); 37 38 // This effect could be stored with one of the cache objects (atlas?) 39 drawState->addCoverageEffect( 40 GrSimpleTextureEffect::CreateWithCustomCoords(fCurrTexture, params), 41 kGlyphCoordsAttributeIndex)->unref(); 42 43 if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { 44 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || 45 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || 46 fPaint.hasColorStage()) { 47 GrPrintf("LCD Text will not draw correctly.\n"); 48 } 49 // setup blend so that we get mask * paintColor + (1-mask)*dstColor 50 drawState->setBlendConstant(fPaint.getColor()); 51 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); 52 // don't modulate by the paint's color in the frag since we're 53 // already doing it via the blend const. 54 drawState->setColor(0xffffffff); 55 } else { 56 // set back to normal in case we took LCD path previously. 57 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff()); 58 drawState->setColor(fPaint.getColor()); 59 } 60 61 int nGlyphs = fCurrVertex / 4; 62 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); 63 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, 64 nGlyphs, 65 4, 6); 66 fDrawTarget->resetVertexSource(); 67 fVertices = NULL; 68 fMaxVertices = 0; 69 fCurrVertex = 0; 70 GrSafeSetNull(fCurrTexture); 71 } 72} 73 74GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint) : fPaint(paint) { 75 fContext = context; 76 fStrike = NULL; 77 78 fCurrTexture = NULL; 79 fCurrVertex = 0; 80 81 const GrClipData* clipData = context->getClip(); 82 83 GrRect devConservativeBound; 84 clipData->fClipStack->getConservativeBounds( 85 -clipData->fOrigin.fX, 86 -clipData->fOrigin.fY, 87 context->getRenderTarget()->width(), 88 context->getRenderTarget()->height(), 89 &devConservativeBound); 90 91 devConservativeBound.roundOut(&fClipRect); 92 93 fAutoMatrix.setIdentity(fContext, &fPaint); 94 95 fDrawTarget = fContext->getTextTarget(); 96 97 fVertices = NULL; 98 fMaxVertices = 0; 99} 100 101GrTextContext::~GrTextContext() { 102 this->flushGlyphs(); 103} 104 105void GrTextContext::flush() { 106 this->flushGlyphs(); 107} 108 109namespace { 110 111// position + texture coord 112extern const GrVertexAttrib gTextVertexAttribs[] = { 113 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, 114 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} 115}; 116 117}; 118 119void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed, 120 GrFixed vx, GrFixed vy, 121 GrFontScaler* scaler) { 122 if (NULL == fDrawTarget) { 123 return; 124 } 125 if (NULL == fStrike) { 126 fStrike = fContext->getFontCache()->getStrike(scaler); 127 } 128 129 GrGlyph* glyph = fStrike->getGlyph(packed, scaler); 130 if (NULL == glyph || glyph->fBounds.isEmpty()) { 131 return; 132 } 133 134 vx += SkIntToFixed(glyph->fBounds.fLeft); 135 vy += SkIntToFixed(glyph->fBounds.fTop); 136 137 // keep them as ints until we've done the clip-test 138 GrFixed width = glyph->fBounds.width(); 139 GrFixed height = glyph->fBounds.height(); 140 141 // check if we clipped out 142 if (true || NULL == glyph->fAtlas) { 143 int x = vx >> 16; 144 int y = vy >> 16; 145 if (fClipRect.quickReject(x, y, x + width, y + height)) { 146// SkCLZ(3); // so we can set a break-point in the debugger 147 return; 148 } 149 } 150 151 if (NULL == glyph->fAtlas) { 152 if (fStrike->getGlyphAtlas(glyph, scaler)) { 153 goto HAS_ATLAS; 154 } 155 156 // before we purge the cache, we must flush any accumulated draws 157 this->flushGlyphs(); 158 fContext->flush(); 159 160 // try to purge 161 fContext->getFontCache()->purgeExceptFor(fStrike); 162 if (fStrike->getGlyphAtlas(glyph, scaler)) { 163 goto HAS_ATLAS; 164 } 165 166 if (NULL == glyph->fPath) { 167 SkPath* path = SkNEW(SkPath); 168 if (!scaler->getGlyphPath(glyph->glyphID(), path)) { 169 // flag the glyph as being dead? 170 delete path; 171 return; 172 } 173 glyph->fPath = path; 174 } 175 176 GrContext::AutoMatrix am; 177 SkMatrix translate; 178 translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)), 179 SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop))); 180 GrPaint tmpPaint(fPaint); 181 am.setPreConcat(fContext, translate, &tmpPaint); 182 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); 183 fContext->drawPath(tmpPaint, *glyph->fPath, stroke); 184 return; 185 } 186 187HAS_ATLAS: 188 GrAssert(glyph->fAtlas); 189 190 // now promote them to fixed (TODO: Rethink using fixed pt). 191 width = SkIntToFixed(width); 192 height = SkIntToFixed(height); 193 194 GrTexture* texture = glyph->fAtlas->texture(); 195 GrAssert(texture); 196 197 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { 198 this->flushGlyphs(); 199 fCurrTexture = texture; 200 fCurrTexture->ref(); 201 } 202 203 if (NULL == fVertices) { 204 // If we need to reserve vertices allow the draw target to suggest 205 // a number of verts to reserve and whether to perform a flush. 206 fMaxVertices = kMinRequestedVerts; 207 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( 208 SK_ARRAY_COUNT(gTextVertexAttribs)); 209 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL); 210 if (flush) { 211 this->flushGlyphs(); 212 fContext->flush(); 213 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( 214 SK_ARRAY_COUNT(gTextVertexAttribs)); 215 } 216 fMaxVertices = kDefaultRequestedVerts; 217 // ignore return, no point in flushing again. 218 fDrawTarget->geometryHints(&fMaxVertices, NULL); 219 220 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads(); 221 if (fMaxVertices < kMinRequestedVerts) { 222 fMaxVertices = kDefaultRequestedVerts; 223 } else if (fMaxVertices > maxQuadVertices) { 224 // don't exceed the limit of the index buffer 225 fMaxVertices = maxQuadVertices; 226 } 227 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices, 228 0, 229 GrTCast<void**>(&fVertices), 230 NULL); 231 GrAlwaysAssert(success); 232 GrAssert(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize()); 233 } 234 235 GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); 236 GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); 237 238 fVertices[2*fCurrVertex].setRectFan(SkFixedToFloat(vx), 239 SkFixedToFloat(vy), 240 SkFixedToFloat(vx + width), 241 SkFixedToFloat(vy + height), 242 2 * sizeof(SkPoint)); 243 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)), 244 SkFixedToFloat(texture->normalizeFixedY(ty)), 245 SkFixedToFloat(texture->normalizeFixedX(tx + width)), 246 SkFixedToFloat(texture->normalizeFixedY(ty + height)), 247 2 * sizeof(SkPoint)); 248 fCurrVertex += 4; 249} 250