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