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