blob: 118565af2967e7f6359d4cf9dfef112199c9dc76 [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;
37static const int kMediumDFFontSize = 64;
38static const int kMediumDFFontLimit = 64;
39static const int kLargeDFFontSize = 128;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +000040
jvanverthfeceba52014-07-25 19:03:34 -070041namespace {
42// position + texture coord
43extern const GrVertexAttrib gTextVertexAttribs[] = {
44 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -070045 {kVec2f_GrVertexAttribType, sizeof(SkPoint) , kGeometryProcessor_GrVertexAttribBinding}
jvanverthfeceba52014-07-25 19:03:34 -070046};
47
egdaniel7b3d5ee2014-08-28 05:41:14 -070048static const size_t kTextVASize = 2 * sizeof(SkPoint);
49
jvanverthfeceba52014-07-25 19:03:34 -070050// position + color + texture coord
51extern const GrVertexAttrib gTextVertexWithColorAttribs[] = {
52 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
53 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -070054 {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kGeometryProcessor_GrVertexAttribBinding}
jvanverthfeceba52014-07-25 19:03:34 -070055};
56
egdaniel7b3d5ee2014-08-28 05:41:14 -070057static const size_t kTextVAColorSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
58
jvanverth73f10532014-10-23 11:57:12 -070059static const int kVerticesPerGlyph = 4;
60static const int kIndicesPerGlyph = 6;
jvanverthfeceba52014-07-25 19:03:34 -070061};
jvanverth@google.comd830d132013-11-11 20:54:09 +000062
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000063GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000064 const SkDeviceProperties& properties,
65 bool enable)
Mike Klein6a25bd02014-08-29 10:03:59 -040066 : GrTextContext(context, properties) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000067#if SK_FORCE_DISTANCEFIELD_FONTS
Mike Klein6a25bd02014-08-29 10:03:59 -040068 fEnableDFRendering = true;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000069#else
Mike Klein6a25bd02014-08-29 10:03:59 -040070 fEnableDFRendering = enable;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000071#endif
Mike Klein6a25bd02014-08-29 10:03:59 -040072 fStrike = NULL;
73 fGammaTexture = NULL;
74
Mike Klein6a25bd02014-08-29 10:03:59 -040075 fEffectTextureUniqueID = SK_InvalidUniqueID;
76 fEffectColor = GrColor_ILLEGAL;
jvanverth6d22eca2014-10-28 11:10:48 -070077 fEffectFlags = kInvalid_DistanceFieldEffectFlag;
Mike Klein6a25bd02014-08-29 10:03:59 -040078
79 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -070080 fCurrVertex = 0;
81 fAllocVertexCount = 0;
82 fTotalVertexCount = 0;
83 fCurrTexture = NULL;
Mike Klein6a25bd02014-08-29 10:03:59 -040084
jvanverth1723bfc2014-07-30 09:16:33 -070085 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +000086}
87
jvanverth8c27a182014-10-14 08:45:50 -070088GrDistanceFieldTextContext* GrDistanceFieldTextContext::Create(GrContext* context,
89 const SkDeviceProperties& props,
90 bool enable) {
91 GrDistanceFieldTextContext* textContext = SkNEW_ARGS(GrDistanceFieldTextContext,
92 (context, props, enable));
93 textContext->fFallbackTextContext = GrBitmapTextContext::Create(context, props);
94
95 return textContext;
96}
97
jvanverth@google.comd830d132013-11-11 20:54:09 +000098GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
jvanverth73f10532014-10-23 11:57:12 -070099 this->finish();
jvanverth2d2a68c2014-06-10 06:42:56 -0700100 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000101}
102
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000103bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000104 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +0000105 return false;
106 }
107
skia.committer@gmail.come1d94432014-04-09 03:04:11 +0000108 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +0000109 // translate well to distance
110 if (paint.getRasterizer() || paint.getMaskFilter() ||
111 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
112 return false;
113 }
114
115 // TODO: add some stroking support
116 if (paint.getStyle() != SkPaint::kFill_Style) {
117 return false;
118 }
119
120 // TODO: choose an appropriate maximum scale for distance fields and
121 // enable perspective
122 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
123 return false;
124 }
125
126 // distance fields cannot represent color fonts
127 SkScalerContext::Rec rec;
128 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
129 return rec.getFormat() != SkMask::kARGB32_Format;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000130}
131
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000132inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
133 GrTextContext::init(paint, skPaint);
134
135 fStrike = NULL;
136
jvanverth76ce81e2014-09-22 14:26:53 -0700137 const SkMatrix& ctm = fContext->getMatrix();
jvanverth9564ce62014-09-16 05:45:19 -0700138
139 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700140 SkASSERT(!ctm.hasPerspective());
141 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700142 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700143 SkScalar scaledTextSize = textSize;
144 // if we have non-unity scale, we need to choose our base text size
145 // based on the SkPaint's text size multiplied by the max scale factor
jvanverth9564ce62014-09-16 05:45:19 -0700146 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
147 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700148 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700149 }
150
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000151 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700152 fCurrVertex = 0;
153 fAllocVertexCount = 0;
154 fTotalVertexCount = 0;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000155
jvanverth76ce81e2014-09-22 14:26:53 -0700156 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700157 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000158 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverth76ce81e2014-09-22 14:26:53 -0700159 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700160 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000161 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
162 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700163 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000164 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
165 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000166
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000167 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000168
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000169 fSkPaint.setLCDRenderText(false);
170 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700171 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000172 fSkPaint.setSubpixelText(true);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000173}
174
jvanverth2d2a68c2014-06-10 06:42:56 -0700175static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
176 const SkDeviceProperties& deviceProperties,
177 GrTexture** gammaTexture) {
178 if (NULL == *gammaTexture) {
179 int width, height;
180 size_t size;
181
182#ifdef SK_GAMMA_CONTRAST
183 SkScalar contrast = SK_GAMMA_CONTRAST;
184#else
185 SkScalar contrast = 0.5f;
186#endif
reedb2d77e42014-10-14 08:26:33 -0700187 SkScalar paintGamma = deviceProperties.gamma();
188 SkScalar deviceGamma = deviceProperties.gamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700189
190 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
191 &width, &height);
192
193 SkAutoTArray<uint8_t> data((int)size);
194 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
195
196 // TODO: Update this to use the cache rather than directly creating a texture.
197 GrTextureDesc desc;
198 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
199 desc.fWidth = width;
200 desc.fHeight = height;
201 desc.fConfig = kAlpha_8_GrPixelConfig;
202
203 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
204 if (NULL == *gammaTexture) {
205 return;
206 }
207
bsalomon81beccc2014-10-13 12:32:55 -0700208 (*gammaTexture)->writePixels(0, 0, width, height,
209 (*gammaTexture)->config(), data.get(), 0,
210 GrContext::kDontFlush_PixelOpsFlag);
jvanverth2d2a68c2014-06-10 06:42:56 -0700211 }
212}
213
jvanverthaab626c2014-10-16 08:04:39 -0700214void GrDistanceFieldTextContext::onDrawText(const GrPaint& paint, const SkPaint& skPaint,
215 const char text[], size_t byteLength,
216 SkScalar x, SkScalar y) {
217 SkASSERT(byteLength == 0 || text != NULL);
218
jvanverth2b9dc1d2014-10-20 06:48:59 -0700219 // nothing to draw
220 if (text == NULL || byteLength == 0) {
jvanverthaab626c2014-10-16 08:04:39 -0700221 return;
222 }
223
jvanverth2b9dc1d2014-10-20 06:48:59 -0700224 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
225 SkAutoGlyphCache autoCache(skPaint, &fDeviceProperties, NULL);
226 SkGlyphCache* cache = autoCache.getCache();
jvanverthaab626c2014-10-16 08:04:39 -0700227
jvanverth2b9dc1d2014-10-20 06:48:59 -0700228 SkTArray<SkScalar> positions;
jvanverthaab626c2014-10-16 08:04:39 -0700229
jvanverth2b9dc1d2014-10-20 06:48:59 -0700230 const char* textPtr = text;
231 SkFixed stopX = 0;
232 SkFixed stopY = 0;
233 SkFixed origin;
234 switch (skPaint.getTextAlign()) {
235 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
236 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
237 case SkPaint::kLeft_Align: origin = 0; break;
238 default: SkFAIL("Invalid paint origin"); return;
239 }
jvanverthaab626c2014-10-16 08:04:39 -0700240
jvanverth2b9dc1d2014-10-20 06:48:59 -0700241 SkAutoKern autokern;
jvanverthaab626c2014-10-16 08:04:39 -0700242 const char* stop = text + byteLength;
jvanverth2b9dc1d2014-10-20 06:48:59 -0700243 while (textPtr < stop) {
244 // don't need x, y here, since all subpixel variants will have the
245 // same advance
246 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
jvanverthaab626c2014-10-16 08:04:39 -0700247
jvanverth2b9dc1d2014-10-20 06:48:59 -0700248 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
249 positions.push_back(SkFixedToScalar(stopX + SkFixedMul_portable(origin, width)));
jvanverthaab626c2014-10-16 08:04:39 -0700250
jvanverth2b9dc1d2014-10-20 06:48:59 -0700251 SkFixed height = glyph.fAdvanceY;
252 positions.push_back(SkFixedToScalar(stopY + SkFixedMul_portable(origin, height)));
jvanverthaab626c2014-10-16 08:04:39 -0700253
jvanverth2b9dc1d2014-10-20 06:48:59 -0700254 stopX += width;
255 stopY += height;
jvanverthaab626c2014-10-16 08:04:39 -0700256 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700257 SkASSERT(textPtr == stop);
jvanverthaab626c2014-10-16 08:04:39 -0700258
jvanverth2b9dc1d2014-10-20 06:48:59 -0700259 // now adjust starting point depending on alignment
260 SkScalar alignX = SkFixedToScalar(stopX);
261 SkScalar alignY = SkFixedToScalar(stopY);
262 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
263 alignX = SkScalarHalf(alignX);
264 alignY = SkScalarHalf(alignY);
265 } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) {
266 alignX = 0;
267 alignY = 0;
jvanverthaab626c2014-10-16 08:04:39 -0700268 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700269 x -= alignX;
270 y -= alignY;
271 SkPoint offset = SkPoint::Make(x, y);
jvanverthaab626c2014-10-16 08:04:39 -0700272
jvanverth2b9dc1d2014-10-20 06:48:59 -0700273 this->drawPosText(paint, skPaint, text, byteLength, positions.begin(), 2, offset);
jvanverthaab626c2014-10-16 08:04:39 -0700274}
275
jvanverth8c27a182014-10-14 08:45:50 -0700276void GrDistanceFieldTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skPaint,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000277 const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -0700278 const SkScalar pos[], int scalarsPerPosition,
279 const SkPoint& offset) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000280
281 SkASSERT(byteLength == 0 || text != NULL);
282 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
283
284 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000285 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000286 return;
287 }
288
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000289 this->init(paint, skPaint);
290
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000291 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
292
jvanverth2d2a68c2014-06-10 06:42:56 -0700293 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
294 SkGlyphCache* cache = autoCache.getCache();
295 GrFontScaler* fontScaler = GetGrFontScaler(cache);
296
297 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000298
jvanverth73f10532014-10-23 11:57:12 -0700299 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
300 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
301
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000302 const char* stop = text + byteLength;
jvanverthfca302c2014-10-20 13:12:54 -0700303 SkTArray<char> fallbackTxt;
304 SkTArray<SkScalar> fallbackPos;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000305
306 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
307 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700308 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000309 // the last 2 parameters are ignored
310 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
311
312 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700313 SkScalar x = offset.x() + pos[0];
314 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000315
jvanverthfca302c2014-10-20 13:12:54 -0700316 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
317 glyph.getSubXFixed(),
318 glyph.getSubYFixed()),
319 SkScalarToFixed(x),
320 SkScalarToFixed(y),
321 fontScaler)) {
322 // couldn't append, send to fallback
323 fallbackTxt.push_back_n(text-lastText, lastText);
324 fallbackPos.push_back(pos[0]);
325 if (2 == scalarsPerPosition) {
326 fallbackPos.push_back(pos[1]);
327 }
328 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000329 }
330 pos += scalarsPerPosition;
331 }
332 } else {
jvanverth2b9dc1d2014-10-20 06:48:59 -0700333 SkScalar alignMul = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? SK_ScalarHalf
334 : SK_Scalar1;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000335 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700336 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000337 // the last 2 parameters are ignored
338 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
339
340 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700341 SkScalar x = offset.x() + pos[0];
342 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000343
jvanverth2b9dc1d2014-10-20 06:48:59 -0700344 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX)*alignMul*fTextRatio;
345 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY)*alignMul*fTextRatio;
346
jvanverthfca302c2014-10-20 13:12:54 -0700347 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
348 glyph.getSubXFixed(),
349 glyph.getSubYFixed()),
350 SkScalarToFixed(x - advanceX),
351 SkScalarToFixed(y - advanceY),
352 fontScaler)) {
353 // couldn't append, send to fallback
354 fallbackTxt.push_back_n(text-lastText, lastText);
355 fallbackPos.push_back(pos[0]);
356 if (2 == scalarsPerPosition) {
357 fallbackPos.push_back(pos[1]);
358 }
359 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000360 }
361 pos += scalarsPerPosition;
362 }
363 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000364
365 this->finish();
jvanverthfca302c2014-10-20 13:12:54 -0700366
367 if (fallbackTxt.count() > 0) {
368 fFallbackTextContext->drawPosText(paint, skPaint, fallbackTxt.begin(), fallbackTxt.count(),
369 fallbackPos.begin(), scalarsPerPosition, offset);
370 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000371}
jvanverth0fedb192014-10-08 09:07:27 -0700372
373static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
374 unsigned r = SkColorGetR(c);
375 unsigned g = SkColorGetG(c);
376 unsigned b = SkColorGetB(c);
377 return GrColorPackRGBA(r, g, b, 0xff);
378}
379
jvanverth73f10532014-10-23 11:57:12 -0700380static void* alloc_vertices(GrDrawTarget* drawTarget, int numVertices, bool useColorVerts) {
381 if (numVertices <= 0) {
382 return NULL;
383 }
384
385 // set up attributes
386 if (useColorVerts) {
387 drawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
388 SK_ARRAY_COUNT(gTextVertexWithColorAttribs), kTextVAColorSize);
389 } else {
390 drawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
391 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVASize);
392 }
393 void* vertices = NULL;
394 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
395 0,
396 &vertices,
397 NULL);
398 GrAlwaysAssert(success);
399 return vertices;
400}
401
jvanverth0fedb192014-10-08 09:07:27 -0700402void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
403 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
404 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
405
406 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
407 const SkMatrix& ctm = fContext->getMatrix();
408
409 // set up any flags
410 uint32_t flags = 0;
411 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
412 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
413 flags |= fUseLCDText && ctm.rectStaysRect() ?
414 kRectToRect_DistanceFieldEffectFlag : 0;
reedb2d77e42014-10-14 08:26:33 -0700415 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
jvanverth0fedb192014-10-08 09:07:27 -0700416 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
417
418 // see if we need to create a new effect
419 if (textureUniqueID != fEffectTextureUniqueID ||
420 filteredColor != fEffectColor ||
421 flags != fEffectFlags) {
422 if (fUseLCDText) {
423 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
424 fCachedGeometryProcessor.reset(GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
425 params,
426 fGammaTexture,
427 gammaParams,
428 colorNoPreMul,
429 flags));
430 } else {
431#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700432 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700433 filteredColor);
434 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
435 params,
436 fGammaTexture,
437 gammaParams,
438 lum/255.f,
439 flags));
440#else
441 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
442 params, flags));
443#endif
444 }
445 fEffectTextureUniqueID = textureUniqueID;
446 fEffectColor = filteredColor;
447 fEffectFlags = flags;
448 }
449
450}
451
jvanverthfca302c2014-10-20 13:12:54 -0700452// Returns true if this method handled the glyph, false if needs to be passed to fallback
453//
454bool GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
jvanverth0fedb192014-10-08 09:07:27 -0700455 SkFixed vx, SkFixed vy,
456 GrFontScaler* scaler) {
457 if (NULL == fDrawTarget) {
jvanverthfca302c2014-10-20 13:12:54 -0700458 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700459 }
460
461 if (NULL == fStrike) {
462 fStrike = fContext->getFontCache()->getStrike(scaler, true);
463 }
464
465 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
466 if (NULL == glyph || glyph->fBounds.isEmpty()) {
jvanverthfca302c2014-10-20 13:12:54 -0700467 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700468 }
469
jvanverthfca302c2014-10-20 13:12:54 -0700470 // fallback to color glyph support
jvanverth294c3262014-10-10 11:36:12 -0700471 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
jvanverthfca302c2014-10-20 13:12:54 -0700472 return false;
jvanverth294c3262014-10-10 11:36:12 -0700473 }
474
jvanverth0fedb192014-10-08 09:07:27 -0700475 SkScalar sx = SkFixedToScalar(vx);
476 SkScalar sy = SkFixedToScalar(vy);
477/*
478 // not valid, need to find a different solution for this
479 vx += SkIntToFixed(glyph->fBounds.fLeft);
480 vy += SkIntToFixed(glyph->fBounds.fTop);
481
482 // keep them as ints until we've done the clip-test
483 GrFixed width = glyph->fBounds.width();
484 GrFixed height = glyph->fBounds.height();
485
486 // check if we clipped out
487 if (true || NULL == glyph->fPlot) {
488 int x = vx >> 16;
489 int y = vy >> 16;
490 if (fClipRect.quickReject(x, y, x + width, y + height)) {
491// SkCLZ(3); // so we can set a break-point in the debugger
492 return;
493 }
494 }
495*/
496 if (NULL == glyph->fPlot) {
497 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
498 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
499 goto HAS_ATLAS;
500 }
501
502 // try to clear out an unused plot before we flush
jvanverth294c3262014-10-10 11:36:12 -0700503 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700504 fStrike->addGlyphToAtlas(glyph, scaler)) {
505 goto HAS_ATLAS;
506 }
507
508 if (c_DumpFontCache) {
509#ifdef SK_DEVELOPER
510 fContext->getFontCache()->dump();
511#endif
512 }
513
514 // before we purge the cache, we must flush any accumulated draws
515 this->flush();
516 fContext->flush();
517
518 // we should have an unused plot now
jvanverth294c3262014-10-10 11:36:12 -0700519 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700520 fStrike->addGlyphToAtlas(glyph, scaler)) {
521 goto HAS_ATLAS;
522 }
523 }
524
525 if (NULL == glyph->fPath) {
526 SkPath* path = SkNEW(SkPath);
527 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
528 // flag the glyph as being dead?
529 delete path;
jvanverthfca302c2014-10-20 13:12:54 -0700530 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700531 }
532 glyph->fPath = path;
533 }
534
bsalomonec87dc62014-10-14 10:52:00 -0700535 // flush any accumulated draws before drawing this glyph as a path.
536 this->flush();
537
jvanverth0fedb192014-10-08 09:07:27 -0700538 GrContext::AutoMatrix am;
539 SkMatrix ctm;
540 ctm.setScale(fTextRatio, fTextRatio);
541 ctm.postTranslate(sx, sy);
542 GrPaint tmpPaint(fPaint);
543 am.setPreConcat(fContext, ctm, &tmpPaint);
544 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
545 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth73f10532014-10-23 11:57:12 -0700546
547 // remove this glyph from the vertices we need to allocate
548 fTotalVertexCount -= kVerticesPerGlyph;
jvanverthfca302c2014-10-20 13:12:54 -0700549 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700550 }
551
552HAS_ATLAS:
553 SkASSERT(glyph->fPlot);
554 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
555 glyph->fPlot->setDrawToken(drawToken);
556
557 GrTexture* texture = glyph->fPlot->texture();
558 SkASSERT(texture);
559
jvanverth73f10532014-10-23 11:57:12 -0700560 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fTotalVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700561 this->flush();
562 fCurrTexture = texture;
563 fCurrTexture->ref();
564 }
565
566 bool useColorVerts = !fUseLCDText;
567
568 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700569 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
570 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
571 fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700572 }
573
574 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
575 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
576 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
577 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
578
579 SkScalar scale = fTextRatio;
580 dx *= scale;
581 dy *= scale;
582 sx += dx;
583 sy += dy;
584 width *= scale;
585 height *= scale;
586
587 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
588 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
589 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
590 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
591
592 SkRect r;
593 r.fLeft = sx;
594 r.fTop = sy;
595 r.fRight = sx + width;
596 r.fBottom = sy + height;
597
598 fVertexBounds.joinNonEmptyArg(r);
599
600 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
601 : (2 * sizeof(SkPoint) + sizeof(GrColor));
602
603 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride());
604
605 SkPoint* positions = reinterpret_cast<SkPoint*>(
606 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
607 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
608
609 // The texture coords are last in both the with and without color vertex layouts.
610 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
611 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
612 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
613 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
614 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
615 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
616 vertSize);
617 if (useColorVerts) {
618 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
619 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
620 }
621 // color comes after position.
622 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
623 for (int i = 0; i < 4; ++i) {
624 *colors = fPaint.getColor();
625 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
626 }
627 }
628
629 fCurrVertex += 4;
jvanverthfca302c2014-10-20 13:12:54 -0700630
631 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700632}
633
634void GrDistanceFieldTextContext::flush() {
635 if (NULL == fDrawTarget) {
636 return;
637 }
638
639 GrDrawState* drawState = fDrawTarget->drawState();
640 GrDrawState::AutoRestoreEffects are(drawState);
641
642 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
643
644 if (fCurrVertex > 0) {
645 // setup our sampler state for our text texture/atlas
646 SkASSERT(SkIsAlign4(fCurrVertex));
647
648 // get our current color
649 SkColor filteredColor;
650 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
651 if (colorFilter) {
652 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
653 } else {
654 filteredColor = fSkPaint.getColor();
655 }
656 this->setupCoverageEffect(filteredColor);
657
658 // Effects could be stored with one of the cache objects (atlas?)
659 drawState->setGeometryProcessor(fCachedGeometryProcessor.get());
660
661 // Set draw state
662 if (fUseLCDText) {
663 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
664 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
665 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
666 fPaint.numColorStages()) {
667 GrPrintf("LCD Text will not draw correctly.\n");
668 }
669 SkASSERT(!drawState->hasColorVertexAttribute());
670 // We don't use the GrPaint's color in this case because it's been premultiplied by
671 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
672 // the mask texture color. The end result is that we get
673 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
674 int a = SkColorGetA(fSkPaint.getColor());
675 // paintAlpha
676 drawState->setColor(SkColorSetARGB(a, a, a, a));
677 // paintColor
678 drawState->setBlendConstant(colorNoPreMul);
679 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
680 } else {
681 // set back to normal in case we took LCD path previously.
682 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
683 // We're using per-vertex color.
684 SkASSERT(drawState->hasColorVertexAttribute());
685 }
jvanverth73f10532014-10-23 11:57:12 -0700686 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700687 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
688 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
689 nGlyphs,
jvanverth73f10532014-10-23 11:57:12 -0700690 kVerticesPerGlyph, kIndicesPerGlyph, &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700691 fDrawTarget->resetVertexSource();
692 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700693 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700694 fCurrVertex = 0;
695 SkSafeSetNull(fCurrTexture);
696 fVertexBounds.setLargestInverted();
697 }
698}
699
700inline void GrDistanceFieldTextContext::finish() {
701 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700702 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700703
704 GrTextContext::finish();
705}
706