blob: a052b641f63d1a085306baa90db005714351b426 [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
bsalomonef3fcd82014-12-12 08:51:38 -0800309 fallbackTxt.push_back_n(SkToInt(text-lastText), lastText);
jvanverthfca302c2014-10-20 13:12:54 -0700310 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
bsalomonef3fcd82014-12-12 08:51:38 -0800338 fallbackTxt.push_back_n(SkToInt(text-lastText), lastText);
jvanverthfca302c2014-10-20 13:12:54 -0700339 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;
joshualitt56995b52014-12-11 15:44:02 -0800418 bool opaque = GrColorIsOpaque(color);
jvanverth0fedb192014-10-08 09:07:27 -0700419#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700420 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700421 filteredColor);
joshualitt2e3b3e32014-12-09 13:31:14 -0800422 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(color,
423 fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700424 params,
425 fGammaTexture,
426 gammaParams,
427 lum/255.f,
joshualitt56995b52014-12-11 15:44:02 -0800428 flags,
429 opaque));
jvanverth0fedb192014-10-08 09:07:27 -0700430#else
joshualitt2e3b3e32014-12-09 13:31:14 -0800431 fCachedGeometryProcessor.reset(GrDistanceFieldNoGammaTextureEffect::Create(color,
432 fCurrTexture,
433 params,
joshualitt56995b52014-12-11 15:44:02 -0800434 flags,
435 opaque));
jvanverth0fedb192014-10-08 09:07:27 -0700436#endif
437 }
438 fEffectTextureUniqueID = textureUniqueID;
439 fEffectColor = filteredColor;
440 fEffectFlags = flags;
441 }
442
443}
444
jvanverth787cdf92014-12-04 10:46:50 -0800445inline bool GrDistanceFieldTextContext::uploadGlyph(GrGlyph* glyph, GrFontScaler* scaler) {
446 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
447 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
448 return true;
449 }
450
451 // try to clear out an unused plot before we flush
452 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
453 fStrike->addGlyphToAtlas(glyph, scaler)) {
454 return true;
455 }
456
457 if (c_DumpFontCache) {
458#ifdef SK_DEVELOPER
459 fContext->getFontCache()->dump();
460#endif
461 }
462
463 // before we purge the cache, we must flush any accumulated draws
464 this->flush();
465 fContext->flush();
466
467 // we should have an unused plot now
468 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
469 fStrike->addGlyphToAtlas(glyph, scaler)) {
470 return true;
471 }
472
473 // we should never get here
474 SkASSERT(false);
475 }
476
477 return false;
478}
479
480
jvanverthfca302c2014-10-20 13:12:54 -0700481// Returns true if this method handled the glyph, false if needs to be passed to fallback
482//
483bool GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
djsollen058f01e2014-10-30 11:54:43 -0700484 SkScalar sx, SkScalar sy,
jvanverth0fedb192014-10-08 09:07:27 -0700485 GrFontScaler* scaler) {
486 if (NULL == fDrawTarget) {
jvanverthfca302c2014-10-20 13:12:54 -0700487 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700488 }
489
490 if (NULL == fStrike) {
491 fStrike = fContext->getFontCache()->getStrike(scaler, true);
492 }
493
494 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
495 if (NULL == glyph || glyph->fBounds.isEmpty()) {
jvanverthfca302c2014-10-20 13:12:54 -0700496 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700497 }
498
jvanverthfca302c2014-10-20 13:12:54 -0700499 // fallback to color glyph support
jvanverth294c3262014-10-10 11:36:12 -0700500 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
jvanverthfca302c2014-10-20 13:12:54 -0700501 return false;
jvanverth294c3262014-10-10 11:36:12 -0700502 }
503
jvanverth2faa2282014-10-31 12:59:57 -0700504 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
505 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
506 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
507 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth0fedb192014-10-08 09:07:27 -0700508
jvanverth2faa2282014-10-31 12:59:57 -0700509 SkScalar scale = fTextRatio;
510 dx *= scale;
511 dy *= scale;
512 sx += dx;
513 sy += dy;
514 width *= scale;
515 height *= scale;
516 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
jvanverth0fedb192014-10-08 09:07:27 -0700517
518 // check if we clipped out
jvanverth2faa2282014-10-31 12:59:57 -0700519 SkRect dstRect;
520 const SkMatrix& ctm = fContext->getMatrix();
521 (void) ctm.mapRect(&dstRect, glyphRect);
522 if (fClipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
523 SkScalarTruncToInt(dstRect.top()),
524 SkScalarTruncToInt(dstRect.right()),
525 SkScalarTruncToInt(dstRect.bottom()))) {
jvanverth0fedb192014-10-08 09:07:27 -0700526// SkCLZ(3); // so we can set a break-point in the debugger
jvanverth2faa2282014-10-31 12:59:57 -0700527 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700528 }
jvanverth2faa2282014-10-31 12:59:57 -0700529
jvanverth95f9b5f2014-12-05 07:56:53 -0800530 if (NULL == glyph->fPlot) {
531 // needs to be a separate conditional to avoid over-optimization
532 // on Nexus 7 and Nexus 10
533 if (!uploadGlyph(glyph, scaler)) {
534 if (NULL == glyph->fPath) {
535 SkPath* path = SkNEW(SkPath);
536 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
537 // flag the glyph as being dead?
538 delete path;
539 return true;
540 }
541 glyph->fPath = path;
jvanverth0fedb192014-10-08 09:07:27 -0700542 }
jvanverth95f9b5f2014-12-05 07:56:53 -0800543
544 // flush any accumulated draws before drawing this glyph as a path.
545 this->flush();
546
547 GrContext::AutoMatrix am;
548 SkMatrix ctm;
549 ctm.setScale(fTextRatio, fTextRatio);
550 ctm.postTranslate(sx - dx, sy - dy);
551 GrPaint tmpPaint(fPaint);
552 am.setPreConcat(fContext, ctm, &tmpPaint);
553 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
554 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
555
556 // remove this glyph from the vertices we need to allocate
557 fTotalVertexCount -= kVerticesPerGlyph;
558 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700559 }
jvanverth0fedb192014-10-08 09:07:27 -0700560 }
561
jvanverth0fedb192014-10-08 09:07:27 -0700562 SkASSERT(glyph->fPlot);
563 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
564 glyph->fPlot->setDrawToken(drawToken);
565
566 GrTexture* texture = glyph->fPlot->texture();
567 SkASSERT(texture);
568
jvanverth73f10532014-10-23 11:57:12 -0700569 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fTotalVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700570 this->flush();
571 fCurrTexture = texture;
572 fCurrTexture->ref();
573 }
574
575 bool useColorVerts = !fUseLCDText;
576
577 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700578 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
579 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
joshualitt9853cce2014-11-17 14:22:48 -0800580 fVertices = alloc_vertices(fDrawTarget,
581 fAllocVertexCount,
582 useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700583 }
584
jvanverth0fedb192014-10-08 09:07:27 -0700585 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
586 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
587 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
588 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
589
jvanverth2faa2282014-10-31 12:59:57 -0700590 fVertexBounds.joinNonEmptyArg(glyphRect);
jvanverth0fedb192014-10-08 09:07:27 -0700591
joshualitt9853cce2014-11-17 14:22:48 -0800592 size_t vertSize = get_vertex_stride(useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700593
594 SkPoint* positions = reinterpret_cast<SkPoint*>(
595 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth2faa2282014-10-31 12:59:57 -0700596 positions->setRectFan(glyphRect.fLeft, glyphRect.fTop, glyphRect.fRight, glyphRect.fBottom,
597 vertSize);
jvanverth0fedb192014-10-08 09:07:27 -0700598
599 // The texture coords are last in both the with and without color vertex layouts.
600 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
601 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
602 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
603 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
604 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
605 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
606 vertSize);
607 if (useColorVerts) {
jvanverth0fedb192014-10-08 09:07:27 -0700608 // color comes after position.
609 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
610 for (int i = 0; i < 4; ++i) {
611 *colors = fPaint.getColor();
612 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
613 }
614 }
615
616 fCurrVertex += 4;
jvanverthfca302c2014-10-20 13:12:54 -0700617
618 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700619}
620
621void GrDistanceFieldTextContext::flush() {
622 if (NULL == fDrawTarget) {
623 return;
624 }
625
jvanverth0fedb192014-10-08 09:07:27 -0700626 if (fCurrVertex > 0) {
joshualitt780b11e2014-11-18 09:40:40 -0800627 GrDrawState drawState;
628 drawState.setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
joshualitt780b11e2014-11-18 09:40:40 -0800629
jvanverth0fedb192014-10-08 09:07:27 -0700630 // setup our sampler state for our text texture/atlas
631 SkASSERT(SkIsAlign4(fCurrVertex));
632
633 // get our current color
634 SkColor filteredColor;
635 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
636 if (colorFilter) {
637 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
638 } else {
639 filteredColor = fSkPaint.getColor();
640 }
641 this->setupCoverageEffect(filteredColor);
642
jvanverth0fedb192014-10-08 09:07:27 -0700643 // 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 }
joshualitt56995b52014-12-11 15:44:02 -0800651 SkASSERT(!fCachedGeometryProcessor->hasVertexColor());
jvanverth0fedb192014-10-08 09:07:27 -0700652 } else {
jvanverth0fedb192014-10-08 09:07:27 -0700653 // We're using per-vertex color.
joshualitt56995b52014-12-11 15:44:02 -0800654 SkASSERT(fCachedGeometryProcessor->hasVertexColor());
jvanverth0fedb192014-10-08 09:07:27 -0700655 }
jvanverth73f10532014-10-23 11:57:12 -0700656 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700657 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
joshualitt9853cce2014-11-17 14:22:48 -0800658 fDrawTarget->drawIndexedInstances(&drawState,
joshualitt56995b52014-12-11 15:44:02 -0800659 fCachedGeometryProcessor.get(),
joshualitt9853cce2014-11-17 14:22:48 -0800660 kTriangles_GrPrimitiveType,
jvanverth0fedb192014-10-08 09:07:27 -0700661 nGlyphs,
joshualitt9853cce2014-11-17 14:22:48 -0800662 kVerticesPerGlyph,
663 kIndicesPerGlyph,
664 &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700665 fDrawTarget->resetVertexSource();
666 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700667 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700668 fCurrVertex = 0;
669 SkSafeSetNull(fCurrTexture);
670 fVertexBounds.setLargestInverted();
671 }
672}
673
674inline void GrDistanceFieldTextContext::finish() {
675 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700676 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700677
678 GrTextContext::finish();
679}
680