blob: 5c9019b2b2cec504ced08180303c85b1c966d845 [file] [log] [blame]
jvanverth@google.comd830d132013-11-11 20:54:09 +00001/*
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"
jvanverth8c27a182014-10-14 08:45:50 -070010#include "GrBitmapTextContext.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000011#include "GrDrawTarget.h"
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000012#include "GrDrawTargetCaps.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000013#include "GrFontScaler.h"
jvanverth2d2a68c2014-06-10 06:42:56 -070014#include "GrGpu.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000015#include "GrIndexBuffer.h"
egdanield58a0ba2014-06-11 10:30:05 -070016#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070017#include "GrTexturePriv.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000018#include "GrTextStrike.h"
19#include "GrTextStrike_impl.h"
bsalomonafbf2d62014-09-30 12:18:44 -070020
jvanverth2b9dc1d2014-10-20 06:48:59 -070021#include "SkAutoKern.h"
bsalomonafbf2d62014-09-30 12:18:44 -070022#include "SkColorFilter.h"
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +000023#include "SkDistanceFieldGen.h"
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +000024#include "SkDraw.h"
bsalomonafbf2d62014-09-30 12:18:44 -070025#include "SkGlyphCache.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000026#include "SkGpuDevice.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000027#include "SkPath.h"
28#include "SkRTConf.h"
29#include "SkStrokeRec.h"
30#include "effects/GrDistanceFieldTextureEffect.h"
31
jvanverthfeceba52014-07-25 19:03:34 -070032SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false,
33 "Dump the contents of the font cache before every purge.");
34
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +000035static const int kSmallDFFontSize = 32;
36static const int kSmallDFFontLimit = 32;
jvanverthada68ef2014-11-03 14:00:24 -080037static const int kMediumDFFontSize = 78;
38static const int kMediumDFFontLimit = 78;
39static const int kLargeDFFontSize = 192;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +000040
jvanverthfeceba52014-07-25 19:03:34 -070041namespace {
42// position + texture coord
43extern const GrVertexAttrib gTextVertexAttribs[] = {
44 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -070045 {kVec2f_GrVertexAttribType, sizeof(SkPoint) , kGeometryProcessor_GrVertexAttribBinding}
jvanverthfeceba52014-07-25 19:03:34 -070046};
47
egdaniel7b3d5ee2014-08-28 05:41:14 -070048static const size_t kTextVASize = 2 * sizeof(SkPoint);
49
jvanverthfeceba52014-07-25 19:03:34 -070050// position + color + texture coord
51extern const GrVertexAttrib gTextVertexWithColorAttribs[] = {
52 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
53 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -070054 {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kGeometryProcessor_GrVertexAttribBinding}
jvanverthfeceba52014-07-25 19:03:34 -070055};
56
egdaniel7b3d5ee2014-08-28 05:41:14 -070057static const size_t kTextVAColorSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
58
jvanverth73f10532014-10-23 11:57:12 -070059static const int kVerticesPerGlyph = 4;
60static const int kIndicesPerGlyph = 6;
jvanverthfeceba52014-07-25 19:03:34 -070061};
jvanverth@google.comd830d132013-11-11 20:54:09 +000062
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000063GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000064 const SkDeviceProperties& properties,
65 bool enable)
Mike Klein6a25bd02014-08-29 10:03:59 -040066 : GrTextContext(context, properties) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000067#if SK_FORCE_DISTANCEFIELD_FONTS
Mike Klein6a25bd02014-08-29 10:03:59 -040068 fEnableDFRendering = true;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000069#else
Mike Klein6a25bd02014-08-29 10:03:59 -040070 fEnableDFRendering = enable;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000071#endif
Mike Klein6a25bd02014-08-29 10:03:59 -040072 fStrike = NULL;
73 fGammaTexture = NULL;
74
Mike Klein6a25bd02014-08-29 10:03:59 -040075 fEffectTextureUniqueID = SK_InvalidUniqueID;
76 fEffectColor = GrColor_ILLEGAL;
jvanverth6d22eca2014-10-28 11:10:48 -070077 fEffectFlags = kInvalid_DistanceFieldEffectFlag;
Mike Klein6a25bd02014-08-29 10:03:59 -040078
79 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -070080 fCurrVertex = 0;
81 fAllocVertexCount = 0;
82 fTotalVertexCount = 0;
83 fCurrTexture = NULL;
Mike Klein6a25bd02014-08-29 10:03:59 -040084
jvanverth1723bfc2014-07-30 09:16:33 -070085 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +000086}
87
jvanverth8c27a182014-10-14 08:45:50 -070088GrDistanceFieldTextContext* GrDistanceFieldTextContext::Create(GrContext* context,
89 const SkDeviceProperties& props,
90 bool enable) {
91 GrDistanceFieldTextContext* textContext = SkNEW_ARGS(GrDistanceFieldTextContext,
92 (context, props, enable));
93 textContext->fFallbackTextContext = GrBitmapTextContext::Create(context, props);
94
95 return textContext;
96}
97
jvanverth@google.comd830d132013-11-11 20:54:09 +000098GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
jvanverth73f10532014-10-23 11:57:12 -070099 this->finish();
jvanverth2d2a68c2014-06-10 06:42:56 -0700100 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000101}
102
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000103bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000104 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +0000105 return false;
106 }
107
skia.committer@gmail.come1d94432014-04-09 03:04:11 +0000108 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +0000109 // translate well to distance
110 if (paint.getRasterizer() || paint.getMaskFilter() ||
111 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
112 return false;
113 }
114
115 // TODO: add some stroking support
116 if (paint.getStyle() != SkPaint::kFill_Style) {
117 return false;
118 }
119
120 // TODO: choose an appropriate maximum scale for distance fields and
121 // enable perspective
122 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
123 return false;
124 }
125
jvanverth2faa2282014-10-31 12:59:57 -0700126 return true;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000127}
128
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000129inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
130 GrTextContext::init(paint, skPaint);
131
132 fStrike = NULL;
133
jvanverth76ce81e2014-09-22 14:26:53 -0700134 const SkMatrix& ctm = fContext->getMatrix();
jvanverth9564ce62014-09-16 05:45:19 -0700135
136 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700137 SkASSERT(!ctm.hasPerspective());
138 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700139 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700140 SkScalar scaledTextSize = textSize;
141 // if we have non-unity scale, we need to choose our base text size
142 // based on the SkPaint's text size multiplied by the max scale factor
jvanverth9564ce62014-09-16 05:45:19 -0700143 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
144 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700145 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700146 }
147
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000148 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700149 fCurrVertex = 0;
150 fAllocVertexCount = 0;
151 fTotalVertexCount = 0;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000152
jvanverth76ce81e2014-09-22 14:26:53 -0700153 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700154 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000155 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800156#if DEBUG_TEXT_SIZE
157 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF));
158 fPaint.setColor(GrColorPackRGBA(0x00, 0x00, 0xFF, 0xFF));
159#endif
jvanverth76ce81e2014-09-22 14:26:53 -0700160 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700161 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000162 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800163#if DEBUG_TEXT_SIZE
164 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00));
165 fPaint.setColor(GrColorPackRGBA(0x00, 0xFF, 0x00, 0xFF));
166#endif
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000167 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700168 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000169 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800170#if DEBUG_TEXT_SIZE
171 fSkPaint.setColor(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00));
172 fPaint.setColor(GrColorPackRGBA(0xFF, 0x00, 0x00, 0xFF));
173#endif
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000174 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000175
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000176 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000177
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000178 fSkPaint.setLCDRenderText(false);
179 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700180 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000181 fSkPaint.setSubpixelText(true);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000182}
183
jvanverth2d2a68c2014-06-10 06:42:56 -0700184static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
185 const SkDeviceProperties& deviceProperties,
186 GrTexture** gammaTexture) {
187 if (NULL == *gammaTexture) {
188 int width, height;
189 size_t size;
190
191#ifdef SK_GAMMA_CONTRAST
192 SkScalar contrast = SK_GAMMA_CONTRAST;
193#else
194 SkScalar contrast = 0.5f;
195#endif
reedb2d77e42014-10-14 08:26:33 -0700196 SkScalar paintGamma = deviceProperties.gamma();
197 SkScalar deviceGamma = deviceProperties.gamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700198
199 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
200 &width, &height);
201
202 SkAutoTArray<uint8_t> data((int)size);
203 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
204
205 // TODO: Update this to use the cache rather than directly creating a texture.
bsalomonf2703d82014-10-28 14:33:06 -0700206 GrSurfaceDesc desc;
207 desc.fFlags = kNone_GrSurfaceFlags;
jvanverth2d2a68c2014-06-10 06:42:56 -0700208 desc.fWidth = width;
209 desc.fHeight = height;
210 desc.fConfig = kAlpha_8_GrPixelConfig;
211
212 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
213 if (NULL == *gammaTexture) {
214 return;
215 }
216
bsalomon81beccc2014-10-13 12:32:55 -0700217 (*gammaTexture)->writePixels(0, 0, width, height,
218 (*gammaTexture)->config(), data.get(), 0,
219 GrContext::kDontFlush_PixelOpsFlag);
jvanverth2d2a68c2014-06-10 06:42:56 -0700220 }
221}
222
jvanverthaab626c2014-10-16 08:04:39 -0700223void GrDistanceFieldTextContext::onDrawText(const GrPaint& paint, const SkPaint& skPaint,
224 const char text[], size_t byteLength,
225 SkScalar x, SkScalar y) {
226 SkASSERT(byteLength == 0 || text != NULL);
227
jvanverth2b9dc1d2014-10-20 06:48:59 -0700228 // nothing to draw
229 if (text == NULL || byteLength == 0) {
jvanverthaab626c2014-10-16 08:04:39 -0700230 return;
231 }
232
jvanverth2b9dc1d2014-10-20 06:48:59 -0700233 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
234 SkAutoGlyphCache autoCache(skPaint, &fDeviceProperties, NULL);
235 SkGlyphCache* cache = autoCache.getCache();
jvanverthaab626c2014-10-16 08:04:39 -0700236
jvanverth2b9dc1d2014-10-20 06:48:59 -0700237 SkTArray<SkScalar> positions;
jvanverthaab626c2014-10-16 08:04:39 -0700238
jvanverth2b9dc1d2014-10-20 06:48:59 -0700239 const char* textPtr = text;
240 SkFixed stopX = 0;
241 SkFixed stopY = 0;
242 SkFixed origin;
243 switch (skPaint.getTextAlign()) {
244 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
245 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
246 case SkPaint::kLeft_Align: origin = 0; break;
247 default: SkFAIL("Invalid paint origin"); return;
248 }
jvanverthaab626c2014-10-16 08:04:39 -0700249
jvanverth2b9dc1d2014-10-20 06:48:59 -0700250 SkAutoKern autokern;
jvanverthaab626c2014-10-16 08:04:39 -0700251 const char* stop = text + byteLength;
jvanverth2b9dc1d2014-10-20 06:48:59 -0700252 while (textPtr < stop) {
253 // don't need x, y here, since all subpixel variants will have the
254 // same advance
255 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
jvanverthaab626c2014-10-16 08:04:39 -0700256
jvanverth2b9dc1d2014-10-20 06:48:59 -0700257 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
258 positions.push_back(SkFixedToScalar(stopX + SkFixedMul_portable(origin, width)));
jvanverthaab626c2014-10-16 08:04:39 -0700259
jvanverth2b9dc1d2014-10-20 06:48:59 -0700260 SkFixed height = glyph.fAdvanceY;
261 positions.push_back(SkFixedToScalar(stopY + SkFixedMul_portable(origin, height)));
jvanverthaab626c2014-10-16 08:04:39 -0700262
jvanverth2b9dc1d2014-10-20 06:48:59 -0700263 stopX += width;
264 stopY += height;
jvanverthaab626c2014-10-16 08:04:39 -0700265 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700266 SkASSERT(textPtr == stop);
jvanverthaab626c2014-10-16 08:04:39 -0700267
jvanverth2b9dc1d2014-10-20 06:48:59 -0700268 // now adjust starting point depending on alignment
269 SkScalar alignX = SkFixedToScalar(stopX);
270 SkScalar alignY = SkFixedToScalar(stopY);
271 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
272 alignX = SkScalarHalf(alignX);
273 alignY = SkScalarHalf(alignY);
274 } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) {
275 alignX = 0;
276 alignY = 0;
jvanverthaab626c2014-10-16 08:04:39 -0700277 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700278 x -= alignX;
279 y -= alignY;
280 SkPoint offset = SkPoint::Make(x, y);
jvanverthaab626c2014-10-16 08:04:39 -0700281
jvanverth2b9dc1d2014-10-20 06:48:59 -0700282 this->drawPosText(paint, skPaint, text, byteLength, positions.begin(), 2, offset);
jvanverthaab626c2014-10-16 08:04:39 -0700283}
284
jvanverth8c27a182014-10-14 08:45:50 -0700285void GrDistanceFieldTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skPaint,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000286 const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -0700287 const SkScalar pos[], int scalarsPerPosition,
288 const SkPoint& offset) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000289
290 SkASSERT(byteLength == 0 || text != NULL);
291 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
292
293 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000294 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000295 return;
296 }
297
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000298 this->init(paint, skPaint);
299
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000300 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
301
jvanverth2d2a68c2014-06-10 06:42:56 -0700302 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
303 SkGlyphCache* cache = autoCache.getCache();
304 GrFontScaler* fontScaler = GetGrFontScaler(cache);
305
306 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000307
jvanverth73f10532014-10-23 11:57:12 -0700308 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
309 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
310
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000311 const char* stop = text + byteLength;
jvanverthfca302c2014-10-20 13:12:54 -0700312 SkTArray<char> fallbackTxt;
313 SkTArray<SkScalar> fallbackPos;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000314
315 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
316 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700317 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000318 // the last 2 parameters are ignored
319 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
320
321 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700322 SkScalar x = offset.x() + pos[0];
323 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000324
jvanverthfca302c2014-10-20 13:12:54 -0700325 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
326 glyph.getSubXFixed(),
327 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700328 x, y, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700329 // couldn't append, send to fallback
330 fallbackTxt.push_back_n(text-lastText, lastText);
331 fallbackPos.push_back(pos[0]);
332 if (2 == scalarsPerPosition) {
333 fallbackPos.push_back(pos[1]);
334 }
335 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000336 }
337 pos += scalarsPerPosition;
338 }
339 } else {
jvanverth2b9dc1d2014-10-20 06:48:59 -0700340 SkScalar alignMul = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? SK_ScalarHalf
341 : SK_Scalar1;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000342 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700343 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000344 // the last 2 parameters are ignored
345 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
346
347 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700348 SkScalar x = offset.x() + pos[0];
349 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000350
jvanverth2b9dc1d2014-10-20 06:48:59 -0700351 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX)*alignMul*fTextRatio;
352 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY)*alignMul*fTextRatio;
353
jvanverthfca302c2014-10-20 13:12:54 -0700354 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
355 glyph.getSubXFixed(),
356 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700357 x - advanceX, y - advanceY, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700358 // couldn't append, send to fallback
359 fallbackTxt.push_back_n(text-lastText, lastText);
360 fallbackPos.push_back(pos[0]);
361 if (2 == scalarsPerPosition) {
362 fallbackPos.push_back(pos[1]);
363 }
364 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000365 }
366 pos += scalarsPerPosition;
367 }
368 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000369
370 this->finish();
jvanverthfca302c2014-10-20 13:12:54 -0700371
372 if (fallbackTxt.count() > 0) {
373 fFallbackTextContext->drawPosText(paint, skPaint, fallbackTxt.begin(), fallbackTxt.count(),
374 fallbackPos.begin(), scalarsPerPosition, offset);
375 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000376}
jvanverth0fedb192014-10-08 09:07:27 -0700377
378static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
379 unsigned r = SkColorGetR(c);
380 unsigned g = SkColorGetG(c);
381 unsigned b = SkColorGetB(c);
382 return GrColorPackRGBA(r, g, b, 0xff);
383}
384
jvanverth73f10532014-10-23 11:57:12 -0700385static void* alloc_vertices(GrDrawTarget* drawTarget, int numVertices, bool useColorVerts) {
386 if (numVertices <= 0) {
387 return NULL;
388 }
389
390 // set up attributes
391 if (useColorVerts) {
392 drawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
393 SK_ARRAY_COUNT(gTextVertexWithColorAttribs), kTextVAColorSize);
394 } else {
395 drawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
396 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVASize);
397 }
398 void* vertices = NULL;
399 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
400 0,
401 &vertices,
402 NULL);
403 GrAlwaysAssert(success);
404 return vertices;
405}
406
jvanverth0fedb192014-10-08 09:07:27 -0700407void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
408 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
409 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
410
411 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
412 const SkMatrix& ctm = fContext->getMatrix();
413
414 // set up any flags
415 uint32_t flags = 0;
416 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
417 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
418 flags |= fUseLCDText && ctm.rectStaysRect() ?
419 kRectToRect_DistanceFieldEffectFlag : 0;
reedb2d77e42014-10-14 08:26:33 -0700420 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
jvanverth0fedb192014-10-08 09:07:27 -0700421 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
422
423 // see if we need to create a new effect
424 if (textureUniqueID != fEffectTextureUniqueID ||
425 filteredColor != fEffectColor ||
426 flags != fEffectFlags) {
427 if (fUseLCDText) {
428 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
429 fCachedGeometryProcessor.reset(GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
430 params,
431 fGammaTexture,
432 gammaParams,
433 colorNoPreMul,
434 flags));
435 } else {
436#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700437 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700438 filteredColor);
439 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
440 params,
441 fGammaTexture,
442 gammaParams,
443 lum/255.f,
444 flags));
445#else
jvanverth2faa2282014-10-31 12:59:57 -0700446 fCachedGeometryProcessor.reset(GrDistanceFieldNoGammaTextureEffect::Create(fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700447 params, flags));
448#endif
449 }
450 fEffectTextureUniqueID = textureUniqueID;
451 fEffectColor = filteredColor;
452 fEffectFlags = flags;
453 }
454
455}
456
jvanverthfca302c2014-10-20 13:12:54 -0700457// Returns true if this method handled the glyph, false if needs to be passed to fallback
458//
459bool GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
djsollen058f01e2014-10-30 11:54:43 -0700460 SkScalar sx, SkScalar sy,
jvanverth0fedb192014-10-08 09:07:27 -0700461 GrFontScaler* scaler) {
462 if (NULL == fDrawTarget) {
jvanverthfca302c2014-10-20 13:12:54 -0700463 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700464 }
465
466 if (NULL == fStrike) {
467 fStrike = fContext->getFontCache()->getStrike(scaler, true);
468 }
469
470 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
471 if (NULL == glyph || glyph->fBounds.isEmpty()) {
jvanverthfca302c2014-10-20 13:12:54 -0700472 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700473 }
474
jvanverthfca302c2014-10-20 13:12:54 -0700475 // fallback to color glyph support
jvanverth294c3262014-10-10 11:36:12 -0700476 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
jvanverthfca302c2014-10-20 13:12:54 -0700477 return false;
jvanverth294c3262014-10-10 11:36:12 -0700478 }
479
jvanverth2faa2282014-10-31 12:59:57 -0700480 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
481 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
482 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
483 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth0fedb192014-10-08 09:07:27 -0700484
jvanverth2faa2282014-10-31 12:59:57 -0700485 SkScalar scale = fTextRatio;
486 dx *= scale;
487 dy *= scale;
488 sx += dx;
489 sy += dy;
490 width *= scale;
491 height *= scale;
492 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
jvanverth0fedb192014-10-08 09:07:27 -0700493
494 // check if we clipped out
jvanverth2faa2282014-10-31 12:59:57 -0700495 SkRect dstRect;
496 const SkMatrix& ctm = fContext->getMatrix();
497 (void) ctm.mapRect(&dstRect, glyphRect);
498 if (fClipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
499 SkScalarTruncToInt(dstRect.top()),
500 SkScalarTruncToInt(dstRect.right()),
501 SkScalarTruncToInt(dstRect.bottom()))) {
jvanverth0fedb192014-10-08 09:07:27 -0700502// SkCLZ(3); // so we can set a break-point in the debugger
jvanverth2faa2282014-10-31 12:59:57 -0700503 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700504 }
jvanverth2faa2282014-10-31 12:59:57 -0700505
jvanverth0fedb192014-10-08 09:07:27 -0700506 if (NULL == glyph->fPlot) {
507 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
508 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
509 goto HAS_ATLAS;
510 }
511
512 // try to clear out an unused plot before we flush
jvanverth294c3262014-10-10 11:36:12 -0700513 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700514 fStrike->addGlyphToAtlas(glyph, scaler)) {
515 goto HAS_ATLAS;
516 }
517
518 if (c_DumpFontCache) {
519#ifdef SK_DEVELOPER
520 fContext->getFontCache()->dump();
521#endif
522 }
523
524 // before we purge the cache, we must flush any accumulated draws
525 this->flush();
526 fContext->flush();
527
528 // we should have an unused plot now
jvanverth294c3262014-10-10 11:36:12 -0700529 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700530 fStrike->addGlyphToAtlas(glyph, scaler)) {
531 goto HAS_ATLAS;
532 }
533 }
534
535 if (NULL == glyph->fPath) {
536 SkPath* path = SkNEW(SkPath);
537 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
538 // flag the glyph as being dead?
539 delete path;
jvanverthfca302c2014-10-20 13:12:54 -0700540 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700541 }
542 glyph->fPath = path;
543 }
544
bsalomonec87dc62014-10-14 10:52:00 -0700545 // flush any accumulated draws before drawing this glyph as a path.
546 this->flush();
547
jvanverth0fedb192014-10-08 09:07:27 -0700548 GrContext::AutoMatrix am;
549 SkMatrix ctm;
550 ctm.setScale(fTextRatio, fTextRatio);
551 ctm.postTranslate(sx, sy);
552 GrPaint tmpPaint(fPaint);
553 am.setPreConcat(fContext, ctm, &tmpPaint);
554 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
555 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth73f10532014-10-23 11:57:12 -0700556
557 // remove this glyph from the vertices we need to allocate
558 fTotalVertexCount -= kVerticesPerGlyph;
jvanverthfca302c2014-10-20 13:12:54 -0700559 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700560 }
561
562HAS_ATLAS:
563 SkASSERT(glyph->fPlot);
564 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
565 glyph->fPlot->setDrawToken(drawToken);
566
567 GrTexture* texture = glyph->fPlot->texture();
568 SkASSERT(texture);
569
jvanverth73f10532014-10-23 11:57:12 -0700570 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fTotalVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700571 this->flush();
572 fCurrTexture = texture;
573 fCurrTexture->ref();
574 }
575
576 bool useColorVerts = !fUseLCDText;
577
578 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700579 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
580 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
581 fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700582 }
583
jvanverth0fedb192014-10-08 09:07:27 -0700584 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
585 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
586 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
587 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
588
jvanverth2faa2282014-10-31 12:59:57 -0700589 fVertexBounds.joinNonEmptyArg(glyphRect);
jvanverth0fedb192014-10-08 09:07:27 -0700590
591 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
592 : (2 * sizeof(SkPoint) + sizeof(GrColor));
593
594 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride());
595
596 SkPoint* positions = reinterpret_cast<SkPoint*>(
597 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth2faa2282014-10-31 12:59:57 -0700598 positions->setRectFan(glyphRect.fLeft, glyphRect.fTop, glyphRect.fRight, glyphRect.fBottom,
599 vertSize);
jvanverth0fedb192014-10-08 09:07:27 -0700600
601 // The texture coords are last in both the with and without color vertex layouts.
602 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
603 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
604 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
605 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
606 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
607 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
608 vertSize);
609 if (useColorVerts) {
610 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
611 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
612 }
613 // color comes after position.
614 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
615 for (int i = 0; i < 4; ++i) {
616 *colors = fPaint.getColor();
617 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
618 }
619 }
620
621 fCurrVertex += 4;
jvanverthfca302c2014-10-20 13:12:54 -0700622
623 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700624}
625
626void GrDistanceFieldTextContext::flush() {
627 if (NULL == fDrawTarget) {
628 return;
629 }
630
631 GrDrawState* drawState = fDrawTarget->drawState();
632 GrDrawState::AutoRestoreEffects are(drawState);
633
634 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
635
636 if (fCurrVertex > 0) {
637 // setup our sampler state for our text texture/atlas
638 SkASSERT(SkIsAlign4(fCurrVertex));
639
640 // get our current color
641 SkColor filteredColor;
642 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
643 if (colorFilter) {
644 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
645 } else {
646 filteredColor = fSkPaint.getColor();
647 }
648 this->setupCoverageEffect(filteredColor);
649
650 // Effects could be stored with one of the cache objects (atlas?)
651 drawState->setGeometryProcessor(fCachedGeometryProcessor.get());
652
653 // Set draw state
654 if (fUseLCDText) {
655 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
656 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
657 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
658 fPaint.numColorStages()) {
tfarina38406c82014-10-31 07:11:12 -0700659 SkDebugf("LCD Text will not draw correctly.\n");
jvanverth0fedb192014-10-08 09:07:27 -0700660 }
661 SkASSERT(!drawState->hasColorVertexAttribute());
662 // We don't use the GrPaint's color in this case because it's been premultiplied by
663 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
664 // the mask texture color. The end result is that we get
665 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
666 int a = SkColorGetA(fSkPaint.getColor());
667 // paintAlpha
668 drawState->setColor(SkColorSetARGB(a, a, a, a));
669 // paintColor
670 drawState->setBlendConstant(colorNoPreMul);
671 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
672 } else {
673 // set back to normal in case we took LCD path previously.
674 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
675 // We're using per-vertex color.
676 SkASSERT(drawState->hasColorVertexAttribute());
677 }
jvanverth73f10532014-10-23 11:57:12 -0700678 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700679 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
680 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
681 nGlyphs,
jvanverth73f10532014-10-23 11:57:12 -0700682 kVerticesPerGlyph, kIndicesPerGlyph, &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700683 fDrawTarget->resetVertexSource();
684 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700685 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700686 fCurrVertex = 0;
687 SkSafeSetNull(fCurrTexture);
688 fVertexBounds.setLargestInverted();
689 }
690}
691
692inline void GrDistanceFieldTextContext::finish() {
693 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700694 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700695
696 GrTextContext::finish();
697}
698