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