blob: 4631991985c255297312a8564e9847e03811e45a [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 Derbycddab252018-07-16 11:19:04 -040087void GrTextContext::drawGlyphRunList(
88 GrContext* context, GrTextUtils::Target* target, const GrClip& clip,
Herb Derbyb935cf82018-07-26 16:54:18 -040089 const SkMatrix& viewMatrix, const SkSurfaceProps& props, const SkGlyphRunList& glyphRunList,
Herb Derbycddab252018-07-16 11:19:04 -040090 const SkIRect& clipBounds) {
Herb Derbyb935cf82018-07-26 16:54:18 -040091 SkPoint origin = glyphRunList.origin();
joshualitt9e36c1a2015-04-14 12:17:27 -070092
Herb Derbycddab252018-07-16 11:19:04 -040093 // Get the first paint to use as the key paint.
Herb Derbyb935cf82018-07-26 16:54:18 -040094 const SkPaint& skPaint = glyphRunList.paint();
Herb Derbycddab252018-07-16 11:19:04 -040095
joshualitt9b8e79e2015-04-24 09:57:12 -070096 // If we have been abandoned, then don't draw
Khushalc421ca12018-06-26 14:38:34 -070097 if (context->abandoned()) {
robertphillipsea461502015-05-26 11:38:03 -070098 return;
99 }
100
Mike Reed80747ef2018-01-23 15:29:32 -0500101 SkMaskFilterBase::BlurRec blurRec;
joshualitt53b5f442015-04-13 06:33:59 -0700102 // It might be worth caching these things, but its not clear at this time
103 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
104 const SkMaskFilter* mf = skPaint.getMaskFilter();
Herb Derbyb935cf82018-07-26 16:54:18 -0400105 bool canCache = glyphRunList.canCache() && !(skPaint.getPathEffect() ||
Herb Derbycddab252018-07-16 11:19:04 -0400106 (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derbyd8327a82018-01-22 14:39:27 -0500107 SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
joshualitt2a0e9f32015-04-13 06:12:21 -0700108
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500109 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500110 GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache();
111
Herb Derbycddab252018-07-16 11:19:04 -0400112 sk_sp<GrTextBlob> cacheBlob;
113 GrTextBlob::Key key;
joshualitt2a0e9f32015-04-13 06:12:21 -0700114 if (canCache) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400115 bool hasLCD = glyphRunList.anyRunsLCD();
joshualitte4cee1f2015-05-11 13:04:28 -0700116
117 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
joshualitt2c89bc12016-02-11 05:42:30 -0800118 SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() :
Herb Derbycddab252018-07-16 11:19:04 -0400119 kUnknown_SkPixelGeometry;
joshualitte4cee1f2015-05-11 13:04:28 -0700120
joshualitt9e36c1a2015-04-14 12:17:27 -0700121 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
122 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
123 // ensure we always match the same key
124 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
Herb Derbycddab252018-07-16 11:19:04 -0400125 ComputeCanonicalColor(skPaint, hasLCD);
joshualitt9e36c1a2015-04-14 12:17:27 -0700126
joshualitte4cee1f2015-05-11 13:04:28 -0700127 key.fPixelGeometry = pixelGeometry;
Herb Derbyb935cf82018-07-26 16:54:18 -0400128 key.fUniqueID = glyphRunList.uniqueID();
joshualitt53b5f442015-04-13 06:33:59 -0700129 key.fStyle = skPaint.getStyle();
130 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700131 key.fCanonicalColor = canonicalColor;
brianosman8d7ffce2016-04-21 08:29:06 -0700132 key.fScalerContextFlags = scalerContextFlags;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500133 cacheBlob = textBlobCache->find(key);
joshualitt2a0e9f32015-04-13 06:12:21 -0700134 }
135
Brian Salomonf18b1d82017-10-27 11:30:49 -0400136 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
joshualittb7133be2015-04-08 09:08:31 -0700137 if (cacheBlob) {
Herb Derbycddab252018-07-16 11:19:04 -0400138 if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, origin.x(), origin.y())) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700139 // We have to remake the blob because changes may invalidate our masks.
140 // TODO we could probably get away reuse most of the time if the pointer is unique,
141 // but we'd have to clear the subrun information
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500142 textBlobCache->remove(cacheBlob.get());
Herb Derbycddab252018-07-16 11:19:04 -0400143 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
144 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400145 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400146 scalerContextFlags, viewMatrix, props, glyphRunList);
joshualittb7133be2015-04-08 09:08:31 -0700147 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500148 textBlobCache->makeMRU(cacheBlob.get());
joshualitt2f2ee832016-02-10 08:52:24 -0800149
150 if (CACHE_SANITY_CHECK) {
Herb Derbyb935cf82018-07-26 16:54:18 -0400151 int glyphCount = glyphRunList.totalGlyphCount();
152 int runCount = glyphRunList.runCount();
Herb Derby86240592018-05-24 16:12:31 -0400153 sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
joshualitt92303772016-02-10 11:55:52 -0800154 sanityBlob->setupKey(key, blurRec, skPaint);
Herb Derbycddab252018-07-16 11:19:04 -0400155 this->regenerateGlyphRunList(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400156 sanityBlob.get(), glyphCache, *context->contextPriv().caps()->shaderCaps(),
Herb Derbycddab252018-07-16 11:19:04 -0400157 paint, scalerContextFlags, viewMatrix, props, glyphRunList);
Herb Derby86240592018-05-24 16:12:31 -0400158 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700159 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700160 }
161 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700162 if (canCache) {
Herb Derbycddab252018-07-16 11:19:04 -0400163 cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, skPaint);
joshualitt2a0e9f32015-04-13 06:12:21 -0700164 } else {
Herb Derbycddab252018-07-16 11:19:04 -0400165 cacheBlob = textBlobCache->makeBlob(glyphRunList);
joshualitt2a0e9f32015-04-13 06:12:21 -0700166 }
Herb Derbycddab252018-07-16 11:19:04 -0400167 this->regenerateGlyphRunList(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400168 *context->contextPriv().caps()->shaderCaps(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400169 scalerContextFlags, viewMatrix, props, glyphRunList);
joshualitt1d89e8d2015-04-01 12:40:54 -0700170 }
171
Robert Phillips5a66efb2018-03-07 15:13:18 -0500172 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Herb Derbycddab252018-07-16 11:19:04 -0400173 clip, viewMatrix, clipBounds, origin.x(), origin.y());
joshualitt1d89e8d2015-04-01 12:40:54 -0700174}
175
Herb Derbycddab252018-07-16 11:19:04 -0400176void GrTextContext::regenerateGlyphRunList(GrTextBlob* cacheBlob,
Herb Derby26cbe512018-05-24 14:39:01 -0400177 GrGlyphCache* glyphCache,
178 const GrShaderCaps& shaderCaps,
179 const GrTextUtils::Paint& paint,
180 SkScalerContextFlags scalerContextFlags,
181 const SkMatrix& viewMatrix,
Herb Derbycddab252018-07-16 11:19:04 -0400182 const SkSurfaceProps& props,
Herb Derbyb935cf82018-07-26 16:54:18 -0400183 const SkGlyphRunList& glyphRunList) const {
184 SkPoint origin = glyphRunList.origin();
Herb Derbycddab252018-07-16 11:19:04 -0400185 cacheBlob->initReusableBlob(paint.luminanceColor(), viewMatrix, origin.x(), origin.y());
joshualitt1d89e8d2015-04-01 12:40:54 -0700186
187 // Regenerate textblob
Herb Derbycddab252018-07-16 11:19:04 -0400188 SkGlyphRunListIterator it(glyphRunList);
Ben Wagner2c312c42018-06-27 14:46:46 -0400189 GrTextUtils::RunPaint runPaint(&paint);
joshualitt1d89e8d2015-04-01 12:40:54 -0700190 for (int run = 0; !it.done(); it.next(), run++) {
191 int glyphCount = it.glyphCount();
192 size_t textLen = glyphCount * sizeof(uint16_t);
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500193 cacheBlob->push_back_run(run);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500194 if (!runPaint.modifyForRun([it](SkPaint* p) { it.applyFontToPaint(p); })) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700195 continue;
196 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500197 cacheBlob->setRunPaintFlags(run, runPaint.skPaint().getFlags());
198
Khushal3e7548c2018-05-23 15:45:01 -0700199 if (CanDrawAsDistanceFields(runPaint, viewMatrix, props,
200 shaderCaps.supportsDistanceFieldText(), fOptions)) {
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400201 this->drawDFGlyphRun(cacheBlob, run, glyphCache, props, runPaint,
202 scalerContextFlags, viewMatrix, it.glyphRun(), origin);
joshualittfcfb9fc2015-04-21 07:35:10 -0700203 } else {
Herb Derbycddab252018-07-16 11:19:04 -0400204 DrawBmpPosText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
205 viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 2,
206 origin);
joshualitt1d89e8d2015-04-01 12:40:54 -0700207 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700208 }
209}
210
Herb Derby86240592018-05-24 16:12:31 -0400211inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400212GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
213 GrGlyphCache* glyphCache,
214 const GrShaderCaps& shaderCaps,
215 const GrTextUtils::Paint& paint,
216 SkScalerContextFlags scalerContextFlags,
217 const SkMatrix& viewMatrix,
218 const SkSurfaceProps& props,
219 const char text[], size_t byteLength,
220 const SkScalar pos[], int scalarsPerPosition, const
221 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500222 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400223 if (!glyphCount) {
224 return nullptr;
225 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700226
Herb Derby86240592018-05-24 16:12:31 -0400227 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800228 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500229 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700230
Khushal3e7548c2018-05-23 15:45:01 -0700231 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
232 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500233 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500234 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700235 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500236 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
237 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700238 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700239 return blob;
240}
241
Herb Derby26cbe512018-05-24 14:39:01 -0400242void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
243 const GrClip& clip, const SkPaint& skPaint,
244 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
245 const char text[], size_t byteLength, const SkScalar pos[],
246 int scalarsPerPosition, const SkPoint& offset,
247 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400248 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Khushalc421ca12018-06-26 14:38:34 -0700249 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800250 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500251 }
252
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500253 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500254 auto textBlobCache = context->contextPriv().getTextBlobCache();
255
Herb Derby86240592018-05-24 16:12:31 -0400256 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400257 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500258 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
259 byteLength, pos, scalarsPerPosition, offset));
260 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500261 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500262 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800263 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700264}
265
Herb Derby86240592018-05-24 16:12:31 -0400266void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400267 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
268 const GrTextUtils::Paint& paint,
269 SkScalerContextFlags scalerContextFlags,
270 const SkMatrix& viewMatrix,
271 const char text[], size_t byteLength, const SkScalar pos[],
272 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500273 SkASSERT(byteLength == 0 || text != nullptr);
274 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
275
276 // nothing to draw
277 if (text == nullptr || byteLength == 0) {
278 return;
279 }
280
281 // Ensure the blob is set for bitmaptext
282 blob->setHasBitmap();
283
Jim Van Verth54d9c882018-02-08 16:14:48 -0500284 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500285 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500286 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500287 return;
288 }
289
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000290 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500291 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500292 SkFindAndPlaceGlyph::ProcessPosText(
293 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400294 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500295 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
296 position += rounding;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500297 BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500298 SkScalarFloorToScalar(position.fX),
299 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400300 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500301 });
Brian Salomon52db9402017-11-07 14:58:55 -0500302}
303
Herb Derby86240592018-05-24 16:12:31 -0400304void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400305 GrGlyphCache* glyphCache,
306 const SkSurfaceProps& props,
307 const GrTextUtils::Paint& origPaint,
308 SkScalerContextFlags scalerContextFlags,
309 const SkMatrix& viewMatrix,
310 const char text[], size_t byteLength,
311 const SkScalar pos[], int scalarsPerPosition,
312 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500313 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
314
315 // nothing to draw
316 if (text == nullptr || byteLength == 0) {
317 return;
318 }
319
320 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500321 SkPaint pathPaint(origPaint);
322 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700323 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
324 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500325
326 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500327 pathPaint.setStyle(SkPaint::kFill_Style);
328 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500329
Jim Van Verthc401bb92018-02-15 14:05:24 -0500330 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500331 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400332 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400333 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500334
335 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500336 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500337 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
338
339 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400340 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500341 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500342 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400343 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500344 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
345 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
346 } else {
347 const SkPath* path = cache->findPath(glyph);
348 if (path) {
349 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
350 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500351 }
352 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500353 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500354 pos += scalarsPerPosition;
355 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500356
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500357 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500358}
359
Herb Derby86240592018-05-24 16:12:31 -0400360void GrTextContext::BmpAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400361 GrGlyphCache* grGlyphCache,
362 sk_sp<GrTextStrike>* strike,
363 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
364 GrColor color, SkGlyphCache* skGlyphCache,
365 SkScalar textRatio, bool needsTransform) {
Brian Salomon52db9402017-11-07 14:58:55 -0500366 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500367 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500368 }
369
370 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
371 skGlyph.getSubXFixed(),
372 skGlyph.getSubYFixed(),
373 GrGlyph::kCoverage_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500374 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500375 if (!glyph) {
376 return;
377 }
378
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500379 SkASSERT(skGlyph.fWidth == glyph->width());
380 SkASSERT(skGlyph.fHeight == glyph->height());
381
Jim Van Verthf4c13162018-01-11 16:40:24 -0500382 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft);
383 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop);
384 SkScalar width = SkIntToScalar(glyph->fBounds.width());
385 SkScalar height = SkIntToScalar(glyph->fBounds.height());
Brian Salomon52db9402017-11-07 14:58:55 -0500386
Jim Van Verthf4c13162018-01-11 16:40:24 -0500387 dx *= textRatio;
388 dy *= textRatio;
389 width *= textRatio;
390 height *= textRatio;
Brian Salomon52db9402017-11-07 14:58:55 -0500391
Jim Van Verthf4c13162018-01-11 16:40:24 -0500392 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500393
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500394 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400395 textRatio, !needsTransform);
Brian Salomon52db9402017-11-07 14:58:55 -0500396}
397
Herb Derby26cbe512018-05-24 14:39:01 -0400398void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700399 if (options->fMaxDistanceFieldFontSize < 0.f) {
400 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
401 }
402 if (options->fMinDistanceFieldFontSize < 0.f) {
403 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
404 }
405}
406
Herb Derby26cbe512018-05-24 14:39:01 -0400407bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
408 const SkSurfaceProps& props,
409 bool contextSupportsDistanceFieldText,
410 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500411 if (!viewMatrix.hasPerspective()) {
412 SkScalar maxScale = viewMatrix.getMaxScale();
413 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
414 // Hinted text looks far better at small resolutions
415 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700416 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
417 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500418 return false;
419 }
420
421 bool useDFT = props.isUseDeviceIndependentFonts();
422#if SK_FORCE_DISTANCE_FIELD_TEXT
423 useDFT = true;
424#endif
425
426 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
427 return false;
428 }
429 }
430
Mike Reed8ad91a92018-01-19 19:09:32 -0500431 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700432 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500433 return false;
434 }
435
436 // TODO: add some stroking support
437 if (skPaint.getStyle() != SkPaint::kFill_Style) {
438 return false;
439 }
440
441 return true;
442}
443
Herb Derby86240592018-05-24 16:12:31 -0400444void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400445 SkPaint* skPaint,
446 const SkMatrix& viewMatrix,
447 const Options& options,
448 SkScalar* textRatio,
449 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500450 SkScalar textSize = skPaint->getTextSize();
451 SkScalar scaledTextSize = textSize;
452
453 if (viewMatrix.hasPerspective()) {
454 // for perspective, we simply force to the medium size
455 // TODO: compute a size based on approximate screen area
456 scaledTextSize = kMediumDFFontLimit;
457 } else {
458 SkScalar maxScale = viewMatrix.getMaxScale();
459 // if we have non-unity scale, we need to choose our base text size
460 // based on the SkPaint's text size multiplied by the max scale factor
461 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
462 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
463 scaledTextSize *= maxScale;
464 }
465 }
466
467 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
468 // and ceiling. A scale outside of this range would require regenerating the distance fields
469 SkScalar dfMaskScaleFloor;
470 SkScalar dfMaskScaleCeil;
471 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700472 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500473 dfMaskScaleCeil = kSmallDFFontLimit;
474 *textRatio = textSize / kSmallDFFontSize;
475 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
476 } else if (scaledTextSize <= kMediumDFFontLimit) {
477 dfMaskScaleFloor = kSmallDFFontLimit;
478 dfMaskScaleCeil = kMediumDFFontLimit;
479 *textRatio = textSize / kMediumDFFontSize;
480 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
481 } else {
482 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700483 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500484 *textRatio = textSize / kLargeDFFontSize;
485 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
486 }
487
488 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
489 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
490 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
491 // tolerate before we'd have to move to a large mip size. When we actually test these values
492 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
493 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
494 // level)
495 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700496 if (blob) {
497 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
498 dfMaskScaleCeil / scaledTextSize);
499 }
Brian Salomon52db9402017-11-07 14:58:55 -0500500
501 skPaint->setAntiAlias(true);
502 skPaint->setLCDRenderText(false);
503 skPaint->setAutohinted(false);
504 skPaint->setHinting(SkPaint::kNormal_Hinting);
505 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400506
507 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700508
509 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
510 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
511 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500512}
513
Herb Derby86240592018-05-24 16:12:31 -0400514void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400515 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
516 const GrTextUtils::Paint& paint,
517 SkScalerContextFlags scalerContextFlags,
518 const SkMatrix& viewMatrix, const char text[],
519 size_t byteLength, const SkScalar pos[],
520 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500521 SkASSERT(byteLength == 0 || text != nullptr);
522 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
523
524 // nothing to draw
525 if (text == nullptr || byteLength == 0) {
526 return;
527 }
528
Khushal3e7548c2018-05-23 15:45:01 -0700529 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500530
Brian Salomon52db9402017-11-07 14:58:55 -0500531 // Setup distance field paint and text ratio
532 SkScalar textRatio;
533 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700534 SkScalerContextFlags flags;
535 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500536 blob->setHasDistanceField();
537 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500538 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500539
Khushalfa8ff092018-06-06 17:46:38 -0700540 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
541 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500542
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500543 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500544
Herb Derby526819d2018-03-09 12:51:12 -0500545 {
Khushal3e7548c2018-05-23 15:45:01 -0700546 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500547 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400548 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500549
Herb Derby526819d2018-03-09 12:51:12 -0500550 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500551
Herb Derby526819d2018-03-09 12:51:12 -0500552 while (text < stop) {
553 const char* lastText = text;
554 // the last 2 parameters are ignored
Hal Canary4014ba62018-07-24 11:33:21 -0400555 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Brian Salomon52db9402017-11-07 14:58:55 -0500556
Herb Derby526819d2018-03-09 12:51:12 -0500557 if (glyph.fWidth) {
558 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400559 glyphPos.fX += pos[0];
560 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500561
Jim Van Verthd401da62018-05-03 10:40:30 -0400562 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby526819d2018-03-09 12:51:12 -0500563 DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
564 glyphPos.fY, paint.filteredPremulColor(), cache.get(), textRatio);
565 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400566 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500567 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
568 glyphPos);
569 }
Brian Salomon52db9402017-11-07 14:58:55 -0500570 }
Herb Derby526819d2018-03-09 12:51:12 -0500571 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500572 }
Brian Salomon52db9402017-11-07 14:58:55 -0500573 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000574
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500575 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500576}
577
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400578void GrTextContext::drawDFGlyphRun(GrTextBlob* blob, int runIndex,
579 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
580 const GrTextUtils::Paint& paint,
581 SkScalerContextFlags scalerContextFlags,
582 const SkMatrix& viewMatrix, const SkGlyphRun& glyphRun,
583 const SkPoint& offset) const {
584 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
585
586 // Setup distance field paint and text ratio
587 SkScalar textRatio;
588 SkPaint dfPaint(paint);
589 SkScalerContextFlags flags;
590 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
591 blob->setHasDistanceField();
592 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
593 paint.skPaint().isAntiAlias(), hasWCoord);
594
595 FallbackGlyphRunHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
596 textRatio);
597
598 sk_sp<GrTextStrike> currStrike;
599
600 {
601 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
602
603 const SkPoint* positionCursor = glyphRun.positions().data();
604 for (auto glyphID : glyphRun.shuntGlyphsIDs()) {
605 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
606 SkPoint glyphPos = offset + *positionCursor++;
607 if (glyph.fWidth > 0) {
608 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
609 DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
610 glyphPos.fY, paint.filteredPremulColor(), cache.get(), textRatio);
611 } else {
612 // can't append non-SDF glyph to SDF batch, send to fallback
613 fallbackTextHelper.appendText(glyph, glyphID, glyphPos);
614 }
615 }
616 }
617 }
618
619 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
620}
621
Jim Van Verthf4c13162018-01-11 16:40:24 -0500622// TODO: merge with BmpAppendGlyph
Herb Derby86240592018-05-24 16:12:31 -0400623void GrTextContext::DfAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400624 GrGlyphCache* grGlyphCache, sk_sp<GrTextStrike>* strike,
625 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
626 GrColor color, SkGlyphCache* skGlyphCache,
627 SkScalar textRatio) {
Brian Salomon52db9402017-11-07 14:58:55 -0500628 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500629 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500630 }
631
632 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
633 skGlyph.getSubXFixed(),
634 skGlyph.getSubYFixed(),
635 GrGlyph::kDistance_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500636 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500637 if (!glyph) {
Jim Van Verthf4c13162018-01-11 16:40:24 -0500638 return;
Brian Salomon52db9402017-11-07 14:58:55 -0500639 }
640
641 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
642 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
643 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
644 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
645
Jim Van Verthf4c13162018-01-11 16:40:24 -0500646 dx *= textRatio;
647 dy *= textRatio;
648 width *= textRatio;
649 height *= textRatio;
650 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500651
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500652 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500653 textRatio, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500654}
655
joshualitt79dfb2b2015-05-11 08:58:08 -0700656///////////////////////////////////////////////////////////////////////////////////////////////////
657
Herb Derby26cbe512018-05-24 14:39:01 -0400658void GrTextContext::FallbackTextHelper::appendText(const SkGlyph& glyph, int count,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500659 const char* text, SkPoint glyphPos) {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400660 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
Khushalfa8ff092018-06-06 17:46:38 -0700661 if (SkScalarNearlyZero(maxDim)) return;
662
Jim Van Verthb515ae72018-05-23 16:44:55 -0400663 if (!fUseTransformedFallback) {
664 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
665 fUseTransformedFallback = true;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500666 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
Jim Van Verthc401bb92018-02-15 14:05:24 -0500667 }
668 }
669
670 fFallbackTxt.append(count, text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400671 if (fUseTransformedFallback) {
Jim Van Verth080a9282018-03-02 10:41:43 -0500672 // If there's a glyph in the font that's particularly large, it's possible
673 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
Jim Van Verth8b7284d2018-05-17 12:33:52 -0400674 // that glyph than make the others blurry, so we set a minimum size of half the
Jim Van Verth080a9282018-03-02 10:41:43 -0500675 // maximum text size to avoid this case.
Jim Van Verthb515ae72018-05-23 16:44:55 -0400676 SkScalar glyphTextSize = SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim),
Jim Van Verth080a9282018-03-02 10:41:43 -0500677 0.5f*fMaxTextSize);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400678 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500679 }
680 *fFallbackPos.append() = glyphPos;
681}
682
Herb Derby86240592018-05-24 16:12:31 -0400683void GrTextContext::FallbackTextHelper::drawText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400684 GrGlyphCache* glyphCache,
685 const SkSurfaceProps& props,
686 const GrTextUtils::Paint& paint,
687 SkScalerContextFlags scalerContextFlags) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500688 if (fFallbackTxt.count()) {
689 blob->initOverride(runIndex);
690 blob->setHasBitmap();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400691 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
Herb Derby526819d2018-03-09 12:51:12 -0500692 SkExclusiveStrikePtr cache;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500693 const SkPaint& skPaint = paint.skPaint();
694 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400695 SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500696 SkColor textColor = paint.filteredPremulColor();
Khushalfa8ff092018-06-06 17:46:38 -0700697
Jim Van Verthc401bb92018-02-15 14:05:24 -0500698 SkScalar textRatio = SK_Scalar1;
Khushalfa8ff092018-06-06 17:46:38 -0700699 SkPaint fallbackPaint(skPaint);
700 SkMatrix matrix = fViewMatrix;
701 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
702 cache = blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500703
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500704 sk_sp<GrTextStrike> currStrike;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500705 const char* text = fFallbackTxt.begin();
706 const char* stop = text + fFallbackTxt.count();
707 SkPoint* glyphPos = fFallbackPos.begin();
708 while (text < stop) {
Hal Canary4014ba62018-07-24 11:33:21 -0400709 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text, stop);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400710 if (!fUseTransformedFallback) {
711 fViewMatrix.mapPoints(glyphPos, 1);
Jim Van Verth76e85162018-03-29 13:46:56 -0400712 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
713 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
714 }
Herb Derby26cbe512018-05-24 14:39:01 -0400715 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500716 glyphPos->fX, glyphPos->fY, textColor,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400717 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500718 glyphPos++;
719 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500720 }
721}
722
Khushalfa8ff092018-06-06 17:46:38 -0700723void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
724 SkMatrix* matrix) const {
725 if (!fUseTransformedFallback) return;
726
727 paint->setTextSize(fTransformedFallbackTextSize);
728 *textRatio = fTextSize / fTransformedFallbackTextSize;
729 *matrix = SkMatrix::I();
730}
731
Jim Van Verthc401bb92018-02-15 14:05:24 -0500732///////////////////////////////////////////////////////////////////////////////////////////////////
733
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400734void GrTextContext::FallbackGlyphRunHelper::appendText(
735 const SkGlyph& glyph, SkGlyphID glyphID, SkPoint glyphPos) {
736 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
737 if (SkScalarNearlyZero(maxDim)) return;
738
739 if (!fUseTransformedFallback) {
740 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
741 fUseTransformedFallback = true;
742 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
743 }
744 }
745
746 fFallbackTxt.push_back(glyphID);
747 if (fUseTransformedFallback) {
748 // If there's a glyph in the font that's particularly large, it's possible
749 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
750 // that glyph than make the others blurry, so we set a minimum size of half the
751 // maximum text size to avoid this case.
752 SkScalar glyphTextSize =
753 SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim), 0.5f*fMaxTextSize);
754 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
755 }
756 fFallbackPos.push_back(glyphPos);
757}
758
759void GrTextContext::FallbackGlyphRunHelper::drawText(
760 GrTextBlob* blob, int runIndex, GrGlyphCache* glyphCache, const SkSurfaceProps& props,
761 const GrTextUtils::Paint& paint, SkScalerContextFlags scalerContextFlags) {
762 if (!fFallbackTxt.empty()) {
763 blob->initOverride(runIndex);
764 blob->setHasBitmap();
765 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
766 const SkPaint& skPaint = paint.skPaint();
767 SkColor textColor = paint.filteredPremulColor();
768
769 SkScalar textRatio = SK_Scalar1;
770 SkPaint fallbackPaint(skPaint);
771 SkMatrix matrix = fViewMatrix;
772 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
773 SkExclusiveStrikePtr cache =
774 blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
775
776 sk_sp<GrTextStrike> currStrike;
777 auto glyphPos = fFallbackPos.begin();
778 for (auto glyphID : fFallbackTxt) {
779 const SkGlyph& glyph = cache->getGlyphIDMetrics(glyphID);
780 if (!fUseTransformedFallback) {
781 fViewMatrix.mapPoints(&*glyphPos, 1);
782 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
783 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
784 }
785 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
786 glyphPos->fX, glyphPos->fY, textColor,
787 cache.get(), textRatio, fUseTransformedFallback);
788 glyphPos++;
789 }
790 }
791}
792
793void GrTextContext::FallbackGlyphRunHelper::initializeForDraw(
794 SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const {
795 if (!fUseTransformedFallback) return;
796
797 paint->setTextSize(fTransformedFallbackTextSize);
798 *textRatio = fTextSize / fTransformedFallbackTextSize;
799 *matrix = SkMatrix::I();
800}
801
802///////////////////////////////////////////////////////////////////////////////////////////////////
803
804
Hal Canary6f6961e2017-01-31 13:50:44 -0500805#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700806
Brian Salomonf18b1d82017-10-27 11:30:49 -0400807#include "GrRenderTargetContext.h"
808
Herb Derby26cbe512018-05-24 14:39:01 -0400809std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
810 GrTextContext* textContext,
811 GrRenderTargetContext* rtc,
812 const SkPaint& skPaint,
813 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400814 const char* text,
815 int x,
816 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500817 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500818
819 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
820
821 size_t textLen = (int)strlen(text);
822
823 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
824
825 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
826 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000827
828 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400829 SkGlyphRunBuilder builder;
Herb Derbyc434ade2018-07-11 16:07:01 -0400830 builder.drawText(skPaint, text, textLen, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400831 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000832
Herb Derby8a6348e2018-07-12 15:30:35 -0400833 auto glyphRunList = builder.useGlyphRunList();
Herb Derbyb935cf82018-07-26 16:54:18 -0400834 if (!glyphRunList.empty()) {
835 auto glyphRun = glyphRunList[0];
Herb Derby8a6348e2018-07-12 15:30:35 -0400836 // Use the text and textLen below, because we don't want to mess with the paint.
837 glyphRun.temporaryShuntToCallback(
Herb Derby59d997a2018-06-07 12:44:09 -0400838 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
839 blob = textContext->makeDrawPosTextBlob(
840 context->contextPriv().getTextBlobCache(), glyphCache,
841 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
Herb Derby8a6348e2018-07-12 15:30:35 -0400842 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps,
Herb Derbycddab252018-07-16 11:19:04 -0400843 text, textLen, pos, 2, origin);
Herb Derby59d997a2018-06-07 12:44:09 -0400844 });
Herb Derby8a6348e2018-07-12 15:30:35 -0400845 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500846
847 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500848 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500849}
850
Brian Salomon44acb5b2017-07-18 19:59:24 -0400851GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700852 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400853 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700854 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700855
856 if (context->uniqueID() != gContextID) {
857 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400858 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700859 }
860
Brian Osman11052242016-10-27 14:47:55 -0400861 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500862 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400863 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700864
joshualitt6c891102015-05-13 08:51:49 -0700865 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400866
867 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
868 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700869 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500870 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700871 skPaint.setLCDRenderText(random->nextBool());
872 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
873 skPaint.setSubpixelText(random->nextBool());
874
joshualitt79dfb2b2015-05-11 08:58:08 -0700875 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700876
joshualittb8d86492016-02-24 09:23:03 -0800877 // create some random x/y offsets, including negative offsets
878 static const int kMaxTrans = 1024;
879 int xPos = (random->nextU() % 2) * 2 - 1;
880 int yPos = (random->nextU() % 2) * 2 - 1;
881 int xInt = (random->nextU() % kMaxTrans) * xPos;
882 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700883
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500884 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
885 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700886}
887
888#endif