blob: 6b047e45d99ea571cabc0db8d430c3d946e4528e [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
jvanverthfeceba52014-07-25 19:03:34 -070059};
jvanverth@google.comd830d132013-11-11 20:54:09 +000060
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000061GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000062 const SkDeviceProperties& properties,
63 bool enable)
Mike Klein6a25bd02014-08-29 10:03:59 -040064 : GrTextContext(context, properties) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000065#if SK_FORCE_DISTANCEFIELD_FONTS
Mike Klein6a25bd02014-08-29 10:03:59 -040066 fEnableDFRendering = true;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000067#else
Mike Klein6a25bd02014-08-29 10:03:59 -040068 fEnableDFRendering = enable;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000069#endif
Mike Klein6a25bd02014-08-29 10:03:59 -040070 fStrike = NULL;
71 fGammaTexture = NULL;
72
73 fCurrTexture = NULL;
74 fCurrVertex = 0;
75 fEffectTextureUniqueID = SK_InvalidUniqueID;
76 fEffectColor = GrColor_ILLEGAL;
77 fEffectFlags = 0;
78
79 fVertices = NULL;
80 fMaxVertices = 0;
81
jvanverth1723bfc2014-07-30 09:16:33 -070082 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +000083}
84
jvanverth8c27a182014-10-14 08:45:50 -070085GrDistanceFieldTextContext* GrDistanceFieldTextContext::Create(GrContext* context,
86 const SkDeviceProperties& props,
87 bool enable) {
88 GrDistanceFieldTextContext* textContext = SkNEW_ARGS(GrDistanceFieldTextContext,
89 (context, props, enable));
90 textContext->fFallbackTextContext = GrBitmapTextContext::Create(context, props);
91
92 return textContext;
93}
94
jvanverth@google.comd830d132013-11-11 20:54:09 +000095GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
jvanverth0fedb192014-10-08 09:07:27 -070096 this->flush();
jvanverth2d2a68c2014-06-10 06:42:56 -070097 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +000098}
99
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000100bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000101 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +0000102 return false;
103 }
104
skia.committer@gmail.come1d94432014-04-09 03:04:11 +0000105 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +0000106 // translate well to distance
107 if (paint.getRasterizer() || paint.getMaskFilter() ||
108 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
109 return false;
110 }
111
112 // TODO: add some stroking support
113 if (paint.getStyle() != SkPaint::kFill_Style) {
114 return false;
115 }
116
117 // TODO: choose an appropriate maximum scale for distance fields and
118 // enable perspective
119 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
120 return false;
121 }
122
123 // distance fields cannot represent color fonts
124 SkScalerContext::Rec rec;
125 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
126 return rec.getFormat() != SkMask::kARGB32_Format;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000127}
128
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000129inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
130 GrTextContext::init(paint, skPaint);
131
132 fStrike = NULL;
133
jvanverth76ce81e2014-09-22 14:26:53 -0700134 const SkMatrix& ctm = fContext->getMatrix();
jvanverth9564ce62014-09-16 05:45:19 -0700135
136 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700137 SkASSERT(!ctm.hasPerspective());
138 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700139 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700140 SkScalar scaledTextSize = textSize;
141 // if we have non-unity scale, we need to choose our base text size
142 // based on the SkPaint's text size multiplied by the max scale factor
jvanverth9564ce62014-09-16 05:45:19 -0700143 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
144 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700145 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700146 }
147
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000148 fCurrVertex = 0;
149
150 fVertices = NULL;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000151
jvanverth76ce81e2014-09-22 14:26:53 -0700152 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700153 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000154 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverth76ce81e2014-09-22 14:26:53 -0700155 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700156 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000157 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
158 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700159 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000160 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
161 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000162
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000163 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000164
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000165 fSkPaint.setLCDRenderText(false);
166 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700167 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000168 fSkPaint.setSubpixelText(true);
jvanverth2d2a68c2014-06-10 06:42:56 -0700169
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000170}
171
jvanverth2d2a68c2014-06-10 06:42:56 -0700172static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
173 const SkDeviceProperties& deviceProperties,
174 GrTexture** gammaTexture) {
175 if (NULL == *gammaTexture) {
176 int width, height;
177 size_t size;
178
179#ifdef SK_GAMMA_CONTRAST
180 SkScalar contrast = SK_GAMMA_CONTRAST;
181#else
182 SkScalar contrast = 0.5f;
183#endif
reedb2d77e42014-10-14 08:26:33 -0700184 SkScalar paintGamma = deviceProperties.gamma();
185 SkScalar deviceGamma = deviceProperties.gamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700186
187 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
188 &width, &height);
189
190 SkAutoTArray<uint8_t> data((int)size);
191 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
192
193 // TODO: Update this to use the cache rather than directly creating a texture.
194 GrTextureDesc desc;
195 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
196 desc.fWidth = width;
197 desc.fHeight = height;
198 desc.fConfig = kAlpha_8_GrPixelConfig;
199
200 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
201 if (NULL == *gammaTexture) {
202 return;
203 }
204
bsalomon81beccc2014-10-13 12:32:55 -0700205 (*gammaTexture)->writePixels(0, 0, width, height,
206 (*gammaTexture)->config(), data.get(), 0,
207 GrContext::kDontFlush_PixelOpsFlag);
jvanverth2d2a68c2014-06-10 06:42:56 -0700208 }
209}
210
jvanverthaab626c2014-10-16 08:04:39 -0700211void GrDistanceFieldTextContext::onDrawText(const GrPaint& paint, const SkPaint& skPaint,
212 const char text[], size_t byteLength,
213 SkScalar x, SkScalar y) {
214 SkASSERT(byteLength == 0 || text != NULL);
215
jvanverth2b9dc1d2014-10-20 06:48:59 -0700216 // nothing to draw
217 if (text == NULL || byteLength == 0) {
jvanverthaab626c2014-10-16 08:04:39 -0700218 return;
219 }
220
jvanverth2b9dc1d2014-10-20 06:48:59 -0700221 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
222 SkAutoGlyphCache autoCache(skPaint, &fDeviceProperties, NULL);
223 SkGlyphCache* cache = autoCache.getCache();
jvanverthaab626c2014-10-16 08:04:39 -0700224
jvanverth2b9dc1d2014-10-20 06:48:59 -0700225 SkTArray<SkScalar> positions;
jvanverthaab626c2014-10-16 08:04:39 -0700226
jvanverth2b9dc1d2014-10-20 06:48:59 -0700227 const char* textPtr = text;
228 SkFixed stopX = 0;
229 SkFixed stopY = 0;
230 SkFixed origin;
231 switch (skPaint.getTextAlign()) {
232 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
233 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
234 case SkPaint::kLeft_Align: origin = 0; break;
235 default: SkFAIL("Invalid paint origin"); return;
236 }
jvanverthaab626c2014-10-16 08:04:39 -0700237
jvanverth2b9dc1d2014-10-20 06:48:59 -0700238 SkAutoKern autokern;
jvanverthaab626c2014-10-16 08:04:39 -0700239 const char* stop = text + byteLength;
jvanverth2b9dc1d2014-10-20 06:48:59 -0700240 while (textPtr < stop) {
241 // don't need x, y here, since all subpixel variants will have the
242 // same advance
243 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
jvanverthaab626c2014-10-16 08:04:39 -0700244
jvanverth2b9dc1d2014-10-20 06:48:59 -0700245 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
246 positions.push_back(SkFixedToScalar(stopX + SkFixedMul_portable(origin, width)));
jvanverthaab626c2014-10-16 08:04:39 -0700247
jvanverth2b9dc1d2014-10-20 06:48:59 -0700248 SkFixed height = glyph.fAdvanceY;
249 positions.push_back(SkFixedToScalar(stopY + SkFixedMul_portable(origin, height)));
jvanverthaab626c2014-10-16 08:04:39 -0700250
jvanverth2b9dc1d2014-10-20 06:48:59 -0700251 stopX += width;
252 stopY += height;
jvanverthaab626c2014-10-16 08:04:39 -0700253 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700254 SkASSERT(textPtr == stop);
jvanverthaab626c2014-10-16 08:04:39 -0700255
jvanverth2b9dc1d2014-10-20 06:48:59 -0700256 // now adjust starting point depending on alignment
257 SkScalar alignX = SkFixedToScalar(stopX);
258 SkScalar alignY = SkFixedToScalar(stopY);
259 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
260 alignX = SkScalarHalf(alignX);
261 alignY = SkScalarHalf(alignY);
262 } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) {
263 alignX = 0;
264 alignY = 0;
jvanverthaab626c2014-10-16 08:04:39 -0700265 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700266 x -= alignX;
267 y -= alignY;
268 SkPoint offset = SkPoint::Make(x, y);
jvanverthaab626c2014-10-16 08:04:39 -0700269
jvanverth2b9dc1d2014-10-20 06:48:59 -0700270 this->drawPosText(paint, skPaint, text, byteLength, positions.begin(), 2, offset);
jvanverthaab626c2014-10-16 08:04:39 -0700271}
272
jvanverth8c27a182014-10-14 08:45:50 -0700273void GrDistanceFieldTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skPaint,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000274 const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -0700275 const SkScalar pos[], int scalarsPerPosition,
276 const SkPoint& offset) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000277
278 SkASSERT(byteLength == 0 || text != NULL);
279 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
280
281 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000282 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000283 return;
284 }
285
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000286 this->init(paint, skPaint);
287
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000288 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
289
jvanverth2d2a68c2014-06-10 06:42:56 -0700290 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
291 SkGlyphCache* cache = autoCache.getCache();
292 GrFontScaler* fontScaler = GetGrFontScaler(cache);
293
294 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000295
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000296 const char* stop = text + byteLength;
297
298 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
299 while (text < stop) {
300 // the last 2 parameters are ignored
301 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
302
303 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700304 SkScalar x = offset.x() + pos[0];
305 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000306
jvanverth0fedb192014-10-08 09:07:27 -0700307 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
308 glyph.getSubXFixed(),
309 glyph.getSubYFixed()),
310 SkScalarToFixed(x),
311 SkScalarToFixed(y),
312 fontScaler);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000313 }
314 pos += scalarsPerPosition;
315 }
316 } else {
jvanverth2b9dc1d2014-10-20 06:48:59 -0700317 SkScalar alignMul = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? SK_ScalarHalf
318 : SK_Scalar1;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000319 while (text < stop) {
320 // the last 2 parameters are ignored
321 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
322
323 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700324 SkScalar x = offset.x() + pos[0];
325 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000326
jvanverth2b9dc1d2014-10-20 06:48:59 -0700327 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX)*alignMul*fTextRatio;
328 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY)*alignMul*fTextRatio;
329
jvanverth0fedb192014-10-08 09:07:27 -0700330 this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
331 glyph.getSubXFixed(),
332 glyph.getSubYFixed()),
jvanverth2b9dc1d2014-10-20 06:48:59 -0700333 SkScalarToFixed(x - advanceX),
334 SkScalarToFixed(y - advanceY),
jvanverth0fedb192014-10-08 09:07:27 -0700335 fontScaler);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000336 }
337 pos += scalarsPerPosition;
338 }
339 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000340
341 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000342}
jvanverth0fedb192014-10-08 09:07:27 -0700343
344static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
345 unsigned r = SkColorGetR(c);
346 unsigned g = SkColorGetG(c);
347 unsigned b = SkColorGetB(c);
348 return GrColorPackRGBA(r, g, b, 0xff);
349}
350
351void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
352 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
353 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
354
355 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
356 const SkMatrix& ctm = fContext->getMatrix();
357
358 // set up any flags
359 uint32_t flags = 0;
360 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
361 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
362 flags |= fUseLCDText && ctm.rectStaysRect() ?
363 kRectToRect_DistanceFieldEffectFlag : 0;
reedb2d77e42014-10-14 08:26:33 -0700364 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
jvanverth0fedb192014-10-08 09:07:27 -0700365 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
366
367 // see if we need to create a new effect
368 if (textureUniqueID != fEffectTextureUniqueID ||
369 filteredColor != fEffectColor ||
370 flags != fEffectFlags) {
371 if (fUseLCDText) {
372 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
373 fCachedGeometryProcessor.reset(GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
374 params,
375 fGammaTexture,
376 gammaParams,
377 colorNoPreMul,
378 flags));
379 } else {
380#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700381 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700382 filteredColor);
383 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
384 params,
385 fGammaTexture,
386 gammaParams,
387 lum/255.f,
388 flags));
389#else
390 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
391 params, flags));
392#endif
393 }
394 fEffectTextureUniqueID = textureUniqueID;
395 fEffectColor = filteredColor;
396 fEffectFlags = flags;
397 }
398
399}
400
401void GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
402 SkFixed vx, SkFixed vy,
403 GrFontScaler* scaler) {
404 if (NULL == fDrawTarget) {
405 return;
406 }
407
408 if (NULL == fStrike) {
409 fStrike = fContext->getFontCache()->getStrike(scaler, true);
410 }
411
412 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
413 if (NULL == glyph || glyph->fBounds.isEmpty()) {
414 return;
415 }
416
jvanverth294c3262014-10-10 11:36:12 -0700417 // TODO: support color glyphs
418 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
419 return;
420 }
421
jvanverth0fedb192014-10-08 09:07:27 -0700422 SkScalar sx = SkFixedToScalar(vx);
423 SkScalar sy = SkFixedToScalar(vy);
424/*
425 // not valid, need to find a different solution for this
426 vx += SkIntToFixed(glyph->fBounds.fLeft);
427 vy += SkIntToFixed(glyph->fBounds.fTop);
428
429 // keep them as ints until we've done the clip-test
430 GrFixed width = glyph->fBounds.width();
431 GrFixed height = glyph->fBounds.height();
432
433 // check if we clipped out
434 if (true || NULL == glyph->fPlot) {
435 int x = vx >> 16;
436 int y = vy >> 16;
437 if (fClipRect.quickReject(x, y, x + width, y + height)) {
438// SkCLZ(3); // so we can set a break-point in the debugger
439 return;
440 }
441 }
442*/
443 if (NULL == glyph->fPlot) {
444 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
445 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
446 goto HAS_ATLAS;
447 }
448
449 // try to clear out an unused plot before we flush
jvanverth294c3262014-10-10 11:36:12 -0700450 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700451 fStrike->addGlyphToAtlas(glyph, scaler)) {
452 goto HAS_ATLAS;
453 }
454
455 if (c_DumpFontCache) {
456#ifdef SK_DEVELOPER
457 fContext->getFontCache()->dump();
458#endif
459 }
460
461 // before we purge the cache, we must flush any accumulated draws
462 this->flush();
463 fContext->flush();
464
465 // we should have an unused plot now
jvanverth294c3262014-10-10 11:36:12 -0700466 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700467 fStrike->addGlyphToAtlas(glyph, scaler)) {
468 goto HAS_ATLAS;
469 }
470 }
471
472 if (NULL == glyph->fPath) {
473 SkPath* path = SkNEW(SkPath);
474 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
475 // flag the glyph as being dead?
476 delete path;
477 return;
478 }
479 glyph->fPath = path;
480 }
481
bsalomonec87dc62014-10-14 10:52:00 -0700482 // flush any accumulated draws before drawing this glyph as a path.
483 this->flush();
484
jvanverth0fedb192014-10-08 09:07:27 -0700485 GrContext::AutoMatrix am;
486 SkMatrix ctm;
487 ctm.setScale(fTextRatio, fTextRatio);
488 ctm.postTranslate(sx, sy);
489 GrPaint tmpPaint(fPaint);
490 am.setPreConcat(fContext, ctm, &tmpPaint);
491 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
492 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
493 return;
494 }
495
496HAS_ATLAS:
497 SkASSERT(glyph->fPlot);
498 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
499 glyph->fPlot->setDrawToken(drawToken);
500
501 GrTexture* texture = glyph->fPlot->texture();
502 SkASSERT(texture);
503
504 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
505 this->flush();
506 fCurrTexture = texture;
507 fCurrTexture->ref();
508 }
509
510 bool useColorVerts = !fUseLCDText;
511
512 if (NULL == fVertices) {
513 // If we need to reserve vertices allow the draw target to suggest
514 // a number of verts to reserve and whether to perform a flush.
515 fMaxVertices = kMinRequestedVerts;
516 if (useColorVerts) {
517 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
518 SK_ARRAY_COUNT(gTextVertexWithColorAttribs), kTextVAColorSize);
519 } else {
520 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
521 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVASize);
522 }
523 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
524 if (flush) {
525 this->flush();
526 fContext->flush();
527 if (useColorVerts) {
528 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
529 SK_ARRAY_COUNT(gTextVertexWithColorAttribs), kTextVAColorSize);
530 } else {
531 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
532 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVASize);
533 }
534 }
535 fMaxVertices = kDefaultRequestedVerts;
536 // ignore return, no point in flushing again.
537 fDrawTarget->geometryHints(&fMaxVertices, NULL);
538
539 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
540 if (fMaxVertices < kMinRequestedVerts) {
541 fMaxVertices = kDefaultRequestedVerts;
542 } else if (fMaxVertices > maxQuadVertices) {
543 // don't exceed the limit of the index buffer
544 fMaxVertices = maxQuadVertices;
545 }
546 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
547 0,
548 &fVertices,
549 NULL);
550 GrAlwaysAssert(success);
551 }
552
553 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
554 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
555 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
556 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
557
558 SkScalar scale = fTextRatio;
559 dx *= scale;
560 dy *= scale;
561 sx += dx;
562 sy += dy;
563 width *= scale;
564 height *= scale;
565
566 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
567 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
568 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
569 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
570
571 SkRect r;
572 r.fLeft = sx;
573 r.fTop = sy;
574 r.fRight = sx + width;
575 r.fBottom = sy + height;
576
577 fVertexBounds.joinNonEmptyArg(r);
578
579 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
580 : (2 * sizeof(SkPoint) + sizeof(GrColor));
581
582 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride());
583
584 SkPoint* positions = reinterpret_cast<SkPoint*>(
585 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
586 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
587
588 // The texture coords are last in both the with and without color vertex layouts.
589 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
590 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
591 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
592 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
593 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
594 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
595 vertSize);
596 if (useColorVerts) {
597 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
598 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
599 }
600 // color comes after position.
601 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
602 for (int i = 0; i < 4; ++i) {
603 *colors = fPaint.getColor();
604 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
605 }
606 }
607
608 fCurrVertex += 4;
609}
610
611void GrDistanceFieldTextContext::flush() {
612 if (NULL == fDrawTarget) {
613 return;
614 }
615
616 GrDrawState* drawState = fDrawTarget->drawState();
617 GrDrawState::AutoRestoreEffects are(drawState);
618
619 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
620
621 if (fCurrVertex > 0) {
622 // setup our sampler state for our text texture/atlas
623 SkASSERT(SkIsAlign4(fCurrVertex));
624
625 // get our current color
626 SkColor filteredColor;
627 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
628 if (colorFilter) {
629 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
630 } else {
631 filteredColor = fSkPaint.getColor();
632 }
633 this->setupCoverageEffect(filteredColor);
634
635 // Effects could be stored with one of the cache objects (atlas?)
636 drawState->setGeometryProcessor(fCachedGeometryProcessor.get());
637
638 // Set draw state
639 if (fUseLCDText) {
640 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
641 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
642 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
643 fPaint.numColorStages()) {
644 GrPrintf("LCD Text will not draw correctly.\n");
645 }
646 SkASSERT(!drawState->hasColorVertexAttribute());
647 // We don't use the GrPaint's color in this case because it's been premultiplied by
648 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
649 // the mask texture color. The end result is that we get
650 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
651 int a = SkColorGetA(fSkPaint.getColor());
652 // paintAlpha
653 drawState->setColor(SkColorSetARGB(a, a, a, a));
654 // paintColor
655 drawState->setBlendConstant(colorNoPreMul);
656 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
657 } else {
658 // set back to normal in case we took LCD path previously.
659 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
660 // We're using per-vertex color.
661 SkASSERT(drawState->hasColorVertexAttribute());
662 }
663 int nGlyphs = fCurrVertex / 4;
664 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
665 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
666 nGlyphs,
667 4, 6, &fVertexBounds);
668 fDrawTarget->resetVertexSource();
669 fVertices = NULL;
670 fMaxVertices = 0;
671 fCurrVertex = 0;
672 SkSafeSetNull(fCurrTexture);
673 fVertexBounds.setLargestInverted();
674 }
675}
676
677inline void GrDistanceFieldTextContext::finish() {
678 this->flush();
679
680 GrTextContext::finish();
681}
682