blob: 6ffa3c3cacb1bf5a2d02cec32bc3c37b2eb2e99c [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
jvanverth2faa2282014-10-31 12:59:57 -0700126 return true;
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 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700149 fCurrVertex = 0;
150 fAllocVertexCount = 0;
151 fTotalVertexCount = 0;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000152
jvanverth76ce81e2014-09-22 14:26:53 -0700153 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700154 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000155 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverth76ce81e2014-09-22 14:26:53 -0700156 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700157 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000158 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
159 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700160 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000161 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
162 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000163
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000164 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000165
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000166 fSkPaint.setLCDRenderText(false);
167 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700168 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000169 fSkPaint.setSubpixelText(true);
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.
bsalomonf2703d82014-10-28 14:33:06 -0700194 GrSurfaceDesc desc;
195 desc.fFlags = kNone_GrSurfaceFlags;
jvanverth2d2a68c2014-06-10 06:42:56 -0700196 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
jvanverth73f10532014-10-23 11:57:12 -0700296 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
297 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
298
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000299 const char* stop = text + byteLength;
jvanverthfca302c2014-10-20 13:12:54 -0700300 SkTArray<char> fallbackTxt;
301 SkTArray<SkScalar> fallbackPos;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000302
303 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
304 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700305 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000306 // the last 2 parameters are ignored
307 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
308
309 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700310 SkScalar x = offset.x() + pos[0];
311 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000312
jvanverthfca302c2014-10-20 13:12:54 -0700313 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
314 glyph.getSubXFixed(),
315 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700316 x, y, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700317 // couldn't append, send to fallback
318 fallbackTxt.push_back_n(text-lastText, lastText);
319 fallbackPos.push_back(pos[0]);
320 if (2 == scalarsPerPosition) {
321 fallbackPos.push_back(pos[1]);
322 }
323 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000324 }
325 pos += scalarsPerPosition;
326 }
327 } else {
jvanverth2b9dc1d2014-10-20 06:48:59 -0700328 SkScalar alignMul = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? SK_ScalarHalf
329 : SK_Scalar1;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000330 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700331 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000332 // the last 2 parameters are ignored
333 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
334
335 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700336 SkScalar x = offset.x() + pos[0];
337 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000338
jvanverth2b9dc1d2014-10-20 06:48:59 -0700339 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX)*alignMul*fTextRatio;
340 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY)*alignMul*fTextRatio;
341
jvanverthfca302c2014-10-20 13:12:54 -0700342 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
343 glyph.getSubXFixed(),
344 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700345 x - advanceX, y - advanceY, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700346 // couldn't append, send to fallback
347 fallbackTxt.push_back_n(text-lastText, lastText);
348 fallbackPos.push_back(pos[0]);
349 if (2 == scalarsPerPosition) {
350 fallbackPos.push_back(pos[1]);
351 }
352 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000353 }
354 pos += scalarsPerPosition;
355 }
356 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000357
358 this->finish();
jvanverthfca302c2014-10-20 13:12:54 -0700359
360 if (fallbackTxt.count() > 0) {
361 fFallbackTextContext->drawPosText(paint, skPaint, fallbackTxt.begin(), fallbackTxt.count(),
362 fallbackPos.begin(), scalarsPerPosition, offset);
363 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000364}
jvanverth0fedb192014-10-08 09:07:27 -0700365
366static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
367 unsigned r = SkColorGetR(c);
368 unsigned g = SkColorGetG(c);
369 unsigned b = SkColorGetB(c);
370 return GrColorPackRGBA(r, g, b, 0xff);
371}
372
jvanverth73f10532014-10-23 11:57:12 -0700373static void* alloc_vertices(GrDrawTarget* drawTarget, int numVertices, bool useColorVerts) {
374 if (numVertices <= 0) {
375 return NULL;
376 }
377
378 // set up attributes
379 if (useColorVerts) {
380 drawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
381 SK_ARRAY_COUNT(gTextVertexWithColorAttribs), kTextVAColorSize);
382 } else {
383 drawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
384 SK_ARRAY_COUNT(gTextVertexAttribs), kTextVASize);
385 }
386 void* vertices = NULL;
387 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
388 0,
389 &vertices,
390 NULL);
391 GrAlwaysAssert(success);
392 return vertices;
393}
394
jvanverth0fedb192014-10-08 09:07:27 -0700395void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
396 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
397 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
398
399 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
400 const SkMatrix& ctm = fContext->getMatrix();
401
402 // set up any flags
403 uint32_t flags = 0;
404 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
405 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
406 flags |= fUseLCDText && ctm.rectStaysRect() ?
407 kRectToRect_DistanceFieldEffectFlag : 0;
reedb2d77e42014-10-14 08:26:33 -0700408 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
jvanverth0fedb192014-10-08 09:07:27 -0700409 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
410
411 // see if we need to create a new effect
412 if (textureUniqueID != fEffectTextureUniqueID ||
413 filteredColor != fEffectColor ||
414 flags != fEffectFlags) {
415 if (fUseLCDText) {
416 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
417 fCachedGeometryProcessor.reset(GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
418 params,
419 fGammaTexture,
420 gammaParams,
421 colorNoPreMul,
422 flags));
423 } else {
424#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700425 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700426 filteredColor);
427 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
428 params,
429 fGammaTexture,
430 gammaParams,
431 lum/255.f,
432 flags));
433#else
jvanverth2faa2282014-10-31 12:59:57 -0700434 fCachedGeometryProcessor.reset(GrDistanceFieldNoGammaTextureEffect::Create(fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700435 params, flags));
436#endif
437 }
438 fEffectTextureUniqueID = textureUniqueID;
439 fEffectColor = filteredColor;
440 fEffectFlags = flags;
441 }
442
443}
444
jvanverthfca302c2014-10-20 13:12:54 -0700445// Returns true if this method handled the glyph, false if needs to be passed to fallback
446//
447bool GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
djsollen058f01e2014-10-30 11:54:43 -0700448 SkScalar sx, SkScalar sy,
jvanverth0fedb192014-10-08 09:07:27 -0700449 GrFontScaler* scaler) {
450 if (NULL == fDrawTarget) {
jvanverthfca302c2014-10-20 13:12:54 -0700451 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700452 }
453
454 if (NULL == fStrike) {
455 fStrike = fContext->getFontCache()->getStrike(scaler, true);
456 }
457
458 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
459 if (NULL == glyph || glyph->fBounds.isEmpty()) {
jvanverthfca302c2014-10-20 13:12:54 -0700460 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700461 }
462
jvanverthfca302c2014-10-20 13:12:54 -0700463 // fallback to color glyph support
jvanverth294c3262014-10-10 11:36:12 -0700464 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
jvanverthfca302c2014-10-20 13:12:54 -0700465 return false;
jvanverth294c3262014-10-10 11:36:12 -0700466 }
467
jvanverth2faa2282014-10-31 12:59:57 -0700468 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
469 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
470 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
471 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth0fedb192014-10-08 09:07:27 -0700472
jvanverth2faa2282014-10-31 12:59:57 -0700473 SkScalar scale = fTextRatio;
474 dx *= scale;
475 dy *= scale;
476 sx += dx;
477 sy += dy;
478 width *= scale;
479 height *= scale;
480 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
jvanverth0fedb192014-10-08 09:07:27 -0700481
482 // check if we clipped out
jvanverth2faa2282014-10-31 12:59:57 -0700483 SkRect dstRect;
484 const SkMatrix& ctm = fContext->getMatrix();
485 (void) ctm.mapRect(&dstRect, glyphRect);
486 if (fClipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
487 SkScalarTruncToInt(dstRect.top()),
488 SkScalarTruncToInt(dstRect.right()),
489 SkScalarTruncToInt(dstRect.bottom()))) {
jvanverth0fedb192014-10-08 09:07:27 -0700490// SkCLZ(3); // so we can set a break-point in the debugger
jvanverth2faa2282014-10-31 12:59:57 -0700491 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700492 }
jvanverth2faa2282014-10-31 12:59:57 -0700493
jvanverth0fedb192014-10-08 09:07:27 -0700494 if (NULL == glyph->fPlot) {
495 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
496 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
497 goto HAS_ATLAS;
498 }
499
500 // try to clear out an unused plot before we flush
jvanverth294c3262014-10-10 11:36:12 -0700501 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700502 fStrike->addGlyphToAtlas(glyph, scaler)) {
503 goto HAS_ATLAS;
504 }
505
506 if (c_DumpFontCache) {
507#ifdef SK_DEVELOPER
508 fContext->getFontCache()->dump();
509#endif
510 }
511
512 // before we purge the cache, we must flush any accumulated draws
513 this->flush();
514 fContext->flush();
515
516 // we should have an unused plot now
jvanverth294c3262014-10-10 11:36:12 -0700517 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
jvanverth0fedb192014-10-08 09:07:27 -0700518 fStrike->addGlyphToAtlas(glyph, scaler)) {
519 goto HAS_ATLAS;
520 }
521 }
522
523 if (NULL == glyph->fPath) {
524 SkPath* path = SkNEW(SkPath);
525 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
526 // flag the glyph as being dead?
527 delete path;
jvanverthfca302c2014-10-20 13:12:54 -0700528 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700529 }
530 glyph->fPath = path;
531 }
532
bsalomonec87dc62014-10-14 10:52:00 -0700533 // flush any accumulated draws before drawing this glyph as a path.
534 this->flush();
535
jvanverth0fedb192014-10-08 09:07:27 -0700536 GrContext::AutoMatrix am;
537 SkMatrix ctm;
538 ctm.setScale(fTextRatio, fTextRatio);
539 ctm.postTranslate(sx, sy);
540 GrPaint tmpPaint(fPaint);
541 am.setPreConcat(fContext, ctm, &tmpPaint);
542 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
543 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth73f10532014-10-23 11:57:12 -0700544
545 // remove this glyph from the vertices we need to allocate
546 fTotalVertexCount -= kVerticesPerGlyph;
jvanverthfca302c2014-10-20 13:12:54 -0700547 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700548 }
549
550HAS_ATLAS:
551 SkASSERT(glyph->fPlot);
552 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
553 glyph->fPlot->setDrawToken(drawToken);
554
555 GrTexture* texture = glyph->fPlot->texture();
556 SkASSERT(texture);
557
jvanverth73f10532014-10-23 11:57:12 -0700558 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fTotalVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700559 this->flush();
560 fCurrTexture = texture;
561 fCurrTexture->ref();
562 }
563
564 bool useColorVerts = !fUseLCDText;
565
566 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700567 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
568 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
569 fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700570 }
571
jvanverth0fedb192014-10-08 09:07:27 -0700572 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
573 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
574 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
575 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
576
jvanverth2faa2282014-10-31 12:59:57 -0700577 fVertexBounds.joinNonEmptyArg(glyphRect);
jvanverth0fedb192014-10-08 09:07:27 -0700578
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);
jvanverth2faa2282014-10-31 12:59:57 -0700586 positions->setRectFan(glyphRect.fLeft, glyphRect.fTop, glyphRect.fRight, glyphRect.fBottom,
587 vertSize);
jvanverth0fedb192014-10-08 09:07:27 -0700588
589 // The texture coords are last in both the with and without color vertex layouts.
590 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
591 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
592 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
593 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
594 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
595 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
596 vertSize);
597 if (useColorVerts) {
598 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
599 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
600 }
601 // color comes after position.
602 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
603 for (int i = 0; i < 4; ++i) {
604 *colors = fPaint.getColor();
605 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
606 }
607 }
608
609 fCurrVertex += 4;
jvanverthfca302c2014-10-20 13:12:54 -0700610
611 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700612}
613
614void GrDistanceFieldTextContext::flush() {
615 if (NULL == fDrawTarget) {
616 return;
617 }
618
619 GrDrawState* drawState = fDrawTarget->drawState();
620 GrDrawState::AutoRestoreEffects are(drawState);
621
622 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
623
624 if (fCurrVertex > 0) {
625 // setup our sampler state for our text texture/atlas
626 SkASSERT(SkIsAlign4(fCurrVertex));
627
628 // get our current color
629 SkColor filteredColor;
630 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
631 if (colorFilter) {
632 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
633 } else {
634 filteredColor = fSkPaint.getColor();
635 }
636 this->setupCoverageEffect(filteredColor);
637
638 // Effects could be stored with one of the cache objects (atlas?)
639 drawState->setGeometryProcessor(fCachedGeometryProcessor.get());
640
641 // Set draw state
642 if (fUseLCDText) {
643 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
644 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
645 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
646 fPaint.numColorStages()) {
tfarina38406c82014-10-31 07:11:12 -0700647 SkDebugf("LCD Text will not draw correctly.\n");
jvanverth0fedb192014-10-08 09:07:27 -0700648 }
649 SkASSERT(!drawState->hasColorVertexAttribute());
650 // We don't use the GrPaint's color in this case because it's been premultiplied by
651 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
652 // the mask texture color. The end result is that we get
653 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
654 int a = SkColorGetA(fSkPaint.getColor());
655 // paintAlpha
656 drawState->setColor(SkColorSetARGB(a, a, a, a));
657 // paintColor
658 drawState->setBlendConstant(colorNoPreMul);
659 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
660 } else {
661 // set back to normal in case we took LCD path previously.
662 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
663 // We're using per-vertex color.
664 SkASSERT(drawState->hasColorVertexAttribute());
665 }
jvanverth73f10532014-10-23 11:57:12 -0700666 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700667 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
668 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
669 nGlyphs,
jvanverth73f10532014-10-23 11:57:12 -0700670 kVerticesPerGlyph, kIndicesPerGlyph, &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700671 fDrawTarget->resetVertexSource();
672 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700673 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700674 fCurrVertex = 0;
675 SkSafeSetNull(fCurrTexture);
676 fVertexBounds.setLargestInverted();
677 }
678}
679
680inline void GrDistanceFieldTextContext::finish() {
681 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700682 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700683
684 GrTextContext::finish();
685}
686