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 | fd0d970 | 2019-02-01 10:19:42 -0500 | [diff] [blame] | 12 | #include "GrRecordingContextPriv.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" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 18 | #include "SkGlyphRun.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 19 | #include "SkGr.h" |
Jim Van Verth | c65b65d | 2018-01-16 16:26:35 -0500 | [diff] [blame] | 20 | #include "SkGraphics.h" |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 21 | #include "SkMakeUnique.h" |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 22 | #include "SkMaskFilterBase.h" |
Herb Derby | eb3f674 | 2018-03-05 14:36:45 -0500 | [diff] [blame] | 23 | #include "SkPaintPriv.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 24 | #include "SkTo.h" |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 25 | #include "ops/GrMeshDrawOp.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 26 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 27 | // DF sizes and thresholds for usage of the small and medium sizes. For example, above |
| 28 | // kSmallDFFontLimit we will use the medium size. The large size is used up until the size at |
| 29 | // which we switch over to drawing as paths as controlled by Options. |
| 30 | static const int kSmallDFFontSize = 32; |
| 31 | static const int kSmallDFFontLimit = 32; |
Jim Van Verth | 825d4d7 | 2018-01-30 20:37:03 +0000 | [diff] [blame] | 32 | static const int kMediumDFFontSize = 72; |
| 33 | static const int kMediumDFFontLimit = 72; |
| 34 | static const int kLargeDFFontSize = 162; |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 35 | |
| 36 | static const int kDefaultMinDistanceFieldFontSize = 18; |
| 37 | #ifdef SK_BUILD_FOR_ANDROID |
| 38 | static const int kDefaultMaxDistanceFieldFontSize = 384; |
| 39 | #else |
| 40 | static const int kDefaultMaxDistanceFieldFontSize = 2 * kLargeDFFontSize; |
| 41 | #endif |
| 42 | |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 43 | GrTextContext::GrTextContext(const Options& options) |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 44 | : fDistanceAdjustTable(new GrDistanceFieldAdjustTable), fOptions(options) { |
| 45 | SanitizeOptions(&fOptions); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 48 | std::unique_ptr<GrTextContext> GrTextContext::Make(const Options& options) { |
| 49 | return std::unique_ptr<GrTextContext>(new GrTextContext(options)); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 52 | SkColor GrTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) { |
Mike Reed | ec7278e | 2019-02-01 14:00:34 -0500 | [diff] [blame] | 53 | SkColor canonicalColor = SkPaintPriv::ComputeLuminanceColor(paint); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 54 | if (lcd) { |
| 55 | // This is the correct computation, but there are tons of cases where LCD can be overridden. |
| 56 | // For now we just regenerate if any run in a textblob has LCD. |
| 57 | // TODO figure out where all of these overrides are and see if we can incorporate that logic |
| 58 | // at a higher level *OR* use sRGB |
| 59 | SkASSERT(false); |
| 60 | //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor); |
| 61 | } else { |
| 62 | // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have |
| 63 | // gamma corrected masks anyways, nor color |
| 64 | U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor), |
| 65 | SkColorGetG(canonicalColor), |
| 66 | SkColorGetB(canonicalColor)); |
| 67 | // reduce to our finite number of bits |
| 68 | canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum)); |
| 69 | } |
| 70 | return canonicalColor; |
| 71 | } |
| 72 | |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 73 | SkScalerContextFlags GrTextContext::ComputeScalerContextFlags( |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 74 | const GrColorSpaceInfo& colorSpaceInfo) { |
Brian Osman | 34ec374 | 2018-07-03 10:40:57 -0400 | [diff] [blame] | 75 | // If we're doing linear blending, then we can disable the gamma hacks. |
brianosman | 5280dcb | 2016-04-14 06:02:59 -0700 | [diff] [blame] | 76 | // 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] | 77 | // TODO: Can we be even smarter about mask gamma based on the dest transfer function? |
| 78 | if (colorSpaceInfo.isLinearlyBlended()) { |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 79 | return SkScalerContextFlags::kBoostContrast; |
brianosman | 32f7782 | 2016-04-07 06:25:45 -0700 | [diff] [blame] | 80 | } else { |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 81 | return SkScalerContextFlags::kFakeGammaAndBoostContrast; |
brianosman | 32f7782 | 2016-04-07 06:25:45 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 85 | void GrTextContext::SanitizeOptions(Options* options) { |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 86 | if (options->fMaxDistanceFieldFontSize < 0.f) { |
| 87 | options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize; |
| 88 | } |
| 89 | if (options->fMinDistanceFieldFontSize < 0.f) { |
| 90 | options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize; |
| 91 | } |
| 92 | } |
| 93 | |
Mike Reed | f2b074e | 2018-12-03 16:52:59 -0500 | [diff] [blame] | 94 | bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& paint, const SkFont& font, |
| 95 | const SkMatrix& viewMatrix, |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 96 | const SkSurfaceProps& props, |
| 97 | bool contextSupportsDistanceFieldText, |
| 98 | const Options& options) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 99 | if (!viewMatrix.hasPerspective()) { |
| 100 | SkScalar maxScale = viewMatrix.getMaxScale(); |
Mike Reed | f2b074e | 2018-12-03 16:52:59 -0500 | [diff] [blame] | 101 | SkScalar scaledTextSize = maxScale * font.getSize(); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 102 | // Hinted text looks far better at small resolutions |
| 103 | // Scaling up beyond 2x yields undesireable artifacts |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 104 | if (scaledTextSize < options.fMinDistanceFieldFontSize || |
| 105 | scaledTextSize > options.fMaxDistanceFieldFontSize) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
| 109 | bool useDFT = props.isUseDeviceIndependentFonts(); |
| 110 | #if SK_FORCE_DISTANCE_FIELD_TEXT |
| 111 | useDFT = true; |
| 112 | #endif |
| 113 | |
| 114 | if (!useDFT && scaledTextSize < kLargeDFFontSize) { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 119 | // mask filters modify alpha, which doesn't translate well to distance |
Mike Reed | f2b074e | 2018-12-03 16:52:59 -0500 | [diff] [blame] | 120 | if (paint.getMaskFilter() || !contextSupportsDistanceFieldText) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
| 124 | // TODO: add some stroking support |
Mike Reed | f2b074e | 2018-12-03 16:52:59 -0500 | [diff] [blame] | 125 | if (paint.getStyle() != SkPaint::kFill_Style) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
Herb Derby | 6f27489 | 2018-12-17 17:30:07 -0500 | [diff] [blame] | 132 | void GrTextContext::InitDistanceFieldPaint(const SkScalar textSize, |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 133 | const SkMatrix& viewMatrix, |
| 134 | const Options& options, |
Herb Derby | 6f27489 | 2018-12-17 17:30:07 -0500 | [diff] [blame] | 135 | GrTextBlob* blob, |
| 136 | SkPaint* skPaint, |
| 137 | SkFont* skFont, |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 138 | SkScalar* textRatio, |
| 139 | SkScalerContextFlags* flags) { |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 140 | SkScalar scaledTextSize = textSize; |
| 141 | |
| 142 | if (viewMatrix.hasPerspective()) { |
| 143 | // for perspective, we simply force to the medium size |
| 144 | // TODO: compute a size based on approximate screen area |
| 145 | scaledTextSize = kMediumDFFontLimit; |
| 146 | } else { |
| 147 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 148 | // if we have non-unity scale, we need to choose our base text size |
| 149 | // based on the SkPaint's text size multiplied by the max scale factor |
| 150 | // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)? |
| 151 | if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) { |
| 152 | scaledTextSize *= maxScale; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // We have three sizes of distance field text, and within each size 'bucket' there is a floor |
| 157 | // and ceiling. A scale outside of this range would require regenerating the distance fields |
| 158 | SkScalar dfMaskScaleFloor; |
| 159 | SkScalar dfMaskScaleCeil; |
| 160 | if (scaledTextSize <= kSmallDFFontLimit) { |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 161 | dfMaskScaleFloor = options.fMinDistanceFieldFontSize; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 162 | dfMaskScaleCeil = kSmallDFFontLimit; |
| 163 | *textRatio = textSize / kSmallDFFontSize; |
Herb Derby | 6f27489 | 2018-12-17 17:30:07 -0500 | [diff] [blame] | 164 | skFont->setSize(SkIntToScalar(kSmallDFFontSize)); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 165 | } else if (scaledTextSize <= kMediumDFFontLimit) { |
| 166 | dfMaskScaleFloor = kSmallDFFontLimit; |
| 167 | dfMaskScaleCeil = kMediumDFFontLimit; |
| 168 | *textRatio = textSize / kMediumDFFontSize; |
Herb Derby | 6f27489 | 2018-12-17 17:30:07 -0500 | [diff] [blame] | 169 | skFont->setSize(SkIntToScalar(kMediumDFFontSize)); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 170 | } else { |
| 171 | dfMaskScaleFloor = kMediumDFFontLimit; |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 172 | dfMaskScaleCeil = options.fMaxDistanceFieldFontSize; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 173 | *textRatio = textSize / kLargeDFFontSize; |
Herb Derby | 6f27489 | 2018-12-17 17:30:07 -0500 | [diff] [blame] | 174 | skFont->setSize(SkIntToScalar(kLargeDFFontSize)); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Because there can be multiple runs in the blob, we want the overall maxMinScale, and |
| 178 | // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale |
| 179 | // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can |
| 180 | // tolerate before we'd have to move to a large mip size. When we actually test these values |
| 181 | // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test |
| 182 | // against these values to decide if we can reuse or not(ie, will a given scale change our mip |
| 183 | // level) |
| 184 | SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil); |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 185 | if (blob) { |
| 186 | blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize, |
| 187 | dfMaskScaleCeil / scaledTextSize); |
| 188 | } |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 189 | |
Herb Derby | 6f27489 | 2018-12-17 17:30:07 -0500 | [diff] [blame] | 190 | skFont->setEdging(SkFont::Edging::kAntiAlias); |
| 191 | skFont->setForceAutoHinting(false); |
| 192 | skFont->setHinting(kNormal_SkFontHinting); |
| 193 | skFont->setSubpixel(true); |
Jim Van Verth | d401da6 | 2018-05-03 10:40:30 -0400 | [diff] [blame] | 194 | |
| 195 | skPaint->setMaskFilter(GrSDFMaskFilter::Make()); |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 196 | |
| 197 | // We apply the fake-gamma by altering the distance in the shader, so we ignore the |
| 198 | // passed-in scaler context flags. (It's only used when we fall-back to bitmap text). |
| 199 | *flags = SkScalerContextFlags::kNone; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 200 | } |
| 201 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 202 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Herb Derby | f4f6bbf | 2018-07-27 11:58:37 -0400 | [diff] [blame] | 203 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 204 | #if GR_TEST_UTILS |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 205 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 206 | #include "GrRenderTargetContext.h" |
| 207 | |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 208 | GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) { |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 209 | static uint32_t gContextID = SK_InvalidGenID; |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 210 | static std::unique_ptr<GrTextContext> gTextContext; |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 211 | static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 212 | |
Robert Phillips | fd0d970 | 2019-02-01 10:19:42 -0500 | [diff] [blame] | 213 | if (context->priv().contextID() != gContextID) { |
| 214 | gContextID = context->priv().contextID(); |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 215 | gTextContext = GrTextContext::Make(GrTextContext::Options()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 218 | const GrBackendFormat format = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 219 | context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 220 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 221 | // Setup dummy SkPaint / GrPaint / GrRenderTargetContext |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 222 | sk_sp<GrRenderTargetContext> rtc(context->priv().makeDeferredRenderTargetContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 223 | format, SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr)); |
brianosman | 8fe485b | 2016-07-25 12:31:51 -0700 | [diff] [blame] | 224 | |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 225 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 226 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 227 | SkPaint skPaint; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 228 | skPaint.setColor(random->nextU()); |
Mike Reed | 191e64b | 2019-01-02 15:35:29 -0500 | [diff] [blame] | 229 | |
| 230 | SkFont font; |
| 231 | if (random->nextBool()) { |
| 232 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 233 | } else { |
| 234 | font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias); |
| 235 | } |
| 236 | font.setSubpixel(random->nextBool()); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 237 | |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 238 | const char* text = "The quick brown fox jumps over the lazy dog."; |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 239 | |
joshualitt | b8d8649 | 2016-02-24 09:23:03 -0800 | [diff] [blame] | 240 | // create some random x/y offsets, including negative offsets |
| 241 | static const int kMaxTrans = 1024; |
| 242 | int xPos = (random->nextU() % 2) * 2 - 1; |
| 243 | int yPos = (random->nextU() % 2) * 2 - 1; |
| 244 | int xInt = (random->nextU() % kMaxTrans) * xPos; |
| 245 | int yInt = (random->nextU() % kMaxTrans) * yPos; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 246 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 247 | return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(), |
Mike Reed | 191e64b | 2019-01-02 15:35:29 -0500 | [diff] [blame] | 248 | skPaint, font, viewMatrix, text, xInt, yInt); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | #endif |