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