blob: bf7ed044d770b0df76c27ffcdbe7966d7ac10c32 [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"
15#include "GrGpuVertex.h"
tomhudson@google.com375ff852012-06-29 18:37:57 +000016#include "GrIndexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "GrTextStrike.h"
18#include "GrTextStrike_impl.h"
tomhudson@google.com375ff852012-06-29 18:37:57 +000019#include "SkPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
tomhudson@google.com375ff852012-06-29 18:37:57 +000021enum {
22 kGlyphMaskStage = GrPaint::kTotalStages,
23};
24
25void GrTextContext::flushGlyphs() {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000026 if (NULL == fDrawTarget) {
27 return;
28 }
29 GrDrawState* drawState = fDrawTarget->drawState();
reed@google.comac10a2d2010-12-22 21:39:39 +000030 if (fCurrVertex > 0) {
reed@google.comac10a2d2010-12-22 21:39:39 +000031 // setup our sampler state for our text texture/atlas
bsalomon@google.com08283af2012-10-26 13:01:20 +000032 drawState->stage(kGlyphMaskStage)->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000033
34 GrAssert(GrIsALIGN4(fCurrVertex));
reed@google.comac10a2d2010-12-22 21:39:39 +000035 GrAssert(fCurrTexture);
bsalomon@google.com0e354aa2012-10-08 20:44:25 +000036 GrTextureParams params(SkShader::kRepeat_TileMode, false);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000037 drawState->createTextureEffect(kGlyphMaskStage, fCurrTexture, GrMatrix::I(), params);
bsalomon@google.com080773c2011-03-15 19:09:25 +000038
bsalomon@google.com669fdc42011-04-05 17:08:27 +000039 if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000040 if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() ||
41 kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() ||
bsalomon@google.com88becf42012-10-05 14:54:42 +000042 fPaint.hasColorStage()) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000043 GrPrintf("LCD Text will not draw correctly.\n");
44 }
45 // setup blend so that we get mask * paintColor + (1-mask)*dstColor
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000046 drawState->setBlendConstant(fPaint.getColor());
bsalomon@google.com47059542012-06-06 20:51:20 +000047 drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +000048 // don't modulate by the paint's color in the frag since we're
49 // already doing it via the blend const.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000050 drawState->setColor(0xffffffff);
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 } else {
52 // set back to normal in case we took LCD path previously.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000053 drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff());
54 drawState->setColor(fPaint.getColor());
bsalomon@google.com080773c2011-03-15 19:09:25 +000055 }
56
bsalomon@google.com934c5702012-03-20 21:17:58 +000057 int nGlyphs = fCurrVertex / 4;
tomhudson@google.com375ff852012-06-29 18:37:57 +000058 fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
bsalomon@google.com47059542012-06-06 20:51:20 +000059 fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType,
bsalomon@google.com934c5702012-03-20 21:17:58 +000060 nGlyphs,
61 4, 6);
tomhudson@google.com375ff852012-06-29 18:37:57 +000062 fDrawTarget->resetVertexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +000063 fVertices = NULL;
tomhudson@google.com375ff852012-06-29 18:37:57 +000064 fMaxVertices = 0;
65 fCurrVertex = 0;
66 GrSafeSetNull(fCurrTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +000067 }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000068 drawState->disableStages();
69 fDrawTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000070}
71
bsalomon@google.com0e354aa2012-10-08 20:44:25 +000072GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint) : fPaint(paint) {
tomhudson@google.com375ff852012-06-29 18:37:57 +000073 fContext = context;
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000074 fStrike = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000075
tomhudson@google.com375ff852012-06-29 18:37:57 +000076 fCurrTexture = NULL;
77 fCurrVertex = 0;
78
robertphillips@google.combeb1af72012-07-26 18:52:16 +000079 const GrClipData* clipData = context->getClip();
80
robertphillips@google.com641f8b12012-07-31 19:15:58 +000081 GrRect devConservativeBound;
82 clipData->fClipStack->getConservativeBounds(
83 -clipData->fOrigin.fX,
84 -clipData->fOrigin.fY,
85 context->getRenderTarget()->width(),
86 context->getRenderTarget()->height(),
87 &devConservativeBound);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000088
robertphillips@google.com641f8b12012-07-31 19:15:58 +000089 devConservativeBound.roundOut(&fClipRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +000090
bsalomon@google.com858804d2012-10-15 14:25:50 +000091 fAutoMatrix.setIdentity(fContext, &fPaint);
bsalomon@google.com39149582011-06-13 21:55:32 +000092
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000093 fDrawTarget = NULL;
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +000094
reed@google.comac10a2d2010-12-22 21:39:39 +000095 fVertices = NULL;
tomhudson@google.com375ff852012-06-29 18:37:57 +000096 fMaxVertices = 0;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000097
rmistry@google.comd6176b02012-08-23 18:14:13 +000098 fVertexLayout =
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000099 GrDrawTarget::kTextFormat_VertexLayoutBit |
100 GrDrawTarget::StageTexCoordVertexLayoutBit(kGlyphMaskStage, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000101}
102
tomhudson@google.com375ff852012-06-29 18:37:57 +0000103GrTextContext::~GrTextContext() {
104 this->flushGlyphs();
105 if (fDrawTarget) {
106 fDrawTarget->drawState()->disableStages();
107 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000108}
109
tomhudson@google.com375ff852012-06-29 18:37:57 +0000110void GrTextContext::flush() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 this->flushGlyphs();
112}
113
114static inline void setRectFan(GrGpuTextVertex v[4], int l, int t, int r, int b,
115 int stride) {
116 v[0 * stride].setI(l, t);
117 v[1 * stride].setI(l, b);
118 v[2 * stride].setI(r, b);
119 v[3 * stride].setI(r, t);
120}
121
tomhudson@google.com375ff852012-06-29 18:37:57 +0000122void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed,
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 GrFixed vx, GrFixed vy,
124 GrFontScaler* scaler) {
125 if (NULL == fStrike) {
126 fStrike = fContext->getFontCache()->getStrike(scaler);
127 }
128
129 GrGlyph* glyph = fStrike->getGlyph(packed, scaler);
130 if (NULL == glyph || glyph->fBounds.isEmpty()) {
131 return;
132 }
133
bsalomon@google.com81712882012-11-01 17:12:34 +0000134 vx += SkIntToFixed(glyph->fBounds.fLeft);
135 vy += SkIntToFixed(glyph->fBounds.fTop);
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
137 // keep them as ints until we've done the clip-test
138 GrFixed width = glyph->fBounds.width();
139 GrFixed height = glyph->fBounds.height();
140
141 // check if we clipped out
142 if (true || NULL == glyph->fAtlas) {
143 int x = vx >> 16;
144 int y = vy >> 16;
145 if (fClipRect.quickReject(x, y, x + width, y + height)) {
146// Gr_clz(3); // so we can set a break-point in the debugger
147 return;
148 }
149 }
150
151 if (NULL == glyph->fAtlas) {
152 if (fStrike->getGlyphAtlas(glyph, scaler)) {
153 goto HAS_ATLAS;
154 }
reed@google.com0ebe81a2011-04-04 20:06:59 +0000155
156 // before we purge the cache, we must flush any accumulated draws
157 this->flushGlyphs();
bsalomon@google.com193395c2012-03-30 17:35:12 +0000158 fContext->flush();
reed@google.com313e4032010-12-22 22:16:59 +0000159
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 // try to purge
161 fContext->getFontCache()->purgeExceptFor(fStrike);
162 if (fStrike->getGlyphAtlas(glyph, scaler)) {
163 goto HAS_ATLAS;
164 }
165
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 if (NULL == glyph->fPath) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000167 SkPath* path = SkNEW(SkPath);
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
169 // flag the glyph as being dead?
170 delete path;
171 return;
172 }
173 glyph->fPath = path;
174 }
reed@google.com07f3ee12011-05-16 17:21:57 +0000175
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000176 GrContext::AutoMatrix am;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000177 GrMatrix translate;
bsalomon@google.com81712882012-11-01 17:12:34 +0000178 translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)),
179 SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop)));
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000180 GrPaint tmpPaint(fPaint);
181 am.setPreConcat(fContext, translate, &tmpPaint);
182 fContext->drawPath(tmpPaint, *glyph->fPath, kWinding_GrPathFill);
reed@google.comac10a2d2010-12-22 21:39:39 +0000183 return;
184 }
185
186HAS_ATLAS:
187 GrAssert(glyph->fAtlas);
188
189 // now promote them to fixed
bsalomon@google.com81712882012-11-01 17:12:34 +0000190 width = SkIntToFixed(width);
191 height = SkIntToFixed(height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000192
193 GrTexture* texture = glyph->fAtlas->texture();
tomhudson@google.com375ff852012-06-29 18:37:57 +0000194 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000195
tomhudson@google.com375ff852012-06-29 18:37:57 +0000196 if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) {
197 this->flushGlyphs();
198 fCurrTexture = texture;
199 fCurrTexture->ref();
200 }
201
202 if (NULL == fVertices) {
203 // If we need to reserve vertices allow the draw target to suggest
204 // a number of verts to reserve and whether to perform a flush.
205 fMaxVertices = kMinRequestedVerts;
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000206 bool flush = (NULL != fDrawTarget) &&
207 fDrawTarget->geometryHints(fVertexLayout,
tomhudson@google.com375ff852012-06-29 18:37:57 +0000208 &fMaxVertices,
209 NULL);
210 if (flush) {
211 this->flushGlyphs();
212 fContext->flush();
tomhudson@google.com375ff852012-06-29 18:37:57 +0000213 }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000214 fDrawTarget = fContext->getTextTarget(fPaint);
215 fMaxVertices = kDefaultRequestedVerts;
216 // ignore return, no point in flushing again.
217 fDrawTarget->geometryHints(fVertexLayout,
218 &fMaxVertices,
219 NULL);
tomhudson@google.com375ff852012-06-29 18:37:57 +0000220
221 int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads();
222 if (fMaxVertices < kMinRequestedVerts) {
223 fMaxVertices = kDefaultRequestedVerts;
224 } else if (fMaxVertices > maxQuadVertices) {
225 // don't exceed the limit of the index buffer
226 fMaxVertices = maxQuadVertices;
227 }
228 bool success = fDrawTarget->reserveVertexAndIndexSpace(
rmistry@google.comd6176b02012-08-23 18:14:13 +0000229 fVertexLayout,
tomhudson@google.com375ff852012-06-29 18:37:57 +0000230 fMaxVertices,
231 0,
232 GrTCast<void**>(&fVertices),
233 NULL);
234 GrAlwaysAssert(success);
235 }
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.com2717d562012-05-07 19:10:52 +0000240#if GR_TEXT_SCALAR_IS_USHORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 int x = vx >> 16;
242 int y = vy >> 16;
243 int w = width >> 16;
244 int h = height >> 16;
245
246 setRectFan(&fVertices[2*fCurrVertex], x, y, x + w, y + h, 2);
247 setRectFan(&fVertices[2*fCurrVertex+1],
248 texture->normalizeFixedX(tx),
249 texture->normalizeFixedY(ty),
250 texture->normalizeFixedX(tx + width),
251 texture->normalizeFixedY(ty + height),
252 2);
253#else
254 fVertices[2*fCurrVertex].setXRectFan(vx, vy, vx + width, vy + height,
255 2 * sizeof(GrGpuTextVertex));
256 fVertices[2*fCurrVertex+1].setXRectFan(texture->normalizeFixedX(tx),
257 texture->normalizeFixedY(ty),
258 texture->normalizeFixedX(tx + width),
259 texture->normalizeFixedY(ty + height),
260 2 * sizeof(GrGpuTextVertex));
261#endif
262 fCurrVertex += 4;
263}
tomhudson@google.com375ff852012-06-29 18:37:57 +0000264