blob: f296244d3e15932032a5fa52c736bfe56390a079 [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
jvanverth73f10532014-10-23 11:57:12 -070041static const int kVerticesPerGlyph = 4;
42static const int kIndicesPerGlyph = 6;
jvanverth@google.comd830d132013-11-11 20:54:09 +000043
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000044GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000045 const SkDeviceProperties& properties,
46 bool enable)
Mike Klein6a25bd02014-08-29 10:03:59 -040047 : GrTextContext(context, properties) {
jvanverth4736e142014-11-07 07:12:46 -080048#if SK_FORCE_DISTANCE_FIELD_TEXT
Mike Klein6a25bd02014-08-29 10:03:59 -040049 fEnableDFRendering = true;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000050#else
Mike Klein6a25bd02014-08-29 10:03:59 -040051 fEnableDFRendering = enable;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000052#endif
Mike Klein6a25bd02014-08-29 10:03:59 -040053 fStrike = NULL;
54 fGammaTexture = NULL;
55
Mike Klein6a25bd02014-08-29 10:03:59 -040056 fEffectTextureUniqueID = SK_InvalidUniqueID;
57 fEffectColor = GrColor_ILLEGAL;
jvanverth6d22eca2014-10-28 11:10:48 -070058 fEffectFlags = kInvalid_DistanceFieldEffectFlag;
Mike Klein6a25bd02014-08-29 10:03:59 -040059
60 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -070061 fCurrVertex = 0;
62 fAllocVertexCount = 0;
63 fTotalVertexCount = 0;
64 fCurrTexture = NULL;
Mike Klein6a25bd02014-08-29 10:03:59 -040065
jvanverth1723bfc2014-07-30 09:16:33 -070066 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +000067}
68
jvanverth8c27a182014-10-14 08:45:50 -070069GrDistanceFieldTextContext* 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.comd830d132013-11-11 20:54:09 +000079GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
jvanverth2d2a68c2014-06-10 06:42:56 -070080 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +000081}
82
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000083bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000084 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000085 return false;
86 }
87
skia.committer@gmail.come1d94432014-04-09 03:04:11 +000088 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000089 // 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
jvanverth2faa2282014-10-31 12:59:57 -0700106 return true;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000107}
108
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000109inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
110 GrTextContext::init(paint, skPaint);
111
112 fStrike = NULL;
113
jvanverth76ce81e2014-09-22 14:26:53 -0700114 const SkMatrix& ctm = fContext->getMatrix();
jvanverth9564ce62014-09-16 05:45:19 -0700115
116 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700117 SkASSERT(!ctm.hasPerspective());
118 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700119 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700120 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
jvanverth9564ce62014-09-16 05:45:19 -0700123 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
124 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700125 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700126 }
127
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000128 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700129 fCurrVertex = 0;
130 fAllocVertexCount = 0;
131 fTotalVertexCount = 0;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000132
jvanverth76ce81e2014-09-22 14:26:53 -0700133 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700134 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000135 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800136#if DEBUG_TEXT_SIZE
137 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF));
138 fPaint.setColor(GrColorPackRGBA(0x00, 0x00, 0xFF, 0xFF));
139#endif
jvanverth76ce81e2014-09-22 14:26:53 -0700140 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700141 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000142 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800143#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.orgdc5cd852014-04-02 19:24:32 +0000147 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700148 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000149 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800150#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.orgdc5cd852014-04-02 19:24:32 +0000154 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000155
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000156 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000157
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000158 fSkPaint.setLCDRenderText(false);
159 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700160 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000161 fSkPaint.setSubpixelText(true);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000162}
163
jvanverth2d2a68c2014-06-10 06:42:56 -0700164static 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
reedb2d77e42014-10-14 08:26:33 -0700176 SkScalar paintGamma = deviceProperties.gamma();
177 SkScalar deviceGamma = deviceProperties.gamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700178
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.
bsalomonf2703d82014-10-28 14:33:06 -0700186 GrSurfaceDesc desc;
187 desc.fFlags = kNone_GrSurfaceFlags;
jvanverth2d2a68c2014-06-10 06:42:56 -0700188 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
bsalomon81beccc2014-10-13 12:32:55 -0700197 (*gammaTexture)->writePixels(0, 0, width, height,
198 (*gammaTexture)->config(), data.get(), 0,
199 GrContext::kDontFlush_PixelOpsFlag);
jvanverth2d2a68c2014-06-10 06:42:56 -0700200 }
201}
202
jvanverthaab626c2014-10-16 08:04:39 -0700203void 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
jvanverth2b9dc1d2014-10-20 06:48:59 -0700208 // nothing to draw
209 if (text == NULL || byteLength == 0) {
jvanverthaab626c2014-10-16 08:04:39 -0700210 return;
211 }
212
jvanverth2b9dc1d2014-10-20 06:48:59 -0700213 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
214 SkAutoGlyphCache autoCache(skPaint, &fDeviceProperties, NULL);
215 SkGlyphCache* cache = autoCache.getCache();
jvanverthaab626c2014-10-16 08:04:39 -0700216
jvanverth2b9dc1d2014-10-20 06:48:59 -0700217 SkTArray<SkScalar> positions;
jvanverthaab626c2014-10-16 08:04:39 -0700218
jvanverth2b9dc1d2014-10-20 06:48:59 -0700219 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 }
jvanverthaab626c2014-10-16 08:04:39 -0700229
jvanverth2b9dc1d2014-10-20 06:48:59 -0700230 SkAutoKern autokern;
jvanverthaab626c2014-10-16 08:04:39 -0700231 const char* stop = text + byteLength;
jvanverth2b9dc1d2014-10-20 06:48:59 -0700232 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);
jvanverthaab626c2014-10-16 08:04:39 -0700236
jvanverth2b9dc1d2014-10-20 06:48:59 -0700237 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
238 positions.push_back(SkFixedToScalar(stopX + SkFixedMul_portable(origin, width)));
jvanverthaab626c2014-10-16 08:04:39 -0700239
jvanverth2b9dc1d2014-10-20 06:48:59 -0700240 SkFixed height = glyph.fAdvanceY;
241 positions.push_back(SkFixedToScalar(stopY + SkFixedMul_portable(origin, height)));
jvanverthaab626c2014-10-16 08:04:39 -0700242
jvanverth2b9dc1d2014-10-20 06:48:59 -0700243 stopX += width;
244 stopY += height;
jvanverthaab626c2014-10-16 08:04:39 -0700245 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700246 SkASSERT(textPtr == stop);
jvanverthaab626c2014-10-16 08:04:39 -0700247
jvanverth2b9dc1d2014-10-20 06:48:59 -0700248 // 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;
jvanverthaab626c2014-10-16 08:04:39 -0700257 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700258 x -= alignX;
259 y -= alignY;
260 SkPoint offset = SkPoint::Make(x, y);
jvanverthaab626c2014-10-16 08:04:39 -0700261
jvanverth2b9dc1d2014-10-20 06:48:59 -0700262 this->drawPosText(paint, skPaint, text, byteLength, positions.begin(), 2, offset);
jvanverthaab626c2014-10-16 08:04:39 -0700263}
264
jvanverth8c27a182014-10-14 08:45:50 -0700265void GrDistanceFieldTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skPaint,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000266 const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -0700267 const SkScalar pos[], int scalarsPerPosition,
268 const SkPoint& offset) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000269
270 SkASSERT(byteLength == 0 || text != NULL);
271 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
272
273 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000274 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000275 return;
276 }
277
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000278 this->init(paint, skPaint);
279
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000280 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
281
jvanverth2d2a68c2014-06-10 06:42:56 -0700282 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.come5d70152014-01-29 07:01:48 +0000287
jvanverth73f10532014-10-23 11:57:12 -0700288 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
289 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
290
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000291 const char* stop = text + byteLength;
jvanverthfca302c2014-10-20 13:12:54 -0700292 SkTArray<char> fallbackTxt;
293 SkTArray<SkScalar> fallbackPos;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000294
295 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
296 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700297 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000298 // the last 2 parameters are ignored
299 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
300
301 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700302 SkScalar x = offset.x() + pos[0];
303 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000304
jvanverthfca302c2014-10-20 13:12:54 -0700305 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
306 glyph.getSubXFixed(),
307 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700308 x, y, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700309 // 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.org8128d8c2013-12-19 16:12:25 +0000316 }
317 pos += scalarsPerPosition;
318 }
319 } else {
jvanverth2b9dc1d2014-10-20 06:48:59 -0700320 SkScalar alignMul = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? SK_ScalarHalf
321 : SK_Scalar1;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000322 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700323 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000324 // the last 2 parameters are ignored
325 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
326
327 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700328 SkScalar x = offset.x() + pos[0];
329 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000330
jvanverth2b9dc1d2014-10-20 06:48:59 -0700331 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX)*alignMul*fTextRatio;
332 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY)*alignMul*fTextRatio;
333
jvanverthfca302c2014-10-20 13:12:54 -0700334 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
335 glyph.getSubXFixed(),
336 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700337 x - advanceX, y - advanceY, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700338 // 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.org8128d8c2013-12-19 16:12:25 +0000345 }
346 pos += scalarsPerPosition;
347 }
348 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000349
350 this->finish();
jvanverthfca302c2014-10-20 13:12:54 -0700351
352 if (fallbackTxt.count() > 0) {
353 fFallbackTextContext->drawPosText(paint, skPaint, fallbackTxt.begin(), fallbackTxt.count(),
354 fallbackPos.begin(), scalarsPerPosition, offset);
355 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000356}
jvanverth0fedb192014-10-08 09:07:27 -0700357
358static 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
joshualitt9853cce2014-11-17 14:22:48 -0800365static size_t get_vertex_stride(bool useColorVerts) {
366 return useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) :
367 (2 * sizeof(SkPoint));
368}
369
370static void* alloc_vertices(GrDrawTarget* drawTarget,
371 int numVertices,
372 bool useColorVerts) {
jvanverth73f10532014-10-23 11:57:12 -0700373 if (numVertices <= 0) {
374 return NULL;
375 }
376
jvanverth73f10532014-10-23 11:57:12 -0700377 void* vertices = NULL;
378 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
joshualitt9853cce2014-11-17 14:22:48 -0800379 get_vertex_stride(useColorVerts),
jvanverth73f10532014-10-23 11:57:12 -0700380 0,
381 &vertices,
382 NULL);
383 GrAlwaysAssert(success);
384 return vertices;
385}
386
jvanverth0fedb192014-10-08 09:07:27 -0700387void 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;
reedb2d77e42014-10-14 08:26:33 -0700400 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
jvanverth0fedb192014-10-08 09:07:27 -0700401 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 {
joshualitt2dd1ae02014-12-03 06:24:10 -0800416 flags |= kColorAttr_DistanceFieldEffectFlag;
jvanverth0fedb192014-10-08 09:07:27 -0700417#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700418 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700419 filteredColor);
420 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
421 params,
422 fGammaTexture,
423 gammaParams,
424 lum/255.f,
425 flags));
426#else
jvanverth2faa2282014-10-31 12:59:57 -0700427 fCachedGeometryProcessor.reset(GrDistanceFieldNoGammaTextureEffect::Create(fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700428 params, flags));
429#endif
430 }
431 fEffectTextureUniqueID = textureUniqueID;
432 fEffectColor = filteredColor;
433 fEffectFlags = flags;
434 }
435
436}
437
jvanverthfca302c2014-10-20 13:12:54 -0700438// Returns true if this method handled the glyph, false if needs to be passed to fallback
439//
440bool GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
djsollen058f01e2014-10-30 11:54:43 -0700441 SkScalar sx, SkScalar sy,
jvanverth0fedb192014-10-08 09:07:27 -0700442 GrFontScaler* scaler) {
443 if (NULL == fDrawTarget) {
jvanverthfca302c2014-10-20 13:12:54 -0700444 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700445 }
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()) {
jvanverthfca302c2014-10-20 13:12:54 -0700453 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700454 }
455
jvanverthfca302c2014-10-20 13:12:54 -0700456 // fallback to color glyph support
jvanverth294c3262014-10-10 11:36:12 -0700457 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
jvanverthfca302c2014-10-20 13:12:54 -0700458 return false;
jvanverth294c3262014-10-10 11:36:12 -0700459 }
460
jvanverth2faa2282014-10-31 12:59:57 -0700461 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);
jvanverth0fedb192014-10-08 09:07:27 -0700465
jvanverth2faa2282014-10-31 12:59:57 -0700466 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);
jvanverth0fedb192014-10-08 09:07:27 -0700474
475 // check if we clipped out
jvanverth2faa2282014-10-31 12:59:57 -0700476 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()))) {
jvanverth0fedb192014-10-08 09:07:27 -0700483// SkCLZ(3); // so we can set a break-point in the debugger
jvanverth2faa2282014-10-31 12:59:57 -0700484 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700485 }
jvanverth2faa2282014-10-31 12:59:57 -0700486
jvanverth0fedb192014-10-08 09:07:27 -0700487 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
jvanverth294c3262014-10-10 11:36:12 -0700494 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700495 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
jvanverth294c3262014-10-10 11:36:12 -0700510 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700511 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;
jvanverthfca302c2014-10-20 13:12:54 -0700521 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700522 }
523 glyph->fPath = path;
524 }
525
bsalomonec87dc62014-10-14 10:52:00 -0700526 // flush any accumulated draws before drawing this glyph as a path.
527 this->flush();
528
jvanverth0fedb192014-10-08 09:07:27 -0700529 GrContext::AutoMatrix am;
530 SkMatrix ctm;
531 ctm.setScale(fTextRatio, fTextRatio);
jvanverth0e66aaa2014-11-04 13:32:53 -0800532 ctm.postTranslate(sx - dx, sy - dy);
jvanverth0fedb192014-10-08 09:07:27 -0700533 GrPaint tmpPaint(fPaint);
534 am.setPreConcat(fContext, ctm, &tmpPaint);
535 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
536 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth73f10532014-10-23 11:57:12 -0700537
538 // remove this glyph from the vertices we need to allocate
539 fTotalVertexCount -= kVerticesPerGlyph;
jvanverthfca302c2014-10-20 13:12:54 -0700540 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700541 }
542
543HAS_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
jvanverth73f10532014-10-23 11:57:12 -0700551 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fTotalVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700552 this->flush();
553 fCurrTexture = texture;
554 fCurrTexture->ref();
555 }
556
557 bool useColorVerts = !fUseLCDText;
558
559 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700560 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
561 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
joshualitt9853cce2014-11-17 14:22:48 -0800562 fVertices = alloc_vertices(fDrawTarget,
563 fAllocVertexCount,
564 useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700565 }
566
jvanverth0fedb192014-10-08 09:07:27 -0700567 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
jvanverth2faa2282014-10-31 12:59:57 -0700572 fVertexBounds.joinNonEmptyArg(glyphRect);
jvanverth0fedb192014-10-08 09:07:27 -0700573
joshualitt9853cce2014-11-17 14:22:48 -0800574 size_t vertSize = get_vertex_stride(useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700575
576 SkPoint* positions = reinterpret_cast<SkPoint*>(
577 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth2faa2282014-10-31 12:59:57 -0700578 positions->setRectFan(glyphRect.fLeft, glyphRect.fTop, glyphRect.fRight, glyphRect.fBottom,
579 vertSize);
jvanverth0fedb192014-10-08 09:07:27 -0700580
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) {
jvanverth0fedb192014-10-08 09:07:27 -0700590 // 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;
jvanverthfca302c2014-10-20 13:12:54 -0700599
600 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700601}
602
603void GrDistanceFieldTextContext::flush() {
604 if (NULL == fDrawTarget) {
605 return;
606 }
607
jvanverth0fedb192014-10-08 09:07:27 -0700608 if (fCurrVertex > 0) {
joshualitt780b11e2014-11-18 09:40:40 -0800609 GrDrawState drawState;
610 drawState.setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
joshualitt780b11e2014-11-18 09:40:40 -0800611
jvanverth0fedb192014-10-08 09:07:27 -0700612 // 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?)
joshualitt9853cce2014-11-17 14:22:48 -0800626 drawState.setGeometryProcessor(fCachedGeometryProcessor.get());
jvanverth0fedb192014-10-08 09:07:27 -0700627
628 // Set draw state
629 if (fUseLCDText) {
630 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
egdaniel378092f2014-12-03 10:40:13 -0800631
632 // TODO: move supportsRGBCoverage check to setupCoverageEffect and only add LCD
633 // processor if the xp can support it. For now we will simply assume that if
634 // fUseLCDText is true, then we have a known color output.
635 if (!drawState.getXPFactory()->supportsRGBCoverage(0, kRGBA_GrColorComponentFlags)) {
tfarina38406c82014-10-31 07:11:12 -0700636 SkDebugf("LCD Text will not draw correctly.\n");
jvanverth0fedb192014-10-08 09:07:27 -0700637 }
joshualitt9853cce2014-11-17 14:22:48 -0800638 SkASSERT(!drawState.hasColorVertexAttribute());
jvanverth0fedb192014-10-08 09:07:27 -0700639 // We don't use the GrPaint's color in this case because it's been premultiplied by
640 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
641 // the mask texture color. The end result is that we get
642 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
643 int a = SkColorGetA(fSkPaint.getColor());
644 // paintAlpha
joshualitt9853cce2014-11-17 14:22:48 -0800645 drawState.setColor(SkColorSetARGB(a, a, a, a));
jvanverth0fedb192014-10-08 09:07:27 -0700646 // paintColor
joshualitt9853cce2014-11-17 14:22:48 -0800647 drawState.setBlendConstant(colorNoPreMul);
648 drawState.setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
jvanverth0fedb192014-10-08 09:07:27 -0700649 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800650 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
651 drawState.setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
652 }
jvanverth0fedb192014-10-08 09:07:27 -0700653 // set back to normal in case we took LCD path previously.
joshualitt9853cce2014-11-17 14:22:48 -0800654 drawState.setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
jvanverth0fedb192014-10-08 09:07:27 -0700655 // We're using per-vertex color.
joshualitt9853cce2014-11-17 14:22:48 -0800656 SkASSERT(drawState.hasColorVertexAttribute());
jvanverth0fedb192014-10-08 09:07:27 -0700657 }
jvanverth73f10532014-10-23 11:57:12 -0700658 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700659 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
joshualitt9853cce2014-11-17 14:22:48 -0800660 fDrawTarget->drawIndexedInstances(&drawState,
661 kTriangles_GrPrimitiveType,
jvanverth0fedb192014-10-08 09:07:27 -0700662 nGlyphs,
joshualitt9853cce2014-11-17 14:22:48 -0800663 kVerticesPerGlyph,
664 kIndicesPerGlyph,
665 &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700666 fDrawTarget->resetVertexSource();
667 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700668 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700669 fCurrVertex = 0;
670 SkSafeSetNull(fCurrTexture);
671 fVertexBounds.setLargestInverted();
672 }
673}
674
675inline void GrDistanceFieldTextContext::finish() {
676 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700677 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700678
679 GrTextContext::finish();
680}
681