epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrTextContext.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 12 | #include "GrAtlas.h" |
| 13 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "GrTextStrike.h" |
| 15 | #include "GrTextStrike_impl.h" |
| 16 | #include "GrFontScaler.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 17 | #include "GrIndexBuffer.h" |
| 18 | #include "GrGpuVertex.h" |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 19 | #include "GrDrawTarget.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 21 | enum { |
| 22 | kGlyphMaskStage = GrPaint::kTotalStages, |
| 23 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | |
| 25 | void GrTextContext::flushGlyphs() { |
| 26 | if (fCurrVertex > 0) { |
| 27 | GrDrawTarget::AutoStateRestore asr(fDrawTarget); |
| 28 | |
| 29 | // setup our sampler state for our text texture/atlas |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 30 | GrSamplerState::Filter filter; |
| 31 | if (fExtMatrix.isIdentity()) { |
| 32 | filter = GrSamplerState::kNearest_Filter; |
| 33 | } else { |
| 34 | filter = GrSamplerState::kBilinear_Filter; |
| 35 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | GrSamplerState sampler(GrSamplerState::kRepeat_WrapMode, |
| 37 | GrSamplerState::kRepeat_WrapMode, |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 38 | filter); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 39 | fDrawTarget->setSamplerState(kGlyphMaskStage, sampler); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | |
| 41 | GrAssert(GrIsALIGN4(fCurrVertex)); |
| 42 | int nIndices = fCurrVertex + (fCurrVertex >> 1); |
| 43 | GrAssert(fCurrTexture); |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 44 | fDrawTarget->setTexture(kGlyphMaskStage, fCurrTexture); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 45 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 46 | if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 47 | if (kOne_BlendCoeff != fPaint.fSrcBlendCoeff || |
| 48 | kISA_BlendCoeff != fPaint.fDstBlendCoeff || |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 49 | fPaint.hasTexture()) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 50 | GrPrintf("LCD Text will not draw correctly.\n"); |
| 51 | } |
| 52 | // setup blend so that we get mask * paintColor + (1-mask)*dstColor |
| 53 | fDrawTarget->setBlendConstant(fPaint.fColor); |
| 54 | fDrawTarget->setBlendFunc(kConstC_BlendCoeff, kISC_BlendCoeff); |
| 55 | // don't modulate by the paint's color in the frag since we're |
| 56 | // already doing it via the blend const. |
| 57 | fDrawTarget->setColor(0xffffffff); |
| 58 | } else { |
| 59 | // set back to normal in case we took LCD path previously. |
| 60 | fDrawTarget->setBlendFunc(fPaint.fSrcBlendCoeff, fPaint.fDstBlendCoeff); |
| 61 | fDrawTarget->setColor(fPaint.fColor); |
| 62 | } |
| 63 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 64 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 66 | fDrawTarget->drawIndexed(kTriangles_PrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | 0, 0, fCurrVertex, nIndices); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 68 | fDrawTarget->resetVertexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | fVertices = NULL; |
| 70 | fMaxVertices = 0; |
| 71 | fCurrVertex = 0; |
| 72 | fCurrTexture->unref(); |
| 73 | fCurrTexture = NULL; |
| 74 | } |
| 75 | } |
| 76 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 77 | GrTextContext::GrTextContext(GrContext* context, |
| 78 | const GrPaint& paint, |
| 79 | const GrMatrix* extMatrix) : fPaint(paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | fContext = context; |
| 81 | fStrike = NULL; |
| 82 | |
| 83 | fCurrTexture = NULL; |
| 84 | fCurrVertex = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 85 | |
| 86 | if (NULL != extMatrix) { |
| 87 | fExtMatrix = *extMatrix; |
| 88 | } else { |
| 89 | fExtMatrix = GrMatrix::I(); |
| 90 | } |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 91 | if (context->getClip().hasConservativeBounds()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 92 | if (!fExtMatrix.isIdentity()) { |
| 93 | GrMatrix inverse; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 94 | GrRect r = context->getClip().getConservativeBounds(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 95 | if (fExtMatrix.invert(&inverse)) { |
| 96 | inverse.mapRect(&r); |
| 97 | r.roundOut(&fClipRect); |
| 98 | } |
| 99 | } else { |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 100 | context->getClip().getConservativeBounds().roundOut(&fClipRect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 101 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 102 | } else { |
| 103 | fClipRect.setLargest(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | } |
| 105 | |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 106 | // save the context's original matrix off and restore in destructor |
| 107 | // this must be done before getTextTarget. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 108 | fOrigViewMatrix = fContext->getMatrix(); |
| 109 | fContext->setMatrix(fExtMatrix); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 111 | /* |
| 112 | We need to call preConcatMatrix with our viewmatrix's inverse, for each |
| 113 | texture and mask in the paint. However, computing the inverse can be |
| 114 | expensive, and its possible we may not have any textures or masks, so these |
| 115 | two loops are written such that we only compute the inverse (once) if we |
| 116 | need it. We do this on our copy of the paint rather than directly on the |
| 117 | draw target because we re-provide the paint to the context when we have |
| 118 | to flush our glyphs or draw a glyph as a path midstream. |
| 119 | */ |
| 120 | bool invVMComputed = false; |
| 121 | GrMatrix invVM; |
| 122 | for (int t = 0; t < GrPaint::kMaxTextures; ++t) { |
| 123 | if (NULL != fPaint.getTexture(t)) { |
| 124 | if (invVMComputed || fOrigViewMatrix.invert(&invVM)) { |
| 125 | invVMComputed = true; |
| 126 | fPaint.getTextureSampler(t)->preConcatMatrix(invVM); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | for (int m = 0; m < GrPaint::kMaxMasks; ++m) { |
| 131 | if (NULL != fPaint.getMask(m)) { |
| 132 | if (invVMComputed || fOrigViewMatrix.invert(&invVM)) { |
| 133 | invVMComputed = true; |
| 134 | fPaint.getMaskSampler(m)->preConcatMatrix(invVM); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 139 | fDrawTarget = fContext->getTextTarget(fPaint); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 140 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 141 | fVertices = NULL; |
| 142 | fMaxVertices = 0; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 143 | |
| 144 | fVertexLayout = |
| 145 | GrDrawTarget::kTextFormat_VertexLayoutBit | |
| 146 | GrDrawTarget::StageTexCoordVertexLayoutBit(kGlyphMaskStage, 0); |
| 147 | |
| 148 | int stageMask = paint.getActiveStageMask(); |
| 149 | if (stageMask) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 150 | for (int i = 0; i < GrPaint::kTotalStages; ++i) { |
| 151 | if ((1 << i) & stageMask) { |
| 152 | fVertexLayout |= |
| 153 | GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i); |
| 154 | GrAssert(i != kGlyphMaskStage); |
| 155 | } |
| 156 | } |
| 157 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | GrTextContext::~GrTextContext() { |
| 161 | this->flushGlyphs(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 162 | fContext->setMatrix(fOrigViewMatrix); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void GrTextContext::flush() { |
| 166 | this->flushGlyphs(); |
| 167 | } |
| 168 | |
| 169 | static inline void setRectFan(GrGpuTextVertex v[4], int l, int t, int r, int b, |
| 170 | int stride) { |
| 171 | v[0 * stride].setI(l, t); |
| 172 | v[1 * stride].setI(l, b); |
| 173 | v[2 * stride].setI(r, b); |
| 174 | v[3 * stride].setI(r, t); |
| 175 | } |
| 176 | |
| 177 | void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
| 178 | GrFixed vx, GrFixed vy, |
| 179 | GrFontScaler* scaler) { |
| 180 | if (NULL == fStrike) { |
| 181 | fStrike = fContext->getFontCache()->getStrike(scaler); |
| 182 | } |
| 183 | |
| 184 | GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
| 185 | if (NULL == glyph || glyph->fBounds.isEmpty()) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | vx += GrIntToFixed(glyph->fBounds.fLeft); |
| 190 | vy += GrIntToFixed(glyph->fBounds.fTop); |
| 191 | |
| 192 | // keep them as ints until we've done the clip-test |
| 193 | GrFixed width = glyph->fBounds.width(); |
| 194 | GrFixed height = glyph->fBounds.height(); |
| 195 | |
| 196 | // check if we clipped out |
| 197 | if (true || NULL == glyph->fAtlas) { |
| 198 | int x = vx >> 16; |
| 199 | int y = vy >> 16; |
| 200 | if (fClipRect.quickReject(x, y, x + width, y + height)) { |
| 201 | // Gr_clz(3); // so we can set a break-point in the debugger |
| 202 | return; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (NULL == glyph->fAtlas) { |
| 207 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 208 | goto HAS_ATLAS; |
| 209 | } |
reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame] | 210 | |
| 211 | // before we purge the cache, we must flush any accumulated draws |
| 212 | this->flushGlyphs(); |
reed@google.com | 313e403 | 2010-12-22 22:16:59 +0000 | [diff] [blame] | 213 | fContext->flushText(); |
| 214 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 215 | // try to purge |
| 216 | fContext->getFontCache()->purgeExceptFor(fStrike); |
| 217 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 218 | goto HAS_ATLAS; |
| 219 | } |
| 220 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | if (NULL == glyph->fPath) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 222 | GrPath* path = new GrPath; |
| 223 | if (!scaler->getGlyphPath(glyph->glyphID(), path)) { |
| 224 | // flag the glyph as being dead? |
| 225 | delete path; |
| 226 | return; |
| 227 | } |
| 228 | glyph->fPath = path; |
| 229 | } |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 230 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | GrPoint translate; |
| 232 | translate.set(GrFixedToScalar(vx - GrIntToFixed(glyph->fBounds.fLeft)), |
| 233 | GrFixedToScalar(vy - GrIntToFixed(glyph->fBounds.fTop))); |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 234 | fContext->drawPath(fPaint, *glyph->fPath, kWinding_PathFill, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 235 | &translate); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
| 239 | HAS_ATLAS: |
| 240 | GrAssert(glyph->fAtlas); |
| 241 | |
| 242 | // now promote them to fixed |
| 243 | width = GrIntToFixed(width); |
| 244 | height = GrIntToFixed(height); |
| 245 | |
| 246 | GrTexture* texture = glyph->fAtlas->texture(); |
| 247 | GrAssert(texture); |
| 248 | |
| 249 | if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { |
| 250 | this->flushGlyphs(); |
| 251 | fCurrTexture = texture; |
| 252 | fCurrTexture->ref(); |
| 253 | } |
| 254 | |
| 255 | if (NULL == fVertices) { |
| 256 | // If we need to reserve vertices allow the draw target to suggest |
| 257 | // a number of verts to reserve and whether to perform a flush. |
| 258 | fMaxVertices = kMinRequestedVerts; |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 259 | bool flush = fDrawTarget->geometryHints(fVertexLayout, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 260 | &fMaxVertices, |
| 261 | NULL); |
| 262 | if (flush) { |
| 263 | this->flushGlyphs(); |
| 264 | fContext->flushText(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 265 | fDrawTarget = fContext->getTextTarget(fPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 266 | fMaxVertices = kDefaultRequestedVerts; |
| 267 | // ignore return, no point in flushing again. |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 268 | fDrawTarget->geometryHints(fVertexLayout, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 269 | &fMaxVertices, |
| 270 | NULL); |
| 271 | } |
| 272 | |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 273 | int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 274 | if (fMaxVertices < kMinRequestedVerts) { |
| 275 | fMaxVertices = kDefaultRequestedVerts; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 276 | } else if (fMaxVertices > maxQuadVertices) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 277 | // don't exceed the limit of the index buffer |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 278 | fMaxVertices = maxQuadVertices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 279 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 280 | bool success = fDrawTarget->reserveVertexSpace(fVertexLayout, |
| 281 | fMaxVertices, |
| 282 | GrTCast<void**>(&fVertices)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 283 | GrAlwaysAssert(success); |
| 284 | } |
| 285 | |
| 286 | GrFixed tx = GrIntToFixed(glyph->fAtlasLocation.fX); |
| 287 | GrFixed ty = GrIntToFixed(glyph->fAtlasLocation.fY); |
| 288 | |
| 289 | #if GR_GL_TEXT_TEXTURE_NORMALIZED |
| 290 | int x = vx >> 16; |
| 291 | int y = vy >> 16; |
| 292 | int w = width >> 16; |
| 293 | int h = height >> 16; |
| 294 | |
| 295 | setRectFan(&fVertices[2*fCurrVertex], x, y, x + w, y + h, 2); |
| 296 | setRectFan(&fVertices[2*fCurrVertex+1], |
| 297 | texture->normalizeFixedX(tx), |
| 298 | texture->normalizeFixedY(ty), |
| 299 | texture->normalizeFixedX(tx + width), |
| 300 | texture->normalizeFixedY(ty + height), |
| 301 | 2); |
| 302 | #else |
| 303 | fVertices[2*fCurrVertex].setXRectFan(vx, vy, vx + width, vy + height, |
| 304 | 2 * sizeof(GrGpuTextVertex)); |
| 305 | fVertices[2*fCurrVertex+1].setXRectFan(texture->normalizeFixedX(tx), |
| 306 | texture->normalizeFixedY(ty), |
| 307 | texture->normalizeFixedX(tx + width), |
| 308 | texture->normalizeFixedY(ty + height), |
| 309 | 2 * sizeof(GrGpuTextVertex)); |
| 310 | #endif |
| 311 | fCurrVertex += 4; |
| 312 | } |
| 313 | |
| 314 | |