blob: a1f5ce66608bac5cdf9ab934faffde042257f25d [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"
10#include "GrDrawTarget.h"
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000011#include "GrDrawTargetCaps.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000012#include "GrFontScaler.h"
jvanverth2d2a68c2014-06-10 06:42:56 -070013#include "GrGpu.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000014#include "GrIndexBuffer.h"
egdanield58a0ba2014-06-11 10:30:05 -070015#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrTexturePriv.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000017#include "GrTextStrike.h"
18#include "GrTextStrike_impl.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019
20#include "SkColorFilter.h"
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +000021#include "SkDistanceFieldGen.h"
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +000022#include "SkDraw.h"
bsalomonafbf2d62014-09-30 12:18:44 -070023#include "SkGlyphCache.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000024#include "SkGpuDevice.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000025#include "SkPath.h"
26#include "SkRTConf.h"
27#include "SkStrokeRec.h"
28#include "effects/GrDistanceFieldTextureEffect.h"
29
jvanverthfeceba52014-07-25 19:03:34 -070030SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false,
31 "Dump the contents of the font cache before every purge.");
32
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +000033static const int kSmallDFFontSize = 32;
34static const int kSmallDFFontLimit = 32;
35static const int kMediumDFFontSize = 64;
36static const int kMediumDFFontLimit = 64;
37static const int kLargeDFFontSize = 128;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +000038
jvanverthfeceba52014-07-25 19:03:34 -070039namespace {
40// position + texture coord
41extern const GrVertexAttrib gTextVertexAttribs[] = {
42 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -070043 {kVec2f_GrVertexAttribType, sizeof(SkPoint) , kGeometryProcessor_GrVertexAttribBinding}
jvanverthfeceba52014-07-25 19:03:34 -070044};
45
egdaniel7b3d5ee2014-08-28 05:41:14 -070046static const size_t kTextVASize = 2 * sizeof(SkPoint);
47
jvanverthfeceba52014-07-25 19:03:34 -070048// position + color + texture coord
49extern const GrVertexAttrib gTextVertexWithColorAttribs[] = {
50 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
51 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -070052 {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kGeometryProcessor_GrVertexAttribBinding}
jvanverthfeceba52014-07-25 19:03:34 -070053};
54
egdaniel7b3d5ee2014-08-28 05:41:14 -070055static const size_t kTextVAColorSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
56
jvanverthfeceba52014-07-25 19:03:34 -070057};
jvanverth@google.comd830d132013-11-11 20:54:09 +000058
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000059GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000060 const SkDeviceProperties& properties,
61 bool enable)
Mike Klein6a25bd02014-08-29 10:03:59 -040062 : GrTextContext(context, properties) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000063#if SK_FORCE_DISTANCEFIELD_FONTS
Mike Klein6a25bd02014-08-29 10:03:59 -040064 fEnableDFRendering = true;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000065#else
Mike Klein6a25bd02014-08-29 10:03:59 -040066 fEnableDFRendering = enable;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000067#endif
Mike Klein6a25bd02014-08-29 10:03:59 -040068 fStrike = NULL;
69 fGammaTexture = NULL;
70
71 fCurrTexture = NULL;
72 fCurrVertex = 0;
73 fEffectTextureUniqueID = SK_InvalidUniqueID;
74 fEffectColor = GrColor_ILLEGAL;
75 fEffectFlags = 0;
76
77 fVertices = NULL;
78 fMaxVertices = 0;
79
jvanverth1723bfc2014-07-30 09:16:33 -070080 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +000081}
82
83GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
jvanverth0fedb192014-10-08 09:07:27 -070084 this->flush();
jvanverth2d2a68c2014-06-10 06:42:56 -070085 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +000086}
87
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000088bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000089 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000090 return false;
91 }
92
skia.committer@gmail.come1d94432014-04-09 03:04:11 +000093 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000094 // translate well to distance
95 if (paint.getRasterizer() || paint.getMaskFilter() ||
96 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
97 return false;
98 }
99
100 // TODO: add some stroking support
101 if (paint.getStyle() != SkPaint::kFill_Style) {
102 return false;
103 }
104
105 // TODO: choose an appropriate maximum scale for distance fields and
106 // enable perspective
107 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
108 return false;
109 }
110
111 // distance fields cannot represent color fonts
112 SkScalerContext::Rec rec;
113 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
114 return rec.getFormat() != SkMask::kARGB32_Format;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000115}
116
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000117inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
118 GrTextContext::init(paint, skPaint);
119
120 fStrike = NULL;
121
jvanverth76ce81e2014-09-22 14:26:53 -0700122 const SkMatrix& ctm = fContext->getMatrix();
jvanverth9564ce62014-09-16 05:45:19 -0700123
124 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700125 SkASSERT(!ctm.hasPerspective());
126 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700127 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700128 SkScalar scaledTextSize = textSize;
129 // if we have non-unity scale, we need to choose our base text size
130 // based on the SkPaint's text size multiplied by the max scale factor
jvanverth9564ce62014-09-16 05:45:19 -0700131 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
132 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700133 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700134 }
135
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000136 fCurrVertex = 0;
137
138 fVertices = NULL;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000139
jvanverth76ce81e2014-09-22 14:26:53 -0700140 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700141 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000142 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverth76ce81e2014-09-22 14:26:53 -0700143 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700144 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000145 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
146 } 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));
149 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000150
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000151 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000152
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000153 fSkPaint.setLCDRenderText(false);
154 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700155 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000156 fSkPaint.setSubpixelText(true);
jvanverth2d2a68c2014-06-10 06:42:56 -0700157
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000158}
159
jvanverth2d2a68c2014-06-10 06:42:56 -0700160static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
161 const SkDeviceProperties& deviceProperties,
162 GrTexture** gammaTexture) {
163 if (NULL == *gammaTexture) {
164 int width, height;
165 size_t size;
166
167#ifdef SK_GAMMA_CONTRAST
168 SkScalar contrast = SK_GAMMA_CONTRAST;
169#else
170 SkScalar contrast = 0.5f;
171#endif
reed4a8126e2014-09-22 07:29:03 -0700172 SkScalar paintGamma = deviceProperties.getGamma();
173 SkScalar deviceGamma = deviceProperties.getGamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700174
175 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
176 &width, &height);
177
178 SkAutoTArray<uint8_t> data((int)size);
179 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
180
181 // TODO: Update this to use the cache rather than directly creating a texture.
182 GrTextureDesc desc;
183 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
184 desc.fWidth = width;
185 desc.fHeight = height;
186 desc.fConfig = kAlpha_8_GrPixelConfig;
187
188 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
189 if (NULL == *gammaTexture) {
190 return;
191 }
192
193 context->writeTexturePixels(*gammaTexture,
194 0, 0, width, height,
195 (*gammaTexture)->config(), data.get(), 0,
196 GrContext::kDontFlush_PixelOpsFlag);
197 }
198}
199
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000200void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
201 const char text[], size_t byteLength,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000202 SkScalar x, SkScalar y) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000203 SkASSERT(byteLength == 0 || text != NULL);
204
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000205 // nothing to draw or can't draw
206 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
207 || fSkPaint.getRasterizer()) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000208 return;
209 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000210
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000211 this->init(paint, skPaint);
212
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000213 SkScalar sizeRatio = fTextRatio;
214
215 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
216
jvanverth2d2a68c2014-06-10 06:42:56 -0700217 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
218 SkGlyphCache* cache = autoCache.getCache();
219 GrFontScaler* fontScaler = GetGrFontScaler(cache);
220
221 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000222
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000223 // need to measure first
224 // TODO - generate positions and pre-load cache as well?
225 const char* stop = text + byteLength;
226 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
227 SkFixed stopX = 0;
228 SkFixed stopY = 0;
229
230 const char* textPtr = text;
231 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);
235
236 stopX += glyph.fAdvanceX;
237 stopY += glyph.fAdvanceY;
238 }
239 SkASSERT(textPtr == stop);
240
241 SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio;
242 SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio;
243
244 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
245 alignX = SkScalarHalf(alignX);
246 alignY = SkScalarHalf(alignY);
247 }
248
249 x -= alignX;
250 y -= alignY;
251 }
252
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000253 SkFixed fx = SkScalarToFixed(x);
254 SkFixed fy = SkScalarToFixed(y);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000255 SkFixed fixedScale = SkScalarToFixed(sizeRatio);
256 while (text < stop) {
commit-bot@chromium.orga9dae712014-03-24 18:34:04 +0000257 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000258
259 if (glyph.fWidth) {
jvanverth0fedb192014-10-08 09:07:27 -0700260 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
261 glyph.getSubXFixed(),
262 glyph.getSubYFixed()),
263 fx,
264 fy,
265 fontScaler);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000266 }
267
268 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale);
269 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale);
270 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000271
272 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000273}
274
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000275void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint,
276 const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -0700277 const SkScalar pos[], int scalarsPerPosition,
278 const SkPoint& offset) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000279
280 SkASSERT(byteLength == 0 || text != NULL);
281 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
282
283 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000284 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000285 return;
286 }
287
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000288 this->init(paint, skPaint);
289
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000290 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
291
jvanverth2d2a68c2014-06-10 06:42:56 -0700292 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
293 SkGlyphCache* cache = autoCache.getCache();
294 GrFontScaler* fontScaler = GetGrFontScaler(cache);
295
296 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000297
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000298 const char* stop = text + byteLength;
299
300 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
301 while (text < stop) {
302 // the last 2 parameters are ignored
303 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
304
305 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700306 SkScalar x = offset.x() + pos[0];
307 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000308
jvanverth0fedb192014-10-08 09:07:27 -0700309 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
310 glyph.getSubXFixed(),
311 glyph.getSubYFixed()),
312 SkScalarToFixed(x),
313 SkScalarToFixed(y),
314 fontScaler);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000315 }
316 pos += scalarsPerPosition;
317 }
318 } else {
319 int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0;
320 while (text < stop) {
321 // the last 2 parameters are ignored
322 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
323
324 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700325 SkScalar x = offset.x() + pos[0];
326 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000327
jvanverth0fedb192014-10-08 09:07:27 -0700328 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
329 glyph.getSubXFixed(),
330 glyph.getSubYFixed()),
331 SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift),
332 SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift),
333 fontScaler);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000334 }
335 pos += scalarsPerPosition;
336 }
337 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000338
339 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000340}
jvanverth0fedb192014-10-08 09:07:27 -0700341
342static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
343 unsigned r = SkColorGetR(c);
344 unsigned g = SkColorGetG(c);
345 unsigned b = SkColorGetB(c);
346 return GrColorPackRGBA(r, g, b, 0xff);
347}
348
349void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
350 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
351 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
352
353 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
354 const SkMatrix& ctm = fContext->getMatrix();
355
356 // set up any flags
357 uint32_t flags = 0;
358 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
359 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
360 flags |= fUseLCDText && ctm.rectStaysRect() ?
361 kRectToRect_DistanceFieldEffectFlag : 0;
362 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.fPixelGeometry);
363 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
364
365 // see if we need to create a new effect
366 if (textureUniqueID != fEffectTextureUniqueID ||
367 filteredColor != fEffectColor ||
368 flags != fEffectFlags) {
369 if (fUseLCDText) {
370 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
371 fCachedGeometryProcessor.reset(GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
372 params,
373 fGammaTexture,
374 gammaParams,
375 colorNoPreMul,
376 flags));
377 } else {
378#ifdef SK_GAMMA_APPLY_TO_A8
379 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.getGamma(),
380 filteredColor);
381 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
382 params,
383 fGammaTexture,
384 gammaParams,
385 lum/255.f,
386 flags));
387#else
388 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
389 params, flags));
390#endif
391 }
392 fEffectTextureUniqueID = textureUniqueID;
393 fEffectColor = filteredColor;
394 fEffectFlags = flags;
395 }
396
397}
398
399void GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
400 SkFixed vx, SkFixed vy,
401 GrFontScaler* scaler) {
402 if (NULL == fDrawTarget) {
403 return;
404 }
405
406 if (NULL == fStrike) {
407 fStrike = fContext->getFontCache()->getStrike(scaler, true);
408 }
409
410 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
411 if (NULL == glyph || glyph->fBounds.isEmpty()) {
412 return;
413 }
414
jvanverth294c3262014-10-10 11:36:12 -0700415 // TODO: support color glyphs
416 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
417 return;
418 }
419
jvanverth0fedb192014-10-08 09:07:27 -0700420 SkScalar sx = SkFixedToScalar(vx);
421 SkScalar sy = SkFixedToScalar(vy);
422/*
423 // not valid, need to find a different solution for this
424 vx += SkIntToFixed(glyph->fBounds.fLeft);
425 vy += SkIntToFixed(glyph->fBounds.fTop);
426
427 // keep them as ints until we've done the clip-test
428 GrFixed width = glyph->fBounds.width();
429 GrFixed height = glyph->fBounds.height();
430
431 // check if we clipped out
432 if (true || NULL == glyph->fPlot) {
433 int x = vx >> 16;
434 int y = vy >> 16;
435 if (fClipRect.quickReject(x, y, x + width, y + height)) {
436// SkCLZ(3); // so we can set a break-point in the debugger
437 return;
438 }
439 }
440*/
441 if (NULL == glyph->fPlot) {
442 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
443 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
444 goto HAS_ATLAS;
445 }
446
447 // try to clear out an unused plot before we flush
jvanverth294c3262014-10-10 11:36:12 -0700448 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700449 fStrike->addGlyphToAtlas(glyph, scaler)) {
450 goto HAS_ATLAS;
451 }
452
453 if (c_DumpFontCache) {
454#ifdef SK_DEVELOPER
455 fContext->getFontCache()->dump();
456#endif
457 }
458
459 // before we purge the cache, we must flush any accumulated draws
460 this->flush();
461 fContext->flush();
462
463 // we should have an unused plot now
jvanverth294c3262014-10-10 11:36:12 -0700464 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700465 fStrike->addGlyphToAtlas(glyph, scaler)) {
466 goto HAS_ATLAS;
467 }
468 }
469
470 if (NULL == glyph->fPath) {
471 SkPath* path = SkNEW(SkPath);
472 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
473 // flag the glyph as being dead?
474 delete path;
475 return;
476 }
477 glyph->fPath = path;
478 }
479
480 GrContext::AutoMatrix am;
481 SkMatrix ctm;
482 ctm.setScale(fTextRatio, fTextRatio);
483 ctm.postTranslate(sx, sy);
484 GrPaint tmpPaint(fPaint);
485 am.setPreConcat(fContext, ctm, &tmpPaint);
486 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
487 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
488 return;
489 }
490
491HAS_ATLAS:
492 SkASSERT(glyph->fPlot);
493 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
494 glyph->fPlot->setDrawToken(drawToken);
495
496 GrTexture* texture = glyph->fPlot->texture();
497 SkASSERT(texture);
498
499 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
500 this->flush();
501 fCurrTexture = texture;
502 fCurrTexture->ref();
503 }
504
505 bool useColorVerts = !fUseLCDText;
506
507 if (NULL == fVertices) {
508 // If we need to reserve vertices allow the draw target to suggest
509 // a number of verts to reserve and whether to perform a flush.
510 fMaxVertices = kMinRequestedVerts;
511 if (useColorVerts) {
512 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
513 SK_ARRAY_COUNT(gTextVertexWithColorAttribs), kTextVAColorSize);
514 } else {
515 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
516 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVASize);
517 }
518 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
519 if (flush) {
520 this->flush();
521 fContext->flush();
522 if (useColorVerts) {
523 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
524 SK_ARRAY_COUNT(gTextVertexWithColorAttribs), kTextVAColorSize);
525 } else {
526 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
527 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVASize);
528 }
529 }
530 fMaxVertices = kDefaultRequestedVerts;
531 // ignore return, no point in flushing again.
532 fDrawTarget->geometryHints(&fMaxVertices, NULL);
533
534 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
535 if (fMaxVertices < kMinRequestedVerts) {
536 fMaxVertices = kDefaultRequestedVerts;
537 } else if (fMaxVertices > maxQuadVertices) {
538 // don't exceed the limit of the index buffer
539 fMaxVertices = maxQuadVertices;
540 }
541 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
542 0,
543 &fVertices,
544 NULL);
545 GrAlwaysAssert(success);
546 }
547
548 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
549 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
550 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
551 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
552
553 SkScalar scale = fTextRatio;
554 dx *= scale;
555 dy *= scale;
556 sx += dx;
557 sy += dy;
558 width *= scale;
559 height *= scale;
560
561 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
562 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
563 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
564 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
565
566 SkRect r;
567 r.fLeft = sx;
568 r.fTop = sy;
569 r.fRight = sx + width;
570 r.fBottom = sy + height;
571
572 fVertexBounds.joinNonEmptyArg(r);
573
574 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
575 : (2 * sizeof(SkPoint) + sizeof(GrColor));
576
577 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride());
578
579 SkPoint* positions = reinterpret_cast<SkPoint*>(
580 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
581 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
582
583 // The texture coords are last in both the with and without color vertex layouts.
584 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
585 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
586 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
587 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
588 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
589 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
590 vertSize);
591 if (useColorVerts) {
592 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
593 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
594 }
595 // color comes after position.
596 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
597 for (int i = 0; i < 4; ++i) {
598 *colors = fPaint.getColor();
599 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
600 }
601 }
602
603 fCurrVertex += 4;
604}
605
606void GrDistanceFieldTextContext::flush() {
607 if (NULL == fDrawTarget) {
608 return;
609 }
610
611 GrDrawState* drawState = fDrawTarget->drawState();
612 GrDrawState::AutoRestoreEffects are(drawState);
613
614 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
615
616 if (fCurrVertex > 0) {
617 // setup our sampler state for our text texture/atlas
618 SkASSERT(SkIsAlign4(fCurrVertex));
619
620 // get our current color
621 SkColor filteredColor;
622 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
623 if (colorFilter) {
624 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
625 } else {
626 filteredColor = fSkPaint.getColor();
627 }
628 this->setupCoverageEffect(filteredColor);
629
630 // Effects could be stored with one of the cache objects (atlas?)
631 drawState->setGeometryProcessor(fCachedGeometryProcessor.get());
632
633 // Set draw state
634 if (fUseLCDText) {
635 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
636 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
637 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
638 fPaint.numColorStages()) {
639 GrPrintf("LCD Text will not draw correctly.\n");
640 }
641 SkASSERT(!drawState->hasColorVertexAttribute());
642 // We don't use the GrPaint's color in this case because it's been premultiplied by
643 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
644 // the mask texture color. The end result is that we get
645 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
646 int a = SkColorGetA(fSkPaint.getColor());
647 // paintAlpha
648 drawState->setColor(SkColorSetARGB(a, a, a, a));
649 // paintColor
650 drawState->setBlendConstant(colorNoPreMul);
651 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
652 } else {
653 // set back to normal in case we took LCD path previously.
654 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
655 // We're using per-vertex color.
656 SkASSERT(drawState->hasColorVertexAttribute());
657 }
658 int nGlyphs = fCurrVertex / 4;
659 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
660 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
661 nGlyphs,
662 4, 6, &fVertexBounds);
663 fDrawTarget->resetVertexSource();
664 fVertices = NULL;
665 fMaxVertices = 0;
666 fCurrVertex = 0;
667 SkSafeSetNull(fCurrTexture);
668 fVertexBounds.setLargestInverted();
669 }
670}
671
672inline void GrDistanceFieldTextContext::finish() {
673 this->flush();
674
675 GrTextContext::finish();
676}
677