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