jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +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 "GrDistanceFieldTextContext.h" |
| 9 | #include "GrAtlas.h" |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 10 | #include "SkColorFilter.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 12 | #include "GrDrawTargetCaps.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 13 | #include "GrFontScaler.h" |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 14 | #include "SkGlyphCache.h" |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 15 | #include "GrGpu.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 16 | #include "GrIndexBuffer.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 17 | #include "GrStrokeInfo.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 18 | #include "GrTextStrike.h" |
| 19 | #include "GrTextStrike_impl.h" |
commit-bot@chromium.org | 64b08a1 | 2014-04-15 17:53:21 +0000 | [diff] [blame] | 20 | #include "SkDistanceFieldGen.h" |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 21 | #include "SkDraw.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 22 | #include "SkGpuDevice.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 23 | #include "SkPath.h" |
| 24 | #include "SkRTConf.h" |
| 25 | #include "SkStrokeRec.h" |
| 26 | #include "effects/GrDistanceFieldTextureEffect.h" |
| 27 | |
| 28 | static const int kGlyphCoordsAttributeIndex = 1; |
| 29 | |
commit-bot@chromium.org | dc5cd85 | 2014-04-02 19:24:32 +0000 | [diff] [blame] | 30 | static const int kSmallDFFontSize = 32; |
| 31 | static const int kSmallDFFontLimit = 32; |
| 32 | static const int kMediumDFFontSize = 64; |
| 33 | static const int kMediumDFFontLimit = 64; |
| 34 | static const int kLargeDFFontSize = 128; |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 35 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 36 | SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, |
| 37 | "Dump the contents of the font cache before every purge."); |
| 38 | |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 39 | GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context, |
commit-bot@chromium.org | 6fcd1ef | 2014-05-02 12:39:41 +0000 | [diff] [blame] | 40 | const SkDeviceProperties& properties, |
| 41 | bool enable) |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 42 | : GrTextContext(context, properties) { |
commit-bot@chromium.org | 6fcd1ef | 2014-05-02 12:39:41 +0000 | [diff] [blame] | 43 | #if SK_FORCE_DISTANCEFIELD_FONTS |
| 44 | fEnableDFRendering = true; |
| 45 | #else |
| 46 | fEnableDFRendering = enable; |
| 47 | #endif |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 48 | fStrike = NULL; |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 49 | fGammaTexture = NULL; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 50 | |
| 51 | fCurrTexture = NULL; |
| 52 | fCurrVertex = 0; |
| 53 | |
| 54 | fVertices = NULL; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | GrDistanceFieldTextContext::~GrDistanceFieldTextContext() { |
| 58 | this->flushGlyphs(); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 59 | SkSafeSetNull(fGammaTexture); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 60 | } |
| 61 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 62 | bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) { |
commit-bot@chromium.org | 6fcd1ef | 2014-05-02 12:39:41 +0000 | [diff] [blame] | 63 | if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) { |
commit-bot@chromium.org | eefd8a0 | 2014-04-08 20:14:32 +0000 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
skia.committer@gmail.com | e1d9443 | 2014-04-09 03:04:11 +0000 | [diff] [blame] | 67 | // rasterizers and mask filters modify alpha, which doesn't |
commit-bot@chromium.org | eefd8a0 | 2014-04-08 20:14:32 +0000 | [diff] [blame] | 68 | // translate well to distance |
| 69 | if (paint.getRasterizer() || paint.getMaskFilter() || |
| 70 | !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | // TODO: add some stroking support |
| 75 | if (paint.getStyle() != SkPaint::kFill_Style) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // TODO: choose an appropriate maximum scale for distance fields and |
| 80 | // enable perspective |
| 81 | if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | // distance fields cannot represent color fonts |
| 86 | SkScalerContext::Rec rec; |
| 87 | SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec); |
| 88 | return rec.getFormat() != SkMask::kARGB32_Format; |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 89 | } |
| 90 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 91 | static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) { |
| 92 | unsigned r = SkColorGetR(c); |
| 93 | unsigned g = SkColorGetG(c); |
| 94 | unsigned b = SkColorGetB(c); |
| 95 | return GrColorPackRGBA(r, g, b, 0xff); |
| 96 | } |
| 97 | |
| 98 | void GrDistanceFieldTextContext::flushGlyphs() { |
| 99 | if (NULL == fDrawTarget) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | GrDrawState* drawState = fDrawTarget->drawState(); |
| 104 | GrDrawState::AutoRestoreEffects are(drawState); |
| 105 | drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget()); |
| 106 | |
| 107 | if (fCurrVertex > 0) { |
| 108 | // setup our sampler state for our text texture/atlas |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 109 | SkASSERT(SkIsAlign4(fCurrVertex)); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 110 | SkASSERT(fCurrTexture); |
| 111 | GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 112 | GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 114 | // Effects could be stored with one of the cache objects (atlas?) |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 115 | SkColor filteredColor; |
| 116 | SkColorFilter* colorFilter = fSkPaint.getColorFilter(); |
| 117 | if (NULL != colorFilter) { |
| 118 | filteredColor = colorFilter->filterColor(fSkPaint.getColor()); |
| 119 | } else { |
| 120 | filteredColor = fSkPaint.getColor(); |
| 121 | } |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 122 | if (fUseLCDText) { |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 123 | GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 124 | bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout == |
| 125 | fDeviceProperties.fGeometry.getLayout(); |
| 126 | drawState->addCoverageEffect(GrDistanceFieldLCDTextureEffect::Create( |
| 127 | fCurrTexture, |
| 128 | params, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 129 | fGammaTexture, |
| 130 | gammaParams, |
| 131 | colorNoPreMul, |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 132 | fContext->getMatrix().rectStaysRect() && |
| 133 | fContext->getMatrix().isSimilarity(), |
| 134 | useBGR), |
| 135 | kGlyphCoordsAttributeIndex)->unref(); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 136 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 137 | if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
| 138 | kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
| 139 | fPaint.numColorStages()) { |
| 140 | GrPrintf("LCD Text will not draw correctly.\n"); |
| 141 | } |
| 142 | // We don't use the GrPaint's color in this case because it's been premultiplied by |
| 143 | // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by |
| 144 | // the mask texture color. The end result is that we get |
| 145 | // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 146 | int a = SkColorGetA(fSkPaint.getColor()); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 147 | // paintAlpha |
| 148 | drawState->setColor(SkColorSetARGB(a, a, a, a)); |
| 149 | // paintColor |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 150 | drawState->setBlendConstant(colorNoPreMul); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 151 | drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
| 152 | } else { |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 153 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 154 | U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.fGamma, |
| 155 | filteredColor); |
| 156 | drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create( |
| 157 | fCurrTexture, params, |
| 158 | fGammaTexture, gammaParams, |
| 159 | lum/255.f, |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 160 | fContext->getMatrix().isSimilarity()), |
| 161 | kGlyphCoordsAttributeIndex)->unref(); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 162 | #else |
| 163 | drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create( |
| 164 | fCurrTexture, params, |
| 165 | fContext->getMatrix().isSimilarity()), |
| 166 | kGlyphCoordsAttributeIndex)->unref(); |
| 167 | #endif |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 168 | // set back to normal in case we took LCD path previously. |
| 169 | drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff()); |
| 170 | drawState->setColor(fPaint.getColor()); |
| 171 | } |
| 172 | |
| 173 | int nGlyphs = fCurrVertex / 4; |
| 174 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
| 175 | fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, |
| 176 | nGlyphs, |
| 177 | 4, 6); |
| 178 | fDrawTarget->resetVertexSource(); |
| 179 | fVertices = NULL; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 180 | fCurrVertex = 0; |
| 181 | SkSafeSetNull(fCurrTexture); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | namespace { |
| 186 | |
| 187 | // position + texture coord |
| 188 | extern const GrVertexAttrib gTextVertexAttribs[] = { |
| 189 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 190 | {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding} |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | }; |
| 194 | |
| 195 | void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 196 | SkFixed vx, SkFixed vy, |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 197 | GrFontScaler* scaler) { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 198 | GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
| 199 | if (NULL == glyph || glyph->fBounds.isEmpty()) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | SkScalar sx = SkFixedToScalar(vx); |
| 204 | SkScalar sy = SkFixedToScalar(vy); |
| 205 | /* |
| 206 | // not valid, need to find a different solution for this |
| 207 | vx += SkIntToFixed(glyph->fBounds.fLeft); |
| 208 | vy += SkIntToFixed(glyph->fBounds.fTop); |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 209 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 210 | // keep them as ints until we've done the clip-test |
| 211 | GrFixed width = glyph->fBounds.width(); |
| 212 | GrFixed height = glyph->fBounds.height(); |
| 213 | |
| 214 | // check if we clipped out |
| 215 | if (true || NULL == glyph->fPlot) { |
| 216 | int x = vx >> 16; |
| 217 | int y = vy >> 16; |
| 218 | if (fClipRect.quickReject(x, y, x + width, y + height)) { |
| 219 | // SkCLZ(3); // so we can set a break-point in the debugger |
| 220 | return; |
| 221 | } |
| 222 | } |
| 223 | */ |
| 224 | if (NULL == glyph->fPlot) { |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 225 | if (fStrike->addGlyphToAtlas(glyph, scaler)) { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 226 | goto HAS_ATLAS; |
| 227 | } |
| 228 | |
| 229 | // try to clear out an unused plot before we flush |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 230 | if (fContext->getFontCache()->freeUnusedPlot(fStrike) && |
| 231 | fStrike->addGlyphToAtlas(glyph, scaler)) { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 232 | goto HAS_ATLAS; |
| 233 | } |
| 234 | |
| 235 | if (c_DumpFontCache) { |
| 236 | #ifdef SK_DEVELOPER |
| 237 | fContext->getFontCache()->dump(); |
| 238 | #endif |
| 239 | } |
| 240 | |
| 241 | // before we purge the cache, we must flush any accumulated draws |
| 242 | this->flushGlyphs(); |
| 243 | fContext->flush(); |
| 244 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 245 | // we should have an unused plot now |
| 246 | if (fContext->getFontCache()->freeUnusedPlot(fStrike) && |
| 247 | fStrike->addGlyphToAtlas(glyph, scaler)) { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 248 | goto HAS_ATLAS; |
| 249 | } |
| 250 | |
| 251 | if (NULL == glyph->fPath) { |
| 252 | SkPath* path = SkNEW(SkPath); |
| 253 | if (!scaler->getGlyphPath(glyph->glyphID(), path)) { |
| 254 | // flag the glyph as being dead? |
| 255 | delete path; |
| 256 | return; |
| 257 | } |
| 258 | glyph->fPath = path; |
| 259 | } |
| 260 | |
| 261 | GrContext::AutoMatrix am; |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 262 | SkMatrix ctm; |
| 263 | ctm.setScale(fTextRatio, fTextRatio); |
| 264 | ctm.postTranslate(sx, sy); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 265 | GrPaint tmpPaint(fPaint); |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 266 | am.setPreConcat(fContext, ctm, &tmpPaint); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 267 | GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle); |
| 268 | fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
| 272 | HAS_ATLAS: |
| 273 | SkASSERT(glyph->fPlot); |
| 274 | GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); |
| 275 | glyph->fPlot->setDrawToken(drawToken); |
| 276 | |
| 277 | GrTexture* texture = glyph->fPlot->texture(); |
| 278 | SkASSERT(texture); |
| 279 | |
jvanverth | 1d38619 | 2014-07-25 11:31:13 -0700 | [diff] [blame^] | 280 | if (fCurrTexture != texture) { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 281 | this->flushGlyphs(); |
| 282 | fCurrTexture = texture; |
| 283 | fCurrTexture->ref(); |
| 284 | } |
| 285 | |
commit-bot@chromium.org | 64b08a1 | 2014-04-15 17:53:21 +0000 | [diff] [blame] | 286 | SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset); |
| 287 | SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset); |
| 288 | SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset); |
| 289 | SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 290 | |
| 291 | SkScalar scale = fTextRatio; |
| 292 | dx *= scale; |
| 293 | dy *= scale; |
| 294 | sx += dx; |
| 295 | sy += dy; |
| 296 | width *= scale; |
| 297 | height *= scale; |
skia.committer@gmail.com | a3b5327 | 2014-02-15 03:02:15 +0000 | [diff] [blame] | 298 | |
commit-bot@chromium.org | 64b08a1 | 2014-04-15 17:53:21 +0000 | [diff] [blame] | 299 | SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset); |
| 300 | SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset); |
| 301 | SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset); |
| 302 | SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 303 | |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 304 | static const size_t kVertexSize = 2 * sizeof(SkPoint); |
jvanverth | 1d38619 | 2014-07-25 11:31:13 -0700 | [diff] [blame^] | 305 | SkPoint* positions = reinterpret_cast<SkPoint*>( |
| 306 | reinterpret_cast<intptr_t>(fVertices) + kVertexSize * fCurrVertex); |
| 307 | positions->setRectFan(sx, |
| 308 | sy, |
| 309 | sx + width, |
| 310 | sy + height, |
| 311 | kVertexSize); |
| 312 | SkPoint* textureCoords = reinterpret_cast<SkPoint*>( |
| 313 | reinterpret_cast<intptr_t>(positions) + kVertexSize - sizeof(SkPoint)); |
| 314 | textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)), |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 315 | SkFixedToFloat(texture->normalizeFixedY(ty)), |
| 316 | SkFixedToFloat(texture->normalizeFixedX(tx + tw)), |
| 317 | SkFixedToFloat(texture->normalizeFixedY(ty + th)), |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 318 | kVertexSize); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 319 | fCurrVertex += 4; |
| 320 | } |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 321 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 322 | inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) { |
| 323 | GrTextContext::init(paint, skPaint); |
| 324 | |
| 325 | fStrike = NULL; |
| 326 | |
| 327 | fCurrTexture = NULL; |
| 328 | fCurrVertex = 0; |
| 329 | |
| 330 | fVertices = NULL; |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 331 | |
commit-bot@chromium.org | dc5cd85 | 2014-04-02 19:24:32 +0000 | [diff] [blame] | 332 | if (fSkPaint.getTextSize() <= kSmallDFFontLimit) { |
| 333 | fTextRatio = fSkPaint.getTextSize()/kSmallDFFontSize; |
| 334 | fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize)); |
| 335 | } else if (fSkPaint.getTextSize() <= kMediumDFFontLimit) { |
| 336 | fTextRatio = fSkPaint.getTextSize()/kMediumDFFontSize; |
| 337 | fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize)); |
| 338 | } else { |
| 339 | fTextRatio = fSkPaint.getTextSize()/kLargeDFFontSize; |
| 340 | fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize)); |
| 341 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 342 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 343 | fUseLCDText = fSkPaint.isLCDRenderText(); |
skia.committer@gmail.com | 221b911 | 2014-04-04 03:04:32 +0000 | [diff] [blame] | 344 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 345 | fSkPaint.setLCDRenderText(false); |
| 346 | fSkPaint.setAutohinted(false); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 347 | fSkPaint.setHinting(SkPaint::kNormal_Hinting); |
commit-bot@chromium.org | 0bed43c | 2014-03-14 21:22:38 +0000 | [diff] [blame] | 348 | fSkPaint.setSubpixelText(true); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 349 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | inline void GrDistanceFieldTextContext::finish() { |
| 353 | flushGlyphs(); |
| 354 | |
| 355 | GrTextContext::finish(); |
| 356 | } |
| 357 | |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 358 | static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache, |
| 359 | const SkDeviceProperties& deviceProperties, |
| 360 | GrTexture** gammaTexture) { |
| 361 | if (NULL == *gammaTexture) { |
| 362 | int width, height; |
| 363 | size_t size; |
| 364 | |
| 365 | #ifdef SK_GAMMA_CONTRAST |
| 366 | SkScalar contrast = SK_GAMMA_CONTRAST; |
| 367 | #else |
| 368 | SkScalar contrast = 0.5f; |
| 369 | #endif |
| 370 | SkScalar paintGamma = deviceProperties.fGamma; |
| 371 | SkScalar deviceGamma = deviceProperties.fGamma; |
| 372 | |
| 373 | size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma, |
| 374 | &width, &height); |
| 375 | |
| 376 | SkAutoTArray<uint8_t> data((int)size); |
| 377 | SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get()); |
| 378 | |
| 379 | // TODO: Update this to use the cache rather than directly creating a texture. |
| 380 | GrTextureDesc desc; |
| 381 | desc.fFlags = kDynamicUpdate_GrTextureFlagBit; |
| 382 | desc.fWidth = width; |
| 383 | desc.fHeight = height; |
| 384 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 385 | |
| 386 | *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0); |
| 387 | if (NULL == *gammaTexture) { |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | context->writeTexturePixels(*gammaTexture, |
| 392 | 0, 0, width, height, |
| 393 | (*gammaTexture)->config(), data.get(), 0, |
| 394 | GrContext::kDontFlush_PixelOpsFlag); |
| 395 | } |
| 396 | } |
| 397 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 398 | void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint, |
| 399 | const char text[], size_t byteLength, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 400 | SkScalar x, SkScalar y) { |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 401 | SkASSERT(byteLength == 0 || text != NULL); |
| 402 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 403 | // nothing to draw or can't draw |
| 404 | if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/ |
| 405 | || fSkPaint.getRasterizer()) { |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 406 | return; |
| 407 | } |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 408 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 409 | this->init(paint, skPaint); |
| 410 | |
jvanverth | 1d38619 | 2014-07-25 11:31:13 -0700 | [diff] [blame^] | 411 | if (NULL == fDrawTarget) { |
| 412 | return; |
| 413 | } |
| 414 | |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 415 | SkScalar sizeRatio = fTextRatio; |
| 416 | |
| 417 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 418 | |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 419 | SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL); |
| 420 | SkGlyphCache* cache = autoCache.getCache(); |
| 421 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
jvanverth | 1d38619 | 2014-07-25 11:31:13 -0700 | [diff] [blame^] | 422 | if (NULL == fStrike) { |
| 423 | fStrike = fContext->getFontCache()->getStrike(fontScaler, true); |
| 424 | } |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 425 | |
| 426 | setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 427 | |
jvanverth | 1d38619 | 2014-07-25 11:31:13 -0700 | [diff] [blame^] | 428 | // allocate vertices |
| 429 | SkASSERT(NULL == fVertices); |
| 430 | fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( |
| 431 | SK_ARRAY_COUNT(gTextVertexAttribs)); |
| 432 | int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL); |
| 433 | bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs, |
| 434 | 0, |
| 435 | &fVertices, |
| 436 | NULL); |
| 437 | GrAlwaysAssert(success); |
| 438 | |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 439 | // need to measure first |
| 440 | // TODO - generate positions and pre-load cache as well? |
| 441 | const char* stop = text + byteLength; |
| 442 | if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
| 443 | SkFixed stopX = 0; |
| 444 | SkFixed stopY = 0; |
| 445 | |
| 446 | const char* textPtr = text; |
| 447 | while (textPtr < stop) { |
| 448 | // don't need x, y here, since all subpixel variants will have the |
| 449 | // same advance |
| 450 | const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0); |
| 451 | |
| 452 | stopX += glyph.fAdvanceX; |
| 453 | stopY += glyph.fAdvanceY; |
| 454 | } |
| 455 | SkASSERT(textPtr == stop); |
| 456 | |
| 457 | SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio; |
| 458 | SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio; |
| 459 | |
| 460 | if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 461 | alignX = SkScalarHalf(alignX); |
| 462 | alignY = SkScalarHalf(alignY); |
| 463 | } |
| 464 | |
| 465 | x -= alignX; |
| 466 | y -= alignY; |
| 467 | } |
| 468 | |
commit-bot@chromium.org | 5408f8f | 2014-05-21 19:44:24 +0000 | [diff] [blame] | 469 | SkFixed fx = SkScalarToFixed(x); |
| 470 | SkFixed fy = SkScalarToFixed(y); |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 471 | SkFixed fixedScale = SkScalarToFixed(sizeRatio); |
| 472 | while (text < stop) { |
commit-bot@chromium.org | a9dae71 | 2014-03-24 18:34:04 +0000 | [diff] [blame] | 473 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 474 | |
| 475 | if (glyph.fWidth) { |
| 476 | this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 477 | glyph.getSubXFixed(), |
| 478 | glyph.getSubYFixed()), |
commit-bot@chromium.org | 5408f8f | 2014-05-21 19:44:24 +0000 | [diff] [blame] | 479 | fx, |
| 480 | fy, |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 481 | fontScaler); |
| 482 | } |
| 483 | |
| 484 | fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale); |
| 485 | fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale); |
| 486 | } |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 487 | |
| 488 | this->finish(); |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 489 | } |
| 490 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 491 | void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint, |
| 492 | const char text[], size_t byteLength, |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 493 | const SkScalar pos[], SkScalar constY, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 494 | int scalarsPerPosition) { |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 495 | |
| 496 | SkASSERT(byteLength == 0 || text != NULL); |
| 497 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 498 | |
| 499 | // nothing to draw |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 500 | if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) { |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 504 | this->init(paint, skPaint); |
| 505 | |
jvanverth | 1d38619 | 2014-07-25 11:31:13 -0700 | [diff] [blame^] | 506 | if (NULL == fDrawTarget) { |
| 507 | return; |
| 508 | } |
| 509 | |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 510 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 511 | |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 512 | SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL); |
| 513 | SkGlyphCache* cache = autoCache.getCache(); |
| 514 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
jvanverth | 1d38619 | 2014-07-25 11:31:13 -0700 | [diff] [blame^] | 515 | if (NULL == fStrike) { |
| 516 | fStrike = fContext->getFontCache()->getStrike(fontScaler, true); |
| 517 | } |
| 518 | |
| 519 | // allocate vertices |
| 520 | SkASSERT(NULL == fVertices); |
| 521 | fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( |
| 522 | SK_ARRAY_COUNT(gTextVertexAttribs)); |
| 523 | int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL); |
| 524 | bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs, |
| 525 | 0, |
| 526 | &fVertices, |
| 527 | NULL); |
| 528 | GrAlwaysAssert(success); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 529 | |
| 530 | setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture); |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 531 | |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 532 | const char* stop = text + byteLength; |
| 533 | |
| 534 | if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
| 535 | while (text < stop) { |
| 536 | // the last 2 parameters are ignored |
| 537 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 538 | |
| 539 | if (glyph.fWidth) { |
| 540 | SkScalar x = pos[0]; |
| 541 | SkScalar y = scalarsPerPosition == 1 ? constY : pos[1]; |
| 542 | |
| 543 | this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 544 | glyph.getSubXFixed(), |
| 545 | glyph.getSubYFixed()), |
commit-bot@chromium.org | 5408f8f | 2014-05-21 19:44:24 +0000 | [diff] [blame] | 546 | SkScalarToFixed(x), |
| 547 | SkScalarToFixed(y), |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 548 | fontScaler); |
| 549 | } |
| 550 | pos += scalarsPerPosition; |
| 551 | } |
| 552 | } else { |
| 553 | int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0; |
| 554 | while (text < stop) { |
| 555 | // the last 2 parameters are ignored |
| 556 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 557 | |
| 558 | if (glyph.fWidth) { |
| 559 | SkScalar x = pos[0]; |
| 560 | SkScalar y = scalarsPerPosition == 1 ? constY : pos[1]; |
skia.committer@gmail.com | 22e9672 | 2013-12-20 07:01:36 +0000 | [diff] [blame] | 561 | |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 562 | this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 563 | glyph.getSubXFixed(), |
| 564 | glyph.getSubYFixed()), |
commit-bot@chromium.org | 5408f8f | 2014-05-21 19:44:24 +0000 | [diff] [blame] | 565 | SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift), |
| 566 | SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift), |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 567 | fontScaler); |
| 568 | } |
| 569 | pos += scalarsPerPosition; |
| 570 | } |
| 571 | } |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 572 | |
| 573 | this->finish(); |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 574 | } |