blob: 37cd53d08061bc51e8c82e36c29cf9126fd58b70 [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,
robertphillips6ee690e2015-12-02 08:57:50 -0800169 blob, x, y, drawFilter, clip);
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,
robertphillips6ee690e2015-12-02 08:57:50 -0800181 blob, x, y, drawFilter, clip);
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,
robertphillips6ee690e2015-12-02 08:57:50 -0800195 blob, x, y, drawFilter, clip);
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,
robertphillips6ee690e2015-12-02 08:57:50 -0800245 SkDrawFilter* drawFilter,
246 const GrClip& clip) {
herbe59124e2015-11-18 10:54:39 -0800247 // The color here is the GrPaint color, and it is used to determine whether we
jvanverth0628a522015-08-18 07:44:22 -0700248 // have to regenerate LCD text blobs.
249 // We use this color vs the SkPaint color because it has the colorfilter applied.
250 cacheBlob->fPaintColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -0700251 cacheBlob->fViewMatrix = viewMatrix;
252 cacheBlob->fX = x;
253 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700254
255 // Regenerate textblob
256 SkPaint runPaint = skPaint;
halcanary33779752015-10-27 14:01:05 -0700257 SkTextBlobRunIterator it(blob);
joshualitt1d89e8d2015-04-01 12:40:54 -0700258 for (int run = 0; !it.done(); it.next(), run++) {
259 int glyphCount = it.glyphCount();
260 size_t textLen = glyphCount * sizeof(uint16_t);
261 const SkPoint& offset = it.offset();
262 // applyFontToPaint() always overwrites the exact same attributes,
263 // so it is safe to not re-seed the paint for this reason.
264 it.applyFontToPaint(&runPaint);
265
266 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
267 // A false return from filter() means we should abort the current draw.
268 runPaint = skPaint;
269 continue;
270 }
271
robertphillipsfcf78292015-06-19 11:49:52 -0700272 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700273
joshualitt18b072d2015-12-07 12:26:12 -0800274 cacheBlob->push_back_run(run);
joshualitt1d89e8d2015-04-01 12:40:54 -0700275
joshualittfcfb9fc2015-04-21 07:35:10 -0700276 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
277 cacheBlob->setHasDistanceField();
278 SkPaint dfPaint = runPaint;
279 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700280 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700281 Run& runIdx = cacheBlob->fRuns[run];
282 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800283 subRun.setUseLCDText(runPaint.isLCDRenderText());
284 subRun.setDrawAsDistanceFields();
joshualitt9a27e632015-04-06 10:53:36 -0700285
joshualittfcfb9fc2015-04-21 07:35:10 -0700286 switch (it.positioning()) {
287 case SkTextBlob::kDefault_Positioning: {
joshualitte76b4bb32015-12-28 07:23:58 -0800288 this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700289 (const char *)it.glyphs(), textLen,
joshualitt5425a9a2015-12-11 11:05:43 -0800290 x + offset.x(), y + offset.y(), textRatio, runPaint);
joshualittfcfb9fc2015-04-21 07:35:10 -0700291 break;
292 }
293 case SkTextBlob::kHorizontal_Positioning: {
joshualitt5425a9a2015-12-11 11:05:43 -0800294 SkPoint dfOffset = SkPoint::Make(x, y + offset.y());
joshualitte76b4bb32015-12-28 07:23:58 -0800295 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700296 (const char*)it.glyphs(), textLen, it.pos(),
joshualitt5425a9a2015-12-11 11:05:43 -0800297 1, dfOffset, textRatio,
298 runPaint);
joshualittfcfb9fc2015-04-21 07:35:10 -0700299 break;
300 }
301 case SkTextBlob::kFull_Positioning: {
joshualitt5425a9a2015-12-11 11:05:43 -0800302 SkPoint dfOffset = SkPoint::Make(x, y);
joshualitte76b4bb32015-12-28 07:23:58 -0800303 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700304 (const char*)it.glyphs(), textLen, it.pos(),
joshualitt5425a9a2015-12-11 11:05:43 -0800305 2, dfOffset, textRatio, runPaint);
joshualittfcfb9fc2015-04-21 07:35:10 -0700306 break;
307 }
308 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700309 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
310 cacheBlob->fRuns[run].fDrawAsPaths = true;
311 } else {
312 cacheBlob->setHasBitmap();
joshualittfcfb9fc2015-04-21 07:35:10 -0700313 switch (it.positioning()) {
314 case SkTextBlob::kDefault_Positioning:
joshualitt29677982015-12-11 06:08:59 -0800315 GrTextUtils::DrawBmpText(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,
318 x + offset.x(), y + offset.y());
joshualittfcfb9fc2015-04-21 07:35:10 -0700319 break;
320 case SkTextBlob::kHorizontal_Positioning:
joshualitt29677982015-12-11 06:08:59 -0800321 GrTextUtils::DrawBmpPosText(cacheBlob, run, fContext->getBatchFontCache(),
joshualitte76b4bb32015-12-28 07:23:58 -0800322 fSurfaceProps, runPaint, color, viewMatrix,
joshualitt29677982015-12-11 06:08:59 -0800323 (const char*)it.glyphs(), textLen, it.pos(), 1,
324 SkPoint::Make(x, y + offset.y()));
joshualittfcfb9fc2015-04-21 07:35:10 -0700325 break;
326 case SkTextBlob::kFull_Positioning:
joshualitt29677982015-12-11 06:08:59 -0800327 GrTextUtils::DrawBmpPosText(cacheBlob, run, fContext->getBatchFontCache(),
joshualitte76b4bb32015-12-28 07:23:58 -0800328 fSurfaceProps, runPaint, color, viewMatrix,
joshualitt29677982015-12-11 06:08:59 -0800329 (const char*)it.glyphs(), textLen, it.pos(), 2,
330 SkPoint::Make(x, y));
joshualittfcfb9fc2015-04-21 07:35:10 -0700331 break;
332 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700333 }
334
335 if (drawFilter) {
336 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
337 runPaint = skPaint;
338 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700339 }
340}
341
joshualitt374b2f72015-07-21 08:05:03 -0700342inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700343 SkPaint* skPaint,
344 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700345 const SkMatrix& viewMatrix) {
346 // getMaxScale doesn't support perspective, so neither do we at the moment
347 SkASSERT(!viewMatrix.hasPerspective());
348 SkScalar maxScale = viewMatrix.getMaxScale();
349 SkScalar textSize = skPaint->getTextSize();
350 SkScalar scaledTextSize = textSize;
351 // if we have non-unity scale, we need to choose our base text size
352 // based on the SkPaint's text size multiplied by the max scale factor
353 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
354 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
355 scaledTextSize *= maxScale;
356 }
357
joshualitt64c99cc2015-04-21 09:43:03 -0700358 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
359 // and ceiling. A scale outside of this range would require regenerating the distance fields
360 SkScalar dfMaskScaleFloor;
361 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700362 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700363 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700364 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700365 *textRatio = textSize / kSmallDFFontSize;
366 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
367 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700368 dfMaskScaleFloor = kSmallDFFontLimit;
369 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700370 *textRatio = textSize / kMediumDFFontSize;
371 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
372 } else {
joshualitta7c63892015-04-21 13:24:37 -0700373 dfMaskScaleFloor = kMediumDFFontLimit;
374 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700375 *textRatio = textSize / kLargeDFFontSize;
376 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
377 }
378
joshualitt64c99cc2015-04-21 09:43:03 -0700379 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
380 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
381 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
382 // tolerate before we'd have to move to a large mip size. When we actually test these values
383 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
384 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
385 // level)
joshualitta7c63892015-04-21 13:24:37 -0700386 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700387 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
388 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
389
joshualitt9bd2daf2015-04-17 09:30:06 -0700390 skPaint->setLCDRenderText(false);
391 skPaint->setAutohinted(false);
392 skPaint->setHinting(SkPaint::kNormal_Hinting);
393 skPaint->setSubpixelText(true);
394}
395
joshualitt374b2f72015-07-21 08:05:03 -0700396inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700397 int runIndex,
jvanverth0628a522015-08-18 07:44:22 -0700398 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700399 const SkPaint& skPaint,
400 const SkMatrix& viewMatrix,
401 const SkTDArray<char>& fallbackTxt,
402 const SkTDArray<SkScalar>& fallbackPos,
403 int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800404 const SkPoint& offset) {
joshualittfec19e12015-04-17 10:32:32 -0700405 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700406 blob->setHasBitmap();
407 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700408 // Push back a new subrun to fill and set the override descriptor
409 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700410 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitte76b4bb32015-12-28 07:23:58 -0800411 GrTextUtils::DrawBmpPosText(blob, runIndex, fContext->getBatchFontCache(), fSurfaceProps,
412 skPaint, color, viewMatrix, fallbackTxt.begin(), fallbackTxt.count(),
joshualitt29677982015-12-11 06:08:59 -0800413 fallbackPos.begin(), scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700414}
415
joshualitt374b2f72015-07-21 08:05:03 -0700416inline GrAtlasTextBlob*
herbe59124e2015-11-18 10:54:39 -0800417GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
418 const SkMatrix& viewMatrix, SkPaint* dfPaint,
jvanverth157e6482015-09-09 08:05:12 -0700419 SkScalar* textRatio) {
joshualittf528e0d2015-12-09 06:42:52 -0800420 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700421
422 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700423 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700424 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700425 Run& run = blob->fRuns[0];
426 PerSubRunInfo& subRun = run.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800427 subRun.setUseLCDText(origPaint.isLCDRenderText());
428 subRun.setDrawAsDistanceFields();
joshualitt9bd2daf2015-04-17 09:30:06 -0700429
joshualitt9bd2daf2015-04-17 09:30:06 -0700430 return blob;
431}
432
joshualitt374b2f72015-07-21 08:05:03 -0700433inline GrAtlasTextBlob*
joshualitt5425a9a2015-12-11 11:05:43 -0800434GrAtlasTextContext::createDrawTextBlob(const GrPaint& paint, const SkPaint& skPaint,
joshualitt79dfb2b2015-05-11 08:58:08 -0700435 const SkMatrix& viewMatrix,
436 const char text[], size_t byteLength,
joshualitt5425a9a2015-12-11 11:05:43 -0800437 SkScalar x, SkScalar y) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700438 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700439
joshualitt374b2f72015-07-21 08:05:03 -0700440 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700441 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
442 SkPaint dfPaint;
443 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700444 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700445
joshualitte76b4bb32015-12-28 07:23:58 -0800446 this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
joshualitt5425a9a2015-12-11 11:05:43 -0800447 byteLength, x, y, textRatio, skPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700448 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800449 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700450 blob->fViewMatrix = viewMatrix;
451
joshualitte76b4bb32015-12-28 07:23:58 -0800452 GrTextUtils::DrawBmpText(blob, 0, fContext->getBatchFontCache(), fSurfaceProps, skPaint,
joshualitt29677982015-12-11 06:08:59 -0800453 paint.getColor(), viewMatrix, text, byteLength, x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700454 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700455 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700456}
457
joshualitt374b2f72015-07-21 08:05:03 -0700458inline GrAtlasTextBlob*
joshualitt5425a9a2015-12-11 11:05:43 -0800459GrAtlasTextContext::createDrawPosTextBlob(const GrPaint& paint, const SkPaint& skPaint,
joshualitt79dfb2b2015-05-11 08:58:08 -0700460 const SkMatrix& viewMatrix,
461 const char text[], size_t byteLength,
462 const SkScalar pos[], int scalarsPerPosition,
joshualitt5425a9a2015-12-11 11:05:43 -0800463 const SkPoint& offset) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700464 int glyphCount = skPaint.countText(text, byteLength);
465
joshualitt374b2f72015-07-21 08:05:03 -0700466 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700467 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
468 SkPaint dfPaint;
469 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700470 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700471
joshualitte76b4bb32015-12-28 07:23:58 -0800472 this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
joshualitt5425a9a2015-12-11 11:05:43 -0800473 byteLength, pos, scalarsPerPosition, offset, textRatio,
474 skPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700475 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800476 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700477 blob->fViewMatrix = viewMatrix;
joshualitte76b4bb32015-12-28 07:23:58 -0800478 GrTextUtils::DrawBmpPosText(blob, 0, fContext->getBatchFontCache(), fSurfaceProps, skPaint,
joshualitt29677982015-12-11 06:08:59 -0800479 paint.getColor(), viewMatrix, text,
480 byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700481 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700482 return blob;
483}
484
robertphillips433625e2015-12-04 06:58:16 -0800485void GrAtlasTextContext::onDrawText(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -0800486 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700487 const GrPaint& paint, const SkPaint& skPaint,
488 const SkMatrix& viewMatrix,
489 const char text[], size_t byteLength,
490 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700491 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt5425a9a2015-12-11 11:05:43 -0800492 this->createDrawTextBlob(paint, skPaint, viewMatrix, text, byteLength, x, y));
joshualitt2e2202e2015-12-10 11:22:08 -0800493 blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint,
494 clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700495}
496
robertphillips433625e2015-12-04 06:58:16 -0800497void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700498 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700499 const GrPaint& paint, const SkPaint& skPaint,
500 const SkMatrix& viewMatrix,
501 const char text[], size_t byteLength,
502 const SkScalar pos[], int scalarsPerPosition,
503 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700504 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt5425a9a2015-12-11 11:05:43 -0800505 this->createDrawPosTextBlob(paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700506 text, byteLength,
507 pos, scalarsPerPosition,
joshualitt5425a9a2015-12-11 11:05:43 -0800508 offset));
joshualitt79dfb2b2015-05-11 08:58:08 -0700509
joshualitt2e2202e2015-12-10 11:22:08 -0800510 blob->flushThrowaway(fContext, dc, fSurfaceProps, fDistanceAdjustTable, skPaint, paint, clip,
511 regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700512}
513
joshualitt374b2f72015-07-21 08:05:03 -0700514void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700515 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700516 const SkMatrix& viewMatrix,
517 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800518 SkScalar x, SkScalar y,
joshualitt9bd2daf2015-04-17 09:30:06 -0700519 SkScalar textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700520 const SkPaint& origPaint) {
halcanary96fcdcc2015-08-27 07:41:13 -0700521 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700522
523 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700524 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700525 return;
526 }
527
528 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
529 SkAutoDescriptor desc;
halcanary96fcdcc2015-08-27 07:41:13 -0700530 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
herbe59124e2015-11-18 10:54:39 -0800531 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
joshualitt9bd2daf2015-04-17 09:30:06 -0700532 desc.getDesc());
533
534 SkTArray<SkScalar> positions;
535
536 const char* textPtr = text;
537 SkFixed stopX = 0;
538 SkFixed stopY = 0;
539 SkFixed origin = 0;
540 switch (origPaint.getTextAlign()) {
541 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
542 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
543 case SkPaint::kLeft_Align: origin = 0; break;
544 }
545
546 SkAutoKern autokern;
547 const char* stop = text + byteLength;
548 while (textPtr < stop) {
549 // don't need x, y here, since all subpixel variants will have the
550 // same advance
551 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
552
553 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
554 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
555
556 SkFixed height = glyph.fAdvanceY;
557 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
558
559 stopX += width;
560 stopY += height;
561 }
562 SkASSERT(textPtr == stop);
563
jvanverth157e6482015-09-09 08:05:12 -0700564 SkGlyphCache::AttachCache(origPaintCache);
565
joshualitt9bd2daf2015-04-17 09:30:06 -0700566 // now adjust starting point depending on alignment
567 SkScalar alignX = SkFixedToScalar(stopX);
568 SkScalar alignY = SkFixedToScalar(stopY);
569 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
570 alignX = SkScalarHalf(alignX);
571 alignY = SkScalarHalf(alignY);
572 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
573 alignX = 0;
574 alignY = 0;
575 }
576 x -= alignX;
577 y -= alignY;
joshualitt5425a9a2015-12-11 11:05:43 -0800578 SkPoint offset = SkPoint::Make(x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700579
joshualitte76b4bb32015-12-28 07:23:58 -0800580 this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength,
joshualitt5425a9a2015-12-11 11:05:43 -0800581 positions.begin(), 2, offset, textRatio, origPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700582}
583
joshualitt374b2f72015-07-21 08:05:03 -0700584void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt5425a9a2015-12-11 11:05:43 -0800585 const SkPaint& skPaint,
586 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700587 const SkMatrix& viewMatrix,
588 const char text[], size_t byteLength,
589 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800590 const SkPoint& offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700591 SkScalar textRatio,
joshualitt5425a9a2015-12-11 11:05:43 -0800592 const SkPaint& origPaint) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700593
halcanary96fcdcc2015-08-27 07:41:13 -0700594 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700595 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
596
597 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700598 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700599 return;
600 }
601
joshualitt5425a9a2015-12-11 11:05:43 -0800602 SkTDArray<char> fallbackTxt;
603 SkTDArray<SkScalar> fallbackPos;
604
halcanary96fcdcc2015-08-27 07:41:13 -0700605 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -0700606
joshualitte76b4bb32015-12-28 07:23:58 -0800607 SkGlyphCache* cache = blob->setupCache(runIndex, fSurfaceProps, skPaint, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700608 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
609 GrFontScaler* fontScaler = GetGrFontScaler(cache);
610
611 const char* stop = text + byteLength;
612
613 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
614 while (text < stop) {
615 const char* lastText = text;
616 // the last 2 parameters are ignored
617 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
618
619 if (glyph.fWidth) {
620 SkScalar x = offset.x() + pos[0];
621 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
622
623 if (!this->dfAppendGlyph(blob,
624 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700625 glyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800626 x, y, color, fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700627 textRatio, viewMatrix)) {
628 // couldn't append, send to fallback
joshualitt5425a9a2015-12-11 11:05:43 -0800629 fallbackTxt.append(SkToInt(text-lastText), lastText);
630 *fallbackPos.append() = pos[0];
joshualitt9bd2daf2015-04-17 09:30:06 -0700631 if (2 == scalarsPerPosition) {
joshualitt5425a9a2015-12-11 11:05:43 -0800632 *fallbackPos.append() = pos[1];
joshualitt9bd2daf2015-04-17 09:30:06 -0700633 }
634 }
635 }
636 pos += scalarsPerPosition;
637 }
638 } else {
639 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
640 : SK_Scalar1;
641 while (text < stop) {
642 const char* lastText = text;
643 // the last 2 parameters are ignored
644 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
645
646 if (glyph.fWidth) {
647 SkScalar x = offset.x() + pos[0];
648 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
649
650 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
651 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
652
653 if (!this->dfAppendGlyph(blob,
654 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700655 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700656 x - advanceX, y - advanceY, color,
657 fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700658 textRatio,
659 viewMatrix)) {
660 // couldn't append, send to fallback
joshualitt5425a9a2015-12-11 11:05:43 -0800661 fallbackTxt.append(SkToInt(text-lastText), lastText);
662 *fallbackPos.append() = pos[0];
joshualitt9bd2daf2015-04-17 09:30:06 -0700663 if (2 == scalarsPerPosition) {
joshualitt5425a9a2015-12-11 11:05:43 -0800664 *fallbackPos.append() = pos[1];
joshualitt9bd2daf2015-04-17 09:30:06 -0700665 }
666 }
667 }
668 pos += scalarsPerPosition;
669 }
670 }
jvanverth157e6482015-09-09 08:05:12 -0700671
joshualitte76b4bb32015-12-28 07:23:58 -0800672 SkGlyphCache::AttachCache(cache);
joshualitt5425a9a2015-12-11 11:05:43 -0800673 if (fallbackTxt.count()) {
674 this->fallbackDrawPosText(blob, runIndex, origPaint.getColor(), origPaint, viewMatrix,
675 fallbackTxt, fallbackPos, scalarsPerPosition, offset);
676 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700677}
678
joshualitt374b2f72015-07-21 08:05:03 -0700679bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700680 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700681 SkScalar sx, SkScalar sy, GrColor color,
682 GrFontScaler* scaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700683 SkScalar textRatio, const SkMatrix& viewMatrix) {
684 if (!fCurrStrike) {
685 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
686 }
687
joshualitt6c2c2b02015-07-24 10:37:00 -0700688 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
689 skGlyph.getSubXFixed(),
690 skGlyph.getSubYFixed(),
691 GrGlyph::kDistance_MaskStyle);
692 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700693 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700694 return true;
695 }
696
697 // fallback to color glyph support
698 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
699 return false;
700 }
701
702 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
703 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
704 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
705 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
706
707 SkScalar scale = textRatio;
708 dx *= scale;
709 dy *= scale;
710 width *= scale;
711 height *= scale;
712 sx += dx;
713 sy += dy;
714 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
715
joshualitta06e6ab2015-12-10 08:54:41 -0800716 blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph, scaler, skGlyph,
717 sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700718 return true;
719}
720
joshualitt79dfb2b2015-05-11 08:58:08 -0700721///////////////////////////////////////////////////////////////////////////////////////////////////
722
723#ifdef GR_TEST_UTILS
724
bsalomonabd30f52015-08-13 13:34:48 -0700725DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700726 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -0700727 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -0700728 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -0700729
730 if (context->uniqueID() != gContextID) {
731 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -0700732 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -0700733
joshualitt79dfb2b2015-05-11 08:58:08 -0700734 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
735 // because we don't really want to have a gpu device here.
736 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -0700737 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -0700738 }
739
joshualitt79dfb2b2015-05-11 08:58:08 -0700740 // Setup dummy SkPaint / GrPaint
741 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -0700742 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -0700743 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -0700744 skPaint.setColor(color);
745 skPaint.setLCDRenderText(random->nextBool());
746 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
747 skPaint.setSubpixelText(random->nextBool());
748
749 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700750 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700751 SkFAIL("couldn't convert paint\n");
752 }
753
754 const char* text = "The quick brown fox jumps over the lazy dog.";
755 int textLen = (int)strlen(text);
756
757 // Setup clip
758 GrClip clip;
joshualitt79dfb2b2015-05-11 08:58:08 -0700759
760 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
761 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -0700762 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt5425a9a2015-12-11 11:05:43 -0800763 gTextContext->createDrawTextBlob(grPaint, skPaint, viewMatrix, text,
764 static_cast<size_t>(textLen), 0, 0));
joshualitt79dfb2b2015-05-11 08:58:08 -0700765
766 SkScalar transX = static_cast<SkScalar>(random->nextU());
767 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -0700768 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt2e2202e2015-12-10 11:22:08 -0800769 return blob->createBatch(info, textLen, 0, 0, color, transX, transY, skPaint,
770 gSurfaceProps, gTextContext->dfAdjustTable(),
771 context->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -0700772}
773
774#endif