blob: acb01d7ba8eb3509a19e7a8087725550dc27eec5 [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
Herb Derby26cbe512018-05-24 14:39:01 -04008#include "GrTextContext.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Brian Salomonc7fe0f72018-05-11 10:14:21 -040010#include "GrCaps.h"
robertphillips73c4e642016-03-02 11:36:59 -080011#include "GrContext.h"
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050012#include "GrContextPriv.h"
Jim Van Verthd401da62018-05-03 10:40:30 -040013#include "GrSDFMaskFilter.h"
joshualittb7133be2015-04-08 09:08:31 -070014#include "GrTextBlobCache.h"
Brian Salomon52db9402017-11-07 14:58:55 -050015#include "SkDistanceFieldGen.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070016#include "SkDraw.h"
Jim Van Verth54d9c882018-02-08 16:14:48 -050017#include "SkDrawProcs.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040018#include "SkGlyphRun.h"
Brian Osman3b655982017-03-07 16:58:08 -050019#include "SkGr.h"
Jim Van Verthc65b65d2018-01-16 16:26:35 -050020#include "SkGraphics.h"
Brian Salomonaf597482017-11-07 16:23:34 -050021#include "SkMakeUnique.h"
Mike Reed80747ef2018-01-23 15:29:32 -050022#include "SkMaskFilterBase.h"
Herb Derbyeb3f6742018-03-05 14:36:45 -050023#include "SkPaintPriv.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040024#include "SkTo.h"
Brian Salomon649a3412017-03-09 13:50:43 -050025#include "ops/GrMeshDrawOp.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) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -050053 SkColor canonicalColor = paint.computeLuminanceColor();
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
Herb Derby26cbe512018-05-24 14:39:01 -040094bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
95 const SkSurfaceProps& props,
96 bool contextSupportsDistanceFieldText,
97 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -050098 if (!viewMatrix.hasPerspective()) {
99 SkScalar maxScale = viewMatrix.getMaxScale();
100 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
101 // Hinted text looks far better at small resolutions
102 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700103 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
104 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500105 return false;
106 }
107
108 bool useDFT = props.isUseDeviceIndependentFonts();
109#if SK_FORCE_DISTANCE_FIELD_TEXT
110 useDFT = true;
111#endif
112
113 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
114 return false;
115 }
116 }
117
Mike Reed8ad91a92018-01-19 19:09:32 -0500118 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700119 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500120 return false;
121 }
122
123 // TODO: add some stroking support
124 if (skPaint.getStyle() != SkPaint::kFill_Style) {
125 return false;
126 }
127
128 return true;
129}
130
Herb Derby86240592018-05-24 16:12:31 -0400131void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400132 SkPaint* skPaint,
133 const SkMatrix& viewMatrix,
134 const Options& options,
135 SkScalar* textRatio,
136 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500137 SkScalar textSize = skPaint->getTextSize();
138 SkScalar scaledTextSize = textSize;
139
140 if (viewMatrix.hasPerspective()) {
141 // for perspective, we simply force to the medium size
142 // TODO: compute a size based on approximate screen area
143 scaledTextSize = kMediumDFFontLimit;
144 } else {
145 SkScalar maxScale = viewMatrix.getMaxScale();
146 // if we have non-unity scale, we need to choose our base text size
147 // based on the SkPaint's text size multiplied by the max scale factor
148 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
149 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
150 scaledTextSize *= maxScale;
151 }
152 }
153
154 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
155 // and ceiling. A scale outside of this range would require regenerating the distance fields
156 SkScalar dfMaskScaleFloor;
157 SkScalar dfMaskScaleCeil;
158 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700159 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500160 dfMaskScaleCeil = kSmallDFFontLimit;
161 *textRatio = textSize / kSmallDFFontSize;
162 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
163 } else if (scaledTextSize <= kMediumDFFontLimit) {
164 dfMaskScaleFloor = kSmallDFFontLimit;
165 dfMaskScaleCeil = kMediumDFFontLimit;
166 *textRatio = textSize / kMediumDFFontSize;
167 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
168 } else {
169 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700170 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500171 *textRatio = textSize / kLargeDFFontSize;
172 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
173 }
174
175 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
176 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
177 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
178 // tolerate before we'd have to move to a large mip size. When we actually test these values
179 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
180 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
181 // level)
182 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700183 if (blob) {
184 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
185 dfMaskScaleCeil / scaledTextSize);
186 }
Brian Salomon52db9402017-11-07 14:58:55 -0500187
188 skPaint->setAntiAlias(true);
189 skPaint->setLCDRenderText(false);
190 skPaint->setAutohinted(false);
Mike Reed9edbf422018-11-07 19:54:33 -0500191 skPaint->setHinting(kNormal_SkFontHinting);
Brian Salomon52db9402017-11-07 14:58:55 -0500192 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400193
194 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700195
196 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
197 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
198 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500199}
200
joshualitt79dfb2b2015-05-11 08:58:08 -0700201///////////////////////////////////////////////////////////////////////////////////////////////////
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400202
Hal Canary6f6961e2017-01-31 13:50:44 -0500203#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700204
Brian Salomonf18b1d82017-10-27 11:30:49 -0400205#include "GrRenderTargetContext.h"
206
Brian Salomon44acb5b2017-07-18 19:59:24 -0400207GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700208 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400209 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700210 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700211
212 if (context->uniqueID() != gContextID) {
213 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400214 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700215 }
216
Greg Daniel4065d452018-11-16 15:43:41 -0500217 const GrBackendFormat format =
218 context->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
219
Brian Osman11052242016-10-27 14:47:55 -0400220 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500221 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500222 format, SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700223
joshualitt6c891102015-05-13 08:51:49 -0700224 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400225
joshualitt79dfb2b2015-05-11 08:58:08 -0700226 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500227 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700228 skPaint.setLCDRenderText(random->nextBool());
229 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
230 skPaint.setSubpixelText(random->nextBool());
231
joshualitt79dfb2b2015-05-11 08:58:08 -0700232 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700233
joshualittb8d86492016-02-24 09:23:03 -0800234 // create some random x/y offsets, including negative offsets
235 static const int kMaxTrans = 1024;
236 int xPos = (random->nextU() % 2) * 2 - 1;
237 int yPos = (random->nextU() % 2) * 2 - 1;
238 int xInt = (random->nextU() % kMaxTrans) * xPos;
239 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700240
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500241 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
242 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700243}
244
245#endif