blob: 4321ff17efc534bd8b476823776506394cf4e7eb [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
31static const int kGlyphCoordsNoColorAttributeIndex = 1;
32static const int kGlyphCoordsWithColorAttributeIndex = 2;
jvanverth@google.comd830d132013-11-11 20:54:09 +000033
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +000034static const int kSmallDFFontSize = 32;
35static const int kSmallDFFontLimit = 32;
36static const int kMediumDFFontSize = 64;
37static const int kMediumDFFontLimit = 64;
38static const int kLargeDFFontSize = 128;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +000039
jvanverthfeceba52014-07-25 19:03:34 -070040namespace {
41// position + texture coord
42extern const GrVertexAttrib gTextVertexAttribs[] = {
43 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
44 {kVec2f_GrVertexAttribType, sizeof(SkPoint) , kEffect_GrVertexAttribBinding}
45};
46
47// position + color + texture coord
48extern const GrVertexAttrib gTextVertexWithColorAttribs[] = {
49 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
50 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
51 {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kEffect_GrVertexAttribBinding}
52};
53
54};
jvanverth@google.comd830d132013-11-11 20:54:09 +000055
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000056GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000057 const SkDeviceProperties& properties,
58 bool enable)
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000059 : GrTextContext(context, properties) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000060#if SK_FORCE_DISTANCEFIELD_FONTS
61 fEnableDFRendering = true;
62#else
63 fEnableDFRendering = enable;
64#endif
jvanverth@google.comd830d132013-11-11 20:54:09 +000065 fStrike = NULL;
jvanverth2d2a68c2014-06-10 06:42:56 -070066 fGammaTexture = NULL;
jvanverth@google.comd830d132013-11-11 20:54:09 +000067
jvanverth9bcd23b2014-08-01 14:05:19 -070068 fCurrTexture = NULL;
jvanverth@google.comd830d132013-11-11 20:54:09 +000069 fCurrVertex = 0;
jvanverth78f07182014-07-30 06:17:59 -070070 fEffectTextureUniqueID = SK_InvalidUniqueID;
71 fEffectColor = GrColor_ILLEGAL;
72 fEffectFlags = 0;
jvanverth1723bfc2014-07-30 09:16:33 -070073
jvanverth@google.comd830d132013-11-11 20:54:09 +000074 fVertices = NULL;
jvanverth9bcd23b2014-08-01 14:05:19 -070075 fMaxVertices = 0;
jvanverth1723bfc2014-07-30 09:16:33 -070076
77 fVertexBounds.setLargestInverted();
jvanverth@google.comd830d132013-11-11 20:54:09 +000078}
79
80GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
81 this->flushGlyphs();
jvanverth2d2a68c2014-06-10 06:42:56 -070082 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +000083}
84
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000085bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000086 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000087 return false;
88 }
89
skia.committer@gmail.come1d94432014-04-09 03:04:11 +000090 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000091 // translate well to distance
92 if (paint.getRasterizer() || paint.getMaskFilter() ||
93 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
94 return false;
95 }
96
97 // TODO: add some stroking support
98 if (paint.getStyle() != SkPaint::kFill_Style) {
99 return false;
100 }
101
102 // TODO: choose an appropriate maximum scale for distance fields and
103 // enable perspective
104 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
105 return false;
106 }
107
108 // distance fields cannot represent color fonts
109 SkScalerContext::Rec rec;
110 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
111 return rec.getFormat() != SkMask::kARGB32_Format;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000112}
113
jvanverth@google.comd830d132013-11-11 20:54:09 +0000114static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
115 unsigned r = SkColorGetR(c);
116 unsigned g = SkColorGetG(c);
117 unsigned b = SkColorGetB(c);
118 return GrColorPackRGBA(r, g, b, 0xff);
119}
120
jvanverth78f07182014-07-30 06:17:59 -0700121void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
122 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
123 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
124
jvanverth9bcd23b2014-08-01 14:05:19 -0700125 uint32_t textureUniqueID = fCurrTexture->getUniqueID();
jvanverth78f07182014-07-30 06:17:59 -0700126
127 // set up any flags
128 uint32_t flags = 0;
129 flags |= fContext->getMatrix().isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
130 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
131 flags |= fUseLCDText && fContext->getMatrix().rectStaysRect() ?
132 kRectToRect_DistanceFieldEffectFlag : 0;
133 bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout ==
134 fDeviceProperties.fGeometry.getLayout();
135 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);
jvanverth9bcd23b2014-08-01 14:05:19 -0700143 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
151 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.fGamma,
152 filteredColor);
jvanverth9bcd23b2014-08-01 14:05:19 -0700153 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
jvanverth78831322014-08-01 17:15:20 -0700160 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);
178 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
179
180 if (fCurrVertex > 0) {
181 // setup our sampler state for our text texture/atlas
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000182 SkASSERT(SkIsAlign4(fCurrVertex));
jvanverth@google.comd830d132013-11-11 20:54:09 +0000183
jvanverth78f07182014-07-30 06:17:59 -0700184 // get our current color
jvanverth2d2a68c2014-06-10 06:42:56 -0700185 SkColor filteredColor;
186 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
187 if (NULL != colorFilter) {
188 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
189 } else {
190 filteredColor = fSkPaint.getColor();
191 }
jvanverth78f07182014-07-30 06:17:59 -0700192 this->setupCoverageEffect(filteredColor);
193
194 // Effects could be stored with one of the cache objects (atlas?)
195 int coordsIdx = drawState->hasColorVertexAttribute() ? kGlyphCoordsWithColorAttributeIndex :
196 kGlyphCoordsNoColorAttributeIndex;
197 drawState->addCoverageEffect(fCachedEffect.get(), coordsIdx);
198
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);
jvanverth9bcd23b2014-08-01 14:05:19 -0700229 fDrawTarget->resetVertexSource();
230 fVertices = NULL;
231 fMaxVertices = 0;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000232 fCurrVertex = 0;
jvanverth9bcd23b2014-08-01 14:05:19 -0700233 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) {
jvanverth9bcd23b2014-08-01 14:05:19 -0700241 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
292 // before we purge the cache, we must flush any accumulated draws
293 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
jvanverth9bcd23b2014-08-01 14:05:19 -0700331 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 } else {
347 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
348 SK_ARRAY_COUNT(gTextVertexAttribs));
349 }
350 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
351 if (flush) {
352 this->flushGlyphs();
353 fContext->flush();
354 if (useColorVerts) {
355 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
356 SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
357 } else {
358 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
359 SK_ARRAY_COUNT(gTextVertexAttribs));
360 }
361 }
362 fMaxVertices = kDefaultRequestedVerts;
363 // ignore return, no point in flushing again.
364 fDrawTarget->geometryHints(&fMaxVertices, NULL);
365
366 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
367 if (fMaxVertices < kMinRequestedVerts) {
368 fMaxVertices = kDefaultRequestedVerts;
369 } else if (fMaxVertices > maxQuadVertices) {
370 // don't exceed the limit of the index buffer
371 fMaxVertices = maxQuadVertices;
372 }
373 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
374 0,
375 &fVertices,
376 NULL);
377 GrAlwaysAssert(success);
378 }
379
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000380 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
381 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
382 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
383 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000384
385 SkScalar scale = fTextRatio;
386 dx *= scale;
387 dy *= scale;
388 sx += dx;
389 sy += dy;
390 width *= scale;
391 height *= scale;
jvanverth1723bfc2014-07-30 09:16:33 -0700392
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000393 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
394 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
395 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
396 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000397
jvanverth1723bfc2014-07-30 09:16:33 -0700398 SkRect r;
jvanverth9bcd23b2014-08-01 14:05:19 -0700399 r.fLeft = sx;
400 r.fTop = sy;
401 r.fRight = sx + width;
402 r.fBottom = sy + height;
jvanverth1723bfc2014-07-30 09:16:33 -0700403
404 fVertexBounds.growToInclude(r);
405
jvanverthfeceba52014-07-25 19:03:34 -0700406 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
407 : (2 * sizeof(SkPoint) + sizeof(GrColor));
jvanverth1723bfc2014-07-30 09:16:33 -0700408
jvanverthfeceba52014-07-25 19:03:34 -0700409 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexSize());
jvanverth1723bfc2014-07-30 09:16:33 -0700410
jvanverthf17bc6c2014-07-25 16:46:53 -0700411 SkPoint* positions = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700412 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth1723bfc2014-07-30 09:16:33 -0700413 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
414
jvanverthfeceba52014-07-25 19:03:34 -0700415 // The texture coords are last in both the with and without color vertex layouts.
jvanverthf17bc6c2014-07-25 16:46:53 -0700416 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700417 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
jvanverthf17bc6c2014-07-25 16:46:53 -0700418 textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
jvanverthfeceba52014-07-25 19:03:34 -0700419 SkFixedToFloat(texture->normalizeFixedY(ty)),
420 SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
421 SkFixedToFloat(texture->normalizeFixedY(ty + th)),
422 vertSize);
jvanverth9bcd23b2014-08-01 14:05:19 -0700423 if (useColorVerts) {
jvanverthfeceba52014-07-25 19:03:34 -0700424 // color comes after position.
425 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
426 for (int i = 0; i < 4; ++i) {
427 *colors = fPaint.getColor();
428 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
429 }
430 }
jvanverth1723bfc2014-07-30 09:16:33 -0700431
jvanverth@google.comd830d132013-11-11 20:54:09 +0000432 fCurrVertex += 4;
433}
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000434
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000435inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
436 GrTextContext::init(paint, skPaint);
437
438 fStrike = NULL;
439
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000440 fCurrVertex = 0;
441
442 fVertices = NULL;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000443
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000444 if (fSkPaint.getTextSize() <= kSmallDFFontLimit) {
445 fTextRatio = fSkPaint.getTextSize()/kSmallDFFontSize;
446 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
447 } else if (fSkPaint.getTextSize() <= kMediumDFFontLimit) {
448 fTextRatio = fSkPaint.getTextSize()/kMediumDFFontSize;
449 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
450 } else {
451 fTextRatio = fSkPaint.getTextSize()/kLargeDFFontSize;
452 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
453 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000454
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000455 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000456
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000457 fSkPaint.setLCDRenderText(false);
458 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700459 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000460 fSkPaint.setSubpixelText(true);
jvanverth2d2a68c2014-06-10 06:42:56 -0700461
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000462}
463
464inline void GrDistanceFieldTextContext::finish() {
jvanverthfeceba52014-07-25 19:03:34 -0700465 this->flushGlyphs();
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000466
467 GrTextContext::finish();
468}
469
jvanverth2d2a68c2014-06-10 06:42:56 -0700470static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
471 const SkDeviceProperties& deviceProperties,
472 GrTexture** gammaTexture) {
473 if (NULL == *gammaTexture) {
474 int width, height;
475 size_t size;
476
477#ifdef SK_GAMMA_CONTRAST
478 SkScalar contrast = SK_GAMMA_CONTRAST;
479#else
480 SkScalar contrast = 0.5f;
481#endif
482 SkScalar paintGamma = deviceProperties.fGamma;
483 SkScalar deviceGamma = deviceProperties.fGamma;
484
485 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
486 &width, &height);
487
488 SkAutoTArray<uint8_t> data((int)size);
489 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
490
491 // TODO: Update this to use the cache rather than directly creating a texture.
492 GrTextureDesc desc;
493 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
494 desc.fWidth = width;
495 desc.fHeight = height;
496 desc.fConfig = kAlpha_8_GrPixelConfig;
497
498 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
499 if (NULL == *gammaTexture) {
500 return;
501 }
502
503 context->writeTexturePixels(*gammaTexture,
504 0, 0, width, height,
505 (*gammaTexture)->config(), data.get(), 0,
506 GrContext::kDontFlush_PixelOpsFlag);
507 }
508}
509
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000510void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
511 const char text[], size_t byteLength,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000512 SkScalar x, SkScalar y) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000513 SkASSERT(byteLength == 0 || text != NULL);
514
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000515 // nothing to draw or can't draw
516 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
517 || fSkPaint.getRasterizer()) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000518 return;
519 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000520
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000521 this->init(paint, skPaint);
522
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000523 SkScalar sizeRatio = fTextRatio;
524
525 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
526
jvanverth2d2a68c2014-06-10 06:42:56 -0700527 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
528 SkGlyphCache* cache = autoCache.getCache();
529 GrFontScaler* fontScaler = GetGrFontScaler(cache);
530
531 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000532
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000533 // need to measure first
534 // TODO - generate positions and pre-load cache as well?
535 const char* stop = text + byteLength;
536 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
537 SkFixed stopX = 0;
538 SkFixed stopY = 0;
539
540 const char* textPtr = text;
541 while (textPtr < stop) {
542 // don't need x, y here, since all subpixel variants will have the
543 // same advance
544 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
545
546 stopX += glyph.fAdvanceX;
547 stopY += glyph.fAdvanceY;
548 }
549 SkASSERT(textPtr == stop);
550
551 SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio;
552 SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio;
553
554 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
555 alignX = SkScalarHalf(alignX);
556 alignY = SkScalarHalf(alignY);
557 }
558
559 x -= alignX;
560 y -= alignY;
561 }
562
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000563 SkFixed fx = SkScalarToFixed(x);
564 SkFixed fy = SkScalarToFixed(y);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000565 SkFixed fixedScale = SkScalarToFixed(sizeRatio);
566 while (text < stop) {
commit-bot@chromium.orga9dae712014-03-24 18:34:04 +0000567 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000568
569 if (glyph.fWidth) {
570 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
571 glyph.getSubXFixed(),
572 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000573 fx,
574 fy,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000575 fontScaler);
576 }
577
578 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale);
579 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale);
580 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000581
582 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000583}
584
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000585void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint,
586 const char text[], size_t byteLength,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000587 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000588 int scalarsPerPosition) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000589
590 SkASSERT(byteLength == 0 || text != NULL);
591 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
592
593 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000594 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000595 return;
596 }
597
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000598 this->init(paint, skPaint);
599
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000600 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
601
jvanverth2d2a68c2014-06-10 06:42:56 -0700602 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
603 SkGlyphCache* cache = autoCache.getCache();
604 GrFontScaler* fontScaler = GetGrFontScaler(cache);
605
606 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000607
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000608 const char* stop = text + byteLength;
609
610 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
611 while (text < stop) {
612 // the last 2 parameters are ignored
613 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
614
615 if (glyph.fWidth) {
616 SkScalar x = pos[0];
617 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
618
619 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
620 glyph.getSubXFixed(),
621 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000622 SkScalarToFixed(x),
623 SkScalarToFixed(y),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000624 fontScaler);
625 }
626 pos += scalarsPerPosition;
627 }
628 } else {
629 int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0;
630 while (text < stop) {
631 // the last 2 parameters are ignored
632 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
633
634 if (glyph.fWidth) {
635 SkScalar x = pos[0];
636 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000637
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000638 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
639 glyph.getSubXFixed(),
640 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000641 SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift),
642 SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000643 fontScaler);
644 }
645 pos += scalarsPerPosition;
646 }
647 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000648
649 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000650}