blob: 6b55aa71116f25ad677368acc7ede2ba9ab34aa9 [file] [log] [blame]
joshualitt1d89e8d2015-04-01 12:40:54 -07001/*
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 Canaryc640d0d2018-06-13 09:59:02 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/text/GrTextContext.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkGraphics.h"
11#include "include/gpu/GrContext.h"
12#include "include/private/SkTo.h"
13#include "src/core/SkDistanceFieldGen.h"
14#include "src/core/SkDraw.h"
15#include "src/core/SkDrawProcs.h"
16#include "src/core/SkGlyphRun.h"
17#include "src/core/SkMakeUnique.h"
18#include "src/core/SkMaskFilterBase.h"
19#include "src/core/SkPaintPriv.h"
20#include "src/gpu/GrCaps.h"
21#include "src/gpu/GrRecordingContextPriv.h"
22#include "src/gpu/SkGr.h"
23#include "src/gpu/ops/GrMeshDrawOp.h"
24#include "src/gpu/text/GrSDFMaskFilter.h"
25#include "src/gpu/text/GrTextBlobCache.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070026
Brian Salomonaf597482017-11-07 16:23:34 -050027// 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.
30static const int kSmallDFFontSize = 32;
31static const int kSmallDFFontLimit = 32;
Jim Van Verth825d4d72018-01-30 20:37:03 +000032static const int kMediumDFFontSize = 72;
33static const int kMediumDFFontLimit = 72;
34static const int kLargeDFFontSize = 162;
Brian Salomonaf597482017-11-07 16:23:34 -050035
36static const int kDefaultMinDistanceFieldFontSize = 18;
37#ifdef SK_BUILD_FOR_ANDROID
38static const int kDefaultMaxDistanceFieldFontSize = 384;
39#else
40static const int kDefaultMaxDistanceFieldFontSize = 2 * kLargeDFFontSize;
41#endif
42
Herb Derby26cbe512018-05-24 14:39:01 -040043GrTextContext::GrTextContext(const Options& options)
Khushal3e7548c2018-05-23 15:45:01 -070044 : fDistanceAdjustTable(new GrDistanceFieldAdjustTable), fOptions(options) {
45 SanitizeOptions(&fOptions);
joshualitt9bd2daf2015-04-17 09:30:06 -070046}
47
Herb Derby26cbe512018-05-24 14:39:01 -040048std::unique_ptr<GrTextContext> GrTextContext::Make(const Options& options) {
49 return std::unique_ptr<GrTextContext>(new GrTextContext(options));
joshualitt1d89e8d2015-04-01 12:40:54 -070050}
51
Herb Derby26cbe512018-05-24 14:39:01 -040052SkColor GrTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
Mike Reedec7278e2019-02-01 14:00:34 -050053 SkColor canonicalColor = SkPaintPriv::ComputeLuminanceColor(paint);
joshualitt9e36c1a2015-04-14 12:17:27 -070054 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 Derby26cbe512018-05-24 14:39:01 -040073SkScalerContextFlags GrTextContext::ComputeScalerContextFlags(
Herb Derbyd8327a82018-01-22 14:39:27 -050074 const GrColorSpaceInfo& colorSpaceInfo) {
Brian Osman34ec3742018-07-03 10:40:57 -040075 // If we're doing linear blending, then we can disable the gamma hacks.
brianosman5280dcb2016-04-14 06:02:59 -070076 // Otherwise, leave them on. In either case, we still want the contrast boost:
Brian Osman34ec3742018-07-03 10:40:57 -040077 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
78 if (colorSpaceInfo.isLinearlyBlended()) {
Herb Derbyd8327a82018-01-22 14:39:27 -050079 return SkScalerContextFlags::kBoostContrast;
brianosman32f77822016-04-07 06:25:45 -070080 } else {
Herb Derbyd8327a82018-01-22 14:39:27 -050081 return SkScalerContextFlags::kFakeGammaAndBoostContrast;
brianosman32f77822016-04-07 06:25:45 -070082 }
83}
84
Herb Derby26cbe512018-05-24 14:39:01 -040085void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -070086 if (options->fMaxDistanceFieldFontSize < 0.f) {
87 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
88 }
89 if (options->fMinDistanceFieldFontSize < 0.f) {
90 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
91 }
92}
93
Mike Reedf2b074e2018-12-03 16:52:59 -050094bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& paint, const SkFont& font,
95 const SkMatrix& viewMatrix,
Herb Derby26cbe512018-05-24 14:39:01 -040096 const SkSurfaceProps& props,
97 bool contextSupportsDistanceFieldText,
98 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -050099 if (!viewMatrix.hasPerspective()) {
100 SkScalar maxScale = viewMatrix.getMaxScale();
Mike Reedf2b074e2018-12-03 16:52:59 -0500101 SkScalar scaledTextSize = maxScale * font.getSize();
Brian Salomon52db9402017-11-07 14:58:55 -0500102 // Hinted text looks far better at small resolutions
103 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700104 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
105 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500106 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 Reed8ad91a92018-01-19 19:09:32 -0500119 // mask filters modify alpha, which doesn't translate well to distance
Mike Reedf2b074e2018-12-03 16:52:59 -0500120 if (paint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500121 return false;
122 }
123
124 // TODO: add some stroking support
Mike Reedf2b074e2018-12-03 16:52:59 -0500125 if (paint.getStyle() != SkPaint::kFill_Style) {
Brian Salomon52db9402017-11-07 14:58:55 -0500126 return false;
127 }
128
129 return true;
130}
131
Herb Derby881c9622019-02-19 11:43:12 -0500132SkScalar scaled_text_size(const SkScalar textSize, const SkMatrix& viewMatrix) {
Brian Salomon52db9402017-11-07 14:58:55 -0500133 SkScalar scaledTextSize = textSize;
134
135 if (viewMatrix.hasPerspective()) {
136 // for perspective, we simply force to the medium size
137 // TODO: compute a size based on approximate screen area
138 scaledTextSize = kMediumDFFontLimit;
139 } else {
140 SkScalar maxScale = viewMatrix.getMaxScale();
141 // if we have non-unity scale, we need to choose our base text size
142 // based on the SkPaint's text size multiplied by the max scale factor
143 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
144 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
145 scaledTextSize *= maxScale;
146 }
147 }
148
Herb Derby881c9622019-02-19 11:43:12 -0500149 return scaledTextSize;
150}
151
152SkFont GrTextContext::InitDistanceFieldFont(const SkFont& font,
153 const SkMatrix& viewMatrix,
154 const Options& options,
155 SkScalar* textRatio) {
156 SkScalar textSize = font.getSize();
157 SkScalar scaledTextSize = scaled_text_size(textSize, viewMatrix);
158
159 SkFont dfFont{font};
160
161 if (scaledTextSize <= kSmallDFFontLimit) {
162 *textRatio = textSize / kSmallDFFontSize;
163 dfFont.setSize(SkIntToScalar(kSmallDFFontSize));
164 } else if (scaledTextSize <= kMediumDFFontLimit) {
165 *textRatio = textSize / kMediumDFFontSize;
166 dfFont.setSize(SkIntToScalar(kMediumDFFontSize));
167 } else {
168 *textRatio = textSize / kLargeDFFontSize;
169 dfFont.setSize(SkIntToScalar(kLargeDFFontSize));
170 }
171
172 dfFont.setEdging(SkFont::Edging::kAntiAlias);
173 dfFont.setForceAutoHinting(false);
Ben Wagner5785e4a2019-05-07 16:50:29 -0400174 dfFont.setHinting(SkFontHinting::kNormal);
Herb Derby7921c012019-04-08 13:37:24 -0400175
176 // The sub-pixel position will always happen when transforming to the screen.
177 dfFont.setSubpixel(false);
Herb Derby881c9622019-02-19 11:43:12 -0500178 return dfFont;
179}
180
181std::pair<SkScalar, SkScalar> GrTextContext::InitDistanceFieldMinMaxScale(
182 SkScalar textSize,
183 const SkMatrix& viewMatrix,
184 const GrTextContext::Options& options) {
185
186 SkScalar scaledTextSize = scaled_text_size(textSize, viewMatrix);
187
Brian Salomon52db9402017-11-07 14:58:55 -0500188 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
189 // and ceiling. A scale outside of this range would require regenerating the distance fields
190 SkScalar dfMaskScaleFloor;
191 SkScalar dfMaskScaleCeil;
192 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700193 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500194 dfMaskScaleCeil = kSmallDFFontLimit;
Brian Salomon52db9402017-11-07 14:58:55 -0500195 } else if (scaledTextSize <= kMediumDFFontLimit) {
196 dfMaskScaleFloor = kSmallDFFontLimit;
197 dfMaskScaleCeil = kMediumDFFontLimit;
Brian Salomon52db9402017-11-07 14:58:55 -0500198 } else {
199 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700200 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500201 }
202
203 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
204 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
205 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
206 // tolerate before we'd have to move to a large mip size. When we actually test these values
207 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
208 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
209 // level)
210 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Brian Salomon52db9402017-11-07 14:58:55 -0500211
Herb Derby881c9622019-02-19 11:43:12 -0500212 return std::make_pair(dfMaskScaleFloor / scaledTextSize, dfMaskScaleCeil / scaledTextSize);
213}
Jim Van Verthd401da62018-05-03 10:40:30 -0400214
Herb Derby881c9622019-02-19 11:43:12 -0500215SkPaint GrTextContext::InitDistanceFieldPaint(const SkPaint& paint) {
216 SkPaint dfPaint{paint};
217 dfPaint.setMaskFilter(GrSDFMaskFilter::Make());
218 return dfPaint;
219}
Khushal3e7548c2018-05-23 15:45:01 -0700220
joshualitt79dfb2b2015-05-11 08:58:08 -0700221///////////////////////////////////////////////////////////////////////////////////////////////////
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400222
Hal Canary6f6961e2017-01-31 13:50:44 -0500223#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700224
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500225#include "src/gpu/GrRenderTargetContext.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -0400226
Brian Salomon44acb5b2017-07-18 19:59:24 -0400227GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700228 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400229 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700230 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700231
Robert Phillipsfd0d9702019-02-01 10:19:42 -0500232 if (context->priv().contextID() != gContextID) {
233 gContextID = context->priv().contextID();
Herb Derby26cbe512018-05-24 14:39:01 -0400234 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700235 }
236
Brian Osman11052242016-10-27 14:47:55 -0400237 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Brian Salomonbf6b9792019-08-21 09:38:10 -0400238 auto rtc = context->priv().makeDeferredRenderTargetContext(SkBackingFit::kApprox, 1024, 1024,
239 GrColorType::kRGBA_8888, nullptr);
brianosman8fe485b2016-07-25 12:31:51 -0700240
joshualitt6c891102015-05-13 08:51:49 -0700241 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400242
joshualitt79dfb2b2015-05-11 08:58:08 -0700243 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500244 skPaint.setColor(random->nextU());
Mike Reed191e64b2019-01-02 15:35:29 -0500245
246 SkFont font;
247 if (random->nextBool()) {
248 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
249 } else {
250 font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
251 }
252 font.setSubpixel(random->nextBool());
joshualitt79dfb2b2015-05-11 08:58:08 -0700253
joshualitt79dfb2b2015-05-11 08:58:08 -0700254 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700255
joshualittb8d86492016-02-24 09:23:03 -0800256 // create some random x/y offsets, including negative offsets
257 static const int kMaxTrans = 1024;
258 int xPos = (random->nextU() % 2) * 2 - 1;
259 int yPos = (random->nextU() % 2) * 2 - 1;
260 int xInt = (random->nextU() % kMaxTrans) * xPos;
261 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700262
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500263 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
Mike Reed191e64b2019-01-02 15:35:29 -0500264 skPaint, font, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700265}
266
267#endif