blob: 28204eb7af6431ad2018781f5473edafbdca9967 [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
Robert Phillips38580452018-06-28 12:00:35 +000087// TODO if this function ever shows up in profiling, then we can compute this value when the
88// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
89// run so this is not a big deal to compute here.
90bool GrTextContext::HasLCD(const SkTextBlob* blob) {
91 SkTextBlobRunIterator it(blob);
92 for (; !it.done(); it.next()) {
93 if (it.isLCD()) {
94 return true;
95 }
96 }
97 return false;
98}
joshualitt9e36c1a2015-04-14 12:17:27 -070099
Robert Phillips38580452018-06-28 12:00:35 +0000100void GrTextContext::drawTextBlob(GrContext* context, GrTextUtils::Target* target,
101 const GrClip& clip, const SkPaint& skPaint,
102 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
103 const SkTextBlob* blob, SkScalar x, SkScalar y,
Ben Wagner2c312c42018-06-27 14:46:46 -0400104 const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700105 // If we have been abandoned, then don't draw
Khushalc421ca12018-06-26 14:38:34 -0700106 if (context->abandoned()) {
robertphillipsea461502015-05-26 11:38:03 -0700107 return;
108 }
109
Robert Phillips38580452018-06-28 12:00:35 +0000110 sk_sp<GrTextBlob> cacheBlob;
Mike Reed80747ef2018-01-23 15:29:32 -0500111 SkMaskFilterBase::BlurRec blurRec;
Robert Phillips38580452018-06-28 12:00:35 +0000112 GrTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700113 // It might be worth caching these things, but its not clear at this time
114 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
115 const SkMaskFilter* mf = skPaint.getMaskFilter();
Ben Wagner2c312c42018-06-27 14:46:46 -0400116 bool canCache = !(skPaint.getPathEffect() || (mf && !as_MFB(mf)->asABlur(&blurRec)));
Herb Derbyd8327a82018-01-22 14:39:27 -0500117 SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
joshualitt2a0e9f32015-04-13 06:12:21 -0700118
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500119 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500120 GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache();
121
joshualitt2a0e9f32015-04-13 06:12:21 -0700122 if (canCache) {
Robert Phillips38580452018-06-28 12:00:35 +0000123 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700124
125 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
joshualitt2c89bc12016-02-11 05:42:30 -0800126 SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() :
Robert Phillips38580452018-06-28 12:00:35 +0000127 kUnknown_SkPixelGeometry;
joshualitte4cee1f2015-05-11 13:04:28 -0700128
joshualitt9e36c1a2015-04-14 12:17:27 -0700129 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
130 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
131 // ensure we always match the same key
132 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
Robert Phillips38580452018-06-28 12:00:35 +0000133 ComputeCanonicalColor(skPaint, hasLCD);
joshualitt9e36c1a2015-04-14 12:17:27 -0700134
joshualitte4cee1f2015-05-11 13:04:28 -0700135 key.fPixelGeometry = pixelGeometry;
Robert Phillips38580452018-06-28 12:00:35 +0000136 key.fUniqueID = blob->uniqueID();
joshualitt53b5f442015-04-13 06:33:59 -0700137 key.fStyle = skPaint.getStyle();
138 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700139 key.fCanonicalColor = canonicalColor;
brianosman8d7ffce2016-04-21 08:29:06 -0700140 key.fScalerContextFlags = scalerContextFlags;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500141 cacheBlob = textBlobCache->find(key);
joshualitt2a0e9f32015-04-13 06:12:21 -0700142 }
143
Brian Salomonf18b1d82017-10-27 11:30:49 -0400144 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
joshualittb7133be2015-04-08 09:08:31 -0700145 if (cacheBlob) {
Robert Phillips38580452018-06-28 12:00:35 +0000146 if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700147 // We have to remake the blob because changes may invalidate our masks.
148 // TODO we could probably get away reuse most of the time if the pointer is unique,
149 // but we'd have to clear the subrun information
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500150 textBlobCache->remove(cacheBlob.get());
Robert Phillips38580452018-06-28 12:00:35 +0000151 cacheBlob = textBlobCache->makeCachedBlob(blob, key, blurRec, skPaint);
152 this->regenerateTextBlob(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400153 *context->contextPriv().caps()->shaderCaps(), paint,
Ben Wagner2c312c42018-06-27 14:46:46 -0400154 scalerContextFlags, viewMatrix, props, blob, x, y);
joshualittb7133be2015-04-08 09:08:31 -0700155 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500156 textBlobCache->makeMRU(cacheBlob.get());
joshualitt2f2ee832016-02-10 08:52:24 -0800157
158 if (CACHE_SANITY_CHECK) {
Robert Phillips38580452018-06-28 12:00:35 +0000159 int glyphCount = 0;
160 int runCount = 0;
161 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
Herb Derby86240592018-05-24 16:12:31 -0400162 sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
joshualitt92303772016-02-10 11:55:52 -0800163 sanityBlob->setupKey(key, blurRec, skPaint);
Robert Phillips38580452018-06-28 12:00:35 +0000164 this->regenerateTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400165 sanityBlob.get(), glyphCache, *context->contextPriv().caps()->shaderCaps(),
Ben Wagner2c312c42018-06-27 14:46:46 -0400166 paint, scalerContextFlags, viewMatrix, props, blob, x, y);
Herb Derby86240592018-05-24 16:12:31 -0400167 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700168 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700169 }
170 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700171 if (canCache) {
Robert Phillips38580452018-06-28 12:00:35 +0000172 cacheBlob = textBlobCache->makeCachedBlob(blob, key, blurRec, skPaint);
joshualitt2a0e9f32015-04-13 06:12:21 -0700173 } else {
Robert Phillips38580452018-06-28 12:00:35 +0000174 cacheBlob = textBlobCache->makeBlob(blob);
joshualitt2a0e9f32015-04-13 06:12:21 -0700175 }
Robert Phillips38580452018-06-28 12:00:35 +0000176 this->regenerateTextBlob(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400177 *context->contextPriv().caps()->shaderCaps(), paint,
Ben Wagner2c312c42018-06-27 14:46:46 -0400178 scalerContextFlags, viewMatrix, props, blob, x, y);
joshualitt1d89e8d2015-04-01 12:40:54 -0700179 }
180
Robert Phillips5a66efb2018-03-07 15:13:18 -0500181 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Robert Phillips38580452018-06-28 12:00:35 +0000182 clip, viewMatrix, clipBounds, x, y);
joshualitt1d89e8d2015-04-01 12:40:54 -0700183}
184
Robert Phillips38580452018-06-28 12:00:35 +0000185void GrTextContext::regenerateTextBlob(GrTextBlob* cacheBlob,
Herb Derby26cbe512018-05-24 14:39:01 -0400186 GrGlyphCache* glyphCache,
187 const GrShaderCaps& shaderCaps,
188 const GrTextUtils::Paint& paint,
189 SkScalerContextFlags scalerContextFlags,
190 const SkMatrix& viewMatrix,
Robert Phillips38580452018-06-28 12:00:35 +0000191 const SkSurfaceProps& props, const SkTextBlob* blob,
Ben Wagner2c312c42018-06-27 14:46:46 -0400192 SkScalar x, SkScalar y) const {
Robert Phillips38580452018-06-28 12:00:35 +0000193 cacheBlob->initReusableBlob(paint.luminanceColor(), viewMatrix, x, y);
joshualitt1d89e8d2015-04-01 12:40:54 -0700194
195 // Regenerate textblob
Robert Phillips38580452018-06-28 12:00:35 +0000196 SkTextBlobRunIterator it(blob);
Ben Wagner2c312c42018-06-27 14:46:46 -0400197 GrTextUtils::RunPaint runPaint(&paint);
joshualitt1d89e8d2015-04-01 12:40:54 -0700198 for (int run = 0; !it.done(); it.next(), run++) {
199 int glyphCount = it.glyphCount();
200 size_t textLen = glyphCount * sizeof(uint16_t);
Robert Phillips38580452018-06-28 12:00:35 +0000201 const SkPoint& offset = it.offset();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500202 cacheBlob->push_back_run(run);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500203 if (!runPaint.modifyForRun([it](SkPaint* p) { it.applyFontToPaint(p); })) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700204 continue;
205 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500206 cacheBlob->setRunPaintFlags(run, runPaint.skPaint().getFlags());
207
Khushal3e7548c2018-05-23 15:45:01 -0700208 if (CanDrawAsDistanceFields(runPaint, viewMatrix, props,
209 shaderCaps.supportsDistanceFieldText(), fOptions)) {
Robert Phillips38580452018-06-28 12:00:35 +0000210 switch (it.positioning()) {
211 case SkTextBlob::kDefault_Positioning: {
212 auto origin = SkPoint::Make(x + offset.x(), y + offset.y());
213 SkGlyphRunBuilder builder;
214 builder.prepareDrawText(runPaint.skPaint(),
215 (const char*)it.glyphs(), textLen, origin);
216
217 auto glyphRun = builder.useGlyphRun();
218
219 glyphRun->temporaryShuntToCallback(
220 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
221 this->drawDFPosText(
222 cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
223 viewMatrix, glyphIDs, 2 * runSize, pos, 2,
224 SkPoint::Make(0,0));
225 });
226 break;
227 }
228
229 case SkTextBlob::kHorizontal_Positioning: {
230 SkPoint dfOffset = SkPoint::Make(x, y + offset.y());
231 this->drawDFPosText(cacheBlob, run, glyphCache, props, runPaint,
232 scalerContextFlags, viewMatrix, (const char*)it.glyphs(),
233 textLen, it.pos(), 1, dfOffset);
234 break;
235 }
236 case SkTextBlob::kFull_Positioning: {
237 SkPoint dfOffset = SkPoint::Make(x, y);
238 this->drawDFPosText(cacheBlob, run, glyphCache, props, runPaint,
239 scalerContextFlags, viewMatrix, (const char*)it.glyphs(),
240 textLen, it.pos(), 2, dfOffset);
241 break;
242 }
243 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700244 } else {
Robert Phillips38580452018-06-28 12:00:35 +0000245 switch (it.positioning()) {
246 case SkTextBlob::kDefault_Positioning: {
247 auto origin = SkPoint::Make(x + offset.x(), y + offset.y());
248 SkGlyphRunBuilder builder;
249 builder.prepareDrawText(runPaint.skPaint(),
250 (const char*)it.glyphs(), textLen, origin);
251
252 auto glyphRun = builder.useGlyphRun();
253
254 glyphRun->temporaryShuntToCallback(
255 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
256 this->DrawBmpPosText(
257 cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
258 viewMatrix, glyphIDs, 2 * runSize,
259 pos, 2, SkPoint::Make(0, 0));
260 });
261 break;
262 }
263 case SkTextBlob::kHorizontal_Positioning:
264 DrawBmpPosText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
265 viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 1,
266 SkPoint::Make(x, y + offset.y()));
267 break;
268 case SkTextBlob::kFull_Positioning:
269 DrawBmpPosText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
270 viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 2,
271 SkPoint::Make(x, y));
272 break;
273 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700274 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700275 }
276}
277
Herb Derby86240592018-05-24 16:12:31 -0400278inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400279GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
280 GrGlyphCache* glyphCache,
281 const GrShaderCaps& shaderCaps,
282 const GrTextUtils::Paint& paint,
283 SkScalerContextFlags scalerContextFlags,
284 const SkMatrix& viewMatrix,
285 const SkSurfaceProps& props,
286 const char text[], size_t byteLength,
287 const SkScalar pos[], int scalarsPerPosition, const
288 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500289 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400290 if (!glyphCount) {
291 return nullptr;
292 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700293
Herb Derby86240592018-05-24 16:12:31 -0400294 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800295 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500296 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700297
Khushal3e7548c2018-05-23 15:45:01 -0700298 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
299 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500300 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500301 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700302 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500303 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
304 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700305 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700306 return blob;
307}
308
Herb Derby26cbe512018-05-24 14:39:01 -0400309void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
310 const GrClip& clip, const SkPaint& skPaint,
311 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
312 const char text[], size_t byteLength, const SkScalar pos[],
313 int scalarsPerPosition, const SkPoint& offset,
314 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400315 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Khushalc421ca12018-06-26 14:38:34 -0700316 if (context->abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800317 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500318 }
319
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500320 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500321 auto textBlobCache = context->contextPriv().getTextBlobCache();
322
Herb Derby86240592018-05-24 16:12:31 -0400323 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400324 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500325 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
326 byteLength, pos, scalarsPerPosition, offset));
327 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500328 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500329 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800330 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700331}
332
Robert Phillips38580452018-06-28 12:00:35 +0000333
Herb Derby86240592018-05-24 16:12:31 -0400334void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400335 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
336 const GrTextUtils::Paint& paint,
337 SkScalerContextFlags scalerContextFlags,
338 const SkMatrix& viewMatrix,
339 const char text[], size_t byteLength, const SkScalar pos[],
340 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500341 SkASSERT(byteLength == 0 || text != nullptr);
342 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
343
344 // nothing to draw
345 if (text == nullptr || byteLength == 0) {
346 return;
347 }
348
349 // Ensure the blob is set for bitmaptext
350 blob->setHasBitmap();
351
Jim Van Verth54d9c882018-02-08 16:14:48 -0500352 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500353 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500354 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500355 return;
356 }
357
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000358 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500359 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500360 SkFindAndPlaceGlyph::ProcessPosText(
361 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400362 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500363 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
364 position += rounding;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500365 BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500366 SkScalarFloorToScalar(position.fX),
367 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400368 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500369 });
Brian Salomon52db9402017-11-07 14:58:55 -0500370}
371
Herb Derby86240592018-05-24 16:12:31 -0400372void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400373 GrGlyphCache* glyphCache,
374 const SkSurfaceProps& props,
375 const GrTextUtils::Paint& origPaint,
376 SkScalerContextFlags scalerContextFlags,
377 const SkMatrix& viewMatrix,
378 const char text[], size_t byteLength,
379 const SkScalar pos[], int scalarsPerPosition,
380 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500381 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
382
383 // nothing to draw
384 if (text == nullptr || byteLength == 0) {
385 return;
386 }
387
388 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500389 SkPaint pathPaint(origPaint);
390 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700391 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
392 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500393
394 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500395 pathPaint.setStyle(SkPaint::kFill_Style);
396 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500397
Jim Van Verthc401bb92018-02-15 14:05:24 -0500398 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500399 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400400 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400401 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500402
403 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500404 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500405 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
406
407 while (text < stop) {
Herb Derby4e34a012018-03-21 10:47:30 -0400408 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500409 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500410 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400411 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500412 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
413 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
414 } else {
415 const SkPath* path = cache->findPath(glyph);
416 if (path) {
417 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
418 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500419 }
420 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500421 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500422 pos += scalarsPerPosition;
423 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500424
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500425 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500426}
427
Herb Derby86240592018-05-24 16:12:31 -0400428void GrTextContext::BmpAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400429 GrGlyphCache* grGlyphCache,
430 sk_sp<GrTextStrike>* strike,
431 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
432 GrColor color, SkGlyphCache* skGlyphCache,
433 SkScalar textRatio, bool needsTransform) {
Brian Salomon52db9402017-11-07 14:58:55 -0500434 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500435 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500436 }
437
438 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
439 skGlyph.getSubXFixed(),
440 skGlyph.getSubYFixed(),
441 GrGlyph::kCoverage_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500442 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500443 if (!glyph) {
444 return;
445 }
446
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500447 SkASSERT(skGlyph.fWidth == glyph->width());
448 SkASSERT(skGlyph.fHeight == glyph->height());
449
Jim Van Verthf4c13162018-01-11 16:40:24 -0500450 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft);
451 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop);
452 SkScalar width = SkIntToScalar(glyph->fBounds.width());
453 SkScalar height = SkIntToScalar(glyph->fBounds.height());
Brian Salomon52db9402017-11-07 14:58:55 -0500454
Jim Van Verthf4c13162018-01-11 16:40:24 -0500455 dx *= textRatio;
456 dy *= textRatio;
457 width *= textRatio;
458 height *= textRatio;
Brian Salomon52db9402017-11-07 14:58:55 -0500459
Jim Van Verthf4c13162018-01-11 16:40:24 -0500460 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500461
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500462 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400463 textRatio, !needsTransform);
Brian Salomon52db9402017-11-07 14:58:55 -0500464}
465
Herb Derby26cbe512018-05-24 14:39:01 -0400466void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700467 if (options->fMaxDistanceFieldFontSize < 0.f) {
468 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
469 }
470 if (options->fMinDistanceFieldFontSize < 0.f) {
471 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
472 }
473}
474
Herb Derby26cbe512018-05-24 14:39:01 -0400475bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
476 const SkSurfaceProps& props,
477 bool contextSupportsDistanceFieldText,
478 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500479 if (!viewMatrix.hasPerspective()) {
480 SkScalar maxScale = viewMatrix.getMaxScale();
481 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
482 // Hinted text looks far better at small resolutions
483 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700484 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
485 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500486 return false;
487 }
488
489 bool useDFT = props.isUseDeviceIndependentFonts();
490#if SK_FORCE_DISTANCE_FIELD_TEXT
491 useDFT = true;
492#endif
493
494 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
495 return false;
496 }
497 }
498
Mike Reed8ad91a92018-01-19 19:09:32 -0500499 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700500 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500501 return false;
502 }
503
504 // TODO: add some stroking support
505 if (skPaint.getStyle() != SkPaint::kFill_Style) {
506 return false;
507 }
508
509 return true;
510}
511
Herb Derby86240592018-05-24 16:12:31 -0400512void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400513 SkPaint* skPaint,
514 const SkMatrix& viewMatrix,
515 const Options& options,
516 SkScalar* textRatio,
517 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500518 SkScalar textSize = skPaint->getTextSize();
519 SkScalar scaledTextSize = textSize;
520
521 if (viewMatrix.hasPerspective()) {
522 // for perspective, we simply force to the medium size
523 // TODO: compute a size based on approximate screen area
524 scaledTextSize = kMediumDFFontLimit;
525 } else {
526 SkScalar maxScale = viewMatrix.getMaxScale();
527 // if we have non-unity scale, we need to choose our base text size
528 // based on the SkPaint's text size multiplied by the max scale factor
529 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
530 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
531 scaledTextSize *= maxScale;
532 }
533 }
534
535 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
536 // and ceiling. A scale outside of this range would require regenerating the distance fields
537 SkScalar dfMaskScaleFloor;
538 SkScalar dfMaskScaleCeil;
539 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700540 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500541 dfMaskScaleCeil = kSmallDFFontLimit;
542 *textRatio = textSize / kSmallDFFontSize;
543 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
544 } else if (scaledTextSize <= kMediumDFFontLimit) {
545 dfMaskScaleFloor = kSmallDFFontLimit;
546 dfMaskScaleCeil = kMediumDFFontLimit;
547 *textRatio = textSize / kMediumDFFontSize;
548 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
549 } else {
550 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700551 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500552 *textRatio = textSize / kLargeDFFontSize;
553 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
554 }
555
556 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
557 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
558 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
559 // tolerate before we'd have to move to a large mip size. When we actually test these values
560 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
561 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
562 // level)
563 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700564 if (blob) {
565 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
566 dfMaskScaleCeil / scaledTextSize);
567 }
Brian Salomon52db9402017-11-07 14:58:55 -0500568
569 skPaint->setAntiAlias(true);
570 skPaint->setLCDRenderText(false);
571 skPaint->setAutohinted(false);
572 skPaint->setHinting(SkPaint::kNormal_Hinting);
573 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400574
575 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700576
577 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
578 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
579 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500580}
581
Herb Derby86240592018-05-24 16:12:31 -0400582void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400583 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
584 const GrTextUtils::Paint& paint,
585 SkScalerContextFlags scalerContextFlags,
586 const SkMatrix& viewMatrix, const char text[],
587 size_t byteLength, const SkScalar pos[],
588 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500589 SkASSERT(byteLength == 0 || text != nullptr);
590 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
591
592 // nothing to draw
593 if (text == nullptr || byteLength == 0) {
594 return;
595 }
596
Khushal3e7548c2018-05-23 15:45:01 -0700597 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500598
Brian Salomon52db9402017-11-07 14:58:55 -0500599 // Setup distance field paint and text ratio
600 SkScalar textRatio;
601 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700602 SkScalerContextFlags flags;
603 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500604 blob->setHasDistanceField();
605 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500606 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500607
Khushalfa8ff092018-06-06 17:46:38 -0700608 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
609 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500610
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500611 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500612
Herb Derby526819d2018-03-09 12:51:12 -0500613 {
Khushal3e7548c2018-05-23 15:45:01 -0700614 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500615 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400616 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500617
Herb Derby526819d2018-03-09 12:51:12 -0500618 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500619
Herb Derby526819d2018-03-09 12:51:12 -0500620 while (text < stop) {
621 const char* lastText = text;
622 // the last 2 parameters are ignored
623 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
Brian Salomon52db9402017-11-07 14:58:55 -0500624
Herb Derby526819d2018-03-09 12:51:12 -0500625 if (glyph.fWidth) {
626 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400627 glyphPos.fX += pos[0];
628 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500629
Jim Van Verthd401da62018-05-03 10:40:30 -0400630 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby526819d2018-03-09 12:51:12 -0500631 DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
632 glyphPos.fY, paint.filteredPremulColor(), cache.get(), textRatio);
633 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400634 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500635 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
636 glyphPos);
637 }
Brian Salomon52db9402017-11-07 14:58:55 -0500638 }
Herb Derby526819d2018-03-09 12:51:12 -0500639 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500640 }
Brian Salomon52db9402017-11-07 14:58:55 -0500641 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000642
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500643 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500644}
645
Jim Van Verthf4c13162018-01-11 16:40:24 -0500646// TODO: merge with BmpAppendGlyph
Herb Derby86240592018-05-24 16:12:31 -0400647void GrTextContext::DfAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400648 GrGlyphCache* grGlyphCache, sk_sp<GrTextStrike>* strike,
649 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
650 GrColor color, SkGlyphCache* skGlyphCache,
651 SkScalar textRatio) {
Brian Salomon52db9402017-11-07 14:58:55 -0500652 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500653 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500654 }
655
656 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
657 skGlyph.getSubXFixed(),
658 skGlyph.getSubYFixed(),
659 GrGlyph::kDistance_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500660 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500661 if (!glyph) {
Jim Van Verthf4c13162018-01-11 16:40:24 -0500662 return;
Brian Salomon52db9402017-11-07 14:58:55 -0500663 }
664
665 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
666 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
667 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
668 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
669
Jim Van Verthf4c13162018-01-11 16:40:24 -0500670 dx *= textRatio;
671 dy *= textRatio;
672 width *= textRatio;
673 height *= textRatio;
674 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500675
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500676 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500677 textRatio, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500678}
679
joshualitt79dfb2b2015-05-11 08:58:08 -0700680///////////////////////////////////////////////////////////////////////////////////////////////////
681
Herb Derby26cbe512018-05-24 14:39:01 -0400682void GrTextContext::FallbackTextHelper::appendText(const SkGlyph& glyph, int count,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500683 const char* text, SkPoint glyphPos) {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400684 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
Khushalfa8ff092018-06-06 17:46:38 -0700685 if (SkScalarNearlyZero(maxDim)) return;
686
Jim Van Verthb515ae72018-05-23 16:44:55 -0400687 if (!fUseTransformedFallback) {
688 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
689 fUseTransformedFallback = true;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500690 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
Jim Van Verthc401bb92018-02-15 14:05:24 -0500691 }
692 }
693
694 fFallbackTxt.append(count, text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400695 if (fUseTransformedFallback) {
Jim Van Verth080a9282018-03-02 10:41:43 -0500696 // If there's a glyph in the font that's particularly large, it's possible
697 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
Jim Van Verth8b7284d2018-05-17 12:33:52 -0400698 // that glyph than make the others blurry, so we set a minimum size of half the
Jim Van Verth080a9282018-03-02 10:41:43 -0500699 // maximum text size to avoid this case.
Jim Van Verthb515ae72018-05-23 16:44:55 -0400700 SkScalar glyphTextSize = SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim),
Jim Van Verth080a9282018-03-02 10:41:43 -0500701 0.5f*fMaxTextSize);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400702 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500703 }
704 *fFallbackPos.append() = glyphPos;
705}
706
Herb Derby86240592018-05-24 16:12:31 -0400707void GrTextContext::FallbackTextHelper::drawText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400708 GrGlyphCache* glyphCache,
709 const SkSurfaceProps& props,
710 const GrTextUtils::Paint& paint,
711 SkScalerContextFlags scalerContextFlags) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500712 if (fFallbackTxt.count()) {
713 blob->initOverride(runIndex);
714 blob->setHasBitmap();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400715 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
Herb Derby526819d2018-03-09 12:51:12 -0500716 SkExclusiveStrikePtr cache;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500717 const SkPaint& skPaint = paint.skPaint();
718 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400719 SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500720 SkColor textColor = paint.filteredPremulColor();
Khushalfa8ff092018-06-06 17:46:38 -0700721
Jim Van Verthc401bb92018-02-15 14:05:24 -0500722 SkScalar textRatio = SK_Scalar1;
Khushalfa8ff092018-06-06 17:46:38 -0700723 SkPaint fallbackPaint(skPaint);
724 SkMatrix matrix = fViewMatrix;
725 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
726 cache = blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500727
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500728 sk_sp<GrTextStrike> currStrike;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500729 const char* text = fFallbackTxt.begin();
730 const char* stop = text + fFallbackTxt.count();
731 SkPoint* glyphPos = fFallbackPos.begin();
732 while (text < stop) {
Herb Derby526819d2018-03-09 12:51:12 -0500733 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400734 if (!fUseTransformedFallback) {
735 fViewMatrix.mapPoints(glyphPos, 1);
Jim Van Verth76e85162018-03-29 13:46:56 -0400736 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
737 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
738 }
Herb Derby26cbe512018-05-24 14:39:01 -0400739 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500740 glyphPos->fX, glyphPos->fY, textColor,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400741 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500742 glyphPos++;
743 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500744 }
745}
746
Khushalfa8ff092018-06-06 17:46:38 -0700747void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
748 SkMatrix* matrix) const {
749 if (!fUseTransformedFallback) return;
750
751 paint->setTextSize(fTransformedFallbackTextSize);
752 *textRatio = fTextSize / fTransformedFallbackTextSize;
753 *matrix = SkMatrix::I();
754}
755
Jim Van Verthc401bb92018-02-15 14:05:24 -0500756///////////////////////////////////////////////////////////////////////////////////////////////////
757
Hal Canary6f6961e2017-01-31 13:50:44 -0500758#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700759
Brian Salomonf18b1d82017-10-27 11:30:49 -0400760#include "GrRenderTargetContext.h"
761
Herb Derby26cbe512018-05-24 14:39:01 -0400762std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
763 GrTextContext* textContext,
764 GrRenderTargetContext* rtc,
765 const SkPaint& skPaint,
766 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400767 const char* text,
768 int x,
769 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500770 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500771
772 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
773
774 size_t textLen = (int)strlen(text);
775
776 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
777
778 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
779 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000780
781 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400782 SkGlyphRunBuilder builder;
783 builder.prepareDrawText(skPaint, text, textLen, origin);
784 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000785
Herb Derby9d85d632018-06-18 16:25:52 -0400786 auto glyphRun = builder.useGlyphRun();
Herb Derby59d997a2018-06-07 12:44:09 -0400787 // Use the text and textLen below, because we don't want to mess with the paint.
Herb Derby9d85d632018-06-18 16:25:52 -0400788 glyphRun->temporaryShuntToCallback(
Herb Derby59d997a2018-06-07 12:44:09 -0400789 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
790 blob = textContext->makeDrawPosTextBlob(
791 context->contextPriv().getTextBlobCache(), glyphCache,
792 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
793 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps, text,
794 textLen, pos, 2, origin);
795 });
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500796
797 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500798 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500799}
800
Brian Salomon44acb5b2017-07-18 19:59:24 -0400801GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700802 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400803 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700804 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700805
806 if (context->uniqueID() != gContextID) {
807 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400808 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700809 }
810
Brian Osman11052242016-10-27 14:47:55 -0400811 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500812 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400813 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700814
joshualitt6c891102015-05-13 08:51:49 -0700815 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400816
817 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
818 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700819 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500820 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700821 skPaint.setLCDRenderText(random->nextBool());
822 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
823 skPaint.setSubpixelText(random->nextBool());
824
joshualitt79dfb2b2015-05-11 08:58:08 -0700825 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700826
joshualittb8d86492016-02-24 09:23:03 -0800827 // create some random x/y offsets, including negative offsets
828 static const int kMaxTrans = 1024;
829 int xPos = (random->nextU() % 2) * 2 - 1;
830 int yPos = (random->nextU() % 2) * 2 - 1;
831 int xInt = (random->nextU() % kMaxTrans) * xPos;
832 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700833
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500834 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
835 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700836}
837
838#endif