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 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 21 | enum { |
| 22 | kGlyphMaskStage = GrPaint::kTotalStages, |
| 23 | }; |
| 24 | |
| 25 | void GrTextContext::flushGlyphs() { |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 26 | if (NULL == fDrawTarget) { |
| 27 | return; |
| 28 | } |
| 29 | GrDrawState* drawState = fDrawTarget->drawState(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 30 | if (fCurrVertex > 0) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | // setup our sampler state for our text texture/atlas |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | GrAssert(GrIsALIGN4(fCurrVertex)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | GrAssert(fCurrTexture); |
bsalomon@google.com | 0e354aa | 2012-10-08 20:44:25 +0000 | [diff] [blame] | 34 | GrTextureParams params(SkShader::kRepeat_TileMode, false); |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 35 | drawState->createTextureEffect(kGlyphMaskStage, fCurrTexture, SkMatrix::I(), params); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 37 | if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 38 | if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
| 39 | kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
bsalomon@google.com | 88becf4 | 2012-10-05 14:54:42 +0000 | [diff] [blame] | 40 | fPaint.hasColorStage()) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 41 | GrPrintf("LCD Text will not draw correctly.\n"); |
| 42 | } |
| 43 | // setup blend so that we get mask * paintColor + (1-mask)*dstColor |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 44 | drawState->setBlendConstant(fPaint.getColor()); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 45 | drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 46 | // don't modulate by the paint's color in the frag since we're |
| 47 | // already doing it via the blend const. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 48 | drawState->setColor(0xffffffff); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 49 | } else { |
| 50 | // set back to normal in case we took LCD path previously. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 51 | drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff()); |
| 52 | drawState->setColor(fPaint.getColor()); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 53 | } |
| 54 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 55 | int nGlyphs = fCurrVertex / 4; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 56 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 57 | fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 58 | nGlyphs, |
| 59 | 4, 6); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 60 | fDrawTarget->resetVertexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 61 | fVertices = NULL; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 62 | fMaxVertices = 0; |
| 63 | fCurrVertex = 0; |
| 64 | GrSafeSetNull(fCurrTexture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 65 | } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 66 | drawState->disableStages(); |
| 67 | fDrawTarget = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 68 | } |
| 69 | |
bsalomon@google.com | 0e354aa | 2012-10-08 20:44:25 +0000 | [diff] [blame] | 70 | GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint) : fPaint(paint) { |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 71 | fContext = context; |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 72 | fStrike = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 74 | fCurrTexture = NULL; |
| 75 | fCurrVertex = 0; |
| 76 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 77 | const GrClipData* clipData = context->getClip(); |
| 78 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 79 | GrRect devConservativeBound; |
| 80 | clipData->fClipStack->getConservativeBounds( |
| 81 | -clipData->fOrigin.fX, |
| 82 | -clipData->fOrigin.fY, |
| 83 | context->getRenderTarget()->width(), |
| 84 | context->getRenderTarget()->height(), |
| 85 | &devConservativeBound); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 86 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 87 | devConservativeBound.roundOut(&fClipRect); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 88 | |
bsalomon@google.com | 858804d | 2012-10-15 14:25:50 +0000 | [diff] [blame] | 89 | fAutoMatrix.setIdentity(fContext, &fPaint); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 90 | |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 91 | fDrawTarget = NULL; |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 92 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | fVertices = NULL; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 94 | fMaxVertices = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | } |
| 96 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 97 | GrTextContext::~GrTextContext() { |
| 98 | this->flushGlyphs(); |
| 99 | if (fDrawTarget) { |
| 100 | fDrawTarget->drawState()->disableStages(); |
| 101 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | } |
| 103 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 104 | void GrTextContext::flush() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | this->flushGlyphs(); |
| 106 | } |
| 107 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 108 | void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | GrFixed vx, GrFixed vy, |
| 110 | GrFontScaler* scaler) { |
| 111 | if (NULL == fStrike) { |
| 112 | fStrike = fContext->getFontCache()->getStrike(scaler); |
| 113 | } |
| 114 | |
| 115 | GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
| 116 | if (NULL == glyph || glyph->fBounds.isEmpty()) { |
| 117 | return; |
| 118 | } |
| 119 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 120 | vx += SkIntToFixed(glyph->fBounds.fLeft); |
| 121 | vy += SkIntToFixed(glyph->fBounds.fTop); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | |
| 123 | // keep them as ints until we've done the clip-test |
| 124 | GrFixed width = glyph->fBounds.width(); |
| 125 | GrFixed height = glyph->fBounds.height(); |
| 126 | |
| 127 | // check if we clipped out |
| 128 | if (true || NULL == glyph->fAtlas) { |
| 129 | int x = vx >> 16; |
| 130 | int y = vy >> 16; |
| 131 | if (fClipRect.quickReject(x, y, x + width, y + height)) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 132 | // 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] | 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (NULL == glyph->fAtlas) { |
| 138 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 139 | goto HAS_ATLAS; |
| 140 | } |
reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame] | 141 | |
| 142 | // before we purge the cache, we must flush any accumulated draws |
| 143 | this->flushGlyphs(); |
bsalomon@google.com | 193395c | 2012-03-30 17:35:12 +0000 | [diff] [blame] | 144 | fContext->flush(); |
reed@google.com | 313e403 | 2010-12-22 22:16:59 +0000 | [diff] [blame] | 145 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | // try to purge |
| 147 | fContext->getFontCache()->purgeExceptFor(fStrike); |
| 148 | if (fStrike->getGlyphAtlas(glyph, scaler)) { |
| 149 | goto HAS_ATLAS; |
| 150 | } |
| 151 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | if (NULL == glyph->fPath) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 153 | SkPath* path = SkNEW(SkPath); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 154 | if (!scaler->getGlyphPath(glyph->glyphID(), path)) { |
| 155 | // flag the glyph as being dead? |
| 156 | delete path; |
| 157 | return; |
| 158 | } |
| 159 | glyph->fPath = path; |
| 160 | } |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 161 | |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 162 | GrContext::AutoMatrix am; |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 163 | SkMatrix translate; |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 164 | translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)), |
| 165 | SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop))); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 166 | GrPaint tmpPaint(fPaint); |
| 167 | am.setPreConcat(fContext, translate, &tmpPaint); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 168 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
| 169 | fContext->drawPath(tmpPaint, *glyph->fPath, stroke); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
| 173 | HAS_ATLAS: |
| 174 | GrAssert(glyph->fAtlas); |
| 175 | |
bsalomon@google.com | 8598328 | 2013-02-07 22:00:29 +0000 | [diff] [blame] | 176 | // now promote them to fixed (TODO: Rethink using fixed pt). |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 177 | width = SkIntToFixed(width); |
| 178 | height = SkIntToFixed(height); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 179 | |
| 180 | GrTexture* texture = glyph->fAtlas->texture(); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 181 | GrAssert(texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 183 | if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { |
| 184 | this->flushGlyphs(); |
| 185 | fCurrTexture = texture; |
| 186 | fCurrTexture->ref(); |
| 187 | } |
| 188 | |
| 189 | if (NULL == fVertices) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame^] | 190 | // position + texture coord |
| 191 | static const GrVertexAttrib kVertexAttribs[] = { |
| 192 | GrVertexAttrib(kVec2f_GrVertexAttribType, 0), |
| 193 | GrVertexAttrib(kVec2f_GrVertexAttribType, sizeof(GrPoint)) |
| 194 | }; |
| 195 | static const GrAttribBindings kAttribBindings = GrDrawState::ExplicitTexCoordAttribBindingsBit(kGlyphMaskStage); |
| 196 | |
| 197 | // 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] | 198 | // a number of verts to reserve and whether to perform a flush. |
| 199 | fMaxVertices = kMinRequestedVerts; |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 200 | bool flush = false; |
| 201 | fDrawTarget = fContext->getTextTarget(fPaint); |
| 202 | if (NULL != fDrawTarget) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame^] | 203 | fDrawTarget->drawState()->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs)); |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 204 | flush = fDrawTarget->geometryHints(&fMaxVertices, NULL); |
skia.committer@gmail.com | ae68392 | 2013-02-06 07:01:54 +0000 | [diff] [blame] | 205 | } |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 206 | if (flush) { |
| 207 | this->flushGlyphs(); |
| 208 | fContext->flush(); |
bsalomon@google.com | f3a7bc7 | 2013-02-05 21:32:06 +0000 | [diff] [blame] | 209 | // flushGlyphs() will reset fDrawTarget to NULL. |
| 210 | fDrawTarget = fContext->getTextTarget(fPaint); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame^] | 211 | fDrawTarget->drawState()->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs)); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 212 | } |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame^] | 213 | fDrawTarget->drawState()->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0); |
| 214 | fDrawTarget->drawState()->setAttribIndex(GrDrawState::kTexCoord_AttribIndex, 1); |
| 215 | fDrawTarget->drawState()->setAttribBindings(kAttribBindings); |
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 | } |
| 227 | bool success = fDrawTarget->reserveVertexAndIndexSpace( |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 228 | fMaxVertices, |
| 229 | 0, |
| 230 | GrTCast<void**>(&fVertices), |
| 231 | NULL); |
| 232 | GrAlwaysAssert(success); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame^] | 233 | GrAssert(2*sizeof(GrPoint) == fDrawTarget->getDrawState().getVertexSize()); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 234 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 235 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 236 | GrFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); |
| 237 | GrFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 238 | |
bsalomon@google.com | 8598328 | 2013-02-07 22:00:29 +0000 | [diff] [blame] | 239 | fVertices[2*fCurrVertex].setRectFan(SkFixedToFloat(vx), |
| 240 | SkFixedToFloat(vy), |
| 241 | SkFixedToFloat(vx + width), |
| 242 | SkFixedToFloat(vy + height), |
| 243 | 2 * sizeof(SkPoint)); |
| 244 | fVertices[2*fCurrVertex+1].setRectFan(SkFixedToFloat(texture->normalizeFixedX(tx)), |
| 245 | SkFixedToFloat(texture->normalizeFixedY(ty)), |
| 246 | SkFixedToFloat(texture->normalizeFixedX(tx + width)), |
| 247 | SkFixedToFloat(texture->normalizeFixedY(ty + height)), |
| 248 | 2 * sizeof(SkPoint)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 249 | fCurrVertex += 4; |
| 250 | } |