joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | #include "GrAtlasTextContext.h" |
| 8 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 9 | #include "GrDrawContext.h" |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 10 | #include "GrDrawTarget.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 11 | #include "GrFontScaler.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 12 | #include "GrStrokeInfo.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 13 | #include "GrTextBlobCache.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 14 | #include "GrTexturePriv.h" |
bsalomon | 72e3ae4 | 2015-04-28 08:08:46 -0700 | [diff] [blame] | 15 | #include "GrVertexBuffer.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 16 | |
| 17 | #include "SkAutoKern.h" |
| 18 | #include "SkColorPriv.h" |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 19 | #include "SkColorFilter.h" |
| 20 | #include "SkDistanceFieldGen.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 21 | #include "SkDraw.h" |
| 22 | #include "SkDrawFilter.h" |
| 23 | #include "SkDrawProcs.h" |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 24 | #include "SkFindAndPlaceGlyph.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 25 | #include "SkGlyphCache.h" |
| 26 | #include "SkGpuDevice.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 27 | #include "SkGrPriv.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 28 | #include "SkPath.h" |
| 29 | #include "SkRTConf.h" |
| 30 | #include "SkStrokeRec.h" |
| 31 | #include "SkTextBlob.h" |
| 32 | #include "SkTextMapStateProc.h" |
| 33 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 34 | #include "batches/GrAtlasTextBatch.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 35 | |
| 36 | namespace { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 37 | static const int kMinDFFontSize = 18; |
| 38 | static const int kSmallDFFontSize = 32; |
| 39 | static const int kSmallDFFontLimit = 32; |
| 40 | static const int kMediumDFFontSize = 72; |
| 41 | static const int kMediumDFFontLimit = 72; |
| 42 | static const int kLargeDFFontSize = 162; |
jvanverth | 97c595f | 2015-06-19 11:06:28 -0700 | [diff] [blame] | 43 | #ifdef SK_BUILD_FOR_ANDROID |
| 44 | static const int kLargeDFFontLimit = 384; |
| 45 | #else |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 46 | static const int kLargeDFFontLimit = 2 * kLargeDFFontSize; |
jvanverth | 97c595f | 2015-06-19 11:06:28 -0700 | [diff] [blame] | 47 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 50 | GrAtlasTextContext::GrAtlasTextContext(GrContext* context, const SkSurfaceProps& surfaceProps) |
| 51 | : INHERITED(context, surfaceProps) |
joshualitt | 1acabf3 | 2015-12-10 09:10:10 -0800 | [diff] [blame] | 52 | , fDistanceAdjustTable(new GrDistanceFieldAdjustTable) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 53 | // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest |
| 54 | // vertexStride |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 55 | static_assert(GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kColorTextVASize && |
| 56 | GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kLCDTextVASize, |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 57 | "vertex_attribute_changed"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | fCurrStrike = nullptr; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 59 | fCache = context->getTextBlobCache(); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 60 | } |
| 61 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 62 | |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 63 | GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 64 | const SkSurfaceProps& surfaceProps) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 65 | return new GrAtlasTextContext(context, surfaceProps); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 66 | } |
| 67 | |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 68 | bool GrAtlasTextContext::canDraw(const SkPaint& skPaint, const SkMatrix& viewMatrix) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 69 | return this->canDrawAsDistanceFields(skPaint, viewMatrix) || |
| 70 | !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 71 | } |
| 72 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 73 | GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) { |
| 74 | GrColor canonicalColor = paint.computeLuminanceColor(); |
| 75 | if (lcd) { |
| 76 | // This is the correct computation, but there are tons of cases where LCD can be overridden. |
| 77 | // For now we just regenerate if any run in a textblob has LCD. |
| 78 | // TODO figure out where all of these overrides are and see if we can incorporate that logic |
| 79 | // at a higher level *OR* use sRGB |
| 80 | SkASSERT(false); |
| 81 | //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor); |
| 82 | } else { |
| 83 | // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have |
| 84 | // gamma corrected masks anyways, nor color |
| 85 | U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor), |
| 86 | SkColorGetG(canonicalColor), |
| 87 | SkColorGetB(canonicalColor)); |
| 88 | // reduce to our finite number of bits |
| 89 | canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum)); |
| 90 | } |
| 91 | return canonicalColor; |
| 92 | } |
| 93 | |
| 94 | // TODO if this function ever shows up in profiling, then we can compute this value when the |
| 95 | // textblob is being built and cache it. However, for the time being textblobs mostly only have 1 |
| 96 | // run so this is not a big deal to compute here. |
| 97 | bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 98 | SkTextBlobRunIterator it(blob); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 99 | for (; !it.done(); it.next()) { |
| 100 | if (it.isLCD()) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 107 | inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 108 | const SkPaint& skPaint, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 109 | const SkMatrix* viewMatrix, |
| 110 | bool noGamma) { |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 111 | skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 112 | run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); |
| 113 | return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc()); |
| 114 | } |
| 115 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 116 | void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 117 | const GrClip& clip, const SkPaint& skPaint, |
| 118 | const SkMatrix& viewMatrix, const SkTextBlob* blob, |
| 119 | SkScalar x, SkScalar y, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 120 | SkDrawFilter* drawFilter, const SkIRect& clipBounds) { |
joshualitt | 9b8e79e | 2015-04-24 09:57:12 -0700 | [diff] [blame] | 121 | // If we have been abandoned, then don't draw |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 122 | if (fContext->abandoned()) { |
| 123 | return; |
| 124 | } |
| 125 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 126 | SkAutoTUnref<GrAtlasTextBlob> cacheBlob; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 127 | SkMaskFilter::BlurRec blurRec; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 128 | GrAtlasTextBlob::Key key; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 129 | // It might be worth caching these things, but its not clear at this time |
| 130 | // TODO for animated mask filters, this will fill up our cache. We need a safeguard here |
| 131 | const SkMaskFilter* mf = skPaint.getMaskFilter(); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 132 | bool canCache = !(skPaint.getPathEffect() || |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 133 | (mf && !mf->asABlur(&blurRec)) || |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 134 | drawFilter); |
| 135 | |
| 136 | if (canCache) { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 137 | bool hasLCD = HasLCD(blob); |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 138 | |
| 139 | // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 140 | SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() : |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 141 | kUnknown_SkPixelGeometry; |
| 142 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 143 | // TODO we want to figure out a way to be able to use the canonical color on LCD text, |
| 144 | // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to |
| 145 | // ensure we always match the same key |
| 146 | GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT : |
| 147 | ComputeCanonicalColor(skPaint, hasLCD); |
| 148 | |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 149 | key.fPixelGeometry = pixelGeometry; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 150 | key.fUniqueID = blob->uniqueID(); |
| 151 | key.fStyle = skPaint.getStyle(); |
| 152 | key.fHasBlur = SkToBool(mf); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 153 | key.fCanonicalColor = canonicalColor; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 154 | cacheBlob.reset(SkSafeRef(fCache->find(key))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 155 | } |
| 156 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 157 | SkScalar transX = 0.f; |
| 158 | SkScalar transY = 0.f; |
| 159 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 160 | // Though for the time being runs in the textblob can override the paint, they only touch font |
| 161 | // info. |
| 162 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 163 | if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) { |
bsalomon | bed83a6 | 2015-04-15 14:18:34 -0700 | [diff] [blame] | 164 | return; |
| 165 | } |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 166 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 167 | if (cacheBlob) { |
joshualitt | fd5f6c1 | 2015-12-10 07:44:50 -0800 | [diff] [blame] | 168 | if (cacheBlob->mustRegenerate(&transX, &transY, skPaint, grPaint.getColor(), blurRec, |
| 169 | viewMatrix, x, y)) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 170 | // We have to remake the blob because changes may invalidate our masks. |
| 171 | // TODO we could probably get away reuse most of the time if the pointer is unique, |
| 172 | // but we'd have to clear the subrun information |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 173 | fCache->remove(cacheBlob); |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 174 | cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint, |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 175 | GrAtlasTextBlob::kGrayTextVASize))); |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 176 | this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 177 | blob, x, y, drawFilter, clip); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 178 | } else { |
| 179 | fCache->makeMRU(cacheBlob); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 180 | #ifdef CACHE_SANITY_CHECK |
| 181 | { |
| 182 | int glyphCount = 0; |
| 183 | int runCount = 0; |
| 184 | GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob); |
| 185 | SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount, |
| 186 | kGrayTextVASize)); |
| 187 | GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint); |
| 188 | this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 189 | blob, x, y, drawFilter, clip); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 190 | GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob); |
| 191 | } |
| 192 | |
| 193 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 194 | } |
| 195 | } else { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 196 | if (canCache) { |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 197 | cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint, |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 198 | GrAtlasTextBlob::kGrayTextVASize))); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 199 | } else { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 200 | cacheBlob.reset(fCache->createBlob(blob, GrAtlasTextBlob::kGrayTextVASize)); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 201 | } |
robertphillips | 9c240a1 | 2015-05-28 07:45:59 -0700 | [diff] [blame] | 202 | this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 203 | blob, x, y, drawFilter, clip); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 204 | } |
| 205 | |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 206 | cacheBlob->flushCached(fContext, dc, blob, fSurfaceProps, fDistanceAdjustTable, skPaint, |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 207 | grPaint, drawFilter, clip, viewMatrix, clipBounds, x, y, transX, transY); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 208 | } |
| 209 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 210 | inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint, |
| 211 | const SkMatrix& viewMatrix) { |
| 212 | // TODO: support perspective (need getMaxScale replacement) |
| 213 | if (viewMatrix.hasPerspective()) { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 218 | SkScalar scaledTextSize = maxScale*skPaint.getTextSize(); |
| 219 | // Hinted text looks far better at small resolutions |
| 220 | // Scaling up beyond 2x yields undesireable artifacts |
jvanverth | 34d7288 | 2015-06-22 08:08:09 -0700 | [diff] [blame] | 221 | if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 225 | bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts(); |
robertphillips | bcd7ab5 | 2015-06-18 05:27:18 -0700 | [diff] [blame] | 226 | #if SK_FORCE_DISTANCE_FIELD_TEXT |
| 227 | useDFT = true; |
| 228 | #endif |
| 229 | |
jvanverth | 4854d13 | 2015-06-22 06:46:56 -0700 | [diff] [blame] | 230 | if (!useDFT && scaledTextSize < kLargeDFFontSize) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // rasterizers and mask filters modify alpha, which doesn't |
| 235 | // translate well to distance |
| 236 | if (skPaint.getRasterizer() || skPaint.getMaskFilter() || |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 237 | !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | |
| 241 | // TODO: add some stroking support |
| 242 | if (skPaint.getStyle() != SkPaint::kFill_Style) { |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 249 | void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob, |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 250 | const SkPaint& skPaint, GrColor color, |
| 251 | const SkMatrix& viewMatrix, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 252 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 253 | SkDrawFilter* drawFilter, |
| 254 | const GrClip& clip) { |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 255 | // The color here is the GrPaint color, and it is used to determine whether we |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 256 | // have to regenerate LCD text blobs. |
| 257 | // We use this color vs the SkPaint color because it has the colorfilter applied. |
| 258 | cacheBlob->fPaintColor = color; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 259 | cacheBlob->fViewMatrix = viewMatrix; |
| 260 | cacheBlob->fX = x; |
| 261 | cacheBlob->fY = y; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 262 | |
| 263 | // Regenerate textblob |
| 264 | SkPaint runPaint = skPaint; |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 265 | SkTextBlobRunIterator it(blob); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 266 | for (int run = 0; !it.done(); it.next(), run++) { |
| 267 | int glyphCount = it.glyphCount(); |
| 268 | size_t textLen = glyphCount * sizeof(uint16_t); |
| 269 | const SkPoint& offset = it.offset(); |
| 270 | // applyFontToPaint() always overwrites the exact same attributes, |
| 271 | // so it is safe to not re-seed the paint for this reason. |
| 272 | it.applyFontToPaint(&runPaint); |
| 273 | |
| 274 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 275 | // A false return from filter() means we should abort the current draw. |
| 276 | runPaint = skPaint; |
| 277 | continue; |
| 278 | } |
| 279 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 280 | runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 281 | |
joshualitt | 18b072d | 2015-12-07 12:26:12 -0800 | [diff] [blame] | 282 | cacheBlob->push_back_run(run); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 283 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 284 | if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) { |
| 285 | cacheBlob->setHasDistanceField(); |
| 286 | SkPaint dfPaint = runPaint; |
| 287 | SkScalar textRatio; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 288 | this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 289 | Run& runIdx = cacheBlob->fRuns[run]; |
| 290 | PerSubRunInfo& subRun = runIdx.fSubRunInfo.back(); |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 291 | subRun.setUseLCDText(runPaint.isLCDRenderText()); |
| 292 | subRun.setDrawAsDistanceFields(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 293 | |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 294 | SkTDArray<char> fallbackTxt; |
| 295 | SkTDArray<SkScalar> fallbackPos; |
| 296 | SkPoint dfOffset; |
| 297 | int scalarsPerPosition = 2; |
| 298 | switch (it.positioning()) { |
| 299 | case SkTextBlob::kDefault_Positioning: { |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 300 | this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 301 | (const char *)it.glyphs(), textLen, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 302 | x + offset.x(), y + offset.y(), textRatio, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 303 | &fallbackTxt, &fallbackPos, &dfOffset, runPaint); |
| 304 | break; |
| 305 | } |
| 306 | case SkTextBlob::kHorizontal_Positioning: { |
| 307 | scalarsPerPosition = 1; |
| 308 | dfOffset = SkPoint::Make(x, y + offset.y()); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 309 | this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 310 | (const char*)it.glyphs(), textLen, it.pos(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 311 | scalarsPerPosition, dfOffset, textRatio, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 312 | &fallbackTxt, &fallbackPos); |
| 313 | break; |
| 314 | } |
| 315 | case SkTextBlob::kFull_Positioning: { |
| 316 | dfOffset = SkPoint::Make(x, y); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 317 | this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 318 | (const char*)it.glyphs(), textLen, it.pos(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 319 | scalarsPerPosition, dfOffset, textRatio, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 320 | &fallbackTxt, &fallbackPos); |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 325 | this->fallbackDrawPosText(cacheBlob, run, clip, color, runPaint, viewMatrix, |
| 326 | fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 327 | } |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 328 | } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) { |
| 329 | cacheBlob->fRuns[run].fDrawAsPaths = true; |
| 330 | } else { |
| 331 | cacheBlob->setHasBitmap(); |
| 332 | SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix, |
| 333 | false); |
| 334 | switch (it.positioning()) { |
| 335 | case SkTextBlob::kDefault_Positioning: |
| 336 | this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 337 | (const char *)it.glyphs(), textLen, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 338 | x + offset.x(), y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 339 | break; |
| 340 | case SkTextBlob::kHorizontal_Positioning: |
| 341 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 342 | (const char*)it.glyphs(), textLen, it.pos(), 1, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 343 | SkPoint::Make(x, y + offset.y())); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 344 | break; |
| 345 | case SkTextBlob::kFull_Positioning: |
| 346 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 347 | (const char*)it.glyphs(), textLen, it.pos(), 2, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 348 | SkPoint::Make(x, y)); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 349 | break; |
| 350 | } |
| 351 | SkGlyphCache::AttachCache(cache); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | if (drawFilter) { |
| 355 | // A draw filter may change the paint arbitrarily, so we must re-seed in this case. |
| 356 | runPaint = skPaint; |
| 357 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 361 | inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob, |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 362 | SkPaint* skPaint, |
| 363 | SkScalar* textRatio, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 364 | const SkMatrix& viewMatrix) { |
| 365 | // getMaxScale doesn't support perspective, so neither do we at the moment |
| 366 | SkASSERT(!viewMatrix.hasPerspective()); |
| 367 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 368 | SkScalar textSize = skPaint->getTextSize(); |
| 369 | SkScalar scaledTextSize = textSize; |
| 370 | // if we have non-unity scale, we need to choose our base text size |
| 371 | // based on the SkPaint's text size multiplied by the max scale factor |
| 372 | // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)? |
| 373 | if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) { |
| 374 | scaledTextSize *= maxScale; |
| 375 | } |
| 376 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 377 | // We have three sizes of distance field text, and within each size 'bucket' there is a floor |
| 378 | // and ceiling. A scale outside of this range would require regenerating the distance fields |
| 379 | SkScalar dfMaskScaleFloor; |
| 380 | SkScalar dfMaskScaleCeil; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 381 | if (scaledTextSize <= kSmallDFFontLimit) { |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 382 | dfMaskScaleFloor = kMinDFFontSize; |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 383 | dfMaskScaleCeil = kSmallDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 384 | *textRatio = textSize / kSmallDFFontSize; |
| 385 | skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize)); |
| 386 | } else if (scaledTextSize <= kMediumDFFontLimit) { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 387 | dfMaskScaleFloor = kSmallDFFontLimit; |
| 388 | dfMaskScaleCeil = kMediumDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 389 | *textRatio = textSize / kMediumDFFontSize; |
| 390 | skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize)); |
| 391 | } else { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 392 | dfMaskScaleFloor = kMediumDFFontLimit; |
| 393 | dfMaskScaleCeil = kLargeDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 394 | *textRatio = textSize / kLargeDFFontSize; |
| 395 | skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize)); |
| 396 | } |
| 397 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 398 | // Because there can be multiple runs in the blob, we want the overall maxMinScale, and |
| 399 | // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale |
| 400 | // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can |
| 401 | // tolerate before we'd have to move to a large mip size. When we actually test these values |
| 402 | // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test |
| 403 | // against these values to decide if we can reuse or not(ie, will a given scale change our mip |
| 404 | // level) |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 405 | SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil); |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 406 | blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale); |
| 407 | blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale); |
| 408 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 409 | skPaint->setLCDRenderText(false); |
| 410 | skPaint->setAutohinted(false); |
| 411 | skPaint->setHinting(SkPaint::kNormal_Hinting); |
| 412 | skPaint->setSubpixelText(true); |
| 413 | } |
| 414 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 415 | inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 416 | int runIndex, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 417 | const GrClip& clip, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 418 | GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 419 | const SkPaint& skPaint, |
| 420 | const SkMatrix& viewMatrix, |
| 421 | const SkTDArray<char>& fallbackTxt, |
| 422 | const SkTDArray<SkScalar>& fallbackPos, |
| 423 | int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 424 | const SkPoint& offset) { |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 425 | SkASSERT(fallbackTxt.count()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 426 | blob->setHasBitmap(); |
| 427 | Run& run = blob->fRuns[runIndex]; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 428 | // Push back a new subrun to fill and set the override descriptor |
| 429 | run.push_back(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 430 | run.fOverrideDescriptor.reset(new SkAutoDescriptor); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 431 | skPaint.getScalerContextDescriptor(run.fOverrideDescriptor, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 432 | fSurfaceProps, &viewMatrix, false); |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 433 | SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface, |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 434 | run.fOverrideDescriptor->getDesc()); |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 435 | this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 436 | fallbackTxt.begin(), fallbackTxt.count(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 437 | fallbackPos.begin(), scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 438 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 439 | } |
| 440 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 441 | inline GrAtlasTextBlob* |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 442 | GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint, |
| 443 | const SkMatrix& viewMatrix, SkPaint* dfPaint, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 444 | SkScalar* textRatio) { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 445 | GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 446 | |
| 447 | *dfPaint = origPaint; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 448 | this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 449 | blob->fViewMatrix = viewMatrix; |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 450 | Run& run = blob->fRuns[0]; |
| 451 | PerSubRunInfo& subRun = run.fSubRunInfo.back(); |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 452 | subRun.setUseLCDText(origPaint.isLCDRenderText()); |
| 453 | subRun.setDrawAsDistanceFields(); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 454 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 455 | return blob; |
| 456 | } |
| 457 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 458 | inline GrAtlasTextBlob* |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 459 | GrAtlasTextContext::createDrawTextBlob(const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 460 | const GrPaint& paint, const SkPaint& skPaint, |
| 461 | const SkMatrix& viewMatrix, |
| 462 | const char text[], size_t byteLength, |
| 463 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 464 | int glyphCount = skPaint.countText(text, byteLength); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 465 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 466 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 467 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 468 | SkPaint dfPaint; |
| 469 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 470 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 471 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 472 | SkTDArray<char> fallbackTxt; |
| 473 | SkTDArray<SkScalar> fallbackPos; |
| 474 | SkPoint offset; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 475 | this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 476 | byteLength, x, y, textRatio, &fallbackTxt, &fallbackPos, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 477 | &offset, skPaint); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 478 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 479 | this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix, |
| 480 | fallbackTxt, fallbackPos, 2, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 481 | } |
| 482 | } else { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 483 | blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 484 | blob->fViewMatrix = viewMatrix; |
| 485 | |
| 486 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 487 | this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 488 | byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 489 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 490 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 491 | return blob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 492 | } |
| 493 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 494 | inline GrAtlasTextBlob* |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 495 | GrAtlasTextContext::createDrawPosTextBlob(const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 496 | const GrPaint& paint, const SkPaint& skPaint, |
| 497 | const SkMatrix& viewMatrix, |
| 498 | const char text[], size_t byteLength, |
| 499 | const SkScalar pos[], int scalarsPerPosition, |
| 500 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 501 | int glyphCount = skPaint.countText(text, byteLength); |
| 502 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 503 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 504 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 505 | SkPaint dfPaint; |
| 506 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 507 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 508 | |
| 509 | SkTDArray<char> fallbackTxt; |
| 510 | SkTDArray<SkScalar> fallbackPos; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 511 | this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 512 | byteLength, pos, scalarsPerPosition, offset, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 513 | textRatio, &fallbackTxt, &fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 514 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 515 | this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix, |
| 516 | fallbackTxt, fallbackPos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 517 | } |
| 518 | } else { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 519 | blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 520 | blob->fViewMatrix = viewMatrix; |
| 521 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 522 | this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 523 | byteLength, pos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 524 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 525 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 526 | return blob; |
| 527 | } |
| 528 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 529 | void GrAtlasTextContext::onDrawText(GrDrawContext* dc, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 530 | const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 531 | const GrPaint& paint, const SkPaint& skPaint, |
| 532 | const SkMatrix& viewMatrix, |
| 533 | const char text[], size_t byteLength, |
| 534 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 535 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 536 | this->createDrawTextBlob(clip, paint, skPaint, viewMatrix, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 537 | text, byteLength, x, y, regionClipBounds)); |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 538 | blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint, |
| 539 | clip, regionClipBounds); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 540 | } |
| 541 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 542 | void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 543 | const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 544 | const GrPaint& paint, const SkPaint& skPaint, |
| 545 | const SkMatrix& viewMatrix, |
| 546 | const char text[], size_t byteLength, |
| 547 | const SkScalar pos[], int scalarsPerPosition, |
| 548 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 549 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 550 | this->createDrawPosTextBlob(clip, paint, skPaint, viewMatrix, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 551 | text, byteLength, |
| 552 | pos, scalarsPerPosition, |
| 553 | offset, regionClipBounds)); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 554 | |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 555 | blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint, clip, |
| 556 | regionClipBounds); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 557 | } |
| 558 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 559 | void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 560 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 561 | GrColor color, |
| 562 | const SkMatrix& viewMatrix, |
| 563 | const char text[], size_t byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 564 | SkScalar x, SkScalar y) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 565 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 566 | |
| 567 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 568 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 572 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 573 | |
| 574 | // Get GrFontScaler from cache |
| 575 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 576 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 577 | SkFindAndPlaceGlyph::ProcessText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 578 | skPaint.getTextEncoding(), text, byteLength, |
| 579 | {x, y}, viewMatrix, skPaint.getTextAlign(), |
| 580 | cache, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 581 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 582 | position += rounding; |
| 583 | this->bmpAppendGlyph( |
| 584 | blob, runIndex, glyph, |
| 585 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 586 | color, fontScaler); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 587 | } |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 588 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 589 | } |
| 590 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 591 | void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 592 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 593 | GrColor color, |
| 594 | const SkMatrix& viewMatrix, |
| 595 | const char text[], size_t byteLength, |
| 596 | const SkScalar pos[], int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 597 | const SkPoint& offset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 598 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 599 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 600 | |
| 601 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 602 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 603 | return; |
| 604 | } |
| 605 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 606 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 607 | |
| 608 | // Get GrFontScaler from cache |
| 609 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 610 | |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 611 | SkFindAndPlaceGlyph::ProcessPosText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 612 | skPaint.getTextEncoding(), text, byteLength, |
| 613 | offset, viewMatrix, pos, scalarsPerPosition, |
| 614 | skPaint.getTextAlign(), cache, |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 615 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 616 | position += rounding; |
| 617 | this->bmpAppendGlyph( |
| 618 | blob, runIndex, glyph, |
| 619 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 620 | color, fontScaler); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 621 | } |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 622 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 623 | } |
| 624 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 625 | void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 626 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 627 | const SkMatrix& viewMatrix, |
| 628 | const char text[], size_t byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 629 | SkScalar x, SkScalar y, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 630 | SkScalar textRatio, |
| 631 | SkTDArray<char>* fallbackTxt, |
| 632 | SkTDArray<SkScalar>* fallbackPos, |
| 633 | SkPoint* offset, |
| 634 | const SkPaint& origPaint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 635 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 636 | |
| 637 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 638 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
| 642 | SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc(); |
| 643 | SkAutoDescriptor desc; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 644 | origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true); |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 645 | SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(), |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 646 | desc.getDesc()); |
| 647 | |
| 648 | SkTArray<SkScalar> positions; |
| 649 | |
| 650 | const char* textPtr = text; |
| 651 | SkFixed stopX = 0; |
| 652 | SkFixed stopY = 0; |
| 653 | SkFixed origin = 0; |
| 654 | switch (origPaint.getTextAlign()) { |
| 655 | case SkPaint::kRight_Align: origin = SK_Fixed1; break; |
| 656 | case SkPaint::kCenter_Align: origin = SK_FixedHalf; break; |
| 657 | case SkPaint::kLeft_Align: origin = 0; break; |
| 658 | } |
| 659 | |
| 660 | SkAutoKern autokern; |
| 661 | const char* stop = text + byteLength; |
| 662 | while (textPtr < stop) { |
| 663 | // don't need x, y here, since all subpixel variants will have the |
| 664 | // same advance |
| 665 | const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0); |
| 666 | |
| 667 | SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph); |
| 668 | positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width))); |
| 669 | |
| 670 | SkFixed height = glyph.fAdvanceY; |
| 671 | positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height))); |
| 672 | |
| 673 | stopX += width; |
| 674 | stopY += height; |
| 675 | } |
| 676 | SkASSERT(textPtr == stop); |
| 677 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 678 | SkGlyphCache::AttachCache(origPaintCache); |
| 679 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 680 | // now adjust starting point depending on alignment |
| 681 | SkScalar alignX = SkFixedToScalar(stopX); |
| 682 | SkScalar alignY = SkFixedToScalar(stopY); |
| 683 | if (origPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 684 | alignX = SkScalarHalf(alignX); |
| 685 | alignY = SkScalarHalf(alignY); |
| 686 | } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) { |
| 687 | alignX = 0; |
| 688 | alignY = 0; |
| 689 | } |
| 690 | x -= alignX; |
| 691 | y -= alignY; |
| 692 | *offset = SkPoint::Make(x, y); |
| 693 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 694 | this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 695 | positions.begin(), 2, *offset, textRatio, fallbackTxt, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 696 | fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 697 | } |
| 698 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 699 | void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 700 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 701 | const SkMatrix& viewMatrix, |
| 702 | const char text[], size_t byteLength, |
| 703 | const SkScalar pos[], int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 704 | const SkPoint& offset, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 705 | SkScalar textRatio, |
| 706 | SkTDArray<char>* fallbackTxt, |
| 707 | SkTDArray<SkScalar>* fallbackPos) { |
| 708 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 709 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 710 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 711 | |
| 712 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 713 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 714 | return; |
| 715 | } |
| 716 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 717 | fCurrStrike = nullptr; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 718 | |
| 719 | SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc(); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 720 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 721 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 722 | |
| 723 | const char* stop = text + byteLength; |
| 724 | |
| 725 | if (SkPaint::kLeft_Align == skPaint.getTextAlign()) { |
| 726 | while (text < stop) { |
| 727 | const char* lastText = text; |
| 728 | // the last 2 parameters are ignored |
| 729 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 730 | |
| 731 | if (glyph.fWidth) { |
| 732 | SkScalar x = offset.x() + pos[0]; |
| 733 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 734 | |
| 735 | if (!this->dfAppendGlyph(blob, |
| 736 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 737 | glyph, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 738 | x, y, color, fontScaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 739 | textRatio, viewMatrix)) { |
| 740 | // couldn't append, send to fallback |
| 741 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 742 | *fallbackPos->append() = pos[0]; |
| 743 | if (2 == scalarsPerPosition) { |
| 744 | *fallbackPos->append() = pos[1]; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | pos += scalarsPerPosition; |
| 749 | } |
| 750 | } else { |
| 751 | SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf |
| 752 | : SK_Scalar1; |
| 753 | while (text < stop) { |
| 754 | const char* lastText = text; |
| 755 | // the last 2 parameters are ignored |
| 756 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 757 | |
| 758 | if (glyph.fWidth) { |
| 759 | SkScalar x = offset.x() + pos[0]; |
| 760 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 761 | |
| 762 | SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio; |
| 763 | SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio; |
| 764 | |
| 765 | if (!this->dfAppendGlyph(blob, |
| 766 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 767 | glyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 768 | x - advanceX, y - advanceY, color, |
| 769 | fontScaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 770 | textRatio, |
| 771 | viewMatrix)) { |
| 772 | // couldn't append, send to fallback |
| 773 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 774 | *fallbackPos->append() = pos[0]; |
| 775 | if (2 == scalarsPerPosition) { |
| 776 | *fallbackPos->append() = pos[1]; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | pos += scalarsPerPosition; |
| 781 | } |
| 782 | } |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 783 | |
| 784 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 785 | } |
| 786 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 787 | void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 788 | const SkGlyph& skGlyph, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 789 | int vx, int vy, GrColor color, GrFontScaler* scaler) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 790 | if (!fCurrStrike) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 791 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 792 | } |
| 793 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 794 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 795 | skGlyph.getSubXFixed(), |
| 796 | skGlyph.getSubYFixed(), |
| 797 | GrGlyph::kCoverage_MaskStyle); |
| 798 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 799 | if (!glyph) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 800 | return; |
| 801 | } |
| 802 | |
| 803 | int x = vx + glyph->fBounds.fLeft; |
| 804 | int y = vy + glyph->fBounds.fTop; |
| 805 | |
| 806 | // keep them as ints until we've done the clip-test |
| 807 | int width = glyph->fBounds.width(); |
| 808 | int height = glyph->fBounds.height(); |
| 809 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 810 | SkRect r; |
| 811 | r.fLeft = SkIntToScalar(x); |
| 812 | r.fTop = SkIntToScalar(y); |
| 813 | r.fRight = r.fLeft + SkIntToScalar(width); |
| 814 | r.fBottom = r.fTop + SkIntToScalar(height); |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 815 | |
joshualitt | a06e6ab | 2015-12-10 08:54:41 -0800 | [diff] [blame] | 816 | blob->appendGlyph(runIndex, r, color, fCurrStrike, glyph, scaler, skGlyph, |
| 817 | SkIntToScalar(vx), SkIntToScalar(vy), 1.0f, false); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 818 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 819 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 820 | bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 821 | const SkGlyph& skGlyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 822 | SkScalar sx, SkScalar sy, GrColor color, |
| 823 | GrFontScaler* scaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 824 | SkScalar textRatio, const SkMatrix& viewMatrix) { |
| 825 | if (!fCurrStrike) { |
| 826 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 827 | } |
| 828 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 829 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 830 | skGlyph.getSubXFixed(), |
| 831 | skGlyph.getSubYFixed(), |
| 832 | GrGlyph::kDistance_MaskStyle); |
| 833 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 834 | if (!glyph) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 835 | return true; |
| 836 | } |
| 837 | |
| 838 | // fallback to color glyph support |
| 839 | if (kA8_GrMaskFormat != glyph->fMaskFormat) { |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset); |
| 844 | SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset); |
| 845 | SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset); |
| 846 | SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset); |
| 847 | |
| 848 | SkScalar scale = textRatio; |
| 849 | dx *= scale; |
| 850 | dy *= scale; |
| 851 | width *= scale; |
| 852 | height *= scale; |
| 853 | sx += dx; |
| 854 | sy += dy; |
| 855 | SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height); |
| 856 | |
joshualitt | a06e6ab | 2015-12-10 08:54:41 -0800 | [diff] [blame] | 857 | blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph, scaler, skGlyph, |
| 858 | sx - dx, sy - dy, scale, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 859 | return true; |
| 860 | } |
| 861 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 862 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 863 | |
| 864 | #ifdef GR_TEST_UTILS |
| 865 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 866 | DRAW_BATCH_TEST_DEFINE(TextBlobBatch) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 867 | static uint32_t gContextID = SK_InvalidGenID; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 868 | static GrAtlasTextContext* gTextContext = nullptr; |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 869 | static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 870 | |
| 871 | if (context->uniqueID() != gContextID) { |
| 872 | gContextID = context->uniqueID(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 873 | delete gTextContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 874 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 875 | // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly |
| 876 | // because we don't really want to have a gpu device here. |
| 877 | // We enable distance fields by twiddling a knob on the paint |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 878 | gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 879 | } |
| 880 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 881 | // Setup dummy SkPaint / GrPaint |
| 882 | GrColor color = GrRandomColor(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 883 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 884 | SkPaint skPaint; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 885 | skPaint.setColor(color); |
| 886 | skPaint.setLCDRenderText(random->nextBool()); |
| 887 | skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool()); |
| 888 | skPaint.setSubpixelText(random->nextBool()); |
| 889 | |
| 890 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 891 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 892 | SkFAIL("couldn't convert paint\n"); |
| 893 | } |
| 894 | |
| 895 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 896 | int textLen = (int)strlen(text); |
| 897 | |
| 898 | // Setup clip |
| 899 | GrClip clip; |
| 900 | SkIRect noClip = SkIRect::MakeLargest(); |
| 901 | |
| 902 | // right now we don't handle textblobs, nor do we handle drawPosText. Since we only |
| 903 | // intend to test the batch with this unit test, that is okay. |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 904 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 905 | gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 906 | static_cast<size_t>(textLen), 0, 0, noClip)); |
| 907 | |
| 908 | SkScalar transX = static_cast<SkScalar>(random->nextU()); |
| 909 | SkScalar transY = static_cast<SkScalar>(random->nextU()); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 910 | const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0]; |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 911 | return blob->createBatch(info, textLen, 0, 0, color, transX, transY, skPaint, |
| 912 | gSurfaceProps, gTextContext->dfAdjustTable(), |
| 913 | context->getBatchFontCache()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | #endif |