blob: d32dcb9c21692a8acc803c0a491f6bd906ee818a [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;
130 flags |= fContext->getMatrix().isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
131 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
132 flags |= fUseLCDText && fContext->getMatrix().rectStaysRect() ?
133 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);
179 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
180
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) {
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000275 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000276 goto HAS_ATLAS;
277 }
278
279 // try to clear out an unused plot before we flush
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000280 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
281 fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000282 goto HAS_ATLAS;
283 }
284
285 if (c_DumpFontCache) {
286#ifdef SK_DEVELOPER
287 fContext->getFontCache()->dump();
288#endif
289 }
290
Mike Klein6a25bd02014-08-29 10:03:59 -0400291 // before we purge the cache, we must flush any accumulated draws
jvanverth@google.comd830d132013-11-11 20:54:09 +0000292 this->flushGlyphs();
293 fContext->flush();
294
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000295 // we should have an unused plot now
296 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
297 fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000298 goto HAS_ATLAS;
299 }
300
301 if (NULL == glyph->fPath) {
302 SkPath* path = SkNEW(SkPath);
303 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
304 // flag the glyph as being dead?
305 delete path;
306 return;
307 }
308 glyph->fPath = path;
309 }
310
311 GrContext::AutoMatrix am;
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000312 SkMatrix ctm;
313 ctm.setScale(fTextRatio, fTextRatio);
314 ctm.postTranslate(sx, sy);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000315 GrPaint tmpPaint(fPaint);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000316 am.setPreConcat(fContext, ctm, &tmpPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700317 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
318 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000319 return;
320 }
321
322HAS_ATLAS:
323 SkASSERT(glyph->fPlot);
324 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
325 glyph->fPlot->setDrawToken(drawToken);
326
327 GrTexture* texture = glyph->fPlot->texture();
328 SkASSERT(texture);
329
Mike Klein6a25bd02014-08-29 10:03:59 -0400330 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
331 this->flushGlyphs();
332 fCurrTexture = texture;
333 fCurrTexture->ref();
334 }
335
336 bool useColorVerts = !fUseLCDText;
337
338 if (NULL == fVertices) {
339 // If we need to reserve vertices allow the draw target to suggest
340 // a number of verts to reserve and whether to perform a flush.
341 fMaxVertices = kMinRequestedVerts;
342 if (useColorVerts) {
343 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
344 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
345 kTextVAColorSize);
346 } else {
347 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
348 SK_ARRAY_COUNT(gTextVertexAttribs),
349 kTextVASize);
350 }
351 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
352 if (flush) {
353 this->flushGlyphs();
354 fContext->flush();
355 if (useColorVerts) {
356 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
357 SK_ARRAY_COUNT(gTextVertexWithColorAttribs),
358 kTextVAColorSize);
359 } else {
360 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
361 SK_ARRAY_COUNT(gTextVertexAttribs),
362 kTextVASize);
363 }
364 }
365 fMaxVertices = kDefaultRequestedVerts;
366 // ignore return, no point in flushing again.
367 fDrawTarget->geometryHints(&fMaxVertices, NULL);
368
369 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
370 if (fMaxVertices < kMinRequestedVerts) {
371 fMaxVertices = kDefaultRequestedVerts;
372 } else if (fMaxVertices > maxQuadVertices) {
373 // don't exceed the limit of the index buffer
374 fMaxVertices = maxQuadVertices;
375 }
376 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
377 0,
378 &fVertices,
379 NULL);
380 GrAlwaysAssert(success);
381 }
382
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000383 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
384 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
385 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
386 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000387
388 SkScalar scale = fTextRatio;
389 dx *= scale;
390 dy *= scale;
391 sx += dx;
392 sy += dy;
393 width *= scale;
394 height *= scale;
Mike Klein6a25bd02014-08-29 10:03:59 -0400395
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000396 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
397 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
398 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
399 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000400
jvanverth1723bfc2014-07-30 09:16:33 -0700401 SkRect r;
jvanverth9bcd23b2014-08-01 14:05:19 -0700402 r.fLeft = sx;
403 r.fTop = sy;
404 r.fRight = sx + width;
405 r.fBottom = sy + height;
jvanverth1723bfc2014-07-30 09:16:33 -0700406
407 fVertexBounds.growToInclude(r);
408
jvanverthfeceba52014-07-25 19:03:34 -0700409 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
410 : (2 * sizeof(SkPoint) + sizeof(GrColor));
jvanverth1723bfc2014-07-30 09:16:33 -0700411
egdaniel7b3d5ee2014-08-28 05:41:14 -0700412 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride());
jvanverth1723bfc2014-07-30 09:16:33 -0700413
jvanverthf17bc6c2014-07-25 16:46:53 -0700414 SkPoint* positions = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700415 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
jvanverth1723bfc2014-07-30 09:16:33 -0700416 positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize);
417
jvanverthfeceba52014-07-25 19:03:34 -0700418 // The texture coords are last in both the with and without color vertex layouts.
jvanverthf17bc6c2014-07-25 16:46:53 -0700419 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700420 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
jvanverthf17bc6c2014-07-25 16:46:53 -0700421 textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
jvanverthfeceba52014-07-25 19:03:34 -0700422 SkFixedToFloat(texture->normalizeFixedY(ty)),
423 SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
424 SkFixedToFloat(texture->normalizeFixedY(ty + th)),
425 vertSize);
Mike Klein6a25bd02014-08-29 10:03:59 -0400426 if (useColorVerts) {
bsalomon62c447d2014-08-08 08:08:50 -0700427 if (0xFF == GrColorUnpackA(fPaint.getColor())) {
428 fDrawTarget->drawState()->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true);
429 }
jvanverthfeceba52014-07-25 19:03:34 -0700430 // color comes after position.
431 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
432 for (int i = 0; i < 4; ++i) {
433 *colors = fPaint.getColor();
434 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
435 }
436 }
jvanverth1723bfc2014-07-30 09:16:33 -0700437
jvanverth@google.comd830d132013-11-11 20:54:09 +0000438 fCurrVertex += 4;
439}
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000440
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000441inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
442 GrTextContext::init(paint, skPaint);
443
444 fStrike = NULL;
445
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000446 fCurrVertex = 0;
447
448 fVertices = NULL;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000449
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000450 if (fSkPaint.getTextSize() <= kSmallDFFontLimit) {
451 fTextRatio = fSkPaint.getTextSize()/kSmallDFFontSize;
452 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
453 } else if (fSkPaint.getTextSize() <= kMediumDFFontLimit) {
454 fTextRatio = fSkPaint.getTextSize()/kMediumDFFontSize;
455 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
456 } else {
457 fTextRatio = fSkPaint.getTextSize()/kLargeDFFontSize;
458 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
459 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000460
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000461 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000462
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000463 fSkPaint.setLCDRenderText(false);
464 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700465 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000466 fSkPaint.setSubpixelText(true);
jvanverth2d2a68c2014-06-10 06:42:56 -0700467
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000468}
469
470inline void GrDistanceFieldTextContext::finish() {
jvanverthfeceba52014-07-25 19:03:34 -0700471 this->flushGlyphs();
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000472
473 GrTextContext::finish();
474}
475
jvanverth2d2a68c2014-06-10 06:42:56 -0700476static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
477 const SkDeviceProperties& deviceProperties,
478 GrTexture** gammaTexture) {
479 if (NULL == *gammaTexture) {
480 int width, height;
481 size_t size;
482
483#ifdef SK_GAMMA_CONTRAST
484 SkScalar contrast = SK_GAMMA_CONTRAST;
485#else
486 SkScalar contrast = 0.5f;
487#endif
488 SkScalar paintGamma = deviceProperties.fGamma;
489 SkScalar deviceGamma = deviceProperties.fGamma;
490
491 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
492 &width, &height);
493
494 SkAutoTArray<uint8_t> data((int)size);
495 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
496
497 // TODO: Update this to use the cache rather than directly creating a texture.
498 GrTextureDesc desc;
499 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
500 desc.fWidth = width;
501 desc.fHeight = height;
502 desc.fConfig = kAlpha_8_GrPixelConfig;
503
504 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
505 if (NULL == *gammaTexture) {
506 return;
507 }
508
509 context->writeTexturePixels(*gammaTexture,
510 0, 0, width, height,
511 (*gammaTexture)->config(), data.get(), 0,
512 GrContext::kDontFlush_PixelOpsFlag);
513 }
514}
515
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000516void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
517 const char text[], size_t byteLength,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000518 SkScalar x, SkScalar y) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000519 SkASSERT(byteLength == 0 || text != NULL);
520
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000521 // nothing to draw or can't draw
522 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
523 || fSkPaint.getRasterizer()) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000524 return;
525 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000526
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000527 this->init(paint, skPaint);
528
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000529 SkScalar sizeRatio = fTextRatio;
530
531 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
532
jvanverth2d2a68c2014-06-10 06:42:56 -0700533 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
534 SkGlyphCache* cache = autoCache.getCache();
535 GrFontScaler* fontScaler = GetGrFontScaler(cache);
536
537 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000538
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000539 // need to measure first
540 // TODO - generate positions and pre-load cache as well?
541 const char* stop = text + byteLength;
542 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
543 SkFixed stopX = 0;
544 SkFixed stopY = 0;
545
546 const char* textPtr = text;
547 while (textPtr < stop) {
548 // don't need x, y here, since all subpixel variants will have the
549 // same advance
550 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
551
552 stopX += glyph.fAdvanceX;
553 stopY += glyph.fAdvanceY;
554 }
555 SkASSERT(textPtr == stop);
556
557 SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio;
558 SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio;
559
560 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
561 alignX = SkScalarHalf(alignX);
562 alignY = SkScalarHalf(alignY);
563 }
564
565 x -= alignX;
566 y -= alignY;
567 }
568
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000569 SkFixed fx = SkScalarToFixed(x);
570 SkFixed fy = SkScalarToFixed(y);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000571 SkFixed fixedScale = SkScalarToFixed(sizeRatio);
572 while (text < stop) {
commit-bot@chromium.orga9dae712014-03-24 18:34:04 +0000573 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000574
575 if (glyph.fWidth) {
576 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
577 glyph.getSubXFixed(),
578 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000579 fx,
580 fy,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000581 fontScaler);
582 }
583
584 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale);
585 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale);
586 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000587
588 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000589}
590
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000591void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint,
592 const char text[], size_t byteLength,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000593 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000594 int scalarsPerPosition) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000595
596 SkASSERT(byteLength == 0 || text != NULL);
597 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
598
599 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000600 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000601 return;
602 }
603
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000604 this->init(paint, skPaint);
605
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000606 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
607
jvanverth2d2a68c2014-06-10 06:42:56 -0700608 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
609 SkGlyphCache* cache = autoCache.getCache();
610 GrFontScaler* fontScaler = GetGrFontScaler(cache);
611
612 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000613
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000614 const char* stop = text + byteLength;
615
616 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
617 while (text < stop) {
618 // the last 2 parameters are ignored
619 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
620
621 if (glyph.fWidth) {
622 SkScalar x = pos[0];
623 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
624
625 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
626 glyph.getSubXFixed(),
627 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000628 SkScalarToFixed(x),
629 SkScalarToFixed(y),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000630 fontScaler);
631 }
632 pos += scalarsPerPosition;
633 }
634 } else {
635 int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0;
636 while (text < stop) {
637 // the last 2 parameters are ignored
638 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
639
640 if (glyph.fWidth) {
641 SkScalar x = pos[0];
642 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000643
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000644 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
645 glyph.getSubXFixed(),
646 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000647 SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift),
648 SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000649 fontScaler);
650 }
651 pos += scalarsPerPosition;
652 }
653 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000654
655 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000656}