blob: df07204fcb352630293eb2585dce12c605bbb109 [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},
41 {kVec2f_GrVertexAttribType, sizeof(SkPoint) , kEffect_GrVertexAttribBinding}
42};
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},
50 {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kEffect_GrVertexAttribBinding}
51};
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();
jvanverth78f07182014-07-30 06:17:59 -0700127
128 // set up any flags
129 uint32_t flags = 0;
jvanverth9564ce62014-09-16 05:45:19 -0700130 flags |= fTextMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
jvanverth78f07182014-07-30 06:17:59 -0700131 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
jvanverth9564ce62014-09-16 05:45:19 -0700132 flags |= fUseLCDText && fTextMatrix.rectStaysRect() ?
jvanverth78f07182014-07-30 06:17:59 -0700133 kRectToRect_DistanceFieldEffectFlag : 0;
reed3716fd02014-09-21 09:39:55 -0700134 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.fPixelGeometry);
jvanverth78f07182014-07-30 06:17:59 -0700135 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
136
137 // see if we need to create a new effect
138 if (textureUniqueID != fEffectTextureUniqueID ||
139 filteredColor != fEffectColor ||
140 flags != fEffectFlags) {
141 if (fUseLCDText) {
142 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
Mike Klein6a25bd02014-08-29 10:03:59 -0400143 fCachedEffect.reset(GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
jvanverth78f07182014-07-30 06:17:59 -0700144 params,
145 fGammaTexture,
146 gammaParams,
147 colorNoPreMul,
148 flags));
149 } else {
150#ifdef SK_GAMMA_APPLY_TO_A8
reed3716fd02014-09-21 09:39:55 -0700151 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.getGamma(),
jvanverth78f07182014-07-30 06:17:59 -0700152 filteredColor);
Mike Klein6a25bd02014-08-29 10:03:59 -0400153 fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
jvanverth78f07182014-07-30 06:17:59 -0700154 params,
155 fGammaTexture,
156 gammaParams,
157 lum/255.f,
158 flags));
159#else
Mike Klein6a25bd02014-08-29 10:03:59 -0400160 fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
jvanverth78f07182014-07-30 06:17:59 -0700161 params, flags));
162#endif
163 }
164 fEffectTextureUniqueID = textureUniqueID;
165 fEffectColor = filteredColor;
166 fEffectFlags = flags;
167 }
168
169}
170
jvanverth@google.comd830d132013-11-11 20:54:09 +0000171void GrDistanceFieldTextContext::flushGlyphs() {
172 if (NULL == fDrawTarget) {
173 return;
174 }
175
176 GrDrawState* drawState = fDrawTarget->drawState();
177 GrDrawState::AutoRestoreEffects are(drawState);
jvanverth9564ce62014-09-16 05:45:19 -0700178
179 drawState->setFromPaint(fPaint, fTextMatrix, fContext->getRenderTarget());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000180
181 if (fCurrVertex > 0) {
182 // setup our sampler state for our text texture/atlas
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000183 SkASSERT(SkIsAlign4(fCurrVertex));
jvanverth@google.comd830d132013-11-11 20:54:09 +0000184
jvanverth78f07182014-07-30 06:17:59 -0700185 // get our current color
jvanverth2d2a68c2014-06-10 06:42:56 -0700186 SkColor filteredColor;
187 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700188 if (colorFilter) {
jvanverth2d2a68c2014-06-10 06:42:56 -0700189 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
190 } else {
191 filteredColor = fSkPaint.getColor();
192 }
jvanverth78f07182014-07-30 06:17:59 -0700193 this->setupCoverageEffect(filteredColor);
194
195 // Effects could be stored with one of the cache objects (atlas?)
joshualitt249af152014-09-15 11:41:13 -0700196 drawState->setGeometryProcessor(fCachedEffect.get());
jvanverth78f07182014-07-30 06:17:59 -0700197
198 // Set draw state
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000199 if (fUseLCDText) {
jvanverth2d2a68c2014-06-10 06:42:56 -0700200 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000201 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
202 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
203 fPaint.numColorStages()) {
204 GrPrintf("LCD Text will not draw correctly.\n");
205 }
jvanverthfeceba52014-07-25 19:03:34 -0700206 SkASSERT(!drawState->hasColorVertexAttribute());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000207 // We don't use the GrPaint's color in this case because it's been premultiplied by
208 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
209 // the mask texture color. The end result is that we get
210 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000211 int a = SkColorGetA(fSkPaint.getColor());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000212 // paintAlpha
213 drawState->setColor(SkColorSetARGB(a, a, a, a));
214 // paintColor
jvanverth2d2a68c2014-06-10 06:42:56 -0700215 drawState->setBlendConstant(colorNoPreMul);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000216 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
217 } else {
218 // set back to normal in case we took LCD path previously.
219 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
jvanverthfeceba52014-07-25 19:03:34 -0700220 // We're using per-vertex color.
221 SkASSERT(drawState->hasColorVertexAttribute());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000222 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000223 int nGlyphs = fCurrVertex / 4;
224 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
225 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
226 nGlyphs,
jvanverth1723bfc2014-07-30 09:16:33 -0700227 4, 6, &fVertexBounds);
Mike Klein6a25bd02014-08-29 10:03:59 -0400228 fDrawTarget->resetVertexSource();
229 fVertices = NULL;
230 fMaxVertices = 0;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000231 fCurrVertex = 0;
Mike Klein6a25bd02014-08-29 10:03:59 -0400232 SkSafeSetNull(fCurrTexture);
jvanverth1723bfc2014-07-30 09:16:33 -0700233 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000234 }
235}
236
jvanverth@google.comd830d132013-11-11 20:54:09 +0000237void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000238 SkFixed vx, SkFixed vy,
jvanverth@google.comd830d132013-11-11 20:54:09 +0000239 GrFontScaler* scaler) {
Mike Klein6a25bd02014-08-29 10:03:59 -0400240 if (NULL == fDrawTarget) {
241 return;
242 }
243
244 if (NULL == fStrike) {
245 fStrike = fContext->getFontCache()->getStrike(scaler, true);
246 }
247
jvanverth@google.comd830d132013-11-11 20:54:09 +0000248 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
249 if (NULL == glyph || glyph->fBounds.isEmpty()) {
250 return;
251 }
252
253 SkScalar sx = SkFixedToScalar(vx);
254 SkScalar sy = SkFixedToScalar(vy);
255/*
256 // not valid, need to find a different solution for this
257 vx += SkIntToFixed(glyph->fBounds.fLeft);
258 vy += SkIntToFixed(glyph->fBounds.fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000259
jvanverth@google.comd830d132013-11-11 20:54:09 +0000260 // keep them as ints until we've done the clip-test
261 GrFixed width = glyph->fBounds.width();
262 GrFixed height = glyph->fBounds.height();
263
264 // check if we clipped out
265 if (true || NULL == glyph->fPlot) {
266 int x = vx >> 16;
267 int y = vy >> 16;
268 if (fClipRect.quickReject(x, y, x + width, y + height)) {
269// SkCLZ(3); // so we can set a break-point in the debugger
270 return;
271 }
272 }
273*/
274 if (NULL == glyph->fPlot) {
jvanverth681e65b2014-09-19 13:07:38 -0700275 if (!fStrike->glyphTooLargeForAtlas(glyph)) {
276 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
277 goto HAS_ATLAS;
278 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000279
jvanverth681e65b2014-09-19 13:07:38 -0700280 // try to clear out an unused plot before we flush
281 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
282 fStrike->addGlyphToAtlas(glyph, scaler)) {
283 goto HAS_ATLAS;
284 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000285
jvanverth681e65b2014-09-19 13:07:38 -0700286 if (c_DumpFontCache) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000287#ifdef SK_DEVELOPER
jvanverth681e65b2014-09-19 13:07:38 -0700288 fContext->getFontCache()->dump();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000289#endif
jvanverth681e65b2014-09-19 13:07:38 -0700290 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000291
jvanverth681e65b2014-09-19 13:07:38 -0700292 // before we purge the cache, we must flush any accumulated draws
293 this->flushGlyphs();
294 fContext->flush();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000295
jvanverth681e65b2014-09-19 13:07:38 -0700296 // we should have an unused plot now
297 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
298 fStrike->addGlyphToAtlas(glyph, scaler)) {
299 goto HAS_ATLAS;
300 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000301 }
302
303 if (NULL == glyph->fPath) {
304 SkPath* path = SkNEW(SkPath);
305 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
306 // flag the glyph as being dead?
307 delete path;
308 return;
309 }
310 glyph->fPath = path;
311 }
312
313 GrContext::AutoMatrix am;
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000314 SkMatrix ctm;
315 ctm.setScale(fTextRatio, fTextRatio);
316 ctm.postTranslate(sx, sy);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000317 GrPaint tmpPaint(fPaint);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000318 am.setPreConcat(fContext, ctm, &tmpPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700319 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
320 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000321 return;
322 }
323
324HAS_ATLAS:
325 SkASSERT(glyph->fPlot);
326 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
327 glyph->fPlot->setDrawToken(drawToken);
328
329 GrTexture* texture = glyph->fPlot->texture();
330 SkASSERT(texture);
331
Mike Klein6a25bd02014-08-29 10:03:59 -0400332 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
333 this->flushGlyphs();
334 fCurrTexture = texture;
335 fCurrTexture->ref();
336 }
337
338 bool useColorVerts = !fUseLCDText;
339
340 if (NULL == fVertices) {
341 // If we need to reserve vertices allow the draw target to suggest
342 // a number of verts to reserve and whether to perform a flush.
343 fMaxVertices = kMinRequestedVerts;
344 if (useColorVerts) {
345 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
346 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
347 kTextVAColorSize);
348 } else {
349 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
350 SK_ARRAY_COUNT(gTextVertexAttribs),
351 kTextVASize);
352 }
353 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
354 if (flush) {
355 this->flushGlyphs();
356 fContext->flush();
357 if (useColorVerts) {
358 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
359 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
360 kTextVAColorSize);
361 } else {
362 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
363 SK_ARRAY_COUNT(gTextVertexAttribs),
364 kTextVASize);
365 }
366 }
367 fMaxVertices = kDefaultRequestedVerts;
368 // ignore return, no point in flushing again.
369 fDrawTarget->geometryHints(&fMaxVertices, NULL);
370
371 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
372 if (fMaxVertices < kMinRequestedVerts) {
373 fMaxVertices = kDefaultRequestedVerts;
374 } else if (fMaxVertices > maxQuadVertices) {
375 // don't exceed the limit of the index buffer
376 fMaxVertices = maxQuadVertices;
377 }
378 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
379 0,
380 &fVertices,
381 NULL);
382 GrAlwaysAssert(success);
383 }
384
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000385 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
386 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
387 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
388 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000389
390 SkScalar scale = fTextRatio;
391 dx *= scale;
392 dy *= scale;
393 sx += dx;
394 sy += dy;
395 width *= scale;
396 height *= scale;
Mike Klein6a25bd02014-08-29 10:03:59 -0400397
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000398 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
399 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
400 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
401 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000402
jvanverth1723bfc2014-07-30 09:16:33 -0700403 SkRect r;
jvanverth9bcd23b2014-08-01 14:05:19 -0700404 r.fLeft = sx;
405 r.fTop = sy;
406 r.fRight = sx + width;
407 r.fBottom = sy + height;
jvanverth1723bfc2014-07-30 09:16:33 -0700408
409 fVertexBounds.growToInclude(r);
410
jvanverthfeceba52014-07-25 19:03:34 -0700411 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
412 : (2 * sizeof(SkPoint) + sizeof(GrColor));
jvanverth1723bfc2014-07-30 09:16:33 -0700413
egdaniel7b3d5ee2014-08-28 05:41:14 -0700414 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride());
jvanverth1723bfc2014-07-30 09:16:33 -0700415
jvanverthf17bc6c2014-07-25 16:46:53 -0700416 SkPoint* positions = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700417 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth1723bfc2014-07-30 09:16:33 -0700418 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
419
jvanverthfeceba52014-07-25 19:03:34 -0700420 // The texture coords are last in both the with and without color vertex layouts.
jvanverthf17bc6c2014-07-25 16:46:53 -0700421 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700422 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
jvanverthf17bc6c2014-07-25 16:46:53 -0700423 textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
jvanverthfeceba52014-07-25 19:03:34 -0700424 SkFixedToFloat(texture->normalizeFixedY(ty)),
425 SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
426 SkFixedToFloat(texture->normalizeFixedY(ty + th)),
427 vertSize);
Mike Klein6a25bd02014-08-29 10:03:59 -0400428 if (useColorVerts) {
bsalomon62c447d2014-08-08 08:08:50 -0700429 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
430 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
431 }
jvanverthfeceba52014-07-25 19:03:34 -0700432 // color comes after position.
433 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
434 for (int i = 0; i < 4; ++i) {
435 *colors = fPaint.getColor();
436 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
437 }
438 }
jvanverth1723bfc2014-07-30 09:16:33 -0700439
jvanverth@google.comd830d132013-11-11 20:54:09 +0000440 fCurrVertex += 4;
441}
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000442
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000443inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
444 GrTextContext::init(paint, skPaint);
445
446 fStrike = NULL;
447
jvanverth9564ce62014-09-16 05:45:19 -0700448 fTextMatrix = fContext->getMatrix();
449
450 // getMaxScale doesn't support perspective, so neither do we at the moment
451 SkASSERT(!fTextMatrix.hasPerspective());
452 SkScalar maxScale = fTextMatrix.getMaxScale();
453 SkScalar textSize = fSkPaint.getTextSize();
454 // if we have non-unity scale, we need to adjust our text size accordingly
455 // to avoid aliasing, and prescale the matrix by the inverse to end up with the same size
456 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
457 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
458 textSize *= maxScale;
459 fTextMatrix.preScale(SK_Scalar1 / maxScale, SK_Scalar1 / maxScale);
460 }
461
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000462 fCurrVertex = 0;
463
464 fVertices = NULL;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000465
jvanverth9564ce62014-09-16 05:45:19 -0700466 if (textSize <= kSmallDFFontLimit) {
467 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000468 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverth9564ce62014-09-16 05:45:19 -0700469 } else if (textSize <= kMediumDFFontLimit) {
470 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000471 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
472 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700473 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000474 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
475 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000476
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000477 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000478
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000479 fSkPaint.setLCDRenderText(false);
480 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700481 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000482 fSkPaint.setSubpixelText(true);
jvanverth2d2a68c2014-06-10 06:42:56 -0700483
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000484}
485
486inline void GrDistanceFieldTextContext::finish() {
jvanverthfeceba52014-07-25 19:03:34 -0700487 this->flushGlyphs();
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000488
489 GrTextContext::finish();
490}
491
jvanverth2d2a68c2014-06-10 06:42:56 -0700492static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
493 const SkDeviceProperties& deviceProperties,
494 GrTexture** gammaTexture) {
495 if (NULL == *gammaTexture) {
496 int width, height;
497 size_t size;
498
499#ifdef SK_GAMMA_CONTRAST
500 SkScalar contrast = SK_GAMMA_CONTRAST;
501#else
502 SkScalar contrast = 0.5f;
503#endif
reed3716fd02014-09-21 09:39:55 -0700504 SkScalar paintGamma = deviceProperties.getGamma();
505 SkScalar deviceGamma = deviceProperties.getGamma();
jvanverth2d2a68c2014-06-10 06:42:56 -0700506
507 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
508 &width, &height);
509
510 SkAutoTArray<uint8_t> data((int)size);
511 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
512
513 // TODO: Update this to use the cache rather than directly creating a texture.
514 GrTextureDesc desc;
515 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
516 desc.fWidth = width;
517 desc.fHeight = height;
518 desc.fConfig = kAlpha_8_GrPixelConfig;
519
520 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
521 if (NULL == *gammaTexture) {
522 return;
523 }
524
525 context->writeTexturePixels(*gammaTexture,
526 0, 0, width, height,
527 (*gammaTexture)->config(), data.get(), 0,
528 GrContext::kDontFlush_PixelOpsFlag);
529 }
530}
531
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000532void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
533 const char text[], size_t byteLength,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000534 SkScalar x, SkScalar y) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000535 SkASSERT(byteLength == 0 || text != NULL);
536
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000537 // nothing to draw or can't draw
538 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
539 || fSkPaint.getRasterizer()) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000540 return;
541 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000542
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000543 this->init(paint, skPaint);
544
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000545 SkScalar sizeRatio = fTextRatio;
546
547 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
548
jvanverth2d2a68c2014-06-10 06:42:56 -0700549 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
550 SkGlyphCache* cache = autoCache.getCache();
551 GrFontScaler* fontScaler = GetGrFontScaler(cache);
552
553 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000554
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000555 // need to measure first
556 // TODO - generate positions and pre-load cache as well?
557 const char* stop = text + byteLength;
558 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
559 SkFixed stopX = 0;
560 SkFixed stopY = 0;
561
562 const char* textPtr = text;
563 while (textPtr < stop) {
564 // don't need x, y here, since all subpixel variants will have the
565 // same advance
566 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
567
568 stopX += glyph.fAdvanceX;
569 stopY += glyph.fAdvanceY;
570 }
571 SkASSERT(textPtr == stop);
572
573 SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio;
574 SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio;
575
576 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
577 alignX = SkScalarHalf(alignX);
578 alignY = SkScalarHalf(alignY);
579 }
580
581 x -= alignX;
582 y -= alignY;
583 }
584
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000585 SkFixed fx = SkScalarToFixed(x);
586 SkFixed fy = SkScalarToFixed(y);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000587 SkFixed fixedScale = SkScalarToFixed(sizeRatio);
588 while (text < stop) {
commit-bot@chromium.orga9dae712014-03-24 18:34:04 +0000589 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000590
591 if (glyph.fWidth) {
592 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
593 glyph.getSubXFixed(),
594 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000595 fx,
596 fy,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000597 fontScaler);
598 }
599
600 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale);
601 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale);
602 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000603
604 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000605}
606
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000607void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint,
608 const char text[], size_t byteLength,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000609 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000610 int scalarsPerPosition) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000611
612 SkASSERT(byteLength == 0 || text != NULL);
613 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
614
615 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000616 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000617 return;
618 }
619
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000620 this->init(paint, skPaint);
621
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000622 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
623
jvanverth2d2a68c2014-06-10 06:42:56 -0700624 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
625 SkGlyphCache* cache = autoCache.getCache();
626 GrFontScaler* fontScaler = GetGrFontScaler(cache);
627
628 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000629
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000630 const char* stop = text + byteLength;
631
632 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
633 while (text < stop) {
634 // the last 2 parameters are ignored
635 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
636
637 if (glyph.fWidth) {
638 SkScalar x = pos[0];
639 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
640
641 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
642 glyph.getSubXFixed(),
643 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000644 SkScalarToFixed(x),
645 SkScalarToFixed(y),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000646 fontScaler);
647 }
648 pos += scalarsPerPosition;
649 }
650 } else {
651 int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0;
652 while (text < stop) {
653 // the last 2 parameters are ignored
654 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
655
656 if (glyph.fWidth) {
657 SkScalar x = pos[0];
658 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000659
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000660 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
661 glyph.getSubXFixed(),
662 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000663 SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift),
664 SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000665 fontScaler);
666 }
667 pos += scalarsPerPosition;
668 }
669 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000670
671 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000672}