blob: ab2bc424d6cc66e5e9f6f0c4b967c44040d268a5 [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
bsalomon@google.comc7818882013-03-20 19:19:53 +000021// glyph rendering shares this stage with edge rendering (kEdgeEffectStage in GrContext) && SW path
22// rendering (kPathMaskStage in GrSWMaskHelper)
23static const int kGlyphMaskStage = GrPaint::kTotalStages;
24static const int kGlyphCoordsAttributeIndex = 1;
tomhudson@google.com375ff852012-06-29 18:37:57 +000025
26void GrTextContext::flushGlyphs() {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000027 if (NULL == fDrawTarget) {
28 return;
29 }
30 GrDrawState* drawState = fDrawTarget->drawState();
reed@google.comac10a2d2010-12-22 21:39:39 +000031 if (fCurrVertex > 0) {
reed@google.comac10a2d2010-12-22 21:39:39 +000032 // setup our sampler state for our text texture/atlas
reed@google.comac10a2d2010-12-22 21:39:39 +000033 GrAssert(GrIsALIGN4(fCurrVertex));
reed@google.comac10a2d2010-12-22 21:39:39 +000034 GrAssert(fCurrTexture);
bsalomon@google.com0e354aa2012-10-08 20:44:25 +000035 GrTextureParams params(SkShader::kRepeat_TileMode, false);
bsalomon@google.comc7818882013-03-20 19:19:53 +000036
37 // This effect could be stored with one of the cache objects (atlas?)
38 drawState->setEffect(kGlyphMaskStage,
39 GrSimpleTextureEffect::CreateWithCustomCoords(fCurrTexture, params),
40 kGlyphCoordsAttributeIndex)->unref();
bsalomon@google.com080773c2011-03-15 19:09:25 +000041
bsalomon@google.com669fdc42011-04-05 17:08:27 +000042 if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000043 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
44 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
bsalomon@google.com88becf42012-10-05 14:54:42 +000045 fPaint.hasColorStage()) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000046 GrPrintf("LCD Text will not draw correctly.\n");
47 }
48 // setup blend so that we get mask * paintColor + (1-mask)*dstColor
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000049 drawState->setBlendConstant(fPaint.getColor());
bsalomon@google.com47059542012-06-06 20:51:20 +000050 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 // don't modulate by the paint's color in the frag since we're
52 // already doing it via the blend const.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000053 drawState->setColor(0xffffffff);
bsalomon@google.com080773c2011-03-15 19:09:25 +000054 } else {
55 // set back to normal in case we took LCD path previously.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000056 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
57 drawState->setColor(fPaint.getColor());
bsalomon@google.com080773c2011-03-15 19:09:25 +000058 }
59
bsalomon@google.com934c5702012-03-20 21:17:58 +000060 int nGlyphs = fCurrVertex / 4;
tomhudson@google.com375ff852012-06-29 18:37:57 +000061 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
bsalomon@google.com47059542012-06-06 20:51:20 +000062 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
bsalomon@google.com934c5702012-03-20 21:17:58 +000063 nGlyphs,
64 4, 6);
tomhudson@google.com375ff852012-06-29 18:37:57 +000065 fDrawTarget->resetVertexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +000066 fVertices = NULL;
tomhudson@google.com375ff852012-06-29 18:37:57 +000067 fMaxVertices = 0;
68 fCurrVertex = 0;
69 GrSafeSetNull(fCurrTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +000070 }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000071 drawState->disableStages();
72 fDrawTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000073}
74
bsalomon@google.com0e354aa2012-10-08 20:44:25 +000075GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint) : fPaint(paint) {
tomhudson@google.com375ff852012-06-29 18:37:57 +000076 fContext = context;
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000077 fStrike = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000078
tomhudson@google.com375ff852012-06-29 18:37:57 +000079 fCurrTexture = NULL;
80 fCurrVertex = 0;
81
robertphillips@google.combeb1af72012-07-26 18:52:16 +000082 const GrClipData* clipData = context->getClip();
83
robertphillips@google.com641f8b12012-07-31 19:15:58 +000084 GrRect devConservativeBound;
85 clipData->fClipStack->getConservativeBounds(
86 -clipData->fOrigin.fX,
87 -clipData->fOrigin.fY,
88 context->getRenderTarget()->width(),
89 context->getRenderTarget()->height(),
90 &devConservativeBound);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000091
robertphillips@google.com641f8b12012-07-31 19:15:58 +000092 devConservativeBound.roundOut(&fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000093
bsalomon@google.com858804d2012-10-15 14:25:50 +000094 fAutoMatrix.setIdentity(fContext, &fPaint);
bsalomon@google.com39149582011-06-13 21:55:32 +000095
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000096 fDrawTarget = NULL;
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +000097
reed@google.comac10a2d2010-12-22 21:39:39 +000098 fVertices = NULL;
tomhudson@google.com375ff852012-06-29 18:37:57 +000099 fMaxVertices = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000100}
101
tomhudson@google.com375ff852012-06-29 18:37:57 +0000102GrTextContext::~GrTextContext() {
103 this->flushGlyphs();
104 if (fDrawTarget) {
105 fDrawTarget->drawState()->disableStages();
106 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000107}
108
tomhudson@google.com375ff852012-06-29 18:37:57 +0000109void GrTextContext::flush() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 this->flushGlyphs();
111}
112
tomhudson@google.com375ff852012-06-29 18:37:57 +0000113void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 GrFixed vx, GrFixed vy,
115 GrFontScaler* scaler) {
116 if (NULL == fStrike) {
117 fStrike = fContext->getFontCache()->getStrike(scaler);
118 }
119
120 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
121 if (NULL == glyph || glyph->fBounds.isEmpty()) {
122 return;
123 }
124
bsalomon@google.com81712882012-11-01 17:12:34 +0000125 vx += SkIntToFixed(glyph->fBounds.fLeft);
126 vy += SkIntToFixed(glyph->fBounds.fTop);
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
128 // keep them as ints until we've done the clip-test
129 GrFixed width = glyph->fBounds.width();
130 GrFixed height = glyph->fBounds.height();
131
132 // check if we clipped out
133 if (true || NULL == glyph->fAtlas) {
134 int x = vx >> 16;
135 int y = vy >> 16;
136 if (fClipRect.quickReject(x, y, x + width, y + height)) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000137// SkCLZ(3); // so we can set a break-point in the debugger
reed@google.comac10a2d2010-12-22 21:39:39 +0000138 return;
139 }
140 }
141
142 if (NULL == glyph->fAtlas) {
143 if (fStrike->getGlyphAtlas(glyph, scaler)) {
144 goto HAS_ATLAS;
145 }
reed@google.com0ebe81a2011-04-04 20:06:59 +0000146
147 // before we purge the cache, we must flush any accumulated draws
148 this->flushGlyphs();
bsalomon@google.com193395c2012-03-30 17:35:12 +0000149 fContext->flush();
reed@google.com313e4032010-12-22 22:16:59 +0000150
reed@google.comac10a2d2010-12-22 21:39:39 +0000151 // try to purge
152 fContext->getFontCache()->purgeExceptFor(fStrike);
153 if (fStrike->getGlyphAtlas(glyph, scaler)) {
154 goto HAS_ATLAS;
155 }
156
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 if (NULL == glyph->fPath) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000158 SkPath* path = SkNEW(SkPath);
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
160 // flag the glyph as being dead?
161 delete path;
162 return;
163 }
164 glyph->fPath = path;
165 }
reed@google.com07f3ee12011-05-16 17:21:57 +0000166
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000167 GrContext::AutoMatrix am;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000168 SkMatrix translate;
bsalomon@google.com81712882012-11-01 17:12:34 +0000169 translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)),
170 SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop)));
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000171 GrPaint tmpPaint(fPaint);
172 am.setPreConcat(fContext, translate, &tmpPaint);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000173 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
174 fContext->drawPath(tmpPaint, *glyph->fPath, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +0000175 return;
176 }
177
178HAS_ATLAS:
179 GrAssert(glyph->fAtlas);
180
bsalomon@google.com85983282013-02-07 22:00:29 +0000181 // now promote them to fixed (TODO: Rethink using fixed pt).
bsalomon@google.com81712882012-11-01 17:12:34 +0000182 width = SkIntToFixed(width);
183 height = SkIntToFixed(height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
185 GrTexture* texture = glyph->fAtlas->texture();
tomhudson@google.com375ff852012-06-29 18:37:57 +0000186 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000187
tomhudson@google.com375ff852012-06-29 18:37:57 +0000188 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
189 this->flushGlyphs();
190 fCurrTexture = texture;
191 fCurrTexture->ref();
192 }
193
194 if (NULL == fVertices) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000195 // position + texture coord
196 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000197 {kVec2f_GrVertexAttribType, 0},
198 {kVec2f_GrVertexAttribType, sizeof(GrPoint)}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000199 };
bsalomon@google.comc7818882013-03-20 19:19:53 +0000200 static const GrAttribBindings kAttribBindings = 0;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000201
202 // If we need to reserve vertices allow the draw target to suggest
tomhudson@google.com375ff852012-06-29 18:37:57 +0000203 // a number of verts to reserve and whether to perform a flush.
204 fMaxVertices = kMinRequestedVerts;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000205 bool flush = false;
206 fDrawTarget = fContext->getTextTarget(fPaint);
207 if (NULL != fDrawTarget) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000208 fDrawTarget->drawState()->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000209 flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000210 }
tomhudson@google.com375ff852012-06-29 18:37:57 +0000211 if (flush) {
212 this->flushGlyphs();
213 fContext->flush();
bsalomon@google.comf3a7bc72013-02-05 21:32:06 +0000214 // flushGlyphs() will reset fDrawTarget to NULL.
215 fDrawTarget = fContext->getTextTarget(fPaint);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000216 fDrawTarget->drawState()->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
tomhudson@google.com375ff852012-06-29 18:37:57 +0000217 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000218 fDrawTarget->drawState()->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000219 fDrawTarget->drawState()->setAttribBindings(kAttribBindings);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000220 fMaxVertices = kDefaultRequestedVerts;
221 // ignore return, no point in flushing again.
bsalomon@google.comf3a7bc72013-02-05 21:32:06 +0000222 fDrawTarget->geometryHints(&fMaxVertices, NULL);
tomhudson@google.com375ff852012-06-29 18:37:57 +0000223
224 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
225 if (fMaxVertices < kMinRequestedVerts) {
226 fMaxVertices = kDefaultRequestedVerts;
227 } else if (fMaxVertices > maxQuadVertices) {
228 // don't exceed the limit of the index buffer
229 fMaxVertices = maxQuadVertices;
230 }
231 bool success = fDrawTarget->reserveVertexAndIndexSpace(
tomhudson@google.com375ff852012-06-29 18:37:57 +0000232 fMaxVertices,
233 0,
234 GrTCast<void**>(&fVertices),
235 NULL);
236 GrAlwaysAssert(success);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000237 GrAssert(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize());
tomhudson@google.com375ff852012-06-29 18:37:57 +0000238 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
bsalomon@google.com81712882012-11-01 17:12:34 +0000240 GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX);
241 GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
bsalomon@google.com85983282013-02-07 22:00:29 +0000243 fVertices[2*fCurrVertex].setRectFan(SkFixedToFloat(vx),
244 SkFixedToFloat(vy),
245 SkFixedToFloat(vx + width),
246 SkFixedToFloat(vy + height),
247 2 * sizeof(SkPoint));
248 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
249 SkFixedToFloat(texture->normalizeFixedY(ty)),
250 SkFixedToFloat(texture->normalizeFixedX(tx + width)),
251 SkFixedToFloat(texture->normalizeFixedY(ty + height)),
252 2 * sizeof(SkPoint));
reed@google.comac10a2d2010-12-22 21:39:39 +0000253 fCurrVertex += 4;
254}