blob: 65e899960e16c3779ffd57ad4f3fc1ed10444a8e [file] [log] [blame]
jvanverth@google.comd830d132013-11-11 20:54:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrDistanceFieldTextContext.h"
9#include "GrAtlas.h"
jvanverth8c27a182014-10-14 08:45:50 -070010#include "GrBitmapTextContext.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000011#include "GrDrawTarget.h"
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000012#include "GrDrawTargetCaps.h"
jvanverth787cdf92014-12-04 10:46:50 -080013#include "GrFontCache.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000014#include "GrFontScaler.h"
jvanverth2d2a68c2014-06-10 06:42:56 -070015#include "GrGpu.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000016#include "GrIndexBuffer.h"
egdanield58a0ba2014-06-11 10:30:05 -070017#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070018#include "GrTexturePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019
jvanverth2b9dc1d2014-10-20 06:48:59 -070020#include "SkAutoKern.h"
bsalomonafbf2d62014-09-30 12:18:44 -070021#include "SkColorFilter.h"
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +000022#include "SkDistanceFieldGen.h"
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +000023#include "SkDraw.h"
bsalomonafbf2d62014-09-30 12:18:44 -070024#include "SkGlyphCache.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000025#include "SkGpuDevice.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000026#include "SkPath.h"
27#include "SkRTConf.h"
28#include "SkStrokeRec.h"
29#include "effects/GrDistanceFieldTextureEffect.h"
30
jvanverthfeceba52014-07-25 19:03:34 -070031SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false,
32 "Dump the contents of the font cache before every purge.");
33
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +000034static const int kSmallDFFontSize = 32;
35static const int kSmallDFFontLimit = 32;
jvanverth5a3d92f2015-01-28 13:08:40 -080036static const int kMediumDFFontSize = 78;
37static const int kMediumDFFontLimit = 78;
38static const int kLargeDFFontSize = 192;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +000039
jvanverth73f10532014-10-23 11:57:12 -070040static const int kVerticesPerGlyph = 4;
41static const int kIndicesPerGlyph = 6;
jvanverth@google.comd830d132013-11-11 20:54:09 +000042
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000043GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000044 const SkDeviceProperties& properties,
45 bool enable)
Mike Klein6a25bd02014-08-29 10:03:59 -040046 : GrTextContext(context, properties) {
jvanverth4736e142014-11-07 07:12:46 -080047#if SK_FORCE_DISTANCE_FIELD_TEXT
Mike Klein6a25bd02014-08-29 10:03:59 -040048 fEnableDFRendering = true;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000049#else
Mike Klein6a25bd02014-08-29 10:03:59 -040050 fEnableDFRendering = enable;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000051#endif
Mike Klein6a25bd02014-08-29 10:03:59 -040052 fStrike = NULL;
53 fGammaTexture = NULL;
54
Mike Klein6a25bd02014-08-29 10:03:59 -040055 fEffectTextureUniqueID = SK_InvalidUniqueID;
56 fEffectColor = GrColor_ILLEGAL;
jvanverth6d22eca2014-10-28 11:10:48 -070057 fEffectFlags = kInvalid_DistanceFieldEffectFlag;
Mike Klein6a25bd02014-08-29 10:03:59 -040058
59 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -070060 fCurrVertex = 0;
61 fAllocVertexCount = 0;
62 fTotalVertexCount = 0;
63 fCurrTexture = NULL;
Mike Klein6a25bd02014-08-29 10:03:59 -040064
jvanverth1723bfc2014-07-30 09:16:33 -070065 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +000066}
67
jvanverth8c27a182014-10-14 08:45:50 -070068GrDistanceFieldTextContext* GrDistanceFieldTextContext::Create(GrContext* context,
69 const SkDeviceProperties& props,
70 bool enable) {
71 GrDistanceFieldTextContext* textContext = SkNEW_ARGS(GrDistanceFieldTextContext,
72 (context, props, enable));
73 textContext->fFallbackTextContext = GrBitmapTextContext::Create(context, props);
74
75 return textContext;
76}
77
jvanverth@google.comd830d132013-11-11 20:54:09 +000078GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
jvanverth2d2a68c2014-06-10 06:42:56 -070079 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +000080}
81
joshualitt5531d512014-12-17 15:50:11 -080082bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint, const SkMatrix& viewMatrix) {
jvanverth5a3d92f2015-01-28 13:08:40 -080083 // TODO: support perspective (need getMaxScale replacement)
84 if (viewMatrix.hasPerspective()) {
85 return false;
86 }
87
88 SkScalar maxScale = viewMatrix.getMaxScale();
89 SkScalar scaledTextSize = maxScale*paint.getTextSize();
90 // Scaling up beyond 2x yields undesireable artifacts
91 if (scaledTextSize > 2*kLargeDFFontSize) {
92 return false;
93 }
94
95 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP() &&
96 scaledTextSize < kLargeDFFontSize) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000097 return false;
98 }
99
skia.committer@gmail.come1d94432014-04-09 03:04:11 +0000100 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +0000101 // translate well to distance
102 if (paint.getRasterizer() || paint.getMaskFilter() ||
103 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
104 return false;
105 }
106
107 // TODO: add some stroking support
108 if (paint.getStyle() != SkPaint::kFill_Style) {
109 return false;
110 }
111
jvanverth2faa2282014-10-31 12:59:57 -0700112 return true;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000113}
114
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000115inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
116 GrTextContext::init(paint, skPaint);
117
118 fStrike = NULL;
119
joshualitt5531d512014-12-17 15:50:11 -0800120 const SkMatrix& ctm = fViewMatrix;
jvanverth9564ce62014-09-16 05:45:19 -0700121
122 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700123 SkASSERT(!ctm.hasPerspective());
124 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700125 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700126 SkScalar scaledTextSize = textSize;
127 // if we have non-unity scale, we need to choose our base text size
128 // based on the SkPaint's text size multiplied by the max scale factor
jvanverth9564ce62014-09-16 05:45:19 -0700129 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
130 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700131 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700132 }
133
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000134 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700135 fCurrVertex = 0;
136 fAllocVertexCount = 0;
137 fTotalVertexCount = 0;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000138
jvanverth76ce81e2014-09-22 14:26:53 -0700139 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700140 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000141 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800142#if DEBUG_TEXT_SIZE
143 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF));
144 fPaint.setColor(GrColorPackRGBA(0x00, 0x00, 0xFF, 0xFF));
145#endif
jvanverth76ce81e2014-09-22 14:26:53 -0700146 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700147 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000148 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800149#if DEBUG_TEXT_SIZE
150 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00));
151 fPaint.setColor(GrColorPackRGBA(0x00, 0xFF, 0x00, 0xFF));
152#endif
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000153 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700154 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000155 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800156#if DEBUG_TEXT_SIZE
157 fSkPaint.setColor(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00));
158 fPaint.setColor(GrColorPackRGBA(0xFF, 0x00, 0x00, 0xFF));
159#endif
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000160 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000161
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000162 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000163
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000164 fSkPaint.setLCDRenderText(false);
165 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700166 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000167 fSkPaint.setSubpixelText(true);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000168}
169
jvanverth2d2a68c2014-06-10 06:42:56 -0700170static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
171 const SkDeviceProperties& deviceProperties,
172 GrTexture** gammaTexture) {
173 if (NULL == *gammaTexture) {
174 int width, height;
175 size_t size;
176
177#ifdef SK_GAMMA_CONTRAST
178 SkScalar contrast = SK_GAMMA_CONTRAST;
179#else
180 SkScalar contrast = 0.5f;
181#endif
reedb2d77e42014-10-14 08:26:33 -0700182 SkScalar paintGamma = deviceProperties.gamma();
183 SkScalar deviceGamma = deviceProperties.gamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700184
185 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
186 &width, &height);
187
188 SkAutoTArray<uint8_t> data((int)size);
189 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
190
191 // TODO: Update this to use the cache rather than directly creating a texture.
bsalomonf2703d82014-10-28 14:33:06 -0700192 GrSurfaceDesc desc;
193 desc.fFlags = kNone_GrSurfaceFlags;
jvanverth2d2a68c2014-06-10 06:42:56 -0700194 desc.fWidth = width;
195 desc.fHeight = height;
196 desc.fConfig = kAlpha_8_GrPixelConfig;
197
bsalomon5236cf42015-01-14 10:42:08 -0800198 *gammaTexture = context->getGpu()->createTexture(desc, true, NULL, 0);
jvanverth2d2a68c2014-06-10 06:42:56 -0700199 if (NULL == *gammaTexture) {
200 return;
201 }
202
bsalomon81beccc2014-10-13 12:32:55 -0700203 (*gammaTexture)->writePixels(0, 0, width, height,
204 (*gammaTexture)->config(), data.get(), 0,
205 GrContext::kDontFlush_PixelOpsFlag);
jvanverth2d2a68c2014-06-10 06:42:56 -0700206 }
207}
208
jvanverthaab626c2014-10-16 08:04:39 -0700209void GrDistanceFieldTextContext::onDrawText(const GrPaint& paint, const SkPaint& skPaint,
joshualitt5531d512014-12-17 15:50:11 -0800210 const SkMatrix& viewMatrix,
211 const char text[], size_t byteLength,
212 SkScalar x, SkScalar y) {
jvanverthaab626c2014-10-16 08:04:39 -0700213 SkASSERT(byteLength == 0 || text != NULL);
214
jvanverth2b9dc1d2014-10-20 06:48:59 -0700215 // nothing to draw
216 if (text == NULL || byteLength == 0) {
jvanverthaab626c2014-10-16 08:04:39 -0700217 return;
218 }
219
joshualitt5531d512014-12-17 15:50:11 -0800220 fViewMatrix = viewMatrix;
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
joshualitt5531d512014-12-17 15:50:11 -0800270 this->drawPosText(paint, skPaint, viewMatrix, 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,
joshualitt5531d512014-12-17 15:50:11 -0800274 const SkMatrix& viewMatrix,
275 const char text[], size_t byteLength,
276 const SkScalar pos[], int scalarsPerPosition,
277 const SkPoint& offset) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000278
279 SkASSERT(byteLength == 0 || text != NULL);
280 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
281
282 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000283 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000284 return;
285 }
286
joshualitt5531d512014-12-17 15:50:11 -0800287 fViewMatrix = viewMatrix;
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
jvanverth73f10532014-10-23 11:57:12 -0700298 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
299 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
300
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000301 const char* stop = text + byteLength;
jvanverthfca302c2014-10-20 13:12:54 -0700302 SkTArray<char> fallbackTxt;
303 SkTArray<SkScalar> fallbackPos;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000304
305 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
306 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700307 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000308 // the last 2 parameters are ignored
309 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
310
311 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700312 SkScalar x = offset.x() + pos[0];
313 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000314
jvanverthfca302c2014-10-20 13:12:54 -0700315 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
316 glyph.getSubXFixed(),
317 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700318 x, y, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700319 // couldn't append, send to fallback
bsalomonef3fcd82014-12-12 08:51:38 -0800320 fallbackTxt.push_back_n(SkToInt(text-lastText), lastText);
jvanverthfca302c2014-10-20 13:12:54 -0700321 fallbackPos.push_back(pos[0]);
322 if (2 == scalarsPerPosition) {
323 fallbackPos.push_back(pos[1]);
324 }
325 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000326 }
327 pos += scalarsPerPosition;
328 }
329 } else {
jvanverth2b9dc1d2014-10-20 06:48:59 -0700330 SkScalar alignMul = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? SK_ScalarHalf
331 : SK_Scalar1;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000332 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700333 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000334 // the last 2 parameters are ignored
335 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
336
337 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700338 SkScalar x = offset.x() + pos[0];
339 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000340
jvanverth2b9dc1d2014-10-20 06:48:59 -0700341 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX)*alignMul*fTextRatio;
342 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY)*alignMul*fTextRatio;
343
jvanverthfca302c2014-10-20 13:12:54 -0700344 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
345 glyph.getSubXFixed(),
346 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700347 x - advanceX, y - advanceY, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700348 // couldn't append, send to fallback
bsalomonef3fcd82014-12-12 08:51:38 -0800349 fallbackTxt.push_back_n(SkToInt(text-lastText), lastText);
jvanverthfca302c2014-10-20 13:12:54 -0700350 fallbackPos.push_back(pos[0]);
351 if (2 == scalarsPerPosition) {
352 fallbackPos.push_back(pos[1]);
353 }
354 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000355 }
356 pos += scalarsPerPosition;
357 }
358 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000359
360 this->finish();
jvanverthfca302c2014-10-20 13:12:54 -0700361
362 if (fallbackTxt.count() > 0) {
joshualitt5531d512014-12-17 15:50:11 -0800363 fFallbackTextContext->drawPosText(paint, skPaint, viewMatrix, fallbackTxt.begin(),
364 fallbackTxt.count(), fallbackPos.begin(),
365 scalarsPerPosition, offset);
jvanverthfca302c2014-10-20 13:12:54 -0700366 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000367}
jvanverth0fedb192014-10-08 09:07:27 -0700368
369static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
370 unsigned r = SkColorGetR(c);
371 unsigned g = SkColorGetG(c);
372 unsigned b = SkColorGetB(c);
373 return GrColorPackRGBA(r, g, b, 0xff);
374}
375
joshualitt9853cce2014-11-17 14:22:48 -0800376static size_t get_vertex_stride(bool useColorVerts) {
jvanverth5a105ff2015-02-18 11:36:35 -0800377 return useColorVerts ? (sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16)) :
378 (sizeof(SkPoint) + sizeof(SkIPoint16));
joshualitt9853cce2014-11-17 14:22:48 -0800379}
380
381static void* alloc_vertices(GrDrawTarget* drawTarget,
382 int numVertices,
383 bool useColorVerts) {
jvanverth73f10532014-10-23 11:57:12 -0700384 if (numVertices <= 0) {
385 return NULL;
386 }
387
jvanverth73f10532014-10-23 11:57:12 -0700388 void* vertices = NULL;
389 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
joshualitt9853cce2014-11-17 14:22:48 -0800390 get_vertex_stride(useColorVerts),
jvanverth73f10532014-10-23 11:57:12 -0700391 0,
392 &vertices,
393 NULL);
394 GrAlwaysAssert(success);
395 return vertices;
396}
397
jvanverth0fedb192014-10-08 09:07:27 -0700398void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
399 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
400 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
401
402 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
joshualitt5531d512014-12-17 15:50:11 -0800403 const SkMatrix& ctm = fViewMatrix;
jvanverth0fedb192014-10-08 09:07:27 -0700404
405 // set up any flags
406 uint32_t flags = 0;
407 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
408 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
409 flags |= fUseLCDText && ctm.rectStaysRect() ?
410 kRectToRect_DistanceFieldEffectFlag : 0;
reedb2d77e42014-10-14 08:26:33 -0700411 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
jvanverth0fedb192014-10-08 09:07:27 -0700412 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
413
414 // see if we need to create a new effect
415 if (textureUniqueID != fEffectTextureUniqueID ||
416 filteredColor != fEffectColor ||
joshualitt8059eb92014-12-29 15:10:07 -0800417 flags != fEffectFlags ||
418 !fCachedGeometryProcessor->viewMatrix().cheapEqualTo(fViewMatrix)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800419 GrColor color = fPaint.getColor();
jvanverth0fedb192014-10-08 09:07:27 -0700420 if (fUseLCDText) {
421 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
joshualitt2e3b3e32014-12-09 13:31:14 -0800422 fCachedGeometryProcessor.reset(GrDistanceFieldLCDTextureEffect::Create(color,
joshualitt8059eb92014-12-29 15:10:07 -0800423 fViewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800424 fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700425 params,
426 fGammaTexture,
427 gammaParams,
428 colorNoPreMul,
429 flags));
430 } else {
joshualitt2dd1ae02014-12-03 06:24:10 -0800431 flags |= kColorAttr_DistanceFieldEffectFlag;
joshualitt56995b52014-12-11 15:44:02 -0800432 bool opaque = GrColorIsOpaque(color);
jvanverth0fedb192014-10-08 09:07:27 -0700433#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700434 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700435 filteredColor);
joshualitt2e3b3e32014-12-09 13:31:14 -0800436 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(color,
joshualitt8059eb92014-12-29 15:10:07 -0800437 fViewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800438 fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700439 params,
440 fGammaTexture,
441 gammaParams,
442 lum/255.f,
joshualitt56995b52014-12-11 15:44:02 -0800443 flags,
444 opaque));
jvanverth0fedb192014-10-08 09:07:27 -0700445#else
jvanverth5a105ff2015-02-18 11:36:35 -0800446 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(color,
447 fViewMatrix,
448 fCurrTexture,
449 params,
450 flags,
451 opaque));
jvanverth0fedb192014-10-08 09:07:27 -0700452#endif
453 }
454 fEffectTextureUniqueID = textureUniqueID;
455 fEffectColor = filteredColor;
456 fEffectFlags = flags;
457 }
458
459}
460
jvanverth787cdf92014-12-04 10:46:50 -0800461inline bool GrDistanceFieldTextContext::uploadGlyph(GrGlyph* glyph, GrFontScaler* scaler) {
462 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
463 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
464 return true;
465 }
466
467 // try to clear out an unused plot before we flush
468 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
469 fStrike->addGlyphToAtlas(glyph, scaler)) {
470 return true;
471 }
472
473 if (c_DumpFontCache) {
474#ifdef SK_DEVELOPER
475 fContext->getFontCache()->dump();
476#endif
477 }
478
479 // before we purge the cache, we must flush any accumulated draws
480 this->flush();
481 fContext->flush();
482
483 // we should have an unused plot now
484 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
485 fStrike->addGlyphToAtlas(glyph, scaler)) {
486 return true;
487 }
488
489 // we should never get here
490 SkASSERT(false);
491 }
492
493 return false;
494}
495
496
jvanverthfca302c2014-10-20 13:12:54 -0700497// Returns true if this method handled the glyph, false if needs to be passed to fallback
498//
499bool GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
djsollen058f01e2014-10-30 11:54:43 -0700500 SkScalar sx, SkScalar sy,
jvanverth0fedb192014-10-08 09:07:27 -0700501 GrFontScaler* scaler) {
502 if (NULL == fDrawTarget) {
jvanverthfca302c2014-10-20 13:12:54 -0700503 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700504 }
505
506 if (NULL == fStrike) {
507 fStrike = fContext->getFontCache()->getStrike(scaler, true);
508 }
509
510 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
511 if (NULL == glyph || glyph->fBounds.isEmpty()) {
jvanverthfca302c2014-10-20 13:12:54 -0700512 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700513 }
514
jvanverthfca302c2014-10-20 13:12:54 -0700515 // fallback to color glyph support
jvanverth294c3262014-10-10 11:36:12 -0700516 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
jvanverthfca302c2014-10-20 13:12:54 -0700517 return false;
jvanverth294c3262014-10-10 11:36:12 -0700518 }
519
jvanverth2faa2282014-10-31 12:59:57 -0700520 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
521 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
522 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
523 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth0fedb192014-10-08 09:07:27 -0700524
jvanverth2faa2282014-10-31 12:59:57 -0700525 SkScalar scale = fTextRatio;
526 dx *= scale;
527 dy *= scale;
528 sx += dx;
529 sy += dy;
530 width *= scale;
531 height *= scale;
532 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
jvanverth0fedb192014-10-08 09:07:27 -0700533
534 // check if we clipped out
jvanverth2faa2282014-10-31 12:59:57 -0700535 SkRect dstRect;
joshualitt5531d512014-12-17 15:50:11 -0800536 const SkMatrix& ctm = fViewMatrix;
jvanverth2faa2282014-10-31 12:59:57 -0700537 (void) ctm.mapRect(&dstRect, glyphRect);
538 if (fClipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
539 SkScalarTruncToInt(dstRect.top()),
540 SkScalarTruncToInt(dstRect.right()),
541 SkScalarTruncToInt(dstRect.bottom()))) {
jvanverth2faa2282014-10-31 12:59:57 -0700542 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700543 }
jvanverth2faa2282014-10-31 12:59:57 -0700544
jvanverth95f9b5f2014-12-05 07:56:53 -0800545 if (NULL == glyph->fPlot) {
546 // needs to be a separate conditional to avoid over-optimization
547 // on Nexus 7 and Nexus 10
joshualittc2625822014-12-18 16:40:54 -0800548
549 // If the glyph is too large we fall back to paths
jvanverth95f9b5f2014-12-05 07:56:53 -0800550 if (!uploadGlyph(glyph, scaler)) {
551 if (NULL == glyph->fPath) {
552 SkPath* path = SkNEW(SkPath);
553 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
554 // flag the glyph as being dead?
555 delete path;
556 return true;
557 }
558 glyph->fPath = path;
jvanverth0fedb192014-10-08 09:07:27 -0700559 }
jvanverth95f9b5f2014-12-05 07:56:53 -0800560
561 // flush any accumulated draws before drawing this glyph as a path.
562 this->flush();
563
jvanverth95f9b5f2014-12-05 07:56:53 -0800564 SkMatrix ctm;
565 ctm.setScale(fTextRatio, fTextRatio);
566 ctm.postTranslate(sx - dx, sy - dy);
joshualitt5531d512014-12-17 15:50:11 -0800567
joshualittc2625822014-12-18 16:40:54 -0800568 SkPath tmpPath(*glyph->fPath);
569 tmpPath.transform(ctm);
570
jvanverth95f9b5f2014-12-05 07:56:53 -0800571 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
joshualittc2625822014-12-18 16:40:54 -0800572 fContext->drawPath(fPaint, fViewMatrix, tmpPath, strokeInfo);
jvanverth95f9b5f2014-12-05 07:56:53 -0800573
574 // remove this glyph from the vertices we need to allocate
575 fTotalVertexCount -= kVerticesPerGlyph;
576 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700577 }
jvanverth0fedb192014-10-08 09:07:27 -0700578 }
579
jvanverth0fedb192014-10-08 09:07:27 -0700580 SkASSERT(glyph->fPlot);
581 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
582 glyph->fPlot->setDrawToken(drawToken);
583
584 GrTexture* texture = glyph->fPlot->texture();
585 SkASSERT(texture);
586
jvanverth73f10532014-10-23 11:57:12 -0700587 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fTotalVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700588 this->flush();
589 fCurrTexture = texture;
590 fCurrTexture->ref();
591 }
592
593 bool useColorVerts = !fUseLCDText;
594
595 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700596 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
597 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
joshualitt9853cce2014-11-17 14:22:48 -0800598 fVertices = alloc_vertices(fDrawTarget,
599 fAllocVertexCount,
600 useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700601 }
602
jvanverth2faa2282014-10-31 12:59:57 -0700603 fVertexBounds.joinNonEmptyArg(glyphRect);
jvanverth0fedb192014-10-08 09:07:27 -0700604
jvanverth5a105ff2015-02-18 11:36:35 -0800605 int u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
606 int v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
607 int u1 = u0 + glyph->fBounds.width() - 2*SK_DistanceFieldInset;
608 int v1 = v0 + glyph->fBounds.height() - 2*SK_DistanceFieldInset;
609
joshualitt9853cce2014-11-17 14:22:48 -0800610 size_t vertSize = get_vertex_stride(useColorVerts);
jvanverth5a105ff2015-02-18 11:36:35 -0800611 intptr_t vertex = reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700612
jvanverth5a105ff2015-02-18 11:36:35 -0800613 // V0
614 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
615 position->set(glyphRect.fLeft, glyphRect.fTop);
jvanverth059034d2015-02-17 08:39:56 -0800616 if (useColorVerts) {
jvanverth5a105ff2015-02-18 11:36:35 -0800617 SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
618 *color = fPaint.getColor();
jvanverth059034d2015-02-17 08:39:56 -0800619 }
jvanverth5a105ff2015-02-18 11:36:35 -0800620 SkIPoint16* textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize -
621 sizeof(SkIPoint16));
622 textureCoords->set(u0, v0);
623 vertex += vertSize;
624
625 // V1
626 position = reinterpret_cast<SkPoint*>(vertex);
627 position->set(glyphRect.fLeft, glyphRect.fBottom);
628 if (useColorVerts) {
629 SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
630 *color = fPaint.getColor();
631 }
632 textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - sizeof(SkIPoint16));
633 textureCoords->set(u0, v1);
634 vertex += vertSize;
635
636 // V2
637 position = reinterpret_cast<SkPoint*>(vertex);
638 position->set(glyphRect.fRight, glyphRect.fBottom);
639 if (useColorVerts) {
640 SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
641 *color = fPaint.getColor();
642 }
643 textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - sizeof(SkIPoint16));
644 textureCoords->set(u1, v1);
645 vertex += vertSize;
646
647 // V3
648 position = reinterpret_cast<SkPoint*>(vertex);
649 position->set(glyphRect.fRight, glyphRect.fTop);
650 if (useColorVerts) {
651 SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
652 *color = fPaint.getColor();
653 }
654 textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - sizeof(SkIPoint16));
655 textureCoords->set(u1, v0);
jvanverth0fedb192014-10-08 09:07:27 -0700656
657 fCurrVertex += 4;
jvanverthfca302c2014-10-20 13:12:54 -0700658
659 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700660}
661
662void GrDistanceFieldTextContext::flush() {
663 if (NULL == fDrawTarget) {
664 return;
665 }
666
jvanverth0fedb192014-10-08 09:07:27 -0700667 if (fCurrVertex > 0) {
egdaniel8dd688b2015-01-22 10:16:09 -0800668 GrPipelineBuilder pipelineBuilder;
669 pipelineBuilder.setFromPaint(fPaint, fContext->getRenderTarget());
joshualitt780b11e2014-11-18 09:40:40 -0800670
jvanverth0fedb192014-10-08 09:07:27 -0700671 // setup our sampler state for our text texture/atlas
672 SkASSERT(SkIsAlign4(fCurrVertex));
673
674 // get our current color
675 SkColor filteredColor;
676 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
677 if (colorFilter) {
678 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
679 } else {
680 filteredColor = fSkPaint.getColor();
681 }
682 this->setupCoverageEffect(filteredColor);
683
jvanverth0fedb192014-10-08 09:07:27 -0700684 // Set draw state
685 if (fUseLCDText) {
egdaniel378092f2014-12-03 10:40:13 -0800686 // TODO: move supportsRGBCoverage check to setupCoverageEffect and only add LCD
687 // processor if the xp can support it. For now we will simply assume that if
688 // fUseLCDText is true, then we have a known color output.
egdaniel8dd688b2015-01-22 10:16:09 -0800689 const GrXPFactory* xpFactory = pipelineBuilder.getXPFactory();
690 if (!xpFactory->supportsRGBCoverage(0, kRGBA_GrColorComponentFlags)) {
tfarina38406c82014-10-31 07:11:12 -0700691 SkDebugf("LCD Text will not draw correctly.\n");
jvanverth0fedb192014-10-08 09:07:27 -0700692 }
joshualitt56995b52014-12-11 15:44:02 -0800693 SkASSERT(!fCachedGeometryProcessor->hasVertexColor());
jvanverth0fedb192014-10-08 09:07:27 -0700694 } else {
jvanverth0fedb192014-10-08 09:07:27 -0700695 // We're using per-vertex color.
joshualitt56995b52014-12-11 15:44:02 -0800696 SkASSERT(fCachedGeometryProcessor->hasVertexColor());
jvanverth0fedb192014-10-08 09:07:27 -0700697 }
jvanverth73f10532014-10-23 11:57:12 -0700698 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700699 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
egdaniel8dd688b2015-01-22 10:16:09 -0800700 fDrawTarget->drawIndexedInstances(&pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800701 fCachedGeometryProcessor.get(),
joshualitt9853cce2014-11-17 14:22:48 -0800702 kTriangles_GrPrimitiveType,
jvanverth0fedb192014-10-08 09:07:27 -0700703 nGlyphs,
joshualitt9853cce2014-11-17 14:22:48 -0800704 kVerticesPerGlyph,
705 kIndicesPerGlyph,
706 &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700707 fDrawTarget->resetVertexSource();
708 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700709 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700710 fCurrVertex = 0;
711 SkSafeSetNull(fCurrTexture);
712 fVertexBounds.setLargestInverted();
713 }
714}
715
716inline void GrDistanceFieldTextContext::finish() {
717 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700718 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700719
720 GrTextContext::finish();
721}
722