blob: 3769b0b439b0af840a4f11948d5930673321bf02 [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 */
7#include "GrAtlasTextContext.h"
8
robertphillipsea461502015-05-26 11:38:03 -07009#include "GrDrawContext.h"
robertphillips2334fb62015-06-17 05:43:33 -070010#include "GrDrawTarget.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070011#include "GrFontScaler.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070012#include "GrStrokeInfo.h"
joshualittb7133be2015-04-08 09:08:31 -070013#include "GrTextBlobCache.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070014#include "GrTexturePriv.h"
joshualitt29677982015-12-11 06:08:59 -080015#include "GrTextUtils.h"
bsalomon72e3ae42015-04-28 08:08:46 -070016#include "GrVertexBuffer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070017
18#include "SkAutoKern.h"
19#include "SkColorPriv.h"
joshualitt9bd2daf2015-04-17 09:30:06 -070020#include "SkColorFilter.h"
21#include "SkDistanceFieldGen.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070022#include "SkDraw.h"
23#include "SkDrawFilter.h"
24#include "SkDrawProcs.h"
herb9be5ff62015-11-11 11:30:11 -080025#include "SkFindAndPlaceGlyph.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070026#include "SkGlyphCache.h"
27#include "SkGpuDevice.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070028#include "SkGrPriv.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070029#include "SkPath.h"
30#include "SkRTConf.h"
31#include "SkStrokeRec.h"
32#include "SkTextBlob.h"
33#include "SkTextMapStateProc.h"
34
joshualitta751c972015-11-20 13:37:32 -080035#include "batches/GrAtlasTextBatch.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070036
37namespace {
joshualitt9bd2daf2015-04-17 09:30:06 -070038static const int kMinDFFontSize = 18;
39static const int kSmallDFFontSize = 32;
40static const int kSmallDFFontLimit = 32;
41static const int kMediumDFFontSize = 72;
42static const int kMediumDFFontLimit = 72;
43static const int kLargeDFFontSize = 162;
jvanverth97c595f2015-06-19 11:06:28 -070044#ifdef SK_BUILD_FOR_ANDROID
45static const int kLargeDFFontLimit = 384;
46#else
joshualitta7c63892015-04-21 13:24:37 -070047static const int kLargeDFFontLimit = 2 * kLargeDFFontSize;
jvanverth97c595f2015-06-19 11:06:28 -070048#endif
joshualitt1d89e8d2015-04-01 12:40:54 -070049};
50
robertphillipsf6703fa2015-09-01 05:36:47 -070051GrAtlasTextContext::GrAtlasTextContext(GrContext* context, const SkSurfaceProps& surfaceProps)
52 : INHERITED(context, surfaceProps)
joshualitt1acabf32015-12-10 09:10:10 -080053 , fDistanceAdjustTable(new GrDistanceFieldAdjustTable) {
joshualittb7133be2015-04-08 09:08:31 -070054 // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest
55 // vertexStride
joshualittf528e0d2015-12-09 06:42:52 -080056 static_assert(GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kColorTextVASize &&
57 GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kLCDTextVASize,
bungeman99fe8222015-08-20 07:57:51 -070058 "vertex_attribute_changed");
halcanary96fcdcc2015-08-27 07:41:13 -070059 fCurrStrike = nullptr;
joshualittb7133be2015-04-08 09:08:31 -070060 fCache = context->getTextBlobCache();
joshualitt9bd2daf2015-04-17 09:30:06 -070061}
62
joshualitt1d89e8d2015-04-01 12:40:54 -070063
joshualittdbd35932015-04-02 09:19:04 -070064GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context,
robertphillipsfcf78292015-06-19 11:49:52 -070065 const SkSurfaceProps& surfaceProps) {
robertphillipsf6703fa2015-09-01 05:36:47 -070066 return new GrAtlasTextContext(context, surfaceProps);
joshualitt1d89e8d2015-04-01 12:40:54 -070067}
68
robertphillips6ee690e2015-12-02 08:57:50 -080069bool GrAtlasTextContext::canDraw(const SkPaint& skPaint, const SkMatrix& viewMatrix) {
joshualitt9bd2daf2015-04-17 09:30:06 -070070 return this->canDrawAsDistanceFields(skPaint, viewMatrix) ||
71 !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -070072}
73
joshualitt9e36c1a2015-04-14 12:17:27 -070074GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
75 GrColor canonicalColor = paint.computeLuminanceColor();
76 if (lcd) {
77 // This is the correct computation, but there are tons of cases where LCD can be overridden.
78 // For now we just regenerate if any run in a textblob has LCD.
79 // TODO figure out where all of these overrides are and see if we can incorporate that logic
80 // at a higher level *OR* use sRGB
81 SkASSERT(false);
82 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
83 } else {
84 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
85 // gamma corrected masks anyways, nor color
86 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
87 SkColorGetG(canonicalColor),
88 SkColorGetB(canonicalColor));
89 // reduce to our finite number of bits
90 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
91 }
92 return canonicalColor;
93}
94
95// TODO if this function ever shows up in profiling, then we can compute this value when the
96// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
97// run so this is not a big deal to compute here.
98bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -070099 SkTextBlobRunIterator it(blob);
joshualitt9e36c1a2015-04-14 12:17:27 -0700100 for (; !it.done(); it.next()) {
101 if (it.isLCD()) {
102 return true;
103 }
104 }
105 return false;
106}
107
joshualitt90e293d2015-12-28 07:14:52 -0800108inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run,
109 const SkPaint& skPaint,
110 const SkMatrix* viewMatrix,
111 bool noGamma) {
112 skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma);
113 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
114 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc());
115}
116
robertphillips433625e2015-12-04 06:58:16 -0800117void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700118 const GrClip& clip, const SkPaint& skPaint,
119 const SkMatrix& viewMatrix, const SkTextBlob* blob,
120 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700121 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700122 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700123 if (fContext->abandoned()) {
124 return;
125 }
126
joshualitt374b2f72015-07-21 08:05:03 -0700127 SkAutoTUnref<GrAtlasTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700128 SkMaskFilter::BlurRec blurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700129 GrAtlasTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700130 // It might be worth caching these things, but its not clear at this time
131 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
132 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700133 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700134 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700135 drawFilter);
136
137 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700138 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700139
140 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
robertphillipsfcf78292015-06-19 11:49:52 -0700141 SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700142 kUnknown_SkPixelGeometry;
143
joshualitt9e36c1a2015-04-14 12:17:27 -0700144 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
145 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
146 // ensure we always match the same key
147 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
148 ComputeCanonicalColor(skPaint, hasLCD);
149
joshualitte4cee1f2015-05-11 13:04:28 -0700150 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700151 key.fUniqueID = blob->uniqueID();
152 key.fStyle = skPaint.getStyle();
153 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700154 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700155 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700156 }
157
joshualitt2a0e9f32015-04-13 06:12:21 -0700158 SkScalar transX = 0.f;
159 SkScalar transY = 0.f;
160
joshualitt9e36c1a2015-04-14 12:17:27 -0700161 // Though for the time being runs in the textblob can override the paint, they only touch font
162 // info.
163 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700164 if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700165 return;
166 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700167
joshualittb7133be2015-04-08 09:08:31 -0700168 if (cacheBlob) {
joshualittfd5f6c12015-12-10 07:44:50 -0800169 if (cacheBlob->mustRegenerate(&transX, &transY, skPaint, grPaint.getColor(), blurRec,
170 viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700171 // We have to remake the blob because changes may invalidate our masks.
172 // TODO we could probably get away reuse most of the time if the pointer is unique,
173 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700174 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700175 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800176 GrAtlasTextBlob::kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700177 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800178 blob, x, y, drawFilter, clip);
joshualittb7133be2015-04-08 09:08:31 -0700179 } else {
180 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700181#ifdef CACHE_SANITY_CHECK
182 {
183 int glyphCount = 0;
184 int runCount = 0;
185 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
186 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
187 kGrayTextVASize));
188 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
189 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800190 blob, x, y, drawFilter, clip);
joshualitt259fbf12015-07-21 11:39:34 -0700191 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
192 }
193
194#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700195 }
196 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700197 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700198 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800199 GrAtlasTextBlob::kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700200 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800201 cacheBlob.reset(fCache->createBlob(blob, GrAtlasTextBlob::kGrayTextVASize));
joshualitt2a0e9f32015-04-13 06:12:21 -0700202 }
robertphillips9c240a12015-05-28 07:45:59 -0700203 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800204 blob, x, y, drawFilter, clip);
joshualitt1d89e8d2015-04-01 12:40:54 -0700205 }
206
joshualitt0a42e682015-12-10 13:20:58 -0800207 cacheBlob->flushCached(fContext, dc, blob, fSurfaceProps, fDistanceAdjustTable, skPaint,
joshualitt2e2202e2015-12-10 11:22:08 -0800208 grPaint, drawFilter, clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700209}
210
joshualitt9bd2daf2015-04-17 09:30:06 -0700211inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
212 const SkMatrix& viewMatrix) {
213 // TODO: support perspective (need getMaxScale replacement)
214 if (viewMatrix.hasPerspective()) {
215 return false;
216 }
217
218 SkScalar maxScale = viewMatrix.getMaxScale();
219 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
220 // Hinted text looks far better at small resolutions
221 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700222 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700223 return false;
224 }
225
bsalomonafcd7cd2015-08-31 12:39:41 -0700226 bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700227#if SK_FORCE_DISTANCE_FIELD_TEXT
228 useDFT = true;
229#endif
230
jvanverth4854d132015-06-22 06:46:56 -0700231 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700232 return false;
233 }
234
235 // rasterizers and mask filters modify alpha, which doesn't
236 // translate well to distance
237 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700238 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700239 return false;
240 }
241
242 // TODO: add some stroking support
243 if (skPaint.getStyle() != SkPaint::kFill_Style) {
244 return false;
245 }
246
247 return true;
248}
249
joshualitt374b2f72015-07-21 08:05:03 -0700250void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700251 const SkPaint& skPaint, GrColor color,
252 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700253 const SkTextBlob* blob, SkScalar x, SkScalar y,
robertphillips6ee690e2015-12-02 08:57:50 -0800254 SkDrawFilter* drawFilter,
255 const GrClip& clip) {
herbe59124e2015-11-18 10:54:39 -0800256 // The color here is the GrPaint color, and it is used to determine whether we
jvanverth0628a522015-08-18 07:44:22 -0700257 // have to regenerate LCD text blobs.
258 // We use this color vs the SkPaint color because it has the colorfilter applied.
259 cacheBlob->fPaintColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -0700260 cacheBlob->fViewMatrix = viewMatrix;
261 cacheBlob->fX = x;
262 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700263
264 // Regenerate textblob
265 SkPaint runPaint = skPaint;
halcanary33779752015-10-27 14:01:05 -0700266 SkTextBlobRunIterator it(blob);
joshualitt1d89e8d2015-04-01 12:40:54 -0700267 for (int run = 0; !it.done(); it.next(), run++) {
268 int glyphCount = it.glyphCount();
269 size_t textLen = glyphCount * sizeof(uint16_t);
270 const SkPoint& offset = it.offset();
271 // applyFontToPaint() always overwrites the exact same attributes,
272 // so it is safe to not re-seed the paint for this reason.
273 it.applyFontToPaint(&runPaint);
274
275 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
276 // A false return from filter() means we should abort the current draw.
277 runPaint = skPaint;
278 continue;
279 }
280
robertphillipsfcf78292015-06-19 11:49:52 -0700281 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700282
joshualitt18b072d2015-12-07 12:26:12 -0800283 cacheBlob->push_back_run(run);
joshualitt1d89e8d2015-04-01 12:40:54 -0700284
joshualittfcfb9fc2015-04-21 07:35:10 -0700285 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
286 cacheBlob->setHasDistanceField();
287 SkPaint dfPaint = runPaint;
288 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700289 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700290 Run& runIdx = cacheBlob->fRuns[run];
291 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800292 subRun.setUseLCDText(runPaint.isLCDRenderText());
293 subRun.setDrawAsDistanceFields();
joshualitt90e293d2015-12-28 07:14:52 -0800294 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], dfPaint, nullptr, true);
joshualitt9a27e632015-04-06 10:53:36 -0700295
joshualittfcfb9fc2015-04-21 07:35:10 -0700296 switch (it.positioning()) {
297 case SkTextBlob::kDefault_Positioning: {
joshualitt90e293d2015-12-28 07:14:52 -0800298 this->internalDrawDFText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700299 (const char *)it.glyphs(), textLen,
joshualitt5425a9a2015-12-11 11:05:43 -0800300 x + offset.x(), y + offset.y(), textRatio, runPaint);
joshualittfcfb9fc2015-04-21 07:35:10 -0700301 break;
302 }
303 case SkTextBlob::kHorizontal_Positioning: {
joshualitt5425a9a2015-12-11 11:05:43 -0800304 SkPoint dfOffset = SkPoint::Make(x, y + offset.y());
joshualitt90e293d2015-12-28 07:14:52 -0800305 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700306 (const char*)it.glyphs(), textLen, it.pos(),
joshualitt5425a9a2015-12-11 11:05:43 -0800307 1, dfOffset, textRatio,
308 runPaint);
joshualittfcfb9fc2015-04-21 07:35:10 -0700309 break;
310 }
311 case SkTextBlob::kFull_Positioning: {
joshualitt5425a9a2015-12-11 11:05:43 -0800312 SkPoint dfOffset = SkPoint::Make(x, y);
joshualitt90e293d2015-12-28 07:14:52 -0800313 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700314 (const char*)it.glyphs(), textLen, it.pos(),
joshualitt5425a9a2015-12-11 11:05:43 -0800315 2, dfOffset, textRatio, runPaint);
joshualittfcfb9fc2015-04-21 07:35:10 -0700316 break;
317 }
318 }
joshualitt90e293d2015-12-28 07:14:52 -0800319 SkGlyphCache::AttachCache(cache);
joshualittfcfb9fc2015-04-21 07:35:10 -0700320 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
321 cacheBlob->fRuns[run].fDrawAsPaths = true;
322 } else {
323 cacheBlob->setHasBitmap();
joshualitt90e293d2015-12-28 07:14:52 -0800324 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
325 false);
joshualittfcfb9fc2015-04-21 07:35:10 -0700326 switch (it.positioning()) {
327 case SkTextBlob::kDefault_Positioning:
joshualitt29677982015-12-11 06:08:59 -0800328 GrTextUtils::DrawBmpText(cacheBlob, run, fContext->getBatchFontCache(),
joshualitt90e293d2015-12-28 07:14:52 -0800329 cache, runPaint, color, viewMatrix,
joshualitt29677982015-12-11 06:08:59 -0800330 (const char *)it.glyphs(), textLen,
331 x + offset.x(), y + offset.y());
joshualittfcfb9fc2015-04-21 07:35:10 -0700332 break;
333 case SkTextBlob::kHorizontal_Positioning:
joshualitt29677982015-12-11 06:08:59 -0800334 GrTextUtils::DrawBmpPosText(cacheBlob, run, fContext->getBatchFontCache(),
joshualitt90e293d2015-12-28 07:14:52 -0800335 cache, runPaint, color, viewMatrix,
joshualitt29677982015-12-11 06:08:59 -0800336 (const char*)it.glyphs(), textLen, it.pos(), 1,
337 SkPoint::Make(x, y + offset.y()));
joshualittfcfb9fc2015-04-21 07:35:10 -0700338 break;
339 case SkTextBlob::kFull_Positioning:
joshualitt29677982015-12-11 06:08:59 -0800340 GrTextUtils::DrawBmpPosText(cacheBlob, run, fContext->getBatchFontCache(),
joshualitt90e293d2015-12-28 07:14:52 -0800341 cache, runPaint, color, viewMatrix,
joshualitt29677982015-12-11 06:08:59 -0800342 (const char*)it.glyphs(), textLen, it.pos(), 2,
343 SkPoint::Make(x, y));
joshualittfcfb9fc2015-04-21 07:35:10 -0700344 break;
345 }
joshualitt90e293d2015-12-28 07:14:52 -0800346 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700347 }
348
349 if (drawFilter) {
350 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
351 runPaint = skPaint;
352 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700353 }
354}
355
joshualitt374b2f72015-07-21 08:05:03 -0700356inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700357 SkPaint* skPaint,
358 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700359 const SkMatrix& viewMatrix) {
360 // getMaxScale doesn't support perspective, so neither do we at the moment
361 SkASSERT(!viewMatrix.hasPerspective());
362 SkScalar maxScale = viewMatrix.getMaxScale();
363 SkScalar textSize = skPaint->getTextSize();
364 SkScalar scaledTextSize = textSize;
365 // if we have non-unity scale, we need to choose our base text size
366 // based on the SkPaint's text size multiplied by the max scale factor
367 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
368 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
369 scaledTextSize *= maxScale;
370 }
371
joshualitt64c99cc2015-04-21 09:43:03 -0700372 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
373 // and ceiling. A scale outside of this range would require regenerating the distance fields
374 SkScalar dfMaskScaleFloor;
375 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700376 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700377 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700378 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700379 *textRatio = textSize / kSmallDFFontSize;
380 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
381 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700382 dfMaskScaleFloor = kSmallDFFontLimit;
383 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700384 *textRatio = textSize / kMediumDFFontSize;
385 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
386 } else {
joshualitta7c63892015-04-21 13:24:37 -0700387 dfMaskScaleFloor = kMediumDFFontLimit;
388 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700389 *textRatio = textSize / kLargeDFFontSize;
390 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
391 }
392
joshualitt64c99cc2015-04-21 09:43:03 -0700393 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
394 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
395 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
396 // tolerate before we'd have to move to a large mip size. When we actually test these values
397 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
398 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
399 // level)
joshualitta7c63892015-04-21 13:24:37 -0700400 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700401 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
402 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
403
joshualitt9bd2daf2015-04-17 09:30:06 -0700404 skPaint->setLCDRenderText(false);
405 skPaint->setAutohinted(false);
406 skPaint->setHinting(SkPaint::kNormal_Hinting);
407 skPaint->setSubpixelText(true);
408}
409
joshualitt374b2f72015-07-21 08:05:03 -0700410inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700411 int runIndex,
jvanverth0628a522015-08-18 07:44:22 -0700412 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700413 const SkPaint& skPaint,
414 const SkMatrix& viewMatrix,
415 const SkTDArray<char>& fallbackTxt,
416 const SkTDArray<SkScalar>& fallbackPos,
417 int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800418 const SkPoint& offset) {
joshualittfec19e12015-04-17 10:32:32 -0700419 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700420 blob->setHasBitmap();
421 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700422 // Push back a new subrun to fill and set the override descriptor
423 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700424 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitt90e293d2015-12-28 07:14:52 -0800425 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
426 fSurfaceProps, &viewMatrix, false);
427 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
428 run.fOverrideDescriptor->getDesc());
429 GrTextUtils::DrawBmpPosText(blob, runIndex, fContext->getBatchFontCache(), cache, skPaint,
430 color, viewMatrix, fallbackTxt.begin(), fallbackTxt.count(),
joshualitt29677982015-12-11 06:08:59 -0800431 fallbackPos.begin(), scalarsPerPosition, offset);
joshualitt90e293d2015-12-28 07:14:52 -0800432 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700433}
434
joshualitt374b2f72015-07-21 08:05:03 -0700435inline GrAtlasTextBlob*
herbe59124e2015-11-18 10:54:39 -0800436GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
437 const SkMatrix& viewMatrix, SkPaint* dfPaint,
jvanverth157e6482015-09-09 08:05:12 -0700438 SkScalar* textRatio) {
joshualittf528e0d2015-12-09 06:42:52 -0800439 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700440
441 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700442 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700443 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700444 Run& run = blob->fRuns[0];
445 PerSubRunInfo& subRun = run.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800446 subRun.setUseLCDText(origPaint.isLCDRenderText());
447 subRun.setDrawAsDistanceFields();
joshualitt9bd2daf2015-04-17 09:30:06 -0700448
joshualitt9bd2daf2015-04-17 09:30:06 -0700449 return blob;
450}
451
joshualitt374b2f72015-07-21 08:05:03 -0700452inline GrAtlasTextBlob*
joshualitt5425a9a2015-12-11 11:05:43 -0800453GrAtlasTextContext::createDrawTextBlob(const GrPaint& paint, const SkPaint& skPaint,
joshualitt79dfb2b2015-05-11 08:58:08 -0700454 const SkMatrix& viewMatrix,
455 const char text[], size_t byteLength,
joshualitt5425a9a2015-12-11 11:05:43 -0800456 SkScalar x, SkScalar y) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700457 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700458
joshualitt374b2f72015-07-21 08:05:03 -0700459 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700460 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
461 SkPaint dfPaint;
462 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700463 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt90e293d2015-12-28 07:14:52 -0800464 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], dfPaint, nullptr, true);
joshualitt1d89e8d2015-04-01 12:40:54 -0700465
joshualitt90e293d2015-12-28 07:14:52 -0800466 this->internalDrawDFText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
joshualitt5425a9a2015-12-11 11:05:43 -0800467 byteLength, x, y, textRatio, skPaint);
joshualitt90e293d2015-12-28 07:14:52 -0800468 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700469 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800470 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700471 blob->fViewMatrix = viewMatrix;
472
joshualitt90e293d2015-12-28 07:14:52 -0800473 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
474 GrTextUtils::DrawBmpText(blob, 0, fContext->getBatchFontCache(), cache, skPaint,
joshualitt29677982015-12-11 06:08:59 -0800475 paint.getColor(), viewMatrix, text, byteLength, x, y);
joshualitt90e293d2015-12-28 07:14:52 -0800476 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700477 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700478 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700479}
480
joshualitt374b2f72015-07-21 08:05:03 -0700481inline GrAtlasTextBlob*
joshualitt5425a9a2015-12-11 11:05:43 -0800482GrAtlasTextContext::createDrawPosTextBlob(const GrPaint& paint, const SkPaint& skPaint,
joshualitt79dfb2b2015-05-11 08:58:08 -0700483 const SkMatrix& viewMatrix,
484 const char text[], size_t byteLength,
485 const SkScalar pos[], int scalarsPerPosition,
joshualitt5425a9a2015-12-11 11:05:43 -0800486 const SkPoint& offset) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700487 int glyphCount = skPaint.countText(text, byteLength);
488
joshualitt374b2f72015-07-21 08:05:03 -0700489 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700490 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
491 SkPaint dfPaint;
492 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700493 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt90e293d2015-12-28 07:14:52 -0800494 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], dfPaint, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700495
joshualitt90e293d2015-12-28 07:14:52 -0800496 this->internalDrawDFPosText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
joshualitt5425a9a2015-12-11 11:05:43 -0800497 byteLength, pos, scalarsPerPosition, offset, textRatio,
498 skPaint);
joshualitt90e293d2015-12-28 07:14:52 -0800499 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700500 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800501 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700502 blob->fViewMatrix = viewMatrix;
joshualitt90e293d2015-12-28 07:14:52 -0800503 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
504 GrTextUtils::DrawBmpPosText(blob, 0, fContext->getBatchFontCache(), cache, skPaint,
joshualitt29677982015-12-11 06:08:59 -0800505 paint.getColor(), viewMatrix, text,
506 byteLength, pos, scalarsPerPosition, offset);
joshualitt90e293d2015-12-28 07:14:52 -0800507 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700508 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700509 return blob;
510}
511
robertphillips433625e2015-12-04 06:58:16 -0800512void GrAtlasTextContext::onDrawText(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -0800513 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700514 const GrPaint& paint, const SkPaint& skPaint,
515 const SkMatrix& viewMatrix,
516 const char text[], size_t byteLength,
517 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700518 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt5425a9a2015-12-11 11:05:43 -0800519 this->createDrawTextBlob(paint, skPaint, viewMatrix, text, byteLength, x, y));
joshualitt2e2202e2015-12-10 11:22:08 -0800520 blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint,
521 clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700522}
523
robertphillips433625e2015-12-04 06:58:16 -0800524void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700525 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700526 const GrPaint& paint, const SkPaint& skPaint,
527 const SkMatrix& viewMatrix,
528 const char text[], size_t byteLength,
529 const SkScalar pos[], int scalarsPerPosition,
530 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700531 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt5425a9a2015-12-11 11:05:43 -0800532 this->createDrawPosTextBlob(paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700533 text, byteLength,
534 pos, scalarsPerPosition,
joshualitt5425a9a2015-12-11 11:05:43 -0800535 offset));
joshualitt79dfb2b2015-05-11 08:58:08 -0700536
joshualitt2e2202e2015-12-10 11:22:08 -0800537 blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint, clip,
538 regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700539}
540
joshualitt374b2f72015-07-21 08:05:03 -0700541void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
joshualitt90e293d2015-12-28 07:14:52 -0800542 SkGlyphCache* cache,
jvanverth157e6482015-09-09 08:05:12 -0700543 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700544 const SkMatrix& viewMatrix,
545 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800546 SkScalar x, SkScalar y,
joshualitt9bd2daf2015-04-17 09:30:06 -0700547 SkScalar textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700548 const SkPaint& origPaint) {
halcanary96fcdcc2015-08-27 07:41:13 -0700549 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700550
551 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700552 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700553 return;
554 }
555
556 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
557 SkAutoDescriptor desc;
halcanary96fcdcc2015-08-27 07:41:13 -0700558 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
herbe59124e2015-11-18 10:54:39 -0800559 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
joshualitt9bd2daf2015-04-17 09:30:06 -0700560 desc.getDesc());
561
562 SkTArray<SkScalar> positions;
563
564 const char* textPtr = text;
565 SkFixed stopX = 0;
566 SkFixed stopY = 0;
567 SkFixed origin = 0;
568 switch (origPaint.getTextAlign()) {
569 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
570 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
571 case SkPaint::kLeft_Align: origin = 0; break;
572 }
573
574 SkAutoKern autokern;
575 const char* stop = text + byteLength;
576 while (textPtr < stop) {
577 // don't need x, y here, since all subpixel variants will have the
578 // same advance
579 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
580
581 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
582 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
583
584 SkFixed height = glyph.fAdvanceY;
585 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
586
587 stopX += width;
588 stopY += height;
589 }
590 SkASSERT(textPtr == stop);
591
jvanverth157e6482015-09-09 08:05:12 -0700592 SkGlyphCache::AttachCache(origPaintCache);
593
joshualitt9bd2daf2015-04-17 09:30:06 -0700594 // now adjust starting point depending on alignment
595 SkScalar alignX = SkFixedToScalar(stopX);
596 SkScalar alignY = SkFixedToScalar(stopY);
597 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
598 alignX = SkScalarHalf(alignX);
599 alignY = SkScalarHalf(alignY);
600 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
601 alignX = 0;
602 alignY = 0;
603 }
604 x -= alignX;
605 y -= alignY;
joshualitt5425a9a2015-12-11 11:05:43 -0800606 SkPoint offset = SkPoint::Make(x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700607
joshualitt90e293d2015-12-28 07:14:52 -0800608 this->internalDrawDFPosText(blob, runIndex, cache, skPaint, color, viewMatrix, text, byteLength,
joshualitt5425a9a2015-12-11 11:05:43 -0800609 positions.begin(), 2, offset, textRatio, origPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700610}
611
joshualitt374b2f72015-07-21 08:05:03 -0700612void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt90e293d2015-12-28 07:14:52 -0800613 SkGlyphCache* cache,
joshualitt5425a9a2015-12-11 11:05:43 -0800614 const SkPaint& skPaint,
615 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700616 const SkMatrix& viewMatrix,
617 const char text[], size_t byteLength,
618 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800619 const SkPoint& offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700620 SkScalar textRatio,
joshualitt5425a9a2015-12-11 11:05:43 -0800621 const SkPaint& origPaint) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700622
halcanary96fcdcc2015-08-27 07:41:13 -0700623 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700624 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
625
626 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700627 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700628 return;
629 }
630
joshualitt5425a9a2015-12-11 11:05:43 -0800631 SkTDArray<char> fallbackTxt;
632 SkTDArray<SkScalar> fallbackPos;
633
halcanary96fcdcc2015-08-27 07:41:13 -0700634 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -0700635
636 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
637 GrFontScaler* fontScaler = GetGrFontScaler(cache);
638
639 const char* stop = text + byteLength;
640
641 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
642 while (text < stop) {
643 const char* lastText = text;
644 // the last 2 parameters are ignored
645 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
646
647 if (glyph.fWidth) {
648 SkScalar x = offset.x() + pos[0];
649 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
650
651 if (!this->dfAppendGlyph(blob,
652 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700653 glyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800654 x, y, color, fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700655 textRatio, viewMatrix)) {
656 // couldn't append, send to fallback
joshualitt5425a9a2015-12-11 11:05:43 -0800657 fallbackTxt.append(SkToInt(text-lastText), lastText);
658 *fallbackPos.append() = pos[0];
joshualitt9bd2daf2015-04-17 09:30:06 -0700659 if (2 == scalarsPerPosition) {
joshualitt5425a9a2015-12-11 11:05:43 -0800660 *fallbackPos.append() = pos[1];
joshualitt9bd2daf2015-04-17 09:30:06 -0700661 }
662 }
663 }
664 pos += scalarsPerPosition;
665 }
666 } else {
667 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
668 : SK_Scalar1;
669 while (text < stop) {
670 const char* lastText = text;
671 // the last 2 parameters are ignored
672 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
673
674 if (glyph.fWidth) {
675 SkScalar x = offset.x() + pos[0];
676 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
677
678 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
679 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
680
681 if (!this->dfAppendGlyph(blob,
682 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700683 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700684 x - advanceX, y - advanceY, color,
685 fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700686 textRatio,
687 viewMatrix)) {
688 // couldn't append, send to fallback
joshualitt5425a9a2015-12-11 11:05:43 -0800689 fallbackTxt.append(SkToInt(text-lastText), lastText);
690 *fallbackPos.append() = pos[0];
joshualitt9bd2daf2015-04-17 09:30:06 -0700691 if (2 == scalarsPerPosition) {
joshualitt5425a9a2015-12-11 11:05:43 -0800692 *fallbackPos.append() = pos[1];
joshualitt9bd2daf2015-04-17 09:30:06 -0700693 }
694 }
695 }
696 pos += scalarsPerPosition;
697 }
698 }
jvanverth157e6482015-09-09 08:05:12 -0700699
joshualitt5425a9a2015-12-11 11:05:43 -0800700 if (fallbackTxt.count()) {
701 this->fallbackDrawPosText(blob, runIndex, origPaint.getColor(), origPaint, viewMatrix,
702 fallbackTxt, fallbackPos, scalarsPerPosition, offset);
703 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700704}
705
joshualitt374b2f72015-07-21 08:05:03 -0700706bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700707 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700708 SkScalar sx, SkScalar sy, GrColor color,
709 GrFontScaler* scaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700710 SkScalar textRatio, const SkMatrix& viewMatrix) {
711 if (!fCurrStrike) {
712 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
713 }
714
joshualitt6c2c2b02015-07-24 10:37:00 -0700715 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
716 skGlyph.getSubXFixed(),
717 skGlyph.getSubYFixed(),
718 GrGlyph::kDistance_MaskStyle);
719 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700720 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700721 return true;
722 }
723
724 // fallback to color glyph support
725 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
726 return false;
727 }
728
729 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
730 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
731 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
732 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
733
734 SkScalar scale = textRatio;
735 dx *= scale;
736 dy *= scale;
737 width *= scale;
738 height *= scale;
739 sx += dx;
740 sy += dy;
741 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
742
joshualitta06e6ab2015-12-10 08:54:41 -0800743 blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph, scaler, skGlyph,
744 sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700745 return true;
746}
747
joshualitt79dfb2b2015-05-11 08:58:08 -0700748///////////////////////////////////////////////////////////////////////////////////////////////////
749
750#ifdef GR_TEST_UTILS
751
bsalomonabd30f52015-08-13 13:34:48 -0700752DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700753 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -0700754 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -0700755 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700756
757 if (context->uniqueID() != gContextID) {
758 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -0700759 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -0700760
joshualitt79dfb2b2015-05-11 08:58:08 -0700761 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
762 // because we don't really want to have a gpu device here.
763 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -0700764 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -0700765 }
766
joshualitt79dfb2b2015-05-11 08:58:08 -0700767 // Setup dummy SkPaint / GrPaint
768 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -0700769 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -0700770 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -0700771 skPaint.setColor(color);
772 skPaint.setLCDRenderText(random->nextBool());
773 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
774 skPaint.setSubpixelText(random->nextBool());
775
776 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700777 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700778 SkFAIL("couldn't convert paint\n");
779 }
780
781 const char* text = "The quick brown fox jumps over the lazy dog.";
782 int textLen = (int)strlen(text);
783
784 // Setup clip
785 GrClip clip;
joshualitt79dfb2b2015-05-11 08:58:08 -0700786
787 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
788 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -0700789 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt5425a9a2015-12-11 11:05:43 -0800790 gTextContext->createDrawTextBlob(grPaint, skPaint, viewMatrix, text,
791 static_cast<size_t>(textLen), 0, 0));
joshualitt79dfb2b2015-05-11 08:58:08 -0700792
793 SkScalar transX = static_cast<SkScalar>(random->nextU());
794 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -0700795 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt2e2202e2015-12-10 11:22:08 -0800796 return blob->createBatch(info, textLen, 0, 0, color, transX, transY, skPaint,
797 gSurfaceProps, gTextContext->dfAdjustTable(),
798 context->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -0700799}
800
801#endif