jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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. |
| 6 | */ |
| 7 | |
| 8 | #include "GrBitmapTextContext.h" |
| 9 | #include "GrAtlas.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 10 | #include "GrDefaultGeoProcFactory.h" |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame] | 12 | #include "GrFontCache.h" |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 13 | #include "GrFontScaler.h" |
| 14 | #include "GrIndexBuffer.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 15 | #include "GrStrokeInfo.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 16 | #include "GrTexturePriv.h" |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 17 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 18 | #include "SkAutoKern.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 19 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 9f94b91 | 2014-01-30 15:22:54 +0000 | [diff] [blame] | 20 | #include "SkDraw.h" |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 21 | #include "SkDrawProcs.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 22 | #include "SkGlyphCache.h" |
| 23 | #include "SkGpuDevice.h" |
| 24 | #include "SkGr.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 25 | #include "SkPath.h" |
| 26 | #include "SkRTConf.h" |
| 27 | #include "SkStrokeRec.h" |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 28 | #include "SkTextMapStateProc.h" |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 29 | |
egdaniel | 309e346 | 2014-12-09 10:35:58 -0800 | [diff] [blame] | 30 | #include "effects/GrBitmapTextGeoProc.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 31 | #include "effects/GrSimpleTextureEffect.h" |
| 32 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 33 | SK_CONF_DECLARE(bool, c_DumpFontCache, "gpu.dumpFontCache", false, |
| 34 | "Dump the contents of the font cache before every purge."); |
| 35 | |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 36 | namespace { |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 37 | static const size_t kLCDTextVASize = 2 * sizeof(SkPoint); |
| 38 | |
| 39 | // position + local coord |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 40 | static const size_t kColorTextVASize = 2 * sizeof(SkPoint); |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 41 | |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 42 | static const size_t kGrayTextVASize = 2 * sizeof(SkPoint) + sizeof(GrColor); |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 43 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 44 | static const int kVerticesPerGlyph = 4; |
| 45 | static const int kIndicesPerGlyph = 6; |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
skia.committer@gmail.com | e5d7015 | 2014-01-29 07:01:48 +0000 | [diff] [blame] | 48 | GrBitmapTextContext::GrBitmapTextContext(GrContext* context, |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 49 | const SkDeviceProperties& properties) |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 50 | : GrTextContext(context, properties) { |
| 51 | fStrike = NULL; |
| 52 | |
| 53 | fCurrTexture = NULL; |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 54 | fEffectTextureUniqueID = SK_InvalidUniqueID; |
| 55 | |
| 56 | fVertices = NULL; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 57 | fCurrVertex = 0; |
| 58 | fAllocVertexCount = 0; |
| 59 | fTotalVertexCount = 0; |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 60 | |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 61 | fVertexBounds.setLargestInverted(); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 62 | } |
| 63 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 64 | GrBitmapTextContext* GrBitmapTextContext::Create(GrContext* context, |
| 65 | const SkDeviceProperties& props) { |
| 66 | return SkNEW_ARGS(GrBitmapTextContext, (context, props)); |
| 67 | } |
| 68 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 69 | bool GrBitmapTextContext::canDraw(const SkPaint& paint) { |
| 70 | return !SkDraw::ShouldDrawTextAsPaths(paint, fContext->getMatrix()); |
| 71 | } |
| 72 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 73 | inline void GrBitmapTextContext::init(const GrPaint& paint, const SkPaint& skPaint) { |
| 74 | GrTextContext::init(paint, skPaint); |
| 75 | |
| 76 | fStrike = NULL; |
| 77 | |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 78 | fCurrTexture = NULL; |
jvanverth | 63b9dc8 | 2014-08-28 10:39:40 -0700 | [diff] [blame] | 79 | fCurrVertex = 0; |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 80 | |
| 81 | fVertices = NULL; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 82 | fAllocVertexCount = 0; |
| 83 | fTotalVertexCount = 0; |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 84 | } |
| 85 | |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 86 | void GrBitmapTextContext::onDrawText(const GrPaint& paint, const SkPaint& skPaint, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 87 | const char text[], size_t byteLength, |
| 88 | SkScalar x, SkScalar y) { |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 89 | SkASSERT(byteLength == 0 || text != NULL); |
| 90 | |
| 91 | // nothing to draw |
| 92 | if (text == NULL || byteLength == 0 /*|| fRC->isEmpty()*/) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | this->init(paint, skPaint); |
| 97 | |
| 98 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 99 | |
| 100 | SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMatrix()); |
| 101 | SkGlyphCache* cache = autoCache.getCache(); |
| 102 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 103 | |
| 104 | // transform our starting point |
| 105 | { |
| 106 | SkPoint loc; |
| 107 | fContext->getMatrix().mapXY(x, y, &loc); |
| 108 | x = loc.fX; |
| 109 | y = loc.fY; |
| 110 | } |
| 111 | |
| 112 | // need to measure first |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 113 | int numGlyphs; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 114 | if (fSkPaint.getTextAlign() != SkPaint::kLeft_Align) { |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 115 | SkVector stopVector; |
| 116 | numGlyphs = MeasureText(cache, glyphCacheProc, text, byteLength, &stopVector); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 117 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 118 | SkScalar stopX = stopVector.fX; |
| 119 | SkScalar stopY = stopVector.fY; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 120 | |
| 121 | if (fSkPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 122 | stopX = SkScalarHalf(stopX); |
| 123 | stopY = SkScalarHalf(stopY); |
| 124 | } |
| 125 | x -= stopX; |
| 126 | y -= stopY; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 127 | } else { |
| 128 | numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL); |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 129 | } |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 130 | fTotalVertexCount = kVerticesPerGlyph*numGlyphs; |
jvanverth | aab626c | 2014-10-16 08:04:39 -0700 | [diff] [blame] | 131 | |
| 132 | const char* stop = text + byteLength; |
| 133 | |
| 134 | SkAutoKern autokern; |
| 135 | |
| 136 | SkFixed fxMask = ~0; |
| 137 | SkFixed fyMask = ~0; |
| 138 | SkFixed halfSampleX, halfSampleY; |
| 139 | if (cache->isSubpixel()) { |
| 140 | halfSampleX = halfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits); |
| 141 | SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(fContext->getMatrix()); |
| 142 | if (kX_SkAxisAlignment == baseline) { |
| 143 | fyMask = 0; |
| 144 | halfSampleY = SK_FixedHalf; |
| 145 | } else if (kY_SkAxisAlignment == baseline) { |
| 146 | fxMask = 0; |
| 147 | halfSampleX = SK_FixedHalf; |
| 148 | } |
| 149 | } else { |
| 150 | halfSampleX = halfSampleY = SK_FixedHalf; |
| 151 | } |
| 152 | |
| 153 | SkFixed fx = SkScalarToFixed(x) + halfSampleX; |
| 154 | SkFixed fy = SkScalarToFixed(y) + halfSampleY; |
| 155 | |
| 156 | GrContext::AutoMatrix autoMatrix; |
| 157 | autoMatrix.setIdentity(fContext, &fPaint); |
| 158 | |
| 159 | while (text < stop) { |
| 160 | const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask); |
| 161 | |
| 162 | fx += autokern.adjust(glyph); |
| 163 | |
| 164 | if (glyph.fWidth) { |
| 165 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 166 | glyph.getSubXFixed(), |
| 167 | glyph.getSubYFixed()), |
| 168 | SkFixedFloorToFixed(fx), |
| 169 | SkFixedFloorToFixed(fy), |
| 170 | fontScaler); |
| 171 | } |
| 172 | |
| 173 | fx += glyph.fAdvanceX; |
| 174 | fy += glyph.fAdvanceY; |
| 175 | } |
| 176 | |
| 177 | this->finish(); |
| 178 | } |
| 179 | |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 180 | void GrBitmapTextContext::onDrawPosText(const GrPaint& paint, const SkPaint& skPaint, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 181 | const char text[], size_t byteLength, |
| 182 | const SkScalar pos[], int scalarsPerPosition, |
| 183 | const SkPoint& offset) { |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 184 | SkASSERT(byteLength == 0 || text != NULL); |
| 185 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 186 | |
| 187 | // nothing to draw |
| 188 | if (text == NULL || byteLength == 0/* || fRC->isEmpty()*/) { |
| 189 | return; |
| 190 | } |
| 191 | |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 192 | this->init(paint, skPaint); |
| 193 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 194 | SkDrawCacheProc glyphCacheProc = fSkPaint.getDrawCacheProc(); |
| 195 | |
| 196 | SkAutoGlyphCache autoCache(fSkPaint, &fDeviceProperties, &fContext->getMatrix()); |
| 197 | SkGlyphCache* cache = autoCache.getCache(); |
| 198 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 199 | |
| 200 | // store original matrix before we reset, so we can use it to transform positions |
| 201 | SkMatrix ctm = fContext->getMatrix(); |
| 202 | GrContext::AutoMatrix autoMatrix; |
| 203 | autoMatrix.setIdentity(fContext, &fPaint); |
| 204 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 205 | int numGlyphs = fSkPaint.textToGlyphs(text, byteLength, NULL); |
| 206 | fTotalVertexCount = kVerticesPerGlyph*numGlyphs; |
| 207 | |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 208 | const char* stop = text + byteLength; |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 209 | SkTextAlignProc alignProc(fSkPaint.getTextAlign()); |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 210 | SkTextMapStateProc tmsProc(ctm, offset, scalarsPerPosition); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 211 | SkFixed halfSampleX = 0, halfSampleY = 0; |
| 212 | |
| 213 | if (cache->isSubpixel()) { |
| 214 | // maybe we should skip the rounding if linearText is set |
| 215 | SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(ctm); |
| 216 | |
| 217 | SkFixed fxMask = ~0; |
| 218 | SkFixed fyMask = ~0; |
| 219 | if (kX_SkAxisAlignment == baseline) { |
| 220 | fyMask = 0; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 221 | halfSampleY = SK_FixedHalf; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 222 | } else if (kY_SkAxisAlignment == baseline) { |
| 223 | fxMask = 0; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 224 | halfSampleX = SK_FixedHalf; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
| 228 | while (text < stop) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 229 | SkPoint tmsLoc; |
| 230 | tmsProc(pos, &tmsLoc); |
| 231 | SkFixed fx = SkScalarToFixed(tmsLoc.fX) + halfSampleX; |
| 232 | SkFixed fy = SkScalarToFixed(tmsLoc.fY) + halfSampleY; |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 233 | |
| 234 | const SkGlyph& glyph = glyphCacheProc(cache, &text, |
| 235 | fx & fxMask, fy & fyMask); |
| 236 | |
| 237 | if (glyph.fWidth) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 238 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 239 | glyph.getSubXFixed(), |
| 240 | glyph.getSubYFixed()), |
| 241 | SkFixedFloorToFixed(fx), |
| 242 | SkFixedFloorToFixed(fy), |
| 243 | fontScaler); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 244 | } |
| 245 | pos += scalarsPerPosition; |
| 246 | } |
| 247 | } else { |
| 248 | while (text < stop) { |
| 249 | const char* currentText = text; |
| 250 | const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0); |
| 251 | |
| 252 | if (metricGlyph.fWidth) { |
| 253 | SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;) |
| 254 | SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;) |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 255 | SkPoint tmsLoc; |
| 256 | tmsProc(pos, &tmsLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 257 | SkIPoint fixedLoc; |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 258 | alignProc(tmsLoc, metricGlyph, &fixedLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 259 | |
| 260 | SkFixed fx = fixedLoc.fX + halfSampleX; |
| 261 | SkFixed fy = fixedLoc.fY + halfSampleY; |
| 262 | |
| 263 | // have to call again, now that we've been "aligned" |
| 264 | const SkGlyph& glyph = glyphCacheProc(cache, ¤tText, |
| 265 | fx & fxMask, fy & fyMask); |
| 266 | // the assumption is that the metrics haven't changed |
| 267 | SkASSERT(prevAdvX == glyph.fAdvanceX); |
| 268 | SkASSERT(prevAdvY == glyph.fAdvanceY); |
| 269 | SkASSERT(glyph.fWidth); |
| 270 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 271 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 272 | glyph.getSubXFixed(), |
| 273 | glyph.getSubYFixed()), |
| 274 | SkFixedFloorToFixed(fx), |
| 275 | SkFixedFloorToFixed(fy), |
| 276 | fontScaler); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 277 | } |
| 278 | pos += scalarsPerPosition; |
| 279 | } |
| 280 | } |
| 281 | } else { // not subpixel |
| 282 | |
| 283 | if (SkPaint::kLeft_Align == fSkPaint.getTextAlign()) { |
| 284 | while (text < stop) { |
| 285 | // the last 2 parameters are ignored |
| 286 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 287 | |
| 288 | if (glyph.fWidth) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 289 | SkPoint tmsLoc; |
| 290 | tmsProc(pos, &tmsLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 291 | |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 292 | SkFixed fx = SkScalarToFixed(tmsLoc.fX) + SK_FixedHalf; //halfSampleX; |
| 293 | SkFixed fy = SkScalarToFixed(tmsLoc.fY) + SK_FixedHalf; //halfSampleY; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 294 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 295 | glyph.getSubXFixed(), |
| 296 | glyph.getSubYFixed()), |
| 297 | SkFixedFloorToFixed(fx), |
| 298 | SkFixedFloorToFixed(fy), |
| 299 | fontScaler); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 300 | } |
| 301 | pos += scalarsPerPosition; |
| 302 | } |
| 303 | } else { |
| 304 | while (text < stop) { |
| 305 | // the last 2 parameters are ignored |
| 306 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 307 | |
| 308 | if (glyph.fWidth) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 309 | SkPoint tmsLoc; |
| 310 | tmsProc(pos, &tmsLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 311 | |
| 312 | SkIPoint fixedLoc; |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 313 | alignProc(tmsLoc, glyph, &fixedLoc); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 314 | |
| 315 | SkFixed fx = fixedLoc.fX + SK_FixedHalf; //halfSampleX; |
| 316 | SkFixed fy = fixedLoc.fY + SK_FixedHalf; //halfSampleY; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 317 | this->appendGlyph(GrGlyph::Pack(glyph.getGlyphID(), |
| 318 | glyph.getSubXFixed(), |
| 319 | glyph.getSubYFixed()), |
| 320 | SkFixedFloorToFixed(fx), |
| 321 | SkFixedFloorToFixed(fy), |
| 322 | fontScaler); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 323 | } |
| 324 | pos += scalarsPerPosition; |
| 325 | } |
| 326 | } |
| 327 | } |
commit-bot@chromium.org | cbbc481 | 2014-01-30 22:05:47 +0000 | [diff] [blame] | 328 | |
| 329 | this->finish(); |
commit-bot@chromium.org | e8612d9 | 2014-01-28 22:02:07 +0000 | [diff] [blame] | 330 | } |
| 331 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 332 | static size_t get_vertex_stride(GrMaskFormat maskFormat) { |
| 333 | switch (maskFormat) { |
| 334 | case kA8_GrMaskFormat: |
| 335 | return kGrayTextVASize; |
| 336 | case kARGB_GrMaskFormat: |
| 337 | return kColorTextVASize; |
| 338 | default: |
| 339 | return kLCDTextVASize; |
| 340 | } |
| 341 | } |
| 342 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 343 | static void* alloc_vertices(GrDrawTarget* drawTarget, |
| 344 | int numVertices, |
| 345 | GrMaskFormat maskFormat) { |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 346 | if (numVertices <= 0) { |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | // set up attributes |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 351 | void* vertices = NULL; |
| 352 | bool success = drawTarget->reserveVertexAndIndexSpace(numVertices, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 353 | get_vertex_stride(maskFormat), |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 354 | 0, |
| 355 | &vertices, |
| 356 | NULL); |
| 357 | GrAlwaysAssert(success); |
| 358 | return vertices; |
| 359 | } |
| 360 | |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame] | 361 | inline bool GrBitmapTextContext::uploadGlyph(GrGlyph* glyph, GrFontScaler* scaler) { |
| 362 | if (!fStrike->glyphTooLargeForAtlas(glyph)) { |
| 363 | if (fStrike->addGlyphToAtlas(glyph, scaler)) { |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | // try to clear out an unused plot before we flush |
| 368 | if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) && |
| 369 | fStrike->addGlyphToAtlas(glyph, scaler)) { |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | if (c_DumpFontCache) { |
| 374 | #ifdef SK_DEVELOPER |
| 375 | fContext->getFontCache()->dump(); |
| 376 | #endif |
| 377 | } |
| 378 | |
| 379 | // before we purge the cache, we must flush any accumulated draws |
| 380 | this->flush(); |
| 381 | fContext->flush(); |
| 382 | |
| 383 | // we should have an unused plot now |
| 384 | if (fContext->getFontCache()->freeUnusedPlot(fStrike, glyph) && |
| 385 | fStrike->addGlyphToAtlas(glyph, scaler)) { |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | // we should never get here |
| 390 | SkASSERT(false); |
| 391 | } |
| 392 | |
| 393 | return false; |
| 394 | } |
| 395 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 396 | void GrBitmapTextContext::appendGlyph(GrGlyph::PackedID packed, |
| 397 | SkFixed vx, SkFixed vy, |
| 398 | GrFontScaler* scaler) { |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 399 | if (NULL == fDrawTarget) { |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | if (NULL == fStrike) { |
| 404 | fStrike = fContext->getFontCache()->getStrike(scaler, false); |
| 405 | } |
| 406 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 407 | GrGlyph* glyph = fStrike->getGlyph(packed, scaler); |
| 408 | if (NULL == glyph || glyph->fBounds.isEmpty()) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | vx += SkIntToFixed(glyph->fBounds.fLeft); |
| 413 | vy += SkIntToFixed(glyph->fBounds.fTop); |
| 414 | |
| 415 | // keep them as ints until we've done the clip-test |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 416 | SkFixed width = glyph->fBounds.width(); |
| 417 | SkFixed height = glyph->fBounds.height(); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 418 | |
| 419 | // check if we clipped out |
| 420 | if (true || NULL == glyph->fPlot) { |
| 421 | int x = vx >> 16; |
| 422 | int y = vy >> 16; |
| 423 | if (fClipRect.quickReject(x, y, x + width, y + height)) { |
| 424 | // SkCLZ(3); // so we can set a break-point in the debugger |
| 425 | return; |
| 426 | } |
| 427 | } |
| 428 | |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame] | 429 | if (NULL == glyph->fPlot && !uploadGlyph(glyph, scaler)) { |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 430 | if (NULL == glyph->fPath) { |
| 431 | SkPath* path = SkNEW(SkPath); |
| 432 | if (!scaler->getGlyphPath(glyph->glyphID(), path)) { |
| 433 | // flag the glyph as being dead? |
| 434 | delete path; |
| 435 | return; |
| 436 | } |
| 437 | glyph->fPath = path; |
| 438 | } |
| 439 | |
bsalomon | ec87dc6 | 2014-10-14 10:52:00 -0700 | [diff] [blame] | 440 | // flush any accumulated draws before drawing this glyph as a path. |
| 441 | this->flush(); |
| 442 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 443 | GrContext::AutoMatrix am; |
| 444 | SkMatrix translate; |
| 445 | translate.setTranslate(SkFixedToScalar(vx - SkIntToFixed(glyph->fBounds.fLeft)), |
| 446 | SkFixedToScalar(vy - SkIntToFixed(glyph->fBounds.fTop))); |
| 447 | GrPaint tmpPaint(fPaint); |
| 448 | am.setPreConcat(fContext, translate, &tmpPaint); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 449 | GrStrokeInfo strokeInfo(SkStrokeRec::kFill_InitStyle); |
| 450 | fContext->drawPath(tmpPaint, *glyph->fPath, strokeInfo); |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 451 | |
| 452 | // remove this glyph from the vertices we need to allocate |
| 453 | fTotalVertexCount -= kVerticesPerGlyph; |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 457 | SkASSERT(glyph->fPlot); |
| 458 | GrDrawTarget::DrawToken drawToken = fDrawTarget->getCurrentDrawToken(); |
| 459 | glyph->fPlot->setDrawToken(drawToken); |
| 460 | |
| 461 | // now promote them to fixed (TODO: Rethink using fixed pt). |
| 462 | width = SkIntToFixed(width); |
| 463 | height = SkIntToFixed(height); |
| 464 | |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 465 | // the current texture/maskformat must match what the glyph needs |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 466 | GrTexture* texture = glyph->fPlot->texture(); |
| 467 | SkASSERT(texture); |
| 468 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 469 | if (fCurrTexture != texture || fCurrVertex + kVerticesPerGlyph > fAllocVertexCount) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 470 | this->flush(); |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 471 | fCurrTexture = texture; |
| 472 | fCurrTexture->ref(); |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 473 | fCurrMaskFormat = glyph->fMaskFormat; |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 476 | if (NULL == fVertices) { |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 477 | int maxQuadVertices = kVerticesPerGlyph * fContext->getQuadIndexBuffer()->maxQuads(); |
| 478 | fAllocVertexCount = SkMin32(fTotalVertexCount, maxQuadVertices); |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 479 | fVertices = alloc_vertices(fDrawTarget, fAllocVertexCount, fCurrMaskFormat); |
Mike Klein | 6a25bd0 | 2014-08-29 10:03:59 -0400 | [diff] [blame] | 480 | } |
| 481 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 482 | SkFixed tx = SkIntToFixed(glyph->fAtlasLocation.fX); |
| 483 | SkFixed ty = SkIntToFixed(glyph->fAtlasLocation.fY); |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 484 | |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 485 | SkRect r; |
| 486 | r.fLeft = SkFixedToFloat(vx); |
| 487 | r.fTop = SkFixedToFloat(vy); |
| 488 | r.fRight = SkFixedToFloat(vx + width); |
| 489 | r.fBottom = SkFixedToFloat(vy + height); |
| 490 | |
reed | 10d0327 | 2014-10-01 09:24:12 -0700 | [diff] [blame] | 491 | fVertexBounds.joinNonEmptyArg(r); |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 492 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 493 | size_t vertSize = get_vertex_stride(fCurrMaskFormat); |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 494 | |
| 495 | SkPoint* positions = reinterpret_cast<SkPoint*>( |
| 496 | reinterpret_cast<intptr_t>(fVertices) + vertSize * fCurrVertex); |
| 497 | positions->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom, vertSize); |
| 498 | |
| 499 | // The texture coords are last in both the with and without color vertex layouts. |
| 500 | SkPoint* textureCoords = reinterpret_cast<SkPoint*>( |
| 501 | reinterpret_cast<intptr_t>(positions) + vertSize - sizeof(SkPoint)); |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 502 | textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)), |
| 503 | SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)), |
| 504 | SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + width)), |
| 505 | SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + height)), |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 506 | vertSize); |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 507 | if (kA8_GrMaskFormat == fCurrMaskFormat) { |
bsalomon | 594069f | 2014-06-06 06:16:34 -0700 | [diff] [blame] | 508 | // color comes after position. |
| 509 | GrColor* colors = reinterpret_cast<GrColor*>(positions + 1); |
| 510 | for (int i = 0; i < 4; ++i) { |
| 511 | *colors = fPaint.getColor(); |
| 512 | colors = reinterpret_cast<GrColor*>(reinterpret_cast<intptr_t>(colors) + vertSize); |
| 513 | } |
| 514 | } |
jvanverth@google.com | c7a40fa | 2013-10-16 18:15:34 +0000 | [diff] [blame] | 515 | fCurrVertex += 4; |
| 516 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 517 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 518 | void GrBitmapTextContext::flush() { |
| 519 | if (NULL == fDrawTarget) { |
| 520 | return; |
| 521 | } |
| 522 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 523 | if (fCurrVertex > 0) { |
joshualitt | 780b11e | 2014-11-18 09:40:40 -0800 | [diff] [blame] | 524 | GrDrawState drawState; |
| 525 | drawState.setFromPaint(fPaint, SkMatrix::I(), fContext->getRenderTarget()); |
| 526 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 527 | // setup our sampler state for our text texture/atlas |
| 528 | SkASSERT(SkIsAlign4(fCurrVertex)); |
| 529 | SkASSERT(fCurrTexture); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 530 | |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 531 | SkASSERT(fStrike); |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame^] | 532 | GrColor color = fPaint.getColor(); |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 533 | switch (fCurrMaskFormat) { |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 534 | // Color bitmap text |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame^] | 535 | case kARGB_GrMaskFormat: { |
| 536 | int a = fSkPaint.getAlpha(); |
| 537 | color = SkColorSetARGB(a, a, a, a); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 538 | break; |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame^] | 539 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 540 | // LCD text |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 541 | case kA565_GrMaskFormat: { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 542 | // TODO: move supportsRGBCoverage check to setupCoverageEffect and only add LCD |
| 543 | // processor if the xp can support it. For now we will simply assume that if |
| 544 | // fUseLCDText is true, then we have a known color output. |
| 545 | if (!drawState.getXPFactory()->supportsRGBCoverage(0, kRGBA_GrColorComponentFlags)) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 546 | SkDebugf("LCD Text will not draw correctly.\n"); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 547 | } |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 548 | break; |
| 549 | } |
| 550 | // Grayscale/BW text |
| 551 | case kA8_GrMaskFormat: |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 552 | drawState.setHint(GrDrawState::kVertexColorsAreOpaque_Hint, |
| 553 | 0xFF == GrColorUnpackA(fPaint.getColor())); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 554 | break; |
| 555 | default: |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 556 | SkFAIL("Unexpected mask format."); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 557 | } |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame^] | 558 | |
| 559 | GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kNone_FilterMode); |
| 560 | // TODO cache these GPs |
| 561 | if (kARGB_GrMaskFormat == fCurrMaskFormat) { |
| 562 | uint32_t textureUniqueID = fCurrTexture->getUniqueID(); |
| 563 | if (textureUniqueID != fEffectTextureUniqueID || |
| 564 | fCachedGeometryProcessor->getColor() != color) { |
| 565 | uint32_t flags = GrDefaultGeoProcFactory::kLocalCoord_GPType; |
| 566 | fCachedGeometryProcessor.reset(GrDefaultGeoProcFactory::Create(color, flags)); |
| 567 | fCachedTextureProcessor.reset(GrSimpleTextureEffect::Create(fCurrTexture, |
| 568 | SkMatrix::I(), |
| 569 | params)); |
| 570 | } |
| 571 | drawState.setGeometryProcessor(fCachedGeometryProcessor.get()); |
| 572 | drawState.addColorProcessor(fCachedTextureProcessor.get()); |
| 573 | } else { |
| 574 | uint32_t textureUniqueID = fCurrTexture->getUniqueID(); |
| 575 | if (textureUniqueID != fEffectTextureUniqueID || |
| 576 | fCachedGeometryProcessor->getColor() != color) { |
| 577 | bool hasColor = kA8_GrMaskFormat == fCurrMaskFormat; |
| 578 | fCachedGeometryProcessor.reset(GrBitmapTextGeoProc::Create(color, |
| 579 | fCurrTexture, |
| 580 | params, |
| 581 | hasColor)); |
| 582 | fEffectTextureUniqueID = textureUniqueID; |
| 583 | } |
| 584 | drawState.setGeometryProcessor(fCachedGeometryProcessor.get()); |
| 585 | } |
| 586 | |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 587 | int nGlyphs = fCurrVertex / kVerticesPerGlyph; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 588 | fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 589 | fDrawTarget->drawIndexedInstances(&drawState, |
| 590 | kTriangles_GrPrimitiveType, |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 591 | nGlyphs, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 592 | kVerticesPerGlyph, |
| 593 | kIndicesPerGlyph, |
| 594 | &fVertexBounds); |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 595 | |
| 596 | fDrawTarget->resetVertexSource(); |
| 597 | fVertices = NULL; |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 598 | fAllocVertexCount = 0; |
| 599 | // reset to be those that are left |
| 600 | fTotalVertexCount -= fCurrVertex; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 601 | fCurrVertex = 0; |
| 602 | fVertexBounds.setLargestInverted(); |
| 603 | SkSafeSetNull(fCurrTexture); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | inline void GrBitmapTextContext::finish() { |
| 608 | this->flush(); |
jvanverth | 73f1053 | 2014-10-23 11:57:12 -0700 | [diff] [blame] | 609 | fTotalVertexCount = 0; |
jvanverth | 0fedb19 | 2014-10-08 09:07:27 -0700 | [diff] [blame] | 610 | |
| 611 | GrTextContext::finish(); |
| 612 | } |
| 613 | |