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" |
robertphillips | 73c4e64 | 2016-03-02 11:36:59 -0800 | [diff] [blame] | 8 | #include "GrContext.h" |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 9 | #include "GrContextPriv.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 10 | #include "GrTextBlobCache.h" |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 11 | #include "SkDistanceFieldGen.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 12 | #include "SkDraw.h" |
| 13 | #include "SkDrawFilter.h" |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 14 | #include "SkFindAndPlaceGlyph.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 15 | #include "SkGr.h" |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 16 | #include "SkGraphics.h" |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 17 | #include "SkMakeUnique.h" |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 18 | #include "ops/GrMeshDrawOp.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 19 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 20 | // DF sizes and thresholds for usage of the small and medium sizes. For example, above |
| 21 | // kSmallDFFontLimit we will use the medium size. The large size is used up until the size at |
| 22 | // which we switch over to drawing as paths as controlled by Options. |
| 23 | static const int kSmallDFFontSize = 32; |
| 24 | static const int kSmallDFFontLimit = 32; |
| 25 | static const int kMediumDFFontSize = 72; |
| 26 | static const int kMediumDFFontLimit = 72; |
| 27 | static const int kLargeDFFontSize = 162; |
| 28 | |
| 29 | static const int kDefaultMinDistanceFieldFontSize = 18; |
| 30 | #ifdef SK_BUILD_FOR_ANDROID |
| 31 | static const int kDefaultMaxDistanceFieldFontSize = 384; |
| 32 | #else |
| 33 | static const int kDefaultMaxDistanceFieldFontSize = 2 * kLargeDFFontSize; |
| 34 | #endif |
| 35 | |
| 36 | GrAtlasTextContext::GrAtlasTextContext(const Options& options) |
| 37 | : fDistanceAdjustTable(new GrDistanceFieldAdjustTable) { |
| 38 | fMaxDistanceFieldFontSize = options.fMaxDistanceFieldFontSize < 0.f |
| 39 | ? kDefaultMaxDistanceFieldFontSize |
| 40 | : options.fMaxDistanceFieldFontSize; |
| 41 | fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize < 0.f |
| 42 | ? kDefaultMinDistanceFieldFontSize |
| 43 | : options.fMinDistanceFieldFontSize; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 44 | fDistanceFieldVerticesAlwaysHaveW = options.fDistanceFieldVerticesAlwaysHaveW; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 47 | std::unique_ptr<GrAtlasTextContext> GrAtlasTextContext::Make(const Options& options) { |
| 48 | return std::unique_ptr<GrAtlasTextContext>(new GrAtlasTextContext(options)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 51 | bool GrAtlasTextContext::canDraw(const GrAtlasGlyphCache* fontCache, |
| 52 | const SkPaint& skPaint, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 53 | const SkMatrix& viewMatrix, |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 54 | const SkSurfaceProps& props, |
| 55 | const GrShaderCaps& shaderCaps) { |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 56 | return this->canDrawAsDistanceFields(skPaint, viewMatrix, props, shaderCaps) || |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 57 | !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix, fontCache->getGlyphSizeLimit()); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 60 | SkColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) { |
| 61 | SkColor canonicalColor = paint.computeLuminanceColor(); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 62 | if (lcd) { |
| 63 | // This is the correct computation, but there are tons of cases where LCD can be overridden. |
| 64 | // For now we just regenerate if any run in a textblob has LCD. |
| 65 | // TODO figure out where all of these overrides are and see if we can incorporate that logic |
| 66 | // at a higher level *OR* use sRGB |
| 67 | SkASSERT(false); |
| 68 | //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor); |
| 69 | } else { |
| 70 | // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have |
| 71 | // gamma corrected masks anyways, nor color |
| 72 | U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor), |
| 73 | SkColorGetG(canonicalColor), |
| 74 | SkColorGetB(canonicalColor)); |
| 75 | // reduce to our finite number of bits |
| 76 | canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum)); |
| 77 | } |
| 78 | return canonicalColor; |
| 79 | } |
| 80 | |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 81 | uint32_t GrAtlasTextContext::ComputeScalerContextFlags(const GrColorSpaceInfo& colorSpaceInfo) { |
brianosman | 5280dcb | 2016-04-14 06:02:59 -0700 | [diff] [blame] | 82 | // If we're doing gamma-correct rendering, then we can disable the gamma hacks. |
| 83 | // Otherwise, leave them on. In either case, we still want the contrast boost: |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 84 | if (colorSpaceInfo.isGammaCorrect()) { |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 85 | return SkPaint::kBoostContrast_ScalerContextFlag; |
brianosman | 32f7782 | 2016-04-07 06:25:45 -0700 | [diff] [blame] | 86 | } else { |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 87 | return SkPaint::kFakeGammaAndBoostContrast_ScalerContextFlags; |
brianosman | 32f7782 | 2016-04-07 06:25:45 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 91 | // TODO if this function ever shows up in profiling, then we can compute this value when the |
| 92 | // textblob is being built and cache it. However, for the time being textblobs mostly only have 1 |
| 93 | // run so this is not a big deal to compute here. |
| 94 | bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 95 | SkTextBlobRunIterator it(blob); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 96 | for (; !it.done(); it.next()) { |
| 97 | if (it.isLCD()) { |
| 98 | return true; |
| 99 | } |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 104 | void GrAtlasTextContext::drawTextBlob(GrContext* context, GrTextUtils::Target* target, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 105 | const GrClip& clip, const SkPaint& skPaint, |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 106 | const SkMatrix& viewMatrix, const SkSurfaceProps& props, |
| 107 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 108 | SkDrawFilter* drawFilter, const SkIRect& clipBounds) { |
joshualitt | 9b8e79e | 2015-04-24 09:57:12 -0700 | [diff] [blame] | 109 | // If we have been abandoned, then don't draw |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 110 | if (context->abandoned()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 114 | sk_sp<GrAtlasTextBlob> cacheBlob; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 115 | SkMaskFilter::BlurRec blurRec; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 116 | GrAtlasTextBlob::Key key; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 117 | // It might be worth caching these things, but its not clear at this time |
| 118 | // TODO for animated mask filters, this will fill up our cache. We need a safeguard here |
| 119 | const SkMaskFilter* mf = skPaint.getMaskFilter(); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 120 | bool canCache = !(skPaint.getPathEffect() || |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 121 | (mf && !mf->asABlur(&blurRec)) || |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 122 | drawFilter); |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 123 | uint32_t scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo()); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 124 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 125 | auto atlasGlyphCache = context->contextPriv().getAtlasGlyphCache(); |
| 126 | GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache(); |
| 127 | |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 128 | if (canCache) { |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 129 | bool hasLCD = HasLCD(blob); |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 130 | |
| 131 | // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 132 | SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() : |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 133 | kUnknown_SkPixelGeometry; |
| 134 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 135 | // TODO we want to figure out a way to be able to use the canonical color on LCD text, |
| 136 | // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to |
| 137 | // ensure we always match the same key |
| 138 | GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT : |
| 139 | ComputeCanonicalColor(skPaint, hasLCD); |
| 140 | |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 141 | key.fPixelGeometry = pixelGeometry; |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 142 | key.fUniqueID = blob->uniqueID(); |
| 143 | key.fStyle = skPaint.getStyle(); |
| 144 | key.fHasBlur = SkToBool(mf); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 145 | key.fCanonicalColor = canonicalColor; |
brianosman | 8d7ffce | 2016-04-21 08:29:06 -0700 | [diff] [blame] | 146 | key.fScalerContextFlags = scalerContextFlags; |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 147 | cacheBlob = textBlobCache->find(key); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 150 | GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo()); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 151 | if (cacheBlob) { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 152 | if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, x, y)) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 153 | // We have to remake the blob because changes may invalidate our masks. |
| 154 | // TODO we could probably get away reuse most of the time if the pointer is unique, |
| 155 | // but we'd have to clear the subrun information |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 156 | textBlobCache->remove(cacheBlob.get()); |
| 157 | cacheBlob = textBlobCache->makeCachedBlob(blob, key, blurRec, skPaint); |
| 158 | this->regenerateTextBlob(cacheBlob.get(), atlasGlyphCache, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 159 | *context->caps()->shaderCaps(), paint, scalerContextFlags, |
| 160 | viewMatrix, props, blob, x, y, drawFilter); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 161 | } else { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 162 | textBlobCache->makeMRU(cacheBlob.get()); |
joshualitt | 2f2ee83 | 2016-02-10 08:52:24 -0800 | [diff] [blame] | 163 | |
| 164 | if (CACHE_SANITY_CHECK) { |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 165 | int glyphCount = 0; |
| 166 | int runCount = 0; |
| 167 | GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 168 | sk_sp<GrAtlasTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount)); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 169 | sanityBlob->setupKey(key, blurRec, skPaint); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 170 | this->regenerateTextBlob(sanityBlob.get(), atlasGlyphCache, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 171 | *context->caps()->shaderCaps(), paint, scalerContextFlags, |
| 172 | viewMatrix, props, blob, x, y, drawFilter); |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 173 | GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob); |
| 174 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 175 | } |
| 176 | } else { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 177 | if (canCache) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 178 | cacheBlob = textBlobCache->makeCachedBlob(blob, key, blurRec, skPaint); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 179 | } else { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 180 | cacheBlob = textBlobCache->makeBlob(blob); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 181 | } |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 182 | this->regenerateTextBlob(cacheBlob.get(), atlasGlyphCache, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 183 | *context->caps()->shaderCaps(), paint, scalerContextFlags, |
| 184 | viewMatrix, props, blob, x, y, drawFilter); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 187 | cacheBlob->flushCached(atlasGlyphCache, target, blob, props, fDistanceAdjustTable.get(), paint, |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 188 | drawFilter, clip, viewMatrix, clipBounds, x, y); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 191 | void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob, |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 192 | GrAtlasGlyphCache* fontCache, |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 193 | const GrShaderCaps& shaderCaps, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 194 | const GrTextUtils::Paint& paint, |
Jim Van Verth | 58c3cce | 2017-10-19 15:50:24 -0400 | [diff] [blame] | 195 | uint32_t scalerContextFlags, |
| 196 | const SkMatrix& viewMatrix, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 197 | const SkSurfaceProps& props, const SkTextBlob* blob, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 198 | SkScalar x, SkScalar y, |
| 199 | SkDrawFilter* drawFilter) const { |
Jim Van Verth | bc2cdd1 | 2017-06-08 11:14:35 -0400 | [diff] [blame] | 200 | cacheBlob->initReusableBlob(paint.luminanceColor(), viewMatrix, x, y); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 201 | |
| 202 | // Regenerate textblob |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 203 | SkTextBlobRunIterator it(blob); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 204 | GrTextUtils::RunPaint runPaint(&paint, drawFilter, props); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 205 | for (int run = 0; !it.done(); it.next(), run++) { |
| 206 | int glyphCount = it.glyphCount(); |
| 207 | size_t textLen = glyphCount * sizeof(uint16_t); |
| 208 | const SkPoint& offset = it.offset(); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 209 | cacheBlob->push_back_run(run); |
| 210 | if (!runPaint.modifyForRun(it)) { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 211 | continue; |
| 212 | } |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 213 | if (this->canDrawAsDistanceFields(runPaint, viewMatrix, props, shaderCaps)) { |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 214 | switch (it.positioning()) { |
| 215 | case SkTextBlob::kDefault_Positioning: { |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 216 | this->drawDFText(cacheBlob, run, fontCache, props, runPaint, scalerContextFlags, |
| 217 | viewMatrix, (const char*)it.glyphs(), textLen, x + offset.x(), |
| 218 | y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | case SkTextBlob::kHorizontal_Positioning: { |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 222 | SkPoint dfOffset = SkPoint::Make(x, y + offset.y()); |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 223 | this->drawDFPosText(cacheBlob, run, fontCache, props, runPaint, |
| 224 | scalerContextFlags, viewMatrix, (const char*)it.glyphs(), |
| 225 | textLen, it.pos(), 1, dfOffset); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | case SkTextBlob::kFull_Positioning: { |
joshualitt | 5425a9a | 2015-12-11 11:05:43 -0800 | [diff] [blame] | 229 | SkPoint dfOffset = SkPoint::Make(x, y); |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 230 | this->drawDFPosText(cacheBlob, run, fontCache, props, runPaint, |
| 231 | scalerContextFlags, viewMatrix, (const char*)it.glyphs(), |
| 232 | textLen, it.pos(), 2, dfOffset); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | } |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 236 | } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) { |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 237 | cacheBlob->setRunTooBigForAtlas(run); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 238 | } else { |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 239 | switch (it.positioning()) { |
| 240 | case SkTextBlob::kDefault_Positioning: |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 241 | DrawBmpText(cacheBlob, run, fontCache, props, runPaint, scalerContextFlags, |
| 242 | viewMatrix, (const char*)it.glyphs(), textLen, x + offset.x(), |
| 243 | y + offset.y()); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 244 | break; |
| 245 | case SkTextBlob::kHorizontal_Positioning: |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 246 | DrawBmpPosText(cacheBlob, run, fontCache, props, runPaint, scalerContextFlags, |
| 247 | viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 1, |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 248 | SkPoint::Make(x, y + offset.y()), SK_Scalar1); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 249 | break; |
| 250 | case SkTextBlob::kFull_Positioning: |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 251 | DrawBmpPosText(cacheBlob, run, fontCache, props, runPaint, scalerContextFlags, |
| 252 | viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 2, |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 253 | SkPoint::Make(x, y), SK_Scalar1); |
joshualitt | fcfb9fc | 2015-04-21 07:35:10 -0700 | [diff] [blame] | 254 | break; |
| 255 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 256 | } |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 260 | inline sk_sp<GrAtlasTextBlob> |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 261 | GrAtlasTextContext::makeDrawTextBlob(GrTextBlobCache* blobCache, |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 262 | GrAtlasGlyphCache* fontCache, |
| 263 | const GrShaderCaps& shaderCaps, |
| 264 | const GrTextUtils::Paint& paint, |
| 265 | uint32_t scalerContextFlags, |
| 266 | const SkMatrix& viewMatrix, |
| 267 | const SkSurfaceProps& props, |
| 268 | const char text[], size_t byteLength, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 269 | SkScalar x, SkScalar y) const { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 270 | int glyphCount = paint.skPaint().countText(text, byteLength); |
Brian Salomon | b3f6ac4 | 2017-07-31 08:00:25 -0400 | [diff] [blame] | 271 | if (!glyphCount) { |
| 272 | return nullptr; |
| 273 | } |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 274 | sk_sp<GrAtlasTextBlob> blob = blobCache->makeBlob(glyphCount, 1); |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 275 | blob->initThrowawayBlob(viewMatrix, x, y); |
joshualitt | a6bf4c5 | 2016-01-19 06:59:29 -0800 | [diff] [blame] | 276 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 277 | if (this->canDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps)) { |
| 278 | this->drawDFText(blob.get(), 0, fontCache, props, paint, scalerContextFlags, viewMatrix, |
| 279 | text, byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 280 | } else { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 281 | DrawBmpText(blob.get(), 0, fontCache, props, paint, scalerContextFlags, viewMatrix, text, |
| 282 | byteLength, x, y); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 283 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 284 | return blob; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 287 | inline sk_sp<GrAtlasTextBlob> |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 288 | GrAtlasTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache, |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 289 | GrAtlasGlyphCache* fontCache, |
| 290 | const GrShaderCaps& shaderCaps, |
| 291 | const GrTextUtils::Paint& paint, |
| 292 | uint32_t scalerContextFlags, |
| 293 | const SkMatrix& viewMatrix, |
| 294 | const SkSurfaceProps& props, |
| 295 | const char text[], size_t byteLength, |
| 296 | const SkScalar pos[], int scalarsPerPosition, const |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 297 | SkPoint& offset) const { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 298 | int glyphCount = paint.skPaint().countText(text, byteLength); |
Brian Salomon | b3f6ac4 | 2017-07-31 08:00:25 -0400 | [diff] [blame] | 299 | if (!glyphCount) { |
| 300 | return nullptr; |
| 301 | } |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 302 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 303 | sk_sp<GrAtlasTextBlob> blob = blobCache->makeBlob(glyphCount, 1); |
joshualitt | 7481e75 | 2016-01-22 06:08:48 -0800 | [diff] [blame] | 304 | blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y()); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 305 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 306 | if (this->canDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps)) { |
| 307 | this->drawDFPosText(blob.get(), 0, fontCache, props, paint, scalerContextFlags, viewMatrix, |
| 308 | text, byteLength, pos, scalarsPerPosition, offset); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 309 | } else { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 310 | DrawBmpPosText(blob.get(), 0, fontCache, props, paint, scalerContextFlags, viewMatrix, text, |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 311 | byteLength, pos, scalarsPerPosition, offset, SK_Scalar1); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 312 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 313 | return blob; |
| 314 | } |
| 315 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 316 | void GrAtlasTextContext::drawText(GrContext* context, GrTextUtils::Target* target, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 317 | const GrClip& clip, const SkPaint& skPaint, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 318 | const SkMatrix& viewMatrix, const SkSurfaceProps& props, |
| 319 | const char text[], size_t byteLength, SkScalar x, SkScalar y, |
| 320 | const SkIRect& regionClipBounds) { |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 321 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 322 | return; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 323 | } |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 324 | |
| 325 | auto atlasGlyphCache = context->contextPriv().getAtlasGlyphCache(); |
| 326 | auto textBlobCache = context->contextPriv().getTextBlobCache(); |
| 327 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 328 | GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo()); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 329 | if (this->canDraw(atlasGlyphCache, skPaint, viewMatrix, props, |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 330 | *context->caps()->shaderCaps())) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 331 | sk_sp<GrAtlasTextBlob> blob( |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 332 | this->makeDrawTextBlob(textBlobCache, atlasGlyphCache, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 333 | *context->caps()->shaderCaps(), paint, |
| 334 | ComputeScalerContextFlags(target->colorSpaceInfo()), |
| 335 | viewMatrix, props, text, byteLength, x, y)); |
Brian Salomon | b3f6ac4 | 2017-07-31 08:00:25 -0400 | [diff] [blame] | 336 | if (blob) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 337 | blob->flushThrowaway(atlasGlyphCache, target, props, fDistanceAdjustTable.get(), paint, |
| 338 | clip, viewMatrix, regionClipBounds, x, y); |
Brian Salomon | b3f6ac4 | 2017-07-31 08:00:25 -0400 | [diff] [blame] | 339 | } |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 343 | // fall back to drawing as a path or scaled glyph |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 344 | GrTextUtils::DrawBigText(target, clip, paint, viewMatrix, text, byteLength, x, y, |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 345 | regionClipBounds); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 348 | void GrAtlasTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 349 | const GrClip& clip, const SkPaint& skPaint, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 350 | const SkMatrix& viewMatrix, const SkSurfaceProps& props, |
| 351 | const char text[], size_t byteLength, const SkScalar pos[], |
| 352 | int scalarsPerPosition, const SkPoint& offset, |
| 353 | const SkIRect& regionClipBounds) { |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 354 | GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo()); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 355 | if (context->abandoned()) { |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 356 | return; |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 357 | } |
| 358 | |
| 359 | auto atlasGlyphCache = context->contextPriv().getAtlasGlyphCache(); |
| 360 | auto textBlobCache = context->contextPriv().getTextBlobCache(); |
| 361 | |
| 362 | if (this->canDraw(atlasGlyphCache, skPaint, viewMatrix, props, |
| 363 | *context->caps()->shaderCaps())) { |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 364 | sk_sp<GrAtlasTextBlob> blob(this->makeDrawPosTextBlob( |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 365 | textBlobCache, atlasGlyphCache, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 366 | *context->caps()->shaderCaps(), paint, |
| 367 | ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text, |
| 368 | byteLength, pos, scalarsPerPosition, offset)); |
Brian Salomon | b3f6ac4 | 2017-07-31 08:00:25 -0400 | [diff] [blame] | 369 | if (blob) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 370 | blob->flushThrowaway(atlasGlyphCache, target, props, fDistanceAdjustTable.get(), paint, |
| 371 | clip, viewMatrix, regionClipBounds, offset.fX, offset.fY); |
Brian Salomon | b3f6ac4 | 2017-07-31 08:00:25 -0400 | [diff] [blame] | 372 | } |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 373 | return; |
| 374 | } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 375 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 376 | // fall back to drawing as a path or scaled glyph |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 377 | GrTextUtils::DrawBigPosText(target, props, clip, paint, viewMatrix, text, |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 378 | byteLength, pos, scalarsPerPosition, offset, regionClipBounds); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 381 | void GrAtlasTextContext::DrawBmpText(GrAtlasTextBlob* blob, int runIndex, |
| 382 | GrAtlasGlyphCache* fontCache, const SkSurfaceProps& props, |
| 383 | const GrTextUtils::Paint& paint, uint32_t scalerContextFlags, |
| 384 | const SkMatrix& viewMatrix, const char text[], |
| 385 | size_t byteLength, SkScalar x, SkScalar y) { |
| 386 | SkASSERT(byteLength == 0 || text != nullptr); |
| 387 | |
| 388 | // nothing to draw |
| 389 | if (text == nullptr || byteLength == 0) { |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | // Ensure the blob is set for bitmaptext |
| 394 | blob->setHasBitmap(); |
| 395 | |
| 396 | GrAtlasTextStrike* currStrike = nullptr; |
| 397 | |
| 398 | SkGlyphCache* cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix); |
| 399 | SkFindAndPlaceGlyph::ProcessText(paint.skPaint().getTextEncoding(), text, byteLength, {x, y}, |
| 400 | viewMatrix, paint.skPaint().getTextAlign(), cache, |
| 401 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 402 | position += rounding; |
| 403 | BmpAppendGlyph(blob, runIndex, fontCache, &currStrike, |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 404 | glyph, SkScalarFloorToScalar(position.fX), |
| 405 | SkScalarFloorToScalar(position.fY), |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 406 | paint.filteredPremulColor(), cache, |
| 407 | SK_Scalar1); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 408 | }); |
| 409 | |
| 410 | SkGlyphCache::AttachCache(cache); |
| 411 | } |
| 412 | |
| 413 | void GrAtlasTextContext::DrawBmpPosText(GrAtlasTextBlob* blob, int runIndex, |
| 414 | GrAtlasGlyphCache* fontCache, const SkSurfaceProps& props, |
| 415 | const GrTextUtils::Paint& paint, |
| 416 | uint32_t scalerContextFlags, const SkMatrix& viewMatrix, |
| 417 | const char text[], size_t byteLength, const SkScalar pos[], |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 418 | int scalarsPerPosition, const SkPoint& offset, |
| 419 | SkScalar textRatio) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 420 | SkASSERT(byteLength == 0 || text != nullptr); |
| 421 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 422 | |
| 423 | // nothing to draw |
| 424 | if (text == nullptr || byteLength == 0) { |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | // Ensure the blob is set for bitmaptext |
| 429 | blob->setHasBitmap(); |
| 430 | |
| 431 | GrAtlasTextStrike* currStrike = nullptr; |
| 432 | |
| 433 | SkGlyphCache* cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix); |
| 434 | |
| 435 | SkFindAndPlaceGlyph::ProcessPosText( |
| 436 | paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos, |
| 437 | scalarsPerPosition, paint.skPaint().getTextAlign(), cache, |
| 438 | [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 439 | position += rounding; |
| 440 | BmpAppendGlyph(blob, runIndex, fontCache, &currStrike, glyph, |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 441 | SkScalarFloorToScalar(position.fX), |
| 442 | SkScalarFloorToScalar(position.fY), |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 443 | paint.filteredPremulColor(), cache, textRatio); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 444 | }); |
| 445 | |
| 446 | SkGlyphCache::AttachCache(cache); |
| 447 | } |
| 448 | |
| 449 | void GrAtlasTextContext::BmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
| 450 | GrAtlasGlyphCache* fontCache, GrAtlasTextStrike** strike, |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 451 | const SkGlyph& skGlyph, SkScalar sx, SkScalar sy, |
| 452 | GrColor color, SkGlyphCache* glyphCache, |
| 453 | SkScalar textRatio) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 454 | if (!*strike) { |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 455 | *strike = fontCache->getStrike(glyphCache); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 459 | skGlyph.getSubXFixed(), |
| 460 | skGlyph.getSubYFixed(), |
| 461 | GrGlyph::kCoverage_MaskStyle); |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 462 | GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, glyphCache); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 463 | if (!glyph) { |
| 464 | return; |
| 465 | } |
| 466 | |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 467 | SkASSERT(skGlyph.fWidth == glyph->width()); |
| 468 | SkASSERT(skGlyph.fHeight == glyph->height()); |
| 469 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 470 | SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft); |
| 471 | SkScalar dy = SkIntToScalar(glyph->fBounds.fTop); |
| 472 | SkScalar width = SkIntToScalar(glyph->fBounds.width()); |
| 473 | SkScalar height = SkIntToScalar(glyph->fBounds.height()); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 474 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 475 | dx *= textRatio; |
| 476 | dy *= textRatio; |
| 477 | width *= textRatio; |
| 478 | height *= textRatio; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 479 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 480 | SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 481 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 482 | blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, glyphCache, skGlyph, sx, sy, |
| 483 | textRatio, true); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 486 | bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix, |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 487 | const SkSurfaceProps& props, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 488 | const GrShaderCaps& caps) const { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 489 | if (!viewMatrix.hasPerspective()) { |
| 490 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 491 | SkScalar scaledTextSize = maxScale * skPaint.getTextSize(); |
| 492 | // Hinted text looks far better at small resolutions |
| 493 | // Scaling up beyond 2x yields undesireable artifacts |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 494 | if (scaledTextSize < fMinDistanceFieldFontSize || |
| 495 | scaledTextSize > fMaxDistanceFieldFontSize) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 496 | return false; |
| 497 | } |
| 498 | |
| 499 | bool useDFT = props.isUseDeviceIndependentFonts(); |
| 500 | #if SK_FORCE_DISTANCE_FIELD_TEXT |
| 501 | useDFT = true; |
| 502 | #endif |
| 503 | |
| 504 | if (!useDFT && scaledTextSize < kLargeDFFontSize) { |
| 505 | return false; |
| 506 | } |
| 507 | } |
| 508 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 509 | // mask filters modify alpha, which doesn't translate well to distance |
| 510 | if (skPaint.getMaskFilter() || !caps.shaderDerivativeSupport()) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | |
| 514 | // TODO: add some stroking support |
| 515 | if (skPaint.getStyle() != SkPaint::kFill_Style) { |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | return true; |
| 520 | } |
| 521 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 522 | void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob, |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 523 | SkPaint* skPaint, |
| 524 | SkScalar* textRatio, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 525 | const SkMatrix& viewMatrix) const { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 526 | SkScalar textSize = skPaint->getTextSize(); |
| 527 | SkScalar scaledTextSize = textSize; |
| 528 | |
| 529 | if (viewMatrix.hasPerspective()) { |
| 530 | // for perspective, we simply force to the medium size |
| 531 | // TODO: compute a size based on approximate screen area |
| 532 | scaledTextSize = kMediumDFFontLimit; |
| 533 | } else { |
| 534 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 535 | // if we have non-unity scale, we need to choose our base text size |
| 536 | // based on the SkPaint's text size multiplied by the max scale factor |
| 537 | // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)? |
| 538 | if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) { |
| 539 | scaledTextSize *= maxScale; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // We have three sizes of distance field text, and within each size 'bucket' there is a floor |
| 544 | // and ceiling. A scale outside of this range would require regenerating the distance fields |
| 545 | SkScalar dfMaskScaleFloor; |
| 546 | SkScalar dfMaskScaleCeil; |
| 547 | if (scaledTextSize <= kSmallDFFontLimit) { |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 548 | dfMaskScaleFloor = fMinDistanceFieldFontSize; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 549 | dfMaskScaleCeil = kSmallDFFontLimit; |
| 550 | *textRatio = textSize / kSmallDFFontSize; |
| 551 | skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize)); |
| 552 | } else if (scaledTextSize <= kMediumDFFontLimit) { |
| 553 | dfMaskScaleFloor = kSmallDFFontLimit; |
| 554 | dfMaskScaleCeil = kMediumDFFontLimit; |
| 555 | *textRatio = textSize / kMediumDFFontSize; |
| 556 | skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize)); |
| 557 | } else { |
| 558 | dfMaskScaleFloor = kMediumDFFontLimit; |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 559 | dfMaskScaleCeil = fMaxDistanceFieldFontSize; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 560 | *textRatio = textSize / kLargeDFFontSize; |
| 561 | skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize)); |
| 562 | } |
| 563 | |
| 564 | // Because there can be multiple runs in the blob, we want the overall maxMinScale, and |
| 565 | // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale |
| 566 | // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can |
| 567 | // tolerate before we'd have to move to a large mip size. When we actually test these values |
| 568 | // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test |
| 569 | // against these values to decide if we can reuse or not(ie, will a given scale change our mip |
| 570 | // level) |
| 571 | SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil); |
| 572 | blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize, dfMaskScaleCeil / scaledTextSize); |
| 573 | |
| 574 | skPaint->setAntiAlias(true); |
| 575 | skPaint->setLCDRenderText(false); |
| 576 | skPaint->setAutohinted(false); |
| 577 | skPaint->setHinting(SkPaint::kNormal_Hinting); |
| 578 | skPaint->setSubpixelText(true); |
| 579 | } |
| 580 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 581 | void GrAtlasTextContext::drawDFText(GrAtlasTextBlob* blob, int runIndex, |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 582 | GrAtlasGlyphCache* fontCache, const SkSurfaceProps& props, |
| 583 | const GrTextUtils::Paint& paint, uint32_t scalerContextFlags, |
| 584 | const SkMatrix& viewMatrix, const char text[], |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 585 | size_t byteLength, SkScalar x, SkScalar y) const { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 586 | SkASSERT(byteLength == 0 || text != nullptr); |
| 587 | |
| 588 | // nothing to draw |
| 589 | if (text == nullptr || byteLength == 0) { |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | const SkPaint& skPaint = paint.skPaint(); |
| 594 | SkPaint::GlyphCacheProc glyphCacheProc = |
| 595 | SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), skPaint.isDevKernText(), true); |
| 596 | SkAutoDescriptor desc; |
| 597 | SkScalerContextEffects effects; |
| 598 | // We apply the fake-gamma by altering the distance in the shader, so we ignore the |
| 599 | // passed-in scaler context flags. (It's only used when we fall-back to bitmap text). |
| 600 | skPaint.getScalerContextDescriptor(&effects, &desc, props, SkPaint::kNone_ScalerContextFlags, |
| 601 | nullptr); |
| 602 | SkGlyphCache* origPaintCache = |
| 603 | SkGlyphCache::DetachCache(skPaint.getTypeface(), effects, desc.getDesc()); |
| 604 | |
| 605 | SkTArray<SkScalar> positions; |
| 606 | |
| 607 | const char* textPtr = text; |
| 608 | SkScalar stopX = 0; |
| 609 | SkScalar stopY = 0; |
| 610 | SkScalar origin = 0; |
| 611 | switch (skPaint.getTextAlign()) { |
| 612 | case SkPaint::kRight_Align: origin = SK_Scalar1; break; |
| 613 | case SkPaint::kCenter_Align: origin = SK_ScalarHalf; break; |
| 614 | case SkPaint::kLeft_Align: origin = 0; break; |
| 615 | } |
| 616 | |
| 617 | SkAutoKern autokern; |
| 618 | const char* stop = text + byteLength; |
| 619 | while (textPtr < stop) { |
| 620 | // don't need x, y here, since all subpixel variants will have the |
| 621 | // same advance |
| 622 | const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr); |
| 623 | |
| 624 | SkScalar width = SkFloatToScalar(glyph.fAdvanceX) + autokern.adjust(glyph); |
| 625 | positions.push_back(stopX + origin * width); |
| 626 | |
| 627 | SkScalar height = SkFloatToScalar(glyph.fAdvanceY); |
| 628 | positions.push_back(stopY + origin * height); |
| 629 | |
| 630 | stopX += width; |
| 631 | stopY += height; |
| 632 | } |
| 633 | SkASSERT(textPtr == stop); |
| 634 | |
| 635 | SkGlyphCache::AttachCache(origPaintCache); |
| 636 | |
| 637 | // now adjust starting point depending on alignment |
| 638 | SkScalar alignX = stopX; |
| 639 | SkScalar alignY = stopY; |
| 640 | if (skPaint.getTextAlign() == SkPaint::kCenter_Align) { |
| 641 | alignX = SkScalarHalf(alignX); |
| 642 | alignY = SkScalarHalf(alignY); |
| 643 | } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) { |
| 644 | alignX = 0; |
| 645 | alignY = 0; |
| 646 | } |
| 647 | x -= alignX; |
| 648 | y -= alignY; |
| 649 | SkPoint offset = SkPoint::Make(x, y); |
| 650 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 651 | this->drawDFPosText(blob, runIndex, fontCache, props, paint, scalerContextFlags, viewMatrix, |
| 652 | text, byteLength, positions.begin(), 2, offset); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 653 | } |
| 654 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 655 | void GrAtlasTextContext::drawDFPosText(GrAtlasTextBlob* blob, int runIndex, |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 656 | GrAtlasGlyphCache* fontCache, const SkSurfaceProps& props, |
| 657 | const GrTextUtils::Paint& paint, uint32_t scalerContextFlags, |
| 658 | const SkMatrix& viewMatrix, const char text[], |
| 659 | size_t byteLength, const SkScalar pos[], |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 660 | int scalarsPerPosition, const SkPoint& offset) const { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 661 | SkASSERT(byteLength == 0 || text != nullptr); |
| 662 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 663 | |
| 664 | // nothing to draw |
| 665 | if (text == nullptr || byteLength == 0) { |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | SkTDArray<char> fallbackTxt; |
| 670 | SkTDArray<SkScalar> fallbackPos; |
| 671 | |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 672 | SkTDArray<char> bigFallbackTxt; |
| 673 | SkTDArray<SkScalar> bigFallbackPos; |
| 674 | SkScalar textSize = paint.skPaint().getTextSize(); |
| 675 | SkScalar maxTextSize = fontCache->getGlyphSizeLimit(); |
| 676 | SkScalar bigFallbackTextSize = maxTextSize; |
| 677 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 678 | |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 679 | bool hasWCoord = viewMatrix.hasPerspective() || fDistanceFieldVerticesAlwaysHaveW; |
| 680 | |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 681 | // Setup distance field paint and text ratio |
| 682 | SkScalar textRatio; |
| 683 | SkPaint dfPaint(paint); |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 684 | this->initDistanceFieldPaint(blob, &dfPaint, &textRatio, viewMatrix); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 685 | blob->setHasDistanceField(); |
| 686 | blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(), |
Brian Salomon | 5c6ac64 | 2017-12-19 11:09:32 -0500 | [diff] [blame] | 687 | paint.skPaint().isAntiAlias(), hasWCoord); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 688 | |
| 689 | GrAtlasTextStrike* currStrike = nullptr; |
| 690 | |
| 691 | // We apply the fake-gamma by altering the distance in the shader, so we ignore the |
| 692 | // passed-in scaler context flags. (It's only used when we fall-back to bitmap text). |
| 693 | SkGlyphCache* cache = |
| 694 | blob->setupCache(runIndex, props, SkPaint::kNone_ScalerContextFlags, dfPaint, nullptr); |
| 695 | SkPaint::GlyphCacheProc glyphCacheProc = |
| 696 | SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), dfPaint.isDevKernText(), true); |
| 697 | |
| 698 | const char* stop = text + byteLength; |
| 699 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 700 | SkPaint::Align align = dfPaint.getTextAlign(); |
| 701 | SkScalar alignMul = SkPaint::kCenter_Align == align ? SK_ScalarHalf : |
| 702 | (SkPaint::kRight_Align == align ? SK_Scalar1 : 0); |
| 703 | while (text < stop) { |
| 704 | const char* lastText = text; |
| 705 | // the last 2 parameters are ignored |
| 706 | const SkGlyph& glyph = glyphCacheProc(cache, &text); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 707 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 708 | if (glyph.fWidth) { |
| 709 | SkScalar x = offset.x() + pos[0]; |
| 710 | SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 711 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 712 | SkScalar advanceX = SkFloatToScalar(glyph.fAdvanceX) * alignMul * textRatio; |
| 713 | SkScalar advanceY = SkFloatToScalar(glyph.fAdvanceY) * alignMul * textRatio; |
| 714 | |
| 715 | if (glyph.fMaskFormat != SkMask::kARGB32_Format) { |
| 716 | DfAppendGlyph(blob, runIndex, fontCache, &currStrike, glyph, x - advanceX, |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 717 | y - advanceY, paint.filteredPremulColor(), cache, textRatio); |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 718 | } else { |
| 719 | // can't append color glyph to SDF batch, send to fallback |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 720 | SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*textRatio; |
| 721 | SkScalar scaledGlyphSize = maxDim*maxScale; |
| 722 | |
| 723 | if (!viewMatrix.hasPerspective() && scaledGlyphSize > maxTextSize) { |
| 724 | bigFallbackTxt.append(SkToInt(text - lastText), lastText); |
| 725 | *bigFallbackPos.append() = maxScale*pos[0]; |
| 726 | if (2 == scalarsPerPosition) { |
| 727 | *bigFallbackPos.append() = maxScale*pos[1]; |
| 728 | } |
| 729 | SkScalar glyphTextSize = SkScalarFloorToScalar(maxTextSize*textSize/maxDim); |
| 730 | bigFallbackTextSize = SkTMin(glyphTextSize, bigFallbackTextSize); |
| 731 | } else { |
| 732 | fallbackTxt.append(SkToInt(text - lastText), lastText); |
| 733 | *fallbackPos.append() = pos[0]; |
| 734 | if (2 == scalarsPerPosition) { |
| 735 | *fallbackPos.append() = pos[1]; |
| 736 | } |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 737 | } |
| 738 | } |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 739 | } |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 740 | pos += scalarsPerPosition; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | SkGlyphCache::AttachCache(cache); |
| 744 | if (fallbackTxt.count()) { |
| 745 | blob->initOverride(runIndex); |
| 746 | GrAtlasTextContext::DrawBmpPosText(blob, runIndex, fontCache, props, paint, |
| 747 | scalerContextFlags, viewMatrix, fallbackTxt.begin(), |
| 748 | fallbackTxt.count(), fallbackPos.begin(), |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 749 | scalarsPerPosition, offset, SK_Scalar1); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 750 | } |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 751 | |
| 752 | if (bigFallbackTxt.count()) { |
| 753 | // Set up paint and matrix to scale glyphs |
| 754 | blob->initOverride(runIndex); |
| 755 | SkPaint largePaint(paint); |
| 756 | largePaint.setTextSize(bigFallbackTextSize); |
| 757 | // remove maxScale from viewMatrix and move it into textRatio |
| 758 | // this keeps the base glyph size consistent regardless of matrix scale |
| 759 | SkMatrix modMatrix(viewMatrix); |
| 760 | SkScalar invScale = SkScalarInvert(maxScale); |
| 761 | modMatrix.preScale(invScale, invScale); |
| 762 | SkScalar bigFallbackTextRatio = textSize*maxScale/bigFallbackTextSize; |
| 763 | SkPoint modOffset(offset); |
| 764 | modOffset *= maxScale; |
| 765 | GrTextUtils::Paint textPaint(&largePaint, paint.dstColorSpaceInfo()); |
| 766 | GrAtlasTextContext::DrawBmpPosText(blob, runIndex, fontCache, props, textPaint, |
| 767 | scalerContextFlags, modMatrix, bigFallbackTxt.begin(), |
| 768 | bigFallbackTxt.count(), bigFallbackPos.begin(), |
| 769 | scalarsPerPosition, modOffset, bigFallbackTextRatio); |
| 770 | } |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 771 | } |
| 772 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 773 | // TODO: merge with BmpAppendGlyph |
| 774 | void GrAtlasTextContext::DfAppendGlyph(GrAtlasTextBlob* blob, int runIndex, |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 775 | GrAtlasGlyphCache* cache, GrAtlasTextStrike** strike, |
| 776 | const SkGlyph& skGlyph, SkScalar sx, SkScalar sy, |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 777 | GrColor color, SkGlyphCache* glyphCache, |
| 778 | SkScalar textRatio) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 779 | if (!*strike) { |
| 780 | *strike = cache->getStrike(glyphCache); |
| 781 | } |
| 782 | |
| 783 | GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(), |
| 784 | skGlyph.getSubXFixed(), |
| 785 | skGlyph.getSubYFixed(), |
| 786 | GrGlyph::kDistance_MaskStyle); |
| 787 | GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, glyphCache); |
| 788 | if (!glyph) { |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 789 | return; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset); |
| 793 | SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset); |
| 794 | SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset); |
| 795 | SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset); |
| 796 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 797 | dx *= textRatio; |
| 798 | dy *= textRatio; |
| 799 | width *= textRatio; |
| 800 | height *= textRatio; |
| 801 | SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 802 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 803 | blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, glyphCache, skGlyph, sx, sy, |
| 804 | textRatio, false); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 805 | } |
| 806 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 807 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 808 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 809 | #if GR_TEST_UTILS |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 810 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 811 | #include "GrRenderTargetContext.h" |
| 812 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 813 | GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 814 | static uint32_t gContextID = SK_InvalidGenID; |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 815 | static std::unique_ptr<GrAtlasTextContext> gTextContext; |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 816 | static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 817 | |
| 818 | if (context->uniqueID() != gContextID) { |
| 819 | gContextID = context->uniqueID(); |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 820 | gTextContext = GrAtlasTextContext::Make(GrAtlasTextContext::Options()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 823 | // Setup dummy SkPaint / GrPaint / GrRenderTargetContext |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 824 | sk_sp<GrRenderTargetContext> rtc(context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 825 | SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr)); |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 826 | |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 827 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 828 | |
| 829 | // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint |
| 830 | // param. |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 831 | SkPaint skPaint; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 832 | skPaint.setColor(random->nextU()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 833 | skPaint.setLCDRenderText(random->nextBool()); |
| 834 | skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool()); |
| 835 | skPaint.setSubpixelText(random->nextBool()); |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 836 | GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 837 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 838 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 839 | int textLen = (int)strlen(text); |
| 840 | |
joshualitt | b8d8649 | 2016-02-24 09:23:03 -0800 | [diff] [blame] | 841 | // create some random x/y offsets, including negative offsets |
| 842 | static const int kMaxTrans = 1024; |
| 843 | int xPos = (random->nextU() % 2) * 2 - 1; |
| 844 | int yPos = (random->nextU() % 2) * 2 - 1; |
| 845 | int xInt = (random->nextU() % kMaxTrans) * xPos; |
| 846 | int yInt = (random->nextU() % kMaxTrans) * yPos; |
| 847 | SkScalar x = SkIntToScalar(xInt); |
| 848 | SkScalar y = SkIntToScalar(yInt); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 849 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 850 | auto atlasGlyphCache = context->contextPriv().getAtlasGlyphCache(); |
| 851 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 852 | // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to |
| 853 | // test the text op with this unit test, that is okay. |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 854 | sk_sp<GrAtlasTextBlob> blob(gTextContext->makeDrawTextBlob( |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 855 | context->contextPriv().getTextBlobCache(), atlasGlyphCache, |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 856 | *context->caps()->shaderCaps(), utilsPaint, |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 857 | GrAtlasTextContext::kTextBlobOpScalerContextFlags, viewMatrix, gSurfaceProps, text, |
| 858 | static_cast<size_t>(textLen), x, y)); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 859 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 860 | return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, gSurfaceProps, |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame^] | 861 | gTextContext->dfAdjustTable(), atlasGlyphCache, |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 862 | rtc->textTarget()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | #endif |