blob: 38b9c042d4433f71cf4ad1393748299191b09cc9 [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
robertphillipsccb1b572015-05-27 11:02:55 -07009#include "GrBlurUtils.h"
robertphillipsea461502015-05-26 11:38:03 -070010#include "GrDrawContext.h"
robertphillips2334fb62015-06-17 05:43:33 -070011#include "GrDrawTarget.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070012#include "GrFontScaler.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070013#include "GrStrokeInfo.h"
joshualittb7133be2015-04-08 09:08:31 -070014#include "GrTextBlobCache.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070015#include "GrTexturePriv.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
joshualitt374b2f72015-07-21 08:05:03 -0700108inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run,
joshualittdbd35932015-04-02 09:19:04 -0700109 const SkPaint& skPaint,
joshualitt9bd2daf2015-04-17 09:30:06 -0700110 const SkMatrix* viewMatrix,
111 bool noGamma) {
robertphillipsfcf78292015-06-19 11:49:52 -0700112 skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma);
joshualitt1d89e8d2015-04-01 12:40:54 -0700113 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
robertphillips433625e2015-12-04 06:58:16 -0800207 this->flush(blob, cacheBlob, dc, skPaint, grPaint, drawFilter,
joshualitt2a0e9f32015-04-13 06:12:21 -0700208 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();
joshualitt9a27e632015-04-06 10:53:36 -0700294
joshualittfcfb9fc2015-04-21 07:35:10 -0700295 SkTDArray<char> fallbackTxt;
296 SkTDArray<SkScalar> fallbackPos;
297 SkPoint dfOffset;
298 int scalarsPerPosition = 2;
299 switch (it.positioning()) {
300 case SkTextBlob::kDefault_Positioning: {
jvanverth157e6482015-09-09 08:05:12 -0700301 this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700302 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800303 x + offset.x(), y + offset.y(), textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700304 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
305 break;
306 }
307 case SkTextBlob::kHorizontal_Positioning: {
308 scalarsPerPosition = 1;
309 dfOffset = SkPoint::Make(x, y + offset.y());
jvanverth157e6482015-09-09 08:05:12 -0700310 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700311 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800312 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700313 &fallbackTxt, &fallbackPos);
314 break;
315 }
316 case SkTextBlob::kFull_Positioning: {
317 dfOffset = SkPoint::Make(x, y);
jvanverth157e6482015-09-09 08:05:12 -0700318 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700319 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800320 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700321 &fallbackTxt, &fallbackPos);
322 break;
323 }
324 }
325 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800326 this->fallbackDrawPosText(cacheBlob, run, clip, color, runPaint, viewMatrix,
327 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset);
joshualittfcfb9fc2015-04-21 07:35:10 -0700328 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700329 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
330 cacheBlob->fRuns[run].fDrawAsPaths = true;
331 } else {
332 cacheBlob->setHasBitmap();
333 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
334 false);
335 switch (it.positioning()) {
336 case SkTextBlob::kDefault_Positioning:
337 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
338 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800339 x + offset.x(), y + offset.y());
joshualittfcfb9fc2015-04-21 07:35:10 -0700340 break;
341 case SkTextBlob::kHorizontal_Positioning:
342 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
343 (const char*)it.glyphs(), textLen, it.pos(), 1,
robertphillips6ee690e2015-12-02 08:57:50 -0800344 SkPoint::Make(x, y + offset.y()));
joshualittfcfb9fc2015-04-21 07:35:10 -0700345 break;
346 case SkTextBlob::kFull_Positioning:
347 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
348 (const char*)it.glyphs(), textLen, it.pos(), 2,
robertphillips6ee690e2015-12-02 08:57:50 -0800349 SkPoint::Make(x, y));
joshualittfcfb9fc2015-04-21 07:35:10 -0700350 break;
351 }
352 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700353 }
354
355 if (drawFilter) {
356 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
357 runPaint = skPaint;
358 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700359 }
360}
361
joshualitt374b2f72015-07-21 08:05:03 -0700362inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700363 SkPaint* skPaint,
364 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700365 const SkMatrix& viewMatrix) {
366 // getMaxScale doesn't support perspective, so neither do we at the moment
367 SkASSERT(!viewMatrix.hasPerspective());
368 SkScalar maxScale = viewMatrix.getMaxScale();
369 SkScalar textSize = skPaint->getTextSize();
370 SkScalar scaledTextSize = textSize;
371 // if we have non-unity scale, we need to choose our base text size
372 // based on the SkPaint's text size multiplied by the max scale factor
373 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
374 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
375 scaledTextSize *= maxScale;
376 }
377
joshualitt64c99cc2015-04-21 09:43:03 -0700378 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
379 // and ceiling. A scale outside of this range would require regenerating the distance fields
380 SkScalar dfMaskScaleFloor;
381 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700382 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700383 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700384 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700385 *textRatio = textSize / kSmallDFFontSize;
386 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
387 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700388 dfMaskScaleFloor = kSmallDFFontLimit;
389 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700390 *textRatio = textSize / kMediumDFFontSize;
391 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
392 } else {
joshualitta7c63892015-04-21 13:24:37 -0700393 dfMaskScaleFloor = kMediumDFFontLimit;
394 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700395 *textRatio = textSize / kLargeDFFontSize;
396 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
397 }
398
joshualitt64c99cc2015-04-21 09:43:03 -0700399 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
400 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
401 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
402 // tolerate before we'd have to move to a large mip size. When we actually test these values
403 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
404 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
405 // level)
joshualitta7c63892015-04-21 13:24:37 -0700406 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700407 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
408 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
409
joshualitt9bd2daf2015-04-17 09:30:06 -0700410 skPaint->setLCDRenderText(false);
411 skPaint->setAutohinted(false);
412 skPaint->setHinting(SkPaint::kNormal_Hinting);
413 skPaint->setSubpixelText(true);
414}
415
joshualitt374b2f72015-07-21 08:05:03 -0700416inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700417 int runIndex,
robertphillips6ee690e2015-12-02 08:57:50 -0800418 const GrClip& clip,
jvanverth0628a522015-08-18 07:44:22 -0700419 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700420 const SkPaint& skPaint,
421 const SkMatrix& viewMatrix,
422 const SkTDArray<char>& fallbackTxt,
423 const SkTDArray<SkScalar>& fallbackPos,
424 int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800425 const SkPoint& offset) {
joshualittfec19e12015-04-17 10:32:32 -0700426 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700427 blob->setHasBitmap();
428 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700429 // Push back a new subrun to fill and set the override descriptor
430 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700431 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitt97202d22015-04-22 13:47:02 -0700432 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700433 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700434 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700435 run.fOverrideDescriptor->getDesc());
jvanverth0628a522015-08-18 07:44:22 -0700436 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700437 fallbackTxt.begin(), fallbackTxt.count(),
robertphillips6ee690e2015-12-02 08:57:50 -0800438 fallbackPos.begin(), scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700439 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700440}
441
joshualitt374b2f72015-07-21 08:05:03 -0700442inline GrAtlasTextBlob*
herbe59124e2015-11-18 10:54:39 -0800443GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
444 const SkMatrix& viewMatrix, SkPaint* dfPaint,
jvanverth157e6482015-09-09 08:05:12 -0700445 SkScalar* textRatio) {
joshualittf528e0d2015-12-09 06:42:52 -0800446 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700447
448 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700449 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700450 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700451 Run& run = blob->fRuns[0];
452 PerSubRunInfo& subRun = run.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800453 subRun.setUseLCDText(origPaint.isLCDRenderText());
454 subRun.setDrawAsDistanceFields();
joshualitt9bd2daf2015-04-17 09:30:06 -0700455
joshualitt9bd2daf2015-04-17 09:30:06 -0700456 return blob;
457}
458
joshualitt374b2f72015-07-21 08:05:03 -0700459inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800460GrAtlasTextContext::createDrawTextBlob(const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700461 const GrPaint& paint, const SkPaint& skPaint,
462 const SkMatrix& viewMatrix,
463 const char text[], size_t byteLength,
464 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700465 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700466
joshualitt374b2f72015-07-21 08:05:03 -0700467 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700468 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
469 SkPaint dfPaint;
470 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700471 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700472
joshualitt9bd2daf2015-04-17 09:30:06 -0700473 SkTDArray<char> fallbackTxt;
474 SkTDArray<SkScalar> fallbackPos;
475 SkPoint offset;
jvanverth157e6482015-09-09 08:05:12 -0700476 this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800477 byteLength, x, y, textRatio, &fallbackTxt, &fallbackPos,
joshualitt9bd2daf2015-04-17 09:30:06 -0700478 &offset, skPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700479 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800480 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
481 fallbackTxt, fallbackPos, 2, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700482 }
483 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800484 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700485 blob->fViewMatrix = viewMatrix;
486
487 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
488 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800489 byteLength, x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700490 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700491 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700492 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700493}
494
joshualitt374b2f72015-07-21 08:05:03 -0700495inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800496GrAtlasTextContext::createDrawPosTextBlob(const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700497 const GrPaint& paint, const SkPaint& skPaint,
498 const SkMatrix& viewMatrix,
499 const char text[], size_t byteLength,
500 const SkScalar pos[], int scalarsPerPosition,
501 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700502 int glyphCount = skPaint.countText(text, byteLength);
503
joshualitt374b2f72015-07-21 08:05:03 -0700504 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700505 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
506 SkPaint dfPaint;
507 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700508 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700509
510 SkTDArray<char> fallbackTxt;
511 SkTDArray<SkScalar> fallbackPos;
jvanverth157e6482015-09-09 08:05:12 -0700512 this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800513 byteLength, pos, scalarsPerPosition, offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700514 textRatio, &fallbackTxt, &fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700515 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800516 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
517 fallbackTxt, fallbackPos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700518 }
519 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800520 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700521 blob->fViewMatrix = viewMatrix;
522 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
523 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800524 byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700525 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700526 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700527 return blob;
528}
529
robertphillips433625e2015-12-04 06:58:16 -0800530void GrAtlasTextContext::onDrawText(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -0800531 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700532 const GrPaint& paint, const SkPaint& skPaint,
533 const SkMatrix& viewMatrix,
534 const char text[], size_t byteLength,
535 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700536 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800537 this->createDrawTextBlob(clip, paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700538 text, byteLength, x, y, regionClipBounds));
robertphillips433625e2015-12-04 06:58:16 -0800539 this->flush(blob, dc, skPaint, paint, 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
robertphillips433625e2015-12-04 06:58:16 -0800555 this->flush(blob, dc, skPaint, paint, clip, regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700556}
557
joshualitt374b2f72015-07-21 08:05:03 -0700558void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700559 SkGlyphCache* cache, const SkPaint& skPaint,
560 GrColor color,
561 const SkMatrix& viewMatrix,
562 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800563 SkScalar x, SkScalar y) {
halcanary96fcdcc2015-08-27 07:41:13 -0700564 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700565
566 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700567 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700568 return;
569 }
570
halcanary96fcdcc2015-08-27 07:41:13 -0700571 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700572
573 // Get GrFontScaler from cache
574 GrFontScaler* fontScaler = GetGrFontScaler(cache);
575
herbe59124e2015-11-18 10:54:39 -0800576 SkFindAndPlaceGlyph::ProcessText(
herb4c11b3f2015-11-20 13:53:12 -0800577 skPaint.getTextEncoding(), text, byteLength,
578 {x, y}, viewMatrix, skPaint.getTextAlign(),
579 cache,
herbe59124e2015-11-18 10:54:39 -0800580 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
581 position += rounding;
582 this->bmpAppendGlyph(
583 blob, runIndex, glyph,
584 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800585 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700586 }
herbe59124e2015-11-18 10:54:39 -0800587 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700588}
589
joshualitt374b2f72015-07-21 08:05:03 -0700590void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700591 SkGlyphCache* cache, const SkPaint& skPaint,
592 GrColor color,
593 const SkMatrix& viewMatrix,
594 const char text[], size_t byteLength,
595 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800596 const SkPoint& offset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700597 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700598 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
599
600 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700601 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700602 return;
603 }
604
halcanary96fcdcc2015-08-27 07:41:13 -0700605 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700606
607 // Get GrFontScaler from cache
608 GrFontScaler* fontScaler = GetGrFontScaler(cache);
609
herb9be5ff62015-11-11 11:30:11 -0800610 SkFindAndPlaceGlyph::ProcessPosText(
herb4c11b3f2015-11-20 13:53:12 -0800611 skPaint.getTextEncoding(), text, byteLength,
612 offset, viewMatrix, pos, scalarsPerPosition,
613 skPaint.getTextAlign(), cache,
herb9be5ff62015-11-11 11:30:11 -0800614 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
615 position += rounding;
616 this->bmpAppendGlyph(
617 blob, runIndex, glyph,
618 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800619 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700620 }
herb9be5ff62015-11-11 11:30:11 -0800621 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700622}
623
joshualitt374b2f72015-07-21 08:05:03 -0700624void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700625 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700626 const SkMatrix& viewMatrix,
627 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800628 SkScalar x, SkScalar y,
joshualitt9bd2daf2015-04-17 09:30:06 -0700629 SkScalar textRatio,
630 SkTDArray<char>* fallbackTxt,
631 SkTDArray<SkScalar>* fallbackPos,
632 SkPoint* offset,
633 const SkPaint& origPaint) {
halcanary96fcdcc2015-08-27 07:41:13 -0700634 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700635
636 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700637 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700638 return;
639 }
640
641 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
642 SkAutoDescriptor desc;
halcanary96fcdcc2015-08-27 07:41:13 -0700643 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
herbe59124e2015-11-18 10:54:39 -0800644 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
joshualitt9bd2daf2015-04-17 09:30:06 -0700645 desc.getDesc());
646
647 SkTArray<SkScalar> positions;
648
649 const char* textPtr = text;
650 SkFixed stopX = 0;
651 SkFixed stopY = 0;
652 SkFixed origin = 0;
653 switch (origPaint.getTextAlign()) {
654 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
655 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
656 case SkPaint::kLeft_Align: origin = 0; break;
657 }
658
659 SkAutoKern autokern;
660 const char* stop = text + byteLength;
661 while (textPtr < stop) {
662 // don't need x, y here, since all subpixel variants will have the
663 // same advance
664 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
665
666 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
667 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
668
669 SkFixed height = glyph.fAdvanceY;
670 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
671
672 stopX += width;
673 stopY += height;
674 }
675 SkASSERT(textPtr == stop);
676
jvanverth157e6482015-09-09 08:05:12 -0700677 SkGlyphCache::AttachCache(origPaintCache);
678
joshualitt9bd2daf2015-04-17 09:30:06 -0700679 // now adjust starting point depending on alignment
680 SkScalar alignX = SkFixedToScalar(stopX);
681 SkScalar alignY = SkFixedToScalar(stopY);
682 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
683 alignX = SkScalarHalf(alignX);
684 alignY = SkScalarHalf(alignY);
685 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
686 alignX = 0;
687 alignY = 0;
688 }
689 x -= alignX;
690 y -= alignY;
691 *offset = SkPoint::Make(x, y);
692
jvanverth157e6482015-09-09 08:05:12 -0700693 this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800694 positions.begin(), 2, *offset, textRatio, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700695 fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700696}
697
joshualitt374b2f72015-07-21 08:05:03 -0700698void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700699 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700700 const SkMatrix& viewMatrix,
701 const char text[], size_t byteLength,
702 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800703 const SkPoint& offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700704 SkScalar textRatio,
705 SkTDArray<char>* fallbackTxt,
706 SkTDArray<SkScalar>* fallbackPos) {
707
halcanary96fcdcc2015-08-27 07:41:13 -0700708 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700709 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
710
711 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700712 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700713 return;
714 }
715
halcanary96fcdcc2015-08-27 07:41:13 -0700716 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -0700717
718 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
jvanverth157e6482015-09-09 08:05:12 -0700719 SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700720 GrFontScaler* fontScaler = GetGrFontScaler(cache);
721
722 const char* stop = text + byteLength;
723
724 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
725 while (text < stop) {
726 const char* lastText = text;
727 // the last 2 parameters are ignored
728 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
729
730 if (glyph.fWidth) {
731 SkScalar x = offset.x() + pos[0];
732 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
733
734 if (!this->dfAppendGlyph(blob,
735 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700736 glyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800737 x, y, color, fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700738 textRatio, viewMatrix)) {
739 // couldn't append, send to fallback
740 fallbackTxt->append(SkToInt(text-lastText), lastText);
741 *fallbackPos->append() = pos[0];
742 if (2 == scalarsPerPosition) {
743 *fallbackPos->append() = pos[1];
744 }
745 }
746 }
747 pos += scalarsPerPosition;
748 }
749 } else {
750 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
751 : SK_Scalar1;
752 while (text < stop) {
753 const char* lastText = text;
754 // the last 2 parameters are ignored
755 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
756
757 if (glyph.fWidth) {
758 SkScalar x = offset.x() + pos[0];
759 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
760
761 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
762 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
763
764 if (!this->dfAppendGlyph(blob,
765 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700766 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700767 x - advanceX, y - advanceY, color,
768 fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700769 textRatio,
770 viewMatrix)) {
771 // couldn't append, send to fallback
772 fallbackTxt->append(SkToInt(text-lastText), lastText);
773 *fallbackPos->append() = pos[0];
774 if (2 == scalarsPerPosition) {
775 *fallbackPos->append() = pos[1];
776 }
777 }
778 }
779 pos += scalarsPerPosition;
780 }
781 }
jvanverth157e6482015-09-09 08:05:12 -0700782
783 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700784}
785
joshualitt374b2f72015-07-21 08:05:03 -0700786void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700787 const SkGlyph& skGlyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800788 int vx, int vy, GrColor color, GrFontScaler* scaler) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700789 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700790 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
791 }
792
joshualitt6c2c2b02015-07-24 10:37:00 -0700793 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
794 skGlyph.getSubXFixed(),
795 skGlyph.getSubYFixed(),
796 GrGlyph::kCoverage_MaskStyle);
797 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700798 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700799 return;
800 }
801
802 int x = vx + glyph->fBounds.fLeft;
803 int y = vy + glyph->fBounds.fTop;
804
805 // keep them as ints until we've done the clip-test
806 int width = glyph->fBounds.width();
807 int height = glyph->fBounds.height();
808
joshualitt1d89e8d2015-04-01 12:40:54 -0700809 SkRect r;
810 r.fLeft = SkIntToScalar(x);
811 r.fTop = SkIntToScalar(y);
812 r.fRight = r.fLeft + SkIntToScalar(width);
813 r.fBottom = r.fTop + SkIntToScalar(height);
joshualittf528e0d2015-12-09 06:42:52 -0800814
joshualitta06e6ab2015-12-10 08:54:41 -0800815 blob->appendGlyph(runIndex, r, color, fCurrStrike, glyph, scaler, skGlyph,
816 SkIntToScalar(vx), SkIntToScalar(vy), 1.0f, false);
joshualitt9bd2daf2015-04-17 09:30:06 -0700817}
joshualitt1d89e8d2015-04-01 12:40:54 -0700818
joshualitt374b2f72015-07-21 08:05:03 -0700819bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700820 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700821 SkScalar sx, SkScalar sy, GrColor color,
822 GrFontScaler* scaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700823 SkScalar textRatio, const SkMatrix& viewMatrix) {
824 if (!fCurrStrike) {
825 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
826 }
827
joshualitt6c2c2b02015-07-24 10:37:00 -0700828 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
829 skGlyph.getSubXFixed(),
830 skGlyph.getSubYFixed(),
831 GrGlyph::kDistance_MaskStyle);
832 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700833 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700834 return true;
835 }
836
837 // fallback to color glyph support
838 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
839 return false;
840 }
841
842 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
843 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
844 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
845 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
846
847 SkScalar scale = textRatio;
848 dx *= scale;
849 dy *= scale;
850 width *= scale;
851 height *= scale;
852 sx += dx;
853 sy += dy;
854 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
855
joshualitta06e6ab2015-12-10 08:54:41 -0800856 blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph, scaler, skGlyph,
857 sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700858 return true;
859}
860
robertphillips7bceedc2015-12-01 12:51:26 -0800861void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -0800862 const SkTextBlobRunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -0700863 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -0700864 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
865 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
866 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -0700867
joshualitt9a27e632015-04-06 10:53:36 -0700868 size_t textLen = it.glyphCount() * sizeof(uint16_t);
869 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -0700870
joshualitt9a27e632015-04-06 10:53:36 -0700871 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -0700872
joshualitt9a27e632015-04-06 10:53:36 -0700873 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
874 return;
joshualitt1d89e8d2015-04-01 12:40:54 -0700875 }
876
robertphillipsfcf78292015-06-19 11:49:52 -0700877 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -0700878
879 switch (it.positioning()) {
880 case SkTextBlob::kDefault_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -0800881 this->drawTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700882 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -0700883 textLen, x + offset.x(), y + offset.y(), clipBounds);
884 break;
885 case SkTextBlob::kHorizontal_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -0800886 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700887 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -0700888 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
889 clipBounds);
890 break;
891 case SkTextBlob::kFull_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -0800892 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700893 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -0700894 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
895 break;
896 }
897}
898
bsalomonabd30f52015-08-13 13:34:48 -0700899inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -0700900GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -0700901 int glyphCount, int run, int subRun,
902 GrColor color, SkScalar transX, SkScalar transY,
903 const SkPaint& skPaint) {
joshualitt3660d532015-12-07 11:32:50 -0800904 GrMaskFormat format = info.maskFormat();
joshualitt79dfb2b2015-05-11 08:58:08 -0700905 GrColor subRunColor;
906 if (kARGB_GrMaskFormat == format) {
907 uint8_t paintAlpha = skPaint.getAlpha();
908 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
909 } else {
910 subRunColor = color;
911 }
912
joshualitta751c972015-11-20 13:37:32 -0800913 GrAtlasTextBatch* batch;
joshualitt3660d532015-12-07 11:32:50 -0800914 if (info.drawAsDistanceFields()) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700915 SkColor filteredColor;
916 SkColorFilter* colorFilter = skPaint.getColorFilter();
917 if (colorFilter) {
918 filteredColor = colorFilter->filterColor(skPaint.getColor());
919 } else {
920 filteredColor = skPaint.getColor();
921 }
robertphillipsfcf78292015-06-19 11:49:52 -0700922 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
joshualitta751c972015-11-20 13:37:32 -0800923 batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
924 fDistanceAdjustTable, filteredColor,
joshualitt3660d532015-12-07 11:32:50 -0800925 info.hasUseLCDText(), useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -0700926 } else {
joshualitta751c972015-11-20 13:37:32 -0800927 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -0700928 }
joshualitta751c972015-11-20 13:37:32 -0800929 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -0700930 geometry.fBlob = SkRef(cacheBlob);
931 geometry.fRun = run;
932 geometry.fSubRun = subRun;
933 geometry.fColor = subRunColor;
934 geometry.fTransX = transX;
935 geometry.fTransY = transY;
936 batch->init();
937
938 return batch;
939}
940
robertphillipsf6703fa2015-09-01 05:36:47 -0700941inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -0700942 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -0700943 SkScalar transX, SkScalar transY,
944 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -0700945 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
946 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
joshualitt3660d532015-12-07 11:32:50 -0800947 int glyphCount = info.glyphCount();
joshualitt9a27e632015-04-06 10:53:36 -0700948 if (0 == glyphCount) {
949 continue;
950 }
951
bsalomonabd30f52015-08-13 13:34:48 -0700952 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
953 subRun, color, transX, transY,
954 skPaint));
robertphillipsf6703fa2015-09-01 05:36:47 -0700955 dc->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -0700956 }
957}
958
herbe59124e2015-11-18 10:54:39 -0800959inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob,
robertphillips7bceedc2015-12-01 12:51:26 -0800960 GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700961 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -0700962 SkScalar transX, SkScalar transY,
963 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -0700964 if (!cacheBlob->fBigGlyphs.count()) {
965 return;
966 }
967
joshualitt9a27e632015-04-06 10:53:36 -0700968 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -0700969 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -0700970 bigGlyph.fVx += transX;
971 bigGlyph.fVy += transY;
joshualitt0fe04a22015-08-25 12:05:50 -0700972 SkMatrix ctm;
973 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
974 ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
975 if (bigGlyph.fApplyVM) {
976 ctm.postConcat(cacheBlob->fViewMatrix);
977 }
joshualittfc072562015-05-13 12:15:06 -0700978
robertphillips7bceedc2015-12-01 12:51:26 -0800979 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, bigGlyph.fPath,
joshualitt0fe04a22015-08-25 12:05:50 -0700980 skPaint, ctm, nullptr, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -0700981 }
982}
joshualitt9a27e632015-04-06 10:53:36 -0700983
robertphillips2334fb62015-06-17 05:43:33 -0700984void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -0700985 GrAtlasTextBlob* cacheBlob,
herbe59124e2015-11-18 10:54:39 -0800986 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -0700987 const SkPaint& skPaint,
988 const GrPaint& grPaint,
989 SkDrawFilter* drawFilter,
990 const GrClip& clip,
991 const SkMatrix& viewMatrix,
992 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -0700993 SkScalar x, SkScalar y,
994 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -0700995 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
996 // it as paths
robertphillips433625e2015-12-04 06:58:16 -0800997 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
joshualitt9a27e632015-04-06 10:53:36 -0700998
999 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001000
halcanary33779752015-10-27 14:01:05 -07001001 SkTextBlobRunIterator it(blob);
joshualitt9a27e632015-04-06 10:53:36 -07001002 for (int run = 0; !it.done(); it.next(), run++) {
1003 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillips7bceedc2015-12-01 12:51:26 -08001004 this->flushRunAsPaths(dc, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07001005 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07001006 continue;
1007 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001008 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillipsf6703fa2015-09-01 05:36:47 -07001009 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07001010 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001011 }
1012
1013 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001014 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001015}
1016
joshualitt374b2f72015-07-21 08:05:03 -07001017void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07001018 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07001019 const SkPaint& skPaint,
1020 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07001021 const GrClip& clip,
1022 const SkIRect& clipBounds) {
robertphillips433625e2015-12-04 06:58:16 -08001023 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
joshualitt9a27e632015-04-06 10:53:36 -07001024
1025 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001026 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillipsf6703fa2015-09-01 05:36:47 -07001027 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001028 }
1029
1030 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001031 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001032}
joshualitt79dfb2b2015-05-11 08:58:08 -07001033
1034///////////////////////////////////////////////////////////////////////////////////////////////////
1035
1036#ifdef GR_TEST_UTILS
1037
bsalomonabd30f52015-08-13 13:34:48 -07001038DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001039 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -07001040 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -07001041 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07001042
1043 if (context->uniqueID() != gContextID) {
1044 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -07001045 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -07001046
joshualitt79dfb2b2015-05-11 08:58:08 -07001047 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
1048 // because we don't really want to have a gpu device here.
1049 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -07001050 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07001051 }
1052
joshualitt79dfb2b2015-05-11 08:58:08 -07001053 // Setup dummy SkPaint / GrPaint
1054 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001055 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07001056 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07001057 skPaint.setColor(color);
1058 skPaint.setLCDRenderText(random->nextBool());
1059 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
1060 skPaint.setSubpixelText(random->nextBool());
1061
1062 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001063 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001064 SkFAIL("couldn't convert paint\n");
1065 }
1066
1067 const char* text = "The quick brown fox jumps over the lazy dog.";
1068 int textLen = (int)strlen(text);
1069
1070 // Setup clip
1071 GrClip clip;
1072 SkIRect noClip = SkIRect::MakeLargest();
1073
1074 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
1075 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07001076 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -08001077 gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text,
joshualitt79dfb2b2015-05-11 08:58:08 -07001078 static_cast<size_t>(textLen), 0, 0, noClip));
1079
1080 SkScalar transX = static_cast<SkScalar>(random->nextU());
1081 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07001082 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07001083 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
1084}
1085
1086#endif