blob: 9fd024af9f1dff250d419c542525a35bed5a5ddd [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"
jvanverth2d2a68c2014-06-10 06:42:56 -070010#include "SkColorFilter.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000011#include "GrDrawTarget.h"
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +000012#include "GrDrawTargetCaps.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000013#include "GrFontScaler.h"
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +000014#include "SkGlyphCache.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"
jvanverth@google.comd830d132013-11-11 20:54:09 +000018#include "GrTextStrike.h"
19#include "GrTextStrike_impl.h"
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +000020#include "SkDistanceFieldGen.h"
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +000021#include "SkDraw.h"
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +000022#include "SkGpuDevice.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000023#include "SkPath.h"
24#include "SkRTConf.h"
25#include "SkStrokeRec.h"
26#include "effects/GrDistanceFieldTextureEffect.h"
27
jvanverthfeceba52014-07-25 19:03:34 -070028SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false,
29 "Dump the contents of the font cache before every purge.");
30
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +000031static const int kSmallDFFontSize = 32;
32static const int kSmallDFFontLimit = 32;
33static const int kMediumDFFontSize = 64;
34static const int kMediumDFFontLimit = 64;
35static const int kLargeDFFontSize = 128;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +000036
jvanverthfeceba52014-07-25 19:03:34 -070037namespace {
38// position + texture coord
39extern const GrVertexAttrib gTextVertexAttribs[] = {
40 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -070041 {kVec2f_GrVertexAttribType, sizeof(SkPoint) , kGeometryProcessor_GrVertexAttribBinding}
jvanverthfeceba52014-07-25 19:03:34 -070042};
43
egdaniel7b3d5ee2014-08-28 05:41:14 -070044static const size_t kTextVASize = 2 * sizeof(SkPoint);
45
jvanverthfeceba52014-07-25 19:03:34 -070046// position + color + texture coord
47extern const GrVertexAttrib gTextVertexWithColorAttribs[] = {
48 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
49 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
joshualittb0a8a372014-09-23 09:50:21 -070050 {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kGeometryProcessor_GrVertexAttribBinding}
jvanverthfeceba52014-07-25 19:03:34 -070051};
52
egdaniel7b3d5ee2014-08-28 05:41:14 -070053static const size_t kTextVAColorSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
54
jvanverthfeceba52014-07-25 19:03:34 -070055};
jvanverth@google.comd830d132013-11-11 20:54:09 +000056
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000057GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000058 const SkDeviceProperties& properties,
59 bool enable)
Mike Klein6a25bd02014-08-29 10:03:59 -040060 : GrTextContext(context, properties) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000061#if SK_FORCE_DISTANCEFIELD_FONTS
Mike Klein6a25bd02014-08-29 10:03:59 -040062 fEnableDFRendering = true;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000063#else
Mike Klein6a25bd02014-08-29 10:03:59 -040064 fEnableDFRendering = enable;
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000065#endif
Mike Klein6a25bd02014-08-29 10:03:59 -040066 fStrike = NULL;
67 fGammaTexture = NULL;
68
69 fCurrTexture = NULL;
70 fCurrVertex = 0;
71 fEffectTextureUniqueID = SK_InvalidUniqueID;
72 fEffectColor = GrColor_ILLEGAL;
73 fEffectFlags = 0;
74
75 fVertices = NULL;
76 fMaxVertices = 0;
77
jvanverth1723bfc2014-07-30 09:16:33 -070078 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +000079}
80
81GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
82 this->flushGlyphs();
jvanverth2d2a68c2014-06-10 06:42:56 -070083 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +000084}
85
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000086bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000087 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000088 return false;
89 }
90
skia.committer@gmail.come1d94432014-04-09 03:04:11 +000091 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000092 // translate well to distance
93 if (paint.getRasterizer() || paint.getMaskFilter() ||
94 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
95 return false;
96 }
97
98 // TODO: add some stroking support
99 if (paint.getStyle() != SkPaint::kFill_Style) {
100 return false;
101 }
102
103 // TODO: choose an appropriate maximum scale for distance fields and
104 // enable perspective
105 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
106 return false;
107 }
108
109 // distance fields cannot represent color fonts
110 SkScalerContext::Rec rec;
111 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
112 return rec.getFormat() != SkMask::kARGB32_Format;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000113}
114
jvanverth@google.comd830d132013-11-11 20:54:09 +0000115static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
116 unsigned r = SkColorGetR(c);
117 unsigned g = SkColorGetG(c);
118 unsigned b = SkColorGetB(c);
119 return GrColorPackRGBA(r, g, b, 0xff);
120}
121
jvanverth78f07182014-07-30 06:17:59 -0700122void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
123 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
124 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
125
Mike Klein6a25bd02014-08-29 10:03:59 -0400126 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
jvanverth76ce81e2014-09-22 14:26:53 -0700127 const SkMatrix& ctm = fContext->getMatrix();
jvanverth78f07182014-07-30 06:17:59 -0700128
129 // set up any flags
130 uint32_t flags = 0;
jvanverth76ce81e2014-09-22 14:26:53 -0700131 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
jvanverth78f07182014-07-30 06:17:59 -0700132 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
jvanverth76ce81e2014-09-22 14:26:53 -0700133 flags |= fUseLCDText && ctm.rectStaysRect() ?
jvanverth78f07182014-07-30 06:17:59 -0700134 kRectToRect_DistanceFieldEffectFlag : 0;
reed4a8126e2014-09-22 07:29:03 -0700135 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.fPixelGeometry);
jvanverth78f07182014-07-30 06:17:59 -0700136 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
137
138 // see if we need to create a new effect
139 if (textureUniqueID != fEffectTextureUniqueID ||
140 filteredColor != fEffectColor ||
141 flags != fEffectFlags) {
142 if (fUseLCDText) {
143 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
joshualittb0a8a372014-09-23 09:50:21 -0700144 fCachedGeometryProcessor.reset(
145 GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
146 params,
147 fGammaTexture,
148 gammaParams,
149 colorNoPreMul,
150 flags));
jvanverth78f07182014-07-30 06:17:59 -0700151 } else {
152#ifdef SK_GAMMA_APPLY_TO_A8
reed4a8126e2014-09-22 07:29:03 -0700153 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.getGamma(),
jvanverth78f07182014-07-30 06:17:59 -0700154 filteredColor);
joshualittb0a8a372014-09-23 09:50:21 -0700155 fCachedGeometryProcessor.reset(
156 GrDistanceFieldTextureEffect::Create(fCurrTexture,
157 params,
158 fGammaTexture,
159 gammaParams,
160 lum/255.f,
161 flags));
jvanverth78f07182014-07-30 06:17:59 -0700162#else
joshualittb0a8a372014-09-23 09:50:21 -0700163 fCachedGeometryProcessor.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
164 params, flags));
jvanverth78f07182014-07-30 06:17:59 -0700165#endif
166 }
167 fEffectTextureUniqueID = textureUniqueID;
168 fEffectColor = filteredColor;
169 fEffectFlags = flags;
170 }
171
172}
173
jvanverth@google.comd830d132013-11-11 20:54:09 +0000174void GrDistanceFieldTextContext::flushGlyphs() {
175 if (NULL == fDrawTarget) {
176 return;
177 }
178
179 GrDrawState* drawState = fDrawTarget->drawState();
180 GrDrawState::AutoRestoreEffects are(drawState);
jvanverth9564ce62014-09-16 05:45:19 -0700181
jvanverth76ce81e2014-09-22 14:26:53 -0700182 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000183
184 if (fCurrVertex > 0) {
185 // setup our sampler state for our text texture/atlas
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000186 SkASSERT(SkIsAlign4(fCurrVertex));
jvanverth@google.comd830d132013-11-11 20:54:09 +0000187
jvanverth78f07182014-07-30 06:17:59 -0700188 // get our current color
jvanverth2d2a68c2014-06-10 06:42:56 -0700189 SkColor filteredColor;
190 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700191 if (colorFilter) {
jvanverth2d2a68c2014-06-10 06:42:56 -0700192 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
193 } else {
194 filteredColor = fSkPaint.getColor();
195 }
jvanverth78f07182014-07-30 06:17:59 -0700196 this->setupCoverageEffect(filteredColor);
197
198 // Effects could be stored with one of the cache objects (atlas?)
joshualittb0a8a372014-09-23 09:50:21 -0700199 drawState->setGeometryProcessor(fCachedGeometryProcessor.get());
jvanverth78f07182014-07-30 06:17:59 -0700200
201 // Set draw state
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000202 if (fUseLCDText) {
jvanverth2d2a68c2014-06-10 06:42:56 -0700203 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000204 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
205 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
206 fPaint.numColorStages()) {
207 GrPrintf("LCD Text will not draw correctly.\n");
208 }
jvanverthfeceba52014-07-25 19:03:34 -0700209 SkASSERT(!drawState->hasColorVertexAttribute());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000210 // We don't use the GrPaint's color in this case because it's been premultiplied by
211 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
212 // the mask texture color. The end result is that we get
213 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000214 int a = SkColorGetA(fSkPaint.getColor());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000215 // paintAlpha
216 drawState->setColor(SkColorSetARGB(a, a, a, a));
217 // paintColor
jvanverth2d2a68c2014-06-10 06:42:56 -0700218 drawState->setBlendConstant(colorNoPreMul);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000219 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
220 } else {
221 // set back to normal in case we took LCD path previously.
222 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
jvanverthfeceba52014-07-25 19:03:34 -0700223 // We're using per-vertex color.
224 SkASSERT(drawState->hasColorVertexAttribute());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000225 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000226 int nGlyphs = fCurrVertex / 4;
227 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
228 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
229 nGlyphs,
jvanverth1723bfc2014-07-30 09:16:33 -0700230 4, 6, &fVertexBounds);
Mike Klein6a25bd02014-08-29 10:03:59 -0400231 fDrawTarget->resetVertexSource();
232 fVertices = NULL;
233 fMaxVertices = 0;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000234 fCurrVertex = 0;
Mike Klein6a25bd02014-08-29 10:03:59 -0400235 SkSafeSetNull(fCurrTexture);
jvanverth1723bfc2014-07-30 09:16:33 -0700236 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000237 }
238}
239
jvanverth@google.comd830d132013-11-11 20:54:09 +0000240void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000241 SkFixed vx, SkFixed vy,
jvanverth@google.comd830d132013-11-11 20:54:09 +0000242 GrFontScaler* scaler) {
Mike Klein6a25bd02014-08-29 10:03:59 -0400243 if (NULL == fDrawTarget) {
244 return;
245 }
246
247 if (NULL == fStrike) {
248 fStrike = fContext->getFontCache()->getStrike(scaler, true);
249 }
250
jvanverth@google.comd830d132013-11-11 20:54:09 +0000251 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
252 if (NULL == glyph || glyph->fBounds.isEmpty()) {
253 return;
254 }
255
256 SkScalar sx = SkFixedToScalar(vx);
257 SkScalar sy = SkFixedToScalar(vy);
258/*
259 // not valid, need to find a different solution for this
260 vx += SkIntToFixed(glyph->fBounds.fLeft);
261 vy += SkIntToFixed(glyph->fBounds.fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000262
jvanverth@google.comd830d132013-11-11 20:54:09 +0000263 // keep them as ints until we've done the clip-test
264 GrFixed width = glyph->fBounds.width();
265 GrFixed height = glyph->fBounds.height();
266
267 // check if we clipped out
268 if (true || NULL == glyph->fPlot) {
269 int x = vx >> 16;
270 int y = vy >> 16;
271 if (fClipRect.quickReject(x, y, x + width, y + height)) {
272// SkCLZ(3); // so we can set a break-point in the debugger
273 return;
274 }
275 }
276*/
277 if (NULL == glyph->fPlot) {
jvanverth681e65b2014-09-19 13:07:38 -0700278 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
279 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
280 goto HAS_ATLAS;
281 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000282
jvanverth681e65b2014-09-19 13:07:38 -0700283 // try to clear out an unused plot before we flush
284 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
285 fStrike->addGlyphToAtlas(glyph, scaler)) {
286 goto HAS_ATLAS;
287 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000288
jvanverth681e65b2014-09-19 13:07:38 -0700289 if (c_DumpFontCache) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000290#ifdef SK_DEVELOPER
jvanverth681e65b2014-09-19 13:07:38 -0700291 fContext->getFontCache()->dump();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000292#endif
jvanverth681e65b2014-09-19 13:07:38 -0700293 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000294
jvanverth681e65b2014-09-19 13:07:38 -0700295 // before we purge the cache, we must flush any accumulated draws
296 this->flushGlyphs();
297 fContext->flush();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000298
jvanverth681e65b2014-09-19 13:07:38 -0700299 // we should have an unused plot now
300 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
301 fStrike->addGlyphToAtlas(glyph, scaler)) {
302 goto HAS_ATLAS;
303 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000304 }
305
306 if (NULL == glyph->fPath) {
307 SkPath* path = SkNEW(SkPath);
308 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
309 // flag the glyph as being dead?
310 delete path;
311 return;
312 }
313 glyph->fPath = path;
314 }
315
316 GrContext::AutoMatrix am;
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000317 SkMatrix ctm;
318 ctm.setScale(fTextRatio, fTextRatio);
319 ctm.postTranslate(sx, sy);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000320 GrPaint tmpPaint(fPaint);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000321 am.setPreConcat(fContext, ctm, &tmpPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700322 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
323 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000324 return;
325 }
326
327HAS_ATLAS:
328 SkASSERT(glyph->fPlot);
329 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
330 glyph->fPlot->setDrawToken(drawToken);
331
332 GrTexture* texture = glyph->fPlot->texture();
333 SkASSERT(texture);
334
Mike Klein6a25bd02014-08-29 10:03:59 -0400335 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
336 this->flushGlyphs();
337 fCurrTexture = texture;
338 fCurrTexture->ref();
339 }
340
341 bool useColorVerts = !fUseLCDText;
342
343 if (NULL == fVertices) {
344 // If we need to reserve vertices allow the draw target to suggest
345 // a number of verts to reserve and whether to perform a flush.
346 fMaxVertices = kMinRequestedVerts;
347 if (useColorVerts) {
348 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
349 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
350 kTextVAColorSize);
351 } else {
352 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
353 SK_ARRAY_COUNT(gTextVertexAttribs),
354 kTextVASize);
355 }
356 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
357 if (flush) {
358 this->flushGlyphs();
359 fContext->flush();
360 if (useColorVerts) {
361 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
362 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
363 kTextVAColorSize);
364 } else {
365 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
366 SK_ARRAY_COUNT(gTextVertexAttribs),
367 kTextVASize);
368 }
369 }
370 fMaxVertices = kDefaultRequestedVerts;
371 // ignore return, no point in flushing again.
372 fDrawTarget->geometryHints(&fMaxVertices, NULL);
373
374 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
375 if (fMaxVertices < kMinRequestedVerts) {
376 fMaxVertices = kDefaultRequestedVerts;
377 } else if (fMaxVertices > maxQuadVertices) {
378 // don't exceed the limit of the index buffer
379 fMaxVertices = maxQuadVertices;
380 }
381 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
382 0,
383 &fVertices,
384 NULL);
385 GrAlwaysAssert(success);
386 }
387
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000388 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
389 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
390 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
391 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000392
393 SkScalar scale = fTextRatio;
394 dx *= scale;
395 dy *= scale;
396 sx += dx;
397 sy += dy;
398 width *= scale;
399 height *= scale;
Mike Klein6a25bd02014-08-29 10:03:59 -0400400
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000401 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
402 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
403 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
404 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000405
jvanverth1723bfc2014-07-30 09:16:33 -0700406 SkRect r;
jvanverth9bcd23b2014-08-01 14:05:19 -0700407 r.fLeft = sx;
408 r.fTop = sy;
409 r.fRight = sx + width;
410 r.fBottom = sy + height;
jvanverth1723bfc2014-07-30 09:16:33 -0700411
412 fVertexBounds.growToInclude(r);
413
jvanverthfeceba52014-07-25 19:03:34 -0700414 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
415 : (2 * sizeof(SkPoint) + sizeof(GrColor));
jvanverth1723bfc2014-07-30 09:16:33 -0700416
egdaniel7b3d5ee2014-08-28 05:41:14 -0700417 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride());
jvanverth1723bfc2014-07-30 09:16:33 -0700418
jvanverthf17bc6c2014-07-25 16:46:53 -0700419 SkPoint* positions = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700420 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth1723bfc2014-07-30 09:16:33 -0700421 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
422
jvanverthfeceba52014-07-25 19:03:34 -0700423 // The texture coords are last in both the with and without color vertex layouts.
jvanverthf17bc6c2014-07-25 16:46:53 -0700424 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700425 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
jvanverthf17bc6c2014-07-25 16:46:53 -0700426 textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
jvanverthfeceba52014-07-25 19:03:34 -0700427 SkFixedToFloat(texture->normalizeFixedY(ty)),
428 SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
429 SkFixedToFloat(texture->normalizeFixedY(ty + th)),
430 vertSize);
Mike Klein6a25bd02014-08-29 10:03:59 -0400431 if (useColorVerts) {
bsalomon62c447d2014-08-08 08:08:50 -0700432 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
433 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
434 }
jvanverthfeceba52014-07-25 19:03:34 -0700435 // color comes after position.
436 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
437 for (int i = 0; i < 4; ++i) {
438 *colors = fPaint.getColor();
439 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
440 }
441 }
jvanverth1723bfc2014-07-30 09:16:33 -0700442
jvanverth@google.comd830d132013-11-11 20:54:09 +0000443 fCurrVertex += 4;
444}
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000445
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000446inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
447 GrTextContext::init(paint, skPaint);
448
449 fStrike = NULL;
450
jvanverth76ce81e2014-09-22 14:26:53 -0700451 const SkMatrix& ctm = fContext->getMatrix();
jvanverth9564ce62014-09-16 05:45:19 -0700452
453 // getMaxScale doesn't support perspective, so neither do we at the moment
jvanverth76ce81e2014-09-22 14:26:53 -0700454 SkASSERT(!ctm.hasPerspective());
455 SkScalar maxScale = ctm.getMaxScale();
jvanverth9564ce62014-09-16 05:45:19 -0700456 SkScalar textSize = fSkPaint.getTextSize();
jvanverth76ce81e2014-09-22 14:26:53 -0700457 SkScalar scaledTextSize = textSize;
458 // if we have non-unity scale, we need to choose our base text size
459 // based on the SkPaint's text size multiplied by the max scale factor
jvanverth9564ce62014-09-16 05:45:19 -0700460 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
461 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
jvanverth76ce81e2014-09-22 14:26:53 -0700462 scaledTextSize *= maxScale;
jvanverth9564ce62014-09-16 05:45:19 -0700463 }
464
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000465 fCurrVertex = 0;
466
467 fVertices = NULL;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000468
jvanverth76ce81e2014-09-22 14:26:53 -0700469 if (scaledTextSize <= kSmallDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700470 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000471 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverth76ce81e2014-09-22 14:26:53 -0700472 } else if (scaledTextSize <= kMediumDFFontLimit) {
jvanverth9564ce62014-09-16 05:45:19 -0700473 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000474 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
475 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700476 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000477 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
478 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000479
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000480 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000481
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000482 fSkPaint.setLCDRenderText(false);
483 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700484 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000485 fSkPaint.setSubpixelText(true);
jvanverth2d2a68c2014-06-10 06:42:56 -0700486
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000487}
488
489inline void GrDistanceFieldTextContext::finish() {
jvanverthfeceba52014-07-25 19:03:34 -0700490 this->flushGlyphs();
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000491
492 GrTextContext::finish();
493}
494
jvanverth2d2a68c2014-06-10 06:42:56 -0700495static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
496 const SkDeviceProperties& deviceProperties,
497 GrTexture** gammaTexture) {
498 if (NULL == *gammaTexture) {
499 int width, height;
500 size_t size;
501
502#ifdef SK_GAMMA_CONTRAST
503 SkScalar contrast = SK_GAMMA_CONTRAST;
504#else
505 SkScalar contrast = 0.5f;
506#endif
reed4a8126e2014-09-22 07:29:03 -0700507 SkScalar paintGamma = deviceProperties.getGamma();
508 SkScalar deviceGamma = deviceProperties.getGamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700509
510 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
511 &width, &height);
512
513 SkAutoTArray<uint8_t> data((int)size);
514 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
515
516 // TODO: Update this to use the cache rather than directly creating a texture.
517 GrTextureDesc desc;
518 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
519 desc.fWidth = width;
520 desc.fHeight = height;
521 desc.fConfig = kAlpha_8_GrPixelConfig;
522
523 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
524 if (NULL == *gammaTexture) {
525 return;
526 }
527
528 context->writeTexturePixels(*gammaTexture,
529 0, 0, width, height,
530 (*gammaTexture)->config(), data.get(), 0,
531 GrContext::kDontFlush_PixelOpsFlag);
532 }
533}
534
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000535void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
536 const char text[], size_t byteLength,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000537 SkScalar x, SkScalar y) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000538 SkASSERT(byteLength == 0 || text != NULL);
539
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000540 // nothing to draw or can't draw
541 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
542 || fSkPaint.getRasterizer()) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000543 return;
544 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000545
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000546 this->init(paint, skPaint);
547
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000548 SkScalar sizeRatio = fTextRatio;
549
550 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
551
jvanverth2d2a68c2014-06-10 06:42:56 -0700552 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
553 SkGlyphCache* cache = autoCache.getCache();
554 GrFontScaler* fontScaler = GetGrFontScaler(cache);
555
556 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000557
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000558 // need to measure first
559 // TODO - generate positions and pre-load cache as well?
560 const char* stop = text + byteLength;
561 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
562 SkFixed stopX = 0;
563 SkFixed stopY = 0;
564
565 const char* textPtr = text;
566 while (textPtr < stop) {
567 // don't need x, y here, since all subpixel variants will have the
568 // same advance
569 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
570
571 stopX += glyph.fAdvanceX;
572 stopY += glyph.fAdvanceY;
573 }
574 SkASSERT(textPtr == stop);
575
576 SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio;
577 SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio;
578
579 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
580 alignX = SkScalarHalf(alignX);
581 alignY = SkScalarHalf(alignY);
582 }
583
584 x -= alignX;
585 y -= alignY;
586 }
587
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000588 SkFixed fx = SkScalarToFixed(x);
589 SkFixed fy = SkScalarToFixed(y);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000590 SkFixed fixedScale = SkScalarToFixed(sizeRatio);
591 while (text < stop) {
commit-bot@chromium.orga9dae712014-03-24 18:34:04 +0000592 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000593
594 if (glyph.fWidth) {
595 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
596 glyph.getSubXFixed(),
597 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000598 fx,
599 fy,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000600 fontScaler);
601 }
602
603 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale);
604 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale);
605 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000606
607 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000608}
609
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000610void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint,
611 const char text[], size_t byteLength,
robertphillipsd46b8d22014-09-29 04:48:52 -0700612 const SkScalar pos[], SkScalar constY,
613 int scalarsPerPosition) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000614
615 SkASSERT(byteLength == 0 || text != NULL);
616 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
617
618 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000619 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000620 return;
621 }
622
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000623 this->init(paint, skPaint);
624
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000625 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
626
jvanverth2d2a68c2014-06-10 06:42:56 -0700627 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
628 SkGlyphCache* cache = autoCache.getCache();
629 GrFontScaler* fontScaler = GetGrFontScaler(cache);
630
631 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000632
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000633 const char* stop = text + byteLength;
634
635 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
636 while (text < stop) {
637 // the last 2 parameters are ignored
638 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
639
640 if (glyph.fWidth) {
robertphillipsd46b8d22014-09-29 04:48:52 -0700641 SkScalar x = pos[0];
642 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000643
644 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
645 glyph.getSubXFixed(),
646 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000647 SkScalarToFixed(x),
648 SkScalarToFixed(y),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000649 fontScaler);
650 }
651 pos += scalarsPerPosition;
652 }
653 } else {
654 int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0;
655 while (text < stop) {
656 // the last 2 parameters are ignored
657 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
658
659 if (glyph.fWidth) {
robertphillipsd46b8d22014-09-29 04:48:52 -0700660 SkScalar x = pos[0];
661 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000662
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000663 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
664 glyph.getSubXFixed(),
665 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000666 SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift),
667 SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000668 fontScaler);
669 }
670 pos += scalarsPerPosition;
671 }
672 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000673
674 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000675}