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