• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
69 #ifdef SKIA_OHOS_FOR_OHOS_TRACE
70     if (scaledTextSize <= fMaxDistanceFieldFontSize) {
71 #else
72     if (scaledTextSize < fMinDistanceFieldFontSize) {
73 #endif
74         return kDirect;
75     } else if (fMaxDistanceFieldFontSize < scaledTextSize) {
76         return kPath;
77     }
78 
79     return kSDFT;
80 }
81 
82 SkScalar scaled_text_size(const SkScalar textSize, const SkMatrix& viewMatrix) {
83     SkScalar scaledTextSize = textSize;
84 
85     if (viewMatrix.hasPerspective()) {
86         // for perspective, we simply force to the medium size
87         // TODO: compute a size based on approximate screen area
88         scaledTextSize = kMediumDFFontLimit;
89     } else {
90         SkScalar maxScale = viewMatrix.getMaxScale();
91         // if we have non-unity scale, we need to choose our base text size
92         // based on the SkPaint's text size multiplied by the max scale factor
93         // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
94         if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
95             scaledTextSize *= maxScale;
96         }
97     }
98 
99     return scaledTextSize;
100 }
101 
102 SkFont GrSDFTControl::getSDFFont(const SkFont& font,
103                                  const SkMatrix& viewMatrix,
104                                  SkScalar* textRatio) const {
105     SkScalar textSize = font.getSize();
106     SkScalar scaledTextSize = scaled_text_size(textSize, viewMatrix);
107 
108     SkFont dfFont{font};
109 
110     if (scaledTextSize <= kSmallDFFontLimit) {
111         *textRatio = textSize / kSmallDFFontSize;
112         dfFont.setSize(SkIntToScalar(kSmallDFFontSize));
113     } else if (scaledTextSize <= kMediumDFFontLimit) {
114         *textRatio = textSize / kMediumDFFontSize;
115         dfFont.setSize(SkIntToScalar(kMediumDFFontSize));
116 #ifdef SK_BUILD_FOR_MAC
117     } else if (scaledTextSize <= kLargeDFFontLimit) {
118         *textRatio = textSize / kLargeDFFontSize;
119         dfFont.setSize(SkIntToScalar(kLargeDFFontSize));
120     } else {
121         *textRatio = textSize / kExtraLargeDFFontSize;
122         dfFont.setSize(SkIntToScalar(kExtraLargeDFFontSize));
123     }
124 #else
125     } else {
126         *textRatio = textSize / kLargeDFFontSize;
127         dfFont.setSize(SkIntToScalar(kLargeDFFontSize));
128     }
129 #endif
130 
131     dfFont.setEdging(SkFont::Edging::kAntiAlias);
132     dfFont.setForceAutoHinting(false);
133     dfFont.setHinting(SkFontHinting::kNormal);
134 
135     // The sub-pixel position will always happen when transforming to the screen.
136     dfFont.setSubpixel(false);
137     return dfFont;
138 }
139 
computeSDFMinMaxScale(SkScalar textSize,const SkMatrix & viewMatrix) const140 std::pair<SkScalar, SkScalar> GrSDFTControl::computeSDFMinMaxScale(
141         SkScalar textSize, const SkMatrix& viewMatrix) const {
142 
143     SkScalar scaledTextSize = scaled_text_size(textSize, viewMatrix);
144 
145     // We have three sizes of distance field text, and within each size 'bucket' there is a floor
146     // and ceiling.  A scale outside of this range would require regenerating the distance fields
147     SkScalar dfMaskScaleFloor;
148     SkScalar dfMaskScaleCeil;
149     if (scaledTextSize <= kSmallDFFontLimit) {
150         dfMaskScaleFloor = fMinDistanceFieldFontSize;
151         dfMaskScaleCeil = kSmallDFFontLimit;
152     } else if (scaledTextSize <= kMediumDFFontLimit) {
153         dfMaskScaleFloor = kSmallDFFontLimit;
154         dfMaskScaleCeil = kMediumDFFontLimit;
155     } else {
156         dfMaskScaleFloor = kMediumDFFontLimit;
157         dfMaskScaleCeil = fMaxDistanceFieldFontSize;
158     }
159 
160     // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
161     // minMaxScale to make regeneration decisions.  Specifically, we want the maximum minimum scale
162     // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
163     // tolerate before we'd have to move to a large mip size.  When we actually test these values
164     // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
165     // against these values to decide if we can reuse or not(ie, will a given scale change our mip
166     // level)
167     SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
168 
169     return std::make_pair(dfMaskScaleFloor / scaledTextSize, dfMaskScaleCeil / scaledTextSize);
170 }
171 
172 
173