blob: 50238574262ebb043f5437a06852e53b40f895ea [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
joshualitta751c972015-11-20 13:37:32 -080058 static_assert(GrAtlasTextBatch::kGrayTextVASize >= GrAtlasTextBatch::kColorTextVASize &&
59 GrAtlasTextBatch::kGrayTextVASize >= GrAtlasTextBatch::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,
joshualitta751c972015-11-20 13:37:32 -0800352 GrAtlasTextBatch::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,
joshualitta751c972015-11-20 13:37:32 -0800380 GrAtlasTextBatch::kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700381 } else {
joshualitta751c972015-11-20 13:37:32 -0800382 cacheBlob.reset(fCache->createBlob(blob, GrAtlasTextBatch::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) {
joshualitta751c972015-11-20 13:37:32 -0800627 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBatch::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 {
joshualitta751c972015-11-20 13:37:32 -0800665 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBatch::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 {
joshualitta751c972015-11-20 13:37:32 -0800701 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBatch::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) {
joshualittae32c102015-04-21 09:37:57 -0700970 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -0700971 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700972 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
973 }
974
joshualitt6c2c2b02015-07-24 10:37:00 -0700975 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
976 skGlyph.getSubXFixed(),
977 skGlyph.getSubYFixed(),
978 GrGlyph::kCoverage_MaskStyle);
979 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700980 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700981 return;
982 }
983
984 int x = vx + glyph->fBounds.fLeft;
985 int y = vy + glyph->fBounds.fTop;
986
987 // keep them as ints until we've done the clip-test
988 int width = glyph->fBounds.width();
989 int height = glyph->fBounds.height();
990
joshualitt1d89e8d2015-04-01 12:40:54 -0700991 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -0700992 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -0700993 this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy));
joshualitt1d89e8d2015-04-01 12:40:54 -0700994 return;
995 }
996
joshualitt1d89e8d2015-04-01 12:40:54 -0700997 GrMaskFormat format = glyph->fMaskFormat;
998
999 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
joshualitt3660d532015-12-07 11:32:50 -08001000 if (run.fInitialized && subRun->maskFormat() != format) {
joshualittd9f13ae2015-07-24 11:24:31 -07001001 subRun = &run.push_back();
joshualitt3660d532015-12-07 11:32:50 -08001002 subRun->setStrike(fCurrStrike);
joshualitt7e97b0b2015-07-31 15:18:08 -07001003 } else if (!run.fInitialized) {
joshualitt18b072d2015-12-07 12:26:12 -08001004 subRun->setStrike(fCurrStrike);
joshualitt1d89e8d2015-04-01 12:40:54 -07001005 }
1006
1007 run.fInitialized = true;
joshualitt1d89e8d2015-04-01 12:40:54 -07001008
joshualitta751c972015-11-20 13:37:32 -08001009 size_t vertexStride = GrAtlasTextBatch::GetVertexStride(format);
joshualitt1d89e8d2015-04-01 12:40:54 -07001010
1011 SkRect r;
1012 r.fLeft = SkIntToScalar(x);
1013 r.fTop = SkIntToScalar(y);
1014 r.fRight = r.fLeft + SkIntToScalar(width);
1015 r.fBottom = r.fTop + SkIntToScalar(height);
joshualitt3660d532015-12-07 11:32:50 -08001016 subRun->setMaskFormat(format);
joshualitt18b072d2015-12-07 12:26:12 -08001017 blob->appendGlyph(&run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format, glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001018}
joshualitt1d89e8d2015-04-01 12:40:54 -07001019
joshualitt374b2f72015-07-21 08:05:03 -07001020bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001021 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001022 SkScalar sx, SkScalar sy, GrColor color,
1023 GrFontScaler* scaler,
joshualitt9bd2daf2015-04-17 09:30:06 -07001024 SkScalar textRatio, const SkMatrix& viewMatrix) {
joshualittae32c102015-04-21 09:37:57 -07001025 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001026 if (!fCurrStrike) {
1027 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1028 }
1029
joshualitt6c2c2b02015-07-24 10:37:00 -07001030 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1031 skGlyph.getSubXFixed(),
1032 skGlyph.getSubYFixed(),
1033 GrGlyph::kDistance_MaskStyle);
1034 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001035 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001036 return true;
1037 }
1038
1039 // fallback to color glyph support
1040 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
1041 return false;
1042 }
1043
1044 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
1045 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
1046 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
1047 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
1048
1049 SkScalar scale = textRatio;
1050 dx *= scale;
1051 dy *= scale;
1052 width *= scale;
1053 height *= scale;
1054 sx += dx;
1055 sy += dy;
1056 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
1057
joshualitt9bd2daf2015-04-17 09:30:06 -07001058 // TODO combine with the above
1059 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001060 if (glyph->fTooLargeForAtlas) {
joshualitt0fe04a22015-08-25 12:05:50 -07001061 this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -07001062 return true;
1063 }
1064
joshualitt9bd2daf2015-04-17 09:30:06 -07001065 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001066 if (!run.fInitialized) {
joshualitt3660d532015-12-07 11:32:50 -08001067 subRun->setStrike(fCurrStrike);
joshualitt7e97b0b2015-07-31 15:18:08 -07001068 }
1069 run.fInitialized = true;
joshualitt9bd2daf2015-04-17 09:30:06 -07001070 SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat);
joshualitt3660d532015-12-07 11:32:50 -08001071 subRun->setMaskFormat(kA8_GrMaskFormat);
joshualitt9bd2daf2015-04-17 09:30:06 -07001072
joshualitta751c972015-11-20 13:37:32 -08001073 size_t vertexStride = GrAtlasTextBatch::GetVertexStrideDf(kA8_GrMaskFormat,
joshualitt3660d532015-12-07 11:32:50 -08001074 subRun->hasUseLCDText());
joshualitt9bd2daf2015-04-17 09:30:06 -07001075
joshualitt3660d532015-12-07 11:32:50 -08001076 bool useColorVerts = !subRun->hasUseLCDText();
joshualitt18b072d2015-12-07 12:26:12 -08001077 blob->appendGlyph(&run, subRun, glyphRect, color, vertexStride, useColorVerts, glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001078 return true;
1079}
1080
joshualitt374b2f72015-07-21 08:05:03 -07001081inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001082 GrFontScaler* scaler, const SkGlyph& skGlyph,
joshualitt0fe04a22015-08-25 12:05:50 -07001083 SkScalar x, SkScalar y, SkScalar scale,
1084 bool applyVM) {
halcanary96fcdcc2015-08-27 07:41:13 -07001085 if (nullptr == glyph->fPath) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001086 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
1087 if (!glyphPath) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001088 return;
1089 }
joshualitt6c2c2b02015-07-24 10:37:00 -07001090
halcanary385fe4d2015-08-26 13:07:48 -07001091 glyph->fPath = new SkPath(*glyphPath);
joshualitt9bd2daf2015-04-17 09:30:06 -07001092 }
joshualitt0fe04a22015-08-25 12:05:50 -07001093 blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
joshualitt9bd2daf2015-04-17 09:30:06 -07001094}
1095
robertphillips7bceedc2015-12-01 12:51:26 -08001096void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -08001097 const SkTextBlobRunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -07001098 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07001099 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
1100 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
1101 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07001102
joshualitt9a27e632015-04-06 10:53:36 -07001103 size_t textLen = it.glyphCount() * sizeof(uint16_t);
1104 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07001105
joshualitt9a27e632015-04-06 10:53:36 -07001106 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07001107
joshualitt9a27e632015-04-06 10:53:36 -07001108 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
1109 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07001110 }
1111
robertphillipsfcf78292015-06-19 11:49:52 -07001112 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07001113
1114 switch (it.positioning()) {
1115 case SkTextBlob::kDefault_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -08001116 this->drawTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001117 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001118 textLen, x + offset.x(), y + offset.y(), clipBounds);
1119 break;
1120 case SkTextBlob::kHorizontal_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -08001121 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001122 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001123 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
1124 clipBounds);
1125 break;
1126 case SkTextBlob::kFull_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -08001127 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001128 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001129 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
1130 break;
1131 }
1132}
1133
bsalomonabd30f52015-08-13 13:34:48 -07001134inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -07001135GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -07001136 int glyphCount, int run, int subRun,
1137 GrColor color, SkScalar transX, SkScalar transY,
1138 const SkPaint& skPaint) {
joshualitt3660d532015-12-07 11:32:50 -08001139 GrMaskFormat format = info.maskFormat();
joshualitt79dfb2b2015-05-11 08:58:08 -07001140 GrColor subRunColor;
1141 if (kARGB_GrMaskFormat == format) {
1142 uint8_t paintAlpha = skPaint.getAlpha();
1143 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
1144 } else {
1145 subRunColor = color;
1146 }
1147
joshualitta751c972015-11-20 13:37:32 -08001148 GrAtlasTextBatch* batch;
joshualitt3660d532015-12-07 11:32:50 -08001149 if (info.drawAsDistanceFields()) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001150 SkColor filteredColor;
1151 SkColorFilter* colorFilter = skPaint.getColorFilter();
1152 if (colorFilter) {
1153 filteredColor = colorFilter->filterColor(skPaint.getColor());
1154 } else {
1155 filteredColor = skPaint.getColor();
1156 }
robertphillipsfcf78292015-06-19 11:49:52 -07001157 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
joshualitta751c972015-11-20 13:37:32 -08001158 batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
1159 fDistanceAdjustTable, filteredColor,
joshualitt3660d532015-12-07 11:32:50 -08001160 info.hasUseLCDText(), useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07001161 } else {
joshualitta751c972015-11-20 13:37:32 -08001162 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07001163 }
joshualitta751c972015-11-20 13:37:32 -08001164 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07001165 geometry.fBlob = SkRef(cacheBlob);
1166 geometry.fRun = run;
1167 geometry.fSubRun = subRun;
1168 geometry.fColor = subRunColor;
1169 geometry.fTransX = transX;
1170 geometry.fTransY = transY;
1171 batch->init();
1172
1173 return batch;
1174}
1175
robertphillipsf6703fa2015-09-01 05:36:47 -07001176inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07001177 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07001178 SkScalar transX, SkScalar transY,
1179 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07001180 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
1181 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
joshualitt3660d532015-12-07 11:32:50 -08001182 int glyphCount = info.glyphCount();
joshualitt9a27e632015-04-06 10:53:36 -07001183 if (0 == glyphCount) {
1184 continue;
1185 }
1186
bsalomonabd30f52015-08-13 13:34:48 -07001187 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
1188 subRun, color, transX, transY,
1189 skPaint));
robertphillipsf6703fa2015-09-01 05:36:47 -07001190 dc->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07001191 }
1192}
1193
herbe59124e2015-11-18 10:54:39 -08001194inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob,
robertphillips7bceedc2015-12-01 12:51:26 -08001195 GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -07001196 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07001197 SkScalar transX, SkScalar transY,
1198 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07001199 if (!cacheBlob->fBigGlyphs.count()) {
1200 return;
1201 }
1202
joshualitt9a27e632015-04-06 10:53:36 -07001203 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07001204 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07001205 bigGlyph.fVx += transX;
1206 bigGlyph.fVy += transY;
joshualitt0fe04a22015-08-25 12:05:50 -07001207 SkMatrix ctm;
1208 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
1209 ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
1210 if (bigGlyph.fApplyVM) {
1211 ctm.postConcat(cacheBlob->fViewMatrix);
1212 }
joshualittfc072562015-05-13 12:15:06 -07001213
robertphillips7bceedc2015-12-01 12:51:26 -08001214 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, bigGlyph.fPath,
joshualitt0fe04a22015-08-25 12:05:50 -07001215 skPaint, ctm, nullptr, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07001216 }
1217}
joshualitt9a27e632015-04-06 10:53:36 -07001218
robertphillips2334fb62015-06-17 05:43:33 -07001219void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -07001220 GrAtlasTextBlob* cacheBlob,
herbe59124e2015-11-18 10:54:39 -08001221 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07001222 const SkPaint& skPaint,
1223 const GrPaint& grPaint,
1224 SkDrawFilter* drawFilter,
1225 const GrClip& clip,
1226 const SkMatrix& viewMatrix,
1227 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07001228 SkScalar x, SkScalar y,
1229 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07001230 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
1231 // it as paths
robertphillips433625e2015-12-04 06:58:16 -08001232 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
joshualitt9a27e632015-04-06 10:53:36 -07001233
1234 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001235
halcanary33779752015-10-27 14:01:05 -07001236 SkTextBlobRunIterator it(blob);
joshualitt9a27e632015-04-06 10:53:36 -07001237 for (int run = 0; !it.done(); it.next(), run++) {
1238 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillips7bceedc2015-12-01 12:51:26 -08001239 this->flushRunAsPaths(dc, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07001240 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07001241 continue;
1242 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001243 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillipsf6703fa2015-09-01 05:36:47 -07001244 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07001245 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001246 }
1247
1248 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001249 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001250}
1251
joshualitt374b2f72015-07-21 08:05:03 -07001252void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07001253 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07001254 const SkPaint& skPaint,
1255 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07001256 const GrClip& clip,
1257 const SkIRect& clipBounds) {
robertphillips433625e2015-12-04 06:58:16 -08001258 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
joshualitt9a27e632015-04-06 10:53:36 -07001259
1260 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001261 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillipsf6703fa2015-09-01 05:36:47 -07001262 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001263 }
1264
1265 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001266 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001267}
joshualitt79dfb2b2015-05-11 08:58:08 -07001268
1269///////////////////////////////////////////////////////////////////////////////////////////////////
1270
1271#ifdef GR_TEST_UTILS
1272
bsalomonabd30f52015-08-13 13:34:48 -07001273DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001274 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -07001275 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -07001276 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07001277
1278 if (context->uniqueID() != gContextID) {
1279 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -07001280 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -07001281
joshualitt79dfb2b2015-05-11 08:58:08 -07001282 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
1283 // because we don't really want to have a gpu device here.
1284 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -07001285 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07001286 }
1287
joshualitt79dfb2b2015-05-11 08:58:08 -07001288 // Setup dummy SkPaint / GrPaint
1289 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001290 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07001291 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07001292 skPaint.setColor(color);
1293 skPaint.setLCDRenderText(random->nextBool());
1294 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
1295 skPaint.setSubpixelText(random->nextBool());
1296
1297 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001298 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001299 SkFAIL("couldn't convert paint\n");
1300 }
1301
1302 const char* text = "The quick brown fox jumps over the lazy dog.";
1303 int textLen = (int)strlen(text);
1304
1305 // Setup clip
1306 GrClip clip;
1307 SkIRect noClip = SkIRect::MakeLargest();
1308
1309 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
1310 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07001311 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -08001312 gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text,
joshualitt79dfb2b2015-05-11 08:58:08 -07001313 static_cast<size_t>(textLen), 0, 0, noClip));
1314
1315 SkScalar transX = static_cast<SkScalar>(random->nextU());
1316 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07001317 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07001318 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
1319}
1320
1321#endif