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