| 1 | /* |
| 2 | * Copyright 2020 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 | */ |
| 7 | |
| 8 | #include "src/gpu/text/GrSDFTOptions.h" |
| 9 | |
| 10 | #include "include/core/SkFont.h" |
| 11 | #include "include/core/SkMatrix.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkScalar.h" |
| 14 | #include "include/core/SkSurfaceProps.h" |
| 15 | |
| 16 | #include <tuple> |
| 17 | |
| 18 | // DF sizes and thresholds for usage of the small and medium sizes. For example, above |
| 19 | // kSmallDFFontLimit we will use the medium size. The large size is used up until the size at |
| 20 | // which we switch over to drawing as paths as controlled by Options. |
| 21 | static const int kSmallDFFontSize = 32; |
| 22 | static const int kSmallDFFontLimit = 32; |
| 23 | static const int kMediumDFFontSize = 72; |
| 24 | static const int kMediumDFFontLimit = 72; |
| 25 | static const int kLargeDFFontSize = 162; |
| 26 | #ifdef SK_BUILD_FOR_MAC |
| 27 | static const int kLargeDFFontLimit = 162; |
| 28 | static const int kExtraLargeDFFontSize = 256; |
| 29 | #endif |
| 30 | |
| 31 | GrSDFTOptions::GrSDFTOptions(SkScalar min, SkScalar max) |
| 32 | : fMinDistanceFieldFontSize{min} |
| 33 | , fMaxDistanceFieldFontSize{max} { |
| 34 | SkASSERT_RELEASE(min > 0 && max >= min); |
| 35 | } |
| 36 | |
| 37 | bool GrSDFTOptions::canDrawAsDistanceFields(const SkPaint& paint, const SkFont& font, |
| 38 | const SkMatrix& viewMatrix, |
| 39 | const SkSurfaceProps& props, |
| 40 | bool contextSupportsDistanceFieldText) const { |
| 41 | // mask filters modify alpha, which doesn't translate well to distance |
| 42 | if (paint.getMaskFilter() || !contextSupportsDistanceFieldText) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // TODO: add some stroking support |
| 47 | if (paint.getStyle() != SkPaint::kFill_Style) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | if (viewMatrix.hasPerspective()) { |
| 52 | // Don't use SDF for perspective. Paths look better. |
| 53 | return false; |
| 54 | } else { |
| 55 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 56 | SkScalar scaledTextSize = maxScale * font.getSize(); |
| 57 | // Hinted text looks far better at small resolutions |
| 58 | // Scaling up beyond 2x yields undesirable artifacts |
| 59 | if (scaledTextSize < fMinDistanceFieldFontSize || |
| 60 | scaledTextSize > fMaxDistanceFieldFontSize) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | bool useDFT = props.isUseDeviceIndependentFonts(); |
| 65 | #if SK_FORCE_DISTANCE_FIELD_TEXT |
| 66 | useDFT = true; |
| 67 | #endif |
| 68 | |
| 69 | if (!useDFT && scaledTextSize < kLargeDFFontSize) { |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | SkScalar scaled_text_size(const SkScalar textSize, const SkMatrix& viewMatrix) { |
| 78 | SkScalar scaledTextSize = textSize; |
| 79 | |
| 80 | if (viewMatrix.hasPerspective()) { |
| 81 | // for perspective, we simply force to the medium size |
| 82 | // TODO: compute a size based on approximate screen area |
| 83 | scaledTextSize = kMediumDFFontLimit; |
| 84 | } else { |
| 85 | SkScalar maxScale = viewMatrix.getMaxScale(); |
| 86 | // if we have non-unity scale, we need to choose our base text size |
| 87 | // based on the SkPaint's text size multiplied by the max scale factor |
| 88 | // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)? |
| 89 | if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) { |
| 90 | scaledTextSize *= maxScale; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return scaledTextSize; |
| 95 | } |
| 96 | |
| 97 | SkFont GrSDFTOptions::getSDFFont(const SkFont& font, |
| 98 | const SkMatrix& viewMatrix, |
| 99 | SkScalar* ) const { |
| 100 | SkScalar textSize = font.getSize(); |
| 101 | SkScalar scaledTextSize = scaled_text_size(textSize, viewMatrix); |
| 102 | |
| 103 | SkFont dfFont{font}; |
| 104 | |
| 105 | if (scaledTextSize <= kSmallDFFontLimit) { |
| 106 | *textRatio = textSize / kSmallDFFontSize; |
| 107 | dfFont.setSize(SkIntToScalar(kSmallDFFontSize)); |
| 108 | } else if (scaledTextSize <= kMediumDFFontLimit) { |
| 109 | *textRatio = textSize / kMediumDFFontSize; |
| 110 | dfFont.setSize(SkIntToScalar(kMediumDFFontSize)); |
| 111 | #ifdef SK_BUILD_FOR_MAC |
| 112 | } else if (scaledTextSize <= kLargeDFFontLimit) { |
| 113 | *textRatio = textSize / kLargeDFFontSize; |
| 114 | dfFont.setSize(SkIntToScalar(kLargeDFFontSize)); |
| 115 | } else { |
| 116 | *textRatio = textSize / kExtraLargeDFFontSize; |
| 117 | dfFont.setSize(SkIntToScalar(kExtraLargeDFFontSize)); |
| 118 | } |
| 119 | #else |
| 120 | } else { |
| 121 | *textRatio = textSize / kLargeDFFontSize; |
| 122 | dfFont.setSize(SkIntToScalar(kLargeDFFontSize)); |
| 123 | } |
| 124 | #endif |
| 125 | |
| 126 | dfFont.setEdging(SkFont::Edging::kAntiAlias); |
| 127 | dfFont.setForceAutoHinting(false); |
| 128 | dfFont.setHinting(SkFontHinting::kNormal); |
| 129 | |
| 130 | // The sub-pixel position will always happen when transforming to the screen. |
| 131 | dfFont.setSubpixel(false); |
| 132 | return dfFont; |
| 133 | } |
| 134 | |
| 135 | std::pair<SkScalar, SkScalar> GrSDFTOptions::computeSDFMinMaxScale( |
| 136 | SkScalar textSize, const SkMatrix& viewMatrix) const { |
| 137 | |
| 138 | SkScalar scaledTextSize = scaled_text_size(textSize, viewMatrix); |
| 139 | |
| 140 | // We have three sizes of distance field text, and within each size 'bucket' there is a floor |
| 141 | // and ceiling. A scale outside of this range would require regenerating the distance fields |
| 142 | SkScalar dfMaskScaleFloor; |
| 143 | SkScalar dfMaskScaleCeil; |
| 144 | if (scaledTextSize <= kSmallDFFontLimit) { |
| 145 | dfMaskScaleFloor = fMinDistanceFieldFontSize; |
| 146 | dfMaskScaleCeil = kSmallDFFontLimit; |
| 147 | } else if (scaledTextSize <= kMediumDFFontLimit) { |
| 148 | dfMaskScaleFloor = kSmallDFFontLimit; |
| 149 | dfMaskScaleCeil = kMediumDFFontLimit; |
| 150 | } else { |
| 151 | dfMaskScaleFloor = kMediumDFFontLimit; |
| 152 | dfMaskScaleCeil = fMaxDistanceFieldFontSize; |
| 153 | } |
| 154 | |
| 155 | // Because there can be multiple runs in the blob, we want the overall maxMinScale, and |
| 156 | // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale |
| 157 | // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can |
| 158 | // tolerate before we'd have to move to a large mip size. When we actually test these values |
| 159 | // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test |
| 160 | // against these values to decide if we can reuse or not(ie, will a given scale change our mip |
| 161 | // level) |
| 162 | SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil); |
| 163 | |
| 164 | return std::make_pair(dfMaskScaleFloor / scaledTextSize, dfMaskScaleCeil / scaledTextSize); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |