blob: f7d4f6f656c869f7abd61d43c0ec408630634288 [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
28static const int kGlyphCoordsAttributeIndex = 1;
29
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +000030static const int kSmallDFFontSize = 32;
31static const int kSmallDFFontLimit = 32;
32static const int kMediumDFFontSize = 64;
33static const int kMediumDFFontLimit = 64;
34static const int kLargeDFFontSize = 128;
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +000035
jvanverth@google.comd830d132013-11-11 20:54:09 +000036SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false,
37 "Dump the contents of the font cache before every purge.");
38
skia.committer@gmail.come5d70152014-01-29 07:01:48 +000039GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context,
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000040 const SkDeviceProperties& properties,
41 bool enable)
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000042 : GrTextContext(context, properties) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000043#if SK_FORCE_DISTANCEFIELD_FONTS
44 fEnableDFRendering = true;
45#else
46 fEnableDFRendering = enable;
47#endif
jvanverth@google.comd830d132013-11-11 20:54:09 +000048 fStrike = NULL;
jvanverth2d2a68c2014-06-10 06:42:56 -070049 fGammaTexture = NULL;
jvanverth@google.comd830d132013-11-11 20:54:09 +000050
51 fCurrTexture = NULL;
52 fCurrVertex = 0;
53
54 fVertices = NULL;
jvanverth@google.comd830d132013-11-11 20:54:09 +000055}
56
57GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
58 this->flushGlyphs();
jvanverth2d2a68c2014-06-10 06:42:56 -070059 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +000060}
61
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000062bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000063 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000064 return false;
65 }
66
skia.committer@gmail.come1d94432014-04-09 03:04:11 +000067 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000068 // translate well to distance
69 if (paint.getRasterizer() || paint.getMaskFilter() ||
70 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
71 return false;
72 }
73
74 // TODO: add some stroking support
75 if (paint.getStyle() != SkPaint::kFill_Style) {
76 return false;
77 }
78
79 // TODO: choose an appropriate maximum scale for distance fields and
80 // enable perspective
81 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
82 return false;
83 }
84
85 // distance fields cannot represent color fonts
86 SkScalerContext::Rec rec;
87 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
88 return rec.getFormat() != SkMask::kARGB32_Format;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000089}
90
jvanverth@google.comd830d132013-11-11 20:54:09 +000091static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
92 unsigned r = SkColorGetR(c);
93 unsigned g = SkColorGetG(c);
94 unsigned b = SkColorGetB(c);
95 return GrColorPackRGBA(r, g, b, 0xff);
96}
97
98void GrDistanceFieldTextContext::flushGlyphs() {
99 if (NULL == fDrawTarget) {
100 return;
101 }
102
103 GrDrawState* drawState = fDrawTarget->drawState();
104 GrDrawState::AutoRestoreEffects are(drawState);
105 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
106
107 if (fCurrVertex > 0) {
108 // setup our sampler state for our text texture/atlas
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000109 SkASSERT(SkIsAlign4(fCurrVertex));
jvanverth@google.comd830d132013-11-11 20:54:09 +0000110 SkASSERT(fCurrTexture);
111 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
jvanverth2d2a68c2014-06-10 06:42:56 -0700112 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000113
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000114 // Effects could be stored with one of the cache objects (atlas?)
jvanverth2d2a68c2014-06-10 06:42:56 -0700115 SkColor filteredColor;
116 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
117 if (NULL != colorFilter) {
118 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
119 } else {
120 filteredColor = fSkPaint.getColor();
121 }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000122 if (fUseLCDText) {
jvanverth2d2a68c2014-06-10 06:42:56 -0700123 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000124 bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout ==
125 fDeviceProperties.fGeometry.getLayout();
126 drawState->addCoverageEffect(GrDistanceFieldLCDTextureEffect::Create(
127 fCurrTexture,
128 params,
jvanverth2d2a68c2014-06-10 06:42:56 -0700129 fGammaTexture,
130 gammaParams,
131 colorNoPreMul,
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000132 fContext->getMatrix().rectStaysRect() &&
133 fContext->getMatrix().isSimilarity(),
134 useBGR),
135 kGlyphCoordsAttributeIndex)->unref();
jvanverth@google.comd830d132013-11-11 20:54:09 +0000136
jvanverth@google.comd830d132013-11-11 20:54:09 +0000137 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
138 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
139 fPaint.numColorStages()) {
140 GrPrintf("LCD Text will not draw correctly.\n");
141 }
142 // We don't use the GrPaint's color in this case because it's been premultiplied by
143 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
144 // the mask texture color. The end result is that we get
145 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000146 int a = SkColorGetA(fSkPaint.getColor());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000147 // paintAlpha
148 drawState->setColor(SkColorSetARGB(a, a, a, a));
149 // paintColor
jvanverth2d2a68c2014-06-10 06:42:56 -0700150 drawState->setBlendConstant(colorNoPreMul);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000151 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
152 } else {
jvanverth2d2a68c2014-06-10 06:42:56 -0700153#ifdef SK_GAMMA_APPLY_TO_A8
154 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.fGamma,
155 filteredColor);
156 drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create(
157 fCurrTexture, params,
158 fGammaTexture, gammaParams,
159 lum/255.f,
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000160 fContext->getMatrix().isSimilarity()),
161 kGlyphCoordsAttributeIndex)->unref();
jvanverth2d2a68c2014-06-10 06:42:56 -0700162#else
163 drawState->addCoverageEffect(GrDistanceFieldTextureEffect::Create(
164 fCurrTexture, params,
165 fContext->getMatrix().isSimilarity()),
166 kGlyphCoordsAttributeIndex)->unref();
167#endif
jvanverth@google.comd830d132013-11-11 20:54:09 +0000168 // set back to normal in case we took LCD path previously.
169 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
170 drawState->setColor(fPaint.getColor());
171 }
172
173 int nGlyphs = fCurrVertex / 4;
174 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
175 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
176 nGlyphs,
177 4, 6);
178 fDrawTarget->resetVertexSource();
179 fVertices = NULL;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000180 fCurrVertex = 0;
181 SkSafeSetNull(fCurrTexture);
182 }
183}
184
185namespace {
186
187// position + texture coord
188extern const GrVertexAttrib gTextVertexAttribs[] = {
189 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000190 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}
jvanverth@google.comd830d132013-11-11 20:54:09 +0000191};
192
193};
194
195void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000196 SkFixed vx, SkFixed vy,
jvanverth@google.comd830d132013-11-11 20:54:09 +0000197 GrFontScaler* scaler) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000198 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
199 if (NULL == glyph || glyph->fBounds.isEmpty()) {
200 return;
201 }
202
203 SkScalar sx = SkFixedToScalar(vx);
204 SkScalar sy = SkFixedToScalar(vy);
205/*
206 // not valid, need to find a different solution for this
207 vx += SkIntToFixed(glyph->fBounds.fLeft);
208 vy += SkIntToFixed(glyph->fBounds.fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000209
jvanverth@google.comd830d132013-11-11 20:54:09 +0000210 // keep them as ints until we've done the clip-test
211 GrFixed width = glyph->fBounds.width();
212 GrFixed height = glyph->fBounds.height();
213
214 // check if we clipped out
215 if (true || NULL == glyph->fPlot) {
216 int x = vx >> 16;
217 int y = vy >> 16;
218 if (fClipRect.quickReject(x, y, x + width, y + height)) {
219// SkCLZ(3); // so we can set a break-point in the debugger
220 return;
221 }
222 }
223*/
224 if (NULL == glyph->fPlot) {
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000225 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000226 goto HAS_ATLAS;
227 }
228
229 // try to clear out an unused plot before we flush
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000230 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
231 fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000232 goto HAS_ATLAS;
233 }
234
235 if (c_DumpFontCache) {
236#ifdef SK_DEVELOPER
237 fContext->getFontCache()->dump();
238#endif
239 }
240
241 // before we purge the cache, we must flush any accumulated draws
242 this->flushGlyphs();
243 fContext->flush();
244
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000245 // we should have an unused plot now
246 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
247 fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000248 goto HAS_ATLAS;
249 }
250
251 if (NULL == glyph->fPath) {
252 SkPath* path = SkNEW(SkPath);
253 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
254 // flag the glyph as being dead?
255 delete path;
256 return;
257 }
258 glyph->fPath = path;
259 }
260
261 GrContext::AutoMatrix am;
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000262 SkMatrix ctm;
263 ctm.setScale(fTextRatio, fTextRatio);
264 ctm.postTranslate(sx, sy);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000265 GrPaint tmpPaint(fPaint);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000266 am.setPreConcat(fContext, ctm, &tmpPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700267 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
268 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000269 return;
270 }
271
272HAS_ATLAS:
273 SkASSERT(glyph->fPlot);
274 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
275 glyph->fPlot->setDrawToken(drawToken);
276
277 GrTexture* texture = glyph->fPlot->texture();
278 SkASSERT(texture);
279
jvanverth1d386192014-07-25 11:31:13 -0700280 if (fCurrTexture != texture) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000281 this->flushGlyphs();
282 fCurrTexture = texture;
283 fCurrTexture->ref();
284 }
285
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000286 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
287 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
288 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
289 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000290
291 SkScalar scale = fTextRatio;
292 dx *= scale;
293 dy *= scale;
294 sx += dx;
295 sy += dy;
296 width *= scale;
297 height *= scale;
skia.committer@gmail.coma3b53272014-02-15 03:02:15 +0000298
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000299 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
300 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
301 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
302 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000303
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000304 static const size_t kVertexSize = 2 * sizeof(SkPoint);
jvanverth1d386192014-07-25 11:31:13 -0700305 SkPoint* positions = reinterpret_cast<SkPoint*>(
306 reinterpret_cast<intptr_t>(fVertices) + kVertexSize * fCurrVertex);
307 positions->setRectFan(sx,
308 sy,
309 sx + width,
310 sy + height,
311 kVertexSize);
312 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
313 reinterpret_cast<intptr_t>(positions) + kVertexSize - sizeof(SkPoint));
314 textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
jvanverth@google.comd830d132013-11-11 20:54:09 +0000315 SkFixedToFloat(texture->normalizeFixedY(ty)),
316 SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
317 SkFixedToFloat(texture->normalizeFixedY(ty + th)),
commit-bot@chromium.org6c89c342014-02-14 21:48:29 +0000318 kVertexSize);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000319 fCurrVertex += 4;
320}
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000321
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000322inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
323 GrTextContext::init(paint, skPaint);
324
325 fStrike = NULL;
326
327 fCurrTexture = NULL;
328 fCurrVertex = 0;
329
330 fVertices = NULL;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000331
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000332 if (fSkPaint.getTextSize() <= kSmallDFFontLimit) {
333 fTextRatio = fSkPaint.getTextSize()/kSmallDFFontSize;
334 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
335 } else if (fSkPaint.getTextSize() <= kMediumDFFontLimit) {
336 fTextRatio = fSkPaint.getTextSize()/kMediumDFFontSize;
337 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
338 } else {
339 fTextRatio = fSkPaint.getTextSize()/kLargeDFFontSize;
340 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
341 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000342
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000343 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000344
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000345 fSkPaint.setLCDRenderText(false);
346 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700347 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000348 fSkPaint.setSubpixelText(true);
jvanverth2d2a68c2014-06-10 06:42:56 -0700349
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000350}
351
352inline void GrDistanceFieldTextContext::finish() {
353 flushGlyphs();
354
355 GrTextContext::finish();
356}
357
jvanverth2d2a68c2014-06-10 06:42:56 -0700358static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
359 const SkDeviceProperties& deviceProperties,
360 GrTexture** gammaTexture) {
361 if (NULL == *gammaTexture) {
362 int width, height;
363 size_t size;
364
365#ifdef SK_GAMMA_CONTRAST
366 SkScalar contrast = SK_GAMMA_CONTRAST;
367#else
368 SkScalar contrast = 0.5f;
369#endif
370 SkScalar paintGamma = deviceProperties.fGamma;
371 SkScalar deviceGamma = deviceProperties.fGamma;
372
373 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
374 &width, &height);
375
376 SkAutoTArray<uint8_t> data((int)size);
377 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
378
379 // TODO: Update this to use the cache rather than directly creating a texture.
380 GrTextureDesc desc;
381 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
382 desc.fWidth = width;
383 desc.fHeight = height;
384 desc.fConfig = kAlpha_8_GrPixelConfig;
385
386 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
387 if (NULL == *gammaTexture) {
388 return;
389 }
390
391 context->writeTexturePixels(*gammaTexture,
392 0, 0, width, height,
393 (*gammaTexture)->config(), data.get(), 0,
394 GrContext::kDontFlush_PixelOpsFlag);
395 }
396}
397
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000398void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
399 const char text[], size_t byteLength,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000400 SkScalar x, SkScalar y) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000401 SkASSERT(byteLength == 0 || text != NULL);
402
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000403 // nothing to draw or can't draw
404 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
405 || fSkPaint.getRasterizer()) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000406 return;
407 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000408
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000409 this->init(paint, skPaint);
410
jvanverth1d386192014-07-25 11:31:13 -0700411 if (NULL == fDrawTarget) {
412 return;
413 }
414
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000415 SkScalar sizeRatio = fTextRatio;
416
417 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
418
jvanverth2d2a68c2014-06-10 06:42:56 -0700419 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
420 SkGlyphCache* cache = autoCache.getCache();
421 GrFontScaler* fontScaler = GetGrFontScaler(cache);
jvanverth1d386192014-07-25 11:31:13 -0700422 if (NULL == fStrike) {
423 fStrike = fContext->getFontCache()->getStrike(fontScaler, true);
424 }
jvanverth2d2a68c2014-06-10 06:42:56 -0700425
426 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000427
jvanverth1d386192014-07-25 11:31:13 -0700428 // allocate vertices
429 SkASSERT(NULL == fVertices);
430 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
431 SK_ARRAY_COUNT(gTextVertexAttribs));
432 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
433 bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs,
434 0,
435 &fVertices,
436 NULL);
437 GrAlwaysAssert(success);
438
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000439 // need to measure first
440 // TODO - generate positions and pre-load cache as well?
441 const char* stop = text + byteLength;
442 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
443 SkFixed stopX = 0;
444 SkFixed stopY = 0;
445
446 const char* textPtr = text;
447 while (textPtr < stop) {
448 // don't need x, y here, since all subpixel variants will have the
449 // same advance
450 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
451
452 stopX += glyph.fAdvanceX;
453 stopY += glyph.fAdvanceY;
454 }
455 SkASSERT(textPtr == stop);
456
457 SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio;
458 SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio;
459
460 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
461 alignX = SkScalarHalf(alignX);
462 alignY = SkScalarHalf(alignY);
463 }
464
465 x -= alignX;
466 y -= alignY;
467 }
468
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000469 SkFixed fx = SkScalarToFixed(x);
470 SkFixed fy = SkScalarToFixed(y);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000471 SkFixed fixedScale = SkScalarToFixed(sizeRatio);
472 while (text < stop) {
commit-bot@chromium.orga9dae712014-03-24 18:34:04 +0000473 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000474
475 if (glyph.fWidth) {
476 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
477 glyph.getSubXFixed(),
478 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000479 fx,
480 fy,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000481 fontScaler);
482 }
483
484 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale);
485 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale);
486 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000487
488 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000489}
490
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000491void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint,
492 const char text[], size_t byteLength,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000493 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000494 int scalarsPerPosition) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000495
496 SkASSERT(byteLength == 0 || text != NULL);
497 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
498
499 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000500 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000501 return;
502 }
503
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000504 this->init(paint, skPaint);
505
jvanverth1d386192014-07-25 11:31:13 -0700506 if (NULL == fDrawTarget) {
507 return;
508 }
509
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000510 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
511
jvanverth2d2a68c2014-06-10 06:42:56 -0700512 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
513 SkGlyphCache* cache = autoCache.getCache();
514 GrFontScaler* fontScaler = GetGrFontScaler(cache);
jvanverth1d386192014-07-25 11:31:13 -0700515 if (NULL == fStrike) {
516 fStrike = fContext->getFontCache()->getStrike(fontScaler, true);
517 }
518
519 // allocate vertices
520 SkASSERT(NULL == fVertices);
521 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
522 SK_ARRAY_COUNT(gTextVertexAttribs));
523 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
524 bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs,
525 0,
526 &fVertices,
527 NULL);
528 GrAlwaysAssert(success);
jvanverth2d2a68c2014-06-10 06:42:56 -0700529
530 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000531
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000532 const char* stop = text + byteLength;
533
534 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
535 while (text < stop) {
536 // the last 2 parameters are ignored
537 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
538
539 if (glyph.fWidth) {
540 SkScalar x = pos[0];
541 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
542
543 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
544 glyph.getSubXFixed(),
545 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000546 SkScalarToFixed(x),
547 SkScalarToFixed(y),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000548 fontScaler);
549 }
550 pos += scalarsPerPosition;
551 }
552 } else {
553 int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0;
554 while (text < stop) {
555 // the last 2 parameters are ignored
556 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
557
558 if (glyph.fWidth) {
559 SkScalar x = pos[0];
560 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000561
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000562 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
563 glyph.getSubXFixed(),
564 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000565 SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift),
566 SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000567 fontScaler);
568 }
569 pos += scalarsPerPosition;
570 }
571 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000572
573 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000574}