blob: 4e8b16049f71a0d943255409979fc7cdcda49ae6 [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
joshualitt25d9c152015-02-18 12:29:52 -0800115inline void GrDistanceFieldTextContext::init(GrRenderTarget* rt, const GrPaint& paint,
116 const SkPaint& skPaint) {
117 GrTextContext::init(rt, paint, skPaint);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000118
119 fStrike = NULL;
120
joshualitt5531d512014-12-17 15:50:11 -0800121 const SkMatrix& ctm = fViewMatrix;
jvanverth9564ce62014-09-16 05:45:19 -0700122
123 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700124 SkASSERT(!ctm.hasPerspective());
125 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700126 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700127 SkScalar scaledTextSize = textSize;
128 // if we have non-unity scale, we need to choose our base text size
129 // based on the SkPaint's text size multiplied by the max scale factor
jvanverth9564ce62014-09-16 05:45:19 -0700130 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
131 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700132 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700133 }
134
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000135 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700136 fCurrVertex = 0;
137 fAllocVertexCount = 0;
138 fTotalVertexCount = 0;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000139
jvanverth76ce81e2014-09-22 14:26:53 -0700140 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700141 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000142 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800143#if DEBUG_TEXT_SIZE
144 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF));
145 fPaint.setColor(GrColorPackRGBA(0x00, 0x00, 0xFF, 0xFF));
146#endif
jvanverth76ce81e2014-09-22 14:26:53 -0700147 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700148 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000149 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800150#if DEBUG_TEXT_SIZE
151 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00));
152 fPaint.setColor(GrColorPackRGBA(0x00, 0xFF, 0x00, 0xFF));
153#endif
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000154 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700155 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000156 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800157#if DEBUG_TEXT_SIZE
158 fSkPaint.setColor(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00));
159 fPaint.setColor(GrColorPackRGBA(0xFF, 0x00, 0x00, 0xFF));
160#endif
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000161 }
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);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000169}
170
jvanverth2d2a68c2014-06-10 06:42:56 -0700171static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
172 const SkDeviceProperties& deviceProperties,
173 GrTexture** gammaTexture) {
174 if (NULL == *gammaTexture) {
175 int width, height;
176 size_t size;
177
178#ifdef SK_GAMMA_CONTRAST
179 SkScalar contrast = SK_GAMMA_CONTRAST;
180#else
181 SkScalar contrast = 0.5f;
182#endif
reedb2d77e42014-10-14 08:26:33 -0700183 SkScalar paintGamma = deviceProperties.gamma();
184 SkScalar deviceGamma = deviceProperties.gamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700185
186 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
187 &width, &height);
188
189 SkAutoTArray<uint8_t> data((int)size);
190 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
191
192 // TODO: Update this to use the cache rather than directly creating a texture.
bsalomonf2703d82014-10-28 14:33:06 -0700193 GrSurfaceDesc desc;
194 desc.fFlags = kNone_GrSurfaceFlags;
jvanverth2d2a68c2014-06-10 06:42:56 -0700195 desc.fWidth = width;
196 desc.fHeight = height;
197 desc.fConfig = kAlpha_8_GrPixelConfig;
198
bsalomon5236cf42015-01-14 10:42:08 -0800199 *gammaTexture = context->getGpu()->createTexture(desc, true, NULL, 0);
jvanverth2d2a68c2014-06-10 06:42:56 -0700200 if (NULL == *gammaTexture) {
201 return;
202 }
203
bsalomon81beccc2014-10-13 12:32:55 -0700204 (*gammaTexture)->writePixels(0, 0, width, height,
205 (*gammaTexture)->config(), data.get(), 0,
206 GrContext::kDontFlush_PixelOpsFlag);
jvanverth2d2a68c2014-06-10 06:42:56 -0700207 }
208}
209
joshualitt25d9c152015-02-18 12:29:52 -0800210void GrDistanceFieldTextContext::onDrawText(GrRenderTarget* rt, const GrPaint& paint,
211 const SkPaint& skPaint, const SkMatrix& viewMatrix,
joshualitt5531d512014-12-17 15:50:11 -0800212 const char text[], size_t byteLength,
213 SkScalar x, SkScalar y) {
jvanverthaab626c2014-10-16 08:04:39 -0700214 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
joshualitt5531d512014-12-17 15:50:11 -0800221 fViewMatrix = viewMatrix;
jvanverth2b9dc1d2014-10-20 06:48:59 -0700222 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
223 SkAutoGlyphCache autoCache(skPaint, &fDeviceProperties, NULL);
224 SkGlyphCache* cache = autoCache.getCache();
jvanverthaab626c2014-10-16 08:04:39 -0700225
jvanverth2b9dc1d2014-10-20 06:48:59 -0700226 SkTArray<SkScalar> positions;
jvanverthaab626c2014-10-16 08:04:39 -0700227
jvanverth2b9dc1d2014-10-20 06:48:59 -0700228 const char* textPtr = text;
229 SkFixed stopX = 0;
230 SkFixed stopY = 0;
231 SkFixed origin;
232 switch (skPaint.getTextAlign()) {
233 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
234 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
235 case SkPaint::kLeft_Align: origin = 0; break;
236 default: SkFAIL("Invalid paint origin"); return;
237 }
jvanverthaab626c2014-10-16 08:04:39 -0700238
jvanverth2b9dc1d2014-10-20 06:48:59 -0700239 SkAutoKern autokern;
jvanverthaab626c2014-10-16 08:04:39 -0700240 const char* stop = text + byteLength;
jvanverth2b9dc1d2014-10-20 06:48:59 -0700241 while (textPtr < stop) {
242 // don't need x, y here, since all subpixel variants will have the
243 // same advance
244 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
jvanverthaab626c2014-10-16 08:04:39 -0700245
jvanverth2b9dc1d2014-10-20 06:48:59 -0700246 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
247 positions.push_back(SkFixedToScalar(stopX + SkFixedMul_portable(origin, width)));
jvanverthaab626c2014-10-16 08:04:39 -0700248
jvanverth2b9dc1d2014-10-20 06:48:59 -0700249 SkFixed height = glyph.fAdvanceY;
250 positions.push_back(SkFixedToScalar(stopY + SkFixedMul_portable(origin, height)));
jvanverthaab626c2014-10-16 08:04:39 -0700251
jvanverth2b9dc1d2014-10-20 06:48:59 -0700252 stopX += width;
253 stopY += height;
jvanverthaab626c2014-10-16 08:04:39 -0700254 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700255 SkASSERT(textPtr == stop);
jvanverthaab626c2014-10-16 08:04:39 -0700256
jvanverth2b9dc1d2014-10-20 06:48:59 -0700257 // now adjust starting point depending on alignment
258 SkScalar alignX = SkFixedToScalar(stopX);
259 SkScalar alignY = SkFixedToScalar(stopY);
260 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
261 alignX = SkScalarHalf(alignX);
262 alignY = SkScalarHalf(alignY);
263 } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) {
264 alignX = 0;
265 alignY = 0;
jvanverthaab626c2014-10-16 08:04:39 -0700266 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700267 x -= alignX;
268 y -= alignY;
269 SkPoint offset = SkPoint::Make(x, y);
jvanverthaab626c2014-10-16 08:04:39 -0700270
joshualitt25d9c152015-02-18 12:29:52 -0800271 this->drawPosText(rt, paint, skPaint, viewMatrix, text, byteLength, positions.begin(), 2,
272 offset);
jvanverthaab626c2014-10-16 08:04:39 -0700273}
274
joshualitt25d9c152015-02-18 12:29:52 -0800275void GrDistanceFieldTextContext::onDrawPosText(GrRenderTarget* rt, const GrPaint& paint,
276 const SkPaint& skPaint, const SkMatrix& viewMatrix,
joshualitt5531d512014-12-17 15:50:11 -0800277 const char text[], size_t byteLength,
278 const SkScalar pos[], int scalarsPerPosition,
279 const SkPoint& offset) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000280
281 SkASSERT(byteLength == 0 || text != NULL);
282 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
283
284 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000285 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000286 return;
287 }
288
joshualitt5531d512014-12-17 15:50:11 -0800289 fViewMatrix = viewMatrix;
joshualitt25d9c152015-02-18 12:29:52 -0800290 this->init(rt, paint, skPaint);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000291
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000292 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
293
jvanverth2d2a68c2014-06-10 06:42:56 -0700294 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
295 SkGlyphCache* cache = autoCache.getCache();
296 GrFontScaler* fontScaler = GetGrFontScaler(cache);
297
298 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000299
jvanverth73f10532014-10-23 11:57:12 -0700300 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
301 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
302
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000303 const char* stop = text + byteLength;
jvanverthfca302c2014-10-20 13:12:54 -0700304 SkTArray<char> fallbackTxt;
305 SkTArray<SkScalar> fallbackPos;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000306
307 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
308 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700309 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000310 // the last 2 parameters are ignored
311 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
312
313 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700314 SkScalar x = offset.x() + pos[0];
315 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000316
jvanverthfca302c2014-10-20 13:12:54 -0700317 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
318 glyph.getSubXFixed(),
319 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700320 x, y, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700321 // couldn't append, send to fallback
bsalomonef3fcd82014-12-12 08:51:38 -0800322 fallbackTxt.push_back_n(SkToInt(text-lastText), lastText);
jvanverthfca302c2014-10-20 13:12:54 -0700323 fallbackPos.push_back(pos[0]);
324 if (2 == scalarsPerPosition) {
325 fallbackPos.push_back(pos[1]);
326 }
327 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000328 }
329 pos += scalarsPerPosition;
330 }
331 } else {
jvanverth2b9dc1d2014-10-20 06:48:59 -0700332 SkScalar alignMul = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? SK_ScalarHalf
333 : SK_Scalar1;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000334 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700335 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000336 // the last 2 parameters are ignored
337 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
338
339 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700340 SkScalar x = offset.x() + pos[0];
341 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000342
jvanverth2b9dc1d2014-10-20 06:48:59 -0700343 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX)*alignMul*fTextRatio;
344 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY)*alignMul*fTextRatio;
345
jvanverthfca302c2014-10-20 13:12:54 -0700346 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
347 glyph.getSubXFixed(),
348 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700349 x - advanceX, y - advanceY, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700350 // couldn't append, send to fallback
bsalomonef3fcd82014-12-12 08:51:38 -0800351 fallbackTxt.push_back_n(SkToInt(text-lastText), lastText);
jvanverthfca302c2014-10-20 13:12:54 -0700352 fallbackPos.push_back(pos[0]);
353 if (2 == scalarsPerPosition) {
354 fallbackPos.push_back(pos[1]);
355 }
356 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000357 }
358 pos += scalarsPerPosition;
359 }
360 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000361
362 this->finish();
jvanverthfca302c2014-10-20 13:12:54 -0700363
364 if (fallbackTxt.count() > 0) {
joshualitt25d9c152015-02-18 12:29:52 -0800365 fFallbackTextContext->drawPosText(rt, paint, skPaint, viewMatrix, fallbackTxt.begin(),
joshualitt5531d512014-12-17 15:50:11 -0800366 fallbackTxt.count(), fallbackPos.begin(),
367 scalarsPerPosition, offset);
jvanverthfca302c2014-10-20 13:12:54 -0700368 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000369}
jvanverth0fedb192014-10-08 09:07:27 -0700370
371static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
372 unsigned r = SkColorGetR(c);
373 unsigned g = SkColorGetG(c);
374 unsigned b = SkColorGetB(c);
375 return GrColorPackRGBA(r, g, b, 0xff);
376}
377
joshualitt9853cce2014-11-17 14:22:48 -0800378static size_t get_vertex_stride(bool useColorVerts) {
jvanverth5a105ff2015-02-18 11:36:35 -0800379 return useColorVerts ? (sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16)) :
380 (sizeof(SkPoint) + sizeof(SkIPoint16));
joshualitt9853cce2014-11-17 14:22:48 -0800381}
382
383static void* alloc_vertices(GrDrawTarget* drawTarget,
384 int numVertices,
385 bool useColorVerts) {
jvanverth73f10532014-10-23 11:57:12 -0700386 if (numVertices <= 0) {
387 return NULL;
388 }
389
jvanverth73f10532014-10-23 11:57:12 -0700390 void* vertices = NULL;
391 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
joshualitt9853cce2014-11-17 14:22:48 -0800392 get_vertex_stride(useColorVerts),
jvanverth73f10532014-10-23 11:57:12 -0700393 0,
394 &vertices,
395 NULL);
396 GrAlwaysAssert(success);
397 return vertices;
398}
399
jvanverth0fedb192014-10-08 09:07:27 -0700400void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
401 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
402 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
403
404 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
joshualitt5531d512014-12-17 15:50:11 -0800405 const SkMatrix& ctm = fViewMatrix;
jvanverth0fedb192014-10-08 09:07:27 -0700406
407 // set up any flags
408 uint32_t flags = 0;
409 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
410 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
411 flags |= fUseLCDText && ctm.rectStaysRect() ?
412 kRectToRect_DistanceFieldEffectFlag : 0;
reedb2d77e42014-10-14 08:26:33 -0700413 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
jvanverth0fedb192014-10-08 09:07:27 -0700414 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
415
416 // see if we need to create a new effect
417 if (textureUniqueID != fEffectTextureUniqueID ||
418 filteredColor != fEffectColor ||
joshualitt8059eb92014-12-29 15:10:07 -0800419 flags != fEffectFlags ||
420 !fCachedGeometryProcessor->viewMatrix().cheapEqualTo(fViewMatrix)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800421 GrColor color = fPaint.getColor();
jvanverth0fedb192014-10-08 09:07:27 -0700422 if (fUseLCDText) {
423 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
joshualitt2e3b3e32014-12-09 13:31:14 -0800424 fCachedGeometryProcessor.reset(GrDistanceFieldLCDTextureEffect::Create(color,
joshualitt8059eb92014-12-29 15:10:07 -0800425 fViewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800426 fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700427 params,
428 fGammaTexture,
429 gammaParams,
430 colorNoPreMul,
431 flags));
432 } else {
joshualitt2dd1ae02014-12-03 06:24:10 -0800433 flags |= kColorAttr_DistanceFieldEffectFlag;
joshualitt56995b52014-12-11 15:44:02 -0800434 bool opaque = GrColorIsOpaque(color);
jvanverth0fedb192014-10-08 09:07:27 -0700435#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700436 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700437 filteredColor);
joshualitt2e3b3e32014-12-09 13:31:14 -0800438 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(color,
joshualitt8059eb92014-12-29 15:10:07 -0800439 fViewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800440 fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700441 params,
442 fGammaTexture,
443 gammaParams,
444 lum/255.f,
joshualitt56995b52014-12-11 15:44:02 -0800445 flags,
446 opaque));
jvanverth0fedb192014-10-08 09:07:27 -0700447#else
jvanverth5a105ff2015-02-18 11:36:35 -0800448 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(color,
449 fViewMatrix,
450 fCurrTexture,
451 params,
452 flags,
453 opaque));
jvanverth0fedb192014-10-08 09:07:27 -0700454#endif
455 }
456 fEffectTextureUniqueID = textureUniqueID;
457 fEffectColor = filteredColor;
458 fEffectFlags = flags;
459 }
460
461}
462
jvanverth787cdf92014-12-04 10:46:50 -0800463inline bool GrDistanceFieldTextContext::uploadGlyph(GrGlyph* glyph, GrFontScaler* scaler) {
464 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
465 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
466 return true;
467 }
468
469 // try to clear out an unused plot before we flush
470 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
471 fStrike->addGlyphToAtlas(glyph, scaler)) {
472 return true;
473 }
474
475 if (c_DumpFontCache) {
476#ifdef SK_DEVELOPER
477 fContext->getFontCache()->dump();
478#endif
479 }
480
481 // before we purge the cache, we must flush any accumulated draws
482 this->flush();
483 fContext->flush();
484
485 // we should have an unused plot now
486 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
487 fStrike->addGlyphToAtlas(glyph, scaler)) {
488 return true;
489 }
490
491 // we should never get here
492 SkASSERT(false);
493 }
494
495 return false;
496}
497
498
jvanverthfca302c2014-10-20 13:12:54 -0700499// Returns true if this method handled the glyph, false if needs to be passed to fallback
500//
501bool GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
djsollen058f01e2014-10-30 11:54:43 -0700502 SkScalar sx, SkScalar sy,
jvanverth0fedb192014-10-08 09:07:27 -0700503 GrFontScaler* scaler) {
504 if (NULL == fDrawTarget) {
jvanverthfca302c2014-10-20 13:12:54 -0700505 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700506 }
507
508 if (NULL == fStrike) {
509 fStrike = fContext->getFontCache()->getStrike(scaler, true);
510 }
511
512 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
513 if (NULL == glyph || glyph->fBounds.isEmpty()) {
jvanverthfca302c2014-10-20 13:12:54 -0700514 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700515 }
516
jvanverthfca302c2014-10-20 13:12:54 -0700517 // fallback to color glyph support
jvanverth294c3262014-10-10 11:36:12 -0700518 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
jvanverthfca302c2014-10-20 13:12:54 -0700519 return false;
jvanverth294c3262014-10-10 11:36:12 -0700520 }
521
jvanverth2faa2282014-10-31 12:59:57 -0700522 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
523 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
524 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
525 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth0fedb192014-10-08 09:07:27 -0700526
jvanverth2faa2282014-10-31 12:59:57 -0700527 SkScalar scale = fTextRatio;
528 dx *= scale;
529 dy *= scale;
530 sx += dx;
531 sy += dy;
532 width *= scale;
533 height *= scale;
534 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
jvanverth0fedb192014-10-08 09:07:27 -0700535
536 // check if we clipped out
jvanverth2faa2282014-10-31 12:59:57 -0700537 SkRect dstRect;
joshualitt5531d512014-12-17 15:50:11 -0800538 const SkMatrix& ctm = fViewMatrix;
jvanverth2faa2282014-10-31 12:59:57 -0700539 (void) ctm.mapRect(&dstRect, glyphRect);
540 if (fClipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
541 SkScalarTruncToInt(dstRect.top()),
542 SkScalarTruncToInt(dstRect.right()),
543 SkScalarTruncToInt(dstRect.bottom()))) {
jvanverth2faa2282014-10-31 12:59:57 -0700544 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700545 }
jvanverth2faa2282014-10-31 12:59:57 -0700546
jvanverth95f9b5f2014-12-05 07:56:53 -0800547 if (NULL == glyph->fPlot) {
548 // needs to be a separate conditional to avoid over-optimization
549 // on Nexus 7 and Nexus 10
joshualittc2625822014-12-18 16:40:54 -0800550
551 // If the glyph is too large we fall back to paths
jvanverth95f9b5f2014-12-05 07:56:53 -0800552 if (!uploadGlyph(glyph, scaler)) {
553 if (NULL == glyph->fPath) {
554 SkPath* path = SkNEW(SkPath);
555 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
556 // flag the glyph as being dead?
557 delete path;
558 return true;
559 }
560 glyph->fPath = path;
jvanverth0fedb192014-10-08 09:07:27 -0700561 }
jvanverth95f9b5f2014-12-05 07:56:53 -0800562
563 // flush any accumulated draws before drawing this glyph as a path.
564 this->flush();
565
jvanverth95f9b5f2014-12-05 07:56:53 -0800566 SkMatrix ctm;
567 ctm.setScale(fTextRatio, fTextRatio);
568 ctm.postTranslate(sx - dx, sy - dy);
joshualitt5531d512014-12-17 15:50:11 -0800569
joshualittc2625822014-12-18 16:40:54 -0800570 SkPath tmpPath(*glyph->fPath);
571 tmpPath.transform(ctm);
572
jvanverth95f9b5f2014-12-05 07:56:53 -0800573 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
joshualitt25d9c152015-02-18 12:29:52 -0800574 fContext->drawPath(fRenderTarget, fPaint, fViewMatrix, tmpPath, strokeInfo);
jvanverth95f9b5f2014-12-05 07:56:53 -0800575
576 // remove this glyph from the vertices we need to allocate
577 fTotalVertexCount -= kVerticesPerGlyph;
578 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700579 }
jvanverth0fedb192014-10-08 09:07:27 -0700580 }
581
jvanverth0fedb192014-10-08 09:07:27 -0700582 SkASSERT(glyph->fPlot);
583 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
584 glyph->fPlot->setDrawToken(drawToken);
585
586 GrTexture* texture = glyph->fPlot->texture();
587 SkASSERT(texture);
588
jvanverth73f10532014-10-23 11:57:12 -0700589 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fTotalVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700590 this->flush();
591 fCurrTexture = texture;
592 fCurrTexture->ref();
593 }
594
595 bool useColorVerts = !fUseLCDText;
596
597 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700598 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
599 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
joshualitt9853cce2014-11-17 14:22:48 -0800600 fVertices = alloc_vertices(fDrawTarget,
601 fAllocVertexCount,
602 useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700603 }
604
jvanverth2faa2282014-10-31 12:59:57 -0700605 fVertexBounds.joinNonEmptyArg(glyphRect);
jvanverth0fedb192014-10-08 09:07:27 -0700606
jvanverth5a105ff2015-02-18 11:36:35 -0800607 int u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
608 int v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
609 int u1 = u0 + glyph->fBounds.width() - 2*SK_DistanceFieldInset;
610 int v1 = v0 + glyph->fBounds.height() - 2*SK_DistanceFieldInset;
611
joshualitt9853cce2014-11-17 14:22:48 -0800612 size_t vertSize = get_vertex_stride(useColorVerts);
jvanverth5a105ff2015-02-18 11:36:35 -0800613 intptr_t vertex = reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700614
jvanverth5a105ff2015-02-18 11:36:35 -0800615 // V0
616 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
617 position->set(glyphRect.fLeft, glyphRect.fTop);
jvanverth059034d2015-02-17 08:39:56 -0800618 if (useColorVerts) {
jvanverth5a105ff2015-02-18 11:36:35 -0800619 SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
620 *color = fPaint.getColor();
jvanverth059034d2015-02-17 08:39:56 -0800621 }
jvanverth5a105ff2015-02-18 11:36:35 -0800622 SkIPoint16* textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize -
623 sizeof(SkIPoint16));
624 textureCoords->set(u0, v0);
625 vertex += vertSize;
626
627 // V1
628 position = reinterpret_cast<SkPoint*>(vertex);
629 position->set(glyphRect.fLeft, glyphRect.fBottom);
630 if (useColorVerts) {
631 SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
632 *color = fPaint.getColor();
633 }
634 textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - sizeof(SkIPoint16));
635 textureCoords->set(u0, v1);
636 vertex += vertSize;
637
638 // V2
639 position = reinterpret_cast<SkPoint*>(vertex);
640 position->set(glyphRect.fRight, glyphRect.fBottom);
641 if (useColorVerts) {
642 SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
643 *color = fPaint.getColor();
644 }
645 textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - sizeof(SkIPoint16));
646 textureCoords->set(u1, v1);
647 vertex += vertSize;
648
649 // V3
650 position = reinterpret_cast<SkPoint*>(vertex);
651 position->set(glyphRect.fRight, glyphRect.fTop);
652 if (useColorVerts) {
653 SkColor* color = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
654 *color = fPaint.getColor();
655 }
656 textureCoords = reinterpret_cast<SkIPoint16*>(vertex + vertSize - sizeof(SkIPoint16));
657 textureCoords->set(u1, v0);
jvanverth0fedb192014-10-08 09:07:27 -0700658
659 fCurrVertex += 4;
jvanverthfca302c2014-10-20 13:12:54 -0700660
661 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700662}
663
664void GrDistanceFieldTextContext::flush() {
665 if (NULL == fDrawTarget) {
666 return;
667 }
668
jvanverth0fedb192014-10-08 09:07:27 -0700669 if (fCurrVertex > 0) {
egdaniel8dd688b2015-01-22 10:16:09 -0800670 GrPipelineBuilder pipelineBuilder;
joshualitt25d9c152015-02-18 12:29:52 -0800671 pipelineBuilder.setFromPaint(fPaint, fRenderTarget);
joshualitt780b11e2014-11-18 09:40:40 -0800672
jvanverth0fedb192014-10-08 09:07:27 -0700673 // setup our sampler state for our text texture/atlas
674 SkASSERT(SkIsAlign4(fCurrVertex));
675
676 // get our current color
677 SkColor filteredColor;
678 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
679 if (colorFilter) {
680 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
681 } else {
682 filteredColor = fSkPaint.getColor();
683 }
684 this->setupCoverageEffect(filteredColor);
685
jvanverth0fedb192014-10-08 09:07:27 -0700686 // Set draw state
687 if (fUseLCDText) {
egdaniel378092f2014-12-03 10:40:13 -0800688 // TODO: move supportsRGBCoverage check to setupCoverageEffect and only add LCD
689 // processor if the xp can support it. For now we will simply assume that if
690 // fUseLCDText is true, then we have a known color output.
egdaniel8dd688b2015-01-22 10:16:09 -0800691 const GrXPFactory* xpFactory = pipelineBuilder.getXPFactory();
692 if (!xpFactory->supportsRGBCoverage(0, kRGBA_GrColorComponentFlags)) {
tfarina38406c82014-10-31 07:11:12 -0700693 SkDebugf("LCD Text will not draw correctly.\n");
jvanverth0fedb192014-10-08 09:07:27 -0700694 }
joshualitt56995b52014-12-11 15:44:02 -0800695 SkASSERT(!fCachedGeometryProcessor->hasVertexColor());
jvanverth0fedb192014-10-08 09:07:27 -0700696 } else {
jvanverth0fedb192014-10-08 09:07:27 -0700697 // We're using per-vertex color.
joshualitt56995b52014-12-11 15:44:02 -0800698 SkASSERT(fCachedGeometryProcessor->hasVertexColor());
jvanverth0fedb192014-10-08 09:07:27 -0700699 }
jvanverth73f10532014-10-23 11:57:12 -0700700 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700701 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
egdaniel8dd688b2015-01-22 10:16:09 -0800702 fDrawTarget->drawIndexedInstances(&pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800703 fCachedGeometryProcessor.get(),
joshualitt9853cce2014-11-17 14:22:48 -0800704 kTriangles_GrPrimitiveType,
jvanverth0fedb192014-10-08 09:07:27 -0700705 nGlyphs,
joshualitt9853cce2014-11-17 14:22:48 -0800706 kVerticesPerGlyph,
707 kIndicesPerGlyph,
708 &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700709 fDrawTarget->resetVertexSource();
710 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700711 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700712 fCurrVertex = 0;
713 SkSafeSetNull(fCurrTexture);
714 fVertexBounds.setLargestInverted();
715 }
716}
717
718inline void GrDistanceFieldTextContext::finish() {
719 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700720 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700721
722 GrTextContext::finish();
723}
724