blob: f089a657fb22308cfd9139fcb7e910ce786ccb00 [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) {
Herb Derby049b5d92018-08-01 13:56:26 -0400139 AppendGlyph(
140 cacheBlob, runIndex, glyphCache, &currStrike,
141 glyph, GrGlyph::kDistance_MaskStyle,
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400142 glyphPos.fX, glyphPos.fY, runPaint.filteredPremulColor(),
Herb Derby049b5d92018-08-01 13:56:26 -0400143 cache.get(), textRatio, true);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400144 } else {
145 // can't append non-SDF glyph to SDF batch, send to fallback
146 fallbackTextHelper.appendText(glyph, glyphID, glyphPos);
147 }
148 }
149 }
150 }
151
152 fallbackTextHelper.drawText(
153 cacheBlob, runIndex, glyphCache, props, runPaint, scalerContextFlags);
154
155 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
156 // Ensure the blob is set for bitmaptext
157 cacheBlob->setHasBitmap();
158
159 // setup our std runPaint, in hopes of getting hits in the cache
160 SkPaint pathPaint(runPaint);
161 SkScalar matrixScale = pathPaint.setupForAsPaths();
162
163 FallbackGlyphRunHelper fallbackTextHelper(
164 viewMatrix, runPaint, glyphCache->getGlyphSizeLimit(), matrixScale);
165
166 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
167 pathPaint.setStyle(SkPaint::kFill_Style);
168 pathPaint.setPathEffect(nullptr);
169
170 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
171 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
172
Herb Derby0ab5ce12018-07-30 10:10:40 -0400173 auto drawOnePath =
174 [&fallbackTextHelper, matrixScale, runIndex, cacheBlob]
175 (const SkPath* path, const SkGlyph& glyph, SkPoint position) {
176 if (glyph.fMaskFormat == SkMask::kARGB32_Format) {
177 fallbackTextHelper.appendText(glyph, glyph.getGlyphID(), position);
178 } else {
179 if (path != nullptr) {
180 cacheBlob->appendPathGlyph(
181 runIndex, *path, position.fX, position.fY, matrixScale, false);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400182 }
183 }
Herb Derby0ab5ce12018-07-30 10:10:40 -0400184 };
185
186 glyphDrawer->drawUsingPaths(glyphRun, origin, cache.get(), drawOnePath);
Herb Derbyf9dfbc32018-07-28 16:16:56 -0400187
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) {
Herb Derby049b5d92018-08-01 13:56:26 -0400201 AppendGlyph(cacheBlob, runIndex, glyphCache, &currStrike,
202 glyph, GrGlyph::kCoverage_MaskStyle,
Herb Derby74c6ed32018-07-28 18:07:54 -0400203 SkScalarFloorToScalar(position.fX),
204 SkScalarFloorToScalar(position.fY),
205 runPaint.filteredPremulColor(), cache, SK_Scalar1, false);
206 };
207
208 glyphDrawer->drawUsingMasks(cache.get(), glyphRun, origin, viewMatrix, drawOneGlyph);
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 Derby74c6ed32018-07-28 18:07:54 -0400273 scalerContextFlags, viewMatrix, props, glyphRunList,
274 target->glyphDrawer());
joshualittb7133be2015-04-08 09:08:31 -0700275 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500276 textBlobCache->makeMRU(cacheBlob.get());
joshualitt2f2ee832016-02-10 08:52:24 -0800277
278 if (CACHE_SANITY_CHECK) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400279 int glyphCount = glyphRunList.totalGlyphCount();
280 int runCount = glyphRunList.runCount();
Herb Derby86240592018-05-24 16:12:31 -0400281 sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
joshualitt92303772016-02-10 11:55:52 -0800282 sanityBlob->setupKey(key, blurRec, skPaint);
Herb Derbycddab252018-07-16 11:19:04 -0400283 this->regenerateGlyphRunList(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400284 sanityBlob.get(), glyphCache, *context->contextPriv().caps()->shaderCaps(),
Herb Derby74c6ed32018-07-28 18:07:54 -0400285 paint, scalerContextFlags, viewMatrix, props, glyphRunList,
286 target->glyphDrawer());
Herb Derby86240592018-05-24 16:12:31 -0400287 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700288 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700289 }
290 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700291 if (canCache) {
Herb Derbycddab252018-07-16 11:19:04 -0400292 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
joshualitt2a0e9f32015-04-13 06:12:21 -0700293 } else {
Herb Derbycddab252018-07-16 11:19:04 -0400294 cacheBlob = textBlobCache->makeBlob(glyphRunList);
joshualitt2a0e9f32015-04-13 06:12:21 -0700295 }
Herb Derbycddab252018-07-16 11:19:04 -0400296 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400297 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derby74c6ed32018-07-28 18:07:54 -0400298 scalerContextFlags, viewMatrix, props, glyphRunList,
299 target->glyphDrawer());
joshualitt1d89e8d2015-04-01 12:40:54 -0700300 }
301
Robert Phillips5a66efb2018-03-07 15:13:18 -0500302 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400303 clip, viewMatrix, clipBounds, origin.x(), origin.y());
joshualitt1d89e8d2015-04-01 12:40:54 -0700304}
305
Herb Derby86240592018-05-24 16:12:31 -0400306inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400307GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
308 GrGlyphCache* glyphCache,
309 const GrShaderCaps& shaderCaps,
310 const GrTextUtils::Paint& paint,
311 SkScalerContextFlags scalerContextFlags,
312 const SkMatrix& viewMatrix,
313 const SkSurfaceProps& props,
314 const char text[], size_t byteLength,
315 const SkScalar pos[], int scalarsPerPosition, const
316 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500317 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400318 if (!glyphCount) {
319 return nullptr;
320 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700321
Herb Derby86240592018-05-24 16:12:31 -0400322 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800323 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500324 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700325
Khushal3e7548c2018-05-23 15:45:01 -0700326 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
327 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500328 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500329 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700330 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500331 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
332 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700333 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700334 return blob;
335}
336
Herb Derby26cbe512018-05-24 14:39:01 -0400337void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
338 const GrClip& clip, const SkPaint& skPaint,
339 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
340 const char text[], size_t byteLength, const SkScalar pos[],
341 int scalarsPerPosition, const SkPoint& offset,
342 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400343 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Khushalc421ca12018-06-26 14:38:34 -0700344 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800345 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500346 }
347
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500348 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500349 auto textBlobCache = context->contextPriv().getTextBlobCache();
350
Herb Derby86240592018-05-24 16:12:31 -0400351 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400352 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500353 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
354 byteLength, pos, scalarsPerPosition, offset));
355 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500356 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500357 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800358 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700359}
360
Herb Derby86240592018-05-24 16:12:31 -0400361void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400362 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
363 const GrTextUtils::Paint& paint,
364 SkScalerContextFlags scalerContextFlags,
365 const SkMatrix& viewMatrix,
366 const char text[], size_t byteLength, const SkScalar pos[],
367 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500368 SkASSERT(byteLength == 0 || text != nullptr);
369 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
370
371 // nothing to draw
372 if (text == nullptr || byteLength == 0) {
373 return;
374 }
375
376 // Ensure the blob is set for bitmaptext
377 blob->setHasBitmap();
378
Jim Van Verth54d9c882018-02-08 16:14:48 -0500379 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500380 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500381 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500382 return;
383 }
384
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000385 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500386 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500387 SkFindAndPlaceGlyph::ProcessPosText(
388 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400389 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500390 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
391 position += rounding;
Herb Derby049b5d92018-08-01 13:56:26 -0400392 AppendGlyph(blob, runIndex, glyphCache, &currStrike,
393 glyph, GrGlyph::kCoverage_MaskStyle,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500394 SkScalarFloorToScalar(position.fX),
395 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400396 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500397 });
Brian Salomon52db9402017-11-07 14:58:55 -0500398}
399
Herb Derby86240592018-05-24 16:12:31 -0400400void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400401 GrGlyphCache* glyphCache,
402 const SkSurfaceProps& props,
403 const GrTextUtils::Paint& origPaint,
404 SkScalerContextFlags scalerContextFlags,
405 const SkMatrix& viewMatrix,
406 const char text[], size_t byteLength,
407 const SkScalar pos[], int scalarsPerPosition,
408 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500409 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
410
411 // nothing to draw
412 if (text == nullptr || byteLength == 0) {
413 return;
414 }
415
416 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500417 SkPaint pathPaint(origPaint);
418 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700419 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
420 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500421
422 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500423 pathPaint.setStyle(SkPaint::kFill_Style);
424 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500425
Jim Van Verthc401bb92018-02-15 14:05:24 -0500426 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500427 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400428 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400429 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500430
431 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500432 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500433 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
434
435 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400436 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500437 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500438 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400439 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500440 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
441 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
442 } else {
443 const SkPath* path = cache->findPath(glyph);
444 if (path) {
445 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
446 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500447 }
448 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500449 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500450 pos += scalarsPerPosition;
451 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500452
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500453 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500454}
455
Herb Derby049b5d92018-08-01 13:56:26 -0400456void GrTextContext::AppendGlyph(GrTextBlob* blob, int runIndex,
457 GrGlyphCache* grGlyphCache,
458 sk_sp<GrTextStrike>* strike,
459 const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
460 SkScalar sx, SkScalar sy,
461 GrColor color, SkGlyphCache* skGlyphCache,
462 SkScalar textRatio, bool needsTransform) {
Brian Salomon52db9402017-11-07 14:58:55 -0500463 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500464 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500465 }
466
467 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
468 skGlyph.getSubXFixed(),
469 skGlyph.getSubYFixed(),
Herb Derby049b5d92018-08-01 13:56:26 -0400470 maskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500471 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500472 if (!glyph) {
473 return;
474 }
475
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500476 SkASSERT(skGlyph.fWidth == glyph->width());
477 SkASSERT(skGlyph.fHeight == glyph->height());
478
Jim Van Verthf4c13162018-01-11 16:40:24 -0500479 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft);
480 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop);
481 SkScalar width = SkIntToScalar(glyph->fBounds.width());
482 SkScalar height = SkIntToScalar(glyph->fBounds.height());
Brian Salomon52db9402017-11-07 14:58:55 -0500483
Herb Derby049b5d92018-08-01 13:56:26 -0400484 if (maskStyle == GrGlyph::kDistance_MaskStyle) {
485 dx += SK_DistanceFieldInset;
486 dy += SK_DistanceFieldInset;
487 width -= 2 * SK_DistanceFieldInset;
488 height -= 2 * SK_DistanceFieldInset;
489 }
490
Jim Van Verthf4c13162018-01-11 16:40:24 -0500491 dx *= textRatio;
492 dy *= textRatio;
493 width *= textRatio;
494 height *= textRatio;
Brian Salomon52db9402017-11-07 14:58:55 -0500495
Jim Van Verthf4c13162018-01-11 16:40:24 -0500496 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500497
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500498 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400499 textRatio, !needsTransform);
Brian Salomon52db9402017-11-07 14:58:55 -0500500}
501
Herb Derby26cbe512018-05-24 14:39:01 -0400502void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700503 if (options->fMaxDistanceFieldFontSize < 0.f) {
504 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
505 }
506 if (options->fMinDistanceFieldFontSize < 0.f) {
507 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
508 }
509}
510
Herb Derby26cbe512018-05-24 14:39:01 -0400511bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
512 const SkSurfaceProps& props,
513 bool contextSupportsDistanceFieldText,
514 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500515 if (!viewMatrix.hasPerspective()) {
516 SkScalar maxScale = viewMatrix.getMaxScale();
517 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
518 // Hinted text looks far better at small resolutions
519 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700520 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
521 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500522 return false;
523 }
524
525 bool useDFT = props.isUseDeviceIndependentFonts();
526#if SK_FORCE_DISTANCE_FIELD_TEXT
527 useDFT = true;
528#endif
529
530 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
531 return false;
532 }
533 }
534
Mike Reed8ad91a92018-01-19 19:09:32 -0500535 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700536 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500537 return false;
538 }
539
540 // TODO: add some stroking support
541 if (skPaint.getStyle() != SkPaint::kFill_Style) {
542 return false;
543 }
544
545 return true;
546}
547
Herb Derby86240592018-05-24 16:12:31 -0400548void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400549 SkPaint* skPaint,
550 const SkMatrix& viewMatrix,
551 const Options& options,
552 SkScalar* textRatio,
553 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500554 SkScalar textSize = skPaint->getTextSize();
555 SkScalar scaledTextSize = textSize;
556
557 if (viewMatrix.hasPerspective()) {
558 // for perspective, we simply force to the medium size
559 // TODO: compute a size based on approximate screen area
560 scaledTextSize = kMediumDFFontLimit;
561 } else {
562 SkScalar maxScale = viewMatrix.getMaxScale();
563 // if we have non-unity scale, we need to choose our base text size
564 // based on the SkPaint's text size multiplied by the max scale factor
565 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
566 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
567 scaledTextSize *= maxScale;
568 }
569 }
570
571 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
572 // and ceiling. A scale outside of this range would require regenerating the distance fields
573 SkScalar dfMaskScaleFloor;
574 SkScalar dfMaskScaleCeil;
575 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700576 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500577 dfMaskScaleCeil = kSmallDFFontLimit;
578 *textRatio = textSize / kSmallDFFontSize;
579 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
580 } else if (scaledTextSize <= kMediumDFFontLimit) {
581 dfMaskScaleFloor = kSmallDFFontLimit;
582 dfMaskScaleCeil = kMediumDFFontLimit;
583 *textRatio = textSize / kMediumDFFontSize;
584 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
585 } else {
586 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700587 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500588 *textRatio = textSize / kLargeDFFontSize;
589 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
590 }
591
592 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
593 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
594 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
595 // tolerate before we'd have to move to a large mip size. When we actually test these values
596 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
597 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
598 // level)
599 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700600 if (blob) {
601 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
602 dfMaskScaleCeil / scaledTextSize);
603 }
Brian Salomon52db9402017-11-07 14:58:55 -0500604
605 skPaint->setAntiAlias(true);
606 skPaint->setLCDRenderText(false);
607 skPaint->setAutohinted(false);
608 skPaint->setHinting(SkPaint::kNormal_Hinting);
609 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400610
611 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700612
613 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
614 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
615 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500616}
617
Herb Derby86240592018-05-24 16:12:31 -0400618void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400619 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
620 const GrTextUtils::Paint& paint,
621 SkScalerContextFlags scalerContextFlags,
622 const SkMatrix& viewMatrix, const char text[],
623 size_t byteLength, const SkScalar pos[],
624 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500625 SkASSERT(byteLength == 0 || text != nullptr);
626 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
627
628 // nothing to draw
629 if (text == nullptr || byteLength == 0) {
630 return;
631 }
632
Khushal3e7548c2018-05-23 15:45:01 -0700633 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500634
Brian Salomon52db9402017-11-07 14:58:55 -0500635 // Setup distance field paint and text ratio
636 SkScalar textRatio;
637 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700638 SkScalerContextFlags flags;
639 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500640 blob->setHasDistanceField();
641 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500642 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500643
Khushalfa8ff092018-06-06 17:46:38 -0700644 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
645 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500646
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500647 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500648
Herb Derby526819d2018-03-09 12:51:12 -0500649 {
Khushal3e7548c2018-05-23 15:45:01 -0700650 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500651 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400652 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500653
Herb Derby526819d2018-03-09 12:51:12 -0500654 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500655
Herb Derby526819d2018-03-09 12:51:12 -0500656 while (text < stop) {
657 const char* lastText = text;
658 // the last 2 parameters are ignored
Hal Canary4014ba62018-07-24 11:33:21 -0400659 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Brian Salomon52db9402017-11-07 14:58:55 -0500660
Herb Derby526819d2018-03-09 12:51:12 -0500661 if (glyph.fWidth) {
662 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400663 glyphPos.fX += pos[0];
664 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500665
Jim Van Verthd401da62018-05-03 10:40:30 -0400666 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby049b5d92018-08-01 13:56:26 -0400667 AppendGlyph(blob, runIndex, glyphCache, &currStrike,
668 glyph, GrGlyph::kDistance_MaskStyle,
669 glyphPos.fX, glyphPos.fY, paint.filteredPremulColor(),
670 cache.get(), textRatio, true);
Herb Derby526819d2018-03-09 12:51:12 -0500671 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400672 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500673 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
674 glyphPos);
675 }
Brian Salomon52db9402017-11-07 14:58:55 -0500676 }
Herb Derby526819d2018-03-09 12:51:12 -0500677 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500678 }
Brian Salomon52db9402017-11-07 14:58:55 -0500679 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000680
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500681 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500682}
683
joshualitt79dfb2b2015-05-11 08:58:08 -0700684///////////////////////////////////////////////////////////////////////////////////////////////////
685
Herb Derby26cbe512018-05-24 14:39:01 -0400686void GrTextContext::FallbackTextHelper::appendText(const SkGlyph& glyph, int count,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500687 const char* text, SkPoint glyphPos) {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400688 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
Khushalfa8ff092018-06-06 17:46:38 -0700689 if (SkScalarNearlyZero(maxDim)) return;
690
Jim Van Verthb515ae72018-05-23 16:44:55 -0400691 if (!fUseTransformedFallback) {
692 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
693 fUseTransformedFallback = true;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500694 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
Jim Van Verthc401bb92018-02-15 14:05:24 -0500695 }
696 }
697
698 fFallbackTxt.append(count, text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400699 if (fUseTransformedFallback) {
Jim Van Verth080a9282018-03-02 10:41:43 -0500700 // If there's a glyph in the font that's particularly large, it's possible
701 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
Jim Van Verth8b7284d2018-05-17 12:33:52 -0400702 // that glyph than make the others blurry, so we set a minimum size of half the
Jim Van Verth080a9282018-03-02 10:41:43 -0500703 // maximum text size to avoid this case.
Jim Van Verthb515ae72018-05-23 16:44:55 -0400704 SkScalar glyphTextSize = SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim),
Jim Van Verth080a9282018-03-02 10:41:43 -0500705 0.5f*fMaxTextSize);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400706 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500707 }
708 *fFallbackPos.append() = glyphPos;
709}
710
Herb Derby86240592018-05-24 16:12:31 -0400711void GrTextContext::FallbackTextHelper::drawText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400712 GrGlyphCache* glyphCache,
713 const SkSurfaceProps& props,
714 const GrTextUtils::Paint& paint,
715 SkScalerContextFlags scalerContextFlags) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500716 if (fFallbackTxt.count()) {
717 blob->initOverride(runIndex);
718 blob->setHasBitmap();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400719 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
Herb Derby526819d2018-03-09 12:51:12 -0500720 SkExclusiveStrikePtr cache;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500721 const SkPaint& skPaint = paint.skPaint();
722 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400723 SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500724 SkColor textColor = paint.filteredPremulColor();
Khushalfa8ff092018-06-06 17:46:38 -0700725
Jim Van Verthc401bb92018-02-15 14:05:24 -0500726 SkScalar textRatio = SK_Scalar1;
Khushalfa8ff092018-06-06 17:46:38 -0700727 SkPaint fallbackPaint(skPaint);
728 SkMatrix matrix = fViewMatrix;
729 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
730 cache = blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500731
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500732 sk_sp<GrTextStrike> currStrike;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500733 const char* text = fFallbackTxt.begin();
734 const char* stop = text + fFallbackTxt.count();
735 SkPoint* glyphPos = fFallbackPos.begin();
736 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400737 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400738 if (!fUseTransformedFallback) {
739 fViewMatrix.mapPoints(glyphPos, 1);
Jim Van Verth76e85162018-03-29 13:46:56 -0400740 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
741 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
742 }
Herb Derby049b5d92018-08-01 13:56:26 -0400743 GrTextContext::AppendGlyph(blob, runIndex, glyphCache, &currStrike,
744 glyph, GrGlyph::kCoverage_MaskStyle,
745 glyphPos->fX, glyphPos->fY, textColor,
746 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500747 glyphPos++;
748 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500749 }
750}
751
Khushalfa8ff092018-06-06 17:46:38 -0700752void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
753 SkMatrix* matrix) const {
754 if (!fUseTransformedFallback) return;
755
756 paint->setTextSize(fTransformedFallbackTextSize);
757 *textRatio = fTextSize / fTransformedFallbackTextSize;
758 *matrix = SkMatrix::I();
759}
760
Jim Van Verthc401bb92018-02-15 14:05:24 -0500761///////////////////////////////////////////////////////////////////////////////////////////////////
762
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400763void GrTextContext::FallbackGlyphRunHelper::appendText(
764 const SkGlyph& glyph, SkGlyphID glyphID, SkPoint glyphPos) {
765 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
766 if (SkScalarNearlyZero(maxDim)) return;
767
768 if (!fUseTransformedFallback) {
769 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
770 fUseTransformedFallback = true;
771 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
772 }
773 }
774
775 fFallbackTxt.push_back(glyphID);
776 if (fUseTransformedFallback) {
777 // If there's a glyph in the font that's particularly large, it's possible
778 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
779 // that glyph than make the others blurry, so we set a minimum size of half the
780 // maximum text size to avoid this case.
781 SkScalar glyphTextSize =
782 SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim), 0.5f*fMaxTextSize);
783 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
784 }
785 fFallbackPos.push_back(glyphPos);
786}
787
788void GrTextContext::FallbackGlyphRunHelper::drawText(
789 GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props,
790 const GrTextUtils::Paint& paint, SkScalerContextFlags scalerContextFlags) {
791 if (!fFallbackTxt.empty()) {
792 blob->initOverride(runIndex);
793 blob->setHasBitmap();
794 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
795 const SkPaint& skPaint = paint.skPaint();
796 SkColor textColor = paint.filteredPremulColor();
797
798 SkScalar textRatio = SK_Scalar1;
799 SkPaint fallbackPaint(skPaint);
800 SkMatrix matrix = fViewMatrix;
801 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
802 SkExclusiveStrikePtr cache =
803 blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
804
805 sk_sp<GrTextStrike> currStrike;
806 auto glyphPos = fFallbackPos.begin();
807 for (auto glyphID : fFallbackTxt) {
808 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
809 if (!fUseTransformedFallback) {
810 fViewMatrix.mapPoints(&*glyphPos, 1);
811 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
812 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
813 }
Herb Derby049b5d92018-08-01 13:56:26 -0400814 GrTextContext::AppendGlyph(blob, runIndex, glyphCache, &currStrike,
815 glyph, GrGlyph::kCoverage_MaskStyle,
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400816 glyphPos->fX, glyphPos->fY, textColor,
817 cache.get(), textRatio, fUseTransformedFallback);
818 glyphPos++;
819 }
820 }
821}
822
823void GrTextContext::FallbackGlyphRunHelper::initializeForDraw(
824 SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const {
825 if (!fUseTransformedFallback) return;
826
827 paint->setTextSize(fTransformedFallbackTextSize);
828 *textRatio = fTextSize / fTransformedFallbackTextSize;
829 *matrix = SkMatrix::I();
830}
831
832///////////////////////////////////////////////////////////////////////////////////////////////////
833
834
Hal Canary6f6961e2017-01-31 13:50:44 -0500835#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700836
Brian Salomonf18b1d82017-10-27 11:30:49 -0400837#include "GrRenderTargetContext.h"
838
Herb Derby26cbe512018-05-24 14:39:01 -0400839std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
840 GrTextContext* textContext,
841 GrRenderTargetContext* rtc,
842 const SkPaint& skPaint,
843 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400844 const char* text,
845 int x,
846 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500847 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500848
849 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
850
851 size_t textLen = (int)strlen(text);
852
853 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
854
855 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
856 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000857
858 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400859 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -0400860 builder.drawText(skPaint, text, textLen, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400861 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000862
Herb Derby8a6348e2018-07-12 15:30:35 -0400863 auto glyphRunList = builder.useGlyphRunList();
Herb Derbyb935cf82018-07-26 16:54:18 -0400864 if (!glyphRunList.empty()) {
865 auto glyphRun = glyphRunList[0];
Herb Derby8a6348e2018-07-12 15:30:35 -0400866 // Use the text and textLen below, because we don't want to mess with the paint.
867 glyphRun.temporaryShuntToCallback(
Herb Derby59d997a2018-06-07 12:44:09 -0400868 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
869 blob = textContext->makeDrawPosTextBlob(
870 context->contextPriv().getTextBlobCache(), glyphCache,
871 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
Herb Derby8a6348e2018-07-12 15:30:35 -0400872 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps,
Herb Derbycddab252018-07-16 11:19:04 -0400873 text, textLen, pos, 2, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400874 });
Herb Derby8a6348e2018-07-12 15:30:35 -0400875 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500876
877 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500878 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500879}
880
Brian Salomon44acb5b2017-07-18 19:59:24 -0400881GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700882 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400883 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700884 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700885
886 if (context->uniqueID() != gContextID) {
887 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400888 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700889 }
890
Brian Osman11052242016-10-27 14:47:55 -0400891 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500892 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400893 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700894
joshualitt6c891102015-05-13 08:51:49 -0700895 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400896
897 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
898 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700899 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500900 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700901 skPaint.setLCDRenderText(random->nextBool());
902 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
903 skPaint.setSubpixelText(random->nextBool());
904
joshualitt79dfb2b2015-05-11 08:58:08 -0700905 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700906
joshualittb8d86492016-02-24 09:23:03 -0800907 // create some random x/y offsets, including negative offsets
908 static const int kMaxTrans = 1024;
909 int xPos = (random->nextU() % 2) * 2 - 1;
910 int yPos = (random->nextU() % 2) * 2 - 1;
911 int xInt = (random->nextU() % kMaxTrans) * xPos;
912 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700913
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500914 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
915 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700916}
917
918#endif