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" |
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 |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 32 | drawState->stage(kGlyphMaskStage)->reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | |
| 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 | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 37 | drawState->createTextureEffect(kGlyphMaskStage, fCurrTexture, GrMatrix::I(), params); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 38 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 39 | if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 40 | if (kOne_GrBlendCoeff != fPaint.getSrcBlendCoeff() || |
| 41 | kISA_GrBlendCoeff != fPaint.getDstBlendCoeff() || |
bsalomon@google.com | 88becf4 | 2012-10-05 14:54:42 +0000 | [diff] [blame] | 42 | fPaint.hasColorStage()) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 43 | GrPrintf("LCD Text will not draw correctly.\n"); |
| 44 | } |
| 45 | // setup blend so that we get mask * paintColor + (1-mask)*dstColor |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 46 | drawState->setBlendConstant(fPaint.getColor()); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 47 | drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 48 | // don't modulate by the paint's color in the frag since we're |
| 49 | // already doing it via the blend const. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 50 | drawState->setColor(0xffffffff); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 51 | } else { |
| 52 | // set back to normal in case we took LCD path previously. |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 53 | drawState->setBlendFunc(fPaint.getSrcBlendCoeff(), fPaint.getDstBlendCoeff()); |
| 54 | drawState->setColor(fPaint.getColor()); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 55 | } |
| 56 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 57 | int nGlyphs = fCurrVertex / 4; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 58 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 59 | fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 60 | nGlyphs, |
| 61 | 4, 6); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 62 | fDrawTarget->resetVertexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | fVertices = NULL; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 64 | fMaxVertices = 0; |
| 65 | fCurrVertex = 0; |
| 66 | GrSafeSetNull(fCurrTexture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 68 | drawState->disableStages(); |
| 69 | fDrawTarget = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 70 | } |
| 71 | |
bsalomon@google.com | 0e354aa | 2012-10-08 20:44:25 +0000 | [diff] [blame] | 72 | GrTextContext::GrTextContext(GrContext* context, const GrPaint& paint) : fPaint(paint) { |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 73 | fContext = context; |
bsalomon@google.com | f4a9c82 | 2012-03-16 14:02:46 +0000 | [diff] [blame] | 74 | fStrike = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 76 | fCurrTexture = NULL; |
| 77 | fCurrVertex = 0; |
| 78 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 79 | const GrClipData* clipData = context->getClip(); |
| 80 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 81 | GrRect devConservativeBound; |
| 82 | clipData->fClipStack->getConservativeBounds( |
| 83 | -clipData->fOrigin.fX, |
| 84 | -clipData->fOrigin.fY, |
| 85 | context->getRenderTarget()->width(), |
| 86 | context->getRenderTarget()->height(), |
| 87 | &devConservativeBound); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 88 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 89 | devConservativeBound.roundOut(&fClipRect); |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 90 | |
bsalomon@google.com | 858804d | 2012-10-15 14:25:50 +0000 | [diff] [blame] | 91 | fAutoMatrix.setIdentity(fContext, &fPaint); |
bsalomon@google.com | 3914958 | 2011-06-13 21:55:32 +0000 | [diff] [blame] | 92 | |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 93 | fDrawTarget = NULL; |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 94 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | fVertices = NULL; |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 96 | fMaxVertices = 0; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 97 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 98 | fVertexLayout = |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 99 | GrDrawTarget::kTextFormat_VertexLayoutBit | |
| 100 | GrDrawTarget::StageTexCoordVertexLayoutBit(kGlyphMaskStage, 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(); |
| 105 | if (fDrawTarget) { |
| 106 | fDrawTarget->drawState()->disableStages(); |
| 107 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | } |
| 109 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 110 | void GrTextContext::flush() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 111 | this->flushGlyphs(); |
| 112 | } |
| 113 | |
| 114 | static inline void setRectFan(GrGpuTextVertex v[4], int l, int t, int r, int b, |
| 115 | int stride) { |
| 116 | v[0 * stride].setI(l, t); |
| 117 | v[1 * stride].setI(l, b); |
| 118 | v[2 * stride].setI(r, b); |
| 119 | v[3 * stride].setI(r, t); |
| 120 | } |
| 121 | |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 122 | void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | GrFixed vx, GrFixed vy, |
| 124 | GrFontScaler* scaler) { |
| 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)) { |
| 146 | // Gr_clz(3); // so we can set a break-point in the debugger |
| 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 | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 177 | GrMatrix 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); |
| 182 | fContext->drawPath(tmpPaint, *glyph->fPath, kWinding_GrPathFill); |
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; |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 206 | bool flush = (NULL != fDrawTarget) && |
| 207 | fDrawTarget->geometryHints(fVertexLayout, |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 208 | &fMaxVertices, |
| 209 | NULL); |
| 210 | if (flush) { |
| 211 | this->flushGlyphs(); |
| 212 | fContext->flush(); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 213 | } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 214 | fDrawTarget = fContext->getTextTarget(fPaint); |
| 215 | fMaxVertices = kDefaultRequestedVerts; |
| 216 | // ignore return, no point in flushing again. |
| 217 | fDrawTarget->geometryHints(fVertexLayout, |
| 218 | &fMaxVertices, |
| 219 | NULL); |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 220 | |
| 221 | int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads(); |
| 222 | if (fMaxVertices < kMinRequestedVerts) { |
| 223 | fMaxVertices = kDefaultRequestedVerts; |
| 224 | } else if (fMaxVertices > maxQuadVertices) { |
| 225 | // don't exceed the limit of the index buffer |
| 226 | fMaxVertices = maxQuadVertices; |
| 227 | } |
| 228 | bool success = fDrawTarget->reserveVertexAndIndexSpace( |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 229 | fVertexLayout, |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 230 | fMaxVertices, |
| 231 | 0, |
| 232 | GrTCast<void**>(&fVertices), |
| 233 | NULL); |
| 234 | GrAlwaysAssert(success); |
| 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 | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 240 | #if GR_TEXT_SCALAR_IS_USHORT |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 241 | int x = vx >> 16; |
| 242 | int y = vy >> 16; |
| 243 | int w = width >> 16; |
| 244 | int h = height >> 16; |
| 245 | |
| 246 | setRectFan(&fVertices[2*fCurrVertex], x, y, x + w, y + h, 2); |
| 247 | setRectFan(&fVertices[2*fCurrVertex+1], |
| 248 | texture->normalizeFixedX(tx), |
| 249 | texture->normalizeFixedY(ty), |
| 250 | texture->normalizeFixedX(tx + width), |
| 251 | texture->normalizeFixedY(ty + height), |
| 252 | 2); |
| 253 | #else |
| 254 | fVertices[2*fCurrVertex].setXRectFan(vx, vy, vx + width, vy + height, |
| 255 | 2 * sizeof(GrGpuTextVertex)); |
| 256 | fVertices[2*fCurrVertex+1].setXRectFan(texture->normalizeFixedX(tx), |
| 257 | texture->normalizeFixedY(ty), |
| 258 | texture->normalizeFixedX(tx + width), |
| 259 | texture->normalizeFixedY(ty + height), |
| 260 | 2 * sizeof(GrGpuTextVertex)); |
| 261 | #endif |
| 262 | fCurrVertex += 4; |
| 263 | } |
tomhudson@google.com | 375ff85 | 2012-06-29 18:37:57 +0000 | [diff] [blame] | 264 | |