blob: b6971f0ccd341438abdba5fa557b5c77161963ec [file] [log] [blame]
joshualitt1d89e8d2015-04-01 12:40:54 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "GrAtlasTextContext.h"
8
robertphillipsea461502015-05-26 11:38:03 -07009#include "GrDrawContext.h"
robertphillips2334fb62015-06-17 05:43:33 -070010#include "GrDrawTarget.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070011#include "GrFontScaler.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070012#include "GrStrokeInfo.h"
joshualittb7133be2015-04-08 09:08:31 -070013#include "GrTextBlobCache.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070014#include "GrTexturePriv.h"
joshualitt29677982015-12-11 06:08:59 -080015#include "GrTextUtils.h"
bsalomon72e3ae42015-04-28 08:08:46 -070016#include "GrVertexBuffer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070017
18#include "SkAutoKern.h"
19#include "SkColorPriv.h"
joshualitt9bd2daf2015-04-17 09:30:06 -070020#include "SkColorFilter.h"
21#include "SkDistanceFieldGen.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070022#include "SkDraw.h"
23#include "SkDrawFilter.h"
24#include "SkDrawProcs.h"
herb9be5ff62015-11-11 11:30:11 -080025#include "SkFindAndPlaceGlyph.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070026#include "SkGlyphCache.h"
27#include "SkGpuDevice.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070028#include "SkGrPriv.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070029#include "SkPath.h"
30#include "SkRTConf.h"
31#include "SkStrokeRec.h"
32#include "SkTextBlob.h"
33#include "SkTextMapStateProc.h"
34
joshualitta751c972015-11-20 13:37:32 -080035#include "batches/GrAtlasTextBatch.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070036
37namespace {
joshualitt9bd2daf2015-04-17 09:30:06 -070038static const int kMinDFFontSize = 18;
39static const int kSmallDFFontSize = 32;
40static const int kSmallDFFontLimit = 32;
41static const int kMediumDFFontSize = 72;
42static const int kMediumDFFontLimit = 72;
43static const int kLargeDFFontSize = 162;
jvanverth97c595f2015-06-19 11:06:28 -070044#ifdef SK_BUILD_FOR_ANDROID
45static const int kLargeDFFontLimit = 384;
46#else
joshualitta7c63892015-04-21 13:24:37 -070047static const int kLargeDFFontLimit = 2 * kLargeDFFontSize;
jvanverth97c595f2015-06-19 11:06:28 -070048#endif
joshualitt1d89e8d2015-04-01 12:40:54 -070049};
50
robertphillipsf6703fa2015-09-01 05:36:47 -070051GrAtlasTextContext::GrAtlasTextContext(GrContext* context, const SkSurfaceProps& surfaceProps)
52 : INHERITED(context, surfaceProps)
joshualitt1acabf32015-12-10 09:10:10 -080053 , fDistanceAdjustTable(new GrDistanceFieldAdjustTable) {
joshualittb7133be2015-04-08 09:08:31 -070054 // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest
55 // vertexStride
joshualittf528e0d2015-12-09 06:42:52 -080056 static_assert(GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kColorTextVASize &&
57 GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kLCDTextVASize,
bungeman99fe8222015-08-20 07:57:51 -070058 "vertex_attribute_changed");
halcanary96fcdcc2015-08-27 07:41:13 -070059 fCurrStrike = nullptr;
joshualittb7133be2015-04-08 09:08:31 -070060 fCache = context->getTextBlobCache();
joshualitt9bd2daf2015-04-17 09:30:06 -070061}
62
joshualitt1d89e8d2015-04-01 12:40:54 -070063
joshualittdbd35932015-04-02 09:19:04 -070064GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context,
robertphillipsfcf78292015-06-19 11:49:52 -070065 const SkSurfaceProps& surfaceProps) {
robertphillipsf6703fa2015-09-01 05:36:47 -070066 return new GrAtlasTextContext(context, surfaceProps);
joshualitt1d89e8d2015-04-01 12:40:54 -070067}
68
robertphillips6ee690e2015-12-02 08:57:50 -080069bool GrAtlasTextContext::canDraw(const SkPaint& skPaint, const SkMatrix& viewMatrix) {
joshualitt9bd2daf2015-04-17 09:30:06 -070070 return this->canDrawAsDistanceFields(skPaint, viewMatrix) ||
71 !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -070072}
73
joshualitt9e36c1a2015-04-14 12:17:27 -070074GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
75 GrColor canonicalColor = paint.computeLuminanceColor();
76 if (lcd) {
77 // This is the correct computation, but there are tons of cases where LCD can be overridden.
78 // For now we just regenerate if any run in a textblob has LCD.
79 // TODO figure out where all of these overrides are and see if we can incorporate that logic
80 // at a higher level *OR* use sRGB
81 SkASSERT(false);
82 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
83 } else {
84 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
85 // gamma corrected masks anyways, nor color
86 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
87 SkColorGetG(canonicalColor),
88 SkColorGetB(canonicalColor));
89 // reduce to our finite number of bits
90 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
91 }
92 return canonicalColor;
93}
94
95// TODO if this function ever shows up in profiling, then we can compute this value when the
96// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
97// run so this is not a big deal to compute here.
98bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -070099 SkTextBlobRunIterator it(blob);
joshualitt9e36c1a2015-04-14 12:17:27 -0700100 for (; !it.done(); it.next()) {
101 if (it.isLCD()) {
102 return true;
103 }
104 }
105 return false;
106}
107
robertphillips433625e2015-12-04 06:58:16 -0800108void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700109 const GrClip& clip, const SkPaint& skPaint,
110 const SkMatrix& viewMatrix, const SkTextBlob* blob,
111 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700112 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700113 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700114 if (fContext->abandoned()) {
115 return;
116 }
117
joshualitt374b2f72015-07-21 08:05:03 -0700118 SkAutoTUnref<GrAtlasTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700119 SkMaskFilter::BlurRec blurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700120 GrAtlasTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700121 // It might be worth caching these things, but its not clear at this time
122 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
123 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700124 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700125 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700126 drawFilter);
127
128 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700129 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700130
131 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
robertphillipsfcf78292015-06-19 11:49:52 -0700132 SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700133 kUnknown_SkPixelGeometry;
134
joshualitt9e36c1a2015-04-14 12:17:27 -0700135 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
136 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
137 // ensure we always match the same key
138 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
139 ComputeCanonicalColor(skPaint, hasLCD);
140
joshualitte4cee1f2015-05-11 13:04:28 -0700141 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700142 key.fUniqueID = blob->uniqueID();
143 key.fStyle = skPaint.getStyle();
144 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700145 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700146 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700147 }
148
joshualitt2a0e9f32015-04-13 06:12:21 -0700149 SkScalar transX = 0.f;
150 SkScalar transY = 0.f;
151
joshualitt9e36c1a2015-04-14 12:17:27 -0700152 // Though for the time being runs in the textblob can override the paint, they only touch font
153 // info.
154 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700155 if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700156 return;
157 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700158
joshualittb7133be2015-04-08 09:08:31 -0700159 if (cacheBlob) {
joshualittfd5f6c12015-12-10 07:44:50 -0800160 if (cacheBlob->mustRegenerate(&transX, &transY, skPaint, grPaint.getColor(), blurRec,
161 viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700162 // We have to remake the blob because changes may invalidate our masks.
163 // TODO we could probably get away reuse most of the time if the pointer is unique,
164 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700165 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700166 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800167 GrAtlasTextBlob::kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700168 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
joshualitta6bf4c52016-01-19 06:59:29 -0800169 blob, x, y, drawFilter);
joshualittb7133be2015-04-08 09:08:31 -0700170 } else {
171 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700172#ifdef CACHE_SANITY_CHECK
173 {
174 int glyphCount = 0;
175 int runCount = 0;
176 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
177 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
178 kGrayTextVASize));
179 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
180 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
joshualitta6bf4c52016-01-19 06:59:29 -0800181 blob, x, y, drawFilter);
joshualitt259fbf12015-07-21 11:39:34 -0700182 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
183 }
184
185#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700186 }
187 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700188 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700189 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800190 GrAtlasTextBlob::kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700191 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800192 cacheBlob.reset(fCache->createBlob(blob, GrAtlasTextBlob::kGrayTextVASize));
joshualitt2a0e9f32015-04-13 06:12:21 -0700193 }
robertphillips9c240a12015-05-28 07:45:59 -0700194 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
joshualitta6bf4c52016-01-19 06:59:29 -0800195 blob, x, y, drawFilter);
joshualitt1d89e8d2015-04-01 12:40:54 -0700196 }
197
joshualitt0a42e682015-12-10 13:20:58 -0800198 cacheBlob->flushCached(fContext, dc, blob, fSurfaceProps, fDistanceAdjustTable, skPaint,
joshualitt2e2202e2015-12-10 11:22:08 -0800199 grPaint, drawFilter, clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700200}
201
joshualitt9bd2daf2015-04-17 09:30:06 -0700202inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
203 const SkMatrix& viewMatrix) {
204 // TODO: support perspective (need getMaxScale replacement)
205 if (viewMatrix.hasPerspective()) {
206 return false;
207 }
208
209 SkScalar maxScale = viewMatrix.getMaxScale();
210 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
211 // Hinted text looks far better at small resolutions
212 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700213 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700214 return false;
215 }
216
bsalomonafcd7cd2015-08-31 12:39:41 -0700217 bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700218#if SK_FORCE_DISTANCE_FIELD_TEXT
219 useDFT = true;
220#endif
221
jvanverth4854d132015-06-22 06:46:56 -0700222 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700223 return false;
224 }
225
226 // rasterizers and mask filters modify alpha, which doesn't
227 // translate well to distance
228 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700229 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700230 return false;
231 }
232
233 // TODO: add some stroking support
234 if (skPaint.getStyle() != SkPaint::kFill_Style) {
235 return false;
236 }
237
238 return true;
239}
240
joshualitt374b2f72015-07-21 08:05:03 -0700241void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700242 const SkPaint& skPaint, GrColor color,
243 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700244 const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualitta6bf4c52016-01-19 06:59:29 -0800245 SkDrawFilter* drawFilter) {
herbe59124e2015-11-18 10:54:39 -0800246 // The color here is the GrPaint color, and it is used to determine whether we
jvanverth0628a522015-08-18 07:44:22 -0700247 // have to regenerate LCD text blobs.
248 // We use this color vs the SkPaint color because it has the colorfilter applied.
249 cacheBlob->fPaintColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -0700250 cacheBlob->fViewMatrix = viewMatrix;
251 cacheBlob->fX = x;
252 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700253
254 // Regenerate textblob
255 SkPaint runPaint = skPaint;
halcanary33779752015-10-27 14:01:05 -0700256 SkTextBlobRunIterator it(blob);
joshualitt1d89e8d2015-04-01 12:40:54 -0700257 for (int run = 0; !it.done(); it.next(), run++) {
258 int glyphCount = it.glyphCount();
259 size_t textLen = glyphCount * sizeof(uint16_t);
260 const SkPoint& offset = it.offset();
261 // applyFontToPaint() always overwrites the exact same attributes,
262 // so it is safe to not re-seed the paint for this reason.
263 it.applyFontToPaint(&runPaint);
264
265 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
266 // A false return from filter() means we should abort the current draw.
267 runPaint = skPaint;
268 continue;
269 }
270
robertphillipsfcf78292015-06-19 11:49:52 -0700271 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700272
joshualitt18b072d2015-12-07 12:26:12 -0800273 cacheBlob->push_back_run(run);
joshualitt1d89e8d2015-04-01 12:40:54 -0700274
joshualittfcfb9fc2015-04-21 07:35:10 -0700275 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700276 switch (it.positioning()) {
277 case SkTextBlob::kDefault_Positioning: {
joshualitta6bf4c52016-01-19 06:59:29 -0800278 this->internalDrawDFText(cacheBlob, run, runPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700279 (const char *)it.glyphs(), textLen,
joshualitta6bf4c52016-01-19 06:59:29 -0800280 x + offset.x(), y + offset.y());
joshualittfcfb9fc2015-04-21 07:35:10 -0700281 break;
282 }
283 case SkTextBlob::kHorizontal_Positioning: {
joshualitt5425a9a2015-12-11 11:05:43 -0800284 SkPoint dfOffset = SkPoint::Make(x, y + offset.y());
joshualitta6bf4c52016-01-19 06:59:29 -0800285 this->internalDrawDFPosText(cacheBlob, run, runPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700286 (const char*)it.glyphs(), textLen, it.pos(),
joshualitta6bf4c52016-01-19 06:59:29 -0800287 1, dfOffset);
joshualittfcfb9fc2015-04-21 07:35:10 -0700288 break;
289 }
290 case SkTextBlob::kFull_Positioning: {
joshualitt5425a9a2015-12-11 11:05:43 -0800291 SkPoint dfOffset = SkPoint::Make(x, y);
joshualitta6bf4c52016-01-19 06:59:29 -0800292 this->internalDrawDFPosText(cacheBlob, run, runPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700293 (const char*)it.glyphs(), textLen, it.pos(),
joshualitta6bf4c52016-01-19 06:59:29 -0800294 2, dfOffset);
joshualittfcfb9fc2015-04-21 07:35:10 -0700295 break;
296 }
297 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700298 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
299 cacheBlob->fRuns[run].fDrawAsPaths = true;
300 } else {
joshualittfcfb9fc2015-04-21 07:35:10 -0700301 switch (it.positioning()) {
302 case SkTextBlob::kDefault_Positioning:
joshualitt29677982015-12-11 06:08:59 -0800303 GrTextUtils::DrawBmpText(cacheBlob, run, fContext->getBatchFontCache(),
joshualitte76b4bb32015-12-28 07:23:58 -0800304 fSurfaceProps, runPaint, color, viewMatrix,
joshualitt29677982015-12-11 06:08:59 -0800305 (const char *)it.glyphs(), textLen,
306 x + offset.x(), y + offset.y());
joshualittfcfb9fc2015-04-21 07:35:10 -0700307 break;
308 case SkTextBlob::kHorizontal_Positioning:
joshualitt29677982015-12-11 06:08:59 -0800309 GrTextUtils::DrawBmpPosText(cacheBlob, run, fContext->getBatchFontCache(),
joshualitte76b4bb32015-12-28 07:23:58 -0800310 fSurfaceProps, runPaint, color, viewMatrix,
joshualitt29677982015-12-11 06:08:59 -0800311 (const char*)it.glyphs(), textLen, it.pos(), 1,
312 SkPoint::Make(x, y + offset.y()));
joshualittfcfb9fc2015-04-21 07:35:10 -0700313 break;
314 case SkTextBlob::kFull_Positioning:
joshualitt29677982015-12-11 06:08:59 -0800315 GrTextUtils::DrawBmpPosText(cacheBlob, run, fContext->getBatchFontCache(),
joshualitte76b4bb32015-12-28 07:23:58 -0800316 fSurfaceProps, runPaint, color, viewMatrix,
joshualitt29677982015-12-11 06:08:59 -0800317 (const char*)it.glyphs(), textLen, it.pos(), 2,
318 SkPoint::Make(x, y));
joshualittfcfb9fc2015-04-21 07:35:10 -0700319 break;
320 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700321 }
322
323 if (drawFilter) {
324 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
325 runPaint = skPaint;
326 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700327 }
328}
329
joshualitt374b2f72015-07-21 08:05:03 -0700330inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700331 SkPaint* skPaint,
332 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700333 const SkMatrix& viewMatrix) {
334 // getMaxScale doesn't support perspective, so neither do we at the moment
335 SkASSERT(!viewMatrix.hasPerspective());
336 SkScalar maxScale = viewMatrix.getMaxScale();
337 SkScalar textSize = skPaint->getTextSize();
338 SkScalar scaledTextSize = textSize;
339 // if we have non-unity scale, we need to choose our base text size
340 // based on the SkPaint's text size multiplied by the max scale factor
341 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
342 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
343 scaledTextSize *= maxScale;
344 }
345
joshualitt64c99cc2015-04-21 09:43:03 -0700346 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
347 // and ceiling. A scale outside of this range would require regenerating the distance fields
348 SkScalar dfMaskScaleFloor;
349 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700350 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700351 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700352 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700353 *textRatio = textSize / kSmallDFFontSize;
354 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
355 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700356 dfMaskScaleFloor = kSmallDFFontLimit;
357 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700358 *textRatio = textSize / kMediumDFFontSize;
359 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
360 } else {
joshualitta7c63892015-04-21 13:24:37 -0700361 dfMaskScaleFloor = kMediumDFFontLimit;
362 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700363 *textRatio = textSize / kLargeDFFontSize;
364 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
365 }
366
joshualitt64c99cc2015-04-21 09:43:03 -0700367 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
368 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
369 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
370 // tolerate before we'd have to move to a large mip size. When we actually test these values
371 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
372 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
373 // level)
joshualitta7c63892015-04-21 13:24:37 -0700374 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700375 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
376 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
377
joshualitt9bd2daf2015-04-17 09:30:06 -0700378 skPaint->setLCDRenderText(false);
379 skPaint->setAutohinted(false);
380 skPaint->setHinting(SkPaint::kNormal_Hinting);
381 skPaint->setSubpixelText(true);
382}
383
joshualitt374b2f72015-07-21 08:05:03 -0700384inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700385 int runIndex,
jvanverth0628a522015-08-18 07:44:22 -0700386 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700387 const SkPaint& skPaint,
388 const SkMatrix& viewMatrix,
389 const SkTDArray<char>& fallbackTxt,
390 const SkTDArray<SkScalar>& fallbackPos,
391 int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800392 const SkPoint& offset) {
joshualittfec19e12015-04-17 10:32:32 -0700393 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700394 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700395 // Push back a new subrun to fill and set the override descriptor
396 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700397 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitte76b4bb32015-12-28 07:23:58 -0800398 GrTextUtils::DrawBmpPosText(blob, runIndex, fContext->getBatchFontCache(), fSurfaceProps,
399 skPaint, color, viewMatrix, fallbackTxt.begin(), fallbackTxt.count(),
joshualitt29677982015-12-11 06:08:59 -0800400 fallbackPos.begin(), scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700401}
402
joshualitt374b2f72015-07-21 08:05:03 -0700403inline GrAtlasTextBlob*
joshualitt5425a9a2015-12-11 11:05:43 -0800404GrAtlasTextContext::createDrawTextBlob(const GrPaint& paint, const SkPaint& skPaint,
joshualitt79dfb2b2015-05-11 08:58:08 -0700405 const SkMatrix& viewMatrix,
406 const char text[], size_t byteLength,
joshualitt5425a9a2015-12-11 11:05:43 -0800407 SkScalar x, SkScalar y) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700408 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700409
joshualitta6bf4c52016-01-19 06:59:29 -0800410 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
411 blob->fViewMatrix = viewMatrix;
412
joshualitt9bd2daf2015-04-17 09:30:06 -0700413 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
joshualitta6bf4c52016-01-19 06:59:29 -0800414 this->internalDrawDFText(blob, 0, skPaint, paint.getColor(), viewMatrix, text,
415 byteLength, x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700416 } else {
joshualitte76b4bb32015-12-28 07:23:58 -0800417 GrTextUtils::DrawBmpText(blob, 0, fContext->getBatchFontCache(), fSurfaceProps, skPaint,
joshualitt29677982015-12-11 06:08:59 -0800418 paint.getColor(), viewMatrix, text, byteLength, x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700419 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700420 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700421}
422
joshualitt374b2f72015-07-21 08:05:03 -0700423inline GrAtlasTextBlob*
joshualitt5425a9a2015-12-11 11:05:43 -0800424GrAtlasTextContext::createDrawPosTextBlob(const GrPaint& paint, const SkPaint& skPaint,
joshualitt79dfb2b2015-05-11 08:58:08 -0700425 const SkMatrix& viewMatrix,
426 const char text[], size_t byteLength,
427 const SkScalar pos[], int scalarsPerPosition,
joshualitt5425a9a2015-12-11 11:05:43 -0800428 const SkPoint& offset) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700429 int glyphCount = skPaint.countText(text, byteLength);
430
joshualitta6bf4c52016-01-19 06:59:29 -0800431 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
432 blob->fViewMatrix = viewMatrix;
joshualitt9bd2daf2015-04-17 09:30:06 -0700433
joshualitta6bf4c52016-01-19 06:59:29 -0800434 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
435 this->internalDrawDFPosText(blob, 0, skPaint, paint.getColor(), viewMatrix, text,
436 byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700437 } else {
joshualitte76b4bb32015-12-28 07:23:58 -0800438 GrTextUtils::DrawBmpPosText(blob, 0, fContext->getBatchFontCache(), fSurfaceProps, skPaint,
joshualitt29677982015-12-11 06:08:59 -0800439 paint.getColor(), viewMatrix, text,
440 byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700441 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700442 return blob;
443}
444
robertphillips433625e2015-12-04 06:58:16 -0800445void GrAtlasTextContext::onDrawText(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -0800446 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700447 const GrPaint& paint, const SkPaint& skPaint,
448 const SkMatrix& viewMatrix,
449 const char text[], size_t byteLength,
450 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700451 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt5425a9a2015-12-11 11:05:43 -0800452 this->createDrawTextBlob(paint, skPaint, viewMatrix, text, byteLength, x, y));
joshualitt2e2202e2015-12-10 11:22:08 -0800453 blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint,
454 clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700455}
456
robertphillips433625e2015-12-04 06:58:16 -0800457void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700458 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700459 const GrPaint& paint, const SkPaint& skPaint,
460 const SkMatrix& viewMatrix,
461 const char text[], size_t byteLength,
462 const SkScalar pos[], int scalarsPerPosition,
463 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700464 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt5425a9a2015-12-11 11:05:43 -0800465 this->createDrawPosTextBlob(paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700466 text, byteLength,
467 pos, scalarsPerPosition,
joshualitt5425a9a2015-12-11 11:05:43 -0800468 offset));
joshualitt79dfb2b2015-05-11 08:58:08 -0700469
joshualitt2e2202e2015-12-10 11:22:08 -0800470 blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint, clip,
471 regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700472}
473
joshualitt374b2f72015-07-21 08:05:03 -0700474void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700475 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700476 const SkMatrix& viewMatrix,
477 const char text[], size_t byteLength,
joshualitta6bf4c52016-01-19 06:59:29 -0800478 SkScalar x, SkScalar y) {
halcanary96fcdcc2015-08-27 07:41:13 -0700479 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700480
481 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700482 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700483 return;
484 }
485
joshualitta6bf4c52016-01-19 06:59:29 -0800486 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
joshualitt9bd2daf2015-04-17 09:30:06 -0700487 SkAutoDescriptor desc;
joshualitta6bf4c52016-01-19 06:59:29 -0800488 skPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
489 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(skPaint.getTypeface(),
joshualitt9bd2daf2015-04-17 09:30:06 -0700490 desc.getDesc());
491
492 SkTArray<SkScalar> positions;
493
494 const char* textPtr = text;
495 SkFixed stopX = 0;
496 SkFixed stopY = 0;
497 SkFixed origin = 0;
joshualitta6bf4c52016-01-19 06:59:29 -0800498 switch (skPaint.getTextAlign()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700499 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
500 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
501 case SkPaint::kLeft_Align: origin = 0; break;
502 }
503
504 SkAutoKern autokern;
505 const char* stop = text + byteLength;
506 while (textPtr < stop) {
507 // don't need x, y here, since all subpixel variants will have the
508 // same advance
509 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
510
511 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
512 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
513
514 SkFixed height = glyph.fAdvanceY;
515 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
516
517 stopX += width;
518 stopY += height;
519 }
520 SkASSERT(textPtr == stop);
521
jvanverth157e6482015-09-09 08:05:12 -0700522 SkGlyphCache::AttachCache(origPaintCache);
523
joshualitt9bd2daf2015-04-17 09:30:06 -0700524 // now adjust starting point depending on alignment
525 SkScalar alignX = SkFixedToScalar(stopX);
526 SkScalar alignY = SkFixedToScalar(stopY);
joshualitta6bf4c52016-01-19 06:59:29 -0800527 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700528 alignX = SkScalarHalf(alignX);
529 alignY = SkScalarHalf(alignY);
joshualitta6bf4c52016-01-19 06:59:29 -0800530 } else if (skPaint.getTextAlign() == SkPaint::kLeft_Align) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700531 alignX = 0;
532 alignY = 0;
533 }
534 x -= alignX;
535 y -= alignY;
joshualitt5425a9a2015-12-11 11:05:43 -0800536 SkPoint offset = SkPoint::Make(x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700537
joshualitte76b4bb32015-12-28 07:23:58 -0800538 this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength,
joshualitta6bf4c52016-01-19 06:59:29 -0800539 positions.begin(), 2, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700540}
541
joshualitt374b2f72015-07-21 08:05:03 -0700542void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitta6bf4c52016-01-19 06:59:29 -0800543 const SkPaint& origPaint,
joshualitt5425a9a2015-12-11 11:05:43 -0800544 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700545 const SkMatrix& viewMatrix,
546 const char text[], size_t byteLength,
547 const SkScalar pos[], int scalarsPerPosition,
joshualitta6bf4c52016-01-19 06:59:29 -0800548 const SkPoint& offset) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700549
halcanary96fcdcc2015-08-27 07:41:13 -0700550 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700551 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
552
553 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700554 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700555 return;
556 }
557
joshualitt5425a9a2015-12-11 11:05:43 -0800558 SkTDArray<char> fallbackTxt;
559 SkTDArray<SkScalar> fallbackPos;
560
joshualitta6bf4c52016-01-19 06:59:29 -0800561 // Setup distance field paint and text ratio
562 SkScalar textRatio;
563 SkPaint dfPaint(origPaint);
564 this->initDistanceFieldPaint(blob, &dfPaint, &textRatio, viewMatrix);
565 blob->setHasDistanceField();
566 blob->setSubRunHasDistanceFields(runIndex, origPaint.isLCDRenderText());
567
halcanary96fcdcc2015-08-27 07:41:13 -0700568 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -0700569
joshualitta6bf4c52016-01-19 06:59:29 -0800570 SkGlyphCache* cache = blob->setupCache(runIndex, fSurfaceProps, dfPaint, nullptr, true);
571 SkDrawCacheProc glyphCacheProc = dfPaint.getDrawCacheProc();
joshualitt9bd2daf2015-04-17 09:30:06 -0700572 GrFontScaler* fontScaler = GetGrFontScaler(cache);
573
574 const char* stop = text + byteLength;
575
joshualitta6bf4c52016-01-19 06:59:29 -0800576 if (SkPaint::kLeft_Align == dfPaint.getTextAlign()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700577 while (text < stop) {
578 const char* lastText = text;
579 // the last 2 parameters are ignored
580 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
581
582 if (glyph.fWidth) {
583 SkScalar x = offset.x() + pos[0];
584 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
585
586 if (!this->dfAppendGlyph(blob,
587 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700588 glyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800589 x, y, color, fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700590 textRatio, viewMatrix)) {
591 // couldn't append, send to fallback
joshualitt5425a9a2015-12-11 11:05:43 -0800592 fallbackTxt.append(SkToInt(text-lastText), lastText);
593 *fallbackPos.append() = pos[0];
joshualitt9bd2daf2015-04-17 09:30:06 -0700594 if (2 == scalarsPerPosition) {
joshualitt5425a9a2015-12-11 11:05:43 -0800595 *fallbackPos.append() = pos[1];
joshualitt9bd2daf2015-04-17 09:30:06 -0700596 }
597 }
598 }
599 pos += scalarsPerPosition;
600 }
601 } else {
joshualitta6bf4c52016-01-19 06:59:29 -0800602 SkScalar alignMul = SkPaint::kCenter_Align == dfPaint.getTextAlign() ? SK_ScalarHalf
joshualitt9bd2daf2015-04-17 09:30:06 -0700603 : SK_Scalar1;
604 while (text < stop) {
605 const char* lastText = text;
606 // the last 2 parameters are ignored
607 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
608
609 if (glyph.fWidth) {
610 SkScalar x = offset.x() + pos[0];
611 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
612
613 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
614 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
615
616 if (!this->dfAppendGlyph(blob,
617 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700618 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700619 x - advanceX, y - advanceY, color,
620 fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700621 textRatio,
622 viewMatrix)) {
623 // couldn't append, send to fallback
joshualitt5425a9a2015-12-11 11:05:43 -0800624 fallbackTxt.append(SkToInt(text-lastText), lastText);
625 *fallbackPos.append() = pos[0];
joshualitt9bd2daf2015-04-17 09:30:06 -0700626 if (2 == scalarsPerPosition) {
joshualitt5425a9a2015-12-11 11:05:43 -0800627 *fallbackPos.append() = pos[1];
joshualitt9bd2daf2015-04-17 09:30:06 -0700628 }
629 }
630 }
631 pos += scalarsPerPosition;
632 }
633 }
jvanverth157e6482015-09-09 08:05:12 -0700634
joshualitte76b4bb32015-12-28 07:23:58 -0800635 SkGlyphCache::AttachCache(cache);
joshualitt5425a9a2015-12-11 11:05:43 -0800636 if (fallbackTxt.count()) {
637 this->fallbackDrawPosText(blob, runIndex, origPaint.getColor(), origPaint, viewMatrix,
638 fallbackTxt, fallbackPos, scalarsPerPosition, offset);
639 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700640}
641
joshualitt374b2f72015-07-21 08:05:03 -0700642bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700643 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700644 SkScalar sx, SkScalar sy, GrColor color,
645 GrFontScaler* scaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700646 SkScalar textRatio, const SkMatrix& viewMatrix) {
647 if (!fCurrStrike) {
648 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
649 }
650
joshualitt6c2c2b02015-07-24 10:37:00 -0700651 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
652 skGlyph.getSubXFixed(),
653 skGlyph.getSubYFixed(),
654 GrGlyph::kDistance_MaskStyle);
655 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700656 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700657 return true;
658 }
659
660 // fallback to color glyph support
661 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
662 return false;
663 }
664
665 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
666 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
667 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
668 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
669
670 SkScalar scale = textRatio;
671 dx *= scale;
672 dy *= scale;
673 width *= scale;
674 height *= scale;
675 sx += dx;
676 sy += dy;
677 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
678
joshualitta06e6ab2015-12-10 08:54:41 -0800679 blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph, scaler, skGlyph,
680 sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700681 return true;
682}
683
joshualitt79dfb2b2015-05-11 08:58:08 -0700684///////////////////////////////////////////////////////////////////////////////////////////////////
685
686#ifdef GR_TEST_UTILS
687
bsalomonabd30f52015-08-13 13:34:48 -0700688DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700689 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -0700690 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -0700691 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700692
693 if (context->uniqueID() != gContextID) {
694 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -0700695 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -0700696
joshualitt79dfb2b2015-05-11 08:58:08 -0700697 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
698 // because we don't really want to have a gpu device here.
699 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -0700700 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -0700701 }
702
joshualitt79dfb2b2015-05-11 08:58:08 -0700703 // Setup dummy SkPaint / GrPaint
704 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -0700705 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -0700706 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -0700707 skPaint.setColor(color);
708 skPaint.setLCDRenderText(random->nextBool());
709 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
710 skPaint.setSubpixelText(random->nextBool());
711
712 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700713 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700714 SkFAIL("couldn't convert paint\n");
715 }
716
717 const char* text = "The quick brown fox jumps over the lazy dog.";
718 int textLen = (int)strlen(text);
719
720 // Setup clip
721 GrClip clip;
joshualitt79dfb2b2015-05-11 08:58:08 -0700722
723 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
724 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -0700725 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt5425a9a2015-12-11 11:05:43 -0800726 gTextContext->createDrawTextBlob(grPaint, skPaint, viewMatrix, text,
727 static_cast<size_t>(textLen), 0, 0));
joshualitt79dfb2b2015-05-11 08:58:08 -0700728
729 SkScalar transX = static_cast<SkScalar>(random->nextU());
730 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -0700731 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt2e2202e2015-12-10 11:22:08 -0800732 return blob->createBatch(info, textLen, 0, 0, color, transX, transY, skPaint,
733 gSurfaceProps, gTextContext->dfAdjustTable(),
734 context->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -0700735}
736
737#endif