blob: 5b679d1c80cad90fb80f7ba6d41a83137bbb51d8 [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;
jvanverthada68ef2014-11-03 14:00:24 -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
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000082bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000083 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000084 return false;
85 }
86
skia.committer@gmail.come1d94432014-04-09 03:04:11 +000087 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000088 // translate well to distance
89 if (paint.getRasterizer() || paint.getMaskFilter() ||
90 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
91 return false;
92 }
93
94 // TODO: add some stroking support
95 if (paint.getStyle() != SkPaint::kFill_Style) {
96 return false;
97 }
98
99 // TODO: choose an appropriate maximum scale for distance fields and
100 // enable perspective
101 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
102 return false;
103 }
104
jvanverth2faa2282014-10-31 12:59:57 -0700105 return true;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000106}
107
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000108inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
109 GrTextContext::init(paint, skPaint);
110
111 fStrike = NULL;
112
jvanverth76ce81e2014-09-22 14:26:53 -0700113 const SkMatrix& ctm = fContext->getMatrix();
jvanverth9564ce62014-09-16 05:45:19 -0700114
115 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700116 SkASSERT(!ctm.hasPerspective());
117 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700118 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700119 SkScalar scaledTextSize = textSize;
120 // if we have non-unity scale, we need to choose our base text size
121 // based on the SkPaint's text size multiplied by the max scale factor
jvanverth9564ce62014-09-16 05:45:19 -0700122 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
123 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700124 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700125 }
126
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000127 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700128 fCurrVertex = 0;
129 fAllocVertexCount = 0;
130 fTotalVertexCount = 0;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000131
jvanverth76ce81e2014-09-22 14:26:53 -0700132 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700133 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000134 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800135#if DEBUG_TEXT_SIZE
136 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF));
137 fPaint.setColor(GrColorPackRGBA(0x00, 0x00, 0xFF, 0xFF));
138#endif
jvanverth76ce81e2014-09-22 14:26:53 -0700139 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700140 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000141 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800142#if DEBUG_TEXT_SIZE
143 fSkPaint.setColor(SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00));
144 fPaint.setColor(GrColorPackRGBA(0x00, 0xFF, 0x00, 0xFF));
145#endif
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000146 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700147 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000148 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
jvanverthada68ef2014-11-03 14:00:24 -0800149#if DEBUG_TEXT_SIZE
150 fSkPaint.setColor(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00));
151 fPaint.setColor(GrColorPackRGBA(0xFF, 0x00, 0x00, 0xFF));
152#endif
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000153 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000154
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000155 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000156
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000157 fSkPaint.setLCDRenderText(false);
158 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700159 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000160 fSkPaint.setSubpixelText(true);
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000161}
162
jvanverth2d2a68c2014-06-10 06:42:56 -0700163static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
164 const SkDeviceProperties& deviceProperties,
165 GrTexture** gammaTexture) {
166 if (NULL == *gammaTexture) {
167 int width, height;
168 size_t size;
169
170#ifdef SK_GAMMA_CONTRAST
171 SkScalar contrast = SK_GAMMA_CONTRAST;
172#else
173 SkScalar contrast = 0.5f;
174#endif
reedb2d77e42014-10-14 08:26:33 -0700175 SkScalar paintGamma = deviceProperties.gamma();
176 SkScalar deviceGamma = deviceProperties.gamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700177
178 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
179 &width, &height);
180
181 SkAutoTArray<uint8_t> data((int)size);
182 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
183
184 // TODO: Update this to use the cache rather than directly creating a texture.
bsalomonf2703d82014-10-28 14:33:06 -0700185 GrSurfaceDesc desc;
186 desc.fFlags = kNone_GrSurfaceFlags;
jvanverth2d2a68c2014-06-10 06:42:56 -0700187 desc.fWidth = width;
188 desc.fHeight = height;
189 desc.fConfig = kAlpha_8_GrPixelConfig;
190
191 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
192 if (NULL == *gammaTexture) {
193 return;
194 }
195
bsalomon81beccc2014-10-13 12:32:55 -0700196 (*gammaTexture)->writePixels(0, 0, width, height,
197 (*gammaTexture)->config(), data.get(), 0,
198 GrContext::kDontFlush_PixelOpsFlag);
jvanverth2d2a68c2014-06-10 06:42:56 -0700199 }
200}
201
jvanverthaab626c2014-10-16 08:04:39 -0700202void GrDistanceFieldTextContext::onDrawText(const GrPaint& paint, const SkPaint& skPaint,
203 const char text[], size_t byteLength,
204 SkScalar x, SkScalar y) {
205 SkASSERT(byteLength == 0 || text != NULL);
206
jvanverth2b9dc1d2014-10-20 06:48:59 -0700207 // nothing to draw
208 if (text == NULL || byteLength == 0) {
jvanverthaab626c2014-10-16 08:04:39 -0700209 return;
210 }
211
jvanverth2b9dc1d2014-10-20 06:48:59 -0700212 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
213 SkAutoGlyphCache autoCache(skPaint, &fDeviceProperties, NULL);
214 SkGlyphCache* cache = autoCache.getCache();
jvanverthaab626c2014-10-16 08:04:39 -0700215
jvanverth2b9dc1d2014-10-20 06:48:59 -0700216 SkTArray<SkScalar> positions;
jvanverthaab626c2014-10-16 08:04:39 -0700217
jvanverth2b9dc1d2014-10-20 06:48:59 -0700218 const char* textPtr = text;
219 SkFixed stopX = 0;
220 SkFixed stopY = 0;
221 SkFixed origin;
222 switch (skPaint.getTextAlign()) {
223 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
224 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
225 case SkPaint::kLeft_Align: origin = 0; break;
226 default: SkFAIL("Invalid paint origin"); return;
227 }
jvanverthaab626c2014-10-16 08:04:39 -0700228
jvanverth2b9dc1d2014-10-20 06:48:59 -0700229 SkAutoKern autokern;
jvanverthaab626c2014-10-16 08:04:39 -0700230 const char* stop = text + byteLength;
jvanverth2b9dc1d2014-10-20 06:48:59 -0700231 while (textPtr < stop) {
232 // don't need x, y here, since all subpixel variants will have the
233 // same advance
234 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
jvanverthaab626c2014-10-16 08:04:39 -0700235
jvanverth2b9dc1d2014-10-20 06:48:59 -0700236 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
237 positions.push_back(SkFixedToScalar(stopX + SkFixedMul_portable(origin, width)));
jvanverthaab626c2014-10-16 08:04:39 -0700238
jvanverth2b9dc1d2014-10-20 06:48:59 -0700239 SkFixed height = glyph.fAdvanceY;
240 positions.push_back(SkFixedToScalar(stopY + SkFixedMul_portable(origin, height)));
jvanverthaab626c2014-10-16 08:04:39 -0700241
jvanverth2b9dc1d2014-10-20 06:48:59 -0700242 stopX += width;
243 stopY += height;
jvanverthaab626c2014-10-16 08:04:39 -0700244 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700245 SkASSERT(textPtr == stop);
jvanverthaab626c2014-10-16 08:04:39 -0700246
jvanverth2b9dc1d2014-10-20 06:48:59 -0700247 // now adjust starting point depending on alignment
248 SkScalar alignX = SkFixedToScalar(stopX);
249 SkScalar alignY = SkFixedToScalar(stopY);
250 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
251 alignX = SkScalarHalf(alignX);
252 alignY = SkScalarHalf(alignY);
253 } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) {
254 alignX = 0;
255 alignY = 0;
jvanverthaab626c2014-10-16 08:04:39 -0700256 }
jvanverth2b9dc1d2014-10-20 06:48:59 -0700257 x -= alignX;
258 y -= alignY;
259 SkPoint offset = SkPoint::Make(x, y);
jvanverthaab626c2014-10-16 08:04:39 -0700260
jvanverth2b9dc1d2014-10-20 06:48:59 -0700261 this->drawPosText(paint, skPaint, text, byteLength, positions.begin(), 2, offset);
jvanverthaab626c2014-10-16 08:04:39 -0700262}
263
jvanverth8c27a182014-10-14 08:45:50 -0700264void GrDistanceFieldTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skPaint,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000265 const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -0700266 const SkScalar pos[], int scalarsPerPosition,
267 const SkPoint& offset) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000268
269 SkASSERT(byteLength == 0 || text != NULL);
270 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
271
272 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000273 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000274 return;
275 }
276
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000277 this->init(paint, skPaint);
278
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000279 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
280
jvanverth2d2a68c2014-06-10 06:42:56 -0700281 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
282 SkGlyphCache* cache = autoCache.getCache();
283 GrFontScaler* fontScaler = GetGrFontScaler(cache);
284
285 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000286
jvanverth73f10532014-10-23 11:57:12 -0700287 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
288 fTotalVertexCount = kVerticesPerGlyph*numGlyphs;
289
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000290 const char* stop = text + byteLength;
jvanverthfca302c2014-10-20 13:12:54 -0700291 SkTArray<char> fallbackTxt;
292 SkTArray<SkScalar> fallbackPos;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000293
294 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
295 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700296 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000297 // the last 2 parameters are ignored
298 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
299
300 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700301 SkScalar x = offset.x() + pos[0];
302 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000303
jvanverthfca302c2014-10-20 13:12:54 -0700304 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
305 glyph.getSubXFixed(),
306 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700307 x, y, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700308 // couldn't append, send to fallback
309 fallbackTxt.push_back_n(text-lastText, lastText);
310 fallbackPos.push_back(pos[0]);
311 if (2 == scalarsPerPosition) {
312 fallbackPos.push_back(pos[1]);
313 }
314 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000315 }
316 pos += scalarsPerPosition;
317 }
318 } else {
jvanverth2b9dc1d2014-10-20 06:48:59 -0700319 SkScalar alignMul = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? SK_ScalarHalf
320 : SK_Scalar1;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000321 while (text < stop) {
jvanverthfca302c2014-10-20 13:12:54 -0700322 const char* lastText = text;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000323 // the last 2 parameters are ignored
324 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
325
326 if (glyph.fWidth) {
fmalita05c4a432014-09-29 06:29:53 -0700327 SkScalar x = offset.x() + pos[0];
328 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000329
jvanverth2b9dc1d2014-10-20 06:48:59 -0700330 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX)*alignMul*fTextRatio;
331 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY)*alignMul*fTextRatio;
332
jvanverthfca302c2014-10-20 13:12:54 -0700333 if (!this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(),
334 glyph.getSubXFixed(),
335 glyph.getSubYFixed()),
djsollen058f01e2014-10-30 11:54:43 -0700336 x - advanceX, y - advanceY, fontScaler)) {
jvanverthfca302c2014-10-20 13:12:54 -0700337 // couldn't append, send to fallback
338 fallbackTxt.push_back_n(text-lastText, lastText);
339 fallbackPos.push_back(pos[0]);
340 if (2 == scalarsPerPosition) {
341 fallbackPos.push_back(pos[1]);
342 }
343 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000344 }
345 pos += scalarsPerPosition;
346 }
347 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000348
349 this->finish();
jvanverthfca302c2014-10-20 13:12:54 -0700350
351 if (fallbackTxt.count() > 0) {
352 fFallbackTextContext->drawPosText(paint, skPaint, fallbackTxt.begin(), fallbackTxt.count(),
353 fallbackPos.begin(), scalarsPerPosition, offset);
354 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000355}
jvanverth0fedb192014-10-08 09:07:27 -0700356
357static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
358 unsigned r = SkColorGetR(c);
359 unsigned g = SkColorGetG(c);
360 unsigned b = SkColorGetB(c);
361 return GrColorPackRGBA(r, g, b, 0xff);
362}
363
joshualitt9853cce2014-11-17 14:22:48 -0800364static size_t get_vertex_stride(bool useColorVerts) {
365 return useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) :
366 (2 * sizeof(SkPoint));
367}
368
369static void* alloc_vertices(GrDrawTarget* drawTarget,
370 int numVertices,
371 bool useColorVerts) {
jvanverth73f10532014-10-23 11:57:12 -0700372 if (numVertices <= 0) {
373 return NULL;
374 }
375
jvanverth73f10532014-10-23 11:57:12 -0700376 void* vertices = NULL;
377 bool success = drawTarget->reserveVertexAndIndexSpace(numVertices,
joshualitt9853cce2014-11-17 14:22:48 -0800378 get_vertex_stride(useColorVerts),
jvanverth73f10532014-10-23 11:57:12 -0700379 0,
380 &vertices,
381 NULL);
382 GrAlwaysAssert(success);
383 return vertices;
384}
385
jvanverth0fedb192014-10-08 09:07:27 -0700386void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
387 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
388 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
389
390 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
391 const SkMatrix& ctm = fContext->getMatrix();
392
393 // set up any flags
394 uint32_t flags = 0;
395 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
396 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
397 flags |= fUseLCDText && ctm.rectStaysRect() ?
398 kRectToRect_DistanceFieldEffectFlag : 0;
reedb2d77e42014-10-14 08:26:33 -0700399 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
jvanverth0fedb192014-10-08 09:07:27 -0700400 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
401
402 // see if we need to create a new effect
403 if (textureUniqueID != fEffectTextureUniqueID ||
404 filteredColor != fEffectColor ||
405 flags != fEffectFlags) {
406 if (fUseLCDText) {
407 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
408 fCachedGeometryProcessor.reset(GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
409 params,
410 fGammaTexture,
411 gammaParams,
412 colorNoPreMul,
413 flags));
414 } else {
joshualitt2dd1ae02014-12-03 06:24:10 -0800415 flags |= kColorAttr_DistanceFieldEffectFlag;
jvanverth0fedb192014-10-08 09:07:27 -0700416#ifdef SK_GAMMA_APPLY_TO_A8
reedb2d77e42014-10-14 08:26:33 -0700417 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.gamma(),
jvanverth0fedb192014-10-08 09:07:27 -0700418 filteredColor);
419 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
420 params,
421 fGammaTexture,
422 gammaParams,
423 lum/255.f,
424 flags));
425#else
jvanverth2faa2282014-10-31 12:59:57 -0700426 fCachedGeometryProcessor.reset(GrDistanceFieldNoGammaTextureEffect::Create(fCurrTexture,
jvanverth0fedb192014-10-08 09:07:27 -0700427 params, flags));
428#endif
429 }
430 fEffectTextureUniqueID = textureUniqueID;
431 fEffectColor = filteredColor;
432 fEffectFlags = flags;
433 }
434
435}
436
jvanverth787cdf92014-12-04 10:46:50 -0800437inline bool GrDistanceFieldTextContext::uploadGlyph(GrGlyph* glyph, GrFontScaler* scaler) {
438 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
439 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
440 return true;
441 }
442
443 // try to clear out an unused plot before we flush
444 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
445 fStrike->addGlyphToAtlas(glyph, scaler)) {
446 return true;
447 }
448
449 if (c_DumpFontCache) {
450#ifdef SK_DEVELOPER
451 fContext->getFontCache()->dump();
452#endif
453 }
454
455 // before we purge the cache, we must flush any accumulated draws
456 this->flush();
457 fContext->flush();
458
459 // we should have an unused plot now
460 if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) &&
461 fStrike->addGlyphToAtlas(glyph, scaler)) {
462 return true;
463 }
464
465 // we should never get here
466 SkASSERT(false);
467 }
468
469 return false;
470}
471
472
jvanverthfca302c2014-10-20 13:12:54 -0700473// Returns true if this method handled the glyph, false if needs to be passed to fallback
474//
475bool GrDistanceFieldTextContext::appendGlyph(GrGlyph::PackedID packed,
djsollen058f01e2014-10-30 11:54:43 -0700476 SkScalar sx, SkScalar sy,
jvanverth0fedb192014-10-08 09:07:27 -0700477 GrFontScaler* scaler) {
478 if (NULL == fDrawTarget) {
jvanverthfca302c2014-10-20 13:12:54 -0700479 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700480 }
481
482 if (NULL == fStrike) {
483 fStrike = fContext->getFontCache()->getStrike(scaler, true);
484 }
485
486 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
487 if (NULL == glyph || glyph->fBounds.isEmpty()) {
jvanverthfca302c2014-10-20 13:12:54 -0700488 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700489 }
490
jvanverthfca302c2014-10-20 13:12:54 -0700491 // fallback to color glyph support
jvanverth294c3262014-10-10 11:36:12 -0700492 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
jvanverthfca302c2014-10-20 13:12:54 -0700493 return false;
jvanverth294c3262014-10-10 11:36:12 -0700494 }
495
jvanverth2faa2282014-10-31 12:59:57 -0700496 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
497 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
498 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
499 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth0fedb192014-10-08 09:07:27 -0700500
jvanverth2faa2282014-10-31 12:59:57 -0700501 SkScalar scale = fTextRatio;
502 dx *= scale;
503 dy *= scale;
504 sx += dx;
505 sy += dy;
506 width *= scale;
507 height *= scale;
508 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
jvanverth0fedb192014-10-08 09:07:27 -0700509
510 // check if we clipped out
jvanverth2faa2282014-10-31 12:59:57 -0700511 SkRect dstRect;
512 const SkMatrix& ctm = fContext->getMatrix();
513 (void) ctm.mapRect(&dstRect, glyphRect);
514 if (fClipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
515 SkScalarTruncToInt(dstRect.top()),
516 SkScalarTruncToInt(dstRect.right()),
517 SkScalarTruncToInt(dstRect.bottom()))) {
jvanverth0fedb192014-10-08 09:07:27 -0700518// SkCLZ(3); // so we can set a break-point in the debugger
jvanverth2faa2282014-10-31 12:59:57 -0700519 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700520 }
jvanverth2faa2282014-10-31 12:59:57 -0700521
jvanverth95f9b5f2014-12-05 07:56:53 -0800522 if (NULL == glyph->fPlot) {
523 // needs to be a separate conditional to avoid over-optimization
524 // on Nexus 7 and Nexus 10
525 if (!uploadGlyph(glyph, scaler)) {
526 if (NULL == glyph->fPath) {
527 SkPath* path = SkNEW(SkPath);
528 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
529 // flag the glyph as being dead?
530 delete path;
531 return true;
532 }
533 glyph->fPath = path;
jvanverth0fedb192014-10-08 09:07:27 -0700534 }
jvanverth95f9b5f2014-12-05 07:56:53 -0800535
536 // flush any accumulated draws before drawing this glyph as a path.
537 this->flush();
538
539 GrContext::AutoMatrix am;
540 SkMatrix ctm;
541 ctm.setScale(fTextRatio, fTextRatio);
542 ctm.postTranslate(sx - dx, sy - dy);
543 GrPaint tmpPaint(fPaint);
544 am.setPreConcat(fContext, ctm, &tmpPaint);
545 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
546 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
547
548 // remove this glyph from the vertices we need to allocate
549 fTotalVertexCount -= kVerticesPerGlyph;
550 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700551 }
jvanverth0fedb192014-10-08 09:07:27 -0700552 }
553
jvanverth0fedb192014-10-08 09:07:27 -0700554 SkASSERT(glyph->fPlot);
555 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
556 glyph->fPlot->setDrawToken(drawToken);
557
558 GrTexture* texture = glyph->fPlot->texture();
559 SkASSERT(texture);
560
jvanverth73f10532014-10-23 11:57:12 -0700561 if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fTotalVertexCount) {
jvanverth0fedb192014-10-08 09:07:27 -0700562 this->flush();
563 fCurrTexture = texture;
564 fCurrTexture->ref();
565 }
566
567 bool useColorVerts = !fUseLCDText;
568
569 if (NULL == fVertices) {
jvanverth73f10532014-10-23 11:57:12 -0700570 int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads();
571 fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices);
joshualitt9853cce2014-11-17 14:22:48 -0800572 fVertices = alloc_vertices(fDrawTarget,
573 fAllocVertexCount,
574 useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700575 }
576
jvanverth0fedb192014-10-08 09:07:27 -0700577 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
578 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
579 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
580 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
581
jvanverth2faa2282014-10-31 12:59:57 -0700582 fVertexBounds.joinNonEmptyArg(glyphRect);
jvanverth0fedb192014-10-08 09:07:27 -0700583
joshualitt9853cce2014-11-17 14:22:48 -0800584 size_t vertSize = get_vertex_stride(useColorVerts);
jvanverth0fedb192014-10-08 09:07:27 -0700585
586 SkPoint* positions = reinterpret_cast<SkPoint*>(
587 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth2faa2282014-10-31 12:59:57 -0700588 positions->setRectFan(glyphRect.fLeft, glyphRect.fTop, glyphRect.fRight, glyphRect.fBottom,
589 vertSize);
jvanverth0fedb192014-10-08 09:07:27 -0700590
591 // The texture coords are last in both the with and without color vertex layouts.
592 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
593 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
594 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
595 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
596 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
597 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
598 vertSize);
599 if (useColorVerts) {
jvanverth0fedb192014-10-08 09:07:27 -0700600 // color comes after position.
601 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
602 for (int i = 0; i < 4; ++i) {
603 *colors = fPaint.getColor();
604 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
605 }
606 }
607
608 fCurrVertex += 4;
jvanverthfca302c2014-10-20 13:12:54 -0700609
610 return true;
jvanverth0fedb192014-10-08 09:07:27 -0700611}
612
613void GrDistanceFieldTextContext::flush() {
614 if (NULL == fDrawTarget) {
615 return;
616 }
617
jvanverth0fedb192014-10-08 09:07:27 -0700618 if (fCurrVertex > 0) {
joshualitt780b11e2014-11-18 09:40:40 -0800619 GrDrawState drawState;
620 drawState.setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
joshualitt780b11e2014-11-18 09:40:40 -0800621
jvanverth0fedb192014-10-08 09:07:27 -0700622 // setup our sampler state for our text texture/atlas
623 SkASSERT(SkIsAlign4(fCurrVertex));
624
625 // get our current color
626 SkColor filteredColor;
627 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
628 if (colorFilter) {
629 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
630 } else {
631 filteredColor = fSkPaint.getColor();
632 }
633 this->setupCoverageEffect(filteredColor);
634
635 // Effects could be stored with one of the cache objects (atlas?)
joshualitt9853cce2014-11-17 14:22:48 -0800636 drawState.setGeometryProcessor(fCachedGeometryProcessor.get());
jvanverth0fedb192014-10-08 09:07:27 -0700637
638 // Set draw state
639 if (fUseLCDText) {
640 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
egdaniel378092f2014-12-03 10:40:13 -0800641
642 // TODO: move supportsRGBCoverage check to setupCoverageEffect and only add LCD
643 // processor if the xp can support it. For now we will simply assume that if
644 // fUseLCDText is true, then we have a known color output.
645 if (!drawState.getXPFactory()->supportsRGBCoverage(0, kRGBA_GrColorComponentFlags)) {
tfarina38406c82014-10-31 07:11:12 -0700646 SkDebugf("LCD Text will not draw correctly.\n");
jvanverth0fedb192014-10-08 09:07:27 -0700647 }
joshualitt9853cce2014-11-17 14:22:48 -0800648 SkASSERT(!drawState.hasColorVertexAttribute());
jvanverth0fedb192014-10-08 09:07:27 -0700649 // We don't use the GrPaint's color in this case because it's been premultiplied by
650 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
651 // the mask texture color. The end result is that we get
652 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
653 int a = SkColorGetA(fSkPaint.getColor());
654 // paintAlpha
joshualitt9853cce2014-11-17 14:22:48 -0800655 drawState.setColor(SkColorSetARGB(a, a, a, a));
jvanverth0fedb192014-10-08 09:07:27 -0700656 // paintColor
joshualitt9853cce2014-11-17 14:22:48 -0800657 drawState.setBlendConstant(colorNoPreMul);
658 drawState.setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
jvanverth0fedb192014-10-08 09:07:27 -0700659 } else {
joshualitt9853cce2014-11-17 14:22:48 -0800660 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
661 drawState.setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
662 }
jvanverth0fedb192014-10-08 09:07:27 -0700663 // set back to normal in case we took LCD path previously.
joshualitt9853cce2014-11-17 14:22:48 -0800664 drawState.setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
jvanverth0fedb192014-10-08 09:07:27 -0700665 // We're using per-vertex color.
joshualitt9853cce2014-11-17 14:22:48 -0800666 SkASSERT(drawState.hasColorVertexAttribute());
jvanverth0fedb192014-10-08 09:07:27 -0700667 }
jvanverth73f10532014-10-23 11:57:12 -0700668 int nGlyphs = fCurrVertex / kVerticesPerGlyph;
jvanverth0fedb192014-10-08 09:07:27 -0700669 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
joshualitt9853cce2014-11-17 14:22:48 -0800670 fDrawTarget->drawIndexedInstances(&drawState,
671 kTriangles_GrPrimitiveType,
jvanverth0fedb192014-10-08 09:07:27 -0700672 nGlyphs,
joshualitt9853cce2014-11-17 14:22:48 -0800673 kVerticesPerGlyph,
674 kIndicesPerGlyph,
675 &fVertexBounds);
jvanverth0fedb192014-10-08 09:07:27 -0700676 fDrawTarget->resetVertexSource();
677 fVertices = NULL;
jvanverth73f10532014-10-23 11:57:12 -0700678 fTotalVertexCount -= fCurrVertex;
jvanverth0fedb192014-10-08 09:07:27 -0700679 fCurrVertex = 0;
680 SkSafeSetNull(fCurrTexture);
681 fVertexBounds.setLargestInverted();
682 }
683}
684
685inline void GrDistanceFieldTextContext::finish() {
686 this->flush();
jvanverth73f10532014-10-23 11:57:12 -0700687 fTotalVertexCount = 0;
jvanverth0fedb192014-10-08 09:07:27 -0700688
689 GrTextContext::finish();
690}
691