blob: ca9d809a5937fb14d5c23e5f8d5c3d188a6f6126 [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"
Brian Salomon52db9402017-11-07 14:58:55 -050018#include "SkFindAndPlaceGlyph.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040019#include "SkGlyphRun.h"
Brian Osman3b655982017-03-07 16:58:08 -050020#include "SkGr.h"
Jim Van Verthc65b65d2018-01-16 16:26:35 -050021#include "SkGraphics.h"
Brian Salomonaf597482017-11-07 16:23:34 -050022#include "SkMakeUnique.h"
Mike Reed80747ef2018-01-23 15:29:32 -050023#include "SkMaskFilterBase.h"
Herb Derbyeb3f6742018-03-05 14:36:45 -050024#include "SkPaintPriv.h"
Jim Van Verth54d9c882018-02-08 16:14:48 -050025#include "SkTextMapStateProc.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040026#include "SkTo.h"
Brian Salomon649a3412017-03-09 13:50:43 -050027#include "ops/GrMeshDrawOp.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070028
Brian Salomonaf597482017-11-07 16:23:34 -050029// DF sizes and thresholds for usage of the small and medium sizes. For example, above
30// kSmallDFFontLimit we will use the medium size. The large size is used up until the size at
31// which we switch over to drawing as paths as controlled by Options.
32static const int kSmallDFFontSize = 32;
33static const int kSmallDFFontLimit = 32;
Jim Van Verth825d4d72018-01-30 20:37:03 +000034static const int kMediumDFFontSize = 72;
35static const int kMediumDFFontLimit = 72;
36static const int kLargeDFFontSize = 162;
Brian Salomonaf597482017-11-07 16:23:34 -050037
38static const int kDefaultMinDistanceFieldFontSize = 18;
39#ifdef SK_BUILD_FOR_ANDROID
40static const int kDefaultMaxDistanceFieldFontSize = 384;
41#else
42static const int kDefaultMaxDistanceFieldFontSize = 2 * kLargeDFFontSize;
43#endif
44
Herb Derby26cbe512018-05-24 14:39:01 -040045GrTextContext::GrTextContext(const Options& options)
Khushal3e7548c2018-05-23 15:45:01 -070046 : fDistanceAdjustTable(new GrDistanceFieldAdjustTable), fOptions(options) {
47 SanitizeOptions(&fOptions);
joshualitt9bd2daf2015-04-17 09:30:06 -070048}
49
Herb Derby26cbe512018-05-24 14:39:01 -040050std::unique_ptr<GrTextContext> GrTextContext::Make(const Options& options) {
51 return std::unique_ptr<GrTextContext>(new GrTextContext(options));
joshualitt1d89e8d2015-04-01 12:40:54 -070052}
53
Herb Derby26cbe512018-05-24 14:39:01 -040054SkColor GrTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -050055 SkColor canonicalColor = paint.computeLuminanceColor();
joshualitt9e36c1a2015-04-14 12:17:27 -070056 if (lcd) {
57 // This is the correct computation, but there are tons of cases where LCD can be overridden.
58 // For now we just regenerate if any run in a textblob has LCD.
59 // TODO figure out where all of these overrides are and see if we can incorporate that logic
60 // at a higher level *OR* use sRGB
61 SkASSERT(false);
62 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
63 } else {
64 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
65 // gamma corrected masks anyways, nor color
66 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
67 SkColorGetG(canonicalColor),
68 SkColorGetB(canonicalColor));
69 // reduce to our finite number of bits
70 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
71 }
72 return canonicalColor;
73}
74
Herb Derby26cbe512018-05-24 14:39:01 -040075SkScalerContextFlags GrTextContext::ComputeScalerContextFlags(
Herb Derbyd8327a82018-01-22 14:39:27 -050076 const GrColorSpaceInfo& colorSpaceInfo) {
Brian Osman34ec3742018-07-03 10:40:57 -040077 // If we're doing linear blending, then we can disable the gamma hacks.
brianosman5280dcb2016-04-14 06:02:59 -070078 // Otherwise, leave them on. In either case, we still want the contrast boost:
Brian Osman34ec3742018-07-03 10:40:57 -040079 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
80 if (colorSpaceInfo.isLinearlyBlended()) {
Herb Derbyd8327a82018-01-22 14:39:27 -050081 return SkScalerContextFlags::kBoostContrast;
brianosman32f77822016-04-07 06:25:45 -070082 } else {
Herb Derbyd8327a82018-01-22 14:39:27 -050083 return SkScalerContextFlags::kFakeGammaAndBoostContrast;
brianosman32f77822016-04-07 06:25:45 -070084 }
85}
86
Herb Derby6de0fca2018-08-01 14:49:26 -040087bool glyph_too_big_for_atlas(const SkGlyph& glyph) {
88 return glyph.fWidth >= 256 || glyph.fHeight >= 256;
89}
90
91static SkRect rect_to_draw(
92 const SkGlyph& glyph, SkPoint origin, SkScalar textScale, GrGlyph::MaskStyle maskStyle) {
93
94 SkScalar dx = SkIntToScalar(glyph.fLeft);
95 SkScalar dy = SkIntToScalar(glyph.fTop);
96 SkScalar width = SkIntToScalar(glyph.fWidth);
97 SkScalar height = SkIntToScalar(glyph.fHeight);
98
99 if (maskStyle == GrGlyph::kDistance_MaskStyle) {
100 dx += SK_DistanceFieldInset;
101 dy += SK_DistanceFieldInset;
102 width -= 2 * SK_DistanceFieldInset;
103 height -= 2 * SK_DistanceFieldInset;
104 }
105
106 dx *= textScale;
107 dy *= textScale;
108 width *= textScale;
109 height *= textScale;
110
111 return SkRect::MakeXYWH(origin.x() + dx, origin.y() + dy, width, height);
112}
Herb Derby12e42562018-07-28 14:27:48 -0400113
Herb Derby12e42562018-07-28 14:27:48 -0400114void GrTextContext::regenerateGlyphRunList(GrTextBlob* cacheBlob,
115 GrGlyphCache* glyphCache,
116 const GrShaderCaps& shaderCaps,
117 const GrTextUtils::Paint& paint,
118 SkScalerContextFlags scalerContextFlags,
119 const SkMatrix& viewMatrix,
120 const SkSurfaceProps& props,
Herb Derby74c6ed32018-07-28 18:07:54 -0400121 const SkGlyphRunList& glyphRunList,
122 SkGlyphRunListDrawer* glyphDrawer) {
Herb Derby12e42562018-07-28 14:27:48 -0400123 SkPoint origin = glyphRunList.origin();
124 cacheBlob->initReusableBlob(paint.luminanceColor(), viewMatrix, origin.x(), origin.y());
125
126 // Regenerate textblob
127 GrTextUtils::RunPaint runPaint(&paint);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400128 int runIndex = 0;
Herb Derby12e42562018-07-28 14:27:48 -0400129 for (const auto& glyphRun : glyphRunList) {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400130 cacheBlob->push_back_run(runIndex);
Herb Derby12e42562018-07-28 14:27:48 -0400131
132 if (!runPaint.modifyForRun([glyphRun](SkPaint* p) { *p = glyphRun.paint(); })) {
133 continue;
134 }
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400135 cacheBlob->setRunPaintFlags(runIndex, runPaint.skPaint().getFlags());
Herb Derby12e42562018-07-28 14:27:48 -0400136
137 if (CanDrawAsDistanceFields(runPaint, viewMatrix, props,
138 shaderCaps.supportsDistanceFieldText(), fOptions)) {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400139 bool hasWCoord = viewMatrix.hasPerspective()
140 || fOptions.fDistanceFieldVerticesAlwaysHaveW;
141
142 // Setup distance field runPaint and text ratio
143 SkScalar textRatio;
144 SkPaint dfPaint(runPaint);
145 SkScalerContextFlags flags;
146 InitDistanceFieldPaint(cacheBlob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
147 cacheBlob->setHasDistanceField();
148 cacheBlob->setSubRunHasDistanceFields(runIndex, runPaint.skPaint().isLCDRenderText(),
149 runPaint.skPaint().isAntiAlias(), hasWCoord);
150
151 FallbackGlyphRunHelper fallbackTextHelper(
152 viewMatrix, runPaint, glyphCache->getGlyphSizeLimit(), textRatio);
153
154 sk_sp<GrTextStrike> currStrike;
155
156 {
157 auto cache = cacheBlob->setupCache(runIndex, props, flags, dfPaint, nullptr);
158
159 const SkPoint* positionCursor = glyphRun.positions().data();
160 for (auto glyphID : glyphRun.shuntGlyphsIDs()) {
161 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
162 SkPoint glyphPos = origin + *positionCursor++;
163 if (glyph.fWidth > 0) {
164 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby6de0fca2018-08-01 14:49:26 -0400165
Herb Derby049b5d92018-08-01 13:56:26 -0400166 AppendGlyph(
167 cacheBlob, runIndex, glyphCache, &currStrike,
168 glyph, GrGlyph::kDistance_MaskStyle,
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400169 glyphPos.fX, glyphPos.fY, runPaint.filteredPremulColor(),
Herb Derby049b5d92018-08-01 13:56:26 -0400170 cache.get(), textRatio, true);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400171 } else {
172 // can't append non-SDF glyph to SDF batch, send to fallback
173 fallbackTextHelper.appendText(glyph, glyphID, glyphPos);
174 }
175 }
176 }
177 }
178
179 fallbackTextHelper.drawText(
180 cacheBlob, runIndex, glyphCache, props, runPaint, scalerContextFlags);
181
182 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
183 // Ensure the blob is set for bitmaptext
184 cacheBlob->setHasBitmap();
185
186 // setup our std runPaint, in hopes of getting hits in the cache
187 SkPaint pathPaint(runPaint);
188 SkScalar matrixScale = pathPaint.setupForAsPaths();
189
190 FallbackGlyphRunHelper fallbackTextHelper(
191 viewMatrix, runPaint, glyphCache->getGlyphSizeLimit(), matrixScale);
192
193 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
194 pathPaint.setStyle(SkPaint::kFill_Style);
195 pathPaint.setPathEffect(nullptr);
196
197 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
198 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
199
Herb Derby0ab5ce12018-07-30 10:10:40 -0400200 auto drawOnePath =
201 [&fallbackTextHelper, matrixScale, runIndex, cacheBlob]
202 (const SkPath* path, const SkGlyph& glyph, SkPoint position) {
203 if (glyph.fMaskFormat == SkMask::kARGB32_Format) {
204 fallbackTextHelper.appendText(glyph, glyph.getGlyphID(), position);
205 } else {
206 if (path != nullptr) {
207 cacheBlob->appendPathGlyph(
208 runIndex, *path, position.fX, position.fY, matrixScale, false);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400209 }
210 }
Herb Derby0ab5ce12018-07-30 10:10:40 -0400211 };
212
213 glyphDrawer->drawUsingPaths(glyphRun, origin, cache.get(), drawOnePath);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400214
215 fallbackTextHelper.drawText(
216 cacheBlob, runIndex, glyphCache, props, runPaint, scalerContextFlags);
217
Herb Derby12e42562018-07-28 14:27:48 -0400218 } else {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400219 // Ensure the blob is set for bitmaptext
220 cacheBlob->setHasBitmap();
221 sk_sp<GrTextStrike> currStrike;
222 auto cache = cacheBlob->setupCache(
223 runIndex, props, scalerContextFlags, runPaint, &viewMatrix);
Herb Derby74c6ed32018-07-28 18:07:54 -0400224
225 auto drawOneGlyph =
226 [cacheBlob, runIndex, glyphCache, &currStrike, runPaint, cache{cache.get()}]
227 (const SkMask& mask, const SkGlyph& glyph, SkPoint position) {
Herb Derby049b5d92018-08-01 13:56:26 -0400228 AppendGlyph(cacheBlob, runIndex, glyphCache, &currStrike,
Herb Derby6de0fca2018-08-01 14:49:26 -0400229 glyph, GrGlyph::kCoverage_MaskStyle,
230 SkScalarFloorToScalar(position.fX),
231 SkScalarFloorToScalar(position.fY),
232 runPaint.filteredPremulColor(), cache, SK_Scalar1, false);
Herb Derby74c6ed32018-07-28 18:07:54 -0400233 };
234
235 glyphDrawer->drawUsingMasks(cache.get(), glyphRun, origin, viewMatrix, drawOneGlyph);
Herb Derby12e42562018-07-28 14:27:48 -0400236 }
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400237 runIndex += 1;
Herb Derby12e42562018-07-28 14:27:48 -0400238 }
239}
240
Herb Derby6de0fca2018-08-01 14:49:26 -0400241void GrTextContext::AppendGlyph(GrTextBlob* blob, int runIndex,
242 GrGlyphCache* grGlyphCache,
243 sk_sp<GrTextStrike>* strike,
244 const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
245 SkScalar sx, SkScalar sy,
246 GrColor color, SkGlyphCache* skGlyphCache,
247 SkScalar textRatio, bool needsTransform) {
248 if (!*strike) {
249 *strike = grGlyphCache->getStrike(skGlyphCache);
250 }
251
252 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
253 skGlyph.getSubXFixed(),
254 skGlyph.getSubYFixed(),
255 maskStyle);
256 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
257 if (!glyph) {
258 return;
259 }
260
261 SkASSERT(skGlyph.fWidth == glyph->width());
262 SkASSERT(skGlyph.fHeight == glyph->height());
263
264 SkRect glyphRect = rect_to_draw(skGlyph, {sx, sy}, textRatio, maskStyle);
265
266 if (!glyphRect.isEmpty()) {
267 // If the glyph is too large we fall back to paths
268 if (glyph_too_big_for_atlas(skGlyph)) {
269 if (glyph->fPath != nullptr) {
270 const SkPath* glyphPath = skGlyphCache->findPath(skGlyph);
271 if (glyphPath != nullptr) {
272 glyph->fPath = new SkPath(*glyphPath);
273 }
274 blob->appendPathGlyph(runIndex, *glyph->fPath, sx, sy, textRatio, !needsTransform);
275 }
276 } else {
277 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph,
278 skGlyphCache, skGlyph, sx, sy, textRatio, !needsTransform);
279 }
280 }
281}
282
Herb Derbycddab252018-07-16 11:19:04 -0400283void GrTextContext::drawGlyphRunList(
284 GrContext* context, GrTextUtils::Target* target, const GrClip& clip,
Herb Derbyb935cf82018-07-26 16:54:18 -0400285 const SkMatrix& viewMatrix, const SkSurfaceProps& props, const SkGlyphRunList& glyphRunList,
Herb Derbycddab252018-07-16 11:19:04 -0400286 const SkIRect& clipBounds) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400287 SkPoint origin = glyphRunList.origin();
joshualitt9e36c1a2015-04-14 12:17:27 -0700288
Herb Derbycddab252018-07-16 11:19:04 -0400289 // Get the first paint to use as the key paint.
Herb Derbyb935cf82018-07-26 16:54:18 -0400290 const SkPaint& skPaint = glyphRunList.paint();
Herb Derbycddab252018-07-16 11:19:04 -0400291
joshualitt9b8e79e2015-04-24 09:57:12 -0700292 // If we have been abandoned, then don't draw
Khushalc421ca12018-06-26 14:38:34 -0700293 if (context->abandoned()) {
robertphillipsea461502015-05-26 11:38:03 -0700294 return;
295 }
296
Mike Reed80747ef2018-01-23 15:29:32 -0500297 SkMaskFilterBase::BlurRec blurRec;
joshualitt53b5f442015-04-13 06:33:59 -0700298 // It might be worth caching these things, but its not clear at this time
299 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
300 const SkMaskFilter* mf = skPaint.getMaskFilter();
Herb Derbyb935cf82018-07-26 16:54:18 -0400301 bool canCache = glyphRunList.canCache() && !(skPaint.getPathEffect() ||
Herb Derbycddab252018-07-16 11:19:04 -0400302 (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derbyd8327a82018-01-22 14:39:27 -0500303 SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
joshualitt2a0e9f32015-04-13 06:12:21 -0700304
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500305 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500306 GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache();
307
Herb Derbycddab252018-07-16 11:19:04 -0400308 sk_sp<GrTextBlob> cacheBlob;
309 GrTextBlob::Key key;
joshualitt2a0e9f32015-04-13 06:12:21 -0700310 if (canCache) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400311 bool hasLCD = glyphRunList.anyRunsLCD();
joshualitte4cee1f2015-05-11 13:04:28 -0700312
313 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
joshualitt2c89bc12016-02-11 05:42:30 -0800314 SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() :
Herb Derbycddab252018-07-16 11:19:04 -0400315 kUnknown_SkPixelGeometry;
joshualitte4cee1f2015-05-11 13:04:28 -0700316
joshualitt9e36c1a2015-04-14 12:17:27 -0700317 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
318 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
319 // ensure we always match the same key
320 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
Herb Derbycddab252018-07-16 11:19:04 -0400321 ComputeCanonicalColor(skPaint, hasLCD);
joshualitt9e36c1a2015-04-14 12:17:27 -0700322
joshualitte4cee1f2015-05-11 13:04:28 -0700323 key.fPixelGeometry = pixelGeometry;
Herb Derbyb935cf82018-07-26 16:54:18 -0400324 key.fUniqueID = glyphRunList.uniqueID();
joshualitt53b5f442015-04-13 06:33:59 -0700325 key.fStyle = skPaint.getStyle();
326 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700327 key.fCanonicalColor = canonicalColor;
brianosman8d7ffce2016-04-21 08:29:06 -0700328 key.fScalerContextFlags = scalerContextFlags;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500329 cacheBlob = textBlobCache->find(key);
joshualitt2a0e9f32015-04-13 06:12:21 -0700330 }
331
Brian Salomonf18b1d82017-10-27 11:30:49 -0400332 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
joshualittb7133be2015-04-08 09:08:31 -0700333 if (cacheBlob) {
Herb Derbycddab252018-07-16 11:19:04 -0400334 if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, origin.x(), origin.y())) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700335 // We have to remake the blob because changes may invalidate our masks.
336 // TODO we could probably get away reuse most of the time if the pointer is unique,
337 // but we'd have to clear the subrun information
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500338 textBlobCache->remove(cacheBlob.get());
Herb Derbycddab252018-07-16 11:19:04 -0400339 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
340 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400341 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derby74c6ed32018-07-28 18:07:54 -0400342 scalerContextFlags, viewMatrix, props, glyphRunList,
343 target->glyphDrawer());
joshualittb7133be2015-04-08 09:08:31 -0700344 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500345 textBlobCache->makeMRU(cacheBlob.get());
joshualitt2f2ee832016-02-10 08:52:24 -0800346
347 if (CACHE_SANITY_CHECK) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400348 int glyphCount = glyphRunList.totalGlyphCount();
349 int runCount = glyphRunList.runCount();
Herb Derby86240592018-05-24 16:12:31 -0400350 sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
joshualitt92303772016-02-10 11:55:52 -0800351 sanityBlob->setupKey(key, blurRec, skPaint);
Herb Derbycddab252018-07-16 11:19:04 -0400352 this->regenerateGlyphRunList(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400353 sanityBlob.get(), glyphCache, *context->contextPriv().caps()->shaderCaps(),
Herb Derby74c6ed32018-07-28 18:07:54 -0400354 paint, scalerContextFlags, viewMatrix, props, glyphRunList,
355 target->glyphDrawer());
Herb Derby86240592018-05-24 16:12:31 -0400356 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700357 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700358 }
359 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700360 if (canCache) {
Herb Derbycddab252018-07-16 11:19:04 -0400361 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
joshualitt2a0e9f32015-04-13 06:12:21 -0700362 } else {
Herb Derbycddab252018-07-16 11:19:04 -0400363 cacheBlob = textBlobCache->makeBlob(glyphRunList);
joshualitt2a0e9f32015-04-13 06:12:21 -0700364 }
Herb Derbycddab252018-07-16 11:19:04 -0400365 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400366 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derby74c6ed32018-07-28 18:07:54 -0400367 scalerContextFlags, viewMatrix, props, glyphRunList,
368 target->glyphDrawer());
joshualitt1d89e8d2015-04-01 12:40:54 -0700369 }
370
Robert Phillips5a66efb2018-03-07 15:13:18 -0500371 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400372 clip, viewMatrix, clipBounds, origin.x(), origin.y());
joshualitt1d89e8d2015-04-01 12:40:54 -0700373}
374
Herb Derby86240592018-05-24 16:12:31 -0400375inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400376GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
377 GrGlyphCache* glyphCache,
378 const GrShaderCaps& shaderCaps,
379 const GrTextUtils::Paint& paint,
380 SkScalerContextFlags scalerContextFlags,
381 const SkMatrix& viewMatrix,
382 const SkSurfaceProps& props,
383 const char text[], size_t byteLength,
384 const SkScalar pos[], int scalarsPerPosition, const
385 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500386 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400387 if (!glyphCount) {
388 return nullptr;
389 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700390
Herb Derby86240592018-05-24 16:12:31 -0400391 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800392 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500393 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700394
Khushal3e7548c2018-05-23 15:45:01 -0700395 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
396 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500397 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500398 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700399 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500400 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
401 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700402 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700403 return blob;
404}
405
Herb Derby26cbe512018-05-24 14:39:01 -0400406void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
407 const GrClip& clip, const SkPaint& skPaint,
408 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
409 const char text[], size_t byteLength, const SkScalar pos[],
410 int scalarsPerPosition, const SkPoint& offset,
411 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400412 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Khushalc421ca12018-06-26 14:38:34 -0700413 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800414 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500415 }
416
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500417 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500418 auto textBlobCache = context->contextPriv().getTextBlobCache();
419
Herb Derby86240592018-05-24 16:12:31 -0400420 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400421 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500422 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
423 byteLength, pos, scalarsPerPosition, offset));
424 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500425 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500426 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800427 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700428}
429
Herb Derby86240592018-05-24 16:12:31 -0400430void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400431 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
432 const GrTextUtils::Paint& paint,
433 SkScalerContextFlags scalerContextFlags,
434 const SkMatrix& viewMatrix,
435 const char text[], size_t byteLength, const SkScalar pos[],
436 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500437 SkASSERT(byteLength == 0 || text != nullptr);
438 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
439
440 // nothing to draw
441 if (text == nullptr || byteLength == 0) {
442 return;
443 }
444
445 // Ensure the blob is set for bitmaptext
446 blob->setHasBitmap();
447
Jim Van Verth54d9c882018-02-08 16:14:48 -0500448 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500449 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500450 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500451 return;
452 }
453
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000454 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500455 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500456 SkFindAndPlaceGlyph::ProcessPosText(
457 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400458 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500459 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
460 position += rounding;
Herb Derby049b5d92018-08-01 13:56:26 -0400461 AppendGlyph(blob, runIndex, glyphCache, &currStrike,
462 glyph, GrGlyph::kCoverage_MaskStyle,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500463 SkScalarFloorToScalar(position.fX),
464 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400465 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500466 });
Brian Salomon52db9402017-11-07 14:58:55 -0500467}
468
Herb Derby86240592018-05-24 16:12:31 -0400469void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400470 GrGlyphCache* glyphCache,
471 const SkSurfaceProps& props,
472 const GrTextUtils::Paint& origPaint,
473 SkScalerContextFlags scalerContextFlags,
474 const SkMatrix& viewMatrix,
475 const char text[], size_t byteLength,
476 const SkScalar pos[], int scalarsPerPosition,
477 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500478 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
479
480 // nothing to draw
481 if (text == nullptr || byteLength == 0) {
482 return;
483 }
484
485 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500486 SkPaint pathPaint(origPaint);
487 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700488 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
489 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500490
491 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500492 pathPaint.setStyle(SkPaint::kFill_Style);
493 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500494
Jim Van Verthc401bb92018-02-15 14:05:24 -0500495 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500496 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400497 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400498 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500499
500 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500501 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500502 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
503
504 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400505 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500506 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500507 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400508 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500509 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
510 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
511 } else {
512 const SkPath* path = cache->findPath(glyph);
513 if (path) {
514 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
515 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500516 }
517 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500518 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500519 pos += scalarsPerPosition;
520 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500521
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500522 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500523}
524
Herb Derby26cbe512018-05-24 14:39:01 -0400525void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700526 if (options->fMaxDistanceFieldFontSize < 0.f) {
527 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
528 }
529 if (options->fMinDistanceFieldFontSize < 0.f) {
530 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
531 }
532}
533
Herb Derby26cbe512018-05-24 14:39:01 -0400534bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
535 const SkSurfaceProps& props,
536 bool contextSupportsDistanceFieldText,
537 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500538 if (!viewMatrix.hasPerspective()) {
539 SkScalar maxScale = viewMatrix.getMaxScale();
540 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
541 // Hinted text looks far better at small resolutions
542 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700543 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
544 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500545 return false;
546 }
547
548 bool useDFT = props.isUseDeviceIndependentFonts();
549#if SK_FORCE_DISTANCE_FIELD_TEXT
550 useDFT = true;
551#endif
552
553 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
554 return false;
555 }
556 }
557
Mike Reed8ad91a92018-01-19 19:09:32 -0500558 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700559 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500560 return false;
561 }
562
563 // TODO: add some stroking support
564 if (skPaint.getStyle() != SkPaint::kFill_Style) {
565 return false;
566 }
567
568 return true;
569}
570
Herb Derby86240592018-05-24 16:12:31 -0400571void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400572 SkPaint* skPaint,
573 const SkMatrix& viewMatrix,
574 const Options& options,
575 SkScalar* textRatio,
576 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500577 SkScalar textSize = skPaint->getTextSize();
578 SkScalar scaledTextSize = textSize;
579
580 if (viewMatrix.hasPerspective()) {
581 // for perspective, we simply force to the medium size
582 // TODO: compute a size based on approximate screen area
583 scaledTextSize = kMediumDFFontLimit;
584 } else {
585 SkScalar maxScale = viewMatrix.getMaxScale();
586 // if we have non-unity scale, we need to choose our base text size
587 // based on the SkPaint's text size multiplied by the max scale factor
588 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
589 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
590 scaledTextSize *= maxScale;
591 }
592 }
593
594 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
595 // and ceiling. A scale outside of this range would require regenerating the distance fields
596 SkScalar dfMaskScaleFloor;
597 SkScalar dfMaskScaleCeil;
598 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700599 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500600 dfMaskScaleCeil = kSmallDFFontLimit;
601 *textRatio = textSize / kSmallDFFontSize;
602 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
603 } else if (scaledTextSize <= kMediumDFFontLimit) {
604 dfMaskScaleFloor = kSmallDFFontLimit;
605 dfMaskScaleCeil = kMediumDFFontLimit;
606 *textRatio = textSize / kMediumDFFontSize;
607 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
608 } else {
609 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700610 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500611 *textRatio = textSize / kLargeDFFontSize;
612 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
613 }
614
615 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
616 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
617 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
618 // tolerate before we'd have to move to a large mip size. When we actually test these values
619 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
620 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
621 // level)
622 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700623 if (blob) {
624 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
625 dfMaskScaleCeil / scaledTextSize);
626 }
Brian Salomon52db9402017-11-07 14:58:55 -0500627
628 skPaint->setAntiAlias(true);
629 skPaint->setLCDRenderText(false);
630 skPaint->setAutohinted(false);
631 skPaint->setHinting(SkPaint::kNormal_Hinting);
632 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400633
634 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700635
636 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
637 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
638 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500639}
640
Herb Derby86240592018-05-24 16:12:31 -0400641void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400642 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
643 const GrTextUtils::Paint& paint,
644 SkScalerContextFlags scalerContextFlags,
645 const SkMatrix& viewMatrix, const char text[],
646 size_t byteLength, const SkScalar pos[],
647 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500648 SkASSERT(byteLength == 0 || text != nullptr);
649 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
650
651 // nothing to draw
652 if (text == nullptr || byteLength == 0) {
653 return;
654 }
655
Khushal3e7548c2018-05-23 15:45:01 -0700656 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500657
Brian Salomon52db9402017-11-07 14:58:55 -0500658 // Setup distance field paint and text ratio
659 SkScalar textRatio;
660 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700661 SkScalerContextFlags flags;
662 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500663 blob->setHasDistanceField();
664 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500665 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500666
Khushalfa8ff092018-06-06 17:46:38 -0700667 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
668 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500669
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500670 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500671
Herb Derby526819d2018-03-09 12:51:12 -0500672 {
Khushal3e7548c2018-05-23 15:45:01 -0700673 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500674 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400675 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500676
Herb Derby526819d2018-03-09 12:51:12 -0500677 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500678
Herb Derby526819d2018-03-09 12:51:12 -0500679 while (text < stop) {
680 const char* lastText = text;
681 // the last 2 parameters are ignored
Hal Canary4014ba62018-07-24 11:33:21 -0400682 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Brian Salomon52db9402017-11-07 14:58:55 -0500683
Herb Derby526819d2018-03-09 12:51:12 -0500684 if (glyph.fWidth) {
685 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400686 glyphPos.fX += pos[0];
687 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500688
Jim Van Verthd401da62018-05-03 10:40:30 -0400689 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby049b5d92018-08-01 13:56:26 -0400690 AppendGlyph(blob, runIndex, glyphCache, &currStrike,
691 glyph, GrGlyph::kDistance_MaskStyle,
692 glyphPos.fX, glyphPos.fY, paint.filteredPremulColor(),
693 cache.get(), textRatio, true);
Herb Derby526819d2018-03-09 12:51:12 -0500694 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400695 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500696 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
697 glyphPos);
698 }
Brian Salomon52db9402017-11-07 14:58:55 -0500699 }
Herb Derby526819d2018-03-09 12:51:12 -0500700 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500701 }
Brian Salomon52db9402017-11-07 14:58:55 -0500702 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000703
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500704 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500705}
706
joshualitt79dfb2b2015-05-11 08:58:08 -0700707///////////////////////////////////////////////////////////////////////////////////////////////////
708
Herb Derby26cbe512018-05-24 14:39:01 -0400709void GrTextContext::FallbackTextHelper::appendText(const SkGlyph& glyph, int count,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500710 const char* text, SkPoint glyphPos) {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400711 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
Khushalfa8ff092018-06-06 17:46:38 -0700712 if (SkScalarNearlyZero(maxDim)) return;
713
Jim Van Verthb515ae72018-05-23 16:44:55 -0400714 if (!fUseTransformedFallback) {
715 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
716 fUseTransformedFallback = true;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500717 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
Jim Van Verthc401bb92018-02-15 14:05:24 -0500718 }
719 }
720
721 fFallbackTxt.append(count, text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400722 if (fUseTransformedFallback) {
Jim Van Verth080a9282018-03-02 10:41:43 -0500723 // If there's a glyph in the font that's particularly large, it's possible
724 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
Jim Van Verth8b7284d2018-05-17 12:33:52 -0400725 // that glyph than make the others blurry, so we set a minimum size of half the
Jim Van Verth080a9282018-03-02 10:41:43 -0500726 // maximum text size to avoid this case.
Jim Van Verthb515ae72018-05-23 16:44:55 -0400727 SkScalar glyphTextSize = SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim),
Jim Van Verth080a9282018-03-02 10:41:43 -0500728 0.5f*fMaxTextSize);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400729 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500730 }
731 *fFallbackPos.append() = glyphPos;
732}
733
Herb Derby86240592018-05-24 16:12:31 -0400734void GrTextContext::FallbackTextHelper::drawText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400735 GrGlyphCache* glyphCache,
736 const SkSurfaceProps& props,
737 const GrTextUtils::Paint& paint,
738 SkScalerContextFlags scalerContextFlags) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500739 if (fFallbackTxt.count()) {
740 blob->initOverride(runIndex);
741 blob->setHasBitmap();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400742 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
Herb Derby526819d2018-03-09 12:51:12 -0500743 SkExclusiveStrikePtr cache;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500744 const SkPaint& skPaint = paint.skPaint();
745 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400746 SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500747 SkColor textColor = paint.filteredPremulColor();
Khushalfa8ff092018-06-06 17:46:38 -0700748
Jim Van Verthc401bb92018-02-15 14:05:24 -0500749 SkScalar textRatio = SK_Scalar1;
Khushalfa8ff092018-06-06 17:46:38 -0700750 SkPaint fallbackPaint(skPaint);
751 SkMatrix matrix = fViewMatrix;
752 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
753 cache = blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500754
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500755 sk_sp<GrTextStrike> currStrike;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500756 const char* text = fFallbackTxt.begin();
757 const char* stop = text + fFallbackTxt.count();
758 SkPoint* glyphPos = fFallbackPos.begin();
759 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400760 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400761 if (!fUseTransformedFallback) {
762 fViewMatrix.mapPoints(glyphPos, 1);
Jim Van Verth76e85162018-03-29 13:46:56 -0400763 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
764 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
765 }
Herb Derby049b5d92018-08-01 13:56:26 -0400766 GrTextContext::AppendGlyph(blob, runIndex, glyphCache, &currStrike,
767 glyph, GrGlyph::kCoverage_MaskStyle,
768 glyphPos->fX, glyphPos->fY, textColor,
769 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500770 glyphPos++;
771 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500772 }
773}
774
Khushalfa8ff092018-06-06 17:46:38 -0700775void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
776 SkMatrix* matrix) const {
777 if (!fUseTransformedFallback) return;
778
779 paint->setTextSize(fTransformedFallbackTextSize);
780 *textRatio = fTextSize / fTransformedFallbackTextSize;
781 *matrix = SkMatrix::I();
782}
783
Jim Van Verthc401bb92018-02-15 14:05:24 -0500784///////////////////////////////////////////////////////////////////////////////////////////////////
785
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400786void GrTextContext::FallbackGlyphRunHelper::appendText(
787 const SkGlyph& glyph, SkGlyphID glyphID, SkPoint glyphPos) {
788 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
789 if (SkScalarNearlyZero(maxDim)) return;
790
791 if (!fUseTransformedFallback) {
792 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
793 fUseTransformedFallback = true;
794 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
795 }
796 }
797
798 fFallbackTxt.push_back(glyphID);
799 if (fUseTransformedFallback) {
800 // If there's a glyph in the font that's particularly large, it's possible
801 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
802 // that glyph than make the others blurry, so we set a minimum size of half the
803 // maximum text size to avoid this case.
804 SkScalar glyphTextSize =
805 SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim), 0.5f*fMaxTextSize);
806 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
807 }
808 fFallbackPos.push_back(glyphPos);
809}
810
811void GrTextContext::FallbackGlyphRunHelper::drawText(
812 GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props,
813 const GrTextUtils::Paint& paint, SkScalerContextFlags scalerContextFlags) {
814 if (!fFallbackTxt.empty()) {
815 blob->initOverride(runIndex);
816 blob->setHasBitmap();
817 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
818 const SkPaint& skPaint = paint.skPaint();
819 SkColor textColor = paint.filteredPremulColor();
820
821 SkScalar textRatio = SK_Scalar1;
822 SkPaint fallbackPaint(skPaint);
823 SkMatrix matrix = fViewMatrix;
824 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
825 SkExclusiveStrikePtr cache =
826 blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
827
828 sk_sp<GrTextStrike> currStrike;
829 auto glyphPos = fFallbackPos.begin();
830 for (auto glyphID : fFallbackTxt) {
831 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
832 if (!fUseTransformedFallback) {
833 fViewMatrix.mapPoints(&*glyphPos, 1);
834 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
835 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
836 }
Herb Derby049b5d92018-08-01 13:56:26 -0400837 GrTextContext::AppendGlyph(blob, runIndex, glyphCache, &currStrike,
838 glyph, GrGlyph::kCoverage_MaskStyle,
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400839 glyphPos->fX, glyphPos->fY, textColor,
840 cache.get(), textRatio, fUseTransformedFallback);
841 glyphPos++;
842 }
843 }
844}
845
846void GrTextContext::FallbackGlyphRunHelper::initializeForDraw(
847 SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const {
848 if (!fUseTransformedFallback) return;
849
850 paint->setTextSize(fTransformedFallbackTextSize);
851 *textRatio = fTextSize / fTransformedFallbackTextSize;
852 *matrix = SkMatrix::I();
853}
854
855///////////////////////////////////////////////////////////////////////////////////////////////////
856
857
Hal Canary6f6961e2017-01-31 13:50:44 -0500858#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700859
Brian Salomonf18b1d82017-10-27 11:30:49 -0400860#include "GrRenderTargetContext.h"
861
Herb Derby26cbe512018-05-24 14:39:01 -0400862std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
863 GrTextContext* textContext,
864 GrRenderTargetContext* rtc,
865 const SkPaint& skPaint,
866 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400867 const char* text,
868 int x,
869 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500870 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500871
872 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
873
874 size_t textLen = (int)strlen(text);
875
876 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
877
878 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
879 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000880
881 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400882 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -0400883 builder.drawText(skPaint, text, textLen, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400884 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000885
Herb Derby8a6348e2018-07-12 15:30:35 -0400886 auto glyphRunList = builder.useGlyphRunList();
Herb Derbyb935cf82018-07-26 16:54:18 -0400887 if (!glyphRunList.empty()) {
888 auto glyphRun = glyphRunList[0];
Herb Derby8a6348e2018-07-12 15:30:35 -0400889 // Use the text and textLen below, because we don't want to mess with the paint.
890 glyphRun.temporaryShuntToCallback(
Herb Derby59d997a2018-06-07 12:44:09 -0400891 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
892 blob = textContext->makeDrawPosTextBlob(
893 context->contextPriv().getTextBlobCache(), glyphCache,
894 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
Herb Derby8a6348e2018-07-12 15:30:35 -0400895 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps,
Herb Derbycddab252018-07-16 11:19:04 -0400896 text, textLen, pos, 2, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400897 });
Herb Derby8a6348e2018-07-12 15:30:35 -0400898 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500899
900 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500901 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500902}
903
Brian Salomon44acb5b2017-07-18 19:59:24 -0400904GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700905 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400906 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700907 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700908
909 if (context->uniqueID() != gContextID) {
910 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400911 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700912 }
913
Brian Osman11052242016-10-27 14:47:55 -0400914 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500915 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400916 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700917
joshualitt6c891102015-05-13 08:51:49 -0700918 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400919
920 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
921 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700922 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500923 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700924 skPaint.setLCDRenderText(random->nextBool());
925 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
926 skPaint.setSubpixelText(random->nextBool());
927
joshualitt79dfb2b2015-05-11 08:58:08 -0700928 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700929
joshualittb8d86492016-02-24 09:23:03 -0800930 // create some random x/y offsets, including negative offsets
931 static const int kMaxTrans = 1024;
932 int xPos = (random->nextU() % 2) * 2 - 1;
933 int yPos = (random->nextU() % 2) * 2 - 1;
934 int xInt = (random->nextU() % kMaxTrans) * xPos;
935 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700936
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500937 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
938 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700939}
940
941#endif