blob: 32d8f0c38454c8243c98cbc2305d5d0b95201f40 [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
jvanverth@google.comd830d132013-11-11 20:54:09 +000068 fCurrVertex = 0;
jvanverth78f07182014-07-30 06:17:59 -070069 fEffectTextureUniqueID = SK_InvalidUniqueID;
70 fEffectColor = GrColor_ILLEGAL;
71 fEffectFlags = 0;
72
jvanverth@google.comd830d132013-11-11 20:54:09 +000073 fVertices = NULL;
jvanverth@google.comd830d132013-11-11 20:54:09 +000074}
75
76GrDistanceFieldTextContext::~GrDistanceFieldTextContext() {
77 this->flushGlyphs();
jvanverth2d2a68c2014-06-10 06:42:56 -070078 SkSafeSetNull(fGammaTexture);
jvanverth@google.comd830d132013-11-11 20:54:09 +000079}
80
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +000081bool GrDistanceFieldTextContext::canDraw(const SkPaint& paint) {
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +000082 if (!fEnableDFRendering && !paint.isDistanceFieldTextTEMP()) {
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000083 return false;
84 }
85
skia.committer@gmail.come1d94432014-04-09 03:04:11 +000086 // rasterizers and mask filters modify alpha, which doesn't
commit-bot@chromium.orgeefd8a02014-04-08 20:14:32 +000087 // translate well to distance
88 if (paint.getRasterizer() || paint.getMaskFilter() ||
89 !fContext->getTextTarget()->caps()->shaderDerivativeSupport()) {
90 return false;
91 }
92
93 // TODO: add some stroking support
94 if (paint.getStyle() != SkPaint::kFill_Style) {
95 return false;
96 }
97
98 // TODO: choose an appropriate maximum scale for distance fields and
99 // enable perspective
100 if (SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix())) {
101 return false;
102 }
103
104 // distance fields cannot represent color fonts
105 SkScalerContext::Rec rec;
106 SkScalerContext::MakeRec(paint, &fDeviceProperties, NULL, &rec);
107 return rec.getFormat() != SkMask::kARGB32_Format;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000108}
109
jvanverth@google.comd830d132013-11-11 20:54:09 +0000110static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
111 unsigned r = SkColorGetR(c);
112 unsigned g = SkColorGetG(c);
113 unsigned b = SkColorGetB(c);
114 return GrColorPackRGBA(r, g, b, 0xff);
115}
116
jvanverth78f07182014-07-30 06:17:59 -0700117void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColor) {
118 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
119 GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
120
121 GrTexture* currTexture = fStrike->getTexture();
122 SkASSERT(currTexture);
123 uint32_t textureUniqueID = currTexture->getUniqueID();
124
125 // set up any flags
126 uint32_t flags = 0;
127 flags |= fContext->getMatrix().isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
128 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
129 flags |= fUseLCDText && fContext->getMatrix().rectStaysRect() ?
130 kRectToRect_DistanceFieldEffectFlag : 0;
131 bool useBGR = SkDeviceProperties::Geometry::kBGR_Layout ==
132 fDeviceProperties.fGeometry.getLayout();
133 flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
134
135 // see if we need to create a new effect
136 if (textureUniqueID != fEffectTextureUniqueID ||
137 filteredColor != fEffectColor ||
138 flags != fEffectFlags) {
139 if (fUseLCDText) {
140 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
141 fCachedEffect.reset(GrDistanceFieldLCDTextureEffect::Create(currTexture,
142 params,
143 fGammaTexture,
144 gammaParams,
145 colorNoPreMul,
146 flags));
147 } else {
148#ifdef SK_GAMMA_APPLY_TO_A8
149 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fDeviceProperties.fGamma,
150 filteredColor);
151 fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(currTexture,
152 params,
153 fGammaTexture,
154 gammaParams,
155 lum/255.f,
156 flags));
157#else
158 fCachedEffect.reset(GrDistanceFieldTextureEffect::Create(currTexture,
159 params, flags));
160#endif
161 }
162 fEffectTextureUniqueID = textureUniqueID;
163 fEffectColor = filteredColor;
164 fEffectFlags = flags;
165 }
166
167}
168
jvanverth@google.comd830d132013-11-11 20:54:09 +0000169void GrDistanceFieldTextContext::flushGlyphs() {
170 if (NULL == fDrawTarget) {
171 return;
172 }
173
174 GrDrawState* drawState = fDrawTarget->drawState();
175 GrDrawState::AutoRestoreEffects are(drawState);
176 drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
177
178 if (fCurrVertex > 0) {
179 // setup our sampler state for our text texture/atlas
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000180 SkASSERT(SkIsAlign4(fCurrVertex));
jvanverth@google.comd830d132013-11-11 20:54:09 +0000181
jvanverth78f07182014-07-30 06:17:59 -0700182 // get our current color
jvanverth2d2a68c2014-06-10 06:42:56 -0700183 SkColor filteredColor;
184 SkColorFilter* colorFilter = fSkPaint.getColorFilter();
185 if (NULL != colorFilter) {
186 filteredColor = colorFilter->filterColor(fSkPaint.getColor());
187 } else {
188 filteredColor = fSkPaint.getColor();
189 }
jvanverth78f07182014-07-30 06:17:59 -0700190 this->setupCoverageEffect(filteredColor);
191
192 // Effects could be stored with one of the cache objects (atlas?)
193 int coordsIdx = drawState->hasColorVertexAttribute() ? kGlyphCoordsWithColorAttributeIndex :
194 kGlyphCoordsNoColorAttributeIndex;
195 drawState->addCoverageEffect(fCachedEffect.get(), coordsIdx);
196
197 // Set draw state
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000198 if (fUseLCDText) {
jvanverth2d2a68c2014-06-10 06:42:56 -0700199 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000200 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
201 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
202 fPaint.numColorStages()) {
203 GrPrintf("LCD Text will not draw correctly.\n");
204 }
jvanverthfeceba52014-07-25 19:03:34 -0700205 SkASSERT(!drawState->hasColorVertexAttribute());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000206 // We don't use the GrPaint's color in this case because it's been premultiplied by
207 // alpha. Instead we feed in a non-premultiplied color, and multiply its alpha by
208 // the mask texture color. The end result is that we get
209 // mask*paintAlpha*paintColor + (1-mask*paintAlpha)*dstColor
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000210 int a = SkColorGetA(fSkPaint.getColor());
jvanverth@google.comd830d132013-11-11 20:54:09 +0000211 // paintAlpha
212 drawState->setColor(SkColorSetARGB(a, a, a, a));
213 // paintColor
jvanverth2d2a68c2014-06-10 06:42:56 -0700214 drawState->setBlendConstant(colorNoPreMul);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000215 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
216 } else {
217 // set back to normal in case we took LCD path previously.
218 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
jvanverthfeceba52014-07-25 19:03:34 -0700219 //drawState->setColor(fPaint.getColor());
220 // We're using per-vertex color.
221 SkASSERT(drawState->hasColorVertexAttribute());
222 drawState->setColor(0xFFFFFFFF);
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,
228 4, 6);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000229 fCurrVertex = 0;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000230 }
jvanverthf17bc6c2014-07-25 16:46:53 -0700231 fDrawTarget->resetVertexSource();
232 fVertices = NULL;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000233}
234
jvanverth@google.comd830d132013-11-11 20:54:09 +0000235void GrDistanceFieldTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000236 SkFixed vx, SkFixed vy,
jvanverth@google.comd830d132013-11-11 20:54:09 +0000237 GrFontScaler* scaler) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000238 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
239 if (NULL == glyph || glyph->fBounds.isEmpty()) {
240 return;
241 }
242
243 SkScalar sx = SkFixedToScalar(vx);
244 SkScalar sy = SkFixedToScalar(vy);
245/*
246 // not valid, need to find a different solution for this
247 vx += SkIntToFixed(glyph->fBounds.fLeft);
248 vy += SkIntToFixed(glyph->fBounds.fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000249
jvanverth@google.comd830d132013-11-11 20:54:09 +0000250 // keep them as ints until we've done the clip-test
251 GrFixed width = glyph->fBounds.width();
252 GrFixed height = glyph->fBounds.height();
253
254 // check if we clipped out
255 if (true || NULL == glyph->fPlot) {
256 int x = vx >> 16;
257 int y = vy >> 16;
258 if (fClipRect.quickReject(x, y, x + width, y + height)) {
259// SkCLZ(3); // so we can set a break-point in the debugger
260 return;
261 }
262 }
263*/
264 if (NULL == glyph->fPlot) {
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000265 if (fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000266 goto HAS_ATLAS;
267 }
268
269 // try to clear out an unused plot before we flush
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000270 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
271 fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000272 goto HAS_ATLAS;
273 }
274
275 if (c_DumpFontCache) {
276#ifdef SK_DEVELOPER
277 fContext->getFontCache()->dump();
278#endif
279 }
280
281 // before we purge the cache, we must flush any accumulated draws
282 this->flushGlyphs();
283 fContext->flush();
284
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000285 // we should have an unused plot now
286 if (fContext->getFontCache()->freeUnusedPlot(fStrike) &&
287 fStrike->addGlyphToAtlas(glyph, scaler)) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000288 goto HAS_ATLAS;
289 }
290
291 if (NULL == glyph->fPath) {
292 SkPath* path = SkNEW(SkPath);
293 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
294 // flag the glyph as being dead?
295 delete path;
296 return;
297 }
298 glyph->fPath = path;
299 }
300
301 GrContext::AutoMatrix am;
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000302 SkMatrix ctm;
303 ctm.setScale(fTextRatio, fTextRatio);
304 ctm.postTranslate(sx, sy);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000305 GrPaint tmpPaint(fPaint);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000306 am.setPreConcat(fContext, ctm, &tmpPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700307 GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle);
308 fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000309 return;
310 }
311
312HAS_ATLAS:
313 SkASSERT(glyph->fPlot);
314 GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken();
315 glyph->fPlot->setDrawToken(drawToken);
316
317 GrTexture* texture = glyph->fPlot->texture();
318 SkASSERT(texture);
319
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000320 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
321 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
322 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
323 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000324
325 SkScalar scale = fTextRatio;
326 dx *= scale;
327 dy *= scale;
328 sx += dx;
329 sy += dy;
330 width *= scale;
331 height *= scale;
skia.committer@gmail.coma3b53272014-02-15 03:02:15 +0000332
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +0000333 SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX + SK_DistanceFieldInset);
334 SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY + SK_DistanceFieldInset);
335 SkFixed tw = SkIntToFixed(glyph->fBounds.width() - 2*SK_DistanceFieldInset);
336 SkFixed th = SkIntToFixed(glyph->fBounds.height() - 2*SK_DistanceFieldInset);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000337
jvanverthfeceba52014-07-25 19:03:34 -0700338 size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint))
339 : (2 * sizeof(SkPoint) + sizeof(GrColor));
340
341 SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexSize());
342
jvanverthf17bc6c2014-07-25 16:46:53 -0700343 SkPoint* positions = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700344 reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex);
345 positions->setRectFan(sx, sy, sx + width, sy + height, vertSize);
346
347 // The texture coords are last in both the with and without color vertex layouts.
jvanverthf17bc6c2014-07-25 16:46:53 -0700348 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(
jvanverthfeceba52014-07-25 19:03:34 -0700349 reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint));
jvanverthf17bc6c2014-07-25 16:46:53 -0700350 textureCoords->setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
jvanverthfeceba52014-07-25 19:03:34 -0700351 SkFixedToFloat(texture->normalizeFixedY(ty)),
352 SkFixedToFloat(texture->normalizeFixedX(tx + tw)),
353 SkFixedToFloat(texture->normalizeFixedY(ty + th)),
354 vertSize);
355 if (!fUseLCDText) {
356 // color comes after position.
357 GrColor* colors = reinterpret_cast<GrColor*>(positions + 1);
358 for (int i = 0; i < 4; ++i) {
359 *colors = fPaint.getColor();
360 colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize);
361 }
362 }
363
jvanverth@google.comd830d132013-11-11 20:54:09 +0000364 fCurrVertex += 4;
365}
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000366
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000367inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint& skPaint) {
368 GrTextContext::init(paint, skPaint);
369
370 fStrike = NULL;
371
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000372 fCurrVertex = 0;
373
374 fVertices = NULL;
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000375
commit-bot@chromium.orgdc5cd852014-04-02 19:24:32 +0000376 if (fSkPaint.getTextSize() <= kSmallDFFontLimit) {
377 fTextRatio = fSkPaint.getTextSize()/kSmallDFFontSize;
378 fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
379 } else if (fSkPaint.getTextSize() <= kMediumDFFontLimit) {
380 fTextRatio = fSkPaint.getTextSize()/kMediumDFFontSize;
381 fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
382 } else {
383 fTextRatio = fSkPaint.getTextSize()/kLargeDFFontSize;
384 fSkPaint.setTextSize(SkIntToScalar(kLargeDFFontSize));
385 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000386
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000387 fUseLCDText = fSkPaint.isLCDRenderText();
skia.committer@gmail.com221b9112014-04-04 03:04:32 +0000388
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000389 fSkPaint.setLCDRenderText(false);
390 fSkPaint.setAutohinted(false);
jvanverth2d2a68c2014-06-10 06:42:56 -0700391 fSkPaint.setHinting(SkPaint::kNormal_Hinting);
commit-bot@chromium.org0bed43c2014-03-14 21:22:38 +0000392 fSkPaint.setSubpixelText(true);
jvanverth2d2a68c2014-06-10 06:42:56 -0700393
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000394}
395
396inline void GrDistanceFieldTextContext::finish() {
jvanverthfeceba52014-07-25 19:03:34 -0700397 this->flushGlyphs();
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000398
399 GrTextContext::finish();
400}
401
jvanverth2d2a68c2014-06-10 06:42:56 -0700402static void setup_gamma_texture(GrContext* context, const SkGlyphCache* cache,
403 const SkDeviceProperties& deviceProperties,
404 GrTexture** gammaTexture) {
405 if (NULL == *gammaTexture) {
406 int width, height;
407 size_t size;
408
409#ifdef SK_GAMMA_CONTRAST
410 SkScalar contrast = SK_GAMMA_CONTRAST;
411#else
412 SkScalar contrast = 0.5f;
413#endif
414 SkScalar paintGamma = deviceProperties.fGamma;
415 SkScalar deviceGamma = deviceProperties.fGamma;
416
417 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
418 &width, &height);
419
420 SkAutoTArray<uint8_t> data((int)size);
421 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
422
423 // TODO: Update this to use the cache rather than directly creating a texture.
424 GrTextureDesc desc;
425 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
426 desc.fWidth = width;
427 desc.fHeight = height;
428 desc.fConfig = kAlpha_8_GrPixelConfig;
429
430 *gammaTexture = context->getGpu()->createTexture(desc, NULL, 0);
431 if (NULL == *gammaTexture) {
432 return;
433 }
434
435 context->writeTexturePixels(*gammaTexture,
436 0, 0, width, height,
437 (*gammaTexture)->config(), data.get(), 0,
438 GrContext::kDontFlush_PixelOpsFlag);
439 }
440}
441
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000442void GrDistanceFieldTextContext::drawText(const GrPaint& paint, const SkPaint& skPaint,
443 const char text[], size_t byteLength,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000444 SkScalar x, SkScalar y) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000445 SkASSERT(byteLength == 0 || text != NULL);
446
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000447 // nothing to draw or can't draw
448 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/
449 || fSkPaint.getRasterizer()) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000450 return;
451 }
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000452
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000453 this->init(paint, skPaint);
454
jvanverthf17bc6c2014-07-25 16:46:53 -0700455 if (NULL == fDrawTarget) {
456 return;
457 }
458
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000459 SkScalar sizeRatio = fTextRatio;
460
461 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
462
jvanverth2d2a68c2014-06-10 06:42:56 -0700463 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
464 SkGlyphCache* cache = autoCache.getCache();
465 GrFontScaler* fontScaler = GetGrFontScaler(cache);
jvanverthf17bc6c2014-07-25 16:46:53 -0700466 if (NULL == fStrike) {
467 fStrike = fContext->getFontCache()->getStrike(fontScaler, true);
468 }
jvanverth2d2a68c2014-06-10 06:42:56 -0700469
470 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000471
jvanverthf17bc6c2014-07-25 16:46:53 -0700472 // allocate vertices
473 SkASSERT(NULL == fVertices);
jvanverthfeceba52014-07-25 19:03:34 -0700474 if (!fUseLCDText) {
475 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
476 SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
477 } else {
478 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
479 SK_ARRAY_COUNT(gTextVertexAttribs));
480 }
jvanverthf17bc6c2014-07-25 16:46:53 -0700481 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
482 bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs,
483 0,
484 &fVertices,
485 NULL);
486 GrAlwaysAssert(success);
487
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000488 // need to measure first
489 // TODO - generate positions and pre-load cache as well?
490 const char* stop = text + byteLength;
491 if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) {
492 SkFixed stopX = 0;
493 SkFixed stopY = 0;
494
495 const char* textPtr = text;
496 while (textPtr < stop) {
497 // don't need x, y here, since all subpixel variants will have the
498 // same advance
499 const SkGlyph& glyph = glyphCacheProc(cache, &textPtr, 0, 0);
500
501 stopX += glyph.fAdvanceX;
502 stopY += glyph.fAdvanceY;
503 }
504 SkASSERT(textPtr == stop);
505
506 SkScalar alignX = SkFixedToScalar(stopX)*sizeRatio;
507 SkScalar alignY = SkFixedToScalar(stopY)*sizeRatio;
508
509 if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) {
510 alignX = SkScalarHalf(alignX);
511 alignY = SkScalarHalf(alignY);
512 }
513
514 x -= alignX;
515 y -= alignY;
516 }
517
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000518 SkFixed fx = SkScalarToFixed(x);
519 SkFixed fy = SkScalarToFixed(y);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000520 SkFixed fixedScale = SkScalarToFixed(sizeRatio);
521 while (text < stop) {
commit-bot@chromium.orga9dae712014-03-24 18:34:04 +0000522 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000523
524 if (glyph.fWidth) {
525 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
526 glyph.getSubXFixed(),
527 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000528 fx,
529 fy,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000530 fontScaler);
531 }
532
533 fx += SkFixedMul_portable(glyph.fAdvanceX, fixedScale);
534 fy += SkFixedMul_portable(glyph.fAdvanceY, fixedScale);
535 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000536
537 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000538}
539
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000540void GrDistanceFieldTextContext::drawPosText(const GrPaint& paint, const SkPaint& skPaint,
541 const char text[], size_t byteLength,
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000542 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.orge8612d92014-01-28 22:02:07 +0000543 int scalarsPerPosition) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000544
545 SkASSERT(byteLength == 0 || text != NULL);
546 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
547
548 // nothing to draw
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000549 if (text == NULL || byteLength == 0 /* no raster clip? || fRC->isEmpty()*/) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000550 return;
551 }
552
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000553 this->init(paint, skPaint);
554
jvanverthf17bc6c2014-07-25 16:46:53 -0700555 if (NULL == fDrawTarget) {
556 return;
557 }
558
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000559 SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc();
560
jvanverth2d2a68c2014-06-10 06:42:56 -0700561 SkAutoGlyphCacheNoGamma autoCache(fSkPaint, &fDeviceProperties, NULL);
562 SkGlyphCache* cache = autoCache.getCache();
563 GrFontScaler* fontScaler = GetGrFontScaler(cache);
jvanverthf17bc6c2014-07-25 16:46:53 -0700564 if (NULL == fStrike) {
565 fStrike = fContext->getFontCache()->getStrike(fontScaler, true);
566 }
567
568 // allocate vertices
569 SkASSERT(NULL == fVertices);
jvanverthfeceba52014-07-25 19:03:34 -0700570 if (!fUseLCDText) {
571 fDrawTarget->drawState()->setVertexAttribs<gTextVertexWithColorAttribs>(
572 SK_ARRAY_COUNT(gTextVertexWithColorAttribs));
573 } else {
574 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
575 SK_ARRAY_COUNT(gTextVertexAttribs));
576 }
jvanverthf17bc6c2014-07-25 16:46:53 -0700577 int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL);
578 bool success = fDrawTarget->reserveVertexAndIndexSpace(4*numGlyphs,
579 0,
580 &fVertices,
581 NULL);
582 GrAlwaysAssert(success);
jvanverth2d2a68c2014-06-10 06:42:56 -0700583
584 setup_gamma_texture(fContext, cache, fDeviceProperties, &fGammaTexture);
skia.committer@gmail.come5d70152014-01-29 07:01:48 +0000585
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000586 const char* stop = text + byteLength;
587
588 if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) {
589 while (text < stop) {
590 // the last 2 parameters are ignored
591 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
592
593 if (glyph.fWidth) {
594 SkScalar x = pos[0];
595 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
596
597 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
598 glyph.getSubXFixed(),
599 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000600 SkScalarToFixed(x),
601 SkScalarToFixed(y),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000602 fontScaler);
603 }
604 pos += scalarsPerPosition;
605 }
606 } else {
607 int alignShift = SkPaint::kCenter_Align == fSkPaint.getTextAlign() ? 1 : 0;
608 while (text < stop) {
609 // the last 2 parameters are ignored
610 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
611
612 if (glyph.fWidth) {
613 SkScalar x = pos[0];
614 SkScalar y = scalarsPerPosition == 1 ? constY : pos[1];
skia.committer@gmail.com22e96722013-12-20 07:01:36 +0000615
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000616 this->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
617 glyph.getSubXFixed(),
618 glyph.getSubYFixed()),
commit-bot@chromium.org5408f8f2014-05-21 19:44:24 +0000619 SkScalarToFixed(x) - (glyph.fAdvanceX >> alignShift),
620 SkScalarToFixed(y) - (glyph.fAdvanceY >> alignShift),
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000621 fontScaler);
622 }
623 pos += scalarsPerPosition;
624 }
625 }
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000626
627 this->finish();
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000628}