blob: 18058be17f01924bafe0c363a6448fb0c770267e [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;
134 bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout ==
135 fDeviceProperties.fGeometry.getLayout();
136 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);
Mike Klein6a25bd02014-08-29 10:03:59 -0400144 fCachedEffect.reset(GrDistanceFieldLCDTextureEffect::Create(fCurrTexture,
jvanverth78f07182014-07-30 06:17:59 -0700145 params,
146 fGammaTexture,
147 gammaParams,
148 colorNoPreMul,
149 flags));
150 } else {
151#ifdef SK_GAMMA_APPLY_TO_A8
152 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.fGamma,
153 filteredColor);
Mike Klein6a25bd02014-08-29 10:03:59 -0400154 fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
jvanverth78f07182014-07-30 06:17:59 -0700155 params,
156 fGammaTexture,
157 gammaParams,
158 lum/255.f,
159 flags));
160#else
Mike Klein6a25bd02014-08-29 10:03:59 -0400161 fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(fCurrTexture,
jvanverth78f07182014-07-30 06:17:59 -0700162 params, flags));
163#endif
164 }
165 fEffectTextureUniqueID = textureUniqueID;
166 fEffectColor = filteredColor;
167 fEffectFlags = flags;
168 }
169
170}
171
jvanverth@google.comd830d132013-11-11 20:54:09 +0000172void GrDistanceFieldTextContext::flushGlyphs() {
173 if (NULL == fDrawTarget) {
174 return;
175 }
176
177 GrDrawState* drawState = fDrawTarget->drawState();
178 GrDrawState::AutoRestoreEffects are(drawState);
jvanverth9564ce62014-09-16 05:45:19 -0700179
180 drawState->setFromPaint(fPaint, fTextMatrix, fContext->getRenderTarget());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000181
182 if (fCurrVertex > 0) {
183 // setup our sampler state for our text texture/atlas
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000184 SkASSERT(SkIsAlign4(fCurrVertex));
jvanverth@google.comd830d132013-11-11 20:54:09 +0000185
jvanverth78f07182014-07-30 06:17:59 -0700186 // get our current color
jvanverth2d2a68c2014-06-10 06:42:56 -0700187 SkColor filteredColor;
188 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
bsalomon49f085d2014-09-05 13:34:00 -0700189 if (colorFilter) {
jvanverth2d2a68c2014-06-10 06:42:56 -0700190 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
191 } else {
192 filteredColor = fSkPaint.getColor();
193 }
jvanverth78f07182014-07-30 06:17:59 -0700194 this->setupCoverageEffect(filteredColor);
195
196 // Effects could be stored with one of the cache objects (atlas?)
joshualitt249af152014-09-15 11:41:13 -0700197 drawState->setGeometryProcessor(fCachedEffect.get());
jvanverth78f07182014-07-30 06:17:59 -0700198
199 // Set draw state
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000200 if (fUseLCDText) {
jvanverth2d2a68c2014-06-10 06:42:56 -0700201 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000202 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
203 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
204 fPaint.numColorStages()) {
205 GrPrintf("LCD Text will not draw correctly.\n");
206 }
jvanverthfeceba52014-07-25 19:03:34 -0700207 SkASSERT(!drawState->hasColorVertexAttribute());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000208 // We don't use the GrPaint's color in this case because it's been premultiplied by
209 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
210 // the mask texture color. The end result is that we get
211 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000212 int a = SkColorGetA(fSkPaint.getColor());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000213 // paintAlpha
214 drawState->setColor(SkColorSetARGB(a, a, a, a));
215 // paintColor
jvanverth2d2a68c2014-06-10 06:42:56 -0700216 drawState->setBlendConstant(colorNoPreMul);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000217 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
218 } else {
219 // set back to normal in case we took LCD path previously.
220 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
jvanverthfeceba52014-07-25 19:03:34 -0700221 // We're using per-vertex color.
222 SkASSERT(drawState->hasColorVertexAttribute());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000223 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000224 int nGlyphs = fCurrVertex / 4;
225 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
226 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
227 nGlyphs,
jvanverth1723bfc2014-07-30 09:16:33 -0700228 4, 6, &fVertexBounds);
Mike Klein6a25bd02014-08-29 10:03:59 -0400229 fDrawTarget->resetVertexSource();
230 fVertices = NULL;
231 fMaxVertices = 0;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000232 fCurrVertex = 0;
Mike Klein6a25bd02014-08-29 10:03:59 -0400233 SkSafeSetNull(fCurrTexture);
jvanverth1723bfc2014-07-30 09:16:33 -0700234 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000235 }
236}
237
jvanverth@google.comd830d132013-11-11 20:54:09 +0000238void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000239 SkFixed vx, SkFixed vy,
jvanverth@google.comd830d132013-11-11 20:54:09 +0000240 GrFontScaler* scaler) {
Mike Klein6a25bd02014-08-29 10:03:59 -0400241 if (NULL == fDrawTarget) {
242 return;
243 }
244
245 if (NULL == fStrike) {
246 fStrike = fContext->getFontCache()->getStrike(scaler, true);
247 }
248
jvanverth@google.comd830d132013-11-11 20:54:09 +0000249 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
250 if (NULL == glyph || glyph->fBounds.isEmpty()) {
251 return;
252 }
253
254 SkScalar sx = SkFixedToScalar(vx);
255 SkScalar sy = SkFixedToScalar(vy);
256/*
257 // not valid, need to find a different solution for this
258 vx += SkIntToFixed(glyph->fBounds.fLeft);
259 vy += SkIntToFixed(glyph->fBounds.fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000260
jvanverth@google.comd830d132013-11-11 20:54:09 +0000261 // keep them as ints until we've done the clip-test
262 GrFixed width = glyph->fBounds.width();
263 GrFixed height = glyph->fBounds.height();
264
265 // check if we clipped out
266 if (true || NULL == glyph->fPlot) {
267 int x = vx >> 16;
268 int y = vy >> 16;
269 if (fClipRect.quickReject(x, y, x + width, y + height)) {
270// SkCLZ(3); // so we can set a break-point in the debugger
271 return;
272 }
273 }
274*/
275 if (NULL == glyph->fPlot) {
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000276 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000277 goto HAS_ATLAS;
278 }
279
280 // try to clear out an unused plot before we flush
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000281 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
282 fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000283 goto HAS_ATLAS;
284 }
285
286 if (c_DumpFontCache) {
287#ifdef SK_DEVELOPER
288 fContext->getFontCache()->dump();
289#endif
290 }
291
Mike Klein6a25bd02014-08-29 10:03:59 -0400292 // before we purge the cache, we must flush any accumulated draws
jvanverth@google.comd830d132013-11-11 20:54:09 +0000293 this->flushGlyphs();
294 fContext->flush();
295
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000296 // we should have an unused plot now
297 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
298 fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000299 goto HAS_ATLAS;
300 }
301
302 if (NULL == glyph->fPath) {
303 SkPath* path = SkNEW(SkPath);
304 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
305 // flag the glyph as being dead?
306 delete path;
307 return;
308 }
309 glyph->fPath = path;
310 }
311
312 GrContext::AutoMatrix am;
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000313 SkMatrix ctm;
314 ctm.setScale(fTextRatio, fTextRatio);
315 ctm.postTranslate(sx, sy);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000316 GrPaint tmpPaint(fPaint);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000317 am.setPreConcat(fContext, ctm, &tmpPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700318 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
319 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000320 return;
321 }
322
323HAS_ATLAS:
324 SkASSERT(glyph->fPlot);
325 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
326 glyph->fPlot->setDrawToken(drawToken);
327
328 GrTexture* texture = glyph->fPlot->texture();
329 SkASSERT(texture);
330
Mike Klein6a25bd02014-08-29 10:03:59 -0400331 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
332 this->flushGlyphs();
333 fCurrTexture = texture;
334 fCurrTexture->ref();
335 }
336
337 bool useColorVerts = !fUseLCDText;
338
339 if (NULL == fVertices) {
340 // If we need to reserve vertices allow the draw target to suggest
341 // a number of verts to reserve and whether to perform a flush.
342 fMaxVertices = kMinRequestedVerts;
343 if (useColorVerts) {
344 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
345 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
346 kTextVAColorSize);
347 } else {
348 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
349 SK_ARRAY_COUNT(gTextVertexAttribs),
350 kTextVASize);
351 }
352 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
353 if (flush) {
354 this->flushGlyphs();
355 fContext->flush();
356 if (useColorVerts) {
357 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
358 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
359 kTextVAColorSize);
360 } else {
361 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
362 SK_ARRAY_COUNT(gTextVertexAttribs),
363 kTextVASize);
364 }
365 }
366 fMaxVertices = kDefaultRequestedVerts;
367 // ignore return, no point in flushing again.
368 fDrawTarget->geometryHints(&fMaxVertices, NULL);
369
370 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
371 if (fMaxVertices < kMinRequestedVerts) {
372 fMaxVertices = kDefaultRequestedVerts;
373 } else if (fMaxVertices > maxQuadVertices) {
374 // don't exceed the limit of the index buffer
375 fMaxVertices = maxQuadVertices;
376 }
377 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
378 0,
379 &fVertices,
380 NULL);
381 GrAlwaysAssert(success);
382 }
383
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000384 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
385 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
386 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
387 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000388
389 SkScalar scale = fTextRatio;
390 dx *= scale;
391 dy *= scale;
392 sx += dx;
393 sy += dy;
394 width *= scale;
395 height *= scale;
Mike Klein6a25bd02014-08-29 10:03:59 -0400396
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000397 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
398 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
399 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
400 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000401
jvanverth1723bfc2014-07-30 09:16:33 -0700402 SkRect r;
jvanverth9bcd23b2014-08-01 14:05:19 -0700403 r.fLeft = sx;
404 r.fTop = sy;
405 r.fRight = sx + width;
406 r.fBottom = sy + height;
jvanverth1723bfc2014-07-30 09:16:33 -0700407
408 fVertexBounds.growToInclude(r);
409
jvanverthfeceba52014-07-25 19:03:34 -0700410 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
411 : (2 * sizeof(SkPoint) + sizeof(GrColor));
jvanverth1723bfc2014-07-30 09:16:33 -0700412
egdaniel7b3d5ee2014-08-28 05:41:14 -0700413 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride());
jvanverth1723bfc2014-07-30 09:16:33 -0700414
jvanverthf17bc6c2014-07-25 16:46:53 -0700415 SkPoint* positions = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700416 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth1723bfc2014-07-30 09:16:33 -0700417 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
418
jvanverthfeceba52014-07-25 19:03:34 -0700419 // The texture coords are last in both the with and without color vertex layouts.
jvanverthf17bc6c2014-07-25 16:46:53 -0700420 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700421 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
jvanverthf17bc6c2014-07-25 16:46:53 -0700422 textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
jvanverthfeceba52014-07-25 19:03:34 -0700423 SkFixedToFloat(texture->normalizeFixedY(ty)),
424 SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
425 SkFixedToFloat(texture->normalizeFixedY(ty + th)),
426 vertSize);
Mike Klein6a25bd02014-08-29 10:03:59 -0400427 if (useColorVerts) {
bsalomon62c447d2014-08-08 08:08:50 -0700428 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
429 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
430 }
jvanverthfeceba52014-07-25 19:03:34 -0700431 // color comes after position.
432 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
433 for (int i = 0; i < 4; ++i) {
434 *colors = fPaint.getColor();
435 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
436 }
437 }
jvanverth1723bfc2014-07-30 09:16:33 -0700438
jvanverth@google.comd830d132013-11-11 20:54:09 +0000439 fCurrVertex += 4;
440}
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000441
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000442inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
443 GrTextContext::init(paint, skPaint);
444
445 fStrike = NULL;
446
jvanverth9564ce62014-09-16 05:45:19 -0700447 fTextMatrix = fContext->getMatrix();
448
449 // getMaxScale doesn't support perspective, so neither do we at the moment
450 SkASSERT(!fTextMatrix.hasPerspective());
451 SkScalar maxScale = fTextMatrix.getMaxScale();
452 SkScalar textSize = fSkPaint.getTextSize();
453 // if we have non-unity scale, we need to adjust our text size accordingly
454 // to avoid aliasing, and prescale the matrix by the inverse to end up with the same size
455 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
456 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
457 textSize *= maxScale;
458 fTextMatrix.preScale(SK_Scalar1 / maxScale, SK_Scalar1 / maxScale);
459 }
460
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000461 fCurrVertex = 0;
462
463 fVertices = NULL;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000464
jvanverth9564ce62014-09-16 05:45:19 -0700465 if (textSize <= kSmallDFFontLimit) {
466 fTextRatio = textSize / kSmallDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000467 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
jvanverth9564ce62014-09-16 05:45:19 -0700468 } else if (textSize <= kMediumDFFontLimit) {
469 fTextRatio = textSize / kMediumDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000470 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
471 } else {
jvanverth9564ce62014-09-16 05:45:19 -0700472 fTextRatio = textSize / kLargeDFFontSize;
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000473 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
474 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000475
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000476 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000477
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000478 fSkPaint.setLCDRenderText(false);
479 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700480 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000481 fSkPaint.setSubpixelText(true);
jvanverth2d2a68c2014-06-10 06:42:56 -0700482
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000483}
484
485inline void GrDistanceFieldTextContext::finish() {
jvanverthfeceba52014-07-25 19:03:34 -0700486 this->flushGlyphs();
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000487
488 GrTextContext::finish();
489}
490
jvanverth2d2a68c2014-06-10 06:42:56 -0700491static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
492 const SkDeviceProperties& deviceProperties,
493 GrTexture** gammaTexture) {
494 if (NULL == *gammaTexture) {
495 int width, height;
496 size_t size;
497
498#ifdef SK_GAMMA_CONTRAST
499 SkScalar contrast = SK_GAMMA_CONTRAST;
500#else
501 SkScalar contrast = 0.5f;
502#endif
503 SkScalar paintGamma = deviceProperties.fGamma;
504 SkScalar deviceGamma = deviceProperties.fGamma;
505
506 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
507 &width, &height);
508
509 SkAutoTArray<uint8_t> data((int)size);
510 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
511
512 // TODO: Update this to use the cache rather than directly creating a texture.
513 GrTextureDesc desc;
514 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
515 desc.fWidth = width;
516 desc.fHeight = height;
517 desc.fConfig = kAlpha_8_GrPixelConfig;
518
519 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
520 if (NULL == *gammaTexture) {
521 return;
522 }
523
524 context->writeTexturePixels(*gammaTexture,
525 0, 0, width, height,
526 (*gammaTexture)->config(), data.get(), 0,
527 GrContext::kDontFlush_PixelOpsFlag);
528 }
529}
530
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000531void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
532 const char text[], size_t byteLength,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000533 SkScalar x, SkScalar y) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000534 SkASSERT(byteLength == 0 || text != NULL);
535
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000536 // nothing to draw or can't draw
537 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
538 || fSkPaint.getRasterizer()) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000539 return;
540 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000541
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000542 this->init(paint, skPaint);
543
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000544 SkScalar sizeRatio = fTextRatio;
545
546 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
547
jvanverth2d2a68c2014-06-10 06:42:56 -0700548 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
549 SkGlyphCache* cache = autoCache.getCache();
550 GrFontScaler* fontScaler = GetGrFontScaler(cache);
551
552 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000553
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000554 // need to measure first
555 // TODO - generate positions and pre-load cache as well?
556 const char* stop = text + byteLength;
557 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
558 SkFixed stopX = 0;
559 SkFixed stopY = 0;
560
561 const char* textPtr = text;
562 while (textPtr < stop) {
563 // don't need x, y here, since all subpixel variants will have the
564 // same advance
565 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
566
567 stopX += glyph.fAdvanceX;
568 stopY += glyph.fAdvanceY;
569 }
570 SkASSERT(textPtr == stop);
571
572 SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio;
573 SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio;
574
575 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
576 alignX = SkScalarHalf(alignX);
577 alignY = SkScalarHalf(alignY);
578 }
579
580 x -= alignX;
581 y -= alignY;
582 }
583
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000584 SkFixed fx = SkScalarToFixed(x);
585 SkFixed fy = SkScalarToFixed(y);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000586 SkFixed fixedScale = SkScalarToFixed(sizeRatio);
587 while (text < stop) {
commit-bot@chromium.orga9dae712014-03-24 18:34:04 +0000588 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000589
590 if (glyph.fWidth) {
591 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
592 glyph.getSubXFixed(),
593 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000594 fx,
595 fy,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000596 fontScaler);
597 }
598
599 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale);
600 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale);
601 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000602
603 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000604}
605
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000606void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint,
607 const char text[], size_t byteLength,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000608 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000609 int scalarsPerPosition) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000610
611 SkASSERT(byteLength == 0 || text != NULL);
612 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
613
614 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000615 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000616 return;
617 }
618
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000619 this->init(paint, skPaint);
620
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000621 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
622
jvanverth2d2a68c2014-06-10 06:42:56 -0700623 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
624 SkGlyphCache* cache = autoCache.getCache();
625 GrFontScaler* fontScaler = GetGrFontScaler(cache);
626
627 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000628
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000629 const char* stop = text + byteLength;
630
631 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
632 while (text < stop) {
633 // the last 2 parameters are ignored
634 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
635
636 if (glyph.fWidth) {
637 SkScalar x = pos[0];
638 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
639
640 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
641 glyph.getSubXFixed(),
642 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000643 SkScalarToFixed(x),
644 SkScalarToFixed(y),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000645 fontScaler);
646 }
647 pos += scalarsPerPosition;
648 }
649 } else {
650 int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0;
651 while (text < stop) {
652 // the last 2 parameters are ignored
653 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
654
655 if (glyph.fWidth) {
656 SkScalar x = pos[0];
657 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000658
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000659 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
660 glyph.getSubXFixed(),
661 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000662 SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift),
663 SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000664 fontScaler);
665 }
666 pos += scalarsPerPosition;
667 }
668 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000669
670 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000671}