blob: d35ba233e720c12f87f228c7c9f4545aed27aee7 [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"
jvanverth787cdf92014-12-04 10:46:50 -080013#include "GrFontCache.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000014#include "GrFontScaler.h"
jvanverth2d2a68c2014-06-10 06:42:56 -070015#include "GrGpu.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000016#include "GrIndexBuffer.h"
egdanield58a0ba2014-06-11 10:30:05 -070017#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070018#include "GrTexturePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019
jvanverth2b9dc1d2014-10-20 06:48:59 -070020#include "SkAutoKern.h"
bsalomonafbf2d62014-09-30 12:18:44 -070021#include "SkColorFilter.h"
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +000022#include "SkDistanceFieldGen.h"
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +000023#include "SkDraw.h"
bsalomonafbf2d62014-09-30 12:18:44 -070024#include "SkGlyphCache.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000025#include "SkGpuDevice.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000026#include "SkPath.h"
27#include "SkRTConf.h"
28#include "SkStrokeRec.h"
29#include "effects/GrDistanceFieldTextureEffect.h"
30
jvanverthfeceba52014-07-25 19:03:34 -070031SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false,
32 "Dump the contents of the font cache before every purge.");
33
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +000034static const int kSmallDFFontSize = 32;
35static const int kSmallDFFontLimit = 32;
jvanverthada68ef2014-11-03 14:00:24 -080036static const int kMediumDFFontSize = 78;
37static const int kMediumDFFontLimit = 78;
38static const int kLargeDFFontSize = 192;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +000039
jvanverth73f10532014-10-23 11:57:12 -070040static const int kVerticesPerGlyph = 4;
41static const int kIndicesPerGlyph = 6;
jvanverth@google.comd830d132013-11-11 20:54:09 +000042
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000043GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000044 const SkDeviceProperties& properties,
45 bool enable)
Mike Klein6a25bd02014-08-29 10:03:59 -040046 : GrTextContext(context, properties) {
jvanverth4736e142014-11-07 07:12:46 -080047#if SK_FORCE_DISTANCE_FIELD_TEXT
Mike Klein6a25bd02014-08-29 10:03:59 -040048 fEnableDFRendering = true;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000049#else
Mike Klein6a25bd02014-08-29 10:03:59 -040050 fEnableDFRendering = enable;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000051#endif
Mike Klein6a25bd02014-08-29 10:03:59 -040052 fStrike = NULL;
53 fGammaTexture = NULL;
54
Mike Klein6a25bd02014-08-29 10:03:59 -040055 fEffectTextureUniqueID = SK_InvalidUniqueID;
56 fEffectColor = GrColor_ILLEGAL;
jvanverth6d22eca2014-10-28 11:10:48 -070057 fEffectFlags = kInvalid_DistanceFieldEffectFlag;
Mike Klein6a25bd02014-08-29 10:03:59 -040058
59 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -070060 fCurrVertex = 0;
61 fAllocVertexCount = 0;
62 fTotalVertexCount = 0;
63 fCurrTexture = NULL;
Mike Klein6a25bd02014-08-29 10:03:59 -040064
jvanverth1723bfc2014-07-30 09:16:33 -070065 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +000066}
67
jvanverth8c27a182014-10-14 08:45:50 -070068GrDistanceFieldTextContext* 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.comd830d132013-11-11 20:54:09 +000078GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
jvanverth2d2a68c2014-06-10 06:42:56 -070079 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +000080}
81
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000082bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000083 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000084 return false;
85 }
86
skia.committer@gmail.come1d94432014-04-09 03:04:11 +000087 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000088 // 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
jvanverth2faa2282014-10-31 12:59:57 -0700105 return true;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000106}
107
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000108inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
109 GrTextContext::init(paint, skPaint);
110
111 fStrike = NULL;
112
jvanverth76ce81e2014-09-22 14:26:53 -0700113 const SkMatrix& ctm = fContext->getMatrix();
jvanverth9564ce62014-09-16 05:45:19 -0700114
115 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700116 SkASSERT(!ctm.hasPerspective());
117 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700118 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700119 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
jvanverth9564ce62014-09-16 05:45:19 -0700122 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
123 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700124 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700125 }
126
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000127 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700128 fCurrVertex = 0;
129 fAllocVertexCount = 0;
130 fTotalVertexCount = 0;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000131
jvanverth76ce81e2014-09-22 14:26:53 -0700132 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700133 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000134 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800135#if DEBUG_TEXT_SIZE
136 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF));
137 fPaint.setColor(GrColorPackRGBA(0x00, 0x00, 0xFF, 0xFF));
138#endif
jvanverth76ce81e2014-09-22 14:26:53 -0700139 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700140 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000141 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800142#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.orgdc5cd852014-04-02 19:24:32 +0000146 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700147 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000148 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800149#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.orgdc5cd852014-04-02 19:24:32 +0000153 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000154
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000155 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000156
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000157 fSkPaint.setLCDRenderText(false);
158 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700159 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000160 fSkPaint.setSubpixelText(true);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000161}
162
jvanverth2d2a68c2014-06-10 06:42:56 -0700163static 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
reedb2d77e42014-10-14 08:26:33 -0700175 SkScalar paintGamma = deviceProperties.gamma();
176 SkScalar deviceGamma = deviceProperties.gamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700177
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.
bsalomonf2703d82014-10-28 14:33:06 -0700185 GrSurfaceDesc desc;
186 desc.fFlags = kNone_GrSurfaceFlags;
jvanverth2d2a68c2014-06-10 06:42:56 -0700187 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
bsalomon81beccc2014-10-13 12:32:55 -0700196 (*gammaTexture)->writePixels(0, 0, width, height,
197 (*gammaTexture)->config(), data.get(), 0,
198 GrContext::kDontFlush_PixelOpsFlag);
jvanverth2d2a68c2014-06-10 06:42:56 -0700199 }
200}
201
jvanverthaab626c2014-10-16 08:04:39 -0700202void 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
jvanverth2b9dc1d2014-10-20 06:48:59 -0700207 // nothing to draw
208 if (text == NULL || byteLength == 0) {
jvanverthaab626c2014-10-16 08:04:39 -0700209 return;
210 }
211
jvanverth2b9dc1d2014-10-20 06:48:59 -0700212 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
213 SkAutoGlyphCache autoCache(skPaint, &fDeviceProperties, NULL);
214 SkGlyphCache* cache = autoCache.getCache();
jvanverthaab626c2014-10-16 08:04:39 -0700215
jvanverth2b9dc1d2014-10-20 06:48:59 -0700216 SkTArray<SkScalar> positions;
jvanverthaab626c2014-10-16 08:04:39 -0700217
jvanverth2b9dc1d2014-10-20 06:48:59 -0700218 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 }
jvanverthaab626c2014-10-16 08:04:39 -0700228
jvanverth2b9dc1d2014-10-20 06:48:59 -0700229 SkAutoKern autokern;
jvanverthaab626c2014-10-16 08:04:39 -0700230 const char* stop = text + byteLength;
jvanverth2b9dc1d2014-10-20 06:48:59 -0700231 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);
jvanverthaab626c2014-10-16 08:04:39 -0700235
jvanverth2b9dc1d2014-10-20 06:48:59 -0700236 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
237 positions.push_back(SkFixedToScalar(stopX + SkFixedMul_portable(origin, width)));
jvanverthaab626c2014-10-16 08:04:39 -0700238
jvanverth2b9dc1d2014-10-20 06:48:59 -0700239 SkFixed height = glyph.fAdvanceY;
240 positions.push_back(SkFixedToScalar(stopY + SkFixedMul_portable(origin, height)));
jvanverthaab626c2014-10-16 08:04:39 -0700241
jvanverth2b9dc1d2014-10-20 06:48:59 -0700242 stopX += width;
243 stopY += height;
jvanverthaab626c2014-10-16 08:04:39 -0700244 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700245 SkASSERT(textPtr == stop);
jvanverthaab626c2014-10-16 08:04:39 -0700246
jvanverth2b9dc1d2014-10-20 06:48:59 -0700247 // 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;
jvanverthaab626c2014-10-16 08:04:39 -0700256 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700257 x -= alignX;
258 y -= alignY;
259 SkPoint offset = SkPoint::Make(x, y);
jvanverthaab626c2014-10-16 08:04:39 -0700260
jvanverth2b9dc1d2014-10-20 06:48:59 -0700261 this->drawPosText(paint, skPaint, text, byteLength, positions.begin(), 2, offset);
jvanverthaab626c2014-10-16 08:04:39 -0700262}
263
jvanverth8c27a182014-10-14 08:45:50 -0700264void GrDistanceFieldTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skPaint,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000265 const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -0700266 const SkScalar pos[], int scalarsPerPosition,
267 const SkPoint& offset) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000268
269 SkASSERT(byteLength == 0 || text != NULL);
270 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
271
272 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000273 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000274 return;
275 }
276
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000277 this->init(paint, skPaint);
278
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000279 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
280
jvanverth2d2a68c2014-06-10 06:42:56 -0700281 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.come5d70152014-01-29 07:01:48 +0000286
jvanverth73f10532014-10-23 11:57:12 -0700287 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
288 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
289
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000290 const char* stop = text + byteLength;
jvanverthfca302c2014-10-20 13:12:54 -0700291 SkTArray<char> fallbackTxt;
292 SkTArray<SkScalar> fallbackPos;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000293
294 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
295 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700296 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000297 // the last 2 parameters are ignored
298 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
299
300 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700301 SkScalar x = offset.x() + pos[0];
302 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000303
jvanverthfca302c2014-10-20 13:12:54 -0700304 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
305 glyph.getSubXFixed(),
306 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700307 x, y, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700308 // 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.org8128d8c2013-12-19 16:12:25 +0000315 }
316 pos += scalarsPerPosition;
317 }
318 } else {
jvanverth2b9dc1d2014-10-20 06:48:59 -0700319 SkScalar alignMul = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? SK_ScalarHalf
320 : SK_Scalar1;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000321 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700322 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000323 // the last 2 parameters are ignored
324 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
325
326 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700327 SkScalar x = offset.x() + pos[0];
328 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000329
jvanverth2b9dc1d2014-10-20 06:48:59 -0700330 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX)*alignMul*fTextRatio;
331 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY)*alignMul*fTextRatio;
332
jvanverthfca302c2014-10-20 13:12:54 -0700333 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
334 glyph.getSubXFixed(),
335 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700336 x - advanceX, y - advanceY, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700337 // 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.org8128d8c2013-12-19 16:12:25 +0000344 }
345 pos += scalarsPerPosition;
346 }
347 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000348
349 this->finish();
jvanverthfca302c2014-10-20 13:12:54 -0700350
351 if (fallbackTxt.count() > 0) {
352 fFallbackTextContext->drawPosText(paint, skPaint, fallbackTxt.begin(), fallbackTxt.count(),
353 fallbackPos.begin(), scalarsPerPosition, offset);
354 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000355}
jvanverth0fedb192014-10-08 09:07:27 -0700356
357static 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
joshualitt9853cce2014-11-17 14:22:48 -0800364static size_t get_vertex_stride(bool useColorVerts) {
365 return useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) :
366 (2 * sizeof(SkPoint));
367}
368
369static void* alloc_vertices(GrDrawTarget* drawTarget,
370 int numVertices,
371 bool useColorVerts) {
jvanverth73f10532014-10-23 11:57:12 -0700372 if (numVertices <= 0) {
373 return NULL;
374 }
375
jvanverth73f10532014-10-23 11:57:12 -0700376 void* vertices = NULL;
377 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
joshualitt9853cce2014-11-17 14:22:48 -0800378 get_vertex_stride(useColorVerts),
jvanverth73f10532014-10-23 11:57:12 -0700379 0,
380 &vertices,
381 NULL);
382 GrAlwaysAssert(success);
383 return vertices;
384}
385
jvanverth0fedb192014-10-08 09:07:27 -0700386void 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;
reedb2d77e42014-10-14 08:26:33 -0700399 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
jvanverth0fedb192014-10-08 09:07:27 -0700400 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) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800406 GrColor color = fPaint.getColor();
jvanverth0fedb192014-10-08 09:07:27 -0700407 if (fUseLCDText) {
408 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
joshualitt2e3b3e32014-12-09 13:31:14 -0800409 fCachedGeometryProcessor.reset(GrDistanceFieldLCDTextureEffect::Create(color,
410 fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700411 params,
412 fGammaTexture,
413 gammaParams,
414 colorNoPreMul,
415 flags));
416 } else {
joshualitt2dd1ae02014-12-03 06:24:10 -0800417 flags |= kColorAttr_DistanceFieldEffectFlag;
jvanverth0fedb192014-10-08 09:07:27 -0700418#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700419 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700420 filteredColor);
joshualitt2e3b3e32014-12-09 13:31:14 -0800421 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(color,
422 fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700423 params,
424 fGammaTexture,
425 gammaParams,
426 lum/255.f,
427 flags));
428#else
joshualitt2e3b3e32014-12-09 13:31:14 -0800429 fCachedGeometryProcessor.reset(GrDistanceFieldNoGammaTextureEffect::Create(color,
430 fCurrTexture,
431 params,
432 flags));
jvanverth0fedb192014-10-08 09:07:27 -0700433#endif
434 }
435 fEffectTextureUniqueID = textureUniqueID;
436 fEffectColor = filteredColor;
437 fEffectFlags = flags;
438 }
439
440}
441
jvanverth787cdf92014-12-04 10:46:50 -0800442inline bool GrDistanceFieldTextContext::uploadGlyph(GrGlyph* glyph, GrFontScaler* scaler) {
443 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
444 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
445 return true;
446 }
447
448 // try to clear out an unused plot before we flush
449 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
450 fStrike->addGlyphToAtlas(glyph, scaler)) {
451 return true;
452 }
453
454 if (c_DumpFontCache) {
455#ifdef SK_DEVELOPER
456 fContext->getFontCache()->dump();
457#endif
458 }
459
460 // before we purge the cache, we must flush any accumulated draws
461 this->flush();
462 fContext->flush();
463
464 // we should have an unused plot now
465 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
466 fStrike->addGlyphToAtlas(glyph, scaler)) {
467 return true;
468 }
469
470 // we should never get here
471 SkASSERT(false);
472 }
473
474 return false;
475}
476
477
jvanverthfca302c2014-10-20 13:12:54 -0700478// Returns true if this method handled the glyph, false if needs to be passed to fallback
479//
480bool GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
djsollen058f01e2014-10-30 11:54:43 -0700481 SkScalar sx, SkScalar sy,
jvanverth0fedb192014-10-08 09:07:27 -0700482 GrFontScaler* scaler) {
483 if (NULL == fDrawTarget) {
jvanverthfca302c2014-10-20 13:12:54 -0700484 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700485 }
486
487 if (NULL == fStrike) {
488 fStrike = fContext->getFontCache()->getStrike(scaler, true);
489 }
490
491 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
492 if (NULL == glyph || glyph->fBounds.isEmpty()) {
jvanverthfca302c2014-10-20 13:12:54 -0700493 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700494 }
495
jvanverthfca302c2014-10-20 13:12:54 -0700496 // fallback to color glyph support
jvanverth294c3262014-10-10 11:36:12 -0700497 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
jvanverthfca302c2014-10-20 13:12:54 -0700498 return false;
jvanverth294c3262014-10-10 11:36:12 -0700499 }
500
jvanverth2faa2282014-10-31 12:59:57 -0700501 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
502 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
503 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
504 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth0fedb192014-10-08 09:07:27 -0700505
jvanverth2faa2282014-10-31 12:59:57 -0700506 SkScalar scale = fTextRatio;
507 dx *= scale;
508 dy *= scale;
509 sx += dx;
510 sy += dy;
511 width *= scale;
512 height *= scale;
513 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
jvanverth0fedb192014-10-08 09:07:27 -0700514
515 // check if we clipped out
jvanverth2faa2282014-10-31 12:59:57 -0700516 SkRect dstRect;
517 const SkMatrix& ctm = fContext->getMatrix();
518 (void) ctm.mapRect(&dstRect, glyphRect);
519 if (fClipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
520 SkScalarTruncToInt(dstRect.top()),
521 SkScalarTruncToInt(dstRect.right()),
522 SkScalarTruncToInt(dstRect.bottom()))) {
jvanverth0fedb192014-10-08 09:07:27 -0700523// SkCLZ(3); // so we can set a break-point in the debugger
jvanverth2faa2282014-10-31 12:59:57 -0700524 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700525 }
jvanverth2faa2282014-10-31 12:59:57 -0700526
jvanverth95f9b5f2014-12-05 07:56:53 -0800527 if (NULL == glyph->fPlot) {
528 // needs to be a separate conditional to avoid over-optimization
529 // on Nexus 7 and Nexus 10
530 if (!uploadGlyph(glyph, scaler)) {
531 if (NULL == glyph->fPath) {
532 SkPath* path = SkNEW(SkPath);
533 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
534 // flag the glyph as being dead?
535 delete path;
536 return true;
537 }
538 glyph->fPath = path;
jvanverth0fedb192014-10-08 09:07:27 -0700539 }
jvanverth95f9b5f2014-12-05 07:56:53 -0800540
541 // flush any accumulated draws before drawing this glyph as a path.
542 this->flush();
543
544 GrContext::AutoMatrix am;
545 SkMatrix ctm;
546 ctm.setScale(fTextRatio, fTextRatio);
547 ctm.postTranslate(sx - dx, sy - dy);
548 GrPaint tmpPaint(fPaint);
549 am.setPreConcat(fContext, ctm, &tmpPaint);
550 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
551 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
552
553 // remove this glyph from the vertices we need to allocate
554 fTotalVertexCount -= kVerticesPerGlyph;
555 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700556 }
jvanverth0fedb192014-10-08 09:07:27 -0700557 }
558
jvanverth0fedb192014-10-08 09:07:27 -0700559 SkASSERT(glyph->fPlot);
560 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
561 glyph->fPlot->setDrawToken(drawToken);
562
563 GrTexture* texture = glyph->fPlot->texture();
564 SkASSERT(texture);
565
jvanverth73f10532014-10-23 11:57:12 -0700566 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fTotalVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700567 this->flush();
568 fCurrTexture = texture;
569 fCurrTexture->ref();
570 }
571
572 bool useColorVerts = !fUseLCDText;
573
574 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700575 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
576 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
joshualitt9853cce2014-11-17 14:22:48 -0800577 fVertices = alloc_vertices(fDrawTarget,
578 fAllocVertexCount,
579 useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700580 }
581
jvanverth0fedb192014-10-08 09:07:27 -0700582 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
583 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
584 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
585 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
586
jvanverth2faa2282014-10-31 12:59:57 -0700587 fVertexBounds.joinNonEmptyArg(glyphRect);
jvanverth0fedb192014-10-08 09:07:27 -0700588
joshualitt9853cce2014-11-17 14:22:48 -0800589 size_t vertSize = get_vertex_stride(useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700590
591 SkPoint* positions = reinterpret_cast<SkPoint*>(
592 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth2faa2282014-10-31 12:59:57 -0700593 positions->setRectFan(glyphRect.fLeft, glyphRect.fTop, glyphRect.fRight, glyphRect.fBottom,
594 vertSize);
jvanverth0fedb192014-10-08 09:07:27 -0700595
596 // The texture coords are last in both the with and without color vertex layouts.
597 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
598 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
599 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
600 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
601 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
602 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
603 vertSize);
604 if (useColorVerts) {
jvanverth0fedb192014-10-08 09:07:27 -0700605 // color comes after position.
606 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
607 for (int i = 0; i < 4; ++i) {
608 *colors = fPaint.getColor();
609 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
610 }
611 }
612
613 fCurrVertex += 4;
jvanverthfca302c2014-10-20 13:12:54 -0700614
615 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700616}
617
618void GrDistanceFieldTextContext::flush() {
619 if (NULL == fDrawTarget) {
620 return;
621 }
622
jvanverth0fedb192014-10-08 09:07:27 -0700623 if (fCurrVertex > 0) {
joshualitt780b11e2014-11-18 09:40:40 -0800624 GrDrawState drawState;
625 drawState.setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
joshualitt780b11e2014-11-18 09:40:40 -0800626
jvanverth0fedb192014-10-08 09:07:27 -0700627 // setup our sampler state for our text texture/atlas
628 SkASSERT(SkIsAlign4(fCurrVertex));
629
630 // get our current color
631 SkColor filteredColor;
632 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
633 if (colorFilter) {
634 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
635 } else {
636 filteredColor = fSkPaint.getColor();
637 }
638 this->setupCoverageEffect(filteredColor);
639
640 // Effects could be stored with one of the cache objects (atlas?)
joshualitt9853cce2014-11-17 14:22:48 -0800641 drawState.setGeometryProcessor(fCachedGeometryProcessor.get());
jvanverth0fedb192014-10-08 09:07:27 -0700642
643 // Set draw state
644 if (fUseLCDText) {
egdaniel378092f2014-12-03 10:40:13 -0800645 // TODO: move supportsRGBCoverage check to setupCoverageEffect and only add LCD
646 // processor if the xp can support it. For now we will simply assume that if
647 // fUseLCDText is true, then we have a known color output.
648 if (!drawState.getXPFactory()->supportsRGBCoverage(0, kRGBA_GrColorComponentFlags)) {
tfarina38406c82014-10-31 07:11:12 -0700649 SkDebugf("LCD Text will not draw correctly.\n");
jvanverth0fedb192014-10-08 09:07:27 -0700650 }
joshualitt9853cce2014-11-17 14:22:48 -0800651 SkASSERT(!drawState.hasColorVertexAttribute());
jvanverth0fedb192014-10-08 09:07:27 -0700652 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800653 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
654 drawState.setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
655 }
jvanverth0fedb192014-10-08 09:07:27 -0700656 // We're using per-vertex color.
joshualitt9853cce2014-11-17 14:22:48 -0800657 SkASSERT(drawState.hasColorVertexAttribute());
jvanverth0fedb192014-10-08 09:07:27 -0700658 }
jvanverth73f10532014-10-23 11:57:12 -0700659 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700660 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
joshualitt9853cce2014-11-17 14:22:48 -0800661 fDrawTarget->drawIndexedInstances(&drawState,
662 kTriangles_GrPrimitiveType,
jvanverth0fedb192014-10-08 09:07:27 -0700663 nGlyphs,
joshualitt9853cce2014-11-17 14:22:48 -0800664 kVerticesPerGlyph,
665 kIndicesPerGlyph,
666 &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700667 fDrawTarget->resetVertexSource();
668 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700669 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700670 fCurrVertex = 0;
671 SkSafeSetNull(fCurrTexture);
672 fVertexBounds.setLargestInverted();
673 }
674}
675
676inline void GrDistanceFieldTextContext::finish() {
677 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700678 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700679
680 GrTextContext::finish();
681}
682