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 | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 9 | #include "GrBlurUtils.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 10 | #include "GrDrawContext.h" |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 12 | #include "GrFontScaler.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 13 | #include "GrStrokeInfo.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 14 | #include "GrTextBlobCache.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 15 | #include "GrTexturePriv.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 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 207 | this->flush(blob, cacheBlob, dc, skPaint, grPaint, drawFilter, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 208 | 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: |
| 337 | this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 338 | (const char *)it.glyphs(), textLen, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 339 | x + offset.x(), y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 340 | break; |
| 341 | case SkTextBlob::kHorizontal_Positioning: |
| 342 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 343 | (const char*)it.glyphs(), textLen, it.pos(), 1, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 344 | SkPoint::Make(x, y + offset.y())); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 345 | break; |
| 346 | case SkTextBlob::kFull_Positioning: |
| 347 | this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix, |
| 348 | (const char*)it.glyphs(), textLen, it.pos(), 2, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 349 | SkPoint::Make(x, y)); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | SkGlyphCache::AttachCache(cache); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (drawFilter) { |
| 356 | // A draw filter may change the paint arbitrarily, so we must re-seed in this case. |
| 357 | runPaint = skPaint; |
| 358 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 362 | inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob, |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 363 | SkPaint* skPaint, |
| 364 | SkScalar* textRatio, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 365 | const SkMatrix& viewMatrix) { |
| 366 | // getMaxScale doesn't support perspective, so neither do we at the moment |
| 367 | SkASSERT(!viewMatrix.hasPerspective()); |
| 368 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 369 | SkScalar textSize = skPaint->getTextSize(); |
| 370 | SkScalar scaledTextSize = textSize; |
| 371 | // if we have non-unity scale, we need to choose our base text size |
| 372 | // based on the SkPaint's text size multiplied by the max scale factor |
| 373 | // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)? |
| 374 | if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) { |
| 375 | scaledTextSize *= maxScale; |
| 376 | } |
| 377 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 378 | // We have three sizes of distance field text, and within each size 'bucket' there is a floor |
| 379 | // and ceiling. A scale outside of this range would require regenerating the distance fields |
| 380 | SkScalar dfMaskScaleFloor; |
| 381 | SkScalar dfMaskScaleCeil; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 382 | if (scaledTextSize <= kSmallDFFontLimit) { |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 383 | dfMaskScaleFloor = kMinDFFontSize; |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 384 | dfMaskScaleCeil = kSmallDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 385 | *textRatio = textSize / kSmallDFFontSize; |
| 386 | skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize)); |
| 387 | } else if (scaledTextSize <= kMediumDFFontLimit) { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 388 | dfMaskScaleFloor = kSmallDFFontLimit; |
| 389 | dfMaskScaleCeil = kMediumDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 390 | *textRatio = textSize / kMediumDFFontSize; |
| 391 | skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize)); |
| 392 | } else { |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 393 | dfMaskScaleFloor = kMediumDFFontLimit; |
| 394 | dfMaskScaleCeil = kLargeDFFontLimit; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 395 | *textRatio = textSize / kLargeDFFontSize; |
| 396 | skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize)); |
| 397 | } |
| 398 | |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 399 | // Because there can be multiple runs in the blob, we want the overall maxMinScale, and |
| 400 | // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale |
| 401 | // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can |
| 402 | // tolerate before we'd have to move to a large mip size. When we actually test these values |
| 403 | // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test |
| 404 | // against these values to decide if we can reuse or not(ie, will a given scale change our mip |
| 405 | // level) |
joshualitt | a7c6389 | 2015-04-21 13:24:37 -0700 | [diff] [blame] | 406 | SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil); |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 407 | blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale); |
| 408 | blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale); |
| 409 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 410 | skPaint->setLCDRenderText(false); |
| 411 | skPaint->setAutohinted(false); |
| 412 | skPaint->setHinting(SkPaint::kNormal_Hinting); |
| 413 | skPaint->setSubpixelText(true); |
| 414 | } |
| 415 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 416 | inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob, |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 417 | int runIndex, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 418 | const GrClip& clip, |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 419 | GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 420 | const SkPaint& skPaint, |
| 421 | const SkMatrix& viewMatrix, |
| 422 | const SkTDArray<char>& fallbackTxt, |
| 423 | const SkTDArray<SkScalar>& fallbackPos, |
| 424 | int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 425 | const SkPoint& offset) { |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 426 | SkASSERT(fallbackTxt.count()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 427 | blob->setHasBitmap(); |
| 428 | Run& run = blob->fRuns[runIndex]; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 429 | // Push back a new subrun to fill and set the override descriptor |
| 430 | run.push_back(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 431 | run.fOverrideDescriptor.reset(new SkAutoDescriptor); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 432 | skPaint.getScalerContextDescriptor(run.fOverrideDescriptor, |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 433 | fSurfaceProps, &viewMatrix, false); |
joshualitt | fec19e1 | 2015-04-17 10:32:32 -0700 | [diff] [blame] | 434 | SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface, |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 435 | run.fOverrideDescriptor->getDesc()); |
jvanverth | 0628a52 | 2015-08-18 07:44:22 -0700 | [diff] [blame] | 436 | this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 437 | fallbackTxt.begin(), fallbackTxt.count(), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 438 | fallbackPos.begin(), scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 439 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 440 | } |
| 441 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 442 | inline GrAtlasTextBlob* |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 443 | GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint, |
| 444 | const SkMatrix& viewMatrix, SkPaint* dfPaint, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 445 | SkScalar* textRatio) { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 446 | GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 447 | |
| 448 | *dfPaint = origPaint; |
joshualitt | 64c99cc | 2015-04-21 09:43:03 -0700 | [diff] [blame] | 449 | this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 450 | blob->fViewMatrix = viewMatrix; |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 451 | Run& run = blob->fRuns[0]; |
| 452 | PerSubRunInfo& subRun = run.fSubRunInfo.back(); |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 453 | subRun.setUseLCDText(origPaint.isLCDRenderText()); |
| 454 | subRun.setDrawAsDistanceFields(); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 455 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 456 | return blob; |
| 457 | } |
| 458 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 459 | inline GrAtlasTextBlob* |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 460 | GrAtlasTextContext::createDrawTextBlob(const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 461 | const GrPaint& paint, const SkPaint& skPaint, |
| 462 | const SkMatrix& viewMatrix, |
| 463 | const char text[], size_t byteLength, |
| 464 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 465 | int glyphCount = skPaint.countText(text, byteLength); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 466 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 467 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 468 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 469 | SkPaint dfPaint; |
| 470 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 471 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 472 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 473 | SkTDArray<char> fallbackTxt; |
| 474 | SkTDArray<SkScalar> fallbackPos; |
| 475 | SkPoint offset; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 476 | this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 477 | byteLength, x, y, textRatio, &fallbackTxt, &fallbackPos, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 478 | &offset, skPaint); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 479 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 480 | this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix, |
| 481 | fallbackTxt, fallbackPos, 2, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 482 | } |
| 483 | } else { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 484 | blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 485 | blob->fViewMatrix = viewMatrix; |
| 486 | |
| 487 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 488 | this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 489 | byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 490 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 491 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 492 | return blob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 493 | } |
| 494 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 495 | inline GrAtlasTextBlob* |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 496 | GrAtlasTextContext::createDrawPosTextBlob(const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 497 | const GrPaint& paint, const SkPaint& skPaint, |
| 498 | const SkMatrix& viewMatrix, |
| 499 | const char text[], size_t byteLength, |
| 500 | const SkScalar pos[], int scalarsPerPosition, |
| 501 | const SkPoint& offset, const SkIRect& regionClipBounds) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 502 | int glyphCount = skPaint.countText(text, byteLength); |
| 503 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 504 | GrAtlasTextBlob* blob; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 505 | if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) { |
| 506 | SkPaint dfPaint; |
| 507 | SkScalar textRatio; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 508 | blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 509 | |
| 510 | SkTDArray<char> fallbackTxt; |
| 511 | SkTDArray<SkScalar> fallbackPos; |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 512 | this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 513 | byteLength, pos, scalarsPerPosition, offset, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 514 | textRatio, &fallbackTxt, &fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 515 | if (fallbackTxt.count()) { |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 516 | this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix, |
| 517 | fallbackTxt, fallbackPos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 518 | } |
| 519 | } else { |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 520 | blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 521 | blob->fViewMatrix = viewMatrix; |
| 522 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false); |
| 523 | this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 524 | byteLength, pos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 525 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 526 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 527 | return blob; |
| 528 | } |
| 529 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 530 | void GrAtlasTextContext::onDrawText(GrDrawContext* dc, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 531 | const GrClip& clip, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 532 | const GrPaint& paint, const SkPaint& skPaint, |
| 533 | const SkMatrix& viewMatrix, |
| 534 | const char text[], size_t byteLength, |
| 535 | SkScalar x, SkScalar y, const SkIRect& regionClipBounds) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 536 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 537 | this->createDrawTextBlob(clip, paint, skPaint, viewMatrix, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 538 | text, byteLength, x, y, regionClipBounds)); |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 539 | this->flush(blob, dc, skPaint, paint, 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 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 555 | this->flush(blob, dc, skPaint, paint, clip, regionClipBounds); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 556 | } |
| 557 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 558 | void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 559 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 560 | GrColor color, |
| 561 | const SkMatrix& viewMatrix, |
| 562 | const char text[], size_t byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 563 | SkScalar x, SkScalar y) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 564 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 565 | |
| 566 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 567 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 568 | return; |
| 569 | } |
| 570 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 571 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 572 | |
| 573 | // Get GrFontScaler from cache |
| 574 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 575 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 576 | SkFindAndPlaceGlyph::ProcessText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 577 | skPaint.getTextEncoding(), text, byteLength, |
| 578 | {x, y}, viewMatrix, skPaint.getTextAlign(), |
| 579 | cache, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 580 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 581 | position += rounding; |
| 582 | this->bmpAppendGlyph( |
| 583 | blob, runIndex, glyph, |
| 584 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 585 | color, fontScaler); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 586 | } |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 587 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 588 | } |
| 589 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 590 | void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 591 | SkGlyphCache* cache, const SkPaint& skPaint, |
| 592 | GrColor color, |
| 593 | const SkMatrix& viewMatrix, |
| 594 | const char text[], size_t byteLength, |
| 595 | const SkScalar pos[], int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 596 | const SkPoint& offset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 597 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 598 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 599 | |
| 600 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 601 | if (text == nullptr || byteLength == 0) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 602 | return; |
| 603 | } |
| 604 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 605 | fCurrStrike = nullptr; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 606 | |
| 607 | // Get GrFontScaler from cache |
| 608 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 609 | |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 610 | SkFindAndPlaceGlyph::ProcessPosText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 611 | skPaint.getTextEncoding(), text, byteLength, |
| 612 | offset, viewMatrix, pos, scalarsPerPosition, |
| 613 | skPaint.getTextAlign(), cache, |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 614 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 615 | position += rounding; |
| 616 | this->bmpAppendGlyph( |
| 617 | blob, runIndex, glyph, |
| 618 | SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY), |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 619 | color, fontScaler); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 620 | } |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 621 | ); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 622 | } |
| 623 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 624 | void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 625 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 626 | const SkMatrix& viewMatrix, |
| 627 | const char text[], size_t byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 628 | SkScalar x, SkScalar y, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 629 | SkScalar textRatio, |
| 630 | SkTDArray<char>* fallbackTxt, |
| 631 | SkTDArray<SkScalar>* fallbackPos, |
| 632 | SkPoint* offset, |
| 633 | const SkPaint& origPaint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 634 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 635 | |
| 636 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 637 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 638 | return; |
| 639 | } |
| 640 | |
| 641 | SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc(); |
| 642 | SkAutoDescriptor desc; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 643 | origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true); |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 644 | SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(), |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 645 | desc.getDesc()); |
| 646 | |
| 647 | SkTArray<SkScalar> positions; |
| 648 | |
| 649 | const char* textPtr = text; |
| 650 | SkFixed stopX = 0; |
| 651 | SkFixed stopY = 0; |
| 652 | SkFixed origin = 0; |
| 653 | switch (origPaint.getTextAlign()) { |
| 654 | case SkPaint::kRight_Align: origin = SK_Fixed1; break; |
| 655 | case SkPaint::kCenter_Align: origin = SK_FixedHalf; break; |
| 656 | case SkPaint::kLeft_Align: origin = 0; break; |
| 657 | } |
| 658 | |
| 659 | SkAutoKern autokern; |
| 660 | const char* stop = text + byteLength; |
| 661 | while (textPtr < stop) { |
| 662 | // don't need x, y here, since all subpixel variants will have the |
| 663 | // same advance |
| 664 | const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0); |
| 665 | |
| 666 | SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph); |
| 667 | positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width))); |
| 668 | |
| 669 | SkFixed height = glyph.fAdvanceY; |
| 670 | positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height))); |
| 671 | |
| 672 | stopX += width; |
| 673 | stopY += height; |
| 674 | } |
| 675 | SkASSERT(textPtr == stop); |
| 676 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 677 | SkGlyphCache::AttachCache(origPaintCache); |
| 678 | |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 679 | // now adjust starting point depending on alignment |
| 680 | SkScalar alignX = SkFixedToScalar(stopX); |
| 681 | SkScalar alignY = SkFixedToScalar(stopY); |
| 682 | if (origPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 683 | alignX = SkScalarHalf(alignX); |
| 684 | alignY = SkScalarHalf(alignY); |
| 685 | } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) { |
| 686 | alignX = 0; |
| 687 | alignY = 0; |
| 688 | } |
| 689 | x -= alignX; |
| 690 | y -= alignY; |
| 691 | *offset = SkPoint::Make(x, y); |
| 692 | |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 693 | this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 694 | positions.begin(), 2, *offset, textRatio, fallbackTxt, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 695 | fallbackPos); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 696 | } |
| 697 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 698 | void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex, |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 699 | const SkPaint& skPaint, GrColor color, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 700 | const SkMatrix& viewMatrix, |
| 701 | const char text[], size_t byteLength, |
| 702 | const SkScalar pos[], int scalarsPerPosition, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 703 | const SkPoint& offset, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 704 | SkScalar textRatio, |
| 705 | SkTDArray<char>* fallbackTxt, |
| 706 | SkTDArray<SkScalar>* fallbackPos) { |
| 707 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 708 | SkASSERT(byteLength == 0 || text != nullptr); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 709 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 710 | |
| 711 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 712 | if (text == nullptr || byteLength == 0) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 713 | return; |
| 714 | } |
| 715 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 716 | fCurrStrike = nullptr; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 717 | |
| 718 | SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc(); |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 719 | SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 720 | GrFontScaler* fontScaler = GetGrFontScaler(cache); |
| 721 | |
| 722 | const char* stop = text + byteLength; |
| 723 | |
| 724 | if (SkPaint::kLeft_Align == skPaint.getTextAlign()) { |
| 725 | while (text < stop) { |
| 726 | const char* lastText = text; |
| 727 | // the last 2 parameters are ignored |
| 728 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 729 | |
| 730 | if (glyph.fWidth) { |
| 731 | SkScalar x = offset.x() + pos[0]; |
| 732 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 733 | |
| 734 | if (!this->dfAppendGlyph(blob, |
| 735 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 736 | glyph, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 737 | x, y, color, fontScaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 738 | textRatio, viewMatrix)) { |
| 739 | // couldn't append, send to fallback |
| 740 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 741 | *fallbackPos->append() = pos[0]; |
| 742 | if (2 == scalarsPerPosition) { |
| 743 | *fallbackPos->append() = pos[1]; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | pos += scalarsPerPosition; |
| 748 | } |
| 749 | } else { |
| 750 | SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf |
| 751 | : SK_Scalar1; |
| 752 | while (text < stop) { |
| 753 | const char* lastText = text; |
| 754 | // the last 2 parameters are ignored |
| 755 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 756 | |
| 757 | if (glyph.fWidth) { |
| 758 | SkScalar x = offset.x() + pos[0]; |
| 759 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
| 760 | |
| 761 | SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio; |
| 762 | SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio; |
| 763 | |
| 764 | if (!this->dfAppendGlyph(blob, |
| 765 | runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 766 | glyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 767 | x - advanceX, y - advanceY, color, |
| 768 | fontScaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 769 | textRatio, |
| 770 | viewMatrix)) { |
| 771 | // couldn't append, send to fallback |
| 772 | fallbackTxt->append(SkToInt(text-lastText), lastText); |
| 773 | *fallbackPos->append() = pos[0]; |
| 774 | if (2 == scalarsPerPosition) { |
| 775 | *fallbackPos->append() = pos[1]; |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | pos += scalarsPerPosition; |
| 780 | } |
| 781 | } |
jvanverth | 157e648 | 2015-09-09 08:05:12 -0700 | [diff] [blame] | 782 | |
| 783 | SkGlyphCache::AttachCache(cache); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 784 | } |
| 785 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 786 | void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 787 | const SkGlyph& skGlyph, |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 788 | int vx, int vy, GrColor color, GrFontScaler* scaler) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 789 | if (!fCurrStrike) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 790 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 791 | } |
| 792 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 793 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 794 | skGlyph.getSubXFixed(), |
| 795 | skGlyph.getSubYFixed(), |
| 796 | GrGlyph::kCoverage_MaskStyle); |
| 797 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 798 | if (!glyph) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 799 | return; |
| 800 | } |
| 801 | |
| 802 | int x = vx + glyph->fBounds.fLeft; |
| 803 | int y = vy + glyph->fBounds.fTop; |
| 804 | |
| 805 | // keep them as ints until we've done the clip-test |
| 806 | int width = glyph->fBounds.width(); |
| 807 | int height = glyph->fBounds.height(); |
| 808 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 809 | SkRect r; |
| 810 | r.fLeft = SkIntToScalar(x); |
| 811 | r.fTop = SkIntToScalar(y); |
| 812 | r.fRight = r.fLeft + SkIntToScalar(width); |
| 813 | r.fBottom = r.fTop + SkIntToScalar(height); |
joshualitt | f528e0d | 2015-12-09 06:42:52 -0800 | [diff] [blame] | 814 | |
joshualitt | a06e6ab | 2015-12-10 08:54:41 -0800 | [diff] [blame] | 815 | blob->appendGlyph(runIndex, r, color, fCurrStrike, glyph, scaler, skGlyph, |
| 816 | SkIntToScalar(vx), SkIntToScalar(vy), 1.0f, false); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 817 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 818 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 819 | bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 820 | const SkGlyph& skGlyph, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 821 | SkScalar sx, SkScalar sy, GrColor color, |
| 822 | GrFontScaler* scaler, |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 823 | SkScalar textRatio, const SkMatrix& viewMatrix) { |
| 824 | if (!fCurrStrike) { |
| 825 | fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler); |
| 826 | } |
| 827 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 828 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 829 | skGlyph.getSubXFixed(), |
| 830 | skGlyph.getSubYFixed(), |
| 831 | GrGlyph::kDistance_MaskStyle); |
| 832 | GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler); |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 833 | if (!glyph) { |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 834 | return true; |
| 835 | } |
| 836 | |
| 837 | // fallback to color glyph support |
| 838 | if (kA8_GrMaskFormat != glyph->fMaskFormat) { |
| 839 | return false; |
| 840 | } |
| 841 | |
| 842 | SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset); |
| 843 | SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset); |
| 844 | SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset); |
| 845 | SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset); |
| 846 | |
| 847 | SkScalar scale = textRatio; |
| 848 | dx *= scale; |
| 849 | dy *= scale; |
| 850 | width *= scale; |
| 851 | height *= scale; |
| 852 | sx += dx; |
| 853 | sy += dy; |
| 854 | SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height); |
| 855 | |
joshualitt | a06e6ab | 2015-12-10 08:54:41 -0800 | [diff] [blame] | 856 | blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph, scaler, skGlyph, |
| 857 | sx - dx, sy - dy, scale, true); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 858 | return true; |
| 859 | } |
| 860 | |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 861 | void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 862 | const SkTextBlobRunIterator& it, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 863 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 864 | SkDrawFilter* drawFilter, const SkMatrix& viewMatrix, |
| 865 | const SkIRect& clipBounds, SkScalar x, SkScalar y) { |
| 866 | SkPaint runPaint = skPaint; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 867 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 868 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 869 | const SkPoint& offset = it.offset(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 870 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 871 | it.applyFontToPaint(&runPaint); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 872 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 873 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 874 | return; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 875 | } |
| 876 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 877 | runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint)); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 878 | |
| 879 | switch (it.positioning()) { |
| 880 | case SkTextBlob::kDefault_Positioning: |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 881 | this->drawTextAsPath(dc, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 882 | (const char *)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 883 | textLen, x + offset.x(), y + offset.y(), clipBounds); |
| 884 | break; |
| 885 | case SkTextBlob::kHorizontal_Positioning: |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 886 | this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 887 | (const char*)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 888 | textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()), |
| 889 | clipBounds); |
| 890 | break; |
| 891 | case SkTextBlob::kFull_Positioning: |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 892 | this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 893 | (const char*)it.glyphs(), |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 894 | textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds); |
| 895 | break; |
| 896 | } |
| 897 | } |
| 898 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 899 | inline GrDrawBatch* |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 900 | GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 901 | int glyphCount, int run, int subRun, |
| 902 | GrColor color, SkScalar transX, SkScalar transY, |
| 903 | const SkPaint& skPaint) { |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 904 | GrMaskFormat format = info.maskFormat(); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 905 | GrColor subRunColor; |
| 906 | if (kARGB_GrMaskFormat == format) { |
| 907 | uint8_t paintAlpha = skPaint.getAlpha(); |
| 908 | subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha); |
| 909 | } else { |
| 910 | subRunColor = color; |
| 911 | } |
| 912 | |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 913 | GrAtlasTextBatch* batch; |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 914 | if (info.drawAsDistanceFields()) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 915 | SkColor filteredColor; |
| 916 | SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 917 | if (colorFilter) { |
| 918 | filteredColor = colorFilter->filterColor(skPaint.getColor()); |
| 919 | } else { |
| 920 | filteredColor = skPaint.getColor(); |
| 921 | } |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 922 | bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry()); |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 923 | batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(), |
| 924 | fDistanceAdjustTable, filteredColor, |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 925 | info.hasUseLCDText(), useBGR); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 926 | } else { |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 927 | batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 928 | } |
joshualitt | a751c97 | 2015-11-20 13:37:32 -0800 | [diff] [blame] | 929 | GrAtlasTextBatch::Geometry& geometry = batch->geometry(); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 930 | geometry.fBlob = SkRef(cacheBlob); |
| 931 | geometry.fRun = run; |
| 932 | geometry.fSubRun = subRun; |
| 933 | geometry.fColor = subRunColor; |
| 934 | geometry.fTransX = transX; |
| 935 | geometry.fTransY = transY; |
| 936 | batch->init(); |
| 937 | |
| 938 | return batch; |
| 939 | } |
| 940 | |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 941 | inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 942 | GrAtlasTextBlob* cacheBlob, int run, GrColor color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 943 | SkScalar transX, SkScalar transY, |
| 944 | const SkPaint& skPaint) { |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 945 | for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) { |
| 946 | const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun]; |
joshualitt | 3660d53 | 2015-12-07 11:32:50 -0800 | [diff] [blame] | 947 | int glyphCount = info.glyphCount(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 948 | if (0 == glyphCount) { |
| 949 | continue; |
| 950 | } |
| 951 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 952 | SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run, |
| 953 | subRun, color, transX, transY, |
| 954 | skPaint)); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 955 | dc->drawBatch(pipelineBuilder, batch); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 959 | inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob, |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 960 | GrDrawContext* dc, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 961 | const GrClip& clip, const SkPaint& skPaint, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 962 | SkScalar transX, SkScalar transY, |
| 963 | const SkIRect& clipBounds) { |
joshualitt | fc07256 | 2015-05-13 12:15:06 -0700 | [diff] [blame] | 964 | if (!cacheBlob->fBigGlyphs.count()) { |
| 965 | return; |
| 966 | } |
| 967 | |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 968 | for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) { |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 969 | GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i]; |
joshualitt | 19e4c02 | 2015-05-13 11:23:03 -0700 | [diff] [blame] | 970 | bigGlyph.fVx += transX; |
| 971 | bigGlyph.fVy += transY; |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 972 | SkMatrix ctm; |
| 973 | ctm.setScale(bigGlyph.fScale, bigGlyph.fScale); |
| 974 | ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy); |
| 975 | if (bigGlyph.fApplyVM) { |
| 976 | ctm.postConcat(cacheBlob->fViewMatrix); |
| 977 | } |
joshualitt | fc07256 | 2015-05-13 12:15:06 -0700 | [diff] [blame] | 978 | |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 979 | GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, bigGlyph.fPath, |
joshualitt | 0fe04a2 | 2015-08-25 12:05:50 -0700 | [diff] [blame] | 980 | skPaint, ctm, nullptr, clipBounds, false); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 981 | } |
| 982 | } |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 983 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 984 | void GrAtlasTextContext::flush(const SkTextBlob* blob, |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 985 | GrAtlasTextBlob* cacheBlob, |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 986 | GrDrawContext* dc, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 987 | const SkPaint& skPaint, |
| 988 | const GrPaint& grPaint, |
| 989 | SkDrawFilter* drawFilter, |
| 990 | const GrClip& clip, |
| 991 | const SkMatrix& viewMatrix, |
| 992 | const SkIRect& clipBounds, |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 993 | SkScalar x, SkScalar y, |
| 994 | SkScalar transX, SkScalar transY) { |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 995 | // We loop through the runs of the blob, flushing each. If any run is too large, then we flush |
| 996 | // it as paths |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 997 | GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 998 | |
| 999 | GrColor color = grPaint.getColor(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1000 | |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 1001 | SkTextBlobRunIterator it(blob); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1002 | for (int run = 0; !it.done(); it.next(), run++) { |
| 1003 | if (cacheBlob->fRuns[run].fDrawAsPaths) { |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1004 | this->flushRunAsPaths(dc, it, clip, skPaint, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1005 | drawFilter, viewMatrix, clipBounds, x, y); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1006 | continue; |
| 1007 | } |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 1008 | cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY); |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1009 | this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1010 | transX, transY, skPaint); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | // Now flush big glyphs |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1014 | this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, transX, transY, clipBounds); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1017 | void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1018 | GrDrawContext* dc, |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1019 | const SkPaint& skPaint, |
| 1020 | const GrPaint& grPaint, |
joshualitt | 1107e90 | 2015-05-11 14:52:11 -0700 | [diff] [blame] | 1021 | const GrClip& clip, |
| 1022 | const SkIRect& clipBounds) { |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 1023 | GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1024 | |
| 1025 | GrColor color = grPaint.getColor(); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1026 | for (int run = 0; run < cacheBlob->fRunCount; run++) { |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1027 | this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | // Now flush big glyphs |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 1031 | this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, 0, 0, clipBounds); |
joshualitt | 9a27e63 | 2015-04-06 10:53:36 -0700 | [diff] [blame] | 1032 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1033 | |
| 1034 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1035 | |
| 1036 | #ifdef GR_TEST_UTILS |
| 1037 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1038 | DRAW_BATCH_TEST_DEFINE(TextBlobBatch) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1039 | static uint32_t gContextID = SK_InvalidGenID; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1040 | static GrAtlasTextContext* gTextContext = nullptr; |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 1041 | static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1042 | |
| 1043 | if (context->uniqueID() != gContextID) { |
| 1044 | gContextID = context->uniqueID(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1045 | delete gTextContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1046 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1047 | // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly |
| 1048 | // because we don't really want to have a gpu device here. |
| 1049 | // We enable distance fields by twiddling a knob on the paint |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 1050 | gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1053 | // Setup dummy SkPaint / GrPaint |
| 1054 | GrColor color = GrRandomColor(random); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 1055 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1056 | SkPaint skPaint; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1057 | skPaint.setColor(color); |
| 1058 | skPaint.setLCDRenderText(random->nextBool()); |
| 1059 | skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool()); |
| 1060 | skPaint.setSubpixelText(random->nextBool()); |
| 1061 | |
| 1062 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 1063 | if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1064 | SkFAIL("couldn't convert paint\n"); |
| 1065 | } |
| 1066 | |
| 1067 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 1068 | int textLen = (int)strlen(text); |
| 1069 | |
| 1070 | // Setup clip |
| 1071 | GrClip clip; |
| 1072 | SkIRect noClip = SkIRect::MakeLargest(); |
| 1073 | |
| 1074 | // right now we don't handle textblobs, nor do we handle drawPosText. Since we only |
| 1075 | // intend to test the batch with this unit test, that is okay. |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1076 | SkAutoTUnref<GrAtlasTextBlob> blob( |
robertphillips | 6ee690e | 2015-12-02 08:57:50 -0800 | [diff] [blame] | 1077 | gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text, |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1078 | static_cast<size_t>(textLen), 0, 0, noClip)); |
| 1079 | |
| 1080 | SkScalar transX = static_cast<SkScalar>(random->nextU()); |
| 1081 | SkScalar transY = static_cast<SkScalar>(random->nextU()); |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 1082 | const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0]; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 1083 | return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint); |
| 1084 | } |
| 1085 | |
| 1086 | #endif |