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