blob: 5e11eb58aca8fbd06072723b82fd847097821a6d [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"
bsalomon72e3ae42015-04-28 08:08:46 -070015#include "GrVertexBuffer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070016
17#include "SkAutoKern.h"
18#include "SkColorPriv.h"
joshualitt9bd2daf2015-04-17 09:30:06 -070019#include "SkColorFilter.h"
20#include "SkDistanceFieldGen.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070021#include "SkDraw.h"
22#include "SkDrawFilter.h"
23#include "SkDrawProcs.h"
herb9be5ff62015-11-11 11:30:11 -080024#include "SkFindAndPlaceGlyph.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070025#include "SkGlyphCache.h"
26#include "SkGpuDevice.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070027#include "SkGrPriv.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070028#include "SkPath.h"
29#include "SkRTConf.h"
30#include "SkStrokeRec.h"
31#include "SkTextBlob.h"
32#include "SkTextMapStateProc.h"
33
joshualitta751c972015-11-20 13:37:32 -080034#include "batches/GrAtlasTextBatch.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070035
36namespace {
joshualitt9bd2daf2015-04-17 09:30:06 -070037static const int kMinDFFontSize = 18;
38static const int kSmallDFFontSize = 32;
39static const int kSmallDFFontLimit = 32;
40static const int kMediumDFFontSize = 72;
41static const int kMediumDFFontLimit = 72;
42static const int kLargeDFFontSize = 162;
jvanverth97c595f2015-06-19 11:06:28 -070043#ifdef SK_BUILD_FOR_ANDROID
44static const int kLargeDFFontLimit = 384;
45#else
joshualitta7c63892015-04-21 13:24:37 -070046static const int kLargeDFFontLimit = 2 * kLargeDFFontSize;
jvanverth97c595f2015-06-19 11:06:28 -070047#endif
joshualitt1d89e8d2015-04-01 12:40:54 -070048};
49
robertphillipsf6703fa2015-09-01 05:36:47 -070050GrAtlasTextContext::GrAtlasTextContext(GrContext* context, const SkSurfaceProps& surfaceProps)
51 : INHERITED(context, surfaceProps)
joshualitt1acabf32015-12-10 09:10:10 -080052 , fDistanceAdjustTable(new GrDistanceFieldAdjustTable) {
joshualittb7133be2015-04-08 09:08:31 -070053 // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest
54 // vertexStride
joshualittf528e0d2015-12-09 06:42:52 -080055 static_assert(GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kColorTextVASize &&
56 GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kLCDTextVASize,
bungeman99fe8222015-08-20 07:57:51 -070057 "vertex_attribute_changed");
halcanary96fcdcc2015-08-27 07:41:13 -070058 fCurrStrike = nullptr;
joshualittb7133be2015-04-08 09:08:31 -070059 fCache = context->getTextBlobCache();
joshualitt9bd2daf2015-04-17 09:30:06 -070060}
61
joshualitt1d89e8d2015-04-01 12:40:54 -070062
joshualittdbd35932015-04-02 09:19:04 -070063GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context,
robertphillipsfcf78292015-06-19 11:49:52 -070064 const SkSurfaceProps& surfaceProps) {
robertphillipsf6703fa2015-09-01 05:36:47 -070065 return new GrAtlasTextContext(context, surfaceProps);
joshualitt1d89e8d2015-04-01 12:40:54 -070066}
67
robertphillips6ee690e2015-12-02 08:57:50 -080068bool GrAtlasTextContext::canDraw(const SkPaint& skPaint, const SkMatrix& viewMatrix) {
joshualitt9bd2daf2015-04-17 09:30:06 -070069 return this->canDrawAsDistanceFields(skPaint, viewMatrix) ||
70 !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -070071}
72
joshualitt9e36c1a2015-04-14 12:17:27 -070073GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
74 GrColor canonicalColor = paint.computeLuminanceColor();
75 if (lcd) {
76 // This is the correct computation, but there are tons of cases where LCD can be overridden.
77 // For now we just regenerate if any run in a textblob has LCD.
78 // TODO figure out where all of these overrides are and see if we can incorporate that logic
79 // at a higher level *OR* use sRGB
80 SkASSERT(false);
81 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
82 } else {
83 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
84 // gamma corrected masks anyways, nor color
85 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
86 SkColorGetG(canonicalColor),
87 SkColorGetB(canonicalColor));
88 // reduce to our finite number of bits
89 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
90 }
91 return canonicalColor;
92}
93
94// TODO if this function ever shows up in profiling, then we can compute this value when the
95// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
96// run so this is not a big deal to compute here.
97bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -070098 SkTextBlobRunIterator it(blob);
joshualitt9e36c1a2015-04-14 12:17:27 -070099 for (; !it.done(); it.next()) {
100 if (it.isLCD()) {
101 return true;
102 }
103 }
104 return false;
105}
106
joshualitt374b2f72015-07-21 08:05:03 -0700107inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run,
joshualittdbd35932015-04-02 09:19:04 -0700108 const SkPaint& skPaint,
joshualitt9bd2daf2015-04-17 09:30:06 -0700109 const SkMatrix* viewMatrix,
110 bool noGamma) {
robertphillipsfcf78292015-06-19 11:49:52 -0700111 skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma);
joshualitt1d89e8d2015-04-01 12:40:54 -0700112 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
113 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc());
114}
115
robertphillips433625e2015-12-04 06:58:16 -0800116void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700117 const GrClip& clip, const SkPaint& skPaint,
118 const SkMatrix& viewMatrix, const SkTextBlob* blob,
119 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700120 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700121 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700122 if (fContext->abandoned()) {
123 return;
124 }
125
joshualitt374b2f72015-07-21 08:05:03 -0700126 SkAutoTUnref<GrAtlasTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700127 SkMaskFilter::BlurRec blurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700128 GrAtlasTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700129 // It might be worth caching these things, but its not clear at this time
130 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
131 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700132 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700133 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700134 drawFilter);
135
136 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700137 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700138
139 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
robertphillipsfcf78292015-06-19 11:49:52 -0700140 SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700141 kUnknown_SkPixelGeometry;
142
joshualitt9e36c1a2015-04-14 12:17:27 -0700143 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
144 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
145 // ensure we always match the same key
146 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
147 ComputeCanonicalColor(skPaint, hasLCD);
148
joshualitte4cee1f2015-05-11 13:04:28 -0700149 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700150 key.fUniqueID = blob->uniqueID();
151 key.fStyle = skPaint.getStyle();
152 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700153 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700154 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700155 }
156
joshualitt2a0e9f32015-04-13 06:12:21 -0700157 SkScalar transX = 0.f;
158 SkScalar transY = 0.f;
159
joshualitt9e36c1a2015-04-14 12:17:27 -0700160 // Though for the time being runs in the textblob can override the paint, they only touch font
161 // info.
162 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700163 if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700164 return;
165 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700166
joshualittb7133be2015-04-08 09:08:31 -0700167 if (cacheBlob) {
joshualittfd5f6c12015-12-10 07:44:50 -0800168 if (cacheBlob->mustRegenerate(&transX, &transY, skPaint, grPaint.getColor(), blurRec,
169 viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700170 // We have to remake the blob because changes may invalidate our masks.
171 // TODO we could probably get away reuse most of the time if the pointer is unique,
172 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700173 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700174 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800175 GrAtlasTextBlob::kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700176 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800177 blob, x, y, drawFilter, clip);
joshualittb7133be2015-04-08 09:08:31 -0700178 } else {
179 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700180#ifdef CACHE_SANITY_CHECK
181 {
182 int glyphCount = 0;
183 int runCount = 0;
184 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
185 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
186 kGrayTextVASize));
187 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
188 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800189 blob, x, y, drawFilter, clip);
joshualitt259fbf12015-07-21 11:39:34 -0700190 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
191 }
192
193#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700194 }
195 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700196 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700197 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800198 GrAtlasTextBlob::kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700199 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800200 cacheBlob.reset(fCache->createBlob(blob, GrAtlasTextBlob::kGrayTextVASize));
joshualitt2a0e9f32015-04-13 06:12:21 -0700201 }
robertphillips9c240a12015-05-28 07:45:59 -0700202 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800203 blob, x, y, drawFilter, clip);
joshualitt1d89e8d2015-04-01 12:40:54 -0700204 }
205
joshualitt2e2202e2015-12-10 11:22:08 -0800206 cacheBlob->flushCached(blob, fContext, dc, this, fSurfaceProps, fDistanceAdjustTable, skPaint,
207 grPaint, drawFilter, clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700208}
209
joshualitt9bd2daf2015-04-17 09:30:06 -0700210inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
211 const SkMatrix& viewMatrix) {
212 // TODO: support perspective (need getMaxScale replacement)
213 if (viewMatrix.hasPerspective()) {
214 return false;
215 }
216
217 SkScalar maxScale = viewMatrix.getMaxScale();
218 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
219 // Hinted text looks far better at small resolutions
220 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700221 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700222 return false;
223 }
224
bsalomonafcd7cd2015-08-31 12:39:41 -0700225 bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700226#if SK_FORCE_DISTANCE_FIELD_TEXT
227 useDFT = true;
228#endif
229
jvanverth4854d132015-06-22 06:46:56 -0700230 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700231 return false;
232 }
233
234 // rasterizers and mask filters modify alpha, which doesn't
235 // translate well to distance
236 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700237 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700238 return false;
239 }
240
241 // TODO: add some stroking support
242 if (skPaint.getStyle() != SkPaint::kFill_Style) {
243 return false;
244 }
245
246 return true;
247}
248
joshualitt374b2f72015-07-21 08:05:03 -0700249void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700250 const SkPaint& skPaint, GrColor color,
251 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700252 const SkTextBlob* blob, SkScalar x, SkScalar y,
robertphillips6ee690e2015-12-02 08:57:50 -0800253 SkDrawFilter* drawFilter,
254 const GrClip& clip) {
herbe59124e2015-11-18 10:54:39 -0800255 // The color here is the GrPaint color, and it is used to determine whether we
jvanverth0628a522015-08-18 07:44:22 -0700256 // have to regenerate LCD text blobs.
257 // We use this color vs the SkPaint color because it has the colorfilter applied.
258 cacheBlob->fPaintColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -0700259 cacheBlob->fViewMatrix = viewMatrix;
260 cacheBlob->fX = x;
261 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700262
263 // Regenerate textblob
264 SkPaint runPaint = skPaint;
halcanary33779752015-10-27 14:01:05 -0700265 SkTextBlobRunIterator it(blob);
joshualitt1d89e8d2015-04-01 12:40:54 -0700266 for (int run = 0; !it.done(); it.next(), run++) {
267 int glyphCount = it.glyphCount();
268 size_t textLen = glyphCount * sizeof(uint16_t);
269 const SkPoint& offset = it.offset();
270 // applyFontToPaint() always overwrites the exact same attributes,
271 // so it is safe to not re-seed the paint for this reason.
272 it.applyFontToPaint(&runPaint);
273
274 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
275 // A false return from filter() means we should abort the current draw.
276 runPaint = skPaint;
277 continue;
278 }
279
robertphillipsfcf78292015-06-19 11:49:52 -0700280 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700281
joshualitt18b072d2015-12-07 12:26:12 -0800282 cacheBlob->push_back_run(run);
joshualitt1d89e8d2015-04-01 12:40:54 -0700283
joshualittfcfb9fc2015-04-21 07:35:10 -0700284 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
285 cacheBlob->setHasDistanceField();
286 SkPaint dfPaint = runPaint;
287 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700288 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700289 Run& runIdx = cacheBlob->fRuns[run];
290 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800291 subRun.setUseLCDText(runPaint.isLCDRenderText());
292 subRun.setDrawAsDistanceFields();
joshualitt9a27e632015-04-06 10:53:36 -0700293
joshualittfcfb9fc2015-04-21 07:35:10 -0700294 SkTDArray<char> fallbackTxt;
295 SkTDArray<SkScalar> fallbackPos;
296 SkPoint dfOffset;
297 int scalarsPerPosition = 2;
298 switch (it.positioning()) {
299 case SkTextBlob::kDefault_Positioning: {
jvanverth157e6482015-09-09 08:05:12 -0700300 this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700301 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800302 x + offset.x(), y + offset.y(), textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700303 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
304 break;
305 }
306 case SkTextBlob::kHorizontal_Positioning: {
307 scalarsPerPosition = 1;
308 dfOffset = SkPoint::Make(x, y + offset.y());
jvanverth157e6482015-09-09 08:05:12 -0700309 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700310 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800311 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700312 &fallbackTxt, &fallbackPos);
313 break;
314 }
315 case SkTextBlob::kFull_Positioning: {
316 dfOffset = SkPoint::Make(x, y);
jvanverth157e6482015-09-09 08:05:12 -0700317 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700318 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800319 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700320 &fallbackTxt, &fallbackPos);
321 break;
322 }
323 }
324 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800325 this->fallbackDrawPosText(cacheBlob, run, clip, color, runPaint, viewMatrix,
326 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset);
joshualittfcfb9fc2015-04-21 07:35:10 -0700327 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700328 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
329 cacheBlob->fRuns[run].fDrawAsPaths = true;
330 } else {
331 cacheBlob->setHasBitmap();
332 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
333 false);
334 switch (it.positioning()) {
335 case SkTextBlob::kDefault_Positioning:
336 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
337 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800338 x + offset.x(), y + offset.y());
joshualittfcfb9fc2015-04-21 07:35:10 -0700339 break;
340 case SkTextBlob::kHorizontal_Positioning:
341 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
342 (const char*)it.glyphs(), textLen, it.pos(), 1,
robertphillips6ee690e2015-12-02 08:57:50 -0800343 SkPoint::Make(x, y + offset.y()));
joshualittfcfb9fc2015-04-21 07:35:10 -0700344 break;
345 case SkTextBlob::kFull_Positioning:
346 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
347 (const char*)it.glyphs(), textLen, it.pos(), 2,
robertphillips6ee690e2015-12-02 08:57:50 -0800348 SkPoint::Make(x, y));
joshualittfcfb9fc2015-04-21 07:35:10 -0700349 break;
350 }
351 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700352 }
353
354 if (drawFilter) {
355 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
356 runPaint = skPaint;
357 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700358 }
359}
360
joshualitt374b2f72015-07-21 08:05:03 -0700361inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700362 SkPaint* skPaint,
363 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700364 const SkMatrix& viewMatrix) {
365 // getMaxScale doesn't support perspective, so neither do we at the moment
366 SkASSERT(!viewMatrix.hasPerspective());
367 SkScalar maxScale = viewMatrix.getMaxScale();
368 SkScalar textSize = skPaint->getTextSize();
369 SkScalar scaledTextSize = textSize;
370 // if we have non-unity scale, we need to choose our base text size
371 // based on the SkPaint's text size multiplied by the max scale factor
372 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
373 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
374 scaledTextSize *= maxScale;
375 }
376
joshualitt64c99cc2015-04-21 09:43:03 -0700377 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
378 // and ceiling. A scale outside of this range would require regenerating the distance fields
379 SkScalar dfMaskScaleFloor;
380 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700381 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700382 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700383 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700384 *textRatio = textSize / kSmallDFFontSize;
385 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
386 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700387 dfMaskScaleFloor = kSmallDFFontLimit;
388 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700389 *textRatio = textSize / kMediumDFFontSize;
390 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
391 } else {
joshualitta7c63892015-04-21 13:24:37 -0700392 dfMaskScaleFloor = kMediumDFFontLimit;
393 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700394 *textRatio = textSize / kLargeDFFontSize;
395 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
396 }
397
joshualitt64c99cc2015-04-21 09:43:03 -0700398 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
399 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
400 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
401 // tolerate before we'd have to move to a large mip size. When we actually test these values
402 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
403 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
404 // level)
joshualitta7c63892015-04-21 13:24:37 -0700405 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700406 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
407 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
408
joshualitt9bd2daf2015-04-17 09:30:06 -0700409 skPaint->setLCDRenderText(false);
410 skPaint->setAutohinted(false);
411 skPaint->setHinting(SkPaint::kNormal_Hinting);
412 skPaint->setSubpixelText(true);
413}
414
joshualitt374b2f72015-07-21 08:05:03 -0700415inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700416 int runIndex,
robertphillips6ee690e2015-12-02 08:57:50 -0800417 const GrClip& clip,
jvanverth0628a522015-08-18 07:44:22 -0700418 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700419 const SkPaint& skPaint,
420 const SkMatrix& viewMatrix,
421 const SkTDArray<char>& fallbackTxt,
422 const SkTDArray<SkScalar>& fallbackPos,
423 int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800424 const SkPoint& offset) {
joshualittfec19e12015-04-17 10:32:32 -0700425 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700426 blob->setHasBitmap();
427 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700428 // Push back a new subrun to fill and set the override descriptor
429 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700430 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitt97202d22015-04-22 13:47:02 -0700431 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700432 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700433 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700434 run.fOverrideDescriptor->getDesc());
jvanverth0628a522015-08-18 07:44:22 -0700435 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700436 fallbackTxt.begin(), fallbackTxt.count(),
robertphillips6ee690e2015-12-02 08:57:50 -0800437 fallbackPos.begin(), scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700438 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700439}
440
joshualitt374b2f72015-07-21 08:05:03 -0700441inline GrAtlasTextBlob*
herbe59124e2015-11-18 10:54:39 -0800442GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
443 const SkMatrix& viewMatrix, SkPaint* dfPaint,
jvanverth157e6482015-09-09 08:05:12 -0700444 SkScalar* textRatio) {
joshualittf528e0d2015-12-09 06:42:52 -0800445 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700446
447 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700448 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700449 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700450 Run& run = blob->fRuns[0];
451 PerSubRunInfo& subRun = run.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800452 subRun.setUseLCDText(origPaint.isLCDRenderText());
453 subRun.setDrawAsDistanceFields();
joshualitt9bd2daf2015-04-17 09:30:06 -0700454
joshualitt9bd2daf2015-04-17 09:30:06 -0700455 return blob;
456}
457
joshualitt374b2f72015-07-21 08:05:03 -0700458inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800459GrAtlasTextContext::createDrawTextBlob(const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700460 const GrPaint& paint, const SkPaint& skPaint,
461 const SkMatrix& viewMatrix,
462 const char text[], size_t byteLength,
463 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700464 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700465
joshualitt374b2f72015-07-21 08:05:03 -0700466 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700467 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
468 SkPaint dfPaint;
469 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700470 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700471
joshualitt9bd2daf2015-04-17 09:30:06 -0700472 SkTDArray<char> fallbackTxt;
473 SkTDArray<SkScalar> fallbackPos;
474 SkPoint offset;
jvanverth157e6482015-09-09 08:05:12 -0700475 this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800476 byteLength, x, y, textRatio, &fallbackTxt, &fallbackPos,
joshualitt9bd2daf2015-04-17 09:30:06 -0700477 &offset, skPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700478 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800479 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
480 fallbackTxt, fallbackPos, 2, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700481 }
482 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800483 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700484 blob->fViewMatrix = viewMatrix;
485
486 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
487 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800488 byteLength, x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700489 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700490 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700491 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700492}
493
joshualitt374b2f72015-07-21 08:05:03 -0700494inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800495GrAtlasTextContext::createDrawPosTextBlob(const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700496 const GrPaint& paint, const SkPaint& skPaint,
497 const SkMatrix& viewMatrix,
498 const char text[], size_t byteLength,
499 const SkScalar pos[], int scalarsPerPosition,
500 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700501 int glyphCount = skPaint.countText(text, byteLength);
502
joshualitt374b2f72015-07-21 08:05:03 -0700503 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700504 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
505 SkPaint dfPaint;
506 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700507 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700508
509 SkTDArray<char> fallbackTxt;
510 SkTDArray<SkScalar> fallbackPos;
jvanverth157e6482015-09-09 08:05:12 -0700511 this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800512 byteLength, pos, scalarsPerPosition, offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700513 textRatio, &fallbackTxt, &fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700514 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800515 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
516 fallbackTxt, fallbackPos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700517 }
518 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800519 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700520 blob->fViewMatrix = viewMatrix;
521 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
522 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800523 byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700524 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700525 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700526 return blob;
527}
528
robertphillips433625e2015-12-04 06:58:16 -0800529void GrAtlasTextContext::onDrawText(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -0800530 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700531 const GrPaint& paint, const SkPaint& skPaint,
532 const SkMatrix& viewMatrix,
533 const char text[], size_t byteLength,
534 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700535 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800536 this->createDrawTextBlob(clip, paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700537 text, byteLength, x, y, regionClipBounds));
joshualitt2e2202e2015-12-10 11:22:08 -0800538 blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint,
539 clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700540}
541
robertphillips433625e2015-12-04 06:58:16 -0800542void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700543 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700544 const GrPaint& paint, const SkPaint& skPaint,
545 const SkMatrix& viewMatrix,
546 const char text[], size_t byteLength,
547 const SkScalar pos[], int scalarsPerPosition,
548 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700549 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800550 this->createDrawPosTextBlob(clip, paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700551 text, byteLength,
552 pos, scalarsPerPosition,
553 offset, regionClipBounds));
joshualitt79dfb2b2015-05-11 08:58:08 -0700554
joshualitt2e2202e2015-12-10 11:22:08 -0800555 blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint, clip,
556 regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700557}
558
joshualitt374b2f72015-07-21 08:05:03 -0700559void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700560 SkGlyphCache* cache, const SkPaint& skPaint,
561 GrColor color,
562 const SkMatrix& viewMatrix,
563 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800564 SkScalar x, SkScalar y) {
halcanary96fcdcc2015-08-27 07:41:13 -0700565 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700566
567 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700568 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700569 return;
570 }
571
halcanary96fcdcc2015-08-27 07:41:13 -0700572 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700573
574 // Get GrFontScaler from cache
575 GrFontScaler* fontScaler = GetGrFontScaler(cache);
576
herbe59124e2015-11-18 10:54:39 -0800577 SkFindAndPlaceGlyph::ProcessText(
herb4c11b3f2015-11-20 13:53:12 -0800578 skPaint.getTextEncoding(), text, byteLength,
579 {x, y}, viewMatrix, skPaint.getTextAlign(),
580 cache,
herbe59124e2015-11-18 10:54:39 -0800581 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
582 position += rounding;
583 this->bmpAppendGlyph(
584 blob, runIndex, glyph,
585 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800586 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700587 }
herbe59124e2015-11-18 10:54:39 -0800588 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700589}
590
joshualitt374b2f72015-07-21 08:05:03 -0700591void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700592 SkGlyphCache* cache, const SkPaint& skPaint,
593 GrColor color,
594 const SkMatrix& viewMatrix,
595 const char text[], size_t byteLength,
596 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800597 const SkPoint& offset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700598 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700599 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
600
601 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700602 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700603 return;
604 }
605
halcanary96fcdcc2015-08-27 07:41:13 -0700606 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700607
608 // Get GrFontScaler from cache
609 GrFontScaler* fontScaler = GetGrFontScaler(cache);
610
herb9be5ff62015-11-11 11:30:11 -0800611 SkFindAndPlaceGlyph::ProcessPosText(
herb4c11b3f2015-11-20 13:53:12 -0800612 skPaint.getTextEncoding(), text, byteLength,
613 offset, viewMatrix, pos, scalarsPerPosition,
614 skPaint.getTextAlign(), cache,
herb9be5ff62015-11-11 11:30:11 -0800615 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
616 position += rounding;
617 this->bmpAppendGlyph(
618 blob, runIndex, glyph,
619 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800620 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700621 }
herb9be5ff62015-11-11 11:30:11 -0800622 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700623}
624
joshualitt374b2f72015-07-21 08:05:03 -0700625void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700626 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700627 const SkMatrix& viewMatrix,
628 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800629 SkScalar x, SkScalar y,
joshualitt9bd2daf2015-04-17 09:30:06 -0700630 SkScalar textRatio,
631 SkTDArray<char>* fallbackTxt,
632 SkTDArray<SkScalar>* fallbackPos,
633 SkPoint* offset,
634 const SkPaint& origPaint) {
halcanary96fcdcc2015-08-27 07:41:13 -0700635 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700636
637 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700638 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700639 return;
640 }
641
642 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
643 SkAutoDescriptor desc;
halcanary96fcdcc2015-08-27 07:41:13 -0700644 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
herbe59124e2015-11-18 10:54:39 -0800645 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
joshualitt9bd2daf2015-04-17 09:30:06 -0700646 desc.getDesc());
647
648 SkTArray<SkScalar> positions;
649
650 const char* textPtr = text;
651 SkFixed stopX = 0;
652 SkFixed stopY = 0;
653 SkFixed origin = 0;
654 switch (origPaint.getTextAlign()) {
655 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
656 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
657 case SkPaint::kLeft_Align: origin = 0; break;
658 }
659
660 SkAutoKern autokern;
661 const char* stop = text + byteLength;
662 while (textPtr < stop) {
663 // don't need x, y here, since all subpixel variants will have the
664 // same advance
665 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
666
667 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
668 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
669
670 SkFixed height = glyph.fAdvanceY;
671 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
672
673 stopX += width;
674 stopY += height;
675 }
676 SkASSERT(textPtr == stop);
677
jvanverth157e6482015-09-09 08:05:12 -0700678 SkGlyphCache::AttachCache(origPaintCache);
679
joshualitt9bd2daf2015-04-17 09:30:06 -0700680 // now adjust starting point depending on alignment
681 SkScalar alignX = SkFixedToScalar(stopX);
682 SkScalar alignY = SkFixedToScalar(stopY);
683 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
684 alignX = SkScalarHalf(alignX);
685 alignY = SkScalarHalf(alignY);
686 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
687 alignX = 0;
688 alignY = 0;
689 }
690 x -= alignX;
691 y -= alignY;
692 *offset = SkPoint::Make(x, y);
693
jvanverth157e6482015-09-09 08:05:12 -0700694 this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800695 positions.begin(), 2, *offset, textRatio, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700696 fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700697}
698
joshualitt374b2f72015-07-21 08:05:03 -0700699void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700700 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700701 const SkMatrix& viewMatrix,
702 const char text[], size_t byteLength,
703 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800704 const SkPoint& offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700705 SkScalar textRatio,
706 SkTDArray<char>* fallbackTxt,
707 SkTDArray<SkScalar>* fallbackPos) {
708
halcanary96fcdcc2015-08-27 07:41:13 -0700709 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700710 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
711
712 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700713 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700714 return;
715 }
716
halcanary96fcdcc2015-08-27 07:41:13 -0700717 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -0700718
719 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
jvanverth157e6482015-09-09 08:05:12 -0700720 SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700721 GrFontScaler* fontScaler = GetGrFontScaler(cache);
722
723 const char* stop = text + byteLength;
724
725 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
726 while (text < stop) {
727 const char* lastText = text;
728 // the last 2 parameters are ignored
729 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
730
731 if (glyph.fWidth) {
732 SkScalar x = offset.x() + pos[0];
733 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
734
735 if (!this->dfAppendGlyph(blob,
736 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700737 glyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800738 x, y, color, fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700739 textRatio, viewMatrix)) {
740 // couldn't append, send to fallback
741 fallbackTxt->append(SkToInt(text-lastText), lastText);
742 *fallbackPos->append() = pos[0];
743 if (2 == scalarsPerPosition) {
744 *fallbackPos->append() = pos[1];
745 }
746 }
747 }
748 pos += scalarsPerPosition;
749 }
750 } else {
751 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
752 : SK_Scalar1;
753 while (text < stop) {
754 const char* lastText = text;
755 // the last 2 parameters are ignored
756 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
757
758 if (glyph.fWidth) {
759 SkScalar x = offset.x() + pos[0];
760 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
761
762 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
763 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
764
765 if (!this->dfAppendGlyph(blob,
766 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700767 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700768 x - advanceX, y - advanceY, color,
769 fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700770 textRatio,
771 viewMatrix)) {
772 // couldn't append, send to fallback
773 fallbackTxt->append(SkToInt(text-lastText), lastText);
774 *fallbackPos->append() = pos[0];
775 if (2 == scalarsPerPosition) {
776 *fallbackPos->append() = pos[1];
777 }
778 }
779 }
780 pos += scalarsPerPosition;
781 }
782 }
jvanverth157e6482015-09-09 08:05:12 -0700783
784 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700785}
786
joshualitt374b2f72015-07-21 08:05:03 -0700787void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700788 const SkGlyph& skGlyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800789 int vx, int vy, GrColor color, GrFontScaler* scaler) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700790 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700791 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
792 }
793
joshualitt6c2c2b02015-07-24 10:37:00 -0700794 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
795 skGlyph.getSubXFixed(),
796 skGlyph.getSubYFixed(),
797 GrGlyph::kCoverage_MaskStyle);
798 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700799 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700800 return;
801 }
802
803 int x = vx + glyph->fBounds.fLeft;
804 int y = vy + glyph->fBounds.fTop;
805
806 // keep them as ints until we've done the clip-test
807 int width = glyph->fBounds.width();
808 int height = glyph->fBounds.height();
809
joshualitt1d89e8d2015-04-01 12:40:54 -0700810 SkRect r;
811 r.fLeft = SkIntToScalar(x);
812 r.fTop = SkIntToScalar(y);
813 r.fRight = r.fLeft + SkIntToScalar(width);
814 r.fBottom = r.fTop + SkIntToScalar(height);
joshualittf528e0d2015-12-09 06:42:52 -0800815
joshualitta06e6ab2015-12-10 08:54:41 -0800816 blob->appendGlyph(runIndex, r, color, fCurrStrike, glyph, scaler, skGlyph,
817 SkIntToScalar(vx), SkIntToScalar(vy), 1.0f, false);
joshualitt9bd2daf2015-04-17 09:30:06 -0700818}
joshualitt1d89e8d2015-04-01 12:40:54 -0700819
joshualitt374b2f72015-07-21 08:05:03 -0700820bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700821 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700822 SkScalar sx, SkScalar sy, GrColor color,
823 GrFontScaler* scaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700824 SkScalar textRatio, const SkMatrix& viewMatrix) {
825 if (!fCurrStrike) {
826 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
827 }
828
joshualitt6c2c2b02015-07-24 10:37:00 -0700829 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
830 skGlyph.getSubXFixed(),
831 skGlyph.getSubYFixed(),
832 GrGlyph::kDistance_MaskStyle);
833 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700834 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700835 return true;
836 }
837
838 // fallback to color glyph support
839 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
840 return false;
841 }
842
843 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
844 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
845 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
846 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
847
848 SkScalar scale = textRatio;
849 dx *= scale;
850 dy *= scale;
851 width *= scale;
852 height *= scale;
853 sx += dx;
854 sy += dy;
855 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
856
joshualitta06e6ab2015-12-10 08:54:41 -0800857 blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph, scaler, skGlyph,
858 sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700859 return true;
860}
861
joshualitt79dfb2b2015-05-11 08:58:08 -0700862///////////////////////////////////////////////////////////////////////////////////////////////////
863
864#ifdef GR_TEST_UTILS
865
bsalomonabd30f52015-08-13 13:34:48 -0700866DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700867 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -0700868 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -0700869 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700870
871 if (context->uniqueID() != gContextID) {
872 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -0700873 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -0700874
joshualitt79dfb2b2015-05-11 08:58:08 -0700875 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
876 // because we don't really want to have a gpu device here.
877 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -0700878 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -0700879 }
880
joshualitt79dfb2b2015-05-11 08:58:08 -0700881 // Setup dummy SkPaint / GrPaint
882 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -0700883 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -0700884 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -0700885 skPaint.setColor(color);
886 skPaint.setLCDRenderText(random->nextBool());
887 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
888 skPaint.setSubpixelText(random->nextBool());
889
890 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700891 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700892 SkFAIL("couldn't convert paint\n");
893 }
894
895 const char* text = "The quick brown fox jumps over the lazy dog.";
896 int textLen = (int)strlen(text);
897
898 // Setup clip
899 GrClip clip;
900 SkIRect noClip = SkIRect::MakeLargest();
901
902 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
903 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -0700904 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800905 gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text,
joshualitt79dfb2b2015-05-11 08:58:08 -0700906 static_cast<size_t>(textLen), 0, 0, noClip));
907
908 SkScalar transX = static_cast<SkScalar>(random->nextU());
909 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -0700910 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt2e2202e2015-12-10 11:22:08 -0800911 return blob->createBatch(info, textLen, 0, 0, color, transX, transY, skPaint,
912 gSurfaceProps, gTextContext->dfAdjustTable(),
913 context->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -0700914}
915
916#endif