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/GrSDFTControl.h"
9
10 #include "include/core/SkFont.h"
11 #include "include/core/SkGraphics.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSurfaceProps.h"
16 #include "src/core/SkGlyphRunPainter.h"
17
18 #include <tuple>
19
20 // DF sizes and thresholds for usage of the small and medium sizes. For example, above
21 // kSmallDFFontLimit we will use the medium size. The large size is used up until the size at
22 // which we switch over to drawing as paths as controlled by Control.
23 static const int kSmallDFFontSize = 32;
24 static const int kSmallDFFontLimit = 32;
25 static const int kMediumDFFontSize = 72;
26 static const int kMediumDFFontLimit = 72;
27 static const int kLargeDFFontSize = 162;
28 #ifdef SK_BUILD_FOR_MAC
29 static const int kLargeDFFontLimit = 162;
30 static const int kExtraLargeDFFontSize = 256;
31 #endif
32
MinSDFTRange(bool useSDFTForSmallText,SkScalar min)33 SkScalar GrSDFTControl::MinSDFTRange(bool useSDFTForSmallText, SkScalar min) {
34 if (!useSDFTForSmallText) {
35 return kLargeDFFontSize;
36 }
37 return min;
38 }
39
GrSDFTControl(bool ableToUseSDFT,bool useSDFTForSmallText,SkScalar min,SkScalar max)40 GrSDFTControl::GrSDFTControl(
41 bool ableToUseSDFT, bool useSDFTForSmallText, SkScalar min, SkScalar max)
42 : fMinDistanceFieldFontSize{MinSDFTRange(useSDFTForSmallText, min)}
43 , fMaxDistanceFieldFontSize{max}
44 , fAbleToUseSDFT{ableToUseSDFT} {
45 SkASSERT_RELEASE(0 < min && min <= max);
46 }
47
drawingType(const SkFont & font,const SkPaint & paint,const SkMatrix & viewMatrix) const48 auto GrSDFTControl::drawingType(
49 const SkFont& font, const SkPaint& paint, const SkMatrix& viewMatrix) const -> DrawingType {
50
51 // Use paths as the first choice for hairlines and perspective.
52 if ((paint.getStyle() == SkPaint::kStroke_Style && paint.getStrokeWidth() == 0)
53 || viewMatrix.hasPerspective()) {
54 return kPath;
55 }
56
57 SkScalar maxScale = viewMatrix.getMaxScale();
58 SkScalar scaledTextSize = maxScale * font.getSize();
59
60 // If we can't use SDFT, then make a simple choice between direct or path.
61 if (!fAbleToUseSDFT || paint.getMaskFilter() || paint.getStyle() != SkPaint::kFill_Style) {
62 constexpr int kAboveIsPath = SkStrikeCommon::kSkSideTooBigForAtlas;
63 return scaledTextSize < kAboveIsPath ? kDirect : kPath;
64 }
65
66 // Hinted text looks far better at small resolutions
67 // Scaling up beyond 2x yields undesirable artifacts
68 if (scaledTextSize < fMinDistanceFieldFontSize) {
69 return kDirect;
70 } else if (fMaxDistanceFieldFontSize < scaledTextSize) {
71 return kPath;
72 }
73
74 return kSDFT;
75 }
76
scaled_text_size(const SkScalar textSize,const SkMatrix & viewMatrix)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
getSDFFont(const SkFont & font,const SkMatrix & viewMatrix,SkScalar * textRatio) const97 SkFont GrSDFTControl::getSDFFont(const SkFont& font,
98 const SkMatrix& viewMatrix,
99 SkScalar* textRatio) 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
computeSDFMinMaxScale(SkScalar textSize,const SkMatrix & viewMatrix) const135 std::pair<SkScalar, SkScalar> GrSDFTControl::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