blob: 966f3c2f7e4d6c81e37772e6aab603369eb0fc2f [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 Derby12e42562018-07-28 14:27:48 -040087
Herb Derby12e42562018-07-28 14:27:48 -040088void GrTextContext::regenerateGlyphRunList(GrTextBlob* cacheBlob,
89 GrGlyphCache* glyphCache,
90 const GrShaderCaps& shaderCaps,
91 const GrTextUtils::Paint& paint,
92 SkScalerContextFlags scalerContextFlags,
93 const SkMatrix& viewMatrix,
94 const SkSurfaceProps& props,
Herb Derby74c6ed32018-07-28 18:07:54 -040095 const SkGlyphRunList& glyphRunList,
96 SkGlyphRunListDrawer* glyphDrawer) {
Herb Derby12e42562018-07-28 14:27:48 -040097 SkPoint origin = glyphRunList.origin();
98 cacheBlob->initReusableBlob(paint.luminanceColor(), viewMatrix, origin.x(), origin.y());
99
100 // Regenerate textblob
101 GrTextUtils::RunPaint runPaint(&paint);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400102 int runIndex = 0;
Herb Derby12e42562018-07-28 14:27:48 -0400103 for (const auto& glyphRun : glyphRunList) {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400104 cacheBlob->push_back_run(runIndex);
Herb Derby12e42562018-07-28 14:27:48 -0400105
106 if (!runPaint.modifyForRun([glyphRun](SkPaint* p) { *p = glyphRun.paint(); })) {
107 continue;
108 }
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400109 cacheBlob->setRunPaintFlags(runIndex, runPaint.skPaint().getFlags());
Herb Derby12e42562018-07-28 14:27:48 -0400110
111 if (CanDrawAsDistanceFields(runPaint, viewMatrix, props,
112 shaderCaps.supportsDistanceFieldText(), fOptions)) {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400113 bool hasWCoord = viewMatrix.hasPerspective()
114 || fOptions.fDistanceFieldVerticesAlwaysHaveW;
115
116 // Setup distance field runPaint and text ratio
117 SkScalar textRatio;
118 SkPaint dfPaint(runPaint);
119 SkScalerContextFlags flags;
120 InitDistanceFieldPaint(cacheBlob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
121 cacheBlob->setHasDistanceField();
122 cacheBlob->setSubRunHasDistanceFields(runIndex, runPaint.skPaint().isLCDRenderText(),
123 runPaint.skPaint().isAntiAlias(), hasWCoord);
124
125 FallbackGlyphRunHelper fallbackTextHelper(
126 viewMatrix, runPaint, glyphCache->getGlyphSizeLimit(), textRatio);
127
128 sk_sp<GrTextStrike> currStrike;
129
130 {
131 auto cache = cacheBlob->setupCache(runIndex, props, flags, dfPaint, nullptr);
132
133 const SkPoint* positionCursor = glyphRun.positions().data();
134 for (auto glyphID : glyphRun.shuntGlyphsIDs()) {
135 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
136 SkPoint glyphPos = origin + *positionCursor++;
137 if (glyph.fWidth > 0) {
138 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
139 DfAppendGlyph(
140 cacheBlob, runIndex, glyphCache, &currStrike, glyph,
141 glyphPos.fX, glyphPos.fY, runPaint.filteredPremulColor(),
142 cache.get(), textRatio);
143 } else {
144 // can't append non-SDF glyph to SDF batch, send to fallback
145 fallbackTextHelper.appendText(glyph, glyphID, glyphPos);
146 }
147 }
148 }
149 }
150
151 fallbackTextHelper.drawText(
152 cacheBlob, runIndex, glyphCache, props, runPaint, scalerContextFlags);
153
154 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
155 // Ensure the blob is set for bitmaptext
156 cacheBlob->setHasBitmap();
157
158 // setup our std runPaint, in hopes of getting hits in the cache
159 SkPaint pathPaint(runPaint);
160 SkScalar matrixScale = pathPaint.setupForAsPaths();
161
162 FallbackGlyphRunHelper fallbackTextHelper(
163 viewMatrix, runPaint, glyphCache->getGlyphSizeLimit(), matrixScale);
164
165 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
166 pathPaint.setStyle(SkPaint::kFill_Style);
167 pathPaint.setPathEffect(nullptr);
168
169 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
170 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
171
Herb Derby0ab5ce12018-07-30 10:10:40 -0400172 auto drawOnePath =
173 [&fallbackTextHelper, matrixScale, runIndex, cacheBlob]
174 (const SkPath* path, const SkGlyph& glyph, SkPoint position) {
175 if (glyph.fMaskFormat == SkMask::kARGB32_Format) {
176 fallbackTextHelper.appendText(glyph, glyph.getGlyphID(), position);
177 } else {
178 if (path != nullptr) {
179 cacheBlob->appendPathGlyph(
180 runIndex, *path, position.fX, position.fY, matrixScale, false);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400181 }
182 }
Herb Derby0ab5ce12018-07-30 10:10:40 -0400183 };
184
185 glyphDrawer->drawUsingPaths(glyphRun, origin, cache.get(), drawOnePath);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400186
187 fallbackTextHelper.drawText(
188 cacheBlob, runIndex, glyphCache, props, runPaint, scalerContextFlags);
189
Herb Derby12e42562018-07-28 14:27:48 -0400190 } else {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400191 // Ensure the blob is set for bitmaptext
192 cacheBlob->setHasBitmap();
193 sk_sp<GrTextStrike> currStrike;
194 auto cache = cacheBlob->setupCache(
195 runIndex, props, scalerContextFlags, runPaint, &viewMatrix);
Herb Derby74c6ed32018-07-28 18:07:54 -0400196
197 auto drawOneGlyph =
198 [cacheBlob, runIndex, glyphCache, &currStrike, runPaint, cache{cache.get()}]
199 (const SkMask& mask, const SkGlyph& glyph, SkPoint position) {
200 BmpAppendGlyph(cacheBlob, runIndex, glyphCache, &currStrike, glyph,
201 SkScalarFloorToScalar(position.fX),
202 SkScalarFloorToScalar(position.fY),
203 runPaint.filteredPremulColor(), cache, SK_Scalar1, false);
204 };
205
206 glyphDrawer->drawUsingMasks(cache.get(), glyphRun, origin, viewMatrix, drawOneGlyph);
Herb Derby12e42562018-07-28 14:27:48 -0400207 }
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400208 runIndex += 1;
Herb Derby12e42562018-07-28 14:27:48 -0400209 }
210}
211
Herb Derbycddab252018-07-16 11:19:04 -0400212void GrTextContext::drawGlyphRunList(
213 GrContext* context, GrTextUtils::Target* target, const GrClip& clip,
Herb Derbyb935cf82018-07-26 16:54:18 -0400214 const SkMatrix& viewMatrix, const SkSurfaceProps& props, const SkGlyphRunList& glyphRunList,
Herb Derbycddab252018-07-16 11:19:04 -0400215 const SkIRect& clipBounds) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400216 SkPoint origin = glyphRunList.origin();
joshualitt9e36c1a2015-04-14 12:17:27 -0700217
Herb Derbycddab252018-07-16 11:19:04 -0400218 // Get the first paint to use as the key paint.
Herb Derbyb935cf82018-07-26 16:54:18 -0400219 const SkPaint& skPaint = glyphRunList.paint();
Herb Derbycddab252018-07-16 11:19:04 -0400220
joshualitt9b8e79e2015-04-24 09:57:12 -0700221 // If we have been abandoned, then don't draw
Khushalc421ca12018-06-26 14:38:34 -0700222 if (context->abandoned()) {
robertphillipsea461502015-05-26 11:38:03 -0700223 return;
224 }
225
Mike Reed80747ef2018-01-23 15:29:32 -0500226 SkMaskFilterBase::BlurRec blurRec;
joshualitt53b5f442015-04-13 06:33:59 -0700227 // It might be worth caching these things, but its not clear at this time
228 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
229 const SkMaskFilter* mf = skPaint.getMaskFilter();
Herb Derbyb935cf82018-07-26 16:54:18 -0400230 bool canCache = glyphRunList.canCache() && !(skPaint.getPathEffect() ||
Herb Derbycddab252018-07-16 11:19:04 -0400231 (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derbyd8327a82018-01-22 14:39:27 -0500232 SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
joshualitt2a0e9f32015-04-13 06:12:21 -0700233
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500234 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500235 GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache();
236
Herb Derbycddab252018-07-16 11:19:04 -0400237 sk_sp<GrTextBlob> cacheBlob;
238 GrTextBlob::Key key;
joshualitt2a0e9f32015-04-13 06:12:21 -0700239 if (canCache) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400240 bool hasLCD = glyphRunList.anyRunsLCD();
joshualitte4cee1f2015-05-11 13:04:28 -0700241
242 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
joshualitt2c89bc12016-02-11 05:42:30 -0800243 SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() :
Herb Derbycddab252018-07-16 11:19:04 -0400244 kUnknown_SkPixelGeometry;
joshualitte4cee1f2015-05-11 13:04:28 -0700245
joshualitt9e36c1a2015-04-14 12:17:27 -0700246 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
247 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
248 // ensure we always match the same key
249 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
Herb Derbycddab252018-07-16 11:19:04 -0400250 ComputeCanonicalColor(skPaint, hasLCD);
joshualitt9e36c1a2015-04-14 12:17:27 -0700251
joshualitte4cee1f2015-05-11 13:04:28 -0700252 key.fPixelGeometry = pixelGeometry;
Herb Derbyb935cf82018-07-26 16:54:18 -0400253 key.fUniqueID = glyphRunList.uniqueID();
joshualitt53b5f442015-04-13 06:33:59 -0700254 key.fStyle = skPaint.getStyle();
255 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700256 key.fCanonicalColor = canonicalColor;
brianosman8d7ffce2016-04-21 08:29:06 -0700257 key.fScalerContextFlags = scalerContextFlags;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500258 cacheBlob = textBlobCache->find(key);
joshualitt2a0e9f32015-04-13 06:12:21 -0700259 }
260
Brian Salomonf18b1d82017-10-27 11:30:49 -0400261 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
joshualittb7133be2015-04-08 09:08:31 -0700262 if (cacheBlob) {
Herb Derbycddab252018-07-16 11:19:04 -0400263 if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, origin.x(), origin.y())) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700264 // We have to remake the blob because changes may invalidate our masks.
265 // TODO we could probably get away reuse most of the time if the pointer is unique,
266 // but we'd have to clear the subrun information
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500267 textBlobCache->remove(cacheBlob.get());
Herb Derbycddab252018-07-16 11:19:04 -0400268 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
269 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400270 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derby74c6ed32018-07-28 18:07:54 -0400271 scalerContextFlags, viewMatrix, props, glyphRunList,
272 target->glyphDrawer());
joshualittb7133be2015-04-08 09:08:31 -0700273 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500274 textBlobCache->makeMRU(cacheBlob.get());
joshualitt2f2ee832016-02-10 08:52:24 -0800275
276 if (CACHE_SANITY_CHECK) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400277 int glyphCount = glyphRunList.totalGlyphCount();
278 int runCount = glyphRunList.runCount();
Herb Derby86240592018-05-24 16:12:31 -0400279 sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
joshualitt92303772016-02-10 11:55:52 -0800280 sanityBlob->setupKey(key, blurRec, skPaint);
Herb Derbycddab252018-07-16 11:19:04 -0400281 this->regenerateGlyphRunList(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400282 sanityBlob.get(), glyphCache, *context->contextPriv().caps()->shaderCaps(),
Herb Derby74c6ed32018-07-28 18:07:54 -0400283 paint, scalerContextFlags, viewMatrix, props, glyphRunList,
284 target->glyphDrawer());
Herb Derby86240592018-05-24 16:12:31 -0400285 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700286 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700287 }
288 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700289 if (canCache) {
Herb Derbycddab252018-07-16 11:19:04 -0400290 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
joshualitt2a0e9f32015-04-13 06:12:21 -0700291 } else {
Herb Derbycddab252018-07-16 11:19:04 -0400292 cacheBlob = textBlobCache->makeBlob(glyphRunList);
joshualitt2a0e9f32015-04-13 06:12:21 -0700293 }
Herb Derbycddab252018-07-16 11:19:04 -0400294 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400295 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derby74c6ed32018-07-28 18:07:54 -0400296 scalerContextFlags, viewMatrix, props, glyphRunList,
297 target->glyphDrawer());
joshualitt1d89e8d2015-04-01 12:40:54 -0700298 }
299
Robert Phillips5a66efb2018-03-07 15:13:18 -0500300 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400301 clip, viewMatrix, clipBounds, origin.x(), origin.y());
joshualitt1d89e8d2015-04-01 12:40:54 -0700302}
303
Herb Derby86240592018-05-24 16:12:31 -0400304inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400305GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
306 GrGlyphCache* glyphCache,
307 const GrShaderCaps& shaderCaps,
308 const GrTextUtils::Paint& paint,
309 SkScalerContextFlags scalerContextFlags,
310 const SkMatrix& viewMatrix,
311 const SkSurfaceProps& props,
312 const char text[], size_t byteLength,
313 const SkScalar pos[], int scalarsPerPosition, const
314 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500315 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400316 if (!glyphCount) {
317 return nullptr;
318 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700319
Herb Derby86240592018-05-24 16:12:31 -0400320 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800321 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500322 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700323
Khushal3e7548c2018-05-23 15:45:01 -0700324 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
325 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500326 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500327 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700328 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500329 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
330 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700331 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700332 return blob;
333}
334
Herb Derby26cbe512018-05-24 14:39:01 -0400335void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
336 const GrClip& clip, const SkPaint& skPaint,
337 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
338 const char text[], size_t byteLength, const SkScalar pos[],
339 int scalarsPerPosition, const SkPoint& offset,
340 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400341 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Khushalc421ca12018-06-26 14:38:34 -0700342 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800343 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500344 }
345
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500346 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500347 auto textBlobCache = context->contextPriv().getTextBlobCache();
348
Herb Derby86240592018-05-24 16:12:31 -0400349 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400350 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500351 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
352 byteLength, pos, scalarsPerPosition, offset));
353 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500354 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500355 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800356 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700357}
358
Herb Derby86240592018-05-24 16:12:31 -0400359void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400360 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
361 const GrTextUtils::Paint& paint,
362 SkScalerContextFlags scalerContextFlags,
363 const SkMatrix& viewMatrix,
364 const char text[], size_t byteLength, const SkScalar pos[],
365 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500366 SkASSERT(byteLength == 0 || text != nullptr);
367 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
368
369 // nothing to draw
370 if (text == nullptr || byteLength == 0) {
371 return;
372 }
373
374 // Ensure the blob is set for bitmaptext
375 blob->setHasBitmap();
376
Jim Van Verth54d9c882018-02-08 16:14:48 -0500377 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500378 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500379 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500380 return;
381 }
382
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000383 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500384 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500385 SkFindAndPlaceGlyph::ProcessPosText(
386 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400387 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500388 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
389 position += rounding;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500390 BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500391 SkScalarFloorToScalar(position.fX),
392 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400393 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500394 });
Brian Salomon52db9402017-11-07 14:58:55 -0500395}
396
Herb Derby86240592018-05-24 16:12:31 -0400397void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400398 GrGlyphCache* glyphCache,
399 const SkSurfaceProps& props,
400 const GrTextUtils::Paint& origPaint,
401 SkScalerContextFlags scalerContextFlags,
402 const SkMatrix& viewMatrix,
403 const char text[], size_t byteLength,
404 const SkScalar pos[], int scalarsPerPosition,
405 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500406 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
407
408 // nothing to draw
409 if (text == nullptr || byteLength == 0) {
410 return;
411 }
412
413 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500414 SkPaint pathPaint(origPaint);
415 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700416 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
417 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500418
419 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500420 pathPaint.setStyle(SkPaint::kFill_Style);
421 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500422
Jim Van Verthc401bb92018-02-15 14:05:24 -0500423 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500424 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400425 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400426 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500427
428 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500429 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500430 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
431
432 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400433 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500434 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500435 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400436 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500437 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
438 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
439 } else {
440 const SkPath* path = cache->findPath(glyph);
441 if (path) {
442 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
443 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500444 }
445 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500446 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500447 pos += scalarsPerPosition;
448 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500449
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500450 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500451}
452
Herb Derby86240592018-05-24 16:12:31 -0400453void GrTextContext::BmpAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400454 GrGlyphCache* grGlyphCache,
455 sk_sp<GrTextStrike>* strike,
456 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
457 GrColor color, SkGlyphCache* skGlyphCache,
458 SkScalar textRatio, bool needsTransform) {
Brian Salomon52db9402017-11-07 14:58:55 -0500459 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500460 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500461 }
462
463 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
464 skGlyph.getSubXFixed(),
465 skGlyph.getSubYFixed(),
466 GrGlyph::kCoverage_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500467 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500468 if (!glyph) {
469 return;
470 }
471
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500472 SkASSERT(skGlyph.fWidth == glyph->width());
473 SkASSERT(skGlyph.fHeight == glyph->height());
474
Jim Van Verthf4c13162018-01-11 16:40:24 -0500475 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft);
476 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop);
477 SkScalar width = SkIntToScalar(glyph->fBounds.width());
478 SkScalar height = SkIntToScalar(glyph->fBounds.height());
Brian Salomon52db9402017-11-07 14:58:55 -0500479
Jim Van Verthf4c13162018-01-11 16:40:24 -0500480 dx *= textRatio;
481 dy *= textRatio;
482 width *= textRatio;
483 height *= textRatio;
Brian Salomon52db9402017-11-07 14:58:55 -0500484
Jim Van Verthf4c13162018-01-11 16:40:24 -0500485 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500486
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500487 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400488 textRatio, !needsTransform);
Brian Salomon52db9402017-11-07 14:58:55 -0500489}
490
Herb Derby26cbe512018-05-24 14:39:01 -0400491void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700492 if (options->fMaxDistanceFieldFontSize < 0.f) {
493 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
494 }
495 if (options->fMinDistanceFieldFontSize < 0.f) {
496 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
497 }
498}
499
Herb Derby26cbe512018-05-24 14:39:01 -0400500bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
501 const SkSurfaceProps& props,
502 bool contextSupportsDistanceFieldText,
503 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500504 if (!viewMatrix.hasPerspective()) {
505 SkScalar maxScale = viewMatrix.getMaxScale();
506 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
507 // Hinted text looks far better at small resolutions
508 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700509 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
510 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500511 return false;
512 }
513
514 bool useDFT = props.isUseDeviceIndependentFonts();
515#if SK_FORCE_DISTANCE_FIELD_TEXT
516 useDFT = true;
517#endif
518
519 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
520 return false;
521 }
522 }
523
Mike Reed8ad91a92018-01-19 19:09:32 -0500524 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700525 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500526 return false;
527 }
528
529 // TODO: add some stroking support
530 if (skPaint.getStyle() != SkPaint::kFill_Style) {
531 return false;
532 }
533
534 return true;
535}
536
Herb Derby86240592018-05-24 16:12:31 -0400537void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400538 SkPaint* skPaint,
539 const SkMatrix& viewMatrix,
540 const Options& options,
541 SkScalar* textRatio,
542 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500543 SkScalar textSize = skPaint->getTextSize();
544 SkScalar scaledTextSize = textSize;
545
546 if (viewMatrix.hasPerspective()) {
547 // for perspective, we simply force to the medium size
548 // TODO: compute a size based on approximate screen area
549 scaledTextSize = kMediumDFFontLimit;
550 } else {
551 SkScalar maxScale = viewMatrix.getMaxScale();
552 // if we have non-unity scale, we need to choose our base text size
553 // based on the SkPaint's text size multiplied by the max scale factor
554 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
555 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
556 scaledTextSize *= maxScale;
557 }
558 }
559
560 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
561 // and ceiling. A scale outside of this range would require regenerating the distance fields
562 SkScalar dfMaskScaleFloor;
563 SkScalar dfMaskScaleCeil;
564 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700565 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500566 dfMaskScaleCeil = kSmallDFFontLimit;
567 *textRatio = textSize / kSmallDFFontSize;
568 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
569 } else if (scaledTextSize <= kMediumDFFontLimit) {
570 dfMaskScaleFloor = kSmallDFFontLimit;
571 dfMaskScaleCeil = kMediumDFFontLimit;
572 *textRatio = textSize / kMediumDFFontSize;
573 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
574 } else {
575 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700576 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500577 *textRatio = textSize / kLargeDFFontSize;
578 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
579 }
580
581 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
582 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
583 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
584 // tolerate before we'd have to move to a large mip size. When we actually test these values
585 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
586 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
587 // level)
588 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700589 if (blob) {
590 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
591 dfMaskScaleCeil / scaledTextSize);
592 }
Brian Salomon52db9402017-11-07 14:58:55 -0500593
594 skPaint->setAntiAlias(true);
595 skPaint->setLCDRenderText(false);
596 skPaint->setAutohinted(false);
597 skPaint->setHinting(SkPaint::kNormal_Hinting);
598 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400599
600 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700601
602 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
603 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
604 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500605}
606
Herb Derby86240592018-05-24 16:12:31 -0400607void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400608 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
609 const GrTextUtils::Paint& paint,
610 SkScalerContextFlags scalerContextFlags,
611 const SkMatrix& viewMatrix, const char text[],
612 size_t byteLength, const SkScalar pos[],
613 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500614 SkASSERT(byteLength == 0 || text != nullptr);
615 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
616
617 // nothing to draw
618 if (text == nullptr || byteLength == 0) {
619 return;
620 }
621
Khushal3e7548c2018-05-23 15:45:01 -0700622 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500623
Brian Salomon52db9402017-11-07 14:58:55 -0500624 // Setup distance field paint and text ratio
625 SkScalar textRatio;
626 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700627 SkScalerContextFlags flags;
628 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500629 blob->setHasDistanceField();
630 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500631 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500632
Khushalfa8ff092018-06-06 17:46:38 -0700633 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
634 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500635
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500636 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500637
Herb Derby526819d2018-03-09 12:51:12 -0500638 {
Khushal3e7548c2018-05-23 15:45:01 -0700639 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500640 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400641 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500642
Herb Derby526819d2018-03-09 12:51:12 -0500643 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500644
Herb Derby526819d2018-03-09 12:51:12 -0500645 while (text < stop) {
646 const char* lastText = text;
647 // the last 2 parameters are ignored
Hal Canary4014ba62018-07-24 11:33:21 -0400648 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Brian Salomon52db9402017-11-07 14:58:55 -0500649
Herb Derby526819d2018-03-09 12:51:12 -0500650 if (glyph.fWidth) {
651 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400652 glyphPos.fX += pos[0];
653 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500654
Jim Van Verthd401da62018-05-03 10:40:30 -0400655 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby526819d2018-03-09 12:51:12 -0500656 DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
657 glyphPos.fY, paint.filteredPremulColor(), cache.get(), textRatio);
658 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400659 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500660 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
661 glyphPos);
662 }
Brian Salomon52db9402017-11-07 14:58:55 -0500663 }
Herb Derby526819d2018-03-09 12:51:12 -0500664 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500665 }
Brian Salomon52db9402017-11-07 14:58:55 -0500666 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000667
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500668 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500669}
670
Jim Van Verthf4c13162018-01-11 16:40:24 -0500671// TODO: merge with BmpAppendGlyph
Herb Derby86240592018-05-24 16:12:31 -0400672void GrTextContext::DfAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400673 GrGlyphCache* grGlyphCache, sk_sp<GrTextStrike>* strike,
674 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
675 GrColor color, SkGlyphCache* skGlyphCache,
676 SkScalar textRatio) {
Brian Salomon52db9402017-11-07 14:58:55 -0500677 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500678 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500679 }
680
681 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
682 skGlyph.getSubXFixed(),
683 skGlyph.getSubYFixed(),
684 GrGlyph::kDistance_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500685 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500686 if (!glyph) {
Jim Van Verthf4c13162018-01-11 16:40:24 -0500687 return;
Brian Salomon52db9402017-11-07 14:58:55 -0500688 }
689
690 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
691 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
692 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
693 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
694
Jim Van Verthf4c13162018-01-11 16:40:24 -0500695 dx *= textRatio;
696 dy *= textRatio;
697 width *= textRatio;
698 height *= textRatio;
699 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500700
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500701 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500702 textRatio, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500703}
704
joshualitt79dfb2b2015-05-11 08:58:08 -0700705///////////////////////////////////////////////////////////////////////////////////////////////////
706
Herb Derby26cbe512018-05-24 14:39:01 -0400707void GrTextContext::FallbackTextHelper::appendText(const SkGlyph& glyph, int count,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500708 const char* text, SkPoint glyphPos) {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400709 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
Khushalfa8ff092018-06-06 17:46:38 -0700710 if (SkScalarNearlyZero(maxDim)) return;
711
Jim Van Verthb515ae72018-05-23 16:44:55 -0400712 if (!fUseTransformedFallback) {
713 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
714 fUseTransformedFallback = true;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500715 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
Jim Van Verthc401bb92018-02-15 14:05:24 -0500716 }
717 }
718
719 fFallbackTxt.append(count, text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400720 if (fUseTransformedFallback) {
Jim Van Verth080a9282018-03-02 10:41:43 -0500721 // If there's a glyph in the font that's particularly large, it's possible
722 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
Jim Van Verth8b7284d2018-05-17 12:33:52 -0400723 // that glyph than make the others blurry, so we set a minimum size of half the
Jim Van Verth080a9282018-03-02 10:41:43 -0500724 // maximum text size to avoid this case.
Jim Van Verthb515ae72018-05-23 16:44:55 -0400725 SkScalar glyphTextSize = SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim),
Jim Van Verth080a9282018-03-02 10:41:43 -0500726 0.5f*fMaxTextSize);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400727 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500728 }
729 *fFallbackPos.append() = glyphPos;
730}
731
Herb Derby86240592018-05-24 16:12:31 -0400732void GrTextContext::FallbackTextHelper::drawText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400733 GrGlyphCache* glyphCache,
734 const SkSurfaceProps& props,
735 const GrTextUtils::Paint& paint,
736 SkScalerContextFlags scalerContextFlags) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500737 if (fFallbackTxt.count()) {
738 blob->initOverride(runIndex);
739 blob->setHasBitmap();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400740 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
Herb Derby526819d2018-03-09 12:51:12 -0500741 SkExclusiveStrikePtr cache;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500742 const SkPaint& skPaint = paint.skPaint();
743 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400744 SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500745 SkColor textColor = paint.filteredPremulColor();
Khushalfa8ff092018-06-06 17:46:38 -0700746
Jim Van Verthc401bb92018-02-15 14:05:24 -0500747 SkScalar textRatio = SK_Scalar1;
Khushalfa8ff092018-06-06 17:46:38 -0700748 SkPaint fallbackPaint(skPaint);
749 SkMatrix matrix = fViewMatrix;
750 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
751 cache = blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500752
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500753 sk_sp<GrTextStrike> currStrike;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500754 const char* text = fFallbackTxt.begin();
755 const char* stop = text + fFallbackTxt.count();
756 SkPoint* glyphPos = fFallbackPos.begin();
757 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400758 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400759 if (!fUseTransformedFallback) {
760 fViewMatrix.mapPoints(glyphPos, 1);
Jim Van Verth76e85162018-03-29 13:46:56 -0400761 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
762 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
763 }
Herb Derby26cbe512018-05-24 14:39:01 -0400764 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500765 glyphPos->fX, glyphPos->fY, textColor,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400766 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500767 glyphPos++;
768 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500769 }
770}
771
Khushalfa8ff092018-06-06 17:46:38 -0700772void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
773 SkMatrix* matrix) const {
774 if (!fUseTransformedFallback) return;
775
776 paint->setTextSize(fTransformedFallbackTextSize);
777 *textRatio = fTextSize / fTransformedFallbackTextSize;
778 *matrix = SkMatrix::I();
779}
780
Jim Van Verthc401bb92018-02-15 14:05:24 -0500781///////////////////////////////////////////////////////////////////////////////////////////////////
782
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400783void GrTextContext::FallbackGlyphRunHelper::appendText(
784 const SkGlyph& glyph, SkGlyphID glyphID, SkPoint glyphPos) {
785 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
786 if (SkScalarNearlyZero(maxDim)) return;
787
788 if (!fUseTransformedFallback) {
789 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
790 fUseTransformedFallback = true;
791 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
792 }
793 }
794
795 fFallbackTxt.push_back(glyphID);
796 if (fUseTransformedFallback) {
797 // If there's a glyph in the font that's particularly large, it's possible
798 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
799 // that glyph than make the others blurry, so we set a minimum size of half the
800 // maximum text size to avoid this case.
801 SkScalar glyphTextSize =
802 SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim), 0.5f*fMaxTextSize);
803 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
804 }
805 fFallbackPos.push_back(glyphPos);
806}
807
808void GrTextContext::FallbackGlyphRunHelper::drawText(
809 GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props,
810 const GrTextUtils::Paint& paint, SkScalerContextFlags scalerContextFlags) {
811 if (!fFallbackTxt.empty()) {
812 blob->initOverride(runIndex);
813 blob->setHasBitmap();
814 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
815 const SkPaint& skPaint = paint.skPaint();
816 SkColor textColor = paint.filteredPremulColor();
817
818 SkScalar textRatio = SK_Scalar1;
819 SkPaint fallbackPaint(skPaint);
820 SkMatrix matrix = fViewMatrix;
821 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
822 SkExclusiveStrikePtr cache =
823 blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
824
825 sk_sp<GrTextStrike> currStrike;
826 auto glyphPos = fFallbackPos.begin();
827 for (auto glyphID : fFallbackTxt) {
828 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
829 if (!fUseTransformedFallback) {
830 fViewMatrix.mapPoints(&*glyphPos, 1);
831 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
832 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
833 }
834 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
835 glyphPos->fX, glyphPos->fY, textColor,
836 cache.get(), textRatio, fUseTransformedFallback);
837 glyphPos++;
838 }
839 }
840}
841
842void GrTextContext::FallbackGlyphRunHelper::initializeForDraw(
843 SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const {
844 if (!fUseTransformedFallback) return;
845
846 paint->setTextSize(fTransformedFallbackTextSize);
847 *textRatio = fTextSize / fTransformedFallbackTextSize;
848 *matrix = SkMatrix::I();
849}
850
851///////////////////////////////////////////////////////////////////////////////////////////////////
852
853
Hal Canary6f6961e2017-01-31 13:50:44 -0500854#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700855
Brian Salomonf18b1d82017-10-27 11:30:49 -0400856#include "GrRenderTargetContext.h"
857
Herb Derby26cbe512018-05-24 14:39:01 -0400858std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
859 GrTextContext* textContext,
860 GrRenderTargetContext* rtc,
861 const SkPaint& skPaint,
862 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400863 const char* text,
864 int x,
865 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500866 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500867
868 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
869
870 size_t textLen = (int)strlen(text);
871
872 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
873
874 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
875 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000876
877 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400878 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -0400879 builder.drawText(skPaint, text, textLen, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400880 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000881
Herb Derby8a6348e2018-07-12 15:30:35 -0400882 auto glyphRunList = builder.useGlyphRunList();
Herb Derbyb935cf82018-07-26 16:54:18 -0400883 if (!glyphRunList.empty()) {
884 auto glyphRun = glyphRunList[0];
Herb Derby8a6348e2018-07-12 15:30:35 -0400885 // Use the text and textLen below, because we don't want to mess with the paint.
886 glyphRun.temporaryShuntToCallback(
Herb Derby59d997a2018-06-07 12:44:09 -0400887 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
888 blob = textContext->makeDrawPosTextBlob(
889 context->contextPriv().getTextBlobCache(), glyphCache,
890 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
Herb Derby8a6348e2018-07-12 15:30:35 -0400891 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps,
Herb Derbycddab252018-07-16 11:19:04 -0400892 text, textLen, pos, 2, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400893 });
Herb Derby8a6348e2018-07-12 15:30:35 -0400894 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500895
896 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500897 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500898}
899
Brian Salomon44acb5b2017-07-18 19:59:24 -0400900GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700901 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400902 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700903 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700904
905 if (context->uniqueID() != gContextID) {
906 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400907 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700908 }
909
Brian Osman11052242016-10-27 14:47:55 -0400910 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500911 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400912 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700913
joshualitt6c891102015-05-13 08:51:49 -0700914 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400915
916 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
917 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700918 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500919 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700920 skPaint.setLCDRenderText(random->nextBool());
921 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
922 skPaint.setSubpixelText(random->nextBool());
923
joshualitt79dfb2b2015-05-11 08:58:08 -0700924 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700925
joshualittb8d86492016-02-24 09:23:03 -0800926 // create some random x/y offsets, including negative offsets
927 static const int kMaxTrans = 1024;
928 int xPos = (random->nextU() % 2) * 2 - 1;
929 int yPos = (random->nextU() % 2) * 2 - 1;
930 int xInt = (random->nextU() % kMaxTrans) * xPos;
931 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700932
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500933 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
934 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700935}
936
937#endif