blob: 153d0fe5169461a16ee92f40f757f37ca65540ee [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
joshualitt9bd2daf2015-04-17 09:30:06 -070049
50SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize = 8;)
joshualitt1d89e8d2015-04-01 12:40:54 -070051};
52
robertphillipsf6703fa2015-09-01 05:36:47 -070053GrAtlasTextContext::GrAtlasTextContext(GrContext* context, const SkSurfaceProps& surfaceProps)
54 : INHERITED(context, surfaceProps)
55 , fDistanceAdjustTable(new DistanceAdjustTable) {
joshualittb7133be2015-04-08 09:08:31 -070056 // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest
57 // vertexStride
joshualittf528e0d2015-12-09 06:42:52 -080058 static_assert(GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kColorTextVASize &&
59 GrAtlasTextBlob::kGrayTextVASize >= GrAtlasTextBlob::kLCDTextVASize,
bungeman99fe8222015-08-20 07:57:51 -070060 "vertex_attribute_changed");
halcanary96fcdcc2015-08-27 07:41:13 -070061 fCurrStrike = nullptr;
joshualittb7133be2015-04-08 09:08:31 -070062 fCache = context->getTextBlobCache();
joshualitt9bd2daf2015-04-17 09:30:06 -070063}
64
robertphillips9fc82752015-06-19 04:46:45 -070065void GrAtlasTextContext::DistanceAdjustTable::buildDistanceAdjustTable() {
joshualitt9bd2daf2015-04-17 09:30:06 -070066
67 // This is used for an approximation of the mask gamma hack, used by raster and bitmap
68 // text. The mask gamma hack is based off of guessing what the blend color is going to
69 // be, and adjusting the mask so that when run through the linear blend will
70 // produce the value closest to the desired result. However, in practice this means
71 // that the 'adjusted' mask is just increasing or decreasing the coverage of
72 // the mask depending on what it is thought it will blit against. For black (on
73 // assumed white) this means that coverages are decreased (on a curve). For white (on
74 // assumed black) this means that coverages are increased (on a a curve). At
75 // middle (perceptual) gray (which could be blit against anything) the coverages
76 // remain the same.
77 //
78 // The idea here is that instead of determining the initial (real) coverage and
79 // then adjusting that coverage, we determine an adjusted coverage directly by
80 // essentially manipulating the geometry (in this case, the distance to the glyph
81 // edge). So for black (on assumed white) this thins a bit; for white (on
82 // assumed black) this fake bolds the geometry a bit.
83 //
84 // The distance adjustment is calculated by determining the actual coverage value which
85 // when fed into in the mask gamma table gives us an 'adjusted coverage' value of 0.5. This
86 // actual coverage value (assuming it's between 0 and 1) corresponds to a distance from the
87 // actual edge. So by subtracting this distance adjustment and computing without the
88 // the coverage adjustment we should get 0.5 coverage at the same point.
89 //
90 // This has several implications:
91 // For non-gray lcd smoothed text, each subpixel essentially is using a
92 // slightly different geometry.
93 //
94 // For black (on assumed white) this may not cover some pixels which were
95 // previously covered; however those pixels would have been only slightly
96 // covered and that slight coverage would have been decreased anyway. Also, some pixels
97 // which were previously fully covered may no longer be fully covered.
98 //
99 // For white (on assumed black) this may cover some pixels which weren't
100 // previously covered at all.
101
102 int width, height;
103 size_t size;
104
105#ifdef SK_GAMMA_CONTRAST
106 SkScalar contrast = SK_GAMMA_CONTRAST;
107#else
108 SkScalar contrast = 0.5f;
109#endif
robertphillips9fc82752015-06-19 04:46:45 -0700110 SkScalar paintGamma = SK_GAMMA_EXPONENT;
111 SkScalar deviceGamma = SK_GAMMA_EXPONENT;
joshualitt9bd2daf2015-04-17 09:30:06 -0700112
113 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
114 &width, &height);
115
116 SkASSERT(kExpectedDistanceAdjustTableSize == height);
halcanary385fe4d2015-08-26 13:07:48 -0700117 fTable = new SkScalar[height];
joshualitt9bd2daf2015-04-17 09:30:06 -0700118
119 SkAutoTArray<uint8_t> data((int)size);
120 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
121
122 // find the inverse points where we cross 0.5
123 // binsearch might be better, but we only need to do this once on creation
124 for (int row = 0; row < height; ++row) {
125 uint8_t* rowPtr = data.get() + row*width;
126 for (int col = 0; col < width - 1; ++col) {
127 if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) {
128 // compute point where a mask value will give us a result of 0.5
129 float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPtr[col]);
130 float borderAlpha = (col + interp) / 255.f;
131
132 // compute t value for that alpha
133 // this is an approximate inverse for smoothstep()
134 float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5.0f) / 3.0f;
135
136 // compute distance which gives us that t value
137 const float kDistanceFieldAAFactor = 0.65f; // should match SK_DistanceFieldAAFactor
138 float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor;
139
140 fTable[row] = d;
141 break;
142 }
143 }
144 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700145}
146
joshualittdbd35932015-04-02 09:19:04 -0700147GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context,
robertphillipsfcf78292015-06-19 11:49:52 -0700148 const SkSurfaceProps& surfaceProps) {
robertphillipsf6703fa2015-09-01 05:36:47 -0700149 return new GrAtlasTextContext(context, surfaceProps);
joshualitt1d89e8d2015-04-01 12:40:54 -0700150}
151
robertphillips6ee690e2015-12-02 08:57:50 -0800152bool GrAtlasTextContext::canDraw(const SkPaint& skPaint, const SkMatrix& viewMatrix) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700153 return this->canDrawAsDistanceFields(skPaint, viewMatrix) ||
154 !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -0700155}
156
joshualitt9e36c1a2015-04-14 12:17:27 -0700157GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
158 GrColor canonicalColor = paint.computeLuminanceColor();
159 if (lcd) {
160 // This is the correct computation, but there are tons of cases where LCD can be overridden.
161 // For now we just regenerate if any run in a textblob has LCD.
162 // TODO figure out where all of these overrides are and see if we can incorporate that logic
163 // at a higher level *OR* use sRGB
164 SkASSERT(false);
165 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
166 } else {
167 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
168 // gamma corrected masks anyways, nor color
169 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
170 SkColorGetG(canonicalColor),
171 SkColorGetB(canonicalColor));
172 // reduce to our finite number of bits
173 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
174 }
175 return canonicalColor;
176}
177
178// TODO if this function ever shows up in profiling, then we can compute this value when the
179// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
180// run so this is not a big deal to compute here.
181bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -0700182 SkTextBlobRunIterator it(blob);
joshualitt9e36c1a2015-04-14 12:17:27 -0700183 for (; !it.done(); it.next()) {
184 if (it.isLCD()) {
185 return true;
186 }
187 }
188 return false;
189}
190
joshualitt374b2f72015-07-21 08:05:03 -0700191inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run,
joshualittdbd35932015-04-02 09:19:04 -0700192 const SkPaint& skPaint,
joshualitt9bd2daf2015-04-17 09:30:06 -0700193 const SkMatrix* viewMatrix,
194 bool noGamma) {
robertphillipsfcf78292015-06-19 11:49:52 -0700195 skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma);
joshualitt1d89e8d2015-04-01 12:40:54 -0700196 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
197 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc());
198}
199
robertphillips433625e2015-12-04 06:58:16 -0800200void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700201 const GrClip& clip, const SkPaint& skPaint,
202 const SkMatrix& viewMatrix, const SkTextBlob* blob,
203 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700204 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700205 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700206 if (fContext->abandoned()) {
207 return;
208 }
209
joshualitt374b2f72015-07-21 08:05:03 -0700210 SkAutoTUnref<GrAtlasTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700211 SkMaskFilter::BlurRec blurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700212 GrAtlasTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700213 // It might be worth caching these things, but its not clear at this time
214 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
215 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700216 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700217 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700218 drawFilter);
219
220 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700221 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700222
223 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
robertphillipsfcf78292015-06-19 11:49:52 -0700224 SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700225 kUnknown_SkPixelGeometry;
226
joshualitt9e36c1a2015-04-14 12:17:27 -0700227 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
228 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
229 // ensure we always match the same key
230 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
231 ComputeCanonicalColor(skPaint, hasLCD);
232
joshualitte4cee1f2015-05-11 13:04:28 -0700233 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700234 key.fUniqueID = blob->uniqueID();
235 key.fStyle = skPaint.getStyle();
236 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700237 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700238 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700239 }
240
joshualitt2a0e9f32015-04-13 06:12:21 -0700241 SkScalar transX = 0.f;
242 SkScalar transY = 0.f;
243
joshualitt9e36c1a2015-04-14 12:17:27 -0700244 // Though for the time being runs in the textblob can override the paint, they only touch font
245 // info.
246 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700247 if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700248 return;
249 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700250
joshualittb7133be2015-04-08 09:08:31 -0700251 if (cacheBlob) {
joshualittfd5f6c12015-12-10 07:44:50 -0800252 if (cacheBlob->mustRegenerate(&transX, &transY, skPaint, grPaint.getColor(), blurRec,
253 viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700254 // We have to remake the blob because changes may invalidate our masks.
255 // TODO we could probably get away reuse most of the time if the pointer is unique,
256 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700257 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700258 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800259 GrAtlasTextBlob::kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700260 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800261 blob, x, y, drawFilter, clip);
joshualittb7133be2015-04-08 09:08:31 -0700262 } else {
263 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700264#ifdef CACHE_SANITY_CHECK
265 {
266 int glyphCount = 0;
267 int runCount = 0;
268 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
269 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
270 kGrayTextVASize));
271 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
272 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800273 blob, x, y, drawFilter, clip);
joshualitt259fbf12015-07-21 11:39:34 -0700274 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
275 }
276
277#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700278 }
279 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700280 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700281 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800282 GrAtlasTextBlob::kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700283 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800284 cacheBlob.reset(fCache->createBlob(blob, GrAtlasTextBlob::kGrayTextVASize));
joshualitt2a0e9f32015-04-13 06:12:21 -0700285 }
robertphillips9c240a12015-05-28 07:45:59 -0700286 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800287 blob, x, y, drawFilter, clip);
joshualitt1d89e8d2015-04-01 12:40:54 -0700288 }
289
robertphillips433625e2015-12-04 06:58:16 -0800290 this->flush(blob, cacheBlob, dc, skPaint, grPaint, drawFilter,
joshualitt2a0e9f32015-04-13 06:12:21 -0700291 clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700292}
293
joshualitt9bd2daf2015-04-17 09:30:06 -0700294inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
295 const SkMatrix& viewMatrix) {
296 // TODO: support perspective (need getMaxScale replacement)
297 if (viewMatrix.hasPerspective()) {
298 return false;
299 }
300
301 SkScalar maxScale = viewMatrix.getMaxScale();
302 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
303 // Hinted text looks far better at small resolutions
304 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700305 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700306 return false;
307 }
308
bsalomonafcd7cd2015-08-31 12:39:41 -0700309 bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700310#if SK_FORCE_DISTANCE_FIELD_TEXT
311 useDFT = true;
312#endif
313
jvanverth4854d132015-06-22 06:46:56 -0700314 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700315 return false;
316 }
317
318 // rasterizers and mask filters modify alpha, which doesn't
319 // translate well to distance
320 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700321 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700322 return false;
323 }
324
325 // TODO: add some stroking support
326 if (skPaint.getStyle() != SkPaint::kFill_Style) {
327 return false;
328 }
329
330 return true;
331}
332
joshualitt374b2f72015-07-21 08:05:03 -0700333void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700334 const SkPaint& skPaint, GrColor color,
335 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700336 const SkTextBlob* blob, SkScalar x, SkScalar y,
robertphillips6ee690e2015-12-02 08:57:50 -0800337 SkDrawFilter* drawFilter,
338 const GrClip& clip) {
herbe59124e2015-11-18 10:54:39 -0800339 // The color here is the GrPaint color, and it is used to determine whether we
jvanverth0628a522015-08-18 07:44:22 -0700340 // have to regenerate LCD text blobs.
341 // We use this color vs the SkPaint color because it has the colorfilter applied.
342 cacheBlob->fPaintColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -0700343 cacheBlob->fViewMatrix = viewMatrix;
344 cacheBlob->fX = x;
345 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700346
347 // Regenerate textblob
348 SkPaint runPaint = skPaint;
halcanary33779752015-10-27 14:01:05 -0700349 SkTextBlobRunIterator it(blob);
joshualitt1d89e8d2015-04-01 12:40:54 -0700350 for (int run = 0; !it.done(); it.next(), run++) {
351 int glyphCount = it.glyphCount();
352 size_t textLen = glyphCount * sizeof(uint16_t);
353 const SkPoint& offset = it.offset();
354 // applyFontToPaint() always overwrites the exact same attributes,
355 // so it is safe to not re-seed the paint for this reason.
356 it.applyFontToPaint(&runPaint);
357
358 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
359 // A false return from filter() means we should abort the current draw.
360 runPaint = skPaint;
361 continue;
362 }
363
robertphillipsfcf78292015-06-19 11:49:52 -0700364 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700365
joshualitt18b072d2015-12-07 12:26:12 -0800366 cacheBlob->push_back_run(run);
joshualitt1d89e8d2015-04-01 12:40:54 -0700367
joshualittfcfb9fc2015-04-21 07:35:10 -0700368 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
369 cacheBlob->setHasDistanceField();
370 SkPaint dfPaint = runPaint;
371 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700372 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700373 Run& runIdx = cacheBlob->fRuns[run];
374 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800375 subRun.setUseLCDText(runPaint.isLCDRenderText());
376 subRun.setDrawAsDistanceFields();
joshualitt9a27e632015-04-06 10:53:36 -0700377
joshualittfcfb9fc2015-04-21 07:35:10 -0700378 SkTDArray<char> fallbackTxt;
379 SkTDArray<SkScalar> fallbackPos;
380 SkPoint dfOffset;
381 int scalarsPerPosition = 2;
382 switch (it.positioning()) {
383 case SkTextBlob::kDefault_Positioning: {
jvanverth157e6482015-09-09 08:05:12 -0700384 this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700385 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800386 x + offset.x(), y + offset.y(), textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700387 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
388 break;
389 }
390 case SkTextBlob::kHorizontal_Positioning: {
391 scalarsPerPosition = 1;
392 dfOffset = SkPoint::Make(x, y + offset.y());
jvanverth157e6482015-09-09 08:05:12 -0700393 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700394 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800395 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700396 &fallbackTxt, &fallbackPos);
397 break;
398 }
399 case SkTextBlob::kFull_Positioning: {
400 dfOffset = SkPoint::Make(x, y);
jvanverth157e6482015-09-09 08:05:12 -0700401 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700402 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800403 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700404 &fallbackTxt, &fallbackPos);
405 break;
406 }
407 }
408 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800409 this->fallbackDrawPosText(cacheBlob, run, clip, color, runPaint, viewMatrix,
410 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset);
joshualittfcfb9fc2015-04-21 07:35:10 -0700411 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700412 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
413 cacheBlob->fRuns[run].fDrawAsPaths = true;
414 } else {
415 cacheBlob->setHasBitmap();
416 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
417 false);
418 switch (it.positioning()) {
419 case SkTextBlob::kDefault_Positioning:
420 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
421 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800422 x + offset.x(), y + offset.y());
joshualittfcfb9fc2015-04-21 07:35:10 -0700423 break;
424 case SkTextBlob::kHorizontal_Positioning:
425 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
426 (const char*)it.glyphs(), textLen, it.pos(), 1,
robertphillips6ee690e2015-12-02 08:57:50 -0800427 SkPoint::Make(x, y + offset.y()));
joshualittfcfb9fc2015-04-21 07:35:10 -0700428 break;
429 case SkTextBlob::kFull_Positioning:
430 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
431 (const char*)it.glyphs(), textLen, it.pos(), 2,
robertphillips6ee690e2015-12-02 08:57:50 -0800432 SkPoint::Make(x, y));
joshualittfcfb9fc2015-04-21 07:35:10 -0700433 break;
434 }
435 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700436 }
437
438 if (drawFilter) {
439 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
440 runPaint = skPaint;
441 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700442 }
443}
444
joshualitt374b2f72015-07-21 08:05:03 -0700445inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700446 SkPaint* skPaint,
447 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700448 const SkMatrix& viewMatrix) {
449 // getMaxScale doesn't support perspective, so neither do we at the moment
450 SkASSERT(!viewMatrix.hasPerspective());
451 SkScalar maxScale = viewMatrix.getMaxScale();
452 SkScalar textSize = skPaint->getTextSize();
453 SkScalar scaledTextSize = textSize;
454 // if we have non-unity scale, we need to choose our base text size
455 // based on the SkPaint's text size multiplied by the max scale factor
456 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
457 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
458 scaledTextSize *= maxScale;
459 }
460
joshualitt64c99cc2015-04-21 09:43:03 -0700461 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
462 // and ceiling. A scale outside of this range would require regenerating the distance fields
463 SkScalar dfMaskScaleFloor;
464 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700465 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700466 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700467 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700468 *textRatio = textSize / kSmallDFFontSize;
469 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
470 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700471 dfMaskScaleFloor = kSmallDFFontLimit;
472 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700473 *textRatio = textSize / kMediumDFFontSize;
474 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
475 } else {
joshualitta7c63892015-04-21 13:24:37 -0700476 dfMaskScaleFloor = kMediumDFFontLimit;
477 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700478 *textRatio = textSize / kLargeDFFontSize;
479 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
480 }
481
joshualitt64c99cc2015-04-21 09:43:03 -0700482 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
483 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
484 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
485 // tolerate before we'd have to move to a large mip size. When we actually test these values
486 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
487 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
488 // level)
joshualitta7c63892015-04-21 13:24:37 -0700489 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700490 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
491 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
492
joshualitt9bd2daf2015-04-17 09:30:06 -0700493 skPaint->setLCDRenderText(false);
494 skPaint->setAutohinted(false);
495 skPaint->setHinting(SkPaint::kNormal_Hinting);
496 skPaint->setSubpixelText(true);
497}
498
joshualitt374b2f72015-07-21 08:05:03 -0700499inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700500 int runIndex,
robertphillips6ee690e2015-12-02 08:57:50 -0800501 const GrClip& clip,
jvanverth0628a522015-08-18 07:44:22 -0700502 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700503 const SkPaint& skPaint,
504 const SkMatrix& viewMatrix,
505 const SkTDArray<char>& fallbackTxt,
506 const SkTDArray<SkScalar>& fallbackPos,
507 int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800508 const SkPoint& offset) {
joshualittfec19e12015-04-17 10:32:32 -0700509 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700510 blob->setHasBitmap();
511 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700512 // Push back a new subrun to fill and set the override descriptor
513 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700514 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitt97202d22015-04-22 13:47:02 -0700515 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700516 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700517 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700518 run.fOverrideDescriptor->getDesc());
jvanverth0628a522015-08-18 07:44:22 -0700519 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700520 fallbackTxt.begin(), fallbackTxt.count(),
robertphillips6ee690e2015-12-02 08:57:50 -0800521 fallbackPos.begin(), scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700522 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700523}
524
joshualitt374b2f72015-07-21 08:05:03 -0700525inline GrAtlasTextBlob*
herbe59124e2015-11-18 10:54:39 -0800526GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
527 const SkMatrix& viewMatrix, SkPaint* dfPaint,
jvanverth157e6482015-09-09 08:05:12 -0700528 SkScalar* textRatio) {
joshualittf528e0d2015-12-09 06:42:52 -0800529 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700530
531 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700532 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700533 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700534 Run& run = blob->fRuns[0];
535 PerSubRunInfo& subRun = run.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800536 subRun.setUseLCDText(origPaint.isLCDRenderText());
537 subRun.setDrawAsDistanceFields();
joshualitt9bd2daf2015-04-17 09:30:06 -0700538
joshualitt9bd2daf2015-04-17 09:30:06 -0700539 return blob;
540}
541
joshualitt374b2f72015-07-21 08:05:03 -0700542inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800543GrAtlasTextContext::createDrawTextBlob(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 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700548 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700549
joshualitt374b2f72015-07-21 08:05:03 -0700550 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700551 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
552 SkPaint dfPaint;
553 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700554 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700555
joshualitt9bd2daf2015-04-17 09:30:06 -0700556 SkTDArray<char> fallbackTxt;
557 SkTDArray<SkScalar> fallbackPos;
558 SkPoint offset;
jvanverth157e6482015-09-09 08:05:12 -0700559 this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800560 byteLength, x, y, textRatio, &fallbackTxt, &fallbackPos,
joshualitt9bd2daf2015-04-17 09:30:06 -0700561 &offset, skPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700562 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800563 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
564 fallbackTxt, fallbackPos, 2, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700565 }
566 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800567 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700568 blob->fViewMatrix = viewMatrix;
569
570 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
571 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800572 byteLength, x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700573 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700574 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700575 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700576}
577
joshualitt374b2f72015-07-21 08:05:03 -0700578inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800579GrAtlasTextContext::createDrawPosTextBlob(const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700580 const GrPaint& paint, const SkPaint& skPaint,
581 const SkMatrix& viewMatrix,
582 const char text[], size_t byteLength,
583 const SkScalar pos[], int scalarsPerPosition,
584 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700585 int glyphCount = skPaint.countText(text, byteLength);
586
joshualitt374b2f72015-07-21 08:05:03 -0700587 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700588 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
589 SkPaint dfPaint;
590 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700591 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700592
593 SkTDArray<char> fallbackTxt;
594 SkTDArray<SkScalar> fallbackPos;
jvanverth157e6482015-09-09 08:05:12 -0700595 this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800596 byteLength, pos, scalarsPerPosition, offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700597 textRatio, &fallbackTxt, &fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700598 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800599 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
600 fallbackTxt, fallbackPos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700601 }
602 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800603 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700604 blob->fViewMatrix = viewMatrix;
605 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
606 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800607 byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700608 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700609 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700610 return blob;
611}
612
robertphillips433625e2015-12-04 06:58:16 -0800613void GrAtlasTextContext::onDrawText(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -0800614 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700615 const GrPaint& paint, const SkPaint& skPaint,
616 const SkMatrix& viewMatrix,
617 const char text[], size_t byteLength,
618 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700619 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800620 this->createDrawTextBlob(clip, paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700621 text, byteLength, x, y, regionClipBounds));
robertphillips433625e2015-12-04 06:58:16 -0800622 this->flush(blob, dc, skPaint, paint, clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700623}
624
robertphillips433625e2015-12-04 06:58:16 -0800625void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700626 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700627 const GrPaint& paint, const SkPaint& skPaint,
628 const SkMatrix& viewMatrix,
629 const char text[], size_t byteLength,
630 const SkScalar pos[], int scalarsPerPosition,
631 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700632 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800633 this->createDrawPosTextBlob(clip, paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700634 text, byteLength,
635 pos, scalarsPerPosition,
636 offset, regionClipBounds));
joshualitt79dfb2b2015-05-11 08:58:08 -0700637
robertphillips433625e2015-12-04 06:58:16 -0800638 this->flush(blob, dc, skPaint, paint, clip, regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700639}
640
joshualitt374b2f72015-07-21 08:05:03 -0700641void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700642 SkGlyphCache* cache, const SkPaint& skPaint,
643 GrColor color,
644 const SkMatrix& viewMatrix,
645 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800646 SkScalar x, SkScalar y) {
halcanary96fcdcc2015-08-27 07:41:13 -0700647 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700648
649 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700650 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700651 return;
652 }
653
halcanary96fcdcc2015-08-27 07:41:13 -0700654 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700655
656 // Get GrFontScaler from cache
657 GrFontScaler* fontScaler = GetGrFontScaler(cache);
658
herbe59124e2015-11-18 10:54:39 -0800659 SkFindAndPlaceGlyph::ProcessText(
herb4c11b3f2015-11-20 13:53:12 -0800660 skPaint.getTextEncoding(), text, byteLength,
661 {x, y}, viewMatrix, skPaint.getTextAlign(),
662 cache,
herbe59124e2015-11-18 10:54:39 -0800663 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
664 position += rounding;
665 this->bmpAppendGlyph(
666 blob, runIndex, glyph,
667 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800668 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700669 }
herbe59124e2015-11-18 10:54:39 -0800670 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700671}
672
joshualitt374b2f72015-07-21 08:05:03 -0700673void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700674 SkGlyphCache* cache, const SkPaint& skPaint,
675 GrColor color,
676 const SkMatrix& viewMatrix,
677 const char text[], size_t byteLength,
678 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800679 const SkPoint& offset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700680 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700681 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
682
683 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700684 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700685 return;
686 }
687
halcanary96fcdcc2015-08-27 07:41:13 -0700688 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700689
690 // Get GrFontScaler from cache
691 GrFontScaler* fontScaler = GetGrFontScaler(cache);
692
herb9be5ff62015-11-11 11:30:11 -0800693 SkFindAndPlaceGlyph::ProcessPosText(
herb4c11b3f2015-11-20 13:53:12 -0800694 skPaint.getTextEncoding(), text, byteLength,
695 offset, viewMatrix, pos, scalarsPerPosition,
696 skPaint.getTextAlign(), cache,
herb9be5ff62015-11-11 11:30:11 -0800697 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
698 position += rounding;
699 this->bmpAppendGlyph(
700 blob, runIndex, glyph,
701 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800702 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700703 }
herb9be5ff62015-11-11 11:30:11 -0800704 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700705}
706
joshualitt374b2f72015-07-21 08:05:03 -0700707void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700708 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700709 const SkMatrix& viewMatrix,
710 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800711 SkScalar x, SkScalar y,
joshualitt9bd2daf2015-04-17 09:30:06 -0700712 SkScalar textRatio,
713 SkTDArray<char>* fallbackTxt,
714 SkTDArray<SkScalar>* fallbackPos,
715 SkPoint* offset,
716 const SkPaint& origPaint) {
halcanary96fcdcc2015-08-27 07:41:13 -0700717 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700718
719 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700720 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700721 return;
722 }
723
724 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
725 SkAutoDescriptor desc;
halcanary96fcdcc2015-08-27 07:41:13 -0700726 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
herbe59124e2015-11-18 10:54:39 -0800727 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
joshualitt9bd2daf2015-04-17 09:30:06 -0700728 desc.getDesc());
729
730 SkTArray<SkScalar> positions;
731
732 const char* textPtr = text;
733 SkFixed stopX = 0;
734 SkFixed stopY = 0;
735 SkFixed origin = 0;
736 switch (origPaint.getTextAlign()) {
737 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
738 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
739 case SkPaint::kLeft_Align: origin = 0; break;
740 }
741
742 SkAutoKern autokern;
743 const char* stop = text + byteLength;
744 while (textPtr < stop) {
745 // don't need x, y here, since all subpixel variants will have the
746 // same advance
747 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
748
749 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
750 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
751
752 SkFixed height = glyph.fAdvanceY;
753 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
754
755 stopX += width;
756 stopY += height;
757 }
758 SkASSERT(textPtr == stop);
759
jvanverth157e6482015-09-09 08:05:12 -0700760 SkGlyphCache::AttachCache(origPaintCache);
761
joshualitt9bd2daf2015-04-17 09:30:06 -0700762 // now adjust starting point depending on alignment
763 SkScalar alignX = SkFixedToScalar(stopX);
764 SkScalar alignY = SkFixedToScalar(stopY);
765 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
766 alignX = SkScalarHalf(alignX);
767 alignY = SkScalarHalf(alignY);
768 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
769 alignX = 0;
770 alignY = 0;
771 }
772 x -= alignX;
773 y -= alignY;
774 *offset = SkPoint::Make(x, y);
775
jvanverth157e6482015-09-09 08:05:12 -0700776 this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800777 positions.begin(), 2, *offset, textRatio, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700778 fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700779}
780
joshualitt374b2f72015-07-21 08:05:03 -0700781void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700782 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700783 const SkMatrix& viewMatrix,
784 const char text[], size_t byteLength,
785 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800786 const SkPoint& offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700787 SkScalar textRatio,
788 SkTDArray<char>* fallbackTxt,
789 SkTDArray<SkScalar>* fallbackPos) {
790
halcanary96fcdcc2015-08-27 07:41:13 -0700791 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700792 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
793
794 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700795 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700796 return;
797 }
798
halcanary96fcdcc2015-08-27 07:41:13 -0700799 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -0700800
801 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
jvanverth157e6482015-09-09 08:05:12 -0700802 SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700803 GrFontScaler* fontScaler = GetGrFontScaler(cache);
804
805 const char* stop = text + byteLength;
806
807 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
808 while (text < stop) {
809 const char* lastText = text;
810 // the last 2 parameters are ignored
811 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
812
813 if (glyph.fWidth) {
814 SkScalar x = offset.x() + pos[0];
815 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
816
817 if (!this->dfAppendGlyph(blob,
818 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700819 glyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800820 x, y, color, fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700821 textRatio, viewMatrix)) {
822 // couldn't append, send to fallback
823 fallbackTxt->append(SkToInt(text-lastText), lastText);
824 *fallbackPos->append() = pos[0];
825 if (2 == scalarsPerPosition) {
826 *fallbackPos->append() = pos[1];
827 }
828 }
829 }
830 pos += scalarsPerPosition;
831 }
832 } else {
833 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
834 : SK_Scalar1;
835 while (text < stop) {
836 const char* lastText = text;
837 // the last 2 parameters are ignored
838 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
839
840 if (glyph.fWidth) {
841 SkScalar x = offset.x() + pos[0];
842 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
843
844 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
845 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
846
847 if (!this->dfAppendGlyph(blob,
848 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700849 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700850 x - advanceX, y - advanceY, color,
851 fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700852 textRatio,
853 viewMatrix)) {
854 // couldn't append, send to fallback
855 fallbackTxt->append(SkToInt(text-lastText), lastText);
856 *fallbackPos->append() = pos[0];
857 if (2 == scalarsPerPosition) {
858 *fallbackPos->append() = pos[1];
859 }
860 }
861 }
862 pos += scalarsPerPosition;
863 }
864 }
jvanverth157e6482015-09-09 08:05:12 -0700865
866 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700867}
868
joshualitt374b2f72015-07-21 08:05:03 -0700869void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700870 const SkGlyph& skGlyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800871 int vx, int vy, GrColor color, GrFontScaler* scaler) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700872 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700873 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
874 }
875
joshualitt6c2c2b02015-07-24 10:37:00 -0700876 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
877 skGlyph.getSubXFixed(),
878 skGlyph.getSubYFixed(),
879 GrGlyph::kCoverage_MaskStyle);
880 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700881 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700882 return;
883 }
884
885 int x = vx + glyph->fBounds.fLeft;
886 int y = vy + glyph->fBounds.fTop;
887
888 // keep them as ints until we've done the clip-test
889 int width = glyph->fBounds.width();
890 int height = glyph->fBounds.height();
891
joshualitt1d89e8d2015-04-01 12:40:54 -0700892 SkRect r;
893 r.fLeft = SkIntToScalar(x);
894 r.fTop = SkIntToScalar(y);
895 r.fRight = r.fLeft + SkIntToScalar(width);
896 r.fBottom = r.fTop + SkIntToScalar(height);
joshualittf528e0d2015-12-09 06:42:52 -0800897
joshualitta06e6ab2015-12-10 08:54:41 -0800898 blob->appendGlyph(runIndex, r, color, fCurrStrike, glyph, scaler, skGlyph,
899 SkIntToScalar(vx), SkIntToScalar(vy), 1.0f, false);
joshualitt9bd2daf2015-04-17 09:30:06 -0700900}
joshualitt1d89e8d2015-04-01 12:40:54 -0700901
joshualitt374b2f72015-07-21 08:05:03 -0700902bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700903 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700904 SkScalar sx, SkScalar sy, GrColor color,
905 GrFontScaler* scaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700906 SkScalar textRatio, const SkMatrix& viewMatrix) {
907 if (!fCurrStrike) {
908 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
909 }
910
joshualitt6c2c2b02015-07-24 10:37:00 -0700911 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
912 skGlyph.getSubXFixed(),
913 skGlyph.getSubYFixed(),
914 GrGlyph::kDistance_MaskStyle);
915 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700916 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700917 return true;
918 }
919
920 // fallback to color glyph support
921 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
922 return false;
923 }
924
925 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
926 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
927 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
928 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
929
930 SkScalar scale = textRatio;
931 dx *= scale;
932 dy *= scale;
933 width *= scale;
934 height *= scale;
935 sx += dx;
936 sy += dy;
937 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
938
joshualitta06e6ab2015-12-10 08:54:41 -0800939 blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph, scaler, skGlyph,
940 sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700941 return true;
942}
943
robertphillips7bceedc2015-12-01 12:51:26 -0800944void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -0800945 const SkTextBlobRunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -0700946 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -0700947 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
948 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
949 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -0700950
joshualitt9a27e632015-04-06 10:53:36 -0700951 size_t textLen = it.glyphCount() * sizeof(uint16_t);
952 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -0700953
joshualitt9a27e632015-04-06 10:53:36 -0700954 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -0700955
joshualitt9a27e632015-04-06 10:53:36 -0700956 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
957 return;
joshualitt1d89e8d2015-04-01 12:40:54 -0700958 }
959
robertphillipsfcf78292015-06-19 11:49:52 -0700960 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -0700961
962 switch (it.positioning()) {
963 case SkTextBlob::kDefault_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -0800964 this->drawTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700965 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -0700966 textLen, x + offset.x(), y + offset.y(), clipBounds);
967 break;
968 case SkTextBlob::kHorizontal_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -0800969 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700970 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -0700971 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
972 clipBounds);
973 break;
974 case SkTextBlob::kFull_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -0800975 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700976 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -0700977 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
978 break;
979 }
980}
981
bsalomonabd30f52015-08-13 13:34:48 -0700982inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -0700983GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -0700984 int glyphCount, int run, int subRun,
985 GrColor color, SkScalar transX, SkScalar transY,
986 const SkPaint& skPaint) {
joshualitt3660d532015-12-07 11:32:50 -0800987 GrMaskFormat format = info.maskFormat();
joshualitt79dfb2b2015-05-11 08:58:08 -0700988 GrColor subRunColor;
989 if (kARGB_GrMaskFormat == format) {
990 uint8_t paintAlpha = skPaint.getAlpha();
991 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
992 } else {
993 subRunColor = color;
994 }
995
joshualitta751c972015-11-20 13:37:32 -0800996 GrAtlasTextBatch* batch;
joshualitt3660d532015-12-07 11:32:50 -0800997 if (info.drawAsDistanceFields()) {
joshualitt79dfb2b2015-05-11 08:58:08 -0700998 SkColor filteredColor;
999 SkColorFilter* colorFilter = skPaint.getColorFilter();
1000 if (colorFilter) {
1001 filteredColor = colorFilter->filterColor(skPaint.getColor());
1002 } else {
1003 filteredColor = skPaint.getColor();
1004 }
robertphillipsfcf78292015-06-19 11:49:52 -07001005 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
joshualitta751c972015-11-20 13:37:32 -08001006 batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
1007 fDistanceAdjustTable, filteredColor,
joshualitt3660d532015-12-07 11:32:50 -08001008 info.hasUseLCDText(), useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07001009 } else {
joshualitta751c972015-11-20 13:37:32 -08001010 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07001011 }
joshualitta751c972015-11-20 13:37:32 -08001012 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07001013 geometry.fBlob = SkRef(cacheBlob);
1014 geometry.fRun = run;
1015 geometry.fSubRun = subRun;
1016 geometry.fColor = subRunColor;
1017 geometry.fTransX = transX;
1018 geometry.fTransY = transY;
1019 batch->init();
1020
1021 return batch;
1022}
1023
robertphillipsf6703fa2015-09-01 05:36:47 -07001024inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07001025 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07001026 SkScalar transX, SkScalar transY,
1027 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07001028 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
1029 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
joshualitt3660d532015-12-07 11:32:50 -08001030 int glyphCount = info.glyphCount();
joshualitt9a27e632015-04-06 10:53:36 -07001031 if (0 == glyphCount) {
1032 continue;
1033 }
1034
bsalomonabd30f52015-08-13 13:34:48 -07001035 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
1036 subRun, color, transX, transY,
1037 skPaint));
robertphillipsf6703fa2015-09-01 05:36:47 -07001038 dc->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07001039 }
1040}
1041
herbe59124e2015-11-18 10:54:39 -08001042inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob,
robertphillips7bceedc2015-12-01 12:51:26 -08001043 GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -07001044 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07001045 SkScalar transX, SkScalar transY,
1046 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07001047 if (!cacheBlob->fBigGlyphs.count()) {
1048 return;
1049 }
1050
joshualitt9a27e632015-04-06 10:53:36 -07001051 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07001052 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07001053 bigGlyph.fVx += transX;
1054 bigGlyph.fVy += transY;
joshualitt0fe04a22015-08-25 12:05:50 -07001055 SkMatrix ctm;
1056 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
1057 ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
1058 if (bigGlyph.fApplyVM) {
1059 ctm.postConcat(cacheBlob->fViewMatrix);
1060 }
joshualittfc072562015-05-13 12:15:06 -07001061
robertphillips7bceedc2015-12-01 12:51:26 -08001062 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, bigGlyph.fPath,
joshualitt0fe04a22015-08-25 12:05:50 -07001063 skPaint, ctm, nullptr, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07001064 }
1065}
joshualitt9a27e632015-04-06 10:53:36 -07001066
robertphillips2334fb62015-06-17 05:43:33 -07001067void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -07001068 GrAtlasTextBlob* cacheBlob,
herbe59124e2015-11-18 10:54:39 -08001069 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07001070 const SkPaint& skPaint,
1071 const GrPaint& grPaint,
1072 SkDrawFilter* drawFilter,
1073 const GrClip& clip,
1074 const SkMatrix& viewMatrix,
1075 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07001076 SkScalar x, SkScalar y,
1077 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07001078 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
1079 // it as paths
robertphillips433625e2015-12-04 06:58:16 -08001080 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
joshualitt9a27e632015-04-06 10:53:36 -07001081
1082 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001083
halcanary33779752015-10-27 14:01:05 -07001084 SkTextBlobRunIterator it(blob);
joshualitt9a27e632015-04-06 10:53:36 -07001085 for (int run = 0; !it.done(); it.next(), run++) {
1086 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillips7bceedc2015-12-01 12:51:26 -08001087 this->flushRunAsPaths(dc, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07001088 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07001089 continue;
1090 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001091 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillipsf6703fa2015-09-01 05:36:47 -07001092 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07001093 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001094 }
1095
1096 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001097 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001098}
1099
joshualitt374b2f72015-07-21 08:05:03 -07001100void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07001101 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07001102 const SkPaint& skPaint,
1103 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07001104 const GrClip& clip,
1105 const SkIRect& clipBounds) {
robertphillips433625e2015-12-04 06:58:16 -08001106 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
joshualitt9a27e632015-04-06 10:53:36 -07001107
1108 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001109 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillipsf6703fa2015-09-01 05:36:47 -07001110 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001111 }
1112
1113 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001114 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001115}
joshualitt79dfb2b2015-05-11 08:58:08 -07001116
1117///////////////////////////////////////////////////////////////////////////////////////////////////
1118
1119#ifdef GR_TEST_UTILS
1120
bsalomonabd30f52015-08-13 13:34:48 -07001121DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001122 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -07001123 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -07001124 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07001125
1126 if (context->uniqueID() != gContextID) {
1127 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -07001128 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -07001129
joshualitt79dfb2b2015-05-11 08:58:08 -07001130 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
1131 // because we don't really want to have a gpu device here.
1132 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -07001133 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07001134 }
1135
joshualitt79dfb2b2015-05-11 08:58:08 -07001136 // Setup dummy SkPaint / GrPaint
1137 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001138 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07001139 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07001140 skPaint.setColor(color);
1141 skPaint.setLCDRenderText(random->nextBool());
1142 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
1143 skPaint.setSubpixelText(random->nextBool());
1144
1145 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001146 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001147 SkFAIL("couldn't convert paint\n");
1148 }
1149
1150 const char* text = "The quick brown fox jumps over the lazy dog.";
1151 int textLen = (int)strlen(text);
1152
1153 // Setup clip
1154 GrClip clip;
1155 SkIRect noClip = SkIRect::MakeLargest();
1156
1157 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
1158 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07001159 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -08001160 gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text,
joshualitt79dfb2b2015-05-11 08:58:08 -07001161 static_cast<size_t>(textLen), 0, 0, noClip));
1162
1163 SkScalar transX = static_cast<SkScalar>(random->nextU());
1164 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07001165 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07001166 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
1167}
1168
1169#endif