reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | #include "GrTextContext.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 19 | #include "GrAtlas.h" |
| 20 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | #include "GrTextStrike.h" |
| 22 | #include "GrTextStrike_impl.h" |
| 23 | #include "GrFontScaler.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 24 | #include "GrIndexBuffer.h" |
| 25 | #include "GrGpuVertex.h" |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 26 | #include "GrDrawTarget.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 28 | static const int TEXT_STAGE = 1; |
| 29 | |
| 30 | static const GrVertexLayout BASE_VLAYOUT = |
| 31 | GrDrawTarget::kTextFormat_VertexLayoutBit | |
| 32 | GrDrawTarget::StageTexCoordVertexLayoutBit(TEXT_STAGE,0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | |
| 34 | void GrTextContext::flushGlyphs() { |
| 35 | if (fCurrVertex > 0) { |
| 36 | GrDrawTarget::AutoStateRestore asr(fDrawTarget); |
| 37 | |
| 38 | // setup our sampler state for our text texture/atlas |
| 39 | |
| 40 | GrSamplerState sampler(GrSamplerState::kRepeat_WrapMode, |
| 41 | GrSamplerState::kRepeat_WrapMode, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | !fExtMatrix.isIdentity()); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 43 | fDrawTarget->setSamplerState(TEXT_STAGE, sampler); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | |
| 45 | GrAssert(GrIsALIGN4(fCurrVertex)); |
| 46 | int nIndices = fCurrVertex + (fCurrVertex >> 1); |
| 47 | GrAssert(fCurrTexture); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 48 | fDrawTarget->setTexture(TEXT_STAGE, fCurrTexture); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 49 | |
| 50 | if (!GrTexture::PixelConfigIsAlphaOnly(fCurrTexture->config())) { |
| 51 | if (kOne_BlendCoeff != fPaint.fSrcBlendCoeff || |
| 52 | kISA_BlendCoeff != fPaint.fDstBlendCoeff || |
| 53 | NULL != fPaint.getTexture()) { |
| 54 | GrPrintf("LCD Text will not draw correctly.\n"); |
| 55 | } |
| 56 | // setup blend so that we get mask * paintColor + (1-mask)*dstColor |
| 57 | fDrawTarget->setBlendConstant(fPaint.fColor); |
| 58 | fDrawTarget->setBlendFunc(kConstC_BlendCoeff, kISC_BlendCoeff); |
| 59 | // don't modulate by the paint's color in the frag since we're |
| 60 | // already doing it via the blend const. |
| 61 | fDrawTarget->setColor(0xffffffff); |
| 62 | } else { |
| 63 | // set back to normal in case we took LCD path previously. |
| 64 | fDrawTarget->setBlendFunc(fPaint.fSrcBlendCoeff, fPaint.fDstBlendCoeff); |
| 65 | fDrawTarget->setColor(fPaint.fColor); |
| 66 | } |
| 67 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 68 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 70 | fDrawTarget->drawIndexed(kTriangles_PrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | 0, 0, fCurrVertex, nIndices); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 72 | fDrawTarget->releaseReservedGeometry(); |
| 73 | fVertices = NULL; |
| 74 | fMaxVertices = 0; |
| 75 | fCurrVertex = 0; |
| 76 | fCurrTexture->unref(); |
| 77 | fCurrTexture = NULL; |
| 78 | } |
| 79 | } |
| 80 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 81 | GrTextContext::GrTextContext(GrContext* context, |
| 82 | const GrPaint& paint, |
| 83 | const GrMatrix* extMatrix) : fPaint(paint) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | fContext = context; |
| 85 | fStrike = NULL; |
| 86 | |
| 87 | fCurrTexture = NULL; |
| 88 | fCurrVertex = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | |
| 90 | if (NULL != extMatrix) { |
| 91 | fExtMatrix = *extMatrix; |
| 92 | } else { |
| 93 | fExtMatrix = GrMatrix::I(); |
| 94 | } |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 95 | if (context->getClip().hasConservativeBounds()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 96 | if (!fExtMatrix.isIdentity()) { |
| 97 | GrMatrix inverse; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 98 | GrRect r = context->getClip().getConservativeBounds(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 99 | if (fExtMatrix.invert(&inverse)) { |
| 100 | inverse.mapRect(&r); |
| 101 | r.roundOut(&fClipRect); |
| 102 | } |
| 103 | } else { |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 104 | context->getClip().getConservativeBounds().roundOut(&fClipRect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 106 | } else { |
| 107 | fClipRect.setLargest(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | } |
| 109 | |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 110 | // save the context's original matrix off and restore in destructor |
| 111 | // this must be done before getTextTarget. |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 112 | fOrigViewMatrix = fContext->getMatrix(); |
| 113 | fContext->setMatrix(fExtMatrix); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 115 | fVertexLayout = BASE_VLAYOUT; |
| 116 | if (NULL != paint.getTexture()) { |
| 117 | fVertexLayout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0); |
| 118 | GrMatrix inverseViewMatrix; |
| 119 | if (fOrigViewMatrix.invert(&inverseViewMatrix)) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 120 | fPaint.fSampler.preConcatMatrix(inverseViewMatrix); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | fVertices = NULL; |
| 125 | fMaxVertices = 0; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 126 | fDrawTarget = fContext->getTextTarget(fPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | GrTextContext::~GrTextContext() { |
| 130 | this->flushGlyphs(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 131 | fContext->setMatrix(fOrigViewMatrix); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void GrTextContext::flush() { |
| 135 | this->flushGlyphs(); |
| 136 | } |
| 137 | |
| 138 | static inline void setRectFan(GrGpuTextVertex v[4], int l, int t, int r, int b, |
| 139 | int stride) { |
| 140 | v[0 * stride].setI(l, t); |
| 141 | v[1 * stride].setI(l, b); |
| 142 | v[2 * stride].setI(r, b); |
| 143 | v[3 * stride].setI(r, t); |
| 144 | } |
| 145 | |
| 146 | void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
| 147 | GrFixed vx, GrFixed vy, |
| 148 | GrFontScaler* scaler) { |
| 149 | if (NULL == fStrike) { |
| 150 | fStrike = fContext->getFontCache()->getStrike(scaler); |
| 151 | } |
| 152 | |
| 153 | GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
| 154 | if (NULL == glyph || glyph->fBounds.isEmpty()) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | vx += GrIntToFixed(glyph->fBounds.fLeft); |
| 159 | vy += GrIntToFixed(glyph->fBounds.fTop); |
| 160 | |
| 161 | // keep them as ints until we've done the clip-test |
| 162 | GrFixed width = glyph->fBounds.width(); |
| 163 | GrFixed height = glyph->fBounds.height(); |
| 164 | |
| 165 | // check if we clipped out |
| 166 | if (true || NULL == glyph->fAtlas) { |
| 167 | int x = vx >> 16; |
| 168 | int y = vy >> 16; |
| 169 | if (fClipRect.quickReject(x, y, x + width, y + height)) { |
| 170 | // Gr_clz(3); // so we can set a break-point in the debugger |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (NULL == glyph->fAtlas) { |
| 176 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 177 | goto HAS_ATLAS; |
| 178 | } |
reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame^] | 179 | |
| 180 | // before we purge the cache, we must flush any accumulated draws |
| 181 | this->flushGlyphs(); |
reed@google.com | 313e403 | 2010-12-22 22:16:59 +0000 | [diff] [blame] | 182 | fContext->flushText(); |
| 183 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 184 | // try to purge |
| 185 | fContext->getFontCache()->purgeExceptFor(fStrike); |
| 186 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 187 | goto HAS_ATLAS; |
| 188 | } |
| 189 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 190 | if (NULL == glyph->fPath) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 191 | GrPath* path = new GrPath; |
| 192 | if (!scaler->getGlyphPath(glyph->glyphID(), path)) { |
| 193 | // flag the glyph as being dead? |
| 194 | delete path; |
| 195 | return; |
| 196 | } |
| 197 | glyph->fPath = path; |
| 198 | } |
| 199 | GrPath::Iter iter(*glyph->fPath); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | GrPoint translate; |
| 201 | translate.set(GrFixedToScalar(vx - GrIntToFixed(glyph->fBounds.fLeft)), |
| 202 | GrFixedToScalar(vy - GrIntToFixed(glyph->fBounds.fTop))); |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 203 | fContext->drawPath(fPaint, &iter, kWinding_PathFill, |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 204 | &translate); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
| 208 | HAS_ATLAS: |
| 209 | GrAssert(glyph->fAtlas); |
| 210 | |
| 211 | // now promote them to fixed |
| 212 | width = GrIntToFixed(width); |
| 213 | height = GrIntToFixed(height); |
| 214 | |
| 215 | GrTexture* texture = glyph->fAtlas->texture(); |
| 216 | GrAssert(texture); |
| 217 | |
| 218 | if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { |
| 219 | this->flushGlyphs(); |
| 220 | fCurrTexture = texture; |
| 221 | fCurrTexture->ref(); |
| 222 | } |
| 223 | |
| 224 | if (NULL == fVertices) { |
| 225 | // If we need to reserve vertices allow the draw target to suggest |
| 226 | // a number of verts to reserve and whether to perform a flush. |
| 227 | fMaxVertices = kMinRequestedVerts; |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 228 | bool flush = fDrawTarget->geometryHints(fVertexLayout, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 229 | &fMaxVertices, |
| 230 | NULL); |
| 231 | if (flush) { |
| 232 | this->flushGlyphs(); |
| 233 | fContext->flushText(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 234 | fDrawTarget = fContext->getTextTarget(fPaint); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 235 | fMaxVertices = kDefaultRequestedVerts; |
| 236 | // ignore return, no point in flushing again. |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 237 | fDrawTarget->geometryHints(fVertexLayout, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 238 | &fMaxVertices, |
| 239 | NULL); |
| 240 | } |
| 241 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 242 | int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->size() / (6 * sizeof(uint16_t)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 243 | if (fMaxVertices < kMinRequestedVerts) { |
| 244 | fMaxVertices = kDefaultRequestedVerts; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 245 | } else if (fMaxVertices > maxQuadVertices) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 246 | // don't exceed the limit of the index buffer |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 247 | fMaxVertices = maxQuadVertices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 248 | } |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 249 | bool success = fDrawTarget->reserveAndLockGeometry(fVertexLayout, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 250 | fMaxVertices, 0, |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 251 | GrTCast<void**>(&fVertices), |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 252 | NULL); |
| 253 | GrAlwaysAssert(success); |
| 254 | } |
| 255 | |
| 256 | GrFixed tx = GrIntToFixed(glyph->fAtlasLocation.fX); |
| 257 | GrFixed ty = GrIntToFixed(glyph->fAtlasLocation.fY); |
| 258 | |
| 259 | #if GR_GL_TEXT_TEXTURE_NORMALIZED |
| 260 | int x = vx >> 16; |
| 261 | int y = vy >> 16; |
| 262 | int w = width >> 16; |
| 263 | int h = height >> 16; |
| 264 | |
| 265 | setRectFan(&fVertices[2*fCurrVertex], x, y, x + w, y + h, 2); |
| 266 | setRectFan(&fVertices[2*fCurrVertex+1], |
| 267 | texture->normalizeFixedX(tx), |
| 268 | texture->normalizeFixedY(ty), |
| 269 | texture->normalizeFixedX(tx + width), |
| 270 | texture->normalizeFixedY(ty + height), |
| 271 | 2); |
| 272 | #else |
| 273 | fVertices[2*fCurrVertex].setXRectFan(vx, vy, vx + width, vy + height, |
| 274 | 2 * sizeof(GrGpuTextVertex)); |
| 275 | fVertices[2*fCurrVertex+1].setXRectFan(texture->normalizeFixedX(tx), |
| 276 | texture->normalizeFixedY(ty), |
| 277 | texture->normalizeFixedX(tx + width), |
| 278 | texture->normalizeFixedY(ty + height), |
| 279 | 2 * sizeof(GrGpuTextVertex)); |
| 280 | #endif |
| 281 | fCurrVertex += 4; |
| 282 | } |
| 283 | |
| 284 | |