blob: d7ccce54cf7d5c146497456e9e2eff972b21d1df [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
joshualitt2a0e9f32015-04-13 06:12:21 -0700191bool GrAtlasTextContext::MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY,
joshualitt374b2f72015-07-21 08:05:03 -0700192 const GrAtlasTextBlob& blob, const SkPaint& paint,
jvanverth0628a522015-08-18 07:44:22 -0700193 GrColor color, const SkMaskFilter::BlurRec& blurRec,
joshualittdbd35932015-04-02 09:19:04 -0700194 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700195 // If we have LCD text then our canonical color will be set to transparent, in this case we have
196 // to regenerate the blob on any color change
jvanverth0628a522015-08-18 07:44:22 -0700197 // We use the grPaint to get any color filter effects
198 if (blob.fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
199 blob.fPaintColor != color) {
joshualitt2a0e9f32015-04-13 06:12:21 -0700200 return true;
201 }
202
203 if (blob.fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
204 return true;
205 }
206
207 if (blob.fViewMatrix.hasPerspective() && !blob.fViewMatrix.cheapEqualTo(viewMatrix)) {
208 return true;
209 }
210
joshualitt53b5f442015-04-13 06:33:59 -0700211 // We only cache one masked version
212 if (blob.fKey.fHasBlur &&
213 (blob.fBlurRec.fSigma != blurRec.fSigma ||
214 blob.fBlurRec.fStyle != blurRec.fStyle ||
215 blob.fBlurRec.fQuality != blurRec.fQuality)) {
216 return true;
217 }
218
219 // Similarly, we only cache one version for each style
220 if (blob.fKey.fStyle != SkPaint::kFill_Style &&
221 (blob.fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
222 blob.fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
223 blob.fStrokeInfo.fJoin != paint.getStrokeJoin())) {
224 return true;
225 }
226
joshualittfcfb9fc2015-04-21 07:35:10 -0700227 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
228 // for mixed blobs if this becomes an issue.
229 if (blob.hasBitmap() && blob.hasDistanceField()) {
joshualitt473ffa12015-04-22 18:23:15 -0700230 // Identical viewmatrices and we can reuse in all cases
231 if (blob.fViewMatrix.cheapEqualTo(viewMatrix) && x == blob.fX && y == blob.fY) {
232 return false;
233 }
joshualitt2a0e9f32015-04-13 06:12:21 -0700234 return true;
235 }
236
joshualittfcfb9fc2015-04-21 07:35:10 -0700237 if (blob.hasBitmap()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700238 if (blob.fViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
239 blob.fViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
240 blob.fViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
241 blob.fViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
242 return true;
243 }
244
joshualittfcfb9fc2015-04-21 07:35:10 -0700245 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
246 // but only for integer translations.
247 // This cool bit of math will determine the necessary translation to apply to the already
248 // generated vertex coordinates to move them to the correct position
249 SkScalar transX = viewMatrix.getTranslateX() +
250 viewMatrix.getScaleX() * (x - blob.fX) +
251 viewMatrix.getSkewX() * (y - blob.fY) -
252 blob.fViewMatrix.getTranslateX();
253 SkScalar transY = viewMatrix.getTranslateY() +
254 viewMatrix.getSkewY() * (x - blob.fX) +
255 viewMatrix.getScaleY() * (y - blob.fY) -
256 blob.fViewMatrix.getTranslateY();
joshualittf0c000d2015-04-27 09:36:55 -0700257 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700258 return true;
259 }
260
joshualittfcfb9fc2015-04-21 07:35:10 -0700261 (*outTransX) = transX;
262 (*outTransY) = transY;
joshualitta7c63892015-04-21 13:24:37 -0700263 } else if (blob.hasDistanceField()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700264 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
265 // distance field being generated, so we have to regenerate in those cases
266 SkScalar newMaxScale = viewMatrix.getMaxScale();
267 SkScalar oldMaxScale = blob.fViewMatrix.getMaxScale();
268 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
269 if (scaleAdjust < blob.fMaxMinScale || scaleAdjust > blob.fMinMaxScale) {
270 return true;
271 }
272
273 (*outTransX) = x - blob.fX;
274 (*outTransY) = y - blob.fY;
joshualittfcfb9fc2015-04-21 07:35:10 -0700275 }
joshualitt374b2f72015-07-21 08:05:03 -0700276
joshualitta7c63892015-04-21 13:24:37 -0700277 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
278 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
279 // the blob anyways at flush time, so no need to regenerate explicitly
joshualitt2a0e9f32015-04-13 06:12:21 -0700280 return false;
joshualitt1d89e8d2015-04-01 12:40:54 -0700281}
282
283
joshualitt374b2f72015-07-21 08:05:03 -0700284inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run,
joshualittdbd35932015-04-02 09:19:04 -0700285 const SkPaint& skPaint,
joshualitt9bd2daf2015-04-17 09:30:06 -0700286 const SkMatrix* viewMatrix,
287 bool noGamma) {
robertphillipsfcf78292015-06-19 11:49:52 -0700288 skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma);
joshualitt1d89e8d2015-04-01 12:40:54 -0700289 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
290 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc());
291}
292
robertphillips433625e2015-12-04 06:58:16 -0800293void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700294 const GrClip& clip, const SkPaint& skPaint,
295 const SkMatrix& viewMatrix, const SkTextBlob* blob,
296 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700297 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700298 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700299 if (fContext->abandoned()) {
300 return;
301 }
302
joshualitt374b2f72015-07-21 08:05:03 -0700303 SkAutoTUnref<GrAtlasTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700304 SkMaskFilter::BlurRec blurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700305 GrAtlasTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700306 // It might be worth caching these things, but its not clear at this time
307 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
308 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700309 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700310 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700311 drawFilter);
312
313 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700314 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700315
316 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
robertphillipsfcf78292015-06-19 11:49:52 -0700317 SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700318 kUnknown_SkPixelGeometry;
319
joshualitt9e36c1a2015-04-14 12:17:27 -0700320 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
321 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
322 // ensure we always match the same key
323 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
324 ComputeCanonicalColor(skPaint, hasLCD);
325
joshualitte4cee1f2015-05-11 13:04:28 -0700326 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700327 key.fUniqueID = blob->uniqueID();
328 key.fStyle = skPaint.getStyle();
329 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700330 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700331 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700332 }
333
joshualitt2a0e9f32015-04-13 06:12:21 -0700334 SkScalar transX = 0.f;
335 SkScalar transY = 0.f;
336
joshualitt9e36c1a2015-04-14 12:17:27 -0700337 // Though for the time being runs in the textblob can override the paint, they only touch font
338 // info.
339 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700340 if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700341 return;
342 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700343
joshualittb7133be2015-04-08 09:08:31 -0700344 if (cacheBlob) {
jvanverth0628a522015-08-18 07:44:22 -0700345 if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, grPaint.getColor(), blurRec,
346 viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700347 // We have to remake the blob because changes may invalidate our masks.
348 // TODO we could probably get away reuse most of the time if the pointer is unique,
349 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700350 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700351 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800352 GrAtlasTextBlob::kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700353 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800354 blob, x, y, drawFilter, clip);
joshualittb7133be2015-04-08 09:08:31 -0700355 } else {
joshualitt9e36c1a2015-04-14 12:17:27 -0700356 // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y
joshualitt7e7b5c52015-07-21 12:56:56 -0700357 // offsets. Note, we offset the vertex bounds right before flushing
joshualitt2a0e9f32015-04-13 06:12:21 -0700358 cacheBlob->fViewMatrix = viewMatrix;
359 cacheBlob->fX = x;
360 cacheBlob->fY = y;
joshualittb7133be2015-04-08 09:08:31 -0700361 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700362#ifdef CACHE_SANITY_CHECK
363 {
364 int glyphCount = 0;
365 int runCount = 0;
366 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
367 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
368 kGrayTextVASize));
369 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
370 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800371 blob, x, y, drawFilter, clip);
joshualitt259fbf12015-07-21 11:39:34 -0700372 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
373 }
374
375#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700376 }
377 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700378 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700379 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
joshualittf528e0d2015-12-09 06:42:52 -0800380 GrAtlasTextBlob::kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700381 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800382 cacheBlob.reset(fCache->createBlob(blob, GrAtlasTextBlob::kGrayTextVASize));
joshualitt2a0e9f32015-04-13 06:12:21 -0700383 }
robertphillips9c240a12015-05-28 07:45:59 -0700384 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillips6ee690e2015-12-02 08:57:50 -0800385 blob, x, y, drawFilter, clip);
joshualitt1d89e8d2015-04-01 12:40:54 -0700386 }
387
robertphillips433625e2015-12-04 06:58:16 -0800388 this->flush(blob, cacheBlob, dc, skPaint, grPaint, drawFilter,
joshualitt2a0e9f32015-04-13 06:12:21 -0700389 clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700390}
391
joshualitt9bd2daf2015-04-17 09:30:06 -0700392inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
393 const SkMatrix& viewMatrix) {
394 // TODO: support perspective (need getMaxScale replacement)
395 if (viewMatrix.hasPerspective()) {
396 return false;
397 }
398
399 SkScalar maxScale = viewMatrix.getMaxScale();
400 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
401 // Hinted text looks far better at small resolutions
402 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700403 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700404 return false;
405 }
406
bsalomonafcd7cd2015-08-31 12:39:41 -0700407 bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700408#if SK_FORCE_DISTANCE_FIELD_TEXT
409 useDFT = true;
410#endif
411
jvanverth4854d132015-06-22 06:46:56 -0700412 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700413 return false;
414 }
415
416 // rasterizers and mask filters modify alpha, which doesn't
417 // translate well to distance
418 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700419 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700420 return false;
421 }
422
423 // TODO: add some stroking support
424 if (skPaint.getStyle() != SkPaint::kFill_Style) {
425 return false;
426 }
427
428 return true;
429}
430
joshualitt374b2f72015-07-21 08:05:03 -0700431void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700432 const SkPaint& skPaint, GrColor color,
433 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700434 const SkTextBlob* blob, SkScalar x, SkScalar y,
robertphillips6ee690e2015-12-02 08:57:50 -0800435 SkDrawFilter* drawFilter,
436 const GrClip& clip) {
herbe59124e2015-11-18 10:54:39 -0800437 // The color here is the GrPaint color, and it is used to determine whether we
jvanverth0628a522015-08-18 07:44:22 -0700438 // have to regenerate LCD text blobs.
439 // We use this color vs the SkPaint color because it has the colorfilter applied.
440 cacheBlob->fPaintColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -0700441 cacheBlob->fViewMatrix = viewMatrix;
442 cacheBlob->fX = x;
443 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700444
445 // Regenerate textblob
446 SkPaint runPaint = skPaint;
halcanary33779752015-10-27 14:01:05 -0700447 SkTextBlobRunIterator it(blob);
joshualitt1d89e8d2015-04-01 12:40:54 -0700448 for (int run = 0; !it.done(); it.next(), run++) {
449 int glyphCount = it.glyphCount();
450 size_t textLen = glyphCount * sizeof(uint16_t);
451 const SkPoint& offset = it.offset();
452 // applyFontToPaint() always overwrites the exact same attributes,
453 // so it is safe to not re-seed the paint for this reason.
454 it.applyFontToPaint(&runPaint);
455
456 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
457 // A false return from filter() means we should abort the current draw.
458 runPaint = skPaint;
459 continue;
460 }
461
robertphillipsfcf78292015-06-19 11:49:52 -0700462 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700463
joshualitt18b072d2015-12-07 12:26:12 -0800464 cacheBlob->push_back_run(run);
joshualitt1d89e8d2015-04-01 12:40:54 -0700465
joshualittfcfb9fc2015-04-21 07:35:10 -0700466 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
467 cacheBlob->setHasDistanceField();
468 SkPaint dfPaint = runPaint;
469 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700470 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700471 Run& runIdx = cacheBlob->fRuns[run];
472 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800473 subRun.setUseLCDText(runPaint.isLCDRenderText());
474 subRun.setDrawAsDistanceFields();
joshualitt9a27e632015-04-06 10:53:36 -0700475
joshualittfcfb9fc2015-04-21 07:35:10 -0700476 SkTDArray<char> fallbackTxt;
477 SkTDArray<SkScalar> fallbackPos;
478 SkPoint dfOffset;
479 int scalarsPerPosition = 2;
480 switch (it.positioning()) {
481 case SkTextBlob::kDefault_Positioning: {
jvanverth157e6482015-09-09 08:05:12 -0700482 this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700483 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800484 x + offset.x(), y + offset.y(), textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700485 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
486 break;
487 }
488 case SkTextBlob::kHorizontal_Positioning: {
489 scalarsPerPosition = 1;
490 dfOffset = SkPoint::Make(x, y + offset.y());
jvanverth157e6482015-09-09 08:05:12 -0700491 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700492 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800493 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700494 &fallbackTxt, &fallbackPos);
495 break;
496 }
497 case SkTextBlob::kFull_Positioning: {
498 dfOffset = SkPoint::Make(x, y);
jvanverth157e6482015-09-09 08:05:12 -0700499 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700500 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800501 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700502 &fallbackTxt, &fallbackPos);
503 break;
504 }
505 }
506 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800507 this->fallbackDrawPosText(cacheBlob, run, clip, color, runPaint, viewMatrix,
508 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset);
joshualittfcfb9fc2015-04-21 07:35:10 -0700509 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700510 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
511 cacheBlob->fRuns[run].fDrawAsPaths = true;
512 } else {
513 cacheBlob->setHasBitmap();
514 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
515 false);
516 switch (it.positioning()) {
517 case SkTextBlob::kDefault_Positioning:
518 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
519 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800520 x + offset.x(), y + offset.y());
joshualittfcfb9fc2015-04-21 07:35:10 -0700521 break;
522 case SkTextBlob::kHorizontal_Positioning:
523 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
524 (const char*)it.glyphs(), textLen, it.pos(), 1,
robertphillips6ee690e2015-12-02 08:57:50 -0800525 SkPoint::Make(x, y + offset.y()));
joshualittfcfb9fc2015-04-21 07:35:10 -0700526 break;
527 case SkTextBlob::kFull_Positioning:
528 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
529 (const char*)it.glyphs(), textLen, it.pos(), 2,
robertphillips6ee690e2015-12-02 08:57:50 -0800530 SkPoint::Make(x, y));
joshualittfcfb9fc2015-04-21 07:35:10 -0700531 break;
532 }
533 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700534 }
535
536 if (drawFilter) {
537 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
538 runPaint = skPaint;
539 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700540 }
541}
542
joshualitt374b2f72015-07-21 08:05:03 -0700543inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700544 SkPaint* skPaint,
545 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700546 const SkMatrix& viewMatrix) {
547 // getMaxScale doesn't support perspective, so neither do we at the moment
548 SkASSERT(!viewMatrix.hasPerspective());
549 SkScalar maxScale = viewMatrix.getMaxScale();
550 SkScalar textSize = skPaint->getTextSize();
551 SkScalar scaledTextSize = textSize;
552 // if we have non-unity scale, we need to choose our base text size
553 // based on the SkPaint's text size multiplied by the max scale factor
554 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
555 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
556 scaledTextSize *= maxScale;
557 }
558
joshualitt64c99cc2015-04-21 09:43:03 -0700559 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
560 // and ceiling. A scale outside of this range would require regenerating the distance fields
561 SkScalar dfMaskScaleFloor;
562 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700563 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700564 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700565 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700566 *textRatio = textSize / kSmallDFFontSize;
567 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
568 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700569 dfMaskScaleFloor = kSmallDFFontLimit;
570 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700571 *textRatio = textSize / kMediumDFFontSize;
572 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
573 } else {
joshualitta7c63892015-04-21 13:24:37 -0700574 dfMaskScaleFloor = kMediumDFFontLimit;
575 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700576 *textRatio = textSize / kLargeDFFontSize;
577 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
578 }
579
joshualitt64c99cc2015-04-21 09:43:03 -0700580 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
581 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
582 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
583 // tolerate before we'd have to move to a large mip size. When we actually test these values
584 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
585 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
586 // level)
joshualitta7c63892015-04-21 13:24:37 -0700587 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700588 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
589 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
590
joshualitt9bd2daf2015-04-17 09:30:06 -0700591 skPaint->setLCDRenderText(false);
592 skPaint->setAutohinted(false);
593 skPaint->setHinting(SkPaint::kNormal_Hinting);
594 skPaint->setSubpixelText(true);
595}
596
joshualitt374b2f72015-07-21 08:05:03 -0700597inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700598 int runIndex,
robertphillips6ee690e2015-12-02 08:57:50 -0800599 const GrClip& clip,
jvanverth0628a522015-08-18 07:44:22 -0700600 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700601 const SkPaint& skPaint,
602 const SkMatrix& viewMatrix,
603 const SkTDArray<char>& fallbackTxt,
604 const SkTDArray<SkScalar>& fallbackPos,
605 int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800606 const SkPoint& offset) {
joshualittfec19e12015-04-17 10:32:32 -0700607 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700608 blob->setHasBitmap();
609 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700610 // Push back a new subrun to fill and set the override descriptor
611 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700612 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitt97202d22015-04-22 13:47:02 -0700613 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700614 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700615 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700616 run.fOverrideDescriptor->getDesc());
jvanverth0628a522015-08-18 07:44:22 -0700617 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700618 fallbackTxt.begin(), fallbackTxt.count(),
robertphillips6ee690e2015-12-02 08:57:50 -0800619 fallbackPos.begin(), scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700620 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700621}
622
joshualitt374b2f72015-07-21 08:05:03 -0700623inline GrAtlasTextBlob*
herbe59124e2015-11-18 10:54:39 -0800624GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
625 const SkMatrix& viewMatrix, SkPaint* dfPaint,
jvanverth157e6482015-09-09 08:05:12 -0700626 SkScalar* textRatio) {
joshualittf528e0d2015-12-09 06:42:52 -0800627 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700628
629 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700630 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700631 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700632 Run& run = blob->fRuns[0];
633 PerSubRunInfo& subRun = run.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -0800634 subRun.setUseLCDText(origPaint.isLCDRenderText());
635 subRun.setDrawAsDistanceFields();
joshualitt9bd2daf2015-04-17 09:30:06 -0700636
joshualitt9bd2daf2015-04-17 09:30:06 -0700637 return blob;
638}
639
joshualitt374b2f72015-07-21 08:05:03 -0700640inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800641GrAtlasTextContext::createDrawTextBlob(const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700642 const GrPaint& paint, const SkPaint& skPaint,
643 const SkMatrix& viewMatrix,
644 const char text[], size_t byteLength,
645 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700646 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700647
joshualitt374b2f72015-07-21 08:05:03 -0700648 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700649 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
650 SkPaint dfPaint;
651 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700652 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700653
joshualitt9bd2daf2015-04-17 09:30:06 -0700654 SkTDArray<char> fallbackTxt;
655 SkTDArray<SkScalar> fallbackPos;
656 SkPoint offset;
jvanverth157e6482015-09-09 08:05:12 -0700657 this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800658 byteLength, x, y, textRatio, &fallbackTxt, &fallbackPos,
joshualitt9bd2daf2015-04-17 09:30:06 -0700659 &offset, skPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700660 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800661 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
662 fallbackTxt, fallbackPos, 2, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700663 }
664 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800665 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700666 blob->fViewMatrix = viewMatrix;
667
668 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
669 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800670 byteLength, x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700671 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700672 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700673 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700674}
675
joshualitt374b2f72015-07-21 08:05:03 -0700676inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800677GrAtlasTextContext::createDrawPosTextBlob(const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700678 const GrPaint& paint, const SkPaint& skPaint,
679 const SkMatrix& viewMatrix,
680 const char text[], size_t byteLength,
681 const SkScalar pos[], int scalarsPerPosition,
682 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700683 int glyphCount = skPaint.countText(text, byteLength);
684
joshualitt374b2f72015-07-21 08:05:03 -0700685 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700686 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
687 SkPaint dfPaint;
688 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700689 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700690
691 SkTDArray<char> fallbackTxt;
692 SkTDArray<SkScalar> fallbackPos;
jvanverth157e6482015-09-09 08:05:12 -0700693 this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800694 byteLength, pos, scalarsPerPosition, offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700695 textRatio, &fallbackTxt, &fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700696 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800697 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
698 fallbackTxt, fallbackPos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700699 }
700 } else {
joshualittf528e0d2015-12-09 06:42:52 -0800701 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBlob::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700702 blob->fViewMatrix = viewMatrix;
703 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
704 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800705 byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700706 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700707 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700708 return blob;
709}
710
robertphillips433625e2015-12-04 06:58:16 -0800711void GrAtlasTextContext::onDrawText(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -0800712 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700713 const GrPaint& paint, const SkPaint& skPaint,
714 const SkMatrix& viewMatrix,
715 const char text[], size_t byteLength,
716 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700717 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800718 this->createDrawTextBlob(clip, paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700719 text, byteLength, x, y, regionClipBounds));
robertphillips433625e2015-12-04 06:58:16 -0800720 this->flush(blob, dc, skPaint, paint, clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700721}
722
robertphillips433625e2015-12-04 06:58:16 -0800723void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -0700724 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700725 const GrPaint& paint, const SkPaint& skPaint,
726 const SkMatrix& viewMatrix,
727 const char text[], size_t byteLength,
728 const SkScalar pos[], int scalarsPerPosition,
729 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700730 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800731 this->createDrawPosTextBlob(clip, paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700732 text, byteLength,
733 pos, scalarsPerPosition,
734 offset, regionClipBounds));
joshualitt79dfb2b2015-05-11 08:58:08 -0700735
robertphillips433625e2015-12-04 06:58:16 -0800736 this->flush(blob, dc, skPaint, paint, clip, regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700737}
738
joshualitt374b2f72015-07-21 08:05:03 -0700739void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700740 SkGlyphCache* cache, const SkPaint& skPaint,
741 GrColor color,
742 const SkMatrix& viewMatrix,
743 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800744 SkScalar x, SkScalar y) {
halcanary96fcdcc2015-08-27 07:41:13 -0700745 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700746
747 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700748 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700749 return;
750 }
751
halcanary96fcdcc2015-08-27 07:41:13 -0700752 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700753
754 // Get GrFontScaler from cache
755 GrFontScaler* fontScaler = GetGrFontScaler(cache);
756
herbe59124e2015-11-18 10:54:39 -0800757 SkFindAndPlaceGlyph::ProcessText(
herb4c11b3f2015-11-20 13:53:12 -0800758 skPaint.getTextEncoding(), text, byteLength,
759 {x, y}, viewMatrix, skPaint.getTextAlign(),
760 cache,
herbe59124e2015-11-18 10:54:39 -0800761 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
762 position += rounding;
763 this->bmpAppendGlyph(
764 blob, runIndex, glyph,
765 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800766 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700767 }
herbe59124e2015-11-18 10:54:39 -0800768 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700769}
770
joshualitt374b2f72015-07-21 08:05:03 -0700771void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700772 SkGlyphCache* cache, const SkPaint& skPaint,
773 GrColor color,
774 const SkMatrix& viewMatrix,
775 const char text[], size_t byteLength,
776 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800777 const SkPoint& offset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700778 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700779 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
780
781 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700782 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700783 return;
784 }
785
halcanary96fcdcc2015-08-27 07:41:13 -0700786 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700787
788 // Get GrFontScaler from cache
789 GrFontScaler* fontScaler = GetGrFontScaler(cache);
790
herb9be5ff62015-11-11 11:30:11 -0800791 SkFindAndPlaceGlyph::ProcessPosText(
herb4c11b3f2015-11-20 13:53:12 -0800792 skPaint.getTextEncoding(), text, byteLength,
793 offset, viewMatrix, pos, scalarsPerPosition,
794 skPaint.getTextAlign(), cache,
herb9be5ff62015-11-11 11:30:11 -0800795 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
796 position += rounding;
797 this->bmpAppendGlyph(
798 blob, runIndex, glyph,
799 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800800 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700801 }
herb9be5ff62015-11-11 11:30:11 -0800802 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700803}
804
joshualitt374b2f72015-07-21 08:05:03 -0700805void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700806 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700807 const SkMatrix& viewMatrix,
808 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800809 SkScalar x, SkScalar y,
joshualitt9bd2daf2015-04-17 09:30:06 -0700810 SkScalar textRatio,
811 SkTDArray<char>* fallbackTxt,
812 SkTDArray<SkScalar>* fallbackPos,
813 SkPoint* offset,
814 const SkPaint& origPaint) {
halcanary96fcdcc2015-08-27 07:41:13 -0700815 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700816
817 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700818 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700819 return;
820 }
821
822 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
823 SkAutoDescriptor desc;
halcanary96fcdcc2015-08-27 07:41:13 -0700824 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
herbe59124e2015-11-18 10:54:39 -0800825 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
joshualitt9bd2daf2015-04-17 09:30:06 -0700826 desc.getDesc());
827
828 SkTArray<SkScalar> positions;
829
830 const char* textPtr = text;
831 SkFixed stopX = 0;
832 SkFixed stopY = 0;
833 SkFixed origin = 0;
834 switch (origPaint.getTextAlign()) {
835 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
836 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
837 case SkPaint::kLeft_Align: origin = 0; break;
838 }
839
840 SkAutoKern autokern;
841 const char* stop = text + byteLength;
842 while (textPtr < stop) {
843 // don't need x, y here, since all subpixel variants will have the
844 // same advance
845 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
846
847 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
848 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
849
850 SkFixed height = glyph.fAdvanceY;
851 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
852
853 stopX += width;
854 stopY += height;
855 }
856 SkASSERT(textPtr == stop);
857
jvanverth157e6482015-09-09 08:05:12 -0700858 SkGlyphCache::AttachCache(origPaintCache);
859
joshualitt9bd2daf2015-04-17 09:30:06 -0700860 // now adjust starting point depending on alignment
861 SkScalar alignX = SkFixedToScalar(stopX);
862 SkScalar alignY = SkFixedToScalar(stopY);
863 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
864 alignX = SkScalarHalf(alignX);
865 alignY = SkScalarHalf(alignY);
866 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
867 alignX = 0;
868 alignY = 0;
869 }
870 x -= alignX;
871 y -= alignY;
872 *offset = SkPoint::Make(x, y);
873
jvanverth157e6482015-09-09 08:05:12 -0700874 this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800875 positions.begin(), 2, *offset, textRatio, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700876 fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700877}
878
joshualitt374b2f72015-07-21 08:05:03 -0700879void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700880 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700881 const SkMatrix& viewMatrix,
882 const char text[], size_t byteLength,
883 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800884 const SkPoint& offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700885 SkScalar textRatio,
886 SkTDArray<char>* fallbackTxt,
887 SkTDArray<SkScalar>* fallbackPos) {
888
halcanary96fcdcc2015-08-27 07:41:13 -0700889 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700890 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
891
892 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700893 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700894 return;
895 }
896
halcanary96fcdcc2015-08-27 07:41:13 -0700897 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -0700898
899 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
jvanverth157e6482015-09-09 08:05:12 -0700900 SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700901 GrFontScaler* fontScaler = GetGrFontScaler(cache);
902
903 const char* stop = text + byteLength;
904
905 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
906 while (text < stop) {
907 const char* lastText = text;
908 // the last 2 parameters are ignored
909 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
910
911 if (glyph.fWidth) {
912 SkScalar x = offset.x() + pos[0];
913 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
914
915 if (!this->dfAppendGlyph(blob,
916 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700917 glyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800918 x, y, color, fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700919 textRatio, viewMatrix)) {
920 // couldn't append, send to fallback
921 fallbackTxt->append(SkToInt(text-lastText), lastText);
922 *fallbackPos->append() = pos[0];
923 if (2 == scalarsPerPosition) {
924 *fallbackPos->append() = pos[1];
925 }
926 }
927 }
928 pos += scalarsPerPosition;
929 }
930 } else {
931 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
932 : SK_Scalar1;
933 while (text < stop) {
934 const char* lastText = text;
935 // the last 2 parameters are ignored
936 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
937
938 if (glyph.fWidth) {
939 SkScalar x = offset.x() + pos[0];
940 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
941
942 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
943 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
944
945 if (!this->dfAppendGlyph(blob,
946 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700947 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700948 x - advanceX, y - advanceY, color,
949 fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700950 textRatio,
951 viewMatrix)) {
952 // couldn't append, send to fallback
953 fallbackTxt->append(SkToInt(text-lastText), lastText);
954 *fallbackPos->append() = pos[0];
955 if (2 == scalarsPerPosition) {
956 *fallbackPos->append() = pos[1];
957 }
958 }
959 }
960 pos += scalarsPerPosition;
961 }
962 }
jvanverth157e6482015-09-09 08:05:12 -0700963
964 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700965}
966
joshualitt374b2f72015-07-21 08:05:03 -0700967void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700968 const SkGlyph& skGlyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800969 int vx, int vy, GrColor color, GrFontScaler* scaler) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700970 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700971 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
972 }
973
joshualitt6c2c2b02015-07-24 10:37:00 -0700974 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
975 skGlyph.getSubXFixed(),
976 skGlyph.getSubYFixed(),
977 GrGlyph::kCoverage_MaskStyle);
978 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700979 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700980 return;
981 }
982
983 int x = vx + glyph->fBounds.fLeft;
984 int y = vy + glyph->fBounds.fTop;
985
986 // keep them as ints until we've done the clip-test
987 int width = glyph->fBounds.width();
988 int height = glyph->fBounds.height();
989
joshualitt1d89e8d2015-04-01 12:40:54 -0700990 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -0700991 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -0700992 this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy));
joshualitt1d89e8d2015-04-01 12:40:54 -0700993 return;
994 }
995
joshualitt1d89e8d2015-04-01 12:40:54 -0700996 SkRect r;
997 r.fLeft = SkIntToScalar(x);
998 r.fTop = SkIntToScalar(y);
999 r.fRight = r.fLeft + SkIntToScalar(width);
1000 r.fBottom = r.fTop + SkIntToScalar(height);
joshualittf528e0d2015-12-09 06:42:52 -08001001
1002 blob->appendGlyph(runIndex, r, color, fCurrStrike, glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001003}
joshualitt1d89e8d2015-04-01 12:40:54 -07001004
joshualitt374b2f72015-07-21 08:05:03 -07001005bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001006 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001007 SkScalar sx, SkScalar sy, GrColor color,
1008 GrFontScaler* scaler,
joshualitt9bd2daf2015-04-17 09:30:06 -07001009 SkScalar textRatio, const SkMatrix& viewMatrix) {
1010 if (!fCurrStrike) {
1011 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1012 }
1013
joshualitt6c2c2b02015-07-24 10:37:00 -07001014 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1015 skGlyph.getSubXFixed(),
1016 skGlyph.getSubYFixed(),
1017 GrGlyph::kDistance_MaskStyle);
1018 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001019 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001020 return true;
1021 }
1022
1023 // fallback to color glyph support
1024 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
1025 return false;
1026 }
1027
1028 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
1029 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
1030 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
1031 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
1032
1033 SkScalar scale = textRatio;
1034 dx *= scale;
1035 dy *= scale;
1036 width *= scale;
1037 height *= scale;
1038 sx += dx;
1039 sy += dy;
1040 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
1041
joshualitt9bd2daf2015-04-17 09:30:06 -07001042 // TODO combine with the above
1043 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001044 if (glyph->fTooLargeForAtlas) {
joshualitt0fe04a22015-08-25 12:05:50 -07001045 this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -07001046 return true;
1047 }
1048
joshualittf528e0d2015-12-09 06:42:52 -08001049 blob->appendGlyph(runIndex, glyphRect, color, fCurrStrike, glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001050 return true;
1051}
1052
joshualitt374b2f72015-07-21 08:05:03 -07001053inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001054 GrFontScaler* scaler, const SkGlyph& skGlyph,
joshualitt0fe04a22015-08-25 12:05:50 -07001055 SkScalar x, SkScalar y, SkScalar scale,
1056 bool applyVM) {
halcanary96fcdcc2015-08-27 07:41:13 -07001057 if (nullptr == glyph->fPath) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001058 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
1059 if (!glyphPath) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001060 return;
1061 }
joshualitt6c2c2b02015-07-24 10:37:00 -07001062
halcanary385fe4d2015-08-26 13:07:48 -07001063 glyph->fPath = new SkPath(*glyphPath);
joshualitt9bd2daf2015-04-17 09:30:06 -07001064 }
joshualitt0fe04a22015-08-25 12:05:50 -07001065 blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
joshualitt9bd2daf2015-04-17 09:30:06 -07001066}
1067
robertphillips7bceedc2015-12-01 12:51:26 -08001068void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -08001069 const SkTextBlobRunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -07001070 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07001071 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
1072 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
1073 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07001074
joshualitt9a27e632015-04-06 10:53:36 -07001075 size_t textLen = it.glyphCount() * sizeof(uint16_t);
1076 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07001077
joshualitt9a27e632015-04-06 10:53:36 -07001078 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07001079
joshualitt9a27e632015-04-06 10:53:36 -07001080 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
1081 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07001082 }
1083
robertphillipsfcf78292015-06-19 11:49:52 -07001084 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07001085
1086 switch (it.positioning()) {
1087 case SkTextBlob::kDefault_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -08001088 this->drawTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001089 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001090 textLen, x + offset.x(), y + offset.y(), clipBounds);
1091 break;
1092 case SkTextBlob::kHorizontal_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -08001093 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001094 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001095 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
1096 clipBounds);
1097 break;
1098 case SkTextBlob::kFull_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -08001099 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001100 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001101 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
1102 break;
1103 }
1104}
1105
bsalomonabd30f52015-08-13 13:34:48 -07001106inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -07001107GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -07001108 int glyphCount, int run, int subRun,
1109 GrColor color, SkScalar transX, SkScalar transY,
1110 const SkPaint& skPaint) {
joshualitt3660d532015-12-07 11:32:50 -08001111 GrMaskFormat format = info.maskFormat();
joshualitt79dfb2b2015-05-11 08:58:08 -07001112 GrColor subRunColor;
1113 if (kARGB_GrMaskFormat == format) {
1114 uint8_t paintAlpha = skPaint.getAlpha();
1115 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
1116 } else {
1117 subRunColor = color;
1118 }
1119
joshualitta751c972015-11-20 13:37:32 -08001120 GrAtlasTextBatch* batch;
joshualitt3660d532015-12-07 11:32:50 -08001121 if (info.drawAsDistanceFields()) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001122 SkColor filteredColor;
1123 SkColorFilter* colorFilter = skPaint.getColorFilter();
1124 if (colorFilter) {
1125 filteredColor = colorFilter->filterColor(skPaint.getColor());
1126 } else {
1127 filteredColor = skPaint.getColor();
1128 }
robertphillipsfcf78292015-06-19 11:49:52 -07001129 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
joshualitta751c972015-11-20 13:37:32 -08001130 batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
1131 fDistanceAdjustTable, filteredColor,
joshualitt3660d532015-12-07 11:32:50 -08001132 info.hasUseLCDText(), useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07001133 } else {
joshualitta751c972015-11-20 13:37:32 -08001134 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07001135 }
joshualitta751c972015-11-20 13:37:32 -08001136 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07001137 geometry.fBlob = SkRef(cacheBlob);
1138 geometry.fRun = run;
1139 geometry.fSubRun = subRun;
1140 geometry.fColor = subRunColor;
1141 geometry.fTransX = transX;
1142 geometry.fTransY = transY;
1143 batch->init();
1144
1145 return batch;
1146}
1147
robertphillipsf6703fa2015-09-01 05:36:47 -07001148inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07001149 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07001150 SkScalar transX, SkScalar transY,
1151 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07001152 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
1153 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
joshualitt3660d532015-12-07 11:32:50 -08001154 int glyphCount = info.glyphCount();
joshualitt9a27e632015-04-06 10:53:36 -07001155 if (0 == glyphCount) {
1156 continue;
1157 }
1158
bsalomonabd30f52015-08-13 13:34:48 -07001159 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
1160 subRun, color, transX, transY,
1161 skPaint));
robertphillipsf6703fa2015-09-01 05:36:47 -07001162 dc->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07001163 }
1164}
1165
herbe59124e2015-11-18 10:54:39 -08001166inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob,
robertphillips7bceedc2015-12-01 12:51:26 -08001167 GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -07001168 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07001169 SkScalar transX, SkScalar transY,
1170 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07001171 if (!cacheBlob->fBigGlyphs.count()) {
1172 return;
1173 }
1174
joshualitt9a27e632015-04-06 10:53:36 -07001175 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07001176 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07001177 bigGlyph.fVx += transX;
1178 bigGlyph.fVy += transY;
joshualitt0fe04a22015-08-25 12:05:50 -07001179 SkMatrix ctm;
1180 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
1181 ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
1182 if (bigGlyph.fApplyVM) {
1183 ctm.postConcat(cacheBlob->fViewMatrix);
1184 }
joshualittfc072562015-05-13 12:15:06 -07001185
robertphillips7bceedc2015-12-01 12:51:26 -08001186 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, bigGlyph.fPath,
joshualitt0fe04a22015-08-25 12:05:50 -07001187 skPaint, ctm, nullptr, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07001188 }
1189}
joshualitt9a27e632015-04-06 10:53:36 -07001190
robertphillips2334fb62015-06-17 05:43:33 -07001191void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -07001192 GrAtlasTextBlob* cacheBlob,
herbe59124e2015-11-18 10:54:39 -08001193 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07001194 const SkPaint& skPaint,
1195 const GrPaint& grPaint,
1196 SkDrawFilter* drawFilter,
1197 const GrClip& clip,
1198 const SkMatrix& viewMatrix,
1199 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07001200 SkScalar x, SkScalar y,
1201 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07001202 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
1203 // it as paths
robertphillips433625e2015-12-04 06:58:16 -08001204 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
joshualitt9a27e632015-04-06 10:53:36 -07001205
1206 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001207
halcanary33779752015-10-27 14:01:05 -07001208 SkTextBlobRunIterator it(blob);
joshualitt9a27e632015-04-06 10:53:36 -07001209 for (int run = 0; !it.done(); it.next(), run++) {
1210 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillips7bceedc2015-12-01 12:51:26 -08001211 this->flushRunAsPaths(dc, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07001212 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07001213 continue;
1214 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001215 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillipsf6703fa2015-09-01 05:36:47 -07001216 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07001217 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001218 }
1219
1220 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001221 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001222}
1223
joshualitt374b2f72015-07-21 08:05:03 -07001224void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07001225 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07001226 const SkPaint& skPaint,
1227 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07001228 const GrClip& clip,
1229 const SkIRect& clipBounds) {
robertphillips433625e2015-12-04 06:58:16 -08001230 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
joshualitt9a27e632015-04-06 10:53:36 -07001231
1232 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001233 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillipsf6703fa2015-09-01 05:36:47 -07001234 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001235 }
1236
1237 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001238 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001239}
joshualitt79dfb2b2015-05-11 08:58:08 -07001240
1241///////////////////////////////////////////////////////////////////////////////////////////////////
1242
1243#ifdef GR_TEST_UTILS
1244
bsalomonabd30f52015-08-13 13:34:48 -07001245DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001246 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -07001247 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -07001248 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07001249
1250 if (context->uniqueID() != gContextID) {
1251 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -07001252 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -07001253
joshualitt79dfb2b2015-05-11 08:58:08 -07001254 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
1255 // because we don't really want to have a gpu device here.
1256 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -07001257 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07001258 }
1259
joshualitt79dfb2b2015-05-11 08:58:08 -07001260 // Setup dummy SkPaint / GrPaint
1261 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001262 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07001263 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07001264 skPaint.setColor(color);
1265 skPaint.setLCDRenderText(random->nextBool());
1266 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
1267 skPaint.setSubpixelText(random->nextBool());
1268
1269 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001270 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001271 SkFAIL("couldn't convert paint\n");
1272 }
1273
1274 const char* text = "The quick brown fox jumps over the lazy dog.";
1275 int textLen = (int)strlen(text);
1276
1277 // Setup clip
1278 GrClip clip;
1279 SkIRect noClip = SkIRect::MakeLargest();
1280
1281 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
1282 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07001283 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -08001284 gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text,
joshualitt79dfb2b2015-05-11 08:58:08 -07001285 static_cast<size_t>(textLen), 0, 0, noClip));
1286
1287 SkScalar transX = static_cast<SkScalar>(random->nextU());
1288 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07001289 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07001290 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
1291}
1292
1293#endif