jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | #include "GrBitmapTextContext.h" |
| 9 | #include "GrAtlas.h" |
| 10 | #include "GrDrawTarget.h" |
| 11 | #include "GrFontScaler.h" |
| 12 | #include "GrIndexBuffer.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 13 | #include "GrStrokeInfo.h" |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 14 | #include "GrTextStrike.h" |
| 15 | #include "GrTextStrike_impl.h" |
commit-bot@chromium.org | 42a8957 | 2013-10-28 15:13:50 +0000 | [diff] [blame] | 16 | #include "SkColorPriv.h" |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 17 | #include "SkPath.h" |
| 18 | #include "SkRTConf.h" |
| 19 | #include "SkStrokeRec.h" |
| 20 | #include "effects/GrCustomCoordsTextureEffect.h" |
| 21 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 22 | #include "SkAutoKern.h" |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 23 | #include "SkDraw.h" |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 24 | #include "SkDrawProcs.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 25 | #include "SkGlyphCache.h" |
| 26 | #include "SkGpuDevice.h" |
| 27 | #include "SkGr.h" |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 28 | #include "SkTextMapStateProc.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 29 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 30 | SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, |
| 31 | "Dump the contents of the font cache before every purge."); |
| 32 | |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 33 | static const int kGlyphCoordsNoColorAttributeIndex = 1; |
| 34 | static const int kGlyphCoordsWithColorAttributeIndex = 2; |
| 35 | |
| 36 | namespace { |
| 37 | // position + texture coord |
| 38 | extern const GrVertexAttrib gTextVertexAttribs[] = { |
| 39 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 40 | {kVec2f_GrVertexAttribType, sizeof(SkPoint) , kEffect_GrVertexAttribBinding} |
| 41 | }; |
| 42 | |
| 43 | // position + color + texture coord |
| 44 | extern const GrVertexAttrib gTextVertexWithColorAttribs[] = { |
| 45 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 46 | {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding}, |
| 47 | {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kEffect_GrVertexAttribBinding} |
| 48 | }; |
| 49 | |
| 50 | }; |
| 51 | |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 52 | GrBitmapTextContext::GrBitmapTextContext(GrContext* context, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 53 | const SkDeviceProperties& properties) |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 54 | : GrTextContext(context, properties) |
| 55 | , fStrike(NULL) |
| 56 | , fEffectTextureUniqueID(SK_InvalidUniqueID) |
| 57 | , fVertices(NULL) |
| 58 | , fVertexCount(0) |
| 59 | , fCurrVertex(0) { |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 60 | fVertexBounds.setLargestInverted(); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | GrBitmapTextContext::~GrBitmapTextContext() { |
| 64 | this->flushGlyphs(); |
| 65 | } |
| 66 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 67 | bool GrBitmapTextContext::canDraw(const SkPaint& paint) { |
| 68 | return !SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix()); |
| 69 | } |
| 70 | |
commit-bot@chromium.org | 42a8957 | 2013-10-28 15:13:50 +0000 | [diff] [blame] | 71 | static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { |
| 72 | unsigned r = SkColorGetR(c); |
| 73 | unsigned g = SkColorGetG(c); |
| 74 | unsigned b = SkColorGetB(c); |
| 75 | return GrColorPackRGBA(r, g, b, 0xff); |
| 76 | } |
| 77 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 78 | void GrBitmapTextContext::flushGlyphs() { |
| 79 | if (NULL == fDrawTarget) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | GrDrawState* drawState = fDrawTarget->drawState(); |
| 84 | GrDrawState::AutoRestoreEffects are(drawState); |
| 85 | drawState->setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); |
| 86 | |
| 87 | if (fCurrVertex > 0) { |
| 88 | // setup our sampler state for our text texture/atlas |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 89 | SkASSERT(SkIsAlign4(fCurrVertex)); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 90 | GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNone_FilterMode); |
| 91 | |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 92 | GrTexture* currTexture = fStrike->getTexture(); |
| 93 | SkASSERT(currTexture); |
| 94 | uint32_t textureUniqueID = currTexture->getUniqueID(); |
bsalomon | 8b2fac4 | 2014-06-19 14:13:45 -0700 | [diff] [blame] | 95 | |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 96 | if (textureUniqueID != fEffectTextureUniqueID) { |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 97 | fCachedEffect.reset(GrCustomCoordsTextureEffect::Create(currTexture, params)); |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 98 | fEffectTextureUniqueID = textureUniqueID; |
bsalomon | 8b2fac4 | 2014-06-19 14:13:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 101 | // This effect could be stored with one of the cache objects (atlas?) |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 102 | int coordsIdx = drawState->hasColorVertexAttribute() ? kGlyphCoordsWithColorAttributeIndex : |
| 103 | kGlyphCoordsNoColorAttributeIndex; |
bsalomon | 8b2fac4 | 2014-06-19 14:13:45 -0700 | [diff] [blame] | 104 | drawState->addCoverageEffect(fCachedEffect.get(), coordsIdx); |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 105 | SkASSERT(NULL != fStrike); |
| 106 | switch (fStrike->getMaskFormat()) { |
| 107 | // Color bitmap text |
| 108 | case kARGB_GrMaskFormat: |
| 109 | SkASSERT(!drawState->hasColorVertexAttribute()); |
| 110 | drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff()); |
| 111 | drawState->setColor(0xffffffff); |
| 112 | break; |
| 113 | // LCD text |
| 114 | case kA888_GrMaskFormat: |
| 115 | case kA565_GrMaskFormat: { |
| 116 | if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
| 117 | kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
| 118 | fPaint.numColorStages()) { |
| 119 | GrPrintf("LCD Text will not draw correctly.\n"); |
| 120 | } |
| 121 | SkASSERT(!drawState->hasColorVertexAttribute()); |
| 122 | // We don't use the GrPaint's color in this case because it's been premultiplied by |
| 123 | // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by |
| 124 | // the mask texture color. The end result is that we get |
| 125 | // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor |
| 126 | int a = SkColorGetA(fSkPaint.getColor()); |
| 127 | // paintAlpha |
| 128 | drawState->setColor(SkColorSetARGB(a, a, a, a)); |
| 129 | // paintColor |
| 130 | drawState->setBlendConstant(skcolor_to_grcolor_nopremultiply(fSkPaint.getColor())); |
| 131 | drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
| 132 | break; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 133 | } |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 134 | // Grayscale/BW text |
| 135 | case kA8_GrMaskFormat: |
| 136 | // set back to normal in case we took LCD path previously. |
| 137 | drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff()); |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 138 | // We're using per-vertex color. |
| 139 | SkASSERT(drawState->hasColorVertexAttribute()); |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 140 | break; |
| 141 | default: |
| 142 | SkFAIL("Unexepected mask format."); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 143 | } |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 144 | int nGlyphs = fCurrVertex / 4; |
| 145 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
| 146 | fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, |
| 147 | nGlyphs, |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 148 | 4, 6, &fVertexBounds); |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 149 | fVertexCount = 0; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 150 | fCurrVertex = 0; |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 151 | fVertexBounds.setLargestInverted(); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 152 | } |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 153 | |
| 154 | fDrawTarget->resetVertexSource(); |
| 155 | fVertices = NULL; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 156 | } |
| 157 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 158 | inline void GrBitmapTextContext::init(const GrPaint& paint, const SkPaint& skPaint) { |
| 159 | GrTextContext::init(paint, skPaint); |
| 160 | |
| 161 | fStrike = NULL; |
| 162 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 163 | fVertices = NULL; |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 164 | fVertexCount = 0; |
| 165 | fCurrVertex = 0; |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | inline void GrBitmapTextContext::finish() { |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 169 | this->flushGlyphs(); |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 170 | |
| 171 | GrTextContext::finish(); |
| 172 | } |
| 173 | |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 174 | void GrBitmapTextContext::allocateVertices(const char text[], size_t byteLength) { |
| 175 | SkASSERT(NULL == fVertices); |
| 176 | bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat(); |
| 177 | if (useColorVerts) { |
| 178 | fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>( |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 179 | SK_ARRAY_COUNT(gTextVertexWithColorAttribs)); |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 180 | } else { |
| 181 | fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 182 | SK_ARRAY_COUNT(gTextVertexAttribs)); |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 183 | } |
| 184 | fVertexCount = 4*fSkPaint.textToGlyphs(text, byteLength, NULL); |
| 185 | bool success = fDrawTarget->reserveVertexAndIndexSpace(fVertexCount, |
| 186 | 0, |
| 187 | &fVertices, |
| 188 | NULL); |
| 189 | GrAlwaysAssert(success); |
| 190 | } |
| 191 | |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 192 | void GrBitmapTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint, |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 193 | const char text[], size_t byteLength, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 194 | SkScalar x, SkScalar y) { |
| 195 | SkASSERT(byteLength == 0 || text != NULL); |
| 196 | |
| 197 | // nothing to draw |
| 198 | if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { |
| 199 | return; |
| 200 | } |
| 201 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 202 | this->init(paint, skPaint); |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 203 | |
| 204 | if (NULL == fDrawTarget) { |
| 205 | return; |
| 206 | } |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 207 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 208 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 209 | |
| 210 | SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMatrix()); |
| 211 | SkGlyphCache* cache = autoCache.getCache(); |
| 212 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 213 | if (NULL == fStrike) { |
| 214 | fStrike = fContext->getFontCache()->getStrike(fontScaler, false); |
| 215 | } |
| 216 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 217 | // transform our starting point |
| 218 | { |
| 219 | SkPoint loc; |
| 220 | fContext->getMatrix().mapXY(x, y, &loc); |
| 221 | x = loc.fX; |
| 222 | y = loc.fY; |
| 223 | } |
| 224 | |
| 225 | // need to measure first |
| 226 | if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
| 227 | SkVector stop; |
| 228 | |
| 229 | MeasureText(cache, glyphCacheProc, text, byteLength, &stop); |
| 230 | |
| 231 | SkScalar stopX = stop.fX; |
| 232 | SkScalar stopY = stop.fY; |
| 233 | |
| 234 | if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 235 | stopX = SkScalarHalf(stopX); |
| 236 | stopY = SkScalarHalf(stopY); |
| 237 | } |
| 238 | x -= stopX; |
| 239 | y -= stopY; |
| 240 | } |
| 241 | |
| 242 | const char* stop = text + byteLength; |
| 243 | |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 244 | this->allocateVertices(text, byteLength); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 245 | |
| 246 | SkFixed fxMask = ~0; |
| 247 | SkFixed fyMask = ~0; |
| 248 | SkFixed halfSampleX, halfSampleY; |
| 249 | if (cache->isSubpixel()) { |
| 250 | halfSampleX = halfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits); |
| 251 | SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(fContext->getMatrix()); |
| 252 | if (kX_SkAxisAlignment == baseline) { |
| 253 | fyMask = 0; |
| 254 | halfSampleY = SK_FixedHalf; |
| 255 | } else if (kY_SkAxisAlignment == baseline) { |
| 256 | fxMask = 0; |
| 257 | halfSampleX = SK_FixedHalf; |
| 258 | } |
| 259 | } else { |
| 260 | halfSampleX = halfSampleY = SK_FixedHalf; |
| 261 | } |
| 262 | |
| 263 | SkFixed fx = SkScalarToFixed(x) + halfSampleX; |
| 264 | SkFixed fy = SkScalarToFixed(y) + halfSampleY; |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 265 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 266 | GrContext::AutoMatrix autoMatrix; |
| 267 | autoMatrix.setIdentity(fContext, &fPaint); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 268 | |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 269 | SkAutoKern autokern; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 270 | while (text < stop) { |
| 271 | const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask); |
| 272 | |
| 273 | fx += autokern.adjust(glyph); |
| 274 | |
| 275 | if (glyph.fWidth) { |
| 276 | this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 277 | glyph.getSubXFixed(), |
| 278 | glyph.getSubYFixed()), |
| 279 | SkFixedFloorToFixed(fx), |
| 280 | SkFixedFloorToFixed(fy), |
| 281 | fontScaler); |
| 282 | } |
| 283 | |
| 284 | fx += glyph.fAdvanceX; |
| 285 | fy += glyph.fAdvanceY; |
| 286 | } |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 287 | |
| 288 | this->finish(); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 289 | } |
| 290 | |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 291 | void GrBitmapTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint, |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 292 | const char text[], size_t byteLength, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 293 | const SkScalar pos[], SkScalar constY, |
| 294 | int scalarsPerPosition) { |
| 295 | SkASSERT(byteLength == 0 || text != NULL); |
| 296 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 297 | |
| 298 | // nothing to draw |
| 299 | if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { |
| 300 | return; |
| 301 | } |
| 302 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 303 | this->init(paint, skPaint); |
| 304 | |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 305 | if (NULL == fDrawTarget) { |
| 306 | return; |
| 307 | } |
| 308 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 309 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 310 | |
| 311 | SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMatrix()); |
| 312 | SkGlyphCache* cache = autoCache.getCache(); |
| 313 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 314 | if (NULL == fStrike) { |
| 315 | fStrike = fContext->getFontCache()->getStrike(fontScaler, false); |
| 316 | } |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 317 | |
| 318 | // store original matrix before we reset, so we can use it to transform positions |
| 319 | SkMatrix ctm = fContext->getMatrix(); |
| 320 | GrContext::AutoMatrix autoMatrix; |
| 321 | autoMatrix.setIdentity(fContext, &fPaint); |
| 322 | |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 323 | this->allocateVertices(text, byteLength); |
| 324 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 325 | const char* stop = text + byteLength; |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 326 | SkTextAlignProc alignProc(fSkPaint.getTextAlign()); |
| 327 | SkTextMapStateProc tmsProc(ctm, constY, scalarsPerPosition); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 328 | SkFixed halfSampleX = 0, halfSampleY = 0; |
| 329 | |
| 330 | if (cache->isSubpixel()) { |
| 331 | // maybe we should skip the rounding if linearText is set |
| 332 | SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(ctm); |
| 333 | |
| 334 | SkFixed fxMask = ~0; |
| 335 | SkFixed fyMask = ~0; |
| 336 | if (kX_SkAxisAlignment == baseline) { |
| 337 | fyMask = 0; |
| 338 | #ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX |
| 339 | halfSampleY = SK_FixedHalf; |
| 340 | #endif |
| 341 | } else if (kY_SkAxisAlignment == baseline) { |
| 342 | fxMask = 0; |
| 343 | #ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX |
| 344 | halfSampleX = SK_FixedHalf; |
| 345 | #endif |
| 346 | } |
| 347 | |
| 348 | if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
| 349 | while (text < stop) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 350 | SkPoint tmsLoc; |
| 351 | tmsProc(pos, &tmsLoc); |
| 352 | SkFixed fx = SkScalarToFixed(tmsLoc.fX) + halfSampleX; |
| 353 | SkFixed fy = SkScalarToFixed(tmsLoc.fY) + halfSampleY; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 354 | |
| 355 | const SkGlyph& glyph = glyphCacheProc(cache, &text, |
| 356 | fx & fxMask, fy & fyMask); |
| 357 | |
| 358 | if (glyph.fWidth) { |
| 359 | this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 360 | glyph.getSubXFixed(), |
| 361 | glyph.getSubYFixed()), |
| 362 | SkFixedFloorToFixed(fx), |
| 363 | SkFixedFloorToFixed(fy), |
| 364 | fontScaler); |
| 365 | } |
| 366 | pos += scalarsPerPosition; |
| 367 | } |
| 368 | } else { |
| 369 | while (text < stop) { |
| 370 | const char* currentText = text; |
| 371 | const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0); |
| 372 | |
| 373 | if (metricGlyph.fWidth) { |
| 374 | SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;) |
| 375 | SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;) |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 376 | SkPoint tmsLoc; |
| 377 | tmsProc(pos, &tmsLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 378 | SkIPoint fixedLoc; |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 379 | alignProc(tmsLoc, metricGlyph, &fixedLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 380 | |
| 381 | SkFixed fx = fixedLoc.fX + halfSampleX; |
| 382 | SkFixed fy = fixedLoc.fY + halfSampleY; |
| 383 | |
| 384 | // have to call again, now that we've been "aligned" |
| 385 | const SkGlyph& glyph = glyphCacheProc(cache, ¤tText, |
| 386 | fx & fxMask, fy & fyMask); |
| 387 | // the assumption is that the metrics haven't changed |
| 388 | SkASSERT(prevAdvX == glyph.fAdvanceX); |
| 389 | SkASSERT(prevAdvY == glyph.fAdvanceY); |
| 390 | SkASSERT(glyph.fWidth); |
| 391 | |
| 392 | this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 393 | glyph.getSubXFixed(), |
| 394 | glyph.getSubYFixed()), |
| 395 | SkFixedFloorToFixed(fx), |
| 396 | SkFixedFloorToFixed(fy), |
| 397 | fontScaler); |
| 398 | } |
| 399 | pos += scalarsPerPosition; |
| 400 | } |
| 401 | } |
| 402 | } else { // not subpixel |
| 403 | |
| 404 | if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
| 405 | while (text < stop) { |
| 406 | // the last 2 parameters are ignored |
| 407 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 408 | |
| 409 | if (glyph.fWidth) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 410 | SkPoint tmsLoc; |
| 411 | tmsProc(pos, &tmsLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 412 | |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 413 | SkFixed fx = SkScalarToFixed(tmsLoc.fX) + SK_FixedHalf; //halfSampleX; |
| 414 | SkFixed fy = SkScalarToFixed(tmsLoc.fY) + SK_FixedHalf; //halfSampleY; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 415 | this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 416 | glyph.getSubXFixed(), |
| 417 | glyph.getSubYFixed()), |
| 418 | SkFixedFloorToFixed(fx), |
| 419 | SkFixedFloorToFixed(fy), |
| 420 | fontScaler); |
| 421 | } |
| 422 | pos += scalarsPerPosition; |
| 423 | } |
| 424 | } else { |
| 425 | while (text < stop) { |
| 426 | // the last 2 parameters are ignored |
| 427 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 428 | |
| 429 | if (glyph.fWidth) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 430 | SkPoint tmsLoc; |
| 431 | tmsProc(pos, &tmsLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 432 | |
| 433 | SkIPoint fixedLoc; |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 434 | alignProc(tmsLoc, glyph, &fixedLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 435 | |
| 436 | SkFixed fx = fixedLoc.fX + SK_FixedHalf; //halfSampleX; |
| 437 | SkFixed fy = fixedLoc.fY + SK_FixedHalf; //halfSampleY; |
| 438 | this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 439 | glyph.getSubXFixed(), |
| 440 | glyph.getSubYFixed()), |
| 441 | SkFixedFloorToFixed(fx), |
| 442 | SkFixedFloorToFixed(fy), |
| 443 | fontScaler); |
| 444 | } |
| 445 | pos += scalarsPerPosition; |
| 446 | } |
| 447 | } |
| 448 | } |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 449 | |
| 450 | this->finish(); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 451 | } |
| 452 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 453 | void GrBitmapTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 454 | SkFixed vx, SkFixed vy, |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 455 | GrFontScaler* scaler) { |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 456 | GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
| 457 | if (NULL == glyph || glyph->fBounds.isEmpty()) { |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | vx += SkIntToFixed(glyph->fBounds.fLeft); |
| 462 | vy += SkIntToFixed(glyph->fBounds.fTop); |
| 463 | |
| 464 | // keep them as ints until we've done the clip-test |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 465 | SkFixed width = glyph->fBounds.width(); |
| 466 | SkFixed height = glyph->fBounds.height(); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 467 | |
| 468 | // check if we clipped out |
| 469 | if (true || NULL == glyph->fPlot) { |
| 470 | int x = vx >> 16; |
| 471 | int y = vy >> 16; |
| 472 | if (fClipRect.quickReject(x, y, x + width, y + height)) { |
| 473 | // SkCLZ(3); // so we can set a break-point in the debugger |
| 474 | return; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if (NULL == glyph->fPlot) { |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 479 | if (fStrike->addGlyphToAtlas(glyph, scaler)) { |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 480 | goto HAS_ATLAS; |
| 481 | } |
| 482 | |
| 483 | // try to clear out an unused plot before we flush |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 484 | if (fContext->getFontCache()->freeUnusedPlot(fStrike) && |
| 485 | fStrike->addGlyphToAtlas(glyph, scaler)) { |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 486 | goto HAS_ATLAS; |
| 487 | } |
| 488 | |
| 489 | if (c_DumpFontCache) { |
| 490 | #ifdef SK_DEVELOPER |
| 491 | fContext->getFontCache()->dump(); |
| 492 | #endif |
| 493 | } |
| 494 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 495 | // flush any accumulated draws to allow us to free up a plot |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 496 | int remainingVertexCount = fVertexCount - fCurrVertex; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 497 | this->flushGlyphs(); |
| 498 | fContext->flush(); |
| 499 | |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 500 | // need to reallocate the vertex buffer for the remaining glyphs |
| 501 | fVertexCount = remainingVertexCount; |
| 502 | bool success = fDrawTarget->reserveVertexAndIndexSpace(fVertexCount, |
| 503 | 0, |
| 504 | &fVertices, |
| 505 | NULL); |
| 506 | GrAlwaysAssert(success); |
| 507 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 508 | // we should have an unused plot now |
| 509 | if (fContext->getFontCache()->freeUnusedPlot(fStrike) && |
| 510 | fStrike->addGlyphToAtlas(glyph, scaler)) { |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 511 | goto HAS_ATLAS; |
| 512 | } |
| 513 | |
| 514 | if (NULL == glyph->fPath) { |
| 515 | SkPath* path = SkNEW(SkPath); |
| 516 | if (!scaler->getGlyphPath(glyph->glyphID(), path)) { |
| 517 | // flag the glyph as being dead? |
| 518 | delete path; |
| 519 | return; |
| 520 | } |
| 521 | glyph->fPath = path; |
| 522 | } |
| 523 | |
| 524 | GrContext::AutoMatrix am; |
| 525 | SkMatrix translate; |
| 526 | translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)), |
| 527 | SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop))); |
| 528 | GrPaint tmpPaint(fPaint); |
| 529 | am.setPreConcat(fContext, translate, &tmpPaint); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 530 | GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle); |
| 531 | fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | |
| 535 | HAS_ATLAS: |
| 536 | SkASSERT(glyph->fPlot); |
| 537 | GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); |
| 538 | glyph->fPlot->setDrawToken(drawToken); |
| 539 | |
| 540 | // now promote them to fixed (TODO: Rethink using fixed pt). |
| 541 | width = SkIntToFixed(width); |
| 542 | height = SkIntToFixed(height); |
| 543 | |
| 544 | GrTexture* texture = glyph->fPlot->texture(); |
| 545 | SkASSERT(texture); |
| 546 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 547 | SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); |
| 548 | SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 549 | |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 550 | SkRect r; |
| 551 | r.fLeft = SkFixedToFloat(vx); |
| 552 | r.fTop = SkFixedToFloat(vy); |
| 553 | r.fRight = SkFixedToFloat(vx + width); |
| 554 | r.fBottom = SkFixedToFloat(vy + height); |
| 555 | |
| 556 | fVertexBounds.growToInclude(r); |
| 557 | |
jvanverth | 9c3d24b | 2014-08-27 11:53:17 -0700 | [diff] [blame] | 558 | bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat(); |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 559 | size_t vertSize = useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) : |
| 560 | (2 * sizeof(SkPoint)); |
| 561 | |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 562 | SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexSize()); |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 563 | |
| 564 | SkPoint* positions = reinterpret_cast<SkPoint*>( |
| 565 | reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); |
| 566 | positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); |
| 567 | |
| 568 | // The texture coords are last in both the with and without color vertex layouts. |
| 569 | SkPoint* textureCoords = reinterpret_cast<SkPoint*>( |
| 570 | reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint)); |
| 571 | textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)), |
| 572 | SkFixedToFloat(texture->normalizeFixedY(ty)), |
| 573 | SkFixedToFloat(texture->normalizeFixedX(tx + width)), |
| 574 | SkFixedToFloat(texture->normalizeFixedY(ty + height)), |
| 575 | vertSize); |
| 576 | if (useColorVerts) { |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 577 | if (0xFF == GrColorUnpackA(fPaint.getColor())) { |
| 578 | fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true); |
| 579 | } |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 580 | // color comes after position. |
| 581 | GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); |
| 582 | for (int i = 0; i < 4; ++i) { |
| 583 | *colors = fPaint.getColor(); |
| 584 | colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize); |
| 585 | } |
| 586 | } |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 587 | fCurrVertex += 4; |
| 588 | } |