blob: 16f164c9c83457aa21fc6e80c74ac9d37e94b357 [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"
17#include "SkDrawFilter.h"
Jim Van Verth54d9c882018-02-08 16:14:48 -050018#include "SkDrawProcs.h"
Brian Salomon52db9402017-11-07 14:58:55 -050019#include "SkFindAndPlaceGlyph.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040020#include "SkGlyphRun.h"
Brian Osman3b655982017-03-07 16:58:08 -050021#include "SkGr.h"
Jim Van Verthc65b65d2018-01-16 16:26:35 -050022#include "SkGraphics.h"
Brian Salomonaf597482017-11-07 16:23:34 -050023#include "SkMakeUnique.h"
Mike Reed80747ef2018-01-23 15:29:32 -050024#include "SkMaskFilterBase.h"
Herb Derbyeb3f6742018-03-05 14:36:45 -050025#include "SkPaintPriv.h"
Jim Van Verth54d9c882018-02-08 16:14:48 -050026#include "SkTextMapStateProc.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040027#include "SkTo.h"
Brian Salomon649a3412017-03-09 13:50:43 -050028#include "ops/GrMeshDrawOp.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070029
Brian Salomonaf597482017-11-07 16:23:34 -050030// DF sizes and thresholds for usage of the small and medium sizes. For example, above
31// kSmallDFFontLimit we will use the medium size. The large size is used up until the size at
32// which we switch over to drawing as paths as controlled by Options.
33static const int kSmallDFFontSize = 32;
34static const int kSmallDFFontLimit = 32;
Jim Van Verth825d4d72018-01-30 20:37:03 +000035static const int kMediumDFFontSize = 72;
36static const int kMediumDFFontLimit = 72;
37static const int kLargeDFFontSize = 162;
Brian Salomonaf597482017-11-07 16:23:34 -050038
39static const int kDefaultMinDistanceFieldFontSize = 18;
40#ifdef SK_BUILD_FOR_ANDROID
41static const int kDefaultMaxDistanceFieldFontSize = 384;
42#else
43static const int kDefaultMaxDistanceFieldFontSize = 2 * kLargeDFFontSize;
44#endif
45
Herb Derby26cbe512018-05-24 14:39:01 -040046GrTextContext::GrTextContext(const Options& options)
Khushal3e7548c2018-05-23 15:45:01 -070047 : fDistanceAdjustTable(new GrDistanceFieldAdjustTable), fOptions(options) {
48 SanitizeOptions(&fOptions);
joshualitt9bd2daf2015-04-17 09:30:06 -070049}
50
Herb Derby26cbe512018-05-24 14:39:01 -040051std::unique_ptr<GrTextContext> GrTextContext::Make(const Options& options) {
52 return std::unique_ptr<GrTextContext>(new GrTextContext(options));
joshualitt1d89e8d2015-04-01 12:40:54 -070053}
54
Herb Derby26cbe512018-05-24 14:39:01 -040055SkColor GrTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -050056 SkColor canonicalColor = paint.computeLuminanceColor();
joshualitt9e36c1a2015-04-14 12:17:27 -070057 if (lcd) {
58 // This is the correct computation, but there are tons of cases where LCD can be overridden.
59 // For now we just regenerate if any run in a textblob has LCD.
60 // TODO figure out where all of these overrides are and see if we can incorporate that logic
61 // at a higher level *OR* use sRGB
62 SkASSERT(false);
63 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
64 } else {
65 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
66 // gamma corrected masks anyways, nor color
67 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
68 SkColorGetG(canonicalColor),
69 SkColorGetB(canonicalColor));
70 // reduce to our finite number of bits
71 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
72 }
73 return canonicalColor;
74}
75
Herb Derby26cbe512018-05-24 14:39:01 -040076SkScalerContextFlags GrTextContext::ComputeScalerContextFlags(
Herb Derbyd8327a82018-01-22 14:39:27 -050077 const GrColorSpaceInfo& colorSpaceInfo) {
Brian Osman34ec3742018-07-03 10:40:57 -040078 // If we're doing linear blending, then we can disable the gamma hacks.
brianosman5280dcb2016-04-14 06:02:59 -070079 // Otherwise, leave them on. In either case, we still want the contrast boost:
Brian Osman34ec3742018-07-03 10:40:57 -040080 // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
81 if (colorSpaceInfo.isLinearlyBlended()) {
Herb Derbyd8327a82018-01-22 14:39:27 -050082 return SkScalerContextFlags::kBoostContrast;
brianosman32f77822016-04-07 06:25:45 -070083 } else {
Herb Derbyd8327a82018-01-22 14:39:27 -050084 return SkScalerContextFlags::kFakeGammaAndBoostContrast;
brianosman32f77822016-04-07 06:25:45 -070085 }
86}
87
Robert Phillips38580452018-06-28 12:00:35 +000088// TODO if this function ever shows up in profiling, then we can compute this value when the
89// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
90// run so this is not a big deal to compute here.
91bool GrTextContext::HasLCD(const SkTextBlob* blob) {
92 SkTextBlobRunIterator it(blob);
93 for (; !it.done(); it.next()) {
94 if (it.isLCD()) {
95 return true;
96 }
97 }
98 return false;
99}
joshualitt9e36c1a2015-04-14 12:17:27 -0700100
Robert Phillips38580452018-06-28 12:00:35 +0000101void GrTextContext::drawTextBlob(GrContext* context, GrTextUtils::Target* target,
102 const GrClip& clip, const SkPaint& skPaint,
103 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
104 const SkTextBlob* blob, SkScalar x, SkScalar y,
105 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700106 // If we have been abandoned, then don't draw
Khushalc421ca12018-06-26 14:38:34 -0700107 if (context->abandoned()) {
robertphillipsea461502015-05-26 11:38:03 -0700108 return;
109 }
110
Robert Phillips38580452018-06-28 12:00:35 +0000111 sk_sp<GrTextBlob> cacheBlob;
Mike Reed80747ef2018-01-23 15:29:32 -0500112 SkMaskFilterBase::BlurRec blurRec;
Robert Phillips38580452018-06-28 12:00:35 +0000113 GrTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700114 // It might be worth caching these things, but its not clear at this time
115 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
116 const SkMaskFilter* mf = skPaint.getMaskFilter();
Robert Phillips38580452018-06-28 12:00:35 +0000117 bool canCache = !(skPaint.getPathEffect() ||
118 (mf && !as_MFB(mf)->asABlur(&blurRec)) ||
119 drawFilter);
Herb Derbyd8327a82018-01-22 14:39:27 -0500120 SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
joshualitt2a0e9f32015-04-13 06:12:21 -0700121
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500122 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500123 GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache();
124
joshualitt2a0e9f32015-04-13 06:12:21 -0700125 if (canCache) {
Robert Phillips38580452018-06-28 12:00:35 +0000126 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700127
128 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
joshualitt2c89bc12016-02-11 05:42:30 -0800129 SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() :
Robert Phillips38580452018-06-28 12:00:35 +0000130 kUnknown_SkPixelGeometry;
joshualitte4cee1f2015-05-11 13:04:28 -0700131
joshualitt9e36c1a2015-04-14 12:17:27 -0700132 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
133 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
134 // ensure we always match the same key
135 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
Robert Phillips38580452018-06-28 12:00:35 +0000136 ComputeCanonicalColor(skPaint, hasLCD);
joshualitt9e36c1a2015-04-14 12:17:27 -0700137
joshualitte4cee1f2015-05-11 13:04:28 -0700138 key.fPixelGeometry = pixelGeometry;
Robert Phillips38580452018-06-28 12:00:35 +0000139 key.fUniqueID = blob->uniqueID();
joshualitt53b5f442015-04-13 06:33:59 -0700140 key.fStyle = skPaint.getStyle();
141 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700142 key.fCanonicalColor = canonicalColor;
brianosman8d7ffce2016-04-21 08:29:06 -0700143 key.fScalerContextFlags = scalerContextFlags;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500144 cacheBlob = textBlobCache->find(key);
joshualitt2a0e9f32015-04-13 06:12:21 -0700145 }
146
Brian Salomonf18b1d82017-10-27 11:30:49 -0400147 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
joshualittb7133be2015-04-08 09:08:31 -0700148 if (cacheBlob) {
Robert Phillips38580452018-06-28 12:00:35 +0000149 if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700150 // We have to remake the blob because changes may invalidate our masks.
151 // TODO we could probably get away reuse most of the time if the pointer is unique,
152 // but we'd have to clear the subrun information
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500153 textBlobCache->remove(cacheBlob.get());
Robert Phillips38580452018-06-28 12:00:35 +0000154 cacheBlob = textBlobCache->makeCachedBlob(blob, key, blurRec, skPaint);
155 this->regenerateTextBlob(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400156 *context->contextPriv().caps()->shaderCaps(), paint,
Robert Phillips38580452018-06-28 12:00:35 +0000157 scalerContextFlags, viewMatrix, props, blob, x, y, drawFilter);
joshualittb7133be2015-04-08 09:08:31 -0700158 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500159 textBlobCache->makeMRU(cacheBlob.get());
joshualitt2f2ee832016-02-10 08:52:24 -0800160
161 if (CACHE_SANITY_CHECK) {
Robert Phillips38580452018-06-28 12:00:35 +0000162 int glyphCount = 0;
163 int runCount = 0;
164 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
Herb Derby86240592018-05-24 16:12:31 -0400165 sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
joshualitt92303772016-02-10 11:55:52 -0800166 sanityBlob->setupKey(key, blurRec, skPaint);
Robert Phillips38580452018-06-28 12:00:35 +0000167 this->regenerateTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400168 sanityBlob.get(), glyphCache, *context->contextPriv().caps()->shaderCaps(),
Robert Phillips38580452018-06-28 12:00:35 +0000169 paint, scalerContextFlags, viewMatrix, props, blob, x, y, drawFilter);
Herb Derby86240592018-05-24 16:12:31 -0400170 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700171 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700172 }
173 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700174 if (canCache) {
Robert Phillips38580452018-06-28 12:00:35 +0000175 cacheBlob = textBlobCache->makeCachedBlob(blob, key, blurRec, skPaint);
joshualitt2a0e9f32015-04-13 06:12:21 -0700176 } else {
Robert Phillips38580452018-06-28 12:00:35 +0000177 cacheBlob = textBlobCache->makeBlob(blob);
joshualitt2a0e9f32015-04-13 06:12:21 -0700178 }
Robert Phillips38580452018-06-28 12:00:35 +0000179 this->regenerateTextBlob(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400180 *context->contextPriv().caps()->shaderCaps(), paint,
Robert Phillips38580452018-06-28 12:00:35 +0000181 scalerContextFlags, viewMatrix, props, blob, x, y, drawFilter);
joshualitt1d89e8d2015-04-01 12:40:54 -0700182 }
183
Robert Phillips5a66efb2018-03-07 15:13:18 -0500184 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Robert Phillips38580452018-06-28 12:00:35 +0000185 clip, viewMatrix, clipBounds, x, y);
joshualitt1d89e8d2015-04-01 12:40:54 -0700186}
187
Robert Phillips38580452018-06-28 12:00:35 +0000188void GrTextContext::regenerateTextBlob(GrTextBlob* cacheBlob,
Herb Derby26cbe512018-05-24 14:39:01 -0400189 GrGlyphCache* glyphCache,
190 const GrShaderCaps& shaderCaps,
191 const GrTextUtils::Paint& paint,
192 SkScalerContextFlags scalerContextFlags,
193 const SkMatrix& viewMatrix,
Robert Phillips38580452018-06-28 12:00:35 +0000194 const SkSurfaceProps& props, const SkTextBlob* blob,
195 SkScalar x, SkScalar y,
196 SkDrawFilter* drawFilter) const {
197 cacheBlob->initReusableBlob(paint.luminanceColor(), viewMatrix, x, y);
joshualitt1d89e8d2015-04-01 12:40:54 -0700198
199 // Regenerate textblob
Robert Phillips38580452018-06-28 12:00:35 +0000200 SkTextBlobRunIterator it(blob);
201 GrTextUtils::RunPaint runPaint(&paint, drawFilter);
joshualitt1d89e8d2015-04-01 12:40:54 -0700202 for (int run = 0; !it.done(); it.next(), run++) {
203 int glyphCount = it.glyphCount();
204 size_t textLen = glyphCount * sizeof(uint16_t);
Robert Phillips38580452018-06-28 12:00:35 +0000205 const SkPoint& offset = it.offset();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500206 cacheBlob->push_back_run(run);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500207 if (!runPaint.modifyForRun([it](SkPaint* p) { it.applyFontToPaint(p); })) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700208 continue;
209 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500210 cacheBlob->setRunPaintFlags(run, runPaint.skPaint().getFlags());
211
Khushal3e7548c2018-05-23 15:45:01 -0700212 if (CanDrawAsDistanceFields(runPaint, viewMatrix, props,
213 shaderCaps.supportsDistanceFieldText(), fOptions)) {
Robert Phillips38580452018-06-28 12:00:35 +0000214 switch (it.positioning()) {
215 case SkTextBlob::kDefault_Positioning: {
216 auto origin = SkPoint::Make(x + offset.x(), y + offset.y());
217 SkGlyphRunBuilder builder;
218 builder.prepareDrawText(runPaint.skPaint(),
219 (const char*)it.glyphs(), textLen, origin);
220
221 auto glyphRun = builder.useGlyphRun();
222
223 glyphRun->temporaryShuntToCallback(
224 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
225 this->drawDFPosText(
226 cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
227 viewMatrix, glyphIDs, 2 * runSize, pos, 2,
228 SkPoint::Make(0,0));
229 });
230 break;
231 }
232
233 case SkTextBlob::kHorizontal_Positioning: {
234 SkPoint dfOffset = SkPoint::Make(x, y + offset.y());
235 this->drawDFPosText(cacheBlob, run, glyphCache, props, runPaint,
236 scalerContextFlags, viewMatrix, (const char*)it.glyphs(),
237 textLen, it.pos(), 1, dfOffset);
238 break;
239 }
240 case SkTextBlob::kFull_Positioning: {
241 SkPoint dfOffset = SkPoint::Make(x, y);
242 this->drawDFPosText(cacheBlob, run, glyphCache, props, runPaint,
243 scalerContextFlags, viewMatrix, (const char*)it.glyphs(),
244 textLen, it.pos(), 2, dfOffset);
245 break;
246 }
247 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700248 } else {
Robert Phillips38580452018-06-28 12:00:35 +0000249 switch (it.positioning()) {
250 case SkTextBlob::kDefault_Positioning: {
251 auto origin = SkPoint::Make(x + offset.x(), y + offset.y());
252 SkGlyphRunBuilder builder;
253 builder.prepareDrawText(runPaint.skPaint(),
254 (const char*)it.glyphs(), textLen, origin);
255
256 auto glyphRun = builder.useGlyphRun();
257
258 glyphRun->temporaryShuntToCallback(
259 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
260 this->DrawBmpPosText(
261 cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
262 viewMatrix, glyphIDs, 2 * runSize,
263 pos, 2, SkPoint::Make(0, 0));
264 });
265 break;
266 }
267 case SkTextBlob::kHorizontal_Positioning:
268 DrawBmpPosText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
269 viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 1,
270 SkPoint::Make(x, y + offset.y()));
271 break;
272 case SkTextBlob::kFull_Positioning:
273 DrawBmpPosText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
274 viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 2,
275 SkPoint::Make(x, y));
276 break;
277 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700278 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700279 }
280}
281
Herb Derby86240592018-05-24 16:12:31 -0400282inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400283GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
284 GrGlyphCache* glyphCache,
285 const GrShaderCaps& shaderCaps,
286 const GrTextUtils::Paint& paint,
287 SkScalerContextFlags scalerContextFlags,
288 const SkMatrix& viewMatrix,
289 const SkSurfaceProps& props,
290 const char text[], size_t byteLength,
291 const SkScalar pos[], int scalarsPerPosition, const
292 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500293 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400294 if (!glyphCount) {
295 return nullptr;
296 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700297
Herb Derby86240592018-05-24 16:12:31 -0400298 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800299 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500300 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700301
Khushal3e7548c2018-05-23 15:45:01 -0700302 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
303 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500304 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500305 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700306 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500307 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
308 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700309 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700310 return blob;
311}
312
Herb Derby26cbe512018-05-24 14:39:01 -0400313void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
314 const GrClip& clip, const SkPaint& skPaint,
315 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
316 const char text[], size_t byteLength, const SkScalar pos[],
317 int scalarsPerPosition, const SkPoint& offset,
318 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400319 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Khushalc421ca12018-06-26 14:38:34 -0700320 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800321 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500322 }
323
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500324 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500325 auto textBlobCache = context->contextPriv().getTextBlobCache();
326
Herb Derby86240592018-05-24 16:12:31 -0400327 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400328 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500329 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
330 byteLength, pos, scalarsPerPosition, offset));
331 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500332 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500333 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800334 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700335}
336
Robert Phillips38580452018-06-28 12:00:35 +0000337
Herb Derby86240592018-05-24 16:12:31 -0400338void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400339 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
340 const GrTextUtils::Paint& paint,
341 SkScalerContextFlags scalerContextFlags,
342 const SkMatrix& viewMatrix,
343 const char text[], size_t byteLength, const SkScalar pos[],
344 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500345 SkASSERT(byteLength == 0 || text != nullptr);
346 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
347
348 // nothing to draw
349 if (text == nullptr || byteLength == 0) {
350 return;
351 }
352
353 // Ensure the blob is set for bitmaptext
354 blob->setHasBitmap();
355
Jim Van Verth54d9c882018-02-08 16:14:48 -0500356 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500357 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500358 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500359 return;
360 }
361
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000362 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500363 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500364 SkFindAndPlaceGlyph::ProcessPosText(
365 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400366 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500367 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
368 position += rounding;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500369 BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500370 SkScalarFloorToScalar(position.fX),
371 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400372 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500373 });
Brian Salomon52db9402017-11-07 14:58:55 -0500374}
375
Herb Derby86240592018-05-24 16:12:31 -0400376void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400377 GrGlyphCache* glyphCache,
378 const SkSurfaceProps& props,
379 const GrTextUtils::Paint& origPaint,
380 SkScalerContextFlags scalerContextFlags,
381 const SkMatrix& viewMatrix,
382 const char text[], size_t byteLength,
383 const SkScalar pos[], int scalarsPerPosition,
384 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500385 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
386
387 // nothing to draw
388 if (text == nullptr || byteLength == 0) {
389 return;
390 }
391
392 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500393 SkPaint pathPaint(origPaint);
394 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700395 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
396 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500397
398 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500399 pathPaint.setStyle(SkPaint::kFill_Style);
400 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500401
Jim Van Verthc401bb92018-02-15 14:05:24 -0500402 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500403 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400404 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400405 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500406
407 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500408 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500409 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
410
411 while (text < stop) {
Herb Derby4e34a012018-03-21 10:47:30 -0400412 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500413 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500414 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400415 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500416 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
417 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
418 } else {
419 const SkPath* path = cache->findPath(glyph);
420 if (path) {
421 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
422 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500423 }
424 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500425 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500426 pos += scalarsPerPosition;
427 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500428
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500429 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500430}
431
Herb Derby86240592018-05-24 16:12:31 -0400432void GrTextContext::BmpAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400433 GrGlyphCache* grGlyphCache,
434 sk_sp<GrTextStrike>* strike,
435 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
436 GrColor color, SkGlyphCache* skGlyphCache,
437 SkScalar textRatio, bool needsTransform) {
Brian Salomon52db9402017-11-07 14:58:55 -0500438 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500439 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500440 }
441
442 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
443 skGlyph.getSubXFixed(),
444 skGlyph.getSubYFixed(),
445 GrGlyph::kCoverage_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500446 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500447 if (!glyph) {
448 return;
449 }
450
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500451 SkASSERT(skGlyph.fWidth == glyph->width());
452 SkASSERT(skGlyph.fHeight == glyph->height());
453
Jim Van Verthf4c13162018-01-11 16:40:24 -0500454 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft);
455 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop);
456 SkScalar width = SkIntToScalar(glyph->fBounds.width());
457 SkScalar height = SkIntToScalar(glyph->fBounds.height());
Brian Salomon52db9402017-11-07 14:58:55 -0500458
Jim Van Verthf4c13162018-01-11 16:40:24 -0500459 dx *= textRatio;
460 dy *= textRatio;
461 width *= textRatio;
462 height *= textRatio;
Brian Salomon52db9402017-11-07 14:58:55 -0500463
Jim Van Verthf4c13162018-01-11 16:40:24 -0500464 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500465
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500466 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400467 textRatio, !needsTransform);
Brian Salomon52db9402017-11-07 14:58:55 -0500468}
469
Herb Derby26cbe512018-05-24 14:39:01 -0400470void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700471 if (options->fMaxDistanceFieldFontSize < 0.f) {
472 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
473 }
474 if (options->fMinDistanceFieldFontSize < 0.f) {
475 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
476 }
477}
478
Herb Derby26cbe512018-05-24 14:39:01 -0400479bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
480 const SkSurfaceProps& props,
481 bool contextSupportsDistanceFieldText,
482 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500483 if (!viewMatrix.hasPerspective()) {
484 SkScalar maxScale = viewMatrix.getMaxScale();
485 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
486 // Hinted text looks far better at small resolutions
487 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700488 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
489 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500490 return false;
491 }
492
493 bool useDFT = props.isUseDeviceIndependentFonts();
494#if SK_FORCE_DISTANCE_FIELD_TEXT
495 useDFT = true;
496#endif
497
498 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
499 return false;
500 }
501 }
502
Mike Reed8ad91a92018-01-19 19:09:32 -0500503 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700504 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500505 return false;
506 }
507
508 // TODO: add some stroking support
509 if (skPaint.getStyle() != SkPaint::kFill_Style) {
510 return false;
511 }
512
513 return true;
514}
515
Herb Derby86240592018-05-24 16:12:31 -0400516void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400517 SkPaint* skPaint,
518 const SkMatrix& viewMatrix,
519 const Options& options,
520 SkScalar* textRatio,
521 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500522 SkScalar textSize = skPaint->getTextSize();
523 SkScalar scaledTextSize = textSize;
524
525 if (viewMatrix.hasPerspective()) {
526 // for perspective, we simply force to the medium size
527 // TODO: compute a size based on approximate screen area
528 scaledTextSize = kMediumDFFontLimit;
529 } else {
530 SkScalar maxScale = viewMatrix.getMaxScale();
531 // if we have non-unity scale, we need to choose our base text size
532 // based on the SkPaint's text size multiplied by the max scale factor
533 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
534 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
535 scaledTextSize *= maxScale;
536 }
537 }
538
539 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
540 // and ceiling. A scale outside of this range would require regenerating the distance fields
541 SkScalar dfMaskScaleFloor;
542 SkScalar dfMaskScaleCeil;
543 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700544 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500545 dfMaskScaleCeil = kSmallDFFontLimit;
546 *textRatio = textSize / kSmallDFFontSize;
547 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
548 } else if (scaledTextSize <= kMediumDFFontLimit) {
549 dfMaskScaleFloor = kSmallDFFontLimit;
550 dfMaskScaleCeil = kMediumDFFontLimit;
551 *textRatio = textSize / kMediumDFFontSize;
552 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
553 } else {
554 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700555 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500556 *textRatio = textSize / kLargeDFFontSize;
557 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
558 }
559
560 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
561 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
562 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
563 // tolerate before we'd have to move to a large mip size. When we actually test these values
564 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
565 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
566 // level)
567 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700568 if (blob) {
569 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
570 dfMaskScaleCeil / scaledTextSize);
571 }
Brian Salomon52db9402017-11-07 14:58:55 -0500572
573 skPaint->setAntiAlias(true);
574 skPaint->setLCDRenderText(false);
575 skPaint->setAutohinted(false);
576 skPaint->setHinting(SkPaint::kNormal_Hinting);
577 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400578
579 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700580
581 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
582 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
583 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500584}
585
Herb Derby86240592018-05-24 16:12:31 -0400586void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400587 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
588 const GrTextUtils::Paint& paint,
589 SkScalerContextFlags scalerContextFlags,
590 const SkMatrix& viewMatrix, const char text[],
591 size_t byteLength, const SkScalar pos[],
592 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500593 SkASSERT(byteLength == 0 || text != nullptr);
594 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
595
596 // nothing to draw
597 if (text == nullptr || byteLength == 0) {
598 return;
599 }
600
Khushal3e7548c2018-05-23 15:45:01 -0700601 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500602
Brian Salomon52db9402017-11-07 14:58:55 -0500603 // Setup distance field paint and text ratio
604 SkScalar textRatio;
605 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700606 SkScalerContextFlags flags;
607 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500608 blob->setHasDistanceField();
609 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500610 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500611
Khushalfa8ff092018-06-06 17:46:38 -0700612 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
613 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500614
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500615 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500616
Herb Derby526819d2018-03-09 12:51:12 -0500617 {
Khushal3e7548c2018-05-23 15:45:01 -0700618 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500619 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400620 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500621
Herb Derby526819d2018-03-09 12:51:12 -0500622 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500623
Herb Derby526819d2018-03-09 12:51:12 -0500624 while (text < stop) {
625 const char* lastText = text;
626 // the last 2 parameters are ignored
627 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
Brian Salomon52db9402017-11-07 14:58:55 -0500628
Herb Derby526819d2018-03-09 12:51:12 -0500629 if (glyph.fWidth) {
630 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400631 glyphPos.fX += pos[0];
632 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500633
Jim Van Verthd401da62018-05-03 10:40:30 -0400634 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby526819d2018-03-09 12:51:12 -0500635 DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
636 glyphPos.fY, paint.filteredPremulColor(), cache.get(), textRatio);
637 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400638 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500639 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
640 glyphPos);
641 }
Brian Salomon52db9402017-11-07 14:58:55 -0500642 }
Herb Derby526819d2018-03-09 12:51:12 -0500643 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500644 }
Brian Salomon52db9402017-11-07 14:58:55 -0500645 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000646
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500647 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500648}
649
Jim Van Verthf4c13162018-01-11 16:40:24 -0500650// TODO: merge with BmpAppendGlyph
Herb Derby86240592018-05-24 16:12:31 -0400651void GrTextContext::DfAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400652 GrGlyphCache* grGlyphCache, sk_sp<GrTextStrike>* strike,
653 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
654 GrColor color, SkGlyphCache* skGlyphCache,
655 SkScalar textRatio) {
Brian Salomon52db9402017-11-07 14:58:55 -0500656 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500657 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500658 }
659
660 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
661 skGlyph.getSubXFixed(),
662 skGlyph.getSubYFixed(),
663 GrGlyph::kDistance_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500664 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500665 if (!glyph) {
Jim Van Verthf4c13162018-01-11 16:40:24 -0500666 return;
Brian Salomon52db9402017-11-07 14:58:55 -0500667 }
668
669 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
670 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
671 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
672 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
673
Jim Van Verthf4c13162018-01-11 16:40:24 -0500674 dx *= textRatio;
675 dy *= textRatio;
676 width *= textRatio;
677 height *= textRatio;
678 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500679
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500680 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500681 textRatio, false);
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) {
Herb Derby526819d2018-03-09 12:51:12 -0500737 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
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 Derby26cbe512018-05-24 14:39:01 -0400743 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500744 glyphPos->fX, glyphPos->fY, textColor,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400745 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500746 glyphPos++;
747 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500748 }
749}
750
Khushalfa8ff092018-06-06 17:46:38 -0700751void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
752 SkMatrix* matrix) const {
753 if (!fUseTransformedFallback) return;
754
755 paint->setTextSize(fTransformedFallbackTextSize);
756 *textRatio = fTextSize / fTransformedFallbackTextSize;
757 *matrix = SkMatrix::I();
758}
759
Jim Van Verthc401bb92018-02-15 14:05:24 -0500760///////////////////////////////////////////////////////////////////////////////////////////////////
761
Hal Canary6f6961e2017-01-31 13:50:44 -0500762#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700763
Brian Salomonf18b1d82017-10-27 11:30:49 -0400764#include "GrRenderTargetContext.h"
765
Herb Derby26cbe512018-05-24 14:39:01 -0400766std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
767 GrTextContext* textContext,
768 GrRenderTargetContext* rtc,
769 const SkPaint& skPaint,
770 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400771 const char* text,
772 int x,
773 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500774 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500775
776 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
777
778 size_t textLen = (int)strlen(text);
779
780 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
781
782 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
783 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000784
785 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400786 SkGlyphRunBuilder builder;
787 builder.prepareDrawText(skPaint, text, textLen, origin);
788 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000789
Herb Derby9d85d632018-06-18 16:25:52 -0400790 auto glyphRun = builder.useGlyphRun();
Herb Derby59d997a2018-06-07 12:44:09 -0400791 // Use the text and textLen below, because we don't want to mess with the paint.
Herb Derby9d85d632018-06-18 16:25:52 -0400792 glyphRun->temporaryShuntToCallback(
Herb Derby59d997a2018-06-07 12:44:09 -0400793 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
794 blob = textContext->makeDrawPosTextBlob(
795 context->contextPriv().getTextBlobCache(), glyphCache,
796 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
797 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps, text,
798 textLen, pos, 2, origin);
799 });
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500800
801 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500802 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500803}
804
Brian Salomon44acb5b2017-07-18 19:59:24 -0400805GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700806 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400807 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700808 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700809
810 if (context->uniqueID() != gContextID) {
811 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400812 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700813 }
814
Brian Osman11052242016-10-27 14:47:55 -0400815 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500816 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400817 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700818
joshualitt6c891102015-05-13 08:51:49 -0700819 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400820
821 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
822 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700823 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500824 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700825 skPaint.setLCDRenderText(random->nextBool());
826 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
827 skPaint.setSubpixelText(random->nextBool());
828
joshualitt79dfb2b2015-05-11 08:58:08 -0700829 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700830
joshualittb8d86492016-02-24 09:23:03 -0800831 // create some random x/y offsets, including negative offsets
832 static const int kMaxTrans = 1024;
833 int xPos = (random->nextU() % 2) * 2 - 1;
834 int yPos = (random->nextU() % 2) * 2 - 1;
835 int xInt = (random->nextU() % kMaxTrans) * xPos;
836 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700837
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500838 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
839 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700840}
841
842#endif