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