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