blob: 5ff13b0fdce5eab75107446bbe8c1daaeeb4edda [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
tomhudson@google.com375ff852012-06-29 18:37:57 +000010#include "GrTextContext.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000011#include "GrAtlas.h"
12#include "GrContext.h"
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000013#include "GrDrawTarget.h"
14#include "GrFontScaler.h"
tomhudson@google.com375ff852012-06-29 18:37:57 +000015#include "GrIndexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016#include "GrTextStrike.h"
17#include "GrTextStrike_impl.h"
tomhudson@google.com375ff852012-06-29 18:37:57 +000018#include "SkPath.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000019#include "SkStrokeRec.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
reed@google.com67e7cde2013-03-20 17:47:16 +000021enum {
22 // glyph rendering shares this stage with edge rendering
23 // (kEdgeEffectStage in GrContext) && SW path rendering
24 // (kPathMaskStage in GrSWMaskHelper)
25 kGlyphMaskStage = GrPaint::kTotalStages,
26};
tomhudson@google.com375ff852012-06-29 18:37:57 +000027
28void GrTextContext::flushGlyphs() {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000029 if (NULL == fDrawTarget) {
30 return;
31 }
32 GrDrawState* drawState = fDrawTarget->drawState();
reed@google.comac10a2d2010-12-22 21:39:39 +000033 if (fCurrVertex > 0) {
reed@google.comac10a2d2010-12-22 21:39:39 +000034 // setup our sampler state for our text texture/atlas
reed@google.comac10a2d2010-12-22 21:39:39 +000035 GrAssert(GrIsALIGN4(fCurrVertex));
reed@google.comac10a2d2010-12-22 21:39:39 +000036 GrAssert(fCurrTexture);
bsalomon@google.com0e354aa2012-10-08 20:44:25 +000037 GrTextureParams params(SkShader::kRepeat_TileMode, false);
reed@google.com67e7cde2013-03-20 17:47:16 +000038 drawState->createTextureEffect(kGlyphMaskStage, fCurrTexture, SkMatrix::I(), params);
bsalomon@google.com080773c2011-03-15 19:09:25 +000039
bsalomon@google.com669fdc42011-04-05 17:08:27 +000040 if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000041 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
42 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
bsalomon@google.com88becf42012-10-05 14:54:42 +000043 fPaint.hasColorStage()) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000044 GrPrintf("LCD Text will not draw correctly.\n");
45 }
46 // setup blend so that we get mask * paintColor + (1-mask)*dstColor
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000047 drawState->setBlendConstant(fPaint.getColor());
bsalomon@google.com47059542012-06-06 20:51:20 +000048 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +000049 // don't modulate by the paint's color in the frag since we're
50 // already doing it via the blend const.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000051 drawState->setColor(0xffffffff);
bsalomon@google.com080773c2011-03-15 19:09:25 +000052 } else {
53 // set back to normal in case we took LCD path previously.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000054 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
55 drawState->setColor(fPaint.getColor());
bsalomon@google.com080773c2011-03-15 19:09:25 +000056 }
57
bsalomon@google.com934c5702012-03-20 21:17:58 +000058 int nGlyphs = fCurrVertex / 4;
tomhudson@google.com375ff852012-06-29 18:37:57 +000059 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
bsalomon@google.com47059542012-06-06 20:51:20 +000060 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
bsalomon@google.com934c5702012-03-20 21:17:58 +000061 nGlyphs,
62 4, 6);
tomhudson@google.com375ff852012-06-29 18:37:57 +000063 fDrawTarget->resetVertexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +000064 fVertices = NULL;
tomhudson@google.com375ff852012-06-29 18:37:57 +000065 fMaxVertices = 0;
66 fCurrVertex = 0;
67 GrSafeSetNull(fCurrTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +000068 }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000069 drawState->disableStages();
70 fDrawTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000071}
72
bsalomon@google.com0e354aa2012-10-08 20:44:25 +000073GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint) : fPaint(paint) {
tomhudson@google.com375ff852012-06-29 18:37:57 +000074 fContext = context;
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000075 fStrike = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000076
tomhudson@google.com375ff852012-06-29 18:37:57 +000077 fCurrTexture = NULL;
78 fCurrVertex = 0;
79
robertphillips@google.combeb1af72012-07-26 18:52:16 +000080 const GrClipData* clipData = context->getClip();
81
robertphillips@google.com641f8b12012-07-31 19:15:58 +000082 GrRect devConservativeBound;
83 clipData->fClipStack->getConservativeBounds(
84 -clipData->fOrigin.fX,
85 -clipData->fOrigin.fY,
86 context->getRenderTarget()->width(),
87 context->getRenderTarget()->height(),
88 &devConservativeBound);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000089
robertphillips@google.com641f8b12012-07-31 19:15:58 +000090 devConservativeBound.roundOut(&fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000091
bsalomon@google.com858804d2012-10-15 14:25:50 +000092 fAutoMatrix.setIdentity(fContext, &fPaint);
bsalomon@google.com39149582011-06-13 21:55:32 +000093
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000094 fDrawTarget = NULL;
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +000095
reed@google.comac10a2d2010-12-22 21:39:39 +000096 fVertices = NULL;
tomhudson@google.com375ff852012-06-29 18:37:57 +000097 fMaxVertices = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000098}
99
tomhudson@google.com375ff852012-06-29 18:37:57 +0000100GrTextContext::~GrTextContext() {
101 this->flushGlyphs();
102 if (fDrawTarget) {
103 fDrawTarget->drawState()->disableStages();
104 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000105}
106
tomhudson@google.com375ff852012-06-29 18:37:57 +0000107void GrTextContext::flush() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 this->flushGlyphs();
109}
110
tomhudson@google.com375ff852012-06-29 18:37:57 +0000111void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 GrFixed vx, GrFixed vy,
113 GrFontScaler* scaler) {
114 if (NULL == fStrike) {
115 fStrike = fContext->getFontCache()->getStrike(scaler);
116 }
117
118 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
119 if (NULL == glyph || glyph->fBounds.isEmpty()) {
120 return;
121 }
122
bsalomon@google.com81712882012-11-01 17:12:34 +0000123 vx += SkIntToFixed(glyph->fBounds.fLeft);
124 vy += SkIntToFixed(glyph->fBounds.fTop);
reed@google.comac10a2d2010-12-22 21:39:39 +0000125
126 // keep them as ints until we've done the clip-test
127 GrFixed width = glyph->fBounds.width();
128 GrFixed height = glyph->fBounds.height();
129
130 // check if we clipped out
131 if (true || NULL == glyph->fAtlas) {
132 int x = vx >> 16;
133 int y = vy >> 16;
134 if (fClipRect.quickReject(x, y, x + width, y + height)) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000135// SkCLZ(3); // so we can set a break-point in the debugger
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 return;
137 }
138 }
139
140 if (NULL == glyph->fAtlas) {
141 if (fStrike->getGlyphAtlas(glyph, scaler)) {
142 goto HAS_ATLAS;
143 }
reed@google.com0ebe81a2011-04-04 20:06:59 +0000144
145 // before we purge the cache, we must flush any accumulated draws
146 this->flushGlyphs();
bsalomon@google.com193395c2012-03-30 17:35:12 +0000147 fContext->flush();
reed@google.com313e4032010-12-22 22:16:59 +0000148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 // try to purge
150 fContext->getFontCache()->purgeExceptFor(fStrike);
151 if (fStrike->getGlyphAtlas(glyph, scaler)) {
152 goto HAS_ATLAS;
153 }
154
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 if (NULL == glyph->fPath) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000156 SkPath* path = SkNEW(SkPath);
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
158 // flag the glyph as being dead?
159 delete path;
160 return;
161 }
162 glyph->fPath = path;
163 }
reed@google.com07f3ee12011-05-16 17:21:57 +0000164
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000165 GrContext::AutoMatrix am;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000166 SkMatrix translate;
bsalomon@google.com81712882012-11-01 17:12:34 +0000167 translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)),
168 SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop)));
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000169 GrPaint tmpPaint(fPaint);
170 am.setPreConcat(fContext, translate, &tmpPaint);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000171 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
172 fContext->drawPath(tmpPaint, *glyph->fPath, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 return;
174 }
175
176HAS_ATLAS:
177 GrAssert(glyph->fAtlas);
178
bsalomon@google.com85983282013-02-07 22:00:29 +0000179 // now promote them to fixed (TODO: Rethink using fixed pt).
bsalomon@google.com81712882012-11-01 17:12:34 +0000180 width = SkIntToFixed(width);
181 height = SkIntToFixed(height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000182
183 GrTexture* texture = glyph->fAtlas->texture();
tomhudson@google.com375ff852012-06-29 18:37:57 +0000184 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
tomhudson@google.com375ff852012-06-29 18:37:57 +0000186 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
187 this->flushGlyphs();
188 fCurrTexture = texture;
189 fCurrTexture->ref();
190 }
191
192 if (NULL == fVertices) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000193 // position + texture coord
194 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000195 {kVec2f_GrVertexAttribType, 0},
196 {kVec2f_GrVertexAttribType, sizeof(GrPoint)}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000197 };
reed@google.com67e7cde2013-03-20 17:47:16 +0000198 static const GrAttribBindings kAttribBindings = GrDrawState::ExplicitTexCoordAttribBindingsBit(kGlyphMaskStage);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000199
200 // If we need to reserve vertices allow the draw target to suggest
tomhudson@google.com375ff852012-06-29 18:37:57 +0000201 // a number of verts to reserve and whether to perform a flush.
202 fMaxVertices = kMinRequestedVerts;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000203 bool flush = false;
204 fDrawTarget = fContext->getTextTarget(fPaint);
205 if (NULL != fDrawTarget) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000206 fDrawTarget->drawState()->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000207 flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000208 }
tomhudson@google.com375ff852012-06-29 18:37:57 +0000209 if (flush) {
210 this->flushGlyphs();
211 fContext->flush();
bsalomon@google.comf3a7bc72013-02-05 21:32:06 +0000212 // flushGlyphs() will reset fDrawTarget to NULL.
213 fDrawTarget = fContext->getTextTarget(fPaint);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000214 fDrawTarget->drawState()->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
tomhudson@google.com375ff852012-06-29 18:37:57 +0000215 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000216 fDrawTarget->drawState()->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
reed@google.com67e7cde2013-03-20 17:47:16 +0000217 fDrawTarget->drawState()->setAttribIndex(GrDrawState::kTexCoord_AttribIndex, 1);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000218 fDrawTarget->drawState()->setAttribBindings(kAttribBindings);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000219 fMaxVertices = kDefaultRequestedVerts;
220 // ignore return, no point in flushing again.
bsalomon@google.comf3a7bc72013-02-05 21:32:06 +0000221 fDrawTarget->geometryHints(&fMaxVertices, NULL);
tomhudson@google.com375ff852012-06-29 18:37:57 +0000222
223 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
224 if (fMaxVertices < kMinRequestedVerts) {
225 fMaxVertices = kDefaultRequestedVerts;
226 } else if (fMaxVertices > maxQuadVertices) {
227 // don't exceed the limit of the index buffer
228 fMaxVertices = maxQuadVertices;
229 }
230 bool success = fDrawTarget->reserveVertexAndIndexSpace(
tomhudson@google.com375ff852012-06-29 18:37:57 +0000231 fMaxVertices,
232 0,
233 GrTCast<void**>(&fVertices),
234 NULL);
235 GrAlwaysAssert(success);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000236 GrAssert(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize());
tomhudson@google.com375ff852012-06-29 18:37:57 +0000237 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
bsalomon@google.com81712882012-11-01 17:12:34 +0000239 GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX);
240 GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
bsalomon@google.com85983282013-02-07 22:00:29 +0000242 fVertices[2*fCurrVertex].setRectFan(SkFixedToFloat(vx),
243 SkFixedToFloat(vy),
244 SkFixedToFloat(vx + width),
245 SkFixedToFloat(vy + height),
246 2 * sizeof(SkPoint));
247 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
248 SkFixedToFloat(texture->normalizeFixedY(ty)),
249 SkFixedToFloat(texture->normalizeFixedX(tx + width)),
250 SkFixedToFloat(texture->normalizeFixedY(ty + height)),
251 2 * sizeof(SkPoint));
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 fCurrVertex += 4;
253}