blob: 7b15abc6669c09f30dbe94b69a05ced66e27672d [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,
95 const SkGlyphRunList& glyphRunList) const {
96 SkPoint origin = glyphRunList.origin();
97 cacheBlob->initReusableBlob(paint.luminanceColor(), viewMatrix, origin.x(), origin.y());
98
99 // Regenerate textblob
100 GrTextUtils::RunPaint runPaint(&paint);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400101 int runIndex = 0;
Herb Derby12e42562018-07-28 14:27:48 -0400102 for (const auto& glyphRun : glyphRunList) {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400103 cacheBlob->push_back_run(runIndex);
Herb Derby12e42562018-07-28 14:27:48 -0400104
105 if (!runPaint.modifyForRun([glyphRun](SkPaint* p) { *p = glyphRun.paint(); })) {
106 continue;
107 }
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400108 cacheBlob->setRunPaintFlags(runIndex, runPaint.skPaint().getFlags());
Herb Derby12e42562018-07-28 14:27:48 -0400109
110 if (CanDrawAsDistanceFields(runPaint, viewMatrix, props,
111 shaderCaps.supportsDistanceFieldText(), fOptions)) {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400112 bool hasWCoord = viewMatrix.hasPerspective()
113 || fOptions.fDistanceFieldVerticesAlwaysHaveW;
114
115 // Setup distance field runPaint and text ratio
116 SkScalar textRatio;
117 SkPaint dfPaint(runPaint);
118 SkScalerContextFlags flags;
119 InitDistanceFieldPaint(cacheBlob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
120 cacheBlob->setHasDistanceField();
121 cacheBlob->setSubRunHasDistanceFields(runIndex, runPaint.skPaint().isLCDRenderText(),
122 runPaint.skPaint().isAntiAlias(), hasWCoord);
123
124 FallbackGlyphRunHelper fallbackTextHelper(
125 viewMatrix, runPaint, glyphCache->getGlyphSizeLimit(), textRatio);
126
127 sk_sp<GrTextStrike> currStrike;
128
129 {
130 auto cache = cacheBlob->setupCache(runIndex, props, flags, dfPaint, nullptr);
131
132 const SkPoint* positionCursor = glyphRun.positions().data();
133 for (auto glyphID : glyphRun.shuntGlyphsIDs()) {
134 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
135 SkPoint glyphPos = origin + *positionCursor++;
136 if (glyph.fWidth > 0) {
137 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
138 DfAppendGlyph(
139 cacheBlob, runIndex, glyphCache, &currStrike, glyph,
140 glyphPos.fX, glyphPos.fY, runPaint.filteredPremulColor(),
141 cache.get(), textRatio);
142 } else {
143 // can't append non-SDF glyph to SDF batch, send to fallback
144 fallbackTextHelper.appendText(glyph, glyphID, glyphPos);
145 }
146 }
147 }
148 }
149
150 fallbackTextHelper.drawText(
151 cacheBlob, runIndex, glyphCache, props, runPaint, scalerContextFlags);
152
153 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
154 // Ensure the blob is set for bitmaptext
155 cacheBlob->setHasBitmap();
156
157 // setup our std runPaint, in hopes of getting hits in the cache
158 SkPaint pathPaint(runPaint);
159 SkScalar matrixScale = pathPaint.setupForAsPaths();
160
161 FallbackGlyphRunHelper fallbackTextHelper(
162 viewMatrix, runPaint, glyphCache->getGlyphSizeLimit(), matrixScale);
163
164 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
165 pathPaint.setStyle(SkPaint::kFill_Style);
166 pathPaint.setPathEffect(nullptr);
167
168 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
169 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
170
171 const SkPoint* positionCursor = glyphRun.positions().data();
172 for (auto glyphID : glyphRun.shuntGlyphsIDs()) {
173 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
174 SkPoint loc = origin + *positionCursor++;
175 if (glyph.fWidth > 0) {
176 if (glyph.fMaskFormat == SkMask::kARGB32_Format) {
177 fallbackTextHelper.appendText(glyph, glyphID, loc);
178 } else {
179 const SkPath* path = cache->findPath(glyph);
180 if (path != nullptr) {
181 cacheBlob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
182 }
183 }
184 }
185 }
186
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);
196 SkFindAndPlaceGlyph::ProcessPosText(
197 SkPaint::kGlyphID_TextEncoding,
198 (const char*) glyphRun.shuntGlyphsIDs().data(),
199 glyphRun.shuntGlyphsIDs().size() * sizeof(SkGlyphID),
200 origin, viewMatrix, (const SkScalar*) glyphRun.positions().data(), 2, cache.get(),
201 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
202 position += rounding;
203 BmpAppendGlyph(cacheBlob, runIndex, glyphCache, &currStrike, glyph,
204 SkScalarFloorToScalar(position.fX),
205 SkScalarFloorToScalar(position.fY),
206 runPaint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
207 }
208 );
Herb Derby12e42562018-07-28 14:27:48 -0400209 }
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400210 runIndex += 1;
Herb Derby12e42562018-07-28 14:27:48 -0400211 }
212}
213
Herb Derbycddab252018-07-16 11:19:04 -0400214void GrTextContext::drawGlyphRunList(
215 GrContext* context, GrTextUtils::Target* target, const GrClip& clip,
Herb Derbyb935cf82018-07-26 16:54:18 -0400216 const SkMatrix& viewMatrix, const SkSurfaceProps& props, const SkGlyphRunList& glyphRunList,
Herb Derbycddab252018-07-16 11:19:04 -0400217 const SkIRect& clipBounds) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400218 SkPoint origin = glyphRunList.origin();
joshualitt9e36c1a2015-04-14 12:17:27 -0700219
Herb Derbycddab252018-07-16 11:19:04 -0400220 // Get the first paint to use as the key paint.
Herb Derbyb935cf82018-07-26 16:54:18 -0400221 const SkPaint& skPaint = glyphRunList.paint();
Herb Derbycddab252018-07-16 11:19:04 -0400222
joshualitt9b8e79e2015-04-24 09:57:12 -0700223 // If we have been abandoned, then don't draw
Khushalc421ca12018-06-26 14:38:34 -0700224 if (context->abandoned()) {
robertphillipsea461502015-05-26 11:38:03 -0700225 return;
226 }
227
Mike Reed80747ef2018-01-23 15:29:32 -0500228 SkMaskFilterBase::BlurRec blurRec;
joshualitt53b5f442015-04-13 06:33:59 -0700229 // It might be worth caching these things, but its not clear at this time
230 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
231 const SkMaskFilter* mf = skPaint.getMaskFilter();
Herb Derbyb935cf82018-07-26 16:54:18 -0400232 bool canCache = glyphRunList.canCache() && !(skPaint.getPathEffect() ||
Herb Derbycddab252018-07-16 11:19:04 -0400233 (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derbyd8327a82018-01-22 14:39:27 -0500234 SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
joshualitt2a0e9f32015-04-13 06:12:21 -0700235
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500236 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500237 GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache();
238
Herb Derbycddab252018-07-16 11:19:04 -0400239 sk_sp<GrTextBlob> cacheBlob;
240 GrTextBlob::Key key;
joshualitt2a0e9f32015-04-13 06:12:21 -0700241 if (canCache) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400242 bool hasLCD = glyphRunList.anyRunsLCD();
joshualitte4cee1f2015-05-11 13:04:28 -0700243
244 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
joshualitt2c89bc12016-02-11 05:42:30 -0800245 SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() :
Herb Derbycddab252018-07-16 11:19:04 -0400246 kUnknown_SkPixelGeometry;
joshualitte4cee1f2015-05-11 13:04:28 -0700247
joshualitt9e36c1a2015-04-14 12:17:27 -0700248 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
249 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
250 // ensure we always match the same key
251 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
Herb Derbycddab252018-07-16 11:19:04 -0400252 ComputeCanonicalColor(skPaint, hasLCD);
joshualitt9e36c1a2015-04-14 12:17:27 -0700253
joshualitte4cee1f2015-05-11 13:04:28 -0700254 key.fPixelGeometry = pixelGeometry;
Herb Derbyb935cf82018-07-26 16:54:18 -0400255 key.fUniqueID = glyphRunList.uniqueID();
joshualitt53b5f442015-04-13 06:33:59 -0700256 key.fStyle = skPaint.getStyle();
257 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700258 key.fCanonicalColor = canonicalColor;
brianosman8d7ffce2016-04-21 08:29:06 -0700259 key.fScalerContextFlags = scalerContextFlags;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500260 cacheBlob = textBlobCache->find(key);
joshualitt2a0e9f32015-04-13 06:12:21 -0700261 }
262
Brian Salomonf18b1d82017-10-27 11:30:49 -0400263 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
joshualittb7133be2015-04-08 09:08:31 -0700264 if (cacheBlob) {
Herb Derbycddab252018-07-16 11:19:04 -0400265 if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, origin.x(), origin.y())) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700266 // We have to remake the blob because changes may invalidate our masks.
267 // TODO we could probably get away reuse most of the time if the pointer is unique,
268 // but we'd have to clear the subrun information
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500269 textBlobCache->remove(cacheBlob.get());
Herb Derbycddab252018-07-16 11:19:04 -0400270 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
271 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400272 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400273 scalerContextFlags, viewMatrix, props, glyphRunList);
joshualittb7133be2015-04-08 09:08:31 -0700274 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500275 textBlobCache->makeMRU(cacheBlob.get());
joshualitt2f2ee832016-02-10 08:52:24 -0800276
277 if (CACHE_SANITY_CHECK) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400278 int glyphCount = glyphRunList.totalGlyphCount();
279 int runCount = glyphRunList.runCount();
Herb Derby86240592018-05-24 16:12:31 -0400280 sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
joshualitt92303772016-02-10 11:55:52 -0800281 sanityBlob->setupKey(key, blurRec, skPaint);
Herb Derbycddab252018-07-16 11:19:04 -0400282 this->regenerateGlyphRunList(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400283 sanityBlob.get(), glyphCache, *context->contextPriv().caps()->shaderCaps(),
Herb Derbycddab252018-07-16 11:19:04 -0400284 paint, scalerContextFlags, viewMatrix, props, glyphRunList);
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 Derbycddab252018-07-16 11:19:04 -0400296 scalerContextFlags, viewMatrix, props, glyphRunList);
joshualitt1d89e8d2015-04-01 12:40:54 -0700297 }
298
Robert Phillips5a66efb2018-03-07 15:13:18 -0500299 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400300 clip, viewMatrix, clipBounds, origin.x(), origin.y());
joshualitt1d89e8d2015-04-01 12:40:54 -0700301}
302
Herb Derby86240592018-05-24 16:12:31 -0400303inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400304GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
305 GrGlyphCache* glyphCache,
306 const GrShaderCaps& shaderCaps,
307 const GrTextUtils::Paint& paint,
308 SkScalerContextFlags scalerContextFlags,
309 const SkMatrix& viewMatrix,
310 const SkSurfaceProps& props,
311 const char text[], size_t byteLength,
312 const SkScalar pos[], int scalarsPerPosition, const
313 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500314 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400315 if (!glyphCount) {
316 return nullptr;
317 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700318
Herb Derby86240592018-05-24 16:12:31 -0400319 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800320 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500321 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700322
Khushal3e7548c2018-05-23 15:45:01 -0700323 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
324 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500325 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500326 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700327 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500328 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
329 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700330 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700331 return blob;
332}
333
Herb Derby26cbe512018-05-24 14:39:01 -0400334void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
335 const GrClip& clip, const SkPaint& skPaint,
336 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
337 const char text[], size_t byteLength, const SkScalar pos[],
338 int scalarsPerPosition, const SkPoint& offset,
339 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400340 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Khushalc421ca12018-06-26 14:38:34 -0700341 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800342 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500343 }
344
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500345 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500346 auto textBlobCache = context->contextPriv().getTextBlobCache();
347
Herb Derby86240592018-05-24 16:12:31 -0400348 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400349 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500350 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
351 byteLength, pos, scalarsPerPosition, offset));
352 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500353 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500354 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800355 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700356}
357
Herb Derby86240592018-05-24 16:12:31 -0400358void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400359 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
360 const GrTextUtils::Paint& paint,
361 SkScalerContextFlags scalerContextFlags,
362 const SkMatrix& viewMatrix,
363 const char text[], size_t byteLength, const SkScalar pos[],
364 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500365 SkASSERT(byteLength == 0 || text != nullptr);
366 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
367
368 // nothing to draw
369 if (text == nullptr || byteLength == 0) {
370 return;
371 }
372
373 // Ensure the blob is set for bitmaptext
374 blob->setHasBitmap();
375
Jim Van Verth54d9c882018-02-08 16:14:48 -0500376 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500377 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500378 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500379 return;
380 }
381
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000382 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500383 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500384 SkFindAndPlaceGlyph::ProcessPosText(
385 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400386 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500387 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
388 position += rounding;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500389 BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500390 SkScalarFloorToScalar(position.fX),
391 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400392 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500393 });
Brian Salomon52db9402017-11-07 14:58:55 -0500394}
395
Herb Derby86240592018-05-24 16:12:31 -0400396void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400397 GrGlyphCache* glyphCache,
398 const SkSurfaceProps& props,
399 const GrTextUtils::Paint& origPaint,
400 SkScalerContextFlags scalerContextFlags,
401 const SkMatrix& viewMatrix,
402 const char text[], size_t byteLength,
403 const SkScalar pos[], int scalarsPerPosition,
404 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500405 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
406
407 // nothing to draw
408 if (text == nullptr || byteLength == 0) {
409 return;
410 }
411
412 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500413 SkPaint pathPaint(origPaint);
414 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700415 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
416 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500417
418 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500419 pathPaint.setStyle(SkPaint::kFill_Style);
420 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500421
Jim Van Verthc401bb92018-02-15 14:05:24 -0500422 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500423 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400424 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400425 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500426
427 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500428 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500429 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
430
431 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400432 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500433 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500434 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400435 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500436 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
437 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
438 } else {
439 const SkPath* path = cache->findPath(glyph);
440 if (path) {
441 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
442 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500443 }
444 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500445 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500446 pos += scalarsPerPosition;
447 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500448
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500449 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500450}
451
Herb Derby86240592018-05-24 16:12:31 -0400452void GrTextContext::BmpAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400453 GrGlyphCache* grGlyphCache,
454 sk_sp<GrTextStrike>* strike,
455 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
456 GrColor color, SkGlyphCache* skGlyphCache,
457 SkScalar textRatio, bool needsTransform) {
Brian Salomon52db9402017-11-07 14:58:55 -0500458 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500459 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500460 }
461
462 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
463 skGlyph.getSubXFixed(),
464 skGlyph.getSubYFixed(),
465 GrGlyph::kCoverage_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500466 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500467 if (!glyph) {
468 return;
469 }
470
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500471 SkASSERT(skGlyph.fWidth == glyph->width());
472 SkASSERT(skGlyph.fHeight == glyph->height());
473
Jim Van Verthf4c13162018-01-11 16:40:24 -0500474 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft);
475 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop);
476 SkScalar width = SkIntToScalar(glyph->fBounds.width());
477 SkScalar height = SkIntToScalar(glyph->fBounds.height());
Brian Salomon52db9402017-11-07 14:58:55 -0500478
Jim Van Verthf4c13162018-01-11 16:40:24 -0500479 dx *= textRatio;
480 dy *= textRatio;
481 width *= textRatio;
482 height *= textRatio;
Brian Salomon52db9402017-11-07 14:58:55 -0500483
Jim Van Verthf4c13162018-01-11 16:40:24 -0500484 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500485
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500486 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400487 textRatio, !needsTransform);
Brian Salomon52db9402017-11-07 14:58:55 -0500488}
489
Herb Derby26cbe512018-05-24 14:39:01 -0400490void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700491 if (options->fMaxDistanceFieldFontSize < 0.f) {
492 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
493 }
494 if (options->fMinDistanceFieldFontSize < 0.f) {
495 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
496 }
497}
498
Herb Derby26cbe512018-05-24 14:39:01 -0400499bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
500 const SkSurfaceProps& props,
501 bool contextSupportsDistanceFieldText,
502 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500503 if (!viewMatrix.hasPerspective()) {
504 SkScalar maxScale = viewMatrix.getMaxScale();
505 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
506 // Hinted text looks far better at small resolutions
507 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700508 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
509 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500510 return false;
511 }
512
513 bool useDFT = props.isUseDeviceIndependentFonts();
514#if SK_FORCE_DISTANCE_FIELD_TEXT
515 useDFT = true;
516#endif
517
518 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
519 return false;
520 }
521 }
522
Mike Reed8ad91a92018-01-19 19:09:32 -0500523 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700524 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500525 return false;
526 }
527
528 // TODO: add some stroking support
529 if (skPaint.getStyle() != SkPaint::kFill_Style) {
530 return false;
531 }
532
533 return true;
534}
535
Herb Derby86240592018-05-24 16:12:31 -0400536void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400537 SkPaint* skPaint,
538 const SkMatrix& viewMatrix,
539 const Options& options,
540 SkScalar* textRatio,
541 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500542 SkScalar textSize = skPaint->getTextSize();
543 SkScalar scaledTextSize = textSize;
544
545 if (viewMatrix.hasPerspective()) {
546 // for perspective, we simply force to the medium size
547 // TODO: compute a size based on approximate screen area
548 scaledTextSize = kMediumDFFontLimit;
549 } else {
550 SkScalar maxScale = viewMatrix.getMaxScale();
551 // if we have non-unity scale, we need to choose our base text size
552 // based on the SkPaint's text size multiplied by the max scale factor
553 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
554 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
555 scaledTextSize *= maxScale;
556 }
557 }
558
559 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
560 // and ceiling. A scale outside of this range would require regenerating the distance fields
561 SkScalar dfMaskScaleFloor;
562 SkScalar dfMaskScaleCeil;
563 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700564 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500565 dfMaskScaleCeil = kSmallDFFontLimit;
566 *textRatio = textSize / kSmallDFFontSize;
567 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
568 } else if (scaledTextSize <= kMediumDFFontLimit) {
569 dfMaskScaleFloor = kSmallDFFontLimit;
570 dfMaskScaleCeil = kMediumDFFontLimit;
571 *textRatio = textSize / kMediumDFFontSize;
572 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
573 } else {
574 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700575 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500576 *textRatio = textSize / kLargeDFFontSize;
577 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
578 }
579
580 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
581 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
582 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
583 // tolerate before we'd have to move to a large mip size. When we actually test these values
584 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
585 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
586 // level)
587 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700588 if (blob) {
589 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
590 dfMaskScaleCeil / scaledTextSize);
591 }
Brian Salomon52db9402017-11-07 14:58:55 -0500592
593 skPaint->setAntiAlias(true);
594 skPaint->setLCDRenderText(false);
595 skPaint->setAutohinted(false);
596 skPaint->setHinting(SkPaint::kNormal_Hinting);
597 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400598
599 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700600
601 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
602 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
603 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500604}
605
Herb Derby86240592018-05-24 16:12:31 -0400606void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400607 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
608 const GrTextUtils::Paint& paint,
609 SkScalerContextFlags scalerContextFlags,
610 const SkMatrix& viewMatrix, const char text[],
611 size_t byteLength, const SkScalar pos[],
612 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500613 SkASSERT(byteLength == 0 || text != nullptr);
614 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
615
616 // nothing to draw
617 if (text == nullptr || byteLength == 0) {
618 return;
619 }
620
Khushal3e7548c2018-05-23 15:45:01 -0700621 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500622
Brian Salomon52db9402017-11-07 14:58:55 -0500623 // Setup distance field paint and text ratio
624 SkScalar textRatio;
625 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700626 SkScalerContextFlags flags;
627 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500628 blob->setHasDistanceField();
629 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500630 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500631
Khushalfa8ff092018-06-06 17:46:38 -0700632 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
633 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500634
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500635 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500636
Herb Derby526819d2018-03-09 12:51:12 -0500637 {
Khushal3e7548c2018-05-23 15:45:01 -0700638 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500639 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400640 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500641
Herb Derby526819d2018-03-09 12:51:12 -0500642 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500643
Herb Derby526819d2018-03-09 12:51:12 -0500644 while (text < stop) {
645 const char* lastText = text;
646 // the last 2 parameters are ignored
Hal Canary4014ba62018-07-24 11:33:21 -0400647 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Brian Salomon52db9402017-11-07 14:58:55 -0500648
Herb Derby526819d2018-03-09 12:51:12 -0500649 if (glyph.fWidth) {
650 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400651 glyphPos.fX += pos[0];
652 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500653
Jim Van Verthd401da62018-05-03 10:40:30 -0400654 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby526819d2018-03-09 12:51:12 -0500655 DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
656 glyphPos.fY, paint.filteredPremulColor(), cache.get(), textRatio);
657 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400658 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500659 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
660 glyphPos);
661 }
Brian Salomon52db9402017-11-07 14:58:55 -0500662 }
Herb Derby526819d2018-03-09 12:51:12 -0500663 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500664 }
Brian Salomon52db9402017-11-07 14:58:55 -0500665 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000666
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500667 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500668}
669
Jim Van Verthf4c13162018-01-11 16:40:24 -0500670// TODO: merge with BmpAppendGlyph
Herb Derby86240592018-05-24 16:12:31 -0400671void GrTextContext::DfAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400672 GrGlyphCache* grGlyphCache, sk_sp<GrTextStrike>* strike,
673 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
674 GrColor color, SkGlyphCache* skGlyphCache,
675 SkScalar textRatio) {
Brian Salomon52db9402017-11-07 14:58:55 -0500676 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500677 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500678 }
679
680 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
681 skGlyph.getSubXFixed(),
682 skGlyph.getSubYFixed(),
683 GrGlyph::kDistance_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500684 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500685 if (!glyph) {
Jim Van Verthf4c13162018-01-11 16:40:24 -0500686 return;
Brian Salomon52db9402017-11-07 14:58:55 -0500687 }
688
689 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
690 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
691 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
692 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
693
Jim Van Verthf4c13162018-01-11 16:40:24 -0500694 dx *= textRatio;
695 dy *= textRatio;
696 width *= textRatio;
697 height *= textRatio;
698 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500699
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500700 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500701 textRatio, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500702}
703
joshualitt79dfb2b2015-05-11 08:58:08 -0700704///////////////////////////////////////////////////////////////////////////////////////////////////
705
Herb Derby26cbe512018-05-24 14:39:01 -0400706void GrTextContext::FallbackTextHelper::appendText(const SkGlyph& glyph, int count,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500707 const char* text, SkPoint glyphPos) {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400708 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
Khushalfa8ff092018-06-06 17:46:38 -0700709 if (SkScalarNearlyZero(maxDim)) return;
710
Jim Van Verthb515ae72018-05-23 16:44:55 -0400711 if (!fUseTransformedFallback) {
712 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
713 fUseTransformedFallback = true;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500714 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
Jim Van Verthc401bb92018-02-15 14:05:24 -0500715 }
716 }
717
718 fFallbackTxt.append(count, text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400719 if (fUseTransformedFallback) {
Jim Van Verth080a9282018-03-02 10:41:43 -0500720 // If there's a glyph in the font that's particularly large, it's possible
721 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
Jim Van Verth8b7284d2018-05-17 12:33:52 -0400722 // that glyph than make the others blurry, so we set a minimum size of half the
Jim Van Verth080a9282018-03-02 10:41:43 -0500723 // maximum text size to avoid this case.
Jim Van Verthb515ae72018-05-23 16:44:55 -0400724 SkScalar glyphTextSize = SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim),
Jim Van Verth080a9282018-03-02 10:41:43 -0500725 0.5f*fMaxTextSize);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400726 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500727 }
728 *fFallbackPos.append() = glyphPos;
729}
730
Herb Derby86240592018-05-24 16:12:31 -0400731void GrTextContext::FallbackTextHelper::drawText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400732 GrGlyphCache* glyphCache,
733 const SkSurfaceProps& props,
734 const GrTextUtils::Paint& paint,
735 SkScalerContextFlags scalerContextFlags) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500736 if (fFallbackTxt.count()) {
737 blob->initOverride(runIndex);
738 blob->setHasBitmap();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400739 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
Herb Derby526819d2018-03-09 12:51:12 -0500740 SkExclusiveStrikePtr cache;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500741 const SkPaint& skPaint = paint.skPaint();
742 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400743 SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500744 SkColor textColor = paint.filteredPremulColor();
Khushalfa8ff092018-06-06 17:46:38 -0700745
Jim Van Verthc401bb92018-02-15 14:05:24 -0500746 SkScalar textRatio = SK_Scalar1;
Khushalfa8ff092018-06-06 17:46:38 -0700747 SkPaint fallbackPaint(skPaint);
748 SkMatrix matrix = fViewMatrix;
749 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
750 cache = blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500751
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500752 sk_sp<GrTextStrike> currStrike;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500753 const char* text = fFallbackTxt.begin();
754 const char* stop = text + fFallbackTxt.count();
755 SkPoint* glyphPos = fFallbackPos.begin();
756 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400757 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400758 if (!fUseTransformedFallback) {
759 fViewMatrix.mapPoints(glyphPos, 1);
Jim Van Verth76e85162018-03-29 13:46:56 -0400760 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
761 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
762 }
Herb Derby26cbe512018-05-24 14:39:01 -0400763 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500764 glyphPos->fX, glyphPos->fY, textColor,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400765 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500766 glyphPos++;
767 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500768 }
769}
770
Khushalfa8ff092018-06-06 17:46:38 -0700771void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
772 SkMatrix* matrix) const {
773 if (!fUseTransformedFallback) return;
774
775 paint->setTextSize(fTransformedFallbackTextSize);
776 *textRatio = fTextSize / fTransformedFallbackTextSize;
777 *matrix = SkMatrix::I();
778}
779
Jim Van Verthc401bb92018-02-15 14:05:24 -0500780///////////////////////////////////////////////////////////////////////////////////////////////////
781
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400782void GrTextContext::FallbackGlyphRunHelper::appendText(
783 const SkGlyph& glyph, SkGlyphID glyphID, SkPoint glyphPos) {
784 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
785 if (SkScalarNearlyZero(maxDim)) return;
786
787 if (!fUseTransformedFallback) {
788 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
789 fUseTransformedFallback = true;
790 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
791 }
792 }
793
794 fFallbackTxt.push_back(glyphID);
795 if (fUseTransformedFallback) {
796 // If there's a glyph in the font that's particularly large, it's possible
797 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
798 // that glyph than make the others blurry, so we set a minimum size of half the
799 // maximum text size to avoid this case.
800 SkScalar glyphTextSize =
801 SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim), 0.5f*fMaxTextSize);
802 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
803 }
804 fFallbackPos.push_back(glyphPos);
805}
806
807void GrTextContext::FallbackGlyphRunHelper::drawText(
808 GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props,
809 const GrTextUtils::Paint& paint, SkScalerContextFlags scalerContextFlags) {
810 if (!fFallbackTxt.empty()) {
811 blob->initOverride(runIndex);
812 blob->setHasBitmap();
813 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
814 const SkPaint& skPaint = paint.skPaint();
815 SkColor textColor = paint.filteredPremulColor();
816
817 SkScalar textRatio = SK_Scalar1;
818 SkPaint fallbackPaint(skPaint);
819 SkMatrix matrix = fViewMatrix;
820 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
821 SkExclusiveStrikePtr cache =
822 blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
823
824 sk_sp<GrTextStrike> currStrike;
825 auto glyphPos = fFallbackPos.begin();
826 for (auto glyphID : fFallbackTxt) {
827 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
828 if (!fUseTransformedFallback) {
829 fViewMatrix.mapPoints(&*glyphPos, 1);
830 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
831 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
832 }
833 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
834 glyphPos->fX, glyphPos->fY, textColor,
835 cache.get(), textRatio, fUseTransformedFallback);
836 glyphPos++;
837 }
838 }
839}
840
841void GrTextContext::FallbackGlyphRunHelper::initializeForDraw(
842 SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const {
843 if (!fUseTransformedFallback) return;
844
845 paint->setTextSize(fTransformedFallbackTextSize);
846 *textRatio = fTextSize / fTransformedFallbackTextSize;
847 *matrix = SkMatrix::I();
848}
849
850///////////////////////////////////////////////////////////////////////////////////////////////////
851
852
Hal Canary6f6961e2017-01-31 13:50:44 -0500853#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700854
Brian Salomonf18b1d82017-10-27 11:30:49 -0400855#include "GrRenderTargetContext.h"
856
Herb Derby26cbe512018-05-24 14:39:01 -0400857std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
858 GrTextContext* textContext,
859 GrRenderTargetContext* rtc,
860 const SkPaint& skPaint,
861 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400862 const char* text,
863 int x,
864 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500865 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500866
867 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
868
869 size_t textLen = (int)strlen(text);
870
871 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
872
873 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
874 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000875
876 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400877 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -0400878 builder.drawText(skPaint, text, textLen, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400879 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000880
Herb Derby8a6348e2018-07-12 15:30:35 -0400881 auto glyphRunList = builder.useGlyphRunList();
Herb Derbyb935cf82018-07-26 16:54:18 -0400882 if (!glyphRunList.empty()) {
883 auto glyphRun = glyphRunList[0];
Herb Derby8a6348e2018-07-12 15:30:35 -0400884 // Use the text and textLen below, because we don't want to mess with the paint.
885 glyphRun.temporaryShuntToCallback(
Herb Derby59d997a2018-06-07 12:44:09 -0400886 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
887 blob = textContext->makeDrawPosTextBlob(
888 context->contextPriv().getTextBlobCache(), glyphCache,
889 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
Herb Derby8a6348e2018-07-12 15:30:35 -0400890 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps,
Herb Derbycddab252018-07-16 11:19:04 -0400891 text, textLen, pos, 2, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400892 });
Herb Derby8a6348e2018-07-12 15:30:35 -0400893 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500894
895 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500896 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500897}
898
Brian Salomon44acb5b2017-07-18 19:59:24 -0400899GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700900 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400901 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700902 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700903
904 if (context->uniqueID() != gContextID) {
905 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400906 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700907 }
908
Brian Osman11052242016-10-27 14:47:55 -0400909 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500910 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400911 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700912
joshualitt6c891102015-05-13 08:51:49 -0700913 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400914
915 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
916 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700917 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500918 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700919 skPaint.setLCDRenderText(random->nextBool());
920 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
921 skPaint.setSubpixelText(random->nextBool());
922
joshualitt79dfb2b2015-05-11 08:58:08 -0700923 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700924
joshualittb8d86492016-02-24 09:23:03 -0800925 // create some random x/y offsets, including negative offsets
926 static const int kMaxTrans = 1024;
927 int xPos = (random->nextU() % 2) * 2 - 1;
928 int yPos = (random->nextU() % 2) * 2 - 1;
929 int xInt = (random->nextU() % kMaxTrans) * xPos;
930 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700931
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500932 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
933 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700934}
935
936#endif