blob: 93bd1826df9ae0545812f529ee0bfd31910411cb [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 Derbye42dc2d2018-08-01 14:49:26 -040087bool glyph_too_big_for_atlas(const SkGlyph& glyph) {
88 return GrDrawOpAtlas::GlyphTooLargeForAtlas(glyph.fWidth, glyph.fHeight);
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 Derbye42dc2d2018-08-01 14:49:26 -0400165
166 SkScalar sx = glyphPos.fX,
167 sy = glyphPos.fY;
168
169 if (glyph_too_big_for_atlas(glyph)) {
170 SkRect glyphRect =
171 rect_to_draw(glyph, glyphPos, textRatio,
172 GrGlyph::kDistance_MaskStyle);
173 if (!glyphRect.isEmpty()) {
174 const SkPath* glyphPath = cache->findPath(glyph);
175 if (glyphPath != nullptr) {
176 cacheBlob->appendPathGlyph(
177 runIndex, *glyphPath, sx, sy, textRatio, false);
178 }
179 }
180 } else {
181 AppendGlyph(cacheBlob, runIndex, glyphCache, &currStrike,
182 glyph, GrGlyph::kDistance_MaskStyle, sx, sy,
183 runPaint.filteredPremulColor(),
184 cache.get(), textRatio, true);
185 }
186
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400187 } else {
188 // can't append non-SDF glyph to SDF batch, send to fallback
189 fallbackTextHelper.appendText(glyph, glyphID, glyphPos);
190 }
191 }
192 }
193 }
194
195 fallbackTextHelper.drawText(
196 cacheBlob, runIndex, glyphCache, props, runPaint, scalerContextFlags);
197
198 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
199 // Ensure the blob is set for bitmaptext
200 cacheBlob->setHasBitmap();
201
202 // setup our std runPaint, in hopes of getting hits in the cache
203 SkPaint pathPaint(runPaint);
204 SkScalar matrixScale = pathPaint.setupForAsPaths();
205
206 FallbackGlyphRunHelper fallbackTextHelper(
207 viewMatrix, runPaint, glyphCache->getGlyphSizeLimit(), matrixScale);
208
209 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
210 pathPaint.setStyle(SkPaint::kFill_Style);
211 pathPaint.setPathEffect(nullptr);
212
213 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
214 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
215
Herb Derby0ab5ce12018-07-30 10:10:40 -0400216 auto drawOnePath =
217 [&fallbackTextHelper, matrixScale, runIndex, cacheBlob]
218 (const SkPath* path, const SkGlyph& glyph, SkPoint position) {
219 if (glyph.fMaskFormat == SkMask::kARGB32_Format) {
220 fallbackTextHelper.appendText(glyph, glyph.getGlyphID(), position);
221 } else {
222 if (path != nullptr) {
223 cacheBlob->appendPathGlyph(
224 runIndex, *path, position.fX, position.fY, matrixScale, false);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400225 }
226 }
Herb Derby0ab5ce12018-07-30 10:10:40 -0400227 };
228
229 glyphDrawer->drawUsingPaths(glyphRun, origin, cache.get(), drawOnePath);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400230
231 fallbackTextHelper.drawText(
232 cacheBlob, runIndex, glyphCache, props, runPaint, scalerContextFlags);
233
Herb Derby12e42562018-07-28 14:27:48 -0400234 } else {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400235 // Ensure the blob is set for bitmaptext
236 cacheBlob->setHasBitmap();
237 sk_sp<GrTextStrike> currStrike;
238 auto cache = cacheBlob->setupCache(
239 runIndex, props, scalerContextFlags, runPaint, &viewMatrix);
Herb Derby74c6ed32018-07-28 18:07:54 -0400240
Herb Derby9a52a392018-08-01 21:13:23 -0400241 auto perGlyph =
Herb Derby74c6ed32018-07-28 18:07:54 -0400242 [cacheBlob, runIndex, glyphCache, &currStrike, runPaint, cache{cache.get()}]
Herb Derby9a52a392018-08-01 21:13:23 -0400243 (const SkGlyph& glyph, SkPoint mappedPt) {
244 SkScalar sx = SkScalarFloorToScalar(mappedPt.fX),
245 sy = SkScalarFloorToScalar(mappedPt.fY);
246 AppendGlyph(cacheBlob, runIndex, glyphCache, &currStrike,
247 glyph, GrGlyph::kCoverage_MaskStyle, sx, sy,
248 runPaint.filteredPremulColor(), cache, SK_Scalar1, false);
Herb Derby74c6ed32018-07-28 18:07:54 -0400249 };
250
Herb Derby9a52a392018-08-01 21:13:23 -0400251 auto perPath =
252 [cacheBlob, runIndex]
253 (const SkPath* path, const SkGlyph& glyph, SkPoint position) {
254 SkScalar sx = SkScalarFloorToScalar(position.fX),
255 sy = SkScalarFloorToScalar(position.fY);
256 cacheBlob->appendPathGlyph(
257 runIndex, *path, sx, sy, SK_Scalar1, true);
258 };
259
260 glyphDrawer->drawGlyphRunAsGlyphWithPathFallback(
261 cache.get(), glyphRun, origin, viewMatrix, perGlyph, perPath);
Herb Derby12e42562018-07-28 14:27:48 -0400262 }
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400263 runIndex += 1;
Herb Derby12e42562018-07-28 14:27:48 -0400264 }
265}
266
Herb Derbye42dc2d2018-08-01 14:49:26 -0400267void GrTextContext::AppendGlyph(GrTextBlob* blob, int runIndex,
268 GrGlyphCache* grGlyphCache,
269 sk_sp<GrTextStrike>* strike,
270 const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
271 SkScalar sx, SkScalar sy,
272 GrColor color, SkGlyphCache* skGlyphCache,
273 SkScalar textRatio, bool needsTransform) {
274 if (!*strike) {
275 *strike = grGlyphCache->getStrike(skGlyphCache);
276 }
277
278 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
279 skGlyph.getSubXFixed(),
280 skGlyph.getSubYFixed(),
281 maskStyle);
282 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
283 if (!glyph) {
284 return;
285 }
286
287 SkASSERT(skGlyph.fWidth == glyph->width());
288 SkASSERT(skGlyph.fHeight == glyph->height());
289
290 SkRect glyphRect = rect_to_draw(skGlyph, {sx, sy}, textRatio, maskStyle);
291
292 if (!glyphRect.isEmpty()) {
293 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph,
294 skGlyphCache, skGlyph, sx, sy, textRatio, !needsTransform);
295 }
296}
297
Herb Derbycddab252018-07-16 11:19:04 -0400298void GrTextContext::drawGlyphRunList(
299 GrContext* context, GrTextUtils::Target* target, const GrClip& clip,
Herb Derbyb935cf82018-07-26 16:54:18 -0400300 const SkMatrix& viewMatrix, const SkSurfaceProps& props, const SkGlyphRunList& glyphRunList,
Herb Derbycddab252018-07-16 11:19:04 -0400301 const SkIRect& clipBounds) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400302 SkPoint origin = glyphRunList.origin();
joshualitt9e36c1a2015-04-14 12:17:27 -0700303
Herb Derbycddab252018-07-16 11:19:04 -0400304 // Get the first paint to use as the key paint.
Herb Derbyb935cf82018-07-26 16:54:18 -0400305 const SkPaint& skPaint = glyphRunList.paint();
Herb Derbycddab252018-07-16 11:19:04 -0400306
joshualitt9b8e79e2015-04-24 09:57:12 -0700307 // If we have been abandoned, then don't draw
Khushalc421ca12018-06-26 14:38:34 -0700308 if (context->abandoned()) {
robertphillipsea461502015-05-26 11:38:03 -0700309 return;
310 }
311
Mike Reed80747ef2018-01-23 15:29:32 -0500312 SkMaskFilterBase::BlurRec blurRec;
joshualitt53b5f442015-04-13 06:33:59 -0700313 // It might be worth caching these things, but its not clear at this time
314 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
315 const SkMaskFilter* mf = skPaint.getMaskFilter();
Herb Derbyb935cf82018-07-26 16:54:18 -0400316 bool canCache = glyphRunList.canCache() && !(skPaint.getPathEffect() ||
Herb Derbycddab252018-07-16 11:19:04 -0400317 (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derbyd8327a82018-01-22 14:39:27 -0500318 SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
joshualitt2a0e9f32015-04-13 06:12:21 -0700319
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500320 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500321 GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache();
322
Herb Derbycddab252018-07-16 11:19:04 -0400323 sk_sp<GrTextBlob> cacheBlob;
324 GrTextBlob::Key key;
joshualitt2a0e9f32015-04-13 06:12:21 -0700325 if (canCache) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400326 bool hasLCD = glyphRunList.anyRunsLCD();
joshualitte4cee1f2015-05-11 13:04:28 -0700327
328 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
joshualitt2c89bc12016-02-11 05:42:30 -0800329 SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() :
Herb Derbycddab252018-07-16 11:19:04 -0400330 kUnknown_SkPixelGeometry;
joshualitte4cee1f2015-05-11 13:04:28 -0700331
joshualitt9e36c1a2015-04-14 12:17:27 -0700332 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
333 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
334 // ensure we always match the same key
335 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
Herb Derbycddab252018-07-16 11:19:04 -0400336 ComputeCanonicalColor(skPaint, hasLCD);
joshualitt9e36c1a2015-04-14 12:17:27 -0700337
joshualitte4cee1f2015-05-11 13:04:28 -0700338 key.fPixelGeometry = pixelGeometry;
Herb Derbyb935cf82018-07-26 16:54:18 -0400339 key.fUniqueID = glyphRunList.uniqueID();
joshualitt53b5f442015-04-13 06:33:59 -0700340 key.fStyle = skPaint.getStyle();
341 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700342 key.fCanonicalColor = canonicalColor;
brianosman8d7ffce2016-04-21 08:29:06 -0700343 key.fScalerContextFlags = scalerContextFlags;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500344 cacheBlob = textBlobCache->find(key);
joshualitt2a0e9f32015-04-13 06:12:21 -0700345 }
346
Brian Salomonf18b1d82017-10-27 11:30:49 -0400347 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
joshualittb7133be2015-04-08 09:08:31 -0700348 if (cacheBlob) {
Herb Derbycddab252018-07-16 11:19:04 -0400349 if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, origin.x(), origin.y())) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700350 // We have to remake the blob because changes may invalidate our masks.
351 // TODO we could probably get away reuse most of the time if the pointer is unique,
352 // but we'd have to clear the subrun information
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500353 textBlobCache->remove(cacheBlob.get());
Herb Derbycddab252018-07-16 11:19:04 -0400354 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
355 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400356 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derby74c6ed32018-07-28 18:07:54 -0400357 scalerContextFlags, viewMatrix, props, glyphRunList,
358 target->glyphDrawer());
joshualittb7133be2015-04-08 09:08:31 -0700359 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500360 textBlobCache->makeMRU(cacheBlob.get());
joshualitt2f2ee832016-02-10 08:52:24 -0800361
362 if (CACHE_SANITY_CHECK) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400363 int glyphCount = glyphRunList.totalGlyphCount();
364 int runCount = glyphRunList.runCount();
Herb Derby86240592018-05-24 16:12:31 -0400365 sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
joshualitt92303772016-02-10 11:55:52 -0800366 sanityBlob->setupKey(key, blurRec, skPaint);
Herb Derbycddab252018-07-16 11:19:04 -0400367 this->regenerateGlyphRunList(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400368 sanityBlob.get(), glyphCache, *context->contextPriv().caps()->shaderCaps(),
Herb Derby74c6ed32018-07-28 18:07:54 -0400369 paint, scalerContextFlags, viewMatrix, props, glyphRunList,
370 target->glyphDrawer());
Herb Derby86240592018-05-24 16:12:31 -0400371 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700372 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700373 }
374 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700375 if (canCache) {
Herb Derbycddab252018-07-16 11:19:04 -0400376 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
joshualitt2a0e9f32015-04-13 06:12:21 -0700377 } else {
Herb Derbycddab252018-07-16 11:19:04 -0400378 cacheBlob = textBlobCache->makeBlob(glyphRunList);
joshualitt2a0e9f32015-04-13 06:12:21 -0700379 }
Herb Derbycddab252018-07-16 11:19:04 -0400380 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400381 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derby74c6ed32018-07-28 18:07:54 -0400382 scalerContextFlags, viewMatrix, props, glyphRunList,
383 target->glyphDrawer());
joshualitt1d89e8d2015-04-01 12:40:54 -0700384 }
385
Robert Phillips5a66efb2018-03-07 15:13:18 -0500386 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400387 clip, viewMatrix, clipBounds, origin.x(), origin.y());
joshualitt1d89e8d2015-04-01 12:40:54 -0700388}
389
Herb Derby86240592018-05-24 16:12:31 -0400390inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400391GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
392 GrGlyphCache* glyphCache,
393 const GrShaderCaps& shaderCaps,
394 const GrTextUtils::Paint& paint,
395 SkScalerContextFlags scalerContextFlags,
396 const SkMatrix& viewMatrix,
397 const SkSurfaceProps& props,
398 const char text[], size_t byteLength,
399 const SkScalar pos[], int scalarsPerPosition, const
400 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500401 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400402 if (!glyphCount) {
403 return nullptr;
404 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700405
Herb Derby86240592018-05-24 16:12:31 -0400406 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800407 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500408 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700409
Khushal3e7548c2018-05-23 15:45:01 -0700410 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
411 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500412 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500413 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700414 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500415 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
416 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700417 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700418 return blob;
419}
420
Herb Derby26cbe512018-05-24 14:39:01 -0400421void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
422 const GrClip& clip, const SkPaint& skPaint,
423 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
424 const char text[], size_t byteLength, const SkScalar pos[],
425 int scalarsPerPosition, const SkPoint& offset,
426 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400427 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Khushalc421ca12018-06-26 14:38:34 -0700428 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800429 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500430 }
431
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500432 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500433 auto textBlobCache = context->contextPriv().getTextBlobCache();
434
Herb Derby86240592018-05-24 16:12:31 -0400435 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400436 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500437 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
438 byteLength, pos, scalarsPerPosition, offset));
439 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500440 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500441 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800442 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700443}
444
Herb Derby86240592018-05-24 16:12:31 -0400445void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400446 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
447 const GrTextUtils::Paint& paint,
448 SkScalerContextFlags scalerContextFlags,
449 const SkMatrix& viewMatrix,
450 const char text[], size_t byteLength, const SkScalar pos[],
451 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500452 SkASSERT(byteLength == 0 || text != nullptr);
453 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
454
455 // nothing to draw
456 if (text == nullptr || byteLength == 0) {
457 return;
458 }
459
460 // Ensure the blob is set for bitmaptext
461 blob->setHasBitmap();
462
Jim Van Verth54d9c882018-02-08 16:14:48 -0500463 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500464 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500465 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500466 return;
467 }
468
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000469 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500470 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500471 SkFindAndPlaceGlyph::ProcessPosText(
472 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400473 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500474 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
475 position += rounding;
Herb Derby049b5d92018-08-01 13:56:26 -0400476 AppendGlyph(blob, runIndex, glyphCache, &currStrike,
477 glyph, GrGlyph::kCoverage_MaskStyle,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500478 SkScalarFloorToScalar(position.fX),
479 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400480 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500481 });
Brian Salomon52db9402017-11-07 14:58:55 -0500482}
483
Herb Derby86240592018-05-24 16:12:31 -0400484void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400485 GrGlyphCache* glyphCache,
486 const SkSurfaceProps& props,
487 const GrTextUtils::Paint& origPaint,
488 SkScalerContextFlags scalerContextFlags,
489 const SkMatrix& viewMatrix,
490 const char text[], size_t byteLength,
491 const SkScalar pos[], int scalarsPerPosition,
492 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500493 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
494
495 // nothing to draw
496 if (text == nullptr || byteLength == 0) {
497 return;
498 }
499
500 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500501 SkPaint pathPaint(origPaint);
502 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700503 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
504 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500505
506 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500507 pathPaint.setStyle(SkPaint::kFill_Style);
508 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500509
Jim Van Verthc401bb92018-02-15 14:05:24 -0500510 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500511 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400512 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400513 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500514
515 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500516 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500517 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
518
519 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400520 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500521 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500522 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400523 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500524 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
525 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
526 } else {
527 const SkPath* path = cache->findPath(glyph);
528 if (path) {
529 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
530 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500531 }
532 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500533 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500534 pos += scalarsPerPosition;
535 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500536
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500537 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500538}
539
Herb Derby26cbe512018-05-24 14:39:01 -0400540void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700541 if (options->fMaxDistanceFieldFontSize < 0.f) {
542 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
543 }
544 if (options->fMinDistanceFieldFontSize < 0.f) {
545 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
546 }
547}
548
Herb Derby26cbe512018-05-24 14:39:01 -0400549bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
550 const SkSurfaceProps& props,
551 bool contextSupportsDistanceFieldText,
552 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500553 if (!viewMatrix.hasPerspective()) {
554 SkScalar maxScale = viewMatrix.getMaxScale();
555 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
556 // Hinted text looks far better at small resolutions
557 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700558 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
559 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500560 return false;
561 }
562
563 bool useDFT = props.isUseDeviceIndependentFonts();
564#if SK_FORCE_DISTANCE_FIELD_TEXT
565 useDFT = true;
566#endif
567
568 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
569 return false;
570 }
571 }
572
Mike Reed8ad91a92018-01-19 19:09:32 -0500573 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700574 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500575 return false;
576 }
577
578 // TODO: add some stroking support
579 if (skPaint.getStyle() != SkPaint::kFill_Style) {
580 return false;
581 }
582
583 return true;
584}
585
Herb Derby86240592018-05-24 16:12:31 -0400586void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400587 SkPaint* skPaint,
588 const SkMatrix& viewMatrix,
589 const Options& options,
590 SkScalar* textRatio,
591 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500592 SkScalar textSize = skPaint->getTextSize();
593 SkScalar scaledTextSize = textSize;
594
595 if (viewMatrix.hasPerspective()) {
596 // for perspective, we simply force to the medium size
597 // TODO: compute a size based on approximate screen area
598 scaledTextSize = kMediumDFFontLimit;
599 } else {
600 SkScalar maxScale = viewMatrix.getMaxScale();
601 // if we have non-unity scale, we need to choose our base text size
602 // based on the SkPaint's text size multiplied by the max scale factor
603 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
604 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
605 scaledTextSize *= maxScale;
606 }
607 }
608
609 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
610 // and ceiling. A scale outside of this range would require regenerating the distance fields
611 SkScalar dfMaskScaleFloor;
612 SkScalar dfMaskScaleCeil;
613 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700614 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500615 dfMaskScaleCeil = kSmallDFFontLimit;
616 *textRatio = textSize / kSmallDFFontSize;
617 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
618 } else if (scaledTextSize <= kMediumDFFontLimit) {
619 dfMaskScaleFloor = kSmallDFFontLimit;
620 dfMaskScaleCeil = kMediumDFFontLimit;
621 *textRatio = textSize / kMediumDFFontSize;
622 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
623 } else {
624 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700625 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500626 *textRatio = textSize / kLargeDFFontSize;
627 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
628 }
629
630 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
631 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
632 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
633 // tolerate before we'd have to move to a large mip size. When we actually test these values
634 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
635 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
636 // level)
637 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700638 if (blob) {
639 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
640 dfMaskScaleCeil / scaledTextSize);
641 }
Brian Salomon52db9402017-11-07 14:58:55 -0500642
643 skPaint->setAntiAlias(true);
644 skPaint->setLCDRenderText(false);
645 skPaint->setAutohinted(false);
646 skPaint->setHinting(SkPaint::kNormal_Hinting);
647 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400648
649 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700650
651 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
652 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
653 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500654}
655
Herb Derby86240592018-05-24 16:12:31 -0400656void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400657 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
658 const GrTextUtils::Paint& paint,
659 SkScalerContextFlags scalerContextFlags,
660 const SkMatrix& viewMatrix, const char text[],
661 size_t byteLength, const SkScalar pos[],
662 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500663 SkASSERT(byteLength == 0 || text != nullptr);
664 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
665
666 // nothing to draw
667 if (text == nullptr || byteLength == 0) {
668 return;
669 }
670
Khushal3e7548c2018-05-23 15:45:01 -0700671 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500672
Brian Salomon52db9402017-11-07 14:58:55 -0500673 // Setup distance field paint and text ratio
674 SkScalar textRatio;
675 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700676 SkScalerContextFlags flags;
677 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500678 blob->setHasDistanceField();
679 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500680 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500681
Khushalfa8ff092018-06-06 17:46:38 -0700682 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
683 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500684
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500685 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500686
Herb Derby526819d2018-03-09 12:51:12 -0500687 {
Khushal3e7548c2018-05-23 15:45:01 -0700688 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500689 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400690 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500691
Herb Derby526819d2018-03-09 12:51:12 -0500692 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500693
Herb Derby526819d2018-03-09 12:51:12 -0500694 while (text < stop) {
695 const char* lastText = text;
696 // the last 2 parameters are ignored
Hal Canary4014ba62018-07-24 11:33:21 -0400697 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Brian Salomon52db9402017-11-07 14:58:55 -0500698
Herb Derby526819d2018-03-09 12:51:12 -0500699 if (glyph.fWidth) {
700 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400701 glyphPos.fX += pos[0];
702 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500703
Jim Van Verthd401da62018-05-03 10:40:30 -0400704 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby049b5d92018-08-01 13:56:26 -0400705 AppendGlyph(blob, runIndex, glyphCache, &currStrike,
706 glyph, GrGlyph::kDistance_MaskStyle,
707 glyphPos.fX, glyphPos.fY, paint.filteredPremulColor(),
708 cache.get(), textRatio, true);
Herb Derby526819d2018-03-09 12:51:12 -0500709 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400710 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500711 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
712 glyphPos);
713 }
Brian Salomon52db9402017-11-07 14:58:55 -0500714 }
Herb Derby526819d2018-03-09 12:51:12 -0500715 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500716 }
Brian Salomon52db9402017-11-07 14:58:55 -0500717 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000718
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500719 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500720}
721
joshualitt79dfb2b2015-05-11 08:58:08 -0700722///////////////////////////////////////////////////////////////////////////////////////////////////
723
Herb Derby26cbe512018-05-24 14:39:01 -0400724void GrTextContext::FallbackTextHelper::appendText(const SkGlyph& glyph, int count,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500725 const char* text, SkPoint glyphPos) {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400726 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
Khushalfa8ff092018-06-06 17:46:38 -0700727 if (SkScalarNearlyZero(maxDim)) return;
728
Jim Van Verthb515ae72018-05-23 16:44:55 -0400729 if (!fUseTransformedFallback) {
730 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
731 fUseTransformedFallback = true;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500732 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
Jim Van Verthc401bb92018-02-15 14:05:24 -0500733 }
734 }
735
736 fFallbackTxt.append(count, text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400737 if (fUseTransformedFallback) {
Jim Van Verth080a9282018-03-02 10:41:43 -0500738 // If there's a glyph in the font that's particularly large, it's possible
739 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
Jim Van Verth8b7284d2018-05-17 12:33:52 -0400740 // that glyph than make the others blurry, so we set a minimum size of half the
Jim Van Verth080a9282018-03-02 10:41:43 -0500741 // maximum text size to avoid this case.
Jim Van Verthb515ae72018-05-23 16:44:55 -0400742 SkScalar glyphTextSize = SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim),
Jim Van Verth080a9282018-03-02 10:41:43 -0500743 0.5f*fMaxTextSize);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400744 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500745 }
746 *fFallbackPos.append() = glyphPos;
747}
748
Herb Derby86240592018-05-24 16:12:31 -0400749void GrTextContext::FallbackTextHelper::drawText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400750 GrGlyphCache* glyphCache,
751 const SkSurfaceProps& props,
752 const GrTextUtils::Paint& paint,
753 SkScalerContextFlags scalerContextFlags) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500754 if (fFallbackTxt.count()) {
755 blob->initOverride(runIndex);
756 blob->setHasBitmap();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400757 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
Herb Derby526819d2018-03-09 12:51:12 -0500758 SkExclusiveStrikePtr cache;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500759 const SkPaint& skPaint = paint.skPaint();
760 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400761 SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500762 SkColor textColor = paint.filteredPremulColor();
Khushalfa8ff092018-06-06 17:46:38 -0700763
Jim Van Verthc401bb92018-02-15 14:05:24 -0500764 SkScalar textRatio = SK_Scalar1;
Khushalfa8ff092018-06-06 17:46:38 -0700765 SkPaint fallbackPaint(skPaint);
766 SkMatrix matrix = fViewMatrix;
767 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
768 cache = blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500769
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500770 sk_sp<GrTextStrike> currStrike;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500771 const char* text = fFallbackTxt.begin();
772 const char* stop = text + fFallbackTxt.count();
773 SkPoint* glyphPos = fFallbackPos.begin();
774 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400775 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400776 if (!fUseTransformedFallback) {
777 fViewMatrix.mapPoints(glyphPos, 1);
Jim Van Verth76e85162018-03-29 13:46:56 -0400778 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
779 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
780 }
Herb Derby049b5d92018-08-01 13:56:26 -0400781 GrTextContext::AppendGlyph(blob, runIndex, glyphCache, &currStrike,
782 glyph, GrGlyph::kCoverage_MaskStyle,
783 glyphPos->fX, glyphPos->fY, textColor,
784 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500785 glyphPos++;
786 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500787 }
788}
789
Khushalfa8ff092018-06-06 17:46:38 -0700790void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
791 SkMatrix* matrix) const {
792 if (!fUseTransformedFallback) return;
793
794 paint->setTextSize(fTransformedFallbackTextSize);
795 *textRatio = fTextSize / fTransformedFallbackTextSize;
796 *matrix = SkMatrix::I();
797}
798
Jim Van Verthc401bb92018-02-15 14:05:24 -0500799///////////////////////////////////////////////////////////////////////////////////////////////////
800
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400801void GrTextContext::FallbackGlyphRunHelper::appendText(
802 const SkGlyph& glyph, SkGlyphID glyphID, SkPoint glyphPos) {
803 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
804 if (SkScalarNearlyZero(maxDim)) return;
805
806 if (!fUseTransformedFallback) {
807 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
808 fUseTransformedFallback = true;
809 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
810 }
811 }
812
813 fFallbackTxt.push_back(glyphID);
814 if (fUseTransformedFallback) {
815 // If there's a glyph in the font that's particularly large, it's possible
816 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
817 // that glyph than make the others blurry, so we set a minimum size of half the
818 // maximum text size to avoid this case.
819 SkScalar glyphTextSize =
820 SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim), 0.5f*fMaxTextSize);
821 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
822 }
823 fFallbackPos.push_back(glyphPos);
824}
825
826void GrTextContext::FallbackGlyphRunHelper::drawText(
827 GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props,
828 const GrTextUtils::Paint& paint, SkScalerContextFlags scalerContextFlags) {
829 if (!fFallbackTxt.empty()) {
830 blob->initOverride(runIndex);
831 blob->setHasBitmap();
832 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
833 const SkPaint& skPaint = paint.skPaint();
834 SkColor textColor = paint.filteredPremulColor();
835
836 SkScalar textRatio = SK_Scalar1;
837 SkPaint fallbackPaint(skPaint);
838 SkMatrix matrix = fViewMatrix;
839 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
840 SkExclusiveStrikePtr cache =
841 blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
842
843 sk_sp<GrTextStrike> currStrike;
844 auto glyphPos = fFallbackPos.begin();
845 for (auto glyphID : fFallbackTxt) {
846 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
847 if (!fUseTransformedFallback) {
848 fViewMatrix.mapPoints(&*glyphPos, 1);
849 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
850 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
851 }
Herb Derby049b5d92018-08-01 13:56:26 -0400852 GrTextContext::AppendGlyph(blob, runIndex, glyphCache, &currStrike,
853 glyph, GrGlyph::kCoverage_MaskStyle,
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400854 glyphPos->fX, glyphPos->fY, textColor,
855 cache.get(), textRatio, fUseTransformedFallback);
856 glyphPos++;
857 }
858 }
859}
860
861void GrTextContext::FallbackGlyphRunHelper::initializeForDraw(
862 SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const {
863 if (!fUseTransformedFallback) return;
864
865 paint->setTextSize(fTransformedFallbackTextSize);
866 *textRatio = fTextSize / fTransformedFallbackTextSize;
867 *matrix = SkMatrix::I();
868}
869
870///////////////////////////////////////////////////////////////////////////////////////////////////
871
872
Hal Canary6f6961e2017-01-31 13:50:44 -0500873#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700874
Brian Salomonf18b1d82017-10-27 11:30:49 -0400875#include "GrRenderTargetContext.h"
876
Herb Derby26cbe512018-05-24 14:39:01 -0400877std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
878 GrTextContext* textContext,
879 GrRenderTargetContext* rtc,
880 const SkPaint& skPaint,
881 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400882 const char* text,
883 int x,
884 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500885 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500886
887 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
888
889 size_t textLen = (int)strlen(text);
890
891 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
892
893 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
894 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000895
896 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400897 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -0400898 builder.drawText(skPaint, text, textLen, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400899 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000900
Herb Derby8a6348e2018-07-12 15:30:35 -0400901 auto glyphRunList = builder.useGlyphRunList();
Herb Derbyb935cf82018-07-26 16:54:18 -0400902 if (!glyphRunList.empty()) {
903 auto glyphRun = glyphRunList[0];
Herb Derby8a6348e2018-07-12 15:30:35 -0400904 // Use the text and textLen below, because we don't want to mess with the paint.
905 glyphRun.temporaryShuntToCallback(
Herb Derby59d997a2018-06-07 12:44:09 -0400906 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
907 blob = textContext->makeDrawPosTextBlob(
908 context->contextPriv().getTextBlobCache(), glyphCache,
909 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
Herb Derby8a6348e2018-07-12 15:30:35 -0400910 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps,
Herb Derbycddab252018-07-16 11:19:04 -0400911 text, textLen, pos, 2, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400912 });
Herb Derby8a6348e2018-07-12 15:30:35 -0400913 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500914
915 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500916 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500917}
918
Brian Salomon44acb5b2017-07-18 19:59:24 -0400919GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700920 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400921 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700922 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700923
924 if (context->uniqueID() != gContextID) {
925 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400926 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700927 }
928
Brian Osman11052242016-10-27 14:47:55 -0400929 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500930 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400931 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700932
joshualitt6c891102015-05-13 08:51:49 -0700933 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400934
935 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
936 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700937 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500938 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700939 skPaint.setLCDRenderText(random->nextBool());
940 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
941 skPaint.setSubpixelText(random->nextBool());
942
joshualitt79dfb2b2015-05-11 08:58:08 -0700943 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700944
joshualittb8d86492016-02-24 09:23:03 -0800945 // create some random x/y offsets, including negative offsets
946 static const int kMaxTrans = 1024;
947 int xPos = (random->nextU() % 2) * 2 - 1;
948 int yPos = (random->nextU() % 2) * 2 - 1;
949 int xInt = (random->nextU() % kMaxTrans) * xPos;
950 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700951
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500952 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
953 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700954}
955
956#endif