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 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 11 | #include "GrAtlas.h" |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 12 | #include "GrDefaultTextContext.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 13 | #include "GrContext.h" |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 14 | #include "GrDrawTarget.h" |
| 15 | #include "GrFontScaler.h" |
| 16 | #include "GrGpuVertex.h" |
| 17 | #include "GrTemplates.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | #include "GrTextStrike.h" |
| 19 | #include "GrTextStrike_impl.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 21 | void GrDefaultTextContext::flushGlyphs() { |
| 22 | GrAssert(this->isValid()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | if (fCurrVertex > 0) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 24 | GrDrawState* drawState = fDrawTarget->drawState(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | // setup our sampler state for our text texture/atlas |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 26 | GrSamplerState::Filter filter; |
| 27 | if (fExtMatrix.isIdentity()) { |
| 28 | filter = GrSamplerState::kNearest_Filter; |
| 29 | } else { |
| 30 | filter = GrSamplerState::kBilinear_Filter; |
| 31 | } |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 32 | drawState->sampler(kGlyphMaskStage)->reset( |
| 33 | GrSamplerState::kRepeat_WrapMode,filter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | |
| 35 | GrAssert(GrIsALIGN4(fCurrVertex)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | GrAssert(fCurrTexture); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 37 | drawState->setTexture(kGlyphMaskStage, fCurrTexture); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 38 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 39 | if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 40 | if (kOne_BlendCoeff != fGrPaint.fSrcBlendCoeff || |
| 41 | kISA_BlendCoeff != fGrPaint.fDstBlendCoeff || |
| 42 | fGrPaint.hasTexture()) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 43 | GrPrintf("LCD Text will not draw correctly.\n"); |
| 44 | } |
| 45 | // setup blend so that we get mask * paintColor + (1-mask)*dstColor |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 46 | drawState->setBlendConstant(fGrPaint.fColor); |
bsalomon@google.com | 6b67e21 | 2011-12-09 16:10:24 +0000 | [diff] [blame] | 47 | drawState->setBlendFunc(kConstC_BlendCoeff, kISC_BlendCoeff); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 48 | // don't modulate by the paint's color in the frag since we're |
| 49 | // already doing it via the blend const. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 50 | drawState->setColor(0xffffffff); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 51 | } else { |
| 52 | // set back to normal in case we took LCD path previously. |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 53 | drawState->setBlendFunc(fGrPaint.fSrcBlendCoeff, fGrPaint.fDstBlendCoeff); |
| 54 | drawState->setColor(fGrPaint.fColor); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 55 | } |
| 56 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 57 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame^] | 58 | int nGlyphs = fCurrVertex / 4; |
| 59 | fDrawTarget->drawIndexedInstances(kTriangles_PrimitiveType, |
| 60 | nGlyphs, |
| 61 | 4, 6); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | fVertices = NULL; |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 63 | this->INHERITED::reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 67 | GrDefaultTextContext::GrDefaultTextContext() { |
| 68 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 70 | GrDefaultTextContext::~GrDefaultTextContext() { |
| 71 | } |
| 72 | |
| 73 | void GrDefaultTextContext::init(GrContext* context, |
| 74 | const GrPaint& paint, |
| 75 | const GrMatrix* extMatrix) { |
| 76 | this->INHERITED::init(context, paint, extMatrix); |
| 77 | |
| 78 | fStrike = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | |
| 80 | if (NULL != extMatrix) { |
| 81 | fExtMatrix = *extMatrix; |
| 82 | } else { |
bsalomon@google.com | 022a3e1 | 2012-03-16 15:41:40 +0000 | [diff] [blame] | 83 | fExtMatrix.reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | } |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 85 | if (context->getClip().hasConservativeBounds()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 86 | if (!fExtMatrix.isIdentity()) { |
| 87 | GrMatrix inverse; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 88 | GrRect r = context->getClip().getConservativeBounds(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 89 | if (fExtMatrix.invert(&inverse)) { |
| 90 | inverse.mapRect(&r); |
| 91 | r.roundOut(&fClipRect); |
| 92 | } |
| 93 | } else { |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 94 | context->getClip().getConservativeBounds().roundOut(&fClipRect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 96 | } else { |
| 97 | fClipRect.setLargest(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | } |
| 99 | |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 100 | // save the context's original matrix off and restore in destructor |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 101 | // getTextTarget should be called after that |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 102 | fOrigViewMatrix = fContext->getMatrix(); |
| 103 | fContext->setMatrix(fExtMatrix); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 105 | /* |
| 106 | We need to call preConcatMatrix with our viewmatrix's inverse, for each |
| 107 | texture and mask in the paint. However, computing the inverse can be |
| 108 | expensive, and its possible we may not have any textures or masks, so these |
| 109 | two loops are written such that we only compute the inverse (once) if we |
| 110 | need it. We do this on our copy of the paint rather than directly on the |
| 111 | draw target because we re-provide the paint to the context when we have |
| 112 | to flush our glyphs or draw a glyph as a path midstream. |
| 113 | */ |
| 114 | bool invVMComputed = false; |
| 115 | GrMatrix invVM; |
| 116 | for (int t = 0; t < GrPaint::kMaxTextures; ++t) { |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 117 | if (NULL != fGrPaint.getTexture(t)) { |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 118 | if (invVMComputed || fOrigViewMatrix.invert(&invVM)) { |
| 119 | invVMComputed = true; |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 120 | fGrPaint.textureSampler(t)->preConcatMatrix(invVM); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | for (int m = 0; m < GrPaint::kMaxMasks; ++m) { |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 125 | if (NULL != fGrPaint.getMask(m)) { |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 126 | if (invVMComputed || fOrigViewMatrix.invert(&invVM)) { |
| 127 | invVMComputed = true; |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 128 | fGrPaint.maskSampler(m)->preConcatMatrix(invVM); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 133 | // this has been already done in the baseclass, but we need to repeat |
| 134 | // due to new matrix |
| 135 | fDrawTarget = fContext->getTextTarget(fGrPaint); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 136 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | fVertices = NULL; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 138 | |
| 139 | fVertexLayout = |
| 140 | GrDrawTarget::kTextFormat_VertexLayoutBit | |
| 141 | GrDrawTarget::StageTexCoordVertexLayoutBit(kGlyphMaskStage, 0); |
| 142 | |
| 143 | int stageMask = paint.getActiveStageMask(); |
| 144 | if (stageMask) { |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 145 | for (int i = 0; i < GrPaint::kTotalStages; ++i) { |
| 146 | if ((1 << i) & stageMask) { |
| 147 | fVertexLayout |= |
| 148 | GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i); |
| 149 | GrAssert(i != kGlyphMaskStage); |
| 150 | } |
| 151 | } |
| 152 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | } |
| 154 | |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 155 | void GrDefaultTextContext::finish() { |
| 156 | this->flush(); |
| 157 | |
| 158 | fStrike = NULL; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 159 | fContext->setMatrix(fOrigViewMatrix); |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 160 | |
| 161 | this->INHERITED::finish(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | } |
| 163 | |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 164 | void GrDefaultTextContext::flush() { |
| 165 | GrAssert(this->isValid()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 177 | void GrDefaultTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | GrFixed vx, GrFixed vy, |
| 179 | GrFontScaler* scaler) { |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 180 | GrAssert(this->isValid()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | if (NULL == fStrike) { |
| 182 | fStrike = fContext->getFontCache()->getStrike(scaler); |
| 183 | } |
| 184 | |
| 185 | GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
| 186 | if (NULL == glyph || glyph->fBounds.isEmpty()) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | vx += GrIntToFixed(glyph->fBounds.fLeft); |
| 191 | vy += GrIntToFixed(glyph->fBounds.fTop); |
| 192 | |
| 193 | // keep them as ints until we've done the clip-test |
| 194 | GrFixed width = glyph->fBounds.width(); |
| 195 | GrFixed height = glyph->fBounds.height(); |
| 196 | |
| 197 | // check if we clipped out |
| 198 | if (true || NULL == glyph->fAtlas) { |
| 199 | int x = vx >> 16; |
| 200 | int y = vy >> 16; |
| 201 | if (fClipRect.quickReject(x, y, x + width, y + height)) { |
| 202 | // Gr_clz(3); // so we can set a break-point in the debugger |
| 203 | return; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if (NULL == glyph->fAtlas) { |
| 208 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 209 | goto HAS_ATLAS; |
| 210 | } |
reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame] | 211 | |
| 212 | // before we purge the cache, we must flush any accumulated draws |
| 213 | this->flushGlyphs(); |
reed@google.com | 313e403 | 2010-12-22 22:16:59 +0000 | [diff] [blame] | 214 | fContext->flushText(); |
| 215 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 216 | // try to purge |
| 217 | fContext->getFontCache()->purgeExceptFor(fStrike); |
| 218 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 219 | goto HAS_ATLAS; |
| 220 | } |
| 221 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 222 | if (NULL == glyph->fPath) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | GrPath* path = new GrPath; |
| 224 | if (!scaler->getGlyphPath(glyph->glyphID(), path)) { |
| 225 | // flag the glyph as being dead? |
| 226 | delete path; |
| 227 | return; |
| 228 | } |
| 229 | glyph->fPath = path; |
| 230 | } |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 231 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 232 | GrPoint translate; |
| 233 | translate.set(GrFixedToScalar(vx - GrIntToFixed(glyph->fBounds.fLeft)), |
| 234 | GrFixedToScalar(vy - GrIntToFixed(glyph->fBounds.fTop))); |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 235 | fContext->drawPath(fGrPaint, *glyph->fPath, kWinding_PathFill, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 236 | &translate); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | |
| 240 | HAS_ATLAS: |
| 241 | GrAssert(glyph->fAtlas); |
| 242 | |
| 243 | // now promote them to fixed |
| 244 | width = GrIntToFixed(width); |
| 245 | height = GrIntToFixed(height); |
| 246 | |
| 247 | GrTexture* texture = glyph->fAtlas->texture(); |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 248 | this->prepareForGlyph(texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 249 | |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 250 | this->setupVertexBuff(GrTCast<void**>(&fVertices), |
| 251 | fVertexLayout); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 252 | |
| 253 | GrFixed tx = GrIntToFixed(glyph->fAtlasLocation.fX); |
| 254 | GrFixed ty = GrIntToFixed(glyph->fAtlasLocation.fY); |
| 255 | |
| 256 | #if GR_GL_TEXT_TEXTURE_NORMALIZED |
| 257 | int x = vx >> 16; |
| 258 | int y = vy >> 16; |
| 259 | int w = width >> 16; |
| 260 | int h = height >> 16; |
| 261 | |
| 262 | setRectFan(&fVertices[2*fCurrVertex], x, y, x + w, y + h, 2); |
| 263 | setRectFan(&fVertices[2*fCurrVertex+1], |
| 264 | texture->normalizeFixedX(tx), |
| 265 | texture->normalizeFixedY(ty), |
| 266 | texture->normalizeFixedX(tx + width), |
| 267 | texture->normalizeFixedY(ty + height), |
| 268 | 2); |
| 269 | #else |
| 270 | fVertices[2*fCurrVertex].setXRectFan(vx, vy, vx + width, vy + height, |
| 271 | 2 * sizeof(GrGpuTextVertex)); |
| 272 | fVertices[2*fCurrVertex+1].setXRectFan(texture->normalizeFixedX(tx), |
| 273 | texture->normalizeFixedY(ty), |
| 274 | texture->normalizeFixedX(tx + width), |
| 275 | texture->normalizeFixedY(ty + height), |
| 276 | 2 * sizeof(GrGpuTextVertex)); |
| 277 | #endif |
| 278 | fCurrVertex += 4; |
| 279 | } |