reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 10 | #include "GrTextContext.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 11 | #include "GrAtlas.h" |
| 12 | #include "GrContext.h" |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 13 | #include "GrDrawTarget.h" |
| 14 | #include "GrFontScaler.h" |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 15 | #include "GrIndexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | #include "GrTextStrike.h" |
| 17 | #include "GrTextStrike_impl.h" |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 18 | #include "SkPath.h" |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 19 | #include "SkStrokeRec.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 21 | static const int kGlyphCoordsAttributeIndex = 1; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 22 | |
| 23 | void GrTextContext::flushGlyphs() { |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 24 | if (NULL == fDrawTarget) { |
| 25 | return; |
| 26 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 27 | |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 28 | GrDrawState* drawState = fDrawTarget->drawState(); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 29 | GrDrawState::AutoRestoreEffects are(drawState); |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 30 | drawState->setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); |
| 31 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | if (fCurrVertex > 0) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | // setup our sampler state for our text texture/atlas |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | GrAssert(GrIsALIGN4(fCurrVertex)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | GrAssert(fCurrTexture); |
bsalomon@google.com | 0e354aa | 2012-10-08 20:44:25 +0000 | [diff] [blame] | 36 | GrTextureParams params(SkShader::kRepeat_TileMode, false); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 37 | |
| 38 | // This effect could be stored with one of the cache objects (atlas?) |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 39 | drawState->addCoverageEffect( |
| 40 | GrSimpleTextureEffect::CreateWithCustomCoords(fCurrTexture, params), |
| 41 | kGlyphCoordsAttributeIndex)->unref(); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 43 | if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 44 | if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
| 45 | kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 46 | fPaint.numColorStages()) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 47 | GrPrintf("LCD Text will not draw correctly.\n"); |
| 48 | } |
| 49 | // setup blend so that we get mask * paintColor + (1-mask)*dstColor |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 50 | drawState->setBlendConstant(fPaint.getColor()); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 51 | drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 52 | // don't modulate by the paint's color in the frag since we're |
| 53 | // already doing it via the blend const. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 54 | drawState->setColor(0xffffffff); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 55 | } else { |
| 56 | // set back to normal in case we took LCD path previously. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 57 | drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff()); |
| 58 | drawState->setColor(fPaint.getColor()); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 61 | int nGlyphs = fCurrVertex / 4; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 62 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 63 | fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 64 | nGlyphs, |
| 65 | 4, 6); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 66 | fDrawTarget->resetVertexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | fVertices = NULL; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 68 | fMaxVertices = 0; |
| 69 | fCurrVertex = 0; |
| 70 | GrSafeSetNull(fCurrTexture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
bsalomon@google.com | 0e354aa | 2012-10-08 20:44:25 +0000 | [diff] [blame] | 74 | GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint) : fPaint(paint) { |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 75 | fContext = context; |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 76 | fStrike = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 77 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 78 | fCurrTexture = NULL; |
| 79 | fCurrVertex = 0; |
| 80 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 81 | const GrClipData* clipData = context->getClip(); |
| 82 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 83 | SkRect devConservativeBound; |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 84 | clipData->fClipStack->getConservativeBounds( |
| 85 | -clipData->fOrigin.fX, |
| 86 | -clipData->fOrigin.fY, |
| 87 | context->getRenderTarget()->width(), |
| 88 | context->getRenderTarget()->height(), |
| 89 | &devConservativeBound); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 90 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 91 | devConservativeBound.roundOut(&fClipRect); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 92 | |
bsalomon@google.com | 858804d | 2012-10-15 14:25:50 +0000 | [diff] [blame] | 93 | fAutoMatrix.setIdentity(fContext, &fPaint); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 94 | |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 95 | fDrawTarget = fContext->getTextTarget(); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 96 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | fVertices = NULL; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 98 | fMaxVertices = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | } |
| 100 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 101 | GrTextContext::~GrTextContext() { |
| 102 | this->flushGlyphs(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 103 | } |
| 104 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 105 | void GrTextContext::flush() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | this->flushGlyphs(); |
| 107 | } |
| 108 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 109 | namespace { |
| 110 | |
| 111 | // position + texture coord |
| 112 | extern const GrVertexAttrib gTextVertexAttribs[] = { |
| 113 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 114 | {kVec2f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
| 115 | }; |
| 116 | |
| 117 | }; |
| 118 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 119 | void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 120 | GrFixed vx, GrFixed vy, |
| 121 | GrFontScaler* scaler) { |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 122 | if (NULL == fDrawTarget) { |
| 123 | return; |
| 124 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 134 | vx += SkIntToFixed(glyph->fBounds.fLeft); |
| 135 | vy += SkIntToFixed(glyph->fBounds.fTop); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 136 | |
| 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)) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 146 | // SkCLZ(3); // so we can set a break-point in the debugger |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (NULL == glyph->fAtlas) { |
| 152 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 153 | goto HAS_ATLAS; |
| 154 | } |
reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame] | 155 | |
| 156 | // before we purge the cache, we must flush any accumulated draws |
| 157 | this->flushGlyphs(); |
bsalomon@google.com | 193395c | 2012-03-30 17:35:12 +0000 | [diff] [blame] | 158 | fContext->flush(); |
reed@google.com | 313e403 | 2010-12-22 22:16:59 +0000 | [diff] [blame] | 159 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 160 | // try to purge |
| 161 | fContext->getFontCache()->purgeExceptFor(fStrike); |
| 162 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 163 | goto HAS_ATLAS; |
| 164 | } |
| 165 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 166 | if (NULL == glyph->fPath) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 167 | SkPath* path = SkNEW(SkPath); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 168 | 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.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 175 | |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 176 | GrContext::AutoMatrix am; |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 177 | SkMatrix translate; |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 178 | translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)), |
| 179 | SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop))); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 180 | GrPaint tmpPaint(fPaint); |
| 181 | am.setPreConcat(fContext, translate, &tmpPaint); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 182 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
| 183 | fContext->drawPath(tmpPaint, *glyph->fPath, stroke); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | HAS_ATLAS: |
| 188 | GrAssert(glyph->fAtlas); |
| 189 | |
bsalomon@google.com | 8598328 | 2013-02-07 22:00:29 +0000 | [diff] [blame] | 190 | // now promote them to fixed (TODO: Rethink using fixed pt). |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 191 | width = SkIntToFixed(width); |
| 192 | height = SkIntToFixed(height); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 193 | |
| 194 | GrTexture* texture = glyph->fAtlas->texture(); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 195 | GrAssert(texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 196 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 197 | if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { |
| 198 | this->flushGlyphs(); |
| 199 | fCurrTexture = texture; |
| 200 | fCurrTexture->ref(); |
| 201 | } |
| 202 | |
| 203 | if (NULL == fVertices) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 204 | // If we need to reserve vertices allow the draw target to suggest |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 205 | // a number of verts to reserve and whether to perform a flush. |
| 206 | fMaxVertices = kMinRequestedVerts; |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 207 | fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( |
| 208 | SK_ARRAY_COUNT(gTextVertexAttribs)); |
| 209 | bool flush = fDrawTarget->geometryHints(&fMaxVertices, NULL); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 210 | if (flush) { |
| 211 | this->flushGlyphs(); |
| 212 | fContext->flush(); |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 213 | fDrawTarget->drawState()->setVertexAttribs<gTextVertexAttribs>( |
| 214 | SK_ARRAY_COUNT(gTextVertexAttribs)); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 215 | } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 216 | fMaxVertices = kDefaultRequestedVerts; |
| 217 | // ignore return, no point in flushing again. |
bsalomon@google.com | f3a7bc7 | 2013-02-05 21:32:06 +0000 | [diff] [blame] | 218 | fDrawTarget->geometryHints(&fMaxVertices, NULL); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 219 | |
| 220 | int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads(); |
| 221 | if (fMaxVertices < kMinRequestedVerts) { |
| 222 | fMaxVertices = kDefaultRequestedVerts; |
| 223 | } else if (fMaxVertices > maxQuadVertices) { |
| 224 | // don't exceed the limit of the index buffer |
| 225 | fMaxVertices = maxQuadVertices; |
| 226 | } |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 227 | bool success = fDrawTarget->reserveVertexAndIndexSpace(fMaxVertices, |
| 228 | 0, |
| 229 | GrTCast<void**>(&fVertices), |
| 230 | NULL); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 231 | GrAlwaysAssert(success); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 232 | GrAssert(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize()); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 233 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 234 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 235 | GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); |
| 236 | GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 237 | |
bsalomon@google.com | 8598328 | 2013-02-07 22:00:29 +0000 | [diff] [blame] | 238 | fVertices[2*fCurrVertex].setRectFan(SkFixedToFloat(vx), |
| 239 | SkFixedToFloat(vy), |
| 240 | SkFixedToFloat(vx + width), |
| 241 | SkFixedToFloat(vy + height), |
| 242 | 2 * sizeof(SkPoint)); |
| 243 | fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)), |
| 244 | SkFixedToFloat(texture->normalizeFixedY(ty)), |
| 245 | SkFixedToFloat(texture->normalizeFixedX(tx + width)), |
| 246 | SkFixedToFloat(texture->normalizeFixedY(ty + height)), |
| 247 | 2 * sizeof(SkPoint)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 248 | fCurrVertex += 4; |
| 249 | } |