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