blob: 6017742707a6121e296a7aab7328a06f48e3243a [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
172 const SkPoint* positionCursor = glyphRun.positions().data();
173 for (auto glyphID : glyphRun.shuntGlyphsIDs()) {
174 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
175 SkPoint loc = origin + *positionCursor++;
176 if (glyph.fWidth > 0) {
177 if (glyph.fMaskFormat == SkMask::kARGB32_Format) {
178 fallbackTextHelper.appendText(glyph, glyphID, loc);
179 } else {
180 const SkPath* path = cache->findPath(glyph);
181 if (path != nullptr) {
182 cacheBlob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
183 }
184 }
185 }
186 }
187
188 fallbackTextHelper.drawText(
189 cacheBlob, runIndex, glyphCache, props, runPaint, scalerContextFlags);
190
Herb Derby12e42562018-07-28 14:27:48 -0400191 } else {
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400192 // Ensure the blob is set for bitmaptext
193 cacheBlob->setHasBitmap();
194 sk_sp<GrTextStrike> currStrike;
195 auto cache = cacheBlob->setupCache(
196 runIndex, props, scalerContextFlags, runPaint, &viewMatrix);
Herb Derby74c6ed32018-07-28 18:07:54 -0400197
198 auto drawOneGlyph =
199 [cacheBlob, runIndex, glyphCache, &currStrike, runPaint, cache{cache.get()}]
200 (const SkMask& mask, const SkGlyph& glyph, SkPoint position) {
201 BmpAppendGlyph(cacheBlob, runIndex, glyphCache, &currStrike, glyph,
202 SkScalarFloorToScalar(position.fX),
203 SkScalarFloorToScalar(position.fY),
204 runPaint.filteredPremulColor(), cache, SK_Scalar1, false);
205 };
206
207 glyphDrawer->drawUsingMasks(cache.get(), glyphRun, origin, viewMatrix, drawOneGlyph);
Herb Derby12e42562018-07-28 14:27:48 -0400208 }
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400209 runIndex += 1;
Herb Derby12e42562018-07-28 14:27:48 -0400210 }
211}
212
Herb Derbycddab252018-07-16 11:19:04 -0400213void GrTextContext::drawGlyphRunList(
214 GrContext* context, GrTextUtils::Target* target, const GrClip& clip,
Herb Derbyb935cf82018-07-26 16:54:18 -0400215 const SkMatrix& viewMatrix, const SkSurfaceProps& props, const SkGlyphRunList& glyphRunList,
Herb Derbycddab252018-07-16 11:19:04 -0400216 const SkIRect& clipBounds) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400217 SkPoint origin = glyphRunList.origin();
joshualitt9e36c1a2015-04-14 12:17:27 -0700218
Herb Derbycddab252018-07-16 11:19:04 -0400219 // Get the first paint to use as the key paint.
Herb Derbyb935cf82018-07-26 16:54:18 -0400220 const SkPaint& skPaint = glyphRunList.paint();
Herb Derbycddab252018-07-16 11:19:04 -0400221
joshualitt9b8e79e2015-04-24 09:57:12 -0700222 // If we have been abandoned, then don't draw
Khushalc421ca12018-06-26 14:38:34 -0700223 if (context->abandoned()) {
robertphillipsea461502015-05-26 11:38:03 -0700224 return;
225 }
226
Mike Reed80747ef2018-01-23 15:29:32 -0500227 SkMaskFilterBase::BlurRec blurRec;
joshualitt53b5f442015-04-13 06:33:59 -0700228 // It might be worth caching these things, but its not clear at this time
229 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
230 const SkMaskFilter* mf = skPaint.getMaskFilter();
Herb Derbyb935cf82018-07-26 16:54:18 -0400231 bool canCache = glyphRunList.canCache() && !(skPaint.getPathEffect() ||
Herb Derbycddab252018-07-16 11:19:04 -0400232 (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derbyd8327a82018-01-22 14:39:27 -0500233 SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
joshualitt2a0e9f32015-04-13 06:12:21 -0700234
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500235 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500236 GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache();
237
Herb Derbycddab252018-07-16 11:19:04 -0400238 sk_sp<GrTextBlob> cacheBlob;
239 GrTextBlob::Key key;
joshualitt2a0e9f32015-04-13 06:12:21 -0700240 if (canCache) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400241 bool hasLCD = glyphRunList.anyRunsLCD();
joshualitte4cee1f2015-05-11 13:04:28 -0700242
243 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
joshualitt2c89bc12016-02-11 05:42:30 -0800244 SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() :
Herb Derbycddab252018-07-16 11:19:04 -0400245 kUnknown_SkPixelGeometry;
joshualitte4cee1f2015-05-11 13:04:28 -0700246
joshualitt9e36c1a2015-04-14 12:17:27 -0700247 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
248 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
249 // ensure we always match the same key
250 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
Herb Derbycddab252018-07-16 11:19:04 -0400251 ComputeCanonicalColor(skPaint, hasLCD);
joshualitt9e36c1a2015-04-14 12:17:27 -0700252
joshualitte4cee1f2015-05-11 13:04:28 -0700253 key.fPixelGeometry = pixelGeometry;
Herb Derbyb935cf82018-07-26 16:54:18 -0400254 key.fUniqueID = glyphRunList.uniqueID();
joshualitt53b5f442015-04-13 06:33:59 -0700255 key.fStyle = skPaint.getStyle();
256 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700257 key.fCanonicalColor = canonicalColor;
brianosman8d7ffce2016-04-21 08:29:06 -0700258 key.fScalerContextFlags = scalerContextFlags;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500259 cacheBlob = textBlobCache->find(key);
joshualitt2a0e9f32015-04-13 06:12:21 -0700260 }
261
Brian Salomonf18b1d82017-10-27 11:30:49 -0400262 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
joshualittb7133be2015-04-08 09:08:31 -0700263 if (cacheBlob) {
Herb Derbycddab252018-07-16 11:19:04 -0400264 if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, origin.x(), origin.y())) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700265 // We have to remake the blob because changes may invalidate our masks.
266 // TODO we could probably get away reuse most of the time if the pointer is unique,
267 // but we'd have to clear the subrun information
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500268 textBlobCache->remove(cacheBlob.get());
Herb Derbycddab252018-07-16 11:19:04 -0400269 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
270 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400271 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derby74c6ed32018-07-28 18:07:54 -0400272 scalerContextFlags, viewMatrix, props, glyphRunList,
273 target->glyphDrawer());
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 Derby74c6ed32018-07-28 18:07:54 -0400284 paint, scalerContextFlags, viewMatrix, props, glyphRunList,
285 target->glyphDrawer());
Herb Derby86240592018-05-24 16:12:31 -0400286 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700287 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700288 }
289 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700290 if (canCache) {
Herb Derbycddab252018-07-16 11:19:04 -0400291 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
joshualitt2a0e9f32015-04-13 06:12:21 -0700292 } else {
Herb Derbycddab252018-07-16 11:19:04 -0400293 cacheBlob = textBlobCache->makeBlob(glyphRunList);
joshualitt2a0e9f32015-04-13 06:12:21 -0700294 }
Herb Derbycddab252018-07-16 11:19:04 -0400295 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400296 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derby74c6ed32018-07-28 18:07:54 -0400297 scalerContextFlags, viewMatrix, props, glyphRunList,
298 target->glyphDrawer());
joshualitt1d89e8d2015-04-01 12:40:54 -0700299 }
300
Robert Phillips5a66efb2018-03-07 15:13:18 -0500301 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400302 clip, viewMatrix, clipBounds, origin.x(), origin.y());
joshualitt1d89e8d2015-04-01 12:40:54 -0700303}
304
Herb Derby86240592018-05-24 16:12:31 -0400305inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400306GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
307 GrGlyphCache* glyphCache,
308 const GrShaderCaps& shaderCaps,
309 const GrTextUtils::Paint& paint,
310 SkScalerContextFlags scalerContextFlags,
311 const SkMatrix& viewMatrix,
312 const SkSurfaceProps& props,
313 const char text[], size_t byteLength,
314 const SkScalar pos[], int scalarsPerPosition, const
315 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500316 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400317 if (!glyphCount) {
318 return nullptr;
319 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700320
Herb Derby86240592018-05-24 16:12:31 -0400321 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800322 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500323 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700324
Khushal3e7548c2018-05-23 15:45:01 -0700325 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
326 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500327 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500328 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700329 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500330 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
331 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700332 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700333 return blob;
334}
335
Herb Derby26cbe512018-05-24 14:39:01 -0400336void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
337 const GrClip& clip, const SkPaint& skPaint,
338 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
339 const char text[], size_t byteLength, const SkScalar pos[],
340 int scalarsPerPosition, const SkPoint& offset,
341 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400342 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Khushalc421ca12018-06-26 14:38:34 -0700343 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800344 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500345 }
346
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500347 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500348 auto textBlobCache = context->contextPriv().getTextBlobCache();
349
Herb Derby86240592018-05-24 16:12:31 -0400350 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400351 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500352 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
353 byteLength, pos, scalarsPerPosition, offset));
354 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500355 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500356 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800357 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700358}
359
Herb Derby86240592018-05-24 16:12:31 -0400360void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400361 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
362 const GrTextUtils::Paint& paint,
363 SkScalerContextFlags scalerContextFlags,
364 const SkMatrix& viewMatrix,
365 const char text[], size_t byteLength, const SkScalar pos[],
366 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500367 SkASSERT(byteLength == 0 || text != nullptr);
368 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
369
370 // nothing to draw
371 if (text == nullptr || byteLength == 0) {
372 return;
373 }
374
375 // Ensure the blob is set for bitmaptext
376 blob->setHasBitmap();
377
Jim Van Verth54d9c882018-02-08 16:14:48 -0500378 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500379 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500380 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500381 return;
382 }
383
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000384 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500385 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500386 SkFindAndPlaceGlyph::ProcessPosText(
387 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400388 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500389 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
390 position += rounding;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500391 BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500392 SkScalarFloorToScalar(position.fX),
393 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400394 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500395 });
Brian Salomon52db9402017-11-07 14:58:55 -0500396}
397
Herb Derby86240592018-05-24 16:12:31 -0400398void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400399 GrGlyphCache* glyphCache,
400 const SkSurfaceProps& props,
401 const GrTextUtils::Paint& origPaint,
402 SkScalerContextFlags scalerContextFlags,
403 const SkMatrix& viewMatrix,
404 const char text[], size_t byteLength,
405 const SkScalar pos[], int scalarsPerPosition,
406 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500407 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
408
409 // nothing to draw
410 if (text == nullptr || byteLength == 0) {
411 return;
412 }
413
414 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500415 SkPaint pathPaint(origPaint);
416 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700417 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
418 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500419
420 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500421 pathPaint.setStyle(SkPaint::kFill_Style);
422 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500423
Jim Van Verthc401bb92018-02-15 14:05:24 -0500424 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500425 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400426 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400427 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500428
429 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500430 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500431 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
432
433 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400434 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500435 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500436 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400437 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500438 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
439 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
440 } else {
441 const SkPath* path = cache->findPath(glyph);
442 if (path) {
443 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
444 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500445 }
446 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500447 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500448 pos += scalarsPerPosition;
449 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500450
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500451 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500452}
453
Herb Derby86240592018-05-24 16:12:31 -0400454void GrTextContext::BmpAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400455 GrGlyphCache* grGlyphCache,
456 sk_sp<GrTextStrike>* strike,
457 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
458 GrColor color, SkGlyphCache* skGlyphCache,
459 SkScalar textRatio, bool needsTransform) {
Brian Salomon52db9402017-11-07 14:58:55 -0500460 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500461 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500462 }
463
464 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
465 skGlyph.getSubXFixed(),
466 skGlyph.getSubYFixed(),
467 GrGlyph::kCoverage_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500468 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500469 if (!glyph) {
470 return;
471 }
472
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500473 SkASSERT(skGlyph.fWidth == glyph->width());
474 SkASSERT(skGlyph.fHeight == glyph->height());
475
Jim Van Verthf4c13162018-01-11 16:40:24 -0500476 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft);
477 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop);
478 SkScalar width = SkIntToScalar(glyph->fBounds.width());
479 SkScalar height = SkIntToScalar(glyph->fBounds.height());
Brian Salomon52db9402017-11-07 14:58:55 -0500480
Jim Van Verthf4c13162018-01-11 16:40:24 -0500481 dx *= textRatio;
482 dy *= textRatio;
483 width *= textRatio;
484 height *= textRatio;
Brian Salomon52db9402017-11-07 14:58:55 -0500485
Jim Van Verthf4c13162018-01-11 16:40:24 -0500486 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500487
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500488 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400489 textRatio, !needsTransform);
Brian Salomon52db9402017-11-07 14:58:55 -0500490}
491
Herb Derby26cbe512018-05-24 14:39:01 -0400492void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700493 if (options->fMaxDistanceFieldFontSize < 0.f) {
494 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
495 }
496 if (options->fMinDistanceFieldFontSize < 0.f) {
497 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
498 }
499}
500
Herb Derby26cbe512018-05-24 14:39:01 -0400501bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
502 const SkSurfaceProps& props,
503 bool contextSupportsDistanceFieldText,
504 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500505 if (!viewMatrix.hasPerspective()) {
506 SkScalar maxScale = viewMatrix.getMaxScale();
507 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
508 // Hinted text looks far better at small resolutions
509 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700510 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
511 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500512 return false;
513 }
514
515 bool useDFT = props.isUseDeviceIndependentFonts();
516#if SK_FORCE_DISTANCE_FIELD_TEXT
517 useDFT = true;
518#endif
519
520 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
521 return false;
522 }
523 }
524
Mike Reed8ad91a92018-01-19 19:09:32 -0500525 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700526 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500527 return false;
528 }
529
530 // TODO: add some stroking support
531 if (skPaint.getStyle() != SkPaint::kFill_Style) {
532 return false;
533 }
534
535 return true;
536}
537
Herb Derby86240592018-05-24 16:12:31 -0400538void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400539 SkPaint* skPaint,
540 const SkMatrix& viewMatrix,
541 const Options& options,
542 SkScalar* textRatio,
543 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500544 SkScalar textSize = skPaint->getTextSize();
545 SkScalar scaledTextSize = textSize;
546
547 if (viewMatrix.hasPerspective()) {
548 // for perspective, we simply force to the medium size
549 // TODO: compute a size based on approximate screen area
550 scaledTextSize = kMediumDFFontLimit;
551 } else {
552 SkScalar maxScale = viewMatrix.getMaxScale();
553 // if we have non-unity scale, we need to choose our base text size
554 // based on the SkPaint's text size multiplied by the max scale factor
555 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
556 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
557 scaledTextSize *= maxScale;
558 }
559 }
560
561 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
562 // and ceiling. A scale outside of this range would require regenerating the distance fields
563 SkScalar dfMaskScaleFloor;
564 SkScalar dfMaskScaleCeil;
565 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700566 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500567 dfMaskScaleCeil = kSmallDFFontLimit;
568 *textRatio = textSize / kSmallDFFontSize;
569 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
570 } else if (scaledTextSize <= kMediumDFFontLimit) {
571 dfMaskScaleFloor = kSmallDFFontLimit;
572 dfMaskScaleCeil = kMediumDFFontLimit;
573 *textRatio = textSize / kMediumDFFontSize;
574 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
575 } else {
576 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700577 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500578 *textRatio = textSize / kLargeDFFontSize;
579 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
580 }
581
582 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
583 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
584 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
585 // tolerate before we'd have to move to a large mip size. When we actually test these values
586 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
587 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
588 // level)
589 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700590 if (blob) {
591 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
592 dfMaskScaleCeil / scaledTextSize);
593 }
Brian Salomon52db9402017-11-07 14:58:55 -0500594
595 skPaint->setAntiAlias(true);
596 skPaint->setLCDRenderText(false);
597 skPaint->setAutohinted(false);
598 skPaint->setHinting(SkPaint::kNormal_Hinting);
599 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400600
601 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700602
603 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
604 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
605 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500606}
607
Herb Derby86240592018-05-24 16:12:31 -0400608void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400609 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
610 const GrTextUtils::Paint& paint,
611 SkScalerContextFlags scalerContextFlags,
612 const SkMatrix& viewMatrix, const char text[],
613 size_t byteLength, const SkScalar pos[],
614 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500615 SkASSERT(byteLength == 0 || text != nullptr);
616 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
617
618 // nothing to draw
619 if (text == nullptr || byteLength == 0) {
620 return;
621 }
622
Khushal3e7548c2018-05-23 15:45:01 -0700623 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500624
Brian Salomon52db9402017-11-07 14:58:55 -0500625 // Setup distance field paint and text ratio
626 SkScalar textRatio;
627 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700628 SkScalerContextFlags flags;
629 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500630 blob->setHasDistanceField();
631 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500632 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500633
Khushalfa8ff092018-06-06 17:46:38 -0700634 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
635 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500636
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500637 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500638
Herb Derby526819d2018-03-09 12:51:12 -0500639 {
Khushal3e7548c2018-05-23 15:45:01 -0700640 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500641 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400642 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500643
Herb Derby526819d2018-03-09 12:51:12 -0500644 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500645
Herb Derby526819d2018-03-09 12:51:12 -0500646 while (text < stop) {
647 const char* lastText = text;
648 // the last 2 parameters are ignored
Hal Canary4014ba62018-07-24 11:33:21 -0400649 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Brian Salomon52db9402017-11-07 14:58:55 -0500650
Herb Derby526819d2018-03-09 12:51:12 -0500651 if (glyph.fWidth) {
652 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400653 glyphPos.fX += pos[0];
654 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500655
Jim Van Verthd401da62018-05-03 10:40:30 -0400656 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby526819d2018-03-09 12:51:12 -0500657 DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
658 glyphPos.fY, paint.filteredPremulColor(), cache.get(), textRatio);
659 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400660 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500661 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
662 glyphPos);
663 }
Brian Salomon52db9402017-11-07 14:58:55 -0500664 }
Herb Derby526819d2018-03-09 12:51:12 -0500665 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500666 }
Brian Salomon52db9402017-11-07 14:58:55 -0500667 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000668
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500669 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500670}
671
Jim Van Verthf4c13162018-01-11 16:40:24 -0500672// TODO: merge with BmpAppendGlyph
Herb Derby86240592018-05-24 16:12:31 -0400673void GrTextContext::DfAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400674 GrGlyphCache* grGlyphCache, sk_sp<GrTextStrike>* strike,
675 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
676 GrColor color, SkGlyphCache* skGlyphCache,
677 SkScalar textRatio) {
Brian Salomon52db9402017-11-07 14:58:55 -0500678 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500679 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500680 }
681
682 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
683 skGlyph.getSubXFixed(),
684 skGlyph.getSubYFixed(),
685 GrGlyph::kDistance_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500686 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500687 if (!glyph) {
Jim Van Verthf4c13162018-01-11 16:40:24 -0500688 return;
Brian Salomon52db9402017-11-07 14:58:55 -0500689 }
690
691 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
692 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
693 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
694 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
695
Jim Van Verthf4c13162018-01-11 16:40:24 -0500696 dx *= textRatio;
697 dy *= textRatio;
698 width *= textRatio;
699 height *= textRatio;
700 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500701
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500702 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500703 textRatio, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500704}
705
joshualitt79dfb2b2015-05-11 08:58:08 -0700706///////////////////////////////////////////////////////////////////////////////////////////////////
707
Herb Derby26cbe512018-05-24 14:39:01 -0400708void GrTextContext::FallbackTextHelper::appendText(const SkGlyph& glyph, int count,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500709 const char* text, SkPoint glyphPos) {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400710 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
Khushalfa8ff092018-06-06 17:46:38 -0700711 if (SkScalarNearlyZero(maxDim)) return;
712
Jim Van Verthb515ae72018-05-23 16:44:55 -0400713 if (!fUseTransformedFallback) {
714 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
715 fUseTransformedFallback = true;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500716 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
Jim Van Verthc401bb92018-02-15 14:05:24 -0500717 }
718 }
719
720 fFallbackTxt.append(count, text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400721 if (fUseTransformedFallback) {
Jim Van Verth080a9282018-03-02 10:41:43 -0500722 // If there's a glyph in the font that's particularly large, it's possible
723 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
Jim Van Verth8b7284d2018-05-17 12:33:52 -0400724 // that glyph than make the others blurry, so we set a minimum size of half the
Jim Van Verth080a9282018-03-02 10:41:43 -0500725 // maximum text size to avoid this case.
Jim Van Verthb515ae72018-05-23 16:44:55 -0400726 SkScalar glyphTextSize = SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim),
Jim Van Verth080a9282018-03-02 10:41:43 -0500727 0.5f*fMaxTextSize);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400728 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500729 }
730 *fFallbackPos.append() = glyphPos;
731}
732
Herb Derby86240592018-05-24 16:12:31 -0400733void GrTextContext::FallbackTextHelper::drawText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400734 GrGlyphCache* glyphCache,
735 const SkSurfaceProps& props,
736 const GrTextUtils::Paint& paint,
737 SkScalerContextFlags scalerContextFlags) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500738 if (fFallbackTxt.count()) {
739 blob->initOverride(runIndex);
740 blob->setHasBitmap();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400741 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
Herb Derby526819d2018-03-09 12:51:12 -0500742 SkExclusiveStrikePtr cache;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500743 const SkPaint& skPaint = paint.skPaint();
744 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400745 SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500746 SkColor textColor = paint.filteredPremulColor();
Khushalfa8ff092018-06-06 17:46:38 -0700747
Jim Van Verthc401bb92018-02-15 14:05:24 -0500748 SkScalar textRatio = SK_Scalar1;
Khushalfa8ff092018-06-06 17:46:38 -0700749 SkPaint fallbackPaint(skPaint);
750 SkMatrix matrix = fViewMatrix;
751 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
752 cache = blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500753
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500754 sk_sp<GrTextStrike> currStrike;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500755 const char* text = fFallbackTxt.begin();
756 const char* stop = text + fFallbackTxt.count();
757 SkPoint* glyphPos = fFallbackPos.begin();
758 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400759 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400760 if (!fUseTransformedFallback) {
761 fViewMatrix.mapPoints(glyphPos, 1);
Jim Van Verth76e85162018-03-29 13:46:56 -0400762 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
763 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
764 }
Herb Derby26cbe512018-05-24 14:39:01 -0400765 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500766 glyphPos->fX, glyphPos->fY, textColor,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400767 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500768 glyphPos++;
769 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500770 }
771}
772
Khushalfa8ff092018-06-06 17:46:38 -0700773void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
774 SkMatrix* matrix) const {
775 if (!fUseTransformedFallback) return;
776
777 paint->setTextSize(fTransformedFallbackTextSize);
778 *textRatio = fTextSize / fTransformedFallbackTextSize;
779 *matrix = SkMatrix::I();
780}
781
Jim Van Verthc401bb92018-02-15 14:05:24 -0500782///////////////////////////////////////////////////////////////////////////////////////////////////
783
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400784void GrTextContext::FallbackGlyphRunHelper::appendText(
785 const SkGlyph& glyph, SkGlyphID glyphID, SkPoint glyphPos) {
786 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
787 if (SkScalarNearlyZero(maxDim)) return;
788
789 if (!fUseTransformedFallback) {
790 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
791 fUseTransformedFallback = true;
792 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
793 }
794 }
795
796 fFallbackTxt.push_back(glyphID);
797 if (fUseTransformedFallback) {
798 // If there's a glyph in the font that's particularly large, it's possible
799 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
800 // that glyph than make the others blurry, so we set a minimum size of half the
801 // maximum text size to avoid this case.
802 SkScalar glyphTextSize =
803 SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim), 0.5f*fMaxTextSize);
804 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
805 }
806 fFallbackPos.push_back(glyphPos);
807}
808
809void GrTextContext::FallbackGlyphRunHelper::drawText(
810 GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props,
811 const GrTextUtils::Paint& paint, SkScalerContextFlags scalerContextFlags) {
812 if (!fFallbackTxt.empty()) {
813 blob->initOverride(runIndex);
814 blob->setHasBitmap();
815 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
816 const SkPaint& skPaint = paint.skPaint();
817 SkColor textColor = paint.filteredPremulColor();
818
819 SkScalar textRatio = SK_Scalar1;
820 SkPaint fallbackPaint(skPaint);
821 SkMatrix matrix = fViewMatrix;
822 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
823 SkExclusiveStrikePtr cache =
824 blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
825
826 sk_sp<GrTextStrike> currStrike;
827 auto glyphPos = fFallbackPos.begin();
828 for (auto glyphID : fFallbackTxt) {
829 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
830 if (!fUseTransformedFallback) {
831 fViewMatrix.mapPoints(&*glyphPos, 1);
832 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
833 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
834 }
835 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
836 glyphPos->fX, glyphPos->fY, textColor,
837 cache.get(), textRatio, fUseTransformedFallback);
838 glyphPos++;
839 }
840 }
841}
842
843void GrTextContext::FallbackGlyphRunHelper::initializeForDraw(
844 SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const {
845 if (!fUseTransformedFallback) return;
846
847 paint->setTextSize(fTransformedFallbackTextSize);
848 *textRatio = fTextSize / fTransformedFallbackTextSize;
849 *matrix = SkMatrix::I();
850}
851
852///////////////////////////////////////////////////////////////////////////////////////////////////
853
854
Hal Canary6f6961e2017-01-31 13:50:44 -0500855#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700856
Brian Salomonf18b1d82017-10-27 11:30:49 -0400857#include "GrRenderTargetContext.h"
858
Herb Derby26cbe512018-05-24 14:39:01 -0400859std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
860 GrTextContext* textContext,
861 GrRenderTargetContext* rtc,
862 const SkPaint& skPaint,
863 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400864 const char* text,
865 int x,
866 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500867 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500868
869 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
870
871 size_t textLen = (int)strlen(text);
872
873 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
874
875 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
876 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000877
878 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400879 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -0400880 builder.drawText(skPaint, text, textLen, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400881 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000882
Herb Derby8a6348e2018-07-12 15:30:35 -0400883 auto glyphRunList = builder.useGlyphRunList();
Herb Derbyb935cf82018-07-26 16:54:18 -0400884 if (!glyphRunList.empty()) {
885 auto glyphRun = glyphRunList[0];
Herb Derby8a6348e2018-07-12 15:30:35 -0400886 // Use the text and textLen below, because we don't want to mess with the paint.
887 glyphRun.temporaryShuntToCallback(
Herb Derby59d997a2018-06-07 12:44:09 -0400888 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
889 blob = textContext->makeDrawPosTextBlob(
890 context->contextPriv().getTextBlobCache(), glyphCache,
891 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
Herb Derby8a6348e2018-07-12 15:30:35 -0400892 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps,
Herb Derbycddab252018-07-16 11:19:04 -0400893 text, textLen, pos, 2, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400894 });
Herb Derby8a6348e2018-07-12 15:30:35 -0400895 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500896
897 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500898 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500899}
900
Brian Salomon44acb5b2017-07-18 19:59:24 -0400901GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700902 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400903 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700904 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700905
906 if (context->uniqueID() != gContextID) {
907 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400908 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700909 }
910
Brian Osman11052242016-10-27 14:47:55 -0400911 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500912 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400913 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700914
joshualitt6c891102015-05-13 08:51:49 -0700915 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400916
917 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
918 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700919 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500920 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700921 skPaint.setLCDRenderText(random->nextBool());
922 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
923 skPaint.setSubpixelText(random->nextBool());
924
joshualitt79dfb2b2015-05-11 08:58:08 -0700925 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700926
joshualittb8d86492016-02-24 09:23:03 -0800927 // create some random x/y offsets, including negative offsets
928 static const int kMaxTrans = 1024;
929 int xPos = (random->nextU() % 2) * 2 - 1;
930 int yPos = (random->nextU() % 2) * 2 - 1;
931 int xInt = (random->nextU() % kMaxTrans) * xPos;
932 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700933
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500934 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
935 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700936}
937
938#endif