blob: 19f18b50a50cb63be85726e8655d0ea662268fc6 [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 }
bsalomon@google.com21c10c52013-06-13 17:44:07 +000030 GrDrawTarget::AutoStateRestore asr(fDrawTarget, GrDrawTarget::kPreserve_ASRInit);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000031 GrDrawState* drawState = fDrawTarget->drawState();
bsalomon@google.com21c10c52013-06-13 17:44:07 +000032 drawState->setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget());
33
reed@google.comac10a2d2010-12-22 21:39:39 +000034 if (fCurrVertex > 0) {
reed@google.comac10a2d2010-12-22 21:39:39 +000035 // setup our sampler state for our text texture/atlas
reed@google.comac10a2d2010-12-22 21:39:39 +000036 GrAssert(GrIsALIGN4(fCurrVertex));
reed@google.comac10a2d2010-12-22 21:39:39 +000037 GrAssert(fCurrTexture);
bsalomon@google.com0e354aa2012-10-08 20:44:25 +000038 GrTextureParams params(SkShader::kRepeat_TileMode, false);
bsalomon@google.comc7818882013-03-20 19:19:53 +000039
40 // This effect could be stored with one of the cache objects (atlas?)
41 drawState->setEffect(kGlyphMaskStage,
42 GrSimpleTextureEffect::CreateWithCustomCoords(fCurrTexture, params),
43 kGlyphCoordsAttributeIndex)->unref();
bsalomon@google.com080773c2011-03-15 19:09:25 +000044
bsalomon@google.com669fdc42011-04-05 17:08:27 +000045 if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000046 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
47 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
bsalomon@google.com88becf42012-10-05 14:54:42 +000048 fPaint.hasColorStage()) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000049 GrPrintf("LCD Text will not draw correctly.\n");
50 }
51 // setup blend so that we get mask * paintColor + (1-mask)*dstColor
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000052 drawState->setBlendConstant(fPaint.getColor());
bsalomon@google.com47059542012-06-06 20:51:20 +000053 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +000054 // don't modulate by the paint's color in the frag since we're
55 // already doing it via the blend const.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000056 drawState->setColor(0xffffffff);
bsalomon@google.com080773c2011-03-15 19:09:25 +000057 } else {
58 // set back to normal in case we took LCD path previously.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000059 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
60 drawState->setColor(fPaint.getColor());
bsalomon@google.com080773c2011-03-15 19:09:25 +000061 }
62
bsalomon@google.com934c5702012-03-20 21:17:58 +000063 int nGlyphs = fCurrVertex / 4;
tomhudson@google.com375ff852012-06-29 18:37:57 +000064 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
bsalomon@google.com47059542012-06-06 20:51:20 +000065 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
bsalomon@google.com934c5702012-03-20 21:17:58 +000066 nGlyphs,
67 4, 6);
tomhudson@google.com375ff852012-06-29 18:37:57 +000068 fDrawTarget->resetVertexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +000069 fVertices = NULL;
tomhudson@google.com375ff852012-06-29 18:37:57 +000070 fMaxVertices = 0;
71 fCurrVertex = 0;
72 GrSafeSetNull(fCurrTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +000073 }
74}
75
bsalomon@google.com0e354aa2012-10-08 20:44:25 +000076GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint) : fPaint(paint) {
tomhudson@google.com375ff852012-06-29 18:37:57 +000077 fContext = context;
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000078 fStrike = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000079
tomhudson@google.com375ff852012-06-29 18:37:57 +000080 fCurrTexture = NULL;
81 fCurrVertex = 0;
82
robertphillips@google.combeb1af72012-07-26 18:52:16 +000083 const GrClipData* clipData = context->getClip();
84
robertphillips@google.com641f8b12012-07-31 19:15:58 +000085 GrRect devConservativeBound;
86 clipData->fClipStack->getConservativeBounds(
87 -clipData->fOrigin.fX,
88 -clipData->fOrigin.fY,
89 context->getRenderTarget()->width(),
90 context->getRenderTarget()->height(),
91 &devConservativeBound);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000092
robertphillips@google.com641f8b12012-07-31 19:15:58 +000093 devConservativeBound.roundOut(&fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000094
bsalomon@google.com858804d2012-10-15 14:25:50 +000095 fAutoMatrix.setIdentity(fContext, &fPaint);
bsalomon@google.com39149582011-06-13 21:55:32 +000096
bsalomon@google.com21c10c52013-06-13 17:44:07 +000097 fDrawTarget = fContext->getTextTarget();
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +000098
reed@google.comac10a2d2010-12-22 21:39:39 +000099 fVertices = NULL;
tomhudson@google.com375ff852012-06-29 18:37:57 +0000100 fMaxVertices = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000101}
102
tomhudson@google.com375ff852012-06-29 18:37:57 +0000103GrTextContext::~GrTextContext() {
104 this->flushGlyphs();
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
robertphillips@google.com42903302013-04-20 12:26:07 +0000111namespace {
112
113// position + texture coord
114extern const GrVertexAttrib gTextVertexAttribs[] = {
115 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
116 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
117};
118
119};
120
tomhudson@google.com375ff852012-06-29 18:37:57 +0000121void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 GrFixed vx, GrFixed vy,
123 GrFontScaler* scaler) {
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000124 if (NULL == fDrawTarget) {
125 return;
126 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 if (NULL == fStrike) {
128 fStrike = fContext->getFontCache()->getStrike(scaler);
129 }
130
131 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
132 if (NULL == glyph || glyph->fBounds.isEmpty()) {
133 return;
134 }
135
bsalomon@google.com81712882012-11-01 17:12:34 +0000136 vx += SkIntToFixed(glyph->fBounds.fLeft);
137 vy += SkIntToFixed(glyph->fBounds.fTop);
reed@google.comac10a2d2010-12-22 21:39:39 +0000138
139 // keep them as ints until we've done the clip-test
140 GrFixed width = glyph->fBounds.width();
141 GrFixed height = glyph->fBounds.height();
142
143 // check if we clipped out
144 if (true || NULL == glyph->fAtlas) {
145 int x = vx >> 16;
146 int y = vy >> 16;
147 if (fClipRect.quickReject(x, y, x + width, y + height)) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000148// SkCLZ(3); // so we can set a break-point in the debugger
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 return;
150 }
151 }
152
153 if (NULL == glyph->fAtlas) {
154 if (fStrike->getGlyphAtlas(glyph, scaler)) {
155 goto HAS_ATLAS;
156 }
reed@google.com0ebe81a2011-04-04 20:06:59 +0000157
158 // before we purge the cache, we must flush any accumulated draws
159 this->flushGlyphs();
bsalomon@google.com193395c2012-03-30 17:35:12 +0000160 fContext->flush();
reed@google.com313e4032010-12-22 22:16:59 +0000161
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 // try to purge
163 fContext->getFontCache()->purgeExceptFor(fStrike);
164 if (fStrike->getGlyphAtlas(glyph, scaler)) {
165 goto HAS_ATLAS;
166 }
167
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 if (NULL == glyph->fPath) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000169 SkPath* path = SkNEW(SkPath);
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
171 // flag the glyph as being dead?
172 delete path;
173 return;
174 }
175 glyph->fPath = path;
176 }
reed@google.com07f3ee12011-05-16 17:21:57 +0000177
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000178 GrContext::AutoMatrix am;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000179 SkMatrix translate;
bsalomon@google.com81712882012-11-01 17:12:34 +0000180 translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)),
181 SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop)));
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000182 GrPaint tmpPaint(fPaint);
183 am.setPreConcat(fContext, translate, &tmpPaint);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000184 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
185 fContext->drawPath(tmpPaint, *glyph->fPath, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +0000186 return;
187 }
188
189HAS_ATLAS:
190 GrAssert(glyph->fAtlas);
191
bsalomon@google.com85983282013-02-07 22:00:29 +0000192 // now promote them to fixed (TODO: Rethink using fixed pt).
bsalomon@google.com81712882012-11-01 17:12:34 +0000193 width = SkIntToFixed(width);
194 height = SkIntToFixed(height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000195
196 GrTexture* texture = glyph->fAtlas->texture();
tomhudson@google.com375ff852012-06-29 18:37:57 +0000197 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000198
tomhudson@google.com375ff852012-06-29 18:37:57 +0000199 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
200 this->flushGlyphs();
201 fCurrTexture = texture;
202 fCurrTexture->ref();
203 }
204
205 if (NULL == fVertices) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000206 // If we need to reserve vertices allow the draw target to suggest
tomhudson@google.com375ff852012-06-29 18:37:57 +0000207 // a number of verts to reserve and whether to perform a flush.
208 fMaxVertices = kMinRequestedVerts;
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000209 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
210 SK_ARRAY_COUNT(gTextVertexAttribs));
211 bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL);
tomhudson@google.com375ff852012-06-29 18:37:57 +0000212 if (flush) {
213 this->flushGlyphs();
214 fContext->flush();
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000215 fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>(
216 SK_ARRAY_COUNT(gTextVertexAttribs));
tomhudson@google.com375ff852012-06-29 18:37:57 +0000217 }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000218 fMaxVertices = kDefaultRequestedVerts;
219 // ignore return, no point in flushing again.
bsalomon@google.comf3a7bc72013-02-05 21:32:06 +0000220 fDrawTarget->geometryHints(&fMaxVertices, NULL);
tomhudson@google.com375ff852012-06-29 18:37:57 +0000221
222 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
223 if (fMaxVertices < kMinRequestedVerts) {
224 fMaxVertices = kDefaultRequestedVerts;
225 } else if (fMaxVertices > maxQuadVertices) {
226 // don't exceed the limit of the index buffer
227 fMaxVertices = maxQuadVertices;
228 }
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000229 bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices,
230 0,
231 GrTCast<void**>(&fVertices),
232 NULL);
tomhudson@google.com375ff852012-06-29 18:37:57 +0000233 GrAlwaysAssert(success);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000234 GrAssert(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize());
tomhudson@google.com375ff852012-06-29 18:37:57 +0000235 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
bsalomon@google.com81712882012-11-01 17:12:34 +0000237 GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX);
238 GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
bsalomon@google.com85983282013-02-07 22:00:29 +0000240 fVertices[2*fCurrVertex].setRectFan(SkFixedToFloat(vx),
241 SkFixedToFloat(vy),
242 SkFixedToFloat(vx + width),
243 SkFixedToFloat(vy + height),
244 2 * sizeof(SkPoint));
245 fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)),
246 SkFixedToFloat(texture->normalizeFixedY(ty)),
247 SkFixedToFloat(texture->normalizeFixedX(tx + width)),
248 SkFixedToFloat(texture->normalizeFixedY(ty + height)),
249 2 * sizeof(SkPoint));
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 fCurrVertex += 4;
251}