blob: b63b7260798186cd8555bc24318c40eedc949ec5 [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) {
brianosman5280dcb2016-04-14 06:02:59 -070078 // If we're doing gamma-correct rendering, then we can disable the gamma hacks.
79 // Otherwise, leave them on. In either case, we still want the contrast boost:
Brian Salomonf3569f02017-10-24 12:52:33 -040080 if (colorSpaceInfo.isGammaCorrect()) {
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
joshualitt9e36c1a2015-04-14 12:17:27 -070087// 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.
Herb Derby26cbe512018-05-24 14:39:01 -040090bool GrTextContext::HasLCD(const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -070091 SkTextBlobRunIterator it(blob);
joshualitt9e36c1a2015-04-14 12:17:27 -070092 for (; !it.done(); it.next()) {
93 if (it.isLCD()) {
94 return true;
95 }
96 }
97 return false;
98}
99
Herb Derby26cbe512018-05-24 14:39:01 -0400100void 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,
104 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700105 // If we have been abandoned, then don't draw
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500106 if (context->contextPriv().abandoned()) {
robertphillipsea461502015-05-26 11:38:03 -0700107 return;
108 }
109
Herb Derby86240592018-05-24 16:12:31 -0400110 sk_sp<GrTextBlob> cacheBlob;
Mike Reed80747ef2018-01-23 15:29:32 -0500111 SkMaskFilterBase::BlurRec blurRec;
Herb Derby86240592018-05-24 16:12:31 -0400112 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();
joshualitt2a0e9f32015-04-13 06:12:21 -0700116 bool canCache = !(skPaint.getPathEffect() ||
Mike Reed80747ef2018-01-23 15:29:32 -0500117 (mf && !as_MFB(mf)->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700118 drawFilter);
Herb Derbyd8327a82018-01-22 14:39:27 -0500119 SkScalerContextFlags scalerContextFlags = ComputeScalerContextFlags(target->colorSpaceInfo());
joshualitt2a0e9f32015-04-13 06:12:21 -0700120
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500121 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500122 GrTextBlobCache* textBlobCache = context->contextPriv().getTextBlobCache();
123
joshualitt2a0e9f32015-04-13 06:12:21 -0700124 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700125 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700126
127 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
joshualitt2c89bc12016-02-11 05:42:30 -0800128 SkPixelGeometry pixelGeometry = hasLCD ? props.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700129 kUnknown_SkPixelGeometry;
130
joshualitt9e36c1a2015-04-14 12:17:27 -0700131 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
132 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
133 // ensure we always match the same key
134 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
135 ComputeCanonicalColor(skPaint, hasLCD);
136
joshualitte4cee1f2015-05-11 13:04:28 -0700137 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700138 key.fUniqueID = blob->uniqueID();
139 key.fStyle = skPaint.getStyle();
140 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700141 key.fCanonicalColor = canonicalColor;
brianosman8d7ffce2016-04-21 08:29:06 -0700142 key.fScalerContextFlags = scalerContextFlags;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500143 cacheBlob = textBlobCache->find(key);
joshualitt2a0e9f32015-04-13 06:12:21 -0700144 }
145
Brian Salomonf18b1d82017-10-27 11:30:49 -0400146 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
joshualittb7133be2015-04-08 09:08:31 -0700147 if (cacheBlob) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500148 if (cacheBlob->mustRegenerate(paint, blurRec, viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700149 // We have to remake the blob because changes may invalidate our masks.
150 // TODO we could probably get away reuse most of the time if the pointer is unique,
151 // but we'd have to clear the subrun information
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500152 textBlobCache->remove(cacheBlob.get());
153 cacheBlob = textBlobCache->makeCachedBlob(blob, key, blurRec, skPaint);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500154 this->regenerateTextBlob(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400155 *context->contextPriv().caps()->shaderCaps(), paint,
156 scalerContextFlags, viewMatrix, props, blob, x, y, drawFilter);
joshualittb7133be2015-04-08 09:08:31 -0700157 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500158 textBlobCache->makeMRU(cacheBlob.get());
joshualitt2f2ee832016-02-10 08:52:24 -0800159
160 if (CACHE_SANITY_CHECK) {
joshualitt259fbf12015-07-21 11:39:34 -0700161 int glyphCount = 0;
162 int runCount = 0;
163 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
Herb Derby86240592018-05-24 16:12:31 -0400164 sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
joshualitt92303772016-02-10 11:55:52 -0800165 sanityBlob->setupKey(key, blurRec, skPaint);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400166 this->regenerateTextBlob(
167 sanityBlob.get(), glyphCache, *context->contextPriv().caps()->shaderCaps(),
168 paint, scalerContextFlags, viewMatrix, props, blob, x, y, drawFilter);
Herb Derby86240592018-05-24 16:12:31 -0400169 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700170 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700171 }
172 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700173 if (canCache) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500174 cacheBlob = textBlobCache->makeCachedBlob(blob, key, blurRec, skPaint);
joshualitt2a0e9f32015-04-13 06:12:21 -0700175 } else {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500176 cacheBlob = textBlobCache->makeBlob(blob);
joshualitt2a0e9f32015-04-13 06:12:21 -0700177 }
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500178 this->regenerateTextBlob(cacheBlob.get(), glyphCache,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400179 *context->contextPriv().caps()->shaderCaps(), paint,
180 scalerContextFlags, viewMatrix, props, blob, x, y, drawFilter);
joshualitt1d89e8d2015-04-01 12:40:54 -0700181 }
182
Robert Phillips5a66efb2018-03-07 15:13:18 -0500183 cacheBlob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500184 clip, viewMatrix, clipBounds, x, y);
joshualitt1d89e8d2015-04-01 12:40:54 -0700185}
186
Herb Derby86240592018-05-24 16:12:31 -0400187void GrTextContext::regenerateTextBlob(GrTextBlob* cacheBlob,
Herb Derby26cbe512018-05-24 14:39:01 -0400188 GrGlyphCache* glyphCache,
189 const GrShaderCaps& shaderCaps,
190 const GrTextUtils::Paint& paint,
191 SkScalerContextFlags scalerContextFlags,
192 const SkMatrix& viewMatrix,
193 const SkSurfaceProps& props, const SkTextBlob* blob,
194 SkScalar x, SkScalar y,
195 SkDrawFilter* drawFilter) const {
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400196 cacheBlob->initReusableBlob(paint.luminanceColor(), viewMatrix, x, y);
joshualitt1d89e8d2015-04-01 12:40:54 -0700197
198 // Regenerate textblob
halcanary33779752015-10-27 14:01:05 -0700199 SkTextBlobRunIterator it(blob);
Ben Wagnerd234afd2018-04-13 15:50:01 -0400200 GrTextUtils::RunPaint runPaint(&paint, drawFilter);
joshualitt1d89e8d2015-04-01 12:40:54 -0700201 for (int run = 0; !it.done(); it.next(), run++) {
202 int glyphCount = it.glyphCount();
203 size_t textLen = glyphCount * sizeof(uint16_t);
204 const SkPoint& offset = it.offset();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500205 cacheBlob->push_back_run(run);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500206 if (!runPaint.modifyForRun([it](SkPaint* p) { it.applyFontToPaint(p); })) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700207 continue;
208 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500209 cacheBlob->setRunPaintFlags(run, runPaint.skPaint().getFlags());
210
Khushal3e7548c2018-05-23 15:45:01 -0700211 if (CanDrawAsDistanceFields(runPaint, viewMatrix, props,
212 shaderCaps.supportsDistanceFieldText(), fOptions)) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700213 switch (it.positioning()) {
214 case SkTextBlob::kDefault_Positioning: {
Herb Derby41f4f312018-06-06 17:45:53 +0000215 auto origin = SkPoint::Make(x + offset.x(), y + offset.y());
Herb Derby59d997a2018-06-07 12:44:09 -0400216 SkGlyphRunBuilder builder;
217 builder.prepareDrawText(runPaint.skPaint(),
218 (const char*)it.glyphs(), textLen, origin);
Herb Derby41f4f312018-06-06 17:45:53 +0000219
Herb Derby59d997a2018-06-07 12:44:09 -0400220 builder.temporaryShuntToCallback(
221 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
222 this->drawDFPosText(
223 cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
224 viewMatrix, glyphIDs, 2 * runSize, pos, 2,
225 SkPoint::Make(0,0));
226 });
joshualittfcfb9fc2015-04-21 07:35:10 -0700227 break;
228 }
Herb Derby41f4f312018-06-06 17:45:53 +0000229
joshualittfcfb9fc2015-04-21 07:35:10 -0700230 case SkTextBlob::kHorizontal_Positioning: {
joshualitt5425a9a2015-12-11 11:05:43 -0800231 SkPoint dfOffset = SkPoint::Make(x, y + offset.y());
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500232 this->drawDFPosText(cacheBlob, run, glyphCache, props, runPaint,
Brian Salomonaf597482017-11-07 16:23:34 -0500233 scalerContextFlags, viewMatrix, (const char*)it.glyphs(),
234 textLen, it.pos(), 1, dfOffset);
joshualittfcfb9fc2015-04-21 07:35:10 -0700235 break;
236 }
237 case SkTextBlob::kFull_Positioning: {
joshualitt5425a9a2015-12-11 11:05:43 -0800238 SkPoint dfOffset = SkPoint::Make(x, y);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500239 this->drawDFPosText(cacheBlob, run, glyphCache, props, runPaint,
Brian Salomonaf597482017-11-07 16:23:34 -0500240 scalerContextFlags, viewMatrix, (const char*)it.glyphs(),
241 textLen, it.pos(), 2, dfOffset);
joshualittfcfb9fc2015-04-21 07:35:10 -0700242 break;
243 }
244 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700245 } else {
joshualittfcfb9fc2015-04-21 07:35:10 -0700246 switch (it.positioning()) {
Herb Derby41f4f312018-06-06 17:45:53 +0000247 case SkTextBlob::kDefault_Positioning: {
248 auto origin = SkPoint::Make(x + offset.x(), y + offset.y());
Herb Derby59d997a2018-06-07 12:44:09 -0400249 SkGlyphRunBuilder builder;
250 builder.prepareDrawText(runPaint.skPaint(),
251 (const char*)it.glyphs(), textLen, origin);
Herb Derby41f4f312018-06-06 17:45:53 +0000252
Herb Derby59d997a2018-06-07 12:44:09 -0400253 builder.temporaryShuntToCallback(
254 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
255 this->DrawBmpPosText(
256 cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
257 viewMatrix, glyphIDs, 2 * runSize,
258 pos, 2, SkPoint::Make(0, 0));
259 });
joshualittfcfb9fc2015-04-21 07:35:10 -0700260 break;
Herb Derby41f4f312018-06-06 17:45:53 +0000261 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700262 case SkTextBlob::kHorizontal_Positioning:
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500263 DrawBmpPosText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
Brian Salomon52db9402017-11-07 14:58:55 -0500264 viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 1,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500265 SkPoint::Make(x, y + offset.y()));
joshualittfcfb9fc2015-04-21 07:35:10 -0700266 break;
267 case SkTextBlob::kFull_Positioning:
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500268 DrawBmpPosText(cacheBlob, run, glyphCache, props, runPaint, scalerContextFlags,
Brian Salomon52db9402017-11-07 14:58:55 -0500269 viewMatrix, (const char*)it.glyphs(), textLen, it.pos(), 2,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500270 SkPoint::Make(x, y));
joshualittfcfb9fc2015-04-21 07:35:10 -0700271 break;
272 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700273 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700274 }
275}
276
Herb Derby86240592018-05-24 16:12:31 -0400277inline sk_sp<GrTextBlob>
Herb Derby26cbe512018-05-24 14:39:01 -0400278GrTextContext::makeDrawPosTextBlob(GrTextBlobCache* blobCache,
279 GrGlyphCache* glyphCache,
280 const GrShaderCaps& shaderCaps,
281 const GrTextUtils::Paint& paint,
282 SkScalerContextFlags scalerContextFlags,
283 const SkMatrix& viewMatrix,
284 const SkSurfaceProps& props,
285 const char text[], size_t byteLength,
286 const SkScalar pos[], int scalarsPerPosition, const
287 SkPoint& offset) const {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500288 int glyphCount = paint.skPaint().countText(text, byteLength);
Brian Salomonb3f6ac42017-07-31 08:00:25 -0400289 if (!glyphCount) {
290 return nullptr;
291 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700292
Herb Derby86240592018-05-24 16:12:31 -0400293 sk_sp<GrTextBlob> blob = blobCache->makeBlob(glyphCount, 1);
joshualitt7481e752016-01-22 06:08:48 -0800294 blob->initThrowawayBlob(viewMatrix, offset.x(), offset.y());
Jim Van Verth54d9c882018-02-08 16:14:48 -0500295 blob->setRunPaintFlags(0, paint.skPaint().getFlags());
joshualitt9bd2daf2015-04-17 09:30:06 -0700296
Khushal3e7548c2018-05-23 15:45:01 -0700297 if (CanDrawAsDistanceFields(paint, viewMatrix, props, shaderCaps.supportsDistanceFieldText(),
298 fOptions)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500299 this->drawDFPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
Brian Salomonaf597482017-11-07 16:23:34 -0500300 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700301 } else {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500302 DrawBmpPosText(blob.get(), 0, glyphCache, props, paint, scalerContextFlags, viewMatrix,
303 text, byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700304 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700305 return blob;
306}
307
Herb Derby26cbe512018-05-24 14:39:01 -0400308void GrTextContext::drawPosText(GrContext* context, GrTextUtils::Target* target,
309 const GrClip& clip, const SkPaint& skPaint,
310 const SkMatrix& viewMatrix, const SkSurfaceProps& props,
311 const char text[], size_t byteLength, const SkScalar pos[],
312 int scalarsPerPosition, const SkPoint& offset,
313 const SkIRect& regionClipBounds) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400314 GrTextUtils::Paint paint(&skPaint, &target->colorSpaceInfo());
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500315 if (context->contextPriv().abandoned()) {
joshualitte55750e2016-02-10 12:52:21 -0800316 return;
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500317 }
318
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500319 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500320 auto textBlobCache = context->contextPriv().getTextBlobCache();
321
Herb Derby86240592018-05-24 16:12:31 -0400322 sk_sp<GrTextBlob> blob(this->makeDrawPosTextBlob(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400323 textBlobCache, glyphCache, *context->contextPriv().caps()->shaderCaps(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500324 ComputeScalerContextFlags(target->colorSpaceInfo()), viewMatrix, props, text,
325 byteLength, pos, scalarsPerPosition, offset));
326 if (blob) {
Robert Phillips5a66efb2018-03-07 15:13:18 -0500327 blob->flush(target, props, fDistanceAdjustTable.get(), paint,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500328 clip, viewMatrix, regionClipBounds, offset.fX, offset.fY);
joshualitte55750e2016-02-10 12:52:21 -0800329 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700330}
331
Brian Salomon52db9402017-11-07 14:58:55 -0500332
Herb Derby86240592018-05-24 16:12:31 -0400333void GrTextContext::DrawBmpPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400334 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
335 const GrTextUtils::Paint& paint,
336 SkScalerContextFlags scalerContextFlags,
337 const SkMatrix& viewMatrix,
338 const char text[], size_t byteLength, const SkScalar pos[],
339 int scalarsPerPosition, const SkPoint& offset) {
Brian Salomon52db9402017-11-07 14:58:55 -0500340 SkASSERT(byteLength == 0 || text != nullptr);
341 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
342
343 // nothing to draw
344 if (text == nullptr || byteLength == 0) {
345 return;
346 }
347
348 // Ensure the blob is set for bitmaptext
349 blob->setHasBitmap();
350
Jim Van Verth54d9c882018-02-08 16:14:48 -0500351 if (SkDraw::ShouldDrawTextAsPaths(paint, viewMatrix)) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500352 DrawBmpPosTextAsPaths(blob, runIndex, glyphCache, props, paint, scalerContextFlags,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500353 viewMatrix, text, byteLength, pos, scalarsPerPosition, offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500354 return;
355 }
356
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000357 sk_sp<GrTextStrike> currStrike;
Herb Derby526819d2018-03-09 12:51:12 -0500358 auto cache = blob->setupCache(runIndex, props, scalerContextFlags, paint, &viewMatrix);
Brian Salomon52db9402017-11-07 14:58:55 -0500359 SkFindAndPlaceGlyph::ProcessPosText(
360 paint.skPaint().getTextEncoding(), text, byteLength, offset, viewMatrix, pos,
Herb Derby1e7c6582018-05-21 16:10:17 -0400361 scalarsPerPosition, cache.get(),
Brian Salomon52db9402017-11-07 14:58:55 -0500362 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
363 position += rounding;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500364 BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500365 SkScalarFloorToScalar(position.fX),
366 SkScalarFloorToScalar(position.fY),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400367 paint.filteredPremulColor(), cache.get(), SK_Scalar1, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500368 });
Brian Salomon52db9402017-11-07 14:58:55 -0500369}
370
Herb Derby86240592018-05-24 16:12:31 -0400371void GrTextContext::DrawBmpPosTextAsPaths(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400372 GrGlyphCache* glyphCache,
373 const SkSurfaceProps& props,
374 const GrTextUtils::Paint& origPaint,
375 SkScalerContextFlags scalerContextFlags,
376 const SkMatrix& viewMatrix,
377 const char text[], size_t byteLength,
378 const SkScalar pos[], int scalarsPerPosition,
379 const SkPoint& offset) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500380 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
381
382 // nothing to draw
383 if (text == nullptr || byteLength == 0) {
384 return;
385 }
386
387 // setup our std paint, in hopes of getting hits in the cache
Jim Van Verthc401bb92018-02-15 14:05:24 -0500388 SkPaint pathPaint(origPaint);
389 SkScalar matrixScale = pathPaint.setupForAsPaths();
Khushalfa8ff092018-06-06 17:46:38 -0700390 FallbackTextHelper fallbackTextHelper(viewMatrix, origPaint, glyphCache->getGlyphSizeLimit(),
391 matrixScale);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500392
393 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
Jim Van Verthc401bb92018-02-15 14:05:24 -0500394 pathPaint.setStyle(SkPaint::kFill_Style);
395 pathPaint.setPathEffect(nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500396
Jim Van Verthc401bb92018-02-15 14:05:24 -0500397 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(pathPaint.getTextEncoding(),
Jim Van Verthc401bb92018-02-15 14:05:24 -0500398 true);
Herb Derbyfa996902018-04-18 11:36:12 -0400399 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derby4e34a012018-03-21 10:47:30 -0400400 pathPaint, &props, SkScalerContextFlags::kFakeGammaAndBoostContrast, nullptr);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500401
402 const char* stop = text + byteLength;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500403 const char* lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500404 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
405
406 while (text < stop) {
Herb Derby4e34a012018-03-21 10:47:30 -0400407 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500408 if (glyph.fWidth) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500409 SkPoint loc;
Herb Derby1e7c6582018-05-21 16:10:17 -0400410 tmsProc(pos, &loc);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500411 if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
412 fallbackTextHelper.appendText(glyph, text - lastText, lastText, loc);
413 } else {
414 const SkPath* path = cache->findPath(glyph);
415 if (path) {
416 blob->appendPathGlyph(runIndex, *path, loc.fX, loc.fY, matrixScale, false);
417 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500418 }
419 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500420 lastText = text;
Jim Van Verth54d9c882018-02-08 16:14:48 -0500421 pos += scalarsPerPosition;
422 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500423
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500424 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, origPaint, scalerContextFlags);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500425}
426
Herb Derby86240592018-05-24 16:12:31 -0400427void GrTextContext::BmpAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400428 GrGlyphCache* grGlyphCache,
429 sk_sp<GrTextStrike>* strike,
430 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
431 GrColor color, SkGlyphCache* skGlyphCache,
432 SkScalar textRatio, bool needsTransform) {
Brian Salomon52db9402017-11-07 14:58:55 -0500433 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500434 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500435 }
436
437 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
438 skGlyph.getSubXFixed(),
439 skGlyph.getSubYFixed(),
440 GrGlyph::kCoverage_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500441 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500442 if (!glyph) {
443 return;
444 }
445
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500446 SkASSERT(skGlyph.fWidth == glyph->width());
447 SkASSERT(skGlyph.fHeight == glyph->height());
448
Jim Van Verthf4c13162018-01-11 16:40:24 -0500449 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft);
450 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop);
451 SkScalar width = SkIntToScalar(glyph->fBounds.width());
452 SkScalar height = SkIntToScalar(glyph->fBounds.height());
Brian Salomon52db9402017-11-07 14:58:55 -0500453
Jim Van Verthf4c13162018-01-11 16:40:24 -0500454 dx *= textRatio;
455 dy *= textRatio;
456 width *= textRatio;
457 height *= textRatio;
Brian Salomon52db9402017-11-07 14:58:55 -0500458
Jim Van Verthf4c13162018-01-11 16:40:24 -0500459 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500460
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500461 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400462 textRatio, !needsTransform);
Brian Salomon52db9402017-11-07 14:58:55 -0500463}
464
Herb Derby26cbe512018-05-24 14:39:01 -0400465void GrTextContext::SanitizeOptions(Options* options) {
Khushal3e7548c2018-05-23 15:45:01 -0700466 if (options->fMaxDistanceFieldFontSize < 0.f) {
467 options->fMaxDistanceFieldFontSize = kDefaultMaxDistanceFieldFontSize;
468 }
469 if (options->fMinDistanceFieldFontSize < 0.f) {
470 options->fMinDistanceFieldFontSize = kDefaultMinDistanceFieldFontSize;
471 }
472}
473
Herb Derby26cbe512018-05-24 14:39:01 -0400474bool GrTextContext::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
475 const SkSurfaceProps& props,
476 bool contextSupportsDistanceFieldText,
477 const Options& options) {
Brian Salomon52db9402017-11-07 14:58:55 -0500478 if (!viewMatrix.hasPerspective()) {
479 SkScalar maxScale = viewMatrix.getMaxScale();
480 SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
481 // Hinted text looks far better at small resolutions
482 // Scaling up beyond 2x yields undesireable artifacts
Khushal3e7548c2018-05-23 15:45:01 -0700483 if (scaledTextSize < options.fMinDistanceFieldFontSize ||
484 scaledTextSize > options.fMaxDistanceFieldFontSize) {
Brian Salomon52db9402017-11-07 14:58:55 -0500485 return false;
486 }
487
488 bool useDFT = props.isUseDeviceIndependentFonts();
489#if SK_FORCE_DISTANCE_FIELD_TEXT
490 useDFT = true;
491#endif
492
493 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
494 return false;
495 }
496 }
497
Mike Reed8ad91a92018-01-19 19:09:32 -0500498 // mask filters modify alpha, which doesn't translate well to distance
Khushal3e7548c2018-05-23 15:45:01 -0700499 if (skPaint.getMaskFilter() || !contextSupportsDistanceFieldText) {
Brian Salomon52db9402017-11-07 14:58:55 -0500500 return false;
501 }
502
503 // TODO: add some stroking support
504 if (skPaint.getStyle() != SkPaint::kFill_Style) {
505 return false;
506 }
507
508 return true;
509}
510
Herb Derby86240592018-05-24 16:12:31 -0400511void GrTextContext::InitDistanceFieldPaint(GrTextBlob* blob,
Herb Derby26cbe512018-05-24 14:39:01 -0400512 SkPaint* skPaint,
513 const SkMatrix& viewMatrix,
514 const Options& options,
515 SkScalar* textRatio,
516 SkScalerContextFlags* flags) {
Brian Salomon52db9402017-11-07 14:58:55 -0500517 SkScalar textSize = skPaint->getTextSize();
518 SkScalar scaledTextSize = textSize;
519
520 if (viewMatrix.hasPerspective()) {
521 // for perspective, we simply force to the medium size
522 // TODO: compute a size based on approximate screen area
523 scaledTextSize = kMediumDFFontLimit;
524 } else {
525 SkScalar maxScale = viewMatrix.getMaxScale();
526 // if we have non-unity scale, we need to choose our base text size
527 // based on the SkPaint's text size multiplied by the max scale factor
528 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
529 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
530 scaledTextSize *= maxScale;
531 }
532 }
533
534 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
535 // and ceiling. A scale outside of this range would require regenerating the distance fields
536 SkScalar dfMaskScaleFloor;
537 SkScalar dfMaskScaleCeil;
538 if (scaledTextSize <= kSmallDFFontLimit) {
Khushal3e7548c2018-05-23 15:45:01 -0700539 dfMaskScaleFloor = options.fMinDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500540 dfMaskScaleCeil = kSmallDFFontLimit;
541 *textRatio = textSize / kSmallDFFontSize;
542 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
543 } else if (scaledTextSize <= kMediumDFFontLimit) {
544 dfMaskScaleFloor = kSmallDFFontLimit;
545 dfMaskScaleCeil = kMediumDFFontLimit;
546 *textRatio = textSize / kMediumDFFontSize;
547 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
548 } else {
549 dfMaskScaleFloor = kMediumDFFontLimit;
Khushal3e7548c2018-05-23 15:45:01 -0700550 dfMaskScaleCeil = options.fMaxDistanceFieldFontSize;
Brian Salomon52db9402017-11-07 14:58:55 -0500551 *textRatio = textSize / kLargeDFFontSize;
552 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
553 }
554
555 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
556 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
557 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
558 // tolerate before we'd have to move to a large mip size. When we actually test these values
559 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
560 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
561 // level)
562 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
Khushal3e7548c2018-05-23 15:45:01 -0700563 if (blob) {
564 blob->setMinAndMaxScale(dfMaskScaleFloor / scaledTextSize,
565 dfMaskScaleCeil / scaledTextSize);
566 }
Brian Salomon52db9402017-11-07 14:58:55 -0500567
568 skPaint->setAntiAlias(true);
569 skPaint->setLCDRenderText(false);
570 skPaint->setAutohinted(false);
571 skPaint->setHinting(SkPaint::kNormal_Hinting);
572 skPaint->setSubpixelText(true);
Jim Van Verthd401da62018-05-03 10:40:30 -0400573
574 skPaint->setMaskFilter(GrSDFMaskFilter::Make());
Khushal3e7548c2018-05-23 15:45:01 -0700575
576 // We apply the fake-gamma by altering the distance in the shader, so we ignore the
577 // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
578 *flags = SkScalerContextFlags::kNone;
Brian Salomon52db9402017-11-07 14:58:55 -0500579}
580
Herb Derby86240592018-05-24 16:12:31 -0400581void GrTextContext::drawDFPosText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400582 GrGlyphCache* glyphCache, const SkSurfaceProps& props,
583 const GrTextUtils::Paint& paint,
584 SkScalerContextFlags scalerContextFlags,
585 const SkMatrix& viewMatrix, const char text[],
586 size_t byteLength, const SkScalar pos[],
587 int scalarsPerPosition, const SkPoint& offset) const {
Brian Salomon52db9402017-11-07 14:58:55 -0500588 SkASSERT(byteLength == 0 || text != nullptr);
589 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
590
591 // nothing to draw
592 if (text == nullptr || byteLength == 0) {
593 return;
594 }
595
Khushal3e7548c2018-05-23 15:45:01 -0700596 bool hasWCoord = viewMatrix.hasPerspective() || fOptions.fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonb5086962017-12-13 10:59:33 -0500597
Brian Salomon52db9402017-11-07 14:58:55 -0500598 // Setup distance field paint and text ratio
599 SkScalar textRatio;
600 SkPaint dfPaint(paint);
Khushal3e7548c2018-05-23 15:45:01 -0700601 SkScalerContextFlags flags;
602 InitDistanceFieldPaint(blob, &dfPaint, viewMatrix, fOptions, &textRatio, &flags);
Brian Salomon52db9402017-11-07 14:58:55 -0500603 blob->setHasDistanceField();
604 blob->setSubRunHasDistanceFields(runIndex, paint.skPaint().isLCDRenderText(),
Brian Salomon5c6ac642017-12-19 11:09:32 -0500605 paint.skPaint().isAntiAlias(), hasWCoord);
Brian Salomon52db9402017-11-07 14:58:55 -0500606
Khushalfa8ff092018-06-06 17:46:38 -0700607 FallbackTextHelper fallbackTextHelper(viewMatrix, paint, glyphCache->getGlyphSizeLimit(),
608 textRatio);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500609
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500610 sk_sp<GrTextStrike> currStrike;
Brian Salomon52db9402017-11-07 14:58:55 -0500611
Herb Derby526819d2018-03-09 12:51:12 -0500612 {
Khushal3e7548c2018-05-23 15:45:01 -0700613 auto cache = blob->setupCache(runIndex, props, flags, dfPaint, nullptr);
Herb Derby526819d2018-03-09 12:51:12 -0500614 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400615 SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), true);
Brian Salomon52db9402017-11-07 14:58:55 -0500616
Herb Derby526819d2018-03-09 12:51:12 -0500617 const char* stop = text + byteLength;
Brian Salomon52db9402017-11-07 14:58:55 -0500618
Herb Derby526819d2018-03-09 12:51:12 -0500619 while (text < stop) {
620 const char* lastText = text;
621 // the last 2 parameters are ignored
622 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
Brian Salomon52db9402017-11-07 14:58:55 -0500623
Herb Derby526819d2018-03-09 12:51:12 -0500624 if (glyph.fWidth) {
625 SkPoint glyphPos(offset);
Herb Derby1e7c6582018-05-21 16:10:17 -0400626 glyphPos.fX += pos[0];
627 glyphPos.fY += (2 == scalarsPerPosition ? pos[1] : 0);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500628
Jim Van Verthd401da62018-05-03 10:40:30 -0400629 if (glyph.fMaskFormat == SkMask::kSDF_Format) {
Herb Derby526819d2018-03-09 12:51:12 -0500630 DfAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph, glyphPos.fX,
631 glyphPos.fY, paint.filteredPremulColor(), cache.get(), textRatio);
632 } else {
Jim Van Verthd401da62018-05-03 10:40:30 -0400633 // can't append non-SDF glyph to SDF batch, send to fallback
Herb Derby526819d2018-03-09 12:51:12 -0500634 fallbackTextHelper.appendText(glyph, SkToInt(text - lastText), lastText,
635 glyphPos);
636 }
Brian Salomon52db9402017-11-07 14:58:55 -0500637 }
Herb Derby526819d2018-03-09 12:51:12 -0500638 pos += scalarsPerPosition;
Brian Salomon52db9402017-11-07 14:58:55 -0500639 }
Brian Salomon52db9402017-11-07 14:58:55 -0500640 }
Herb Derbyab6fd7e2018-03-07 18:05:39 +0000641
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500642 fallbackTextHelper.drawText(blob, runIndex, glyphCache, props, paint, scalerContextFlags);
Brian Salomon52db9402017-11-07 14:58:55 -0500643}
644
Jim Van Verthf4c13162018-01-11 16:40:24 -0500645// TODO: merge with BmpAppendGlyph
Herb Derby86240592018-05-24 16:12:31 -0400646void GrTextContext::DfAppendGlyph(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400647 GrGlyphCache* grGlyphCache, sk_sp<GrTextStrike>* strike,
648 const SkGlyph& skGlyph, SkScalar sx, SkScalar sy,
649 GrColor color, SkGlyphCache* skGlyphCache,
650 SkScalar textRatio) {
Brian Salomon52db9402017-11-07 14:58:55 -0500651 if (!*strike) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500652 *strike = grGlyphCache->getStrike(skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500653 }
654
655 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
656 skGlyph.getSubXFixed(),
657 skGlyph.getSubYFixed(),
658 GrGlyph::kDistance_MaskStyle);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500659 GrGlyph* glyph = (*strike)->getGlyph(skGlyph, id, skGlyphCache);
Brian Salomon52db9402017-11-07 14:58:55 -0500660 if (!glyph) {
Jim Van Verthf4c13162018-01-11 16:40:24 -0500661 return;
Brian Salomon52db9402017-11-07 14:58:55 -0500662 }
663
664 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
665 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
666 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
667 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
668
Jim Van Verthf4c13162018-01-11 16:40:24 -0500669 dx *= textRatio;
670 dy *= textRatio;
671 width *= textRatio;
672 height *= textRatio;
673 SkRect glyphRect = SkRect::MakeXYWH(sx + dx, sy + dy, width, height);
Brian Salomon52db9402017-11-07 14:58:55 -0500674
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500675 blob->appendGlyph(runIndex, glyphRect, color, *strike, glyph, skGlyphCache, skGlyph, sx, sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500676 textRatio, false);
Brian Salomon52db9402017-11-07 14:58:55 -0500677}
678
joshualitt79dfb2b2015-05-11 08:58:08 -0700679///////////////////////////////////////////////////////////////////////////////////////////////////
680
Herb Derby26cbe512018-05-24 14:39:01 -0400681void GrTextContext::FallbackTextHelper::appendText(const SkGlyph& glyph, int count,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500682 const char* text, SkPoint glyphPos) {
Jim Van Verthb515ae72018-05-23 16:44:55 -0400683 SkScalar maxDim = SkTMax(glyph.fWidth, glyph.fHeight)*fTextRatio;
Khushalfa8ff092018-06-06 17:46:38 -0700684 if (SkScalarNearlyZero(maxDim)) return;
685
Jim Van Verthb515ae72018-05-23 16:44:55 -0400686 if (!fUseTransformedFallback) {
687 if (!fViewMatrix.isScaleTranslate() || maxDim*fMaxScale > fMaxTextSize) {
688 fUseTransformedFallback = true;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500689 fMaxTextSize -= 2; // Subtract 2 to account for the bilerp pad around the glyph
Jim Van Verthc401bb92018-02-15 14:05:24 -0500690 }
691 }
692
693 fFallbackTxt.append(count, text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400694 if (fUseTransformedFallback) {
Jim Van Verth080a9282018-03-02 10:41:43 -0500695 // If there's a glyph in the font that's particularly large, it's possible
696 // that fScaledFallbackTextSize may end up minimizing too much. We'd rather skip
Jim Van Verth8b7284d2018-05-17 12:33:52 -0400697 // that glyph than make the others blurry, so we set a minimum size of half the
Jim Van Verth080a9282018-03-02 10:41:43 -0500698 // maximum text size to avoid this case.
Jim Van Verthb515ae72018-05-23 16:44:55 -0400699 SkScalar glyphTextSize = SkTMax(SkScalarFloorToScalar(fTextSize * fMaxTextSize/maxDim),
Jim Van Verth080a9282018-03-02 10:41:43 -0500700 0.5f*fMaxTextSize);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400701 fTransformedFallbackTextSize = SkTMin(glyphTextSize, fTransformedFallbackTextSize);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500702 }
703 *fFallbackPos.append() = glyphPos;
704}
705
Herb Derby86240592018-05-24 16:12:31 -0400706void GrTextContext::FallbackTextHelper::drawText(GrTextBlob* blob, int runIndex,
Herb Derby26cbe512018-05-24 14:39:01 -0400707 GrGlyphCache* glyphCache,
708 const SkSurfaceProps& props,
709 const GrTextUtils::Paint& paint,
710 SkScalerContextFlags scalerContextFlags) {
Jim Van Verthc401bb92018-02-15 14:05:24 -0500711 if (fFallbackTxt.count()) {
712 blob->initOverride(runIndex);
713 blob->setHasBitmap();
Jim Van Verthb515ae72018-05-23 16:44:55 -0400714 blob->setSubRunHasW(runIndex, fViewMatrix.hasPerspective());
Herb Derby526819d2018-03-09 12:51:12 -0500715 SkExclusiveStrikePtr cache;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500716 const SkPaint& skPaint = paint.skPaint();
717 SkPaint::GlyphCacheProc glyphCacheProc =
Herb Derbyfcac00f2018-05-01 11:57:56 -0400718 SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), true);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500719 SkColor textColor = paint.filteredPremulColor();
Khushalfa8ff092018-06-06 17:46:38 -0700720
Jim Van Verthc401bb92018-02-15 14:05:24 -0500721 SkScalar textRatio = SK_Scalar1;
Khushalfa8ff092018-06-06 17:46:38 -0700722 SkPaint fallbackPaint(skPaint);
723 SkMatrix matrix = fViewMatrix;
724 this->initializeForDraw(&fallbackPaint, &textRatio, &matrix);
725 cache = blob->setupCache(runIndex, props, scalerContextFlags, fallbackPaint, &matrix);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500726
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500727 sk_sp<GrTextStrike> currStrike;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500728 const char* text = fFallbackTxt.begin();
729 const char* stop = text + fFallbackTxt.count();
730 SkPoint* glyphPos = fFallbackPos.begin();
731 while (text < stop) {
Herb Derby526819d2018-03-09 12:51:12 -0500732 const SkGlyph& glyph = glyphCacheProc(cache.get(), &text);
Jim Van Verthb515ae72018-05-23 16:44:55 -0400733 if (!fUseTransformedFallback) {
734 fViewMatrix.mapPoints(glyphPos, 1);
Jim Van Verth76e85162018-03-29 13:46:56 -0400735 glyphPos->fX = SkScalarFloorToScalar(glyphPos->fX);
736 glyphPos->fY = SkScalarFloorToScalar(glyphPos->fY);
737 }
Herb Derby26cbe512018-05-24 14:39:01 -0400738 GrTextContext::BmpAppendGlyph(blob, runIndex, glyphCache, &currStrike, glyph,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500739 glyphPos->fX, glyphPos->fY, textColor,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400740 cache.get(), textRatio, fUseTransformedFallback);
Jim Van Verthc401bb92018-02-15 14:05:24 -0500741 glyphPos++;
742 }
Jim Van Verthc401bb92018-02-15 14:05:24 -0500743 }
744}
745
Khushalfa8ff092018-06-06 17:46:38 -0700746void GrTextContext::FallbackTextHelper::initializeForDraw(SkPaint* paint, SkScalar* textRatio,
747 SkMatrix* matrix) const {
748 if (!fUseTransformedFallback) return;
749
750 paint->setTextSize(fTransformedFallbackTextSize);
751 *textRatio = fTextSize / fTransformedFallbackTextSize;
752 *matrix = SkMatrix::I();
753}
754
Jim Van Verthc401bb92018-02-15 14:05:24 -0500755///////////////////////////////////////////////////////////////////////////////////////////////////
756
Hal Canary6f6961e2017-01-31 13:50:44 -0500757#if GR_TEST_UTILS
joshualitt79dfb2b2015-05-11 08:58:08 -0700758
Brian Salomonf18b1d82017-10-27 11:30:49 -0400759#include "GrRenderTargetContext.h"
760
Herb Derby26cbe512018-05-24 14:39:01 -0400761std::unique_ptr<GrDrawOp> GrTextContext::createOp_TestingOnly(GrContext* context,
762 GrTextContext* textContext,
763 GrRenderTargetContext* rtc,
764 const SkPaint& skPaint,
765 const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -0400766 const char* text,
767 int x,
768 int y) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500769 auto glyphCache = context->contextPriv().getGlyphCache();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500770
771 static SkSurfaceProps surfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
772
773 size_t textLen = (int)strlen(text);
774
775 GrTextUtils::Paint utilsPaint(&skPaint, &rtc->colorSpaceInfo());
776
777 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only intend to
778 // test the text op with this unit test, that is okay.
Herb Derby41f4f312018-06-06 17:45:53 +0000779
780 auto origin = SkPoint::Make(x, y);
Herb Derby59d997a2018-06-07 12:44:09 -0400781 SkGlyphRunBuilder builder;
782 builder.prepareDrawText(skPaint, text, textLen, origin);
783 sk_sp<GrTextBlob> blob;
Herb Derby41f4f312018-06-06 17:45:53 +0000784
Herb Derby59d997a2018-06-07 12:44:09 -0400785 // Use the text and textLen below, because we don't want to mess with the paint.
786 builder.temporaryShuntToCallback(
787 [&](size_t runSize, const char* glyphIDs, const SkScalar* pos) {
788 blob = textContext->makeDrawPosTextBlob(
789 context->contextPriv().getTextBlobCache(), glyphCache,
790 *context->contextPriv().caps()->shaderCaps(), utilsPaint,
791 GrTextContext::kTextBlobOpScalerContextFlags, viewMatrix, surfaceProps, text,
792 textLen, pos, 2, origin);
793 });
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500794
795 return blob->test_makeOp(textLen, 0, 0, viewMatrix, x, y, utilsPaint, surfaceProps,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500796 textContext->dfAdjustTable(), rtc->textTarget());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500797}
798
Brian Salomon44acb5b2017-07-18 19:59:24 -0400799GR_DRAW_OP_TEST_DEFINE(GrAtlasTextOp) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700800 static uint32_t gContextID = SK_InvalidGenID;
Herb Derby26cbe512018-05-24 14:39:01 -0400801 static std::unique_ptr<GrTextContext> gTextContext;
robertphillipsfcf78292015-06-19 11:49:52 -0700802 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700803
804 if (context->uniqueID() != gContextID) {
805 gContextID = context->uniqueID();
Herb Derby26cbe512018-05-24 14:39:01 -0400806 gTextContext = GrTextContext::Make(GrTextContext::Options());
joshualitt79dfb2b2015-05-11 08:58:08 -0700807 }
808
Brian Osman11052242016-10-27 14:47:55 -0400809 // Setup dummy SkPaint / GrPaint / GrRenderTargetContext
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500810 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400811 SkBackingFit::kApprox, 1024, 1024, kRGBA_8888_GrPixelConfig, nullptr));
brianosman8fe485b2016-07-25 12:31:51 -0700812
joshualitt6c891102015-05-13 08:51:49 -0700813 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
Brian Salomon44acb5b2017-07-18 19:59:24 -0400814
815 // Because we the GrTextUtils::Paint requires an SkPaint for font info, we ignore the GrPaint
816 // param.
joshualitt79dfb2b2015-05-11 08:58:08 -0700817 SkPaint skPaint;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500818 skPaint.setColor(random->nextU());
joshualitt79dfb2b2015-05-11 08:58:08 -0700819 skPaint.setLCDRenderText(random->nextBool());
820 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
821 skPaint.setSubpixelText(random->nextBool());
822
joshualitt79dfb2b2015-05-11 08:58:08 -0700823 const char* text = "The quick brown fox jumps over the lazy dog.";
joshualitt79dfb2b2015-05-11 08:58:08 -0700824
joshualittb8d86492016-02-24 09:23:03 -0800825 // create some random x/y offsets, including negative offsets
826 static const int kMaxTrans = 1024;
827 int xPos = (random->nextU() % 2) * 2 - 1;
828 int yPos = (random->nextU() % 2) * 2 - 1;
829 int xInt = (random->nextU() % kMaxTrans) * xPos;
830 int yInt = (random->nextU() % kMaxTrans) * yPos;
halcanary9d524f22016-03-29 09:03:52 -0700831
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500832 return gTextContext->createOp_TestingOnly(context, gTextContext.get(), rtc.get(),
833 skPaint, viewMatrix, text, xInt, yInt);
joshualitt79dfb2b2015-05-11 08:58:08 -0700834}
835
836#endif