blob: 7d0d93684cd206758114f21f1d4a5f060c3f7868 [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
robertphillipsf6703fa2015-09-01 05:36:47 -0700293void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc, GrRenderTarget* rt,
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
robertphillipsf6703fa2015-09-01 05:36:47 -0700388 this->flush(blob, cacheBlob, dc, rt, 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
joshualitt1d89e8d2015-04-01 12:40:54 -0700464 // setup vertex / glyphIndex for the new run
465 if (run > 0) {
466 PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back();
467 PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back();
468
469 newRun.fVertexStartIndex = lastRun.fVertexEndIndex;
470 newRun.fVertexEndIndex = lastRun.fVertexEndIndex;
471
472 newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex;
473 newRun.fGlyphEndIndex = lastRun.fGlyphEndIndex;
474 }
475
joshualittfcfb9fc2015-04-21 07:35:10 -0700476 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
477 cacheBlob->setHasDistanceField();
478 SkPaint dfPaint = runPaint;
479 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700480 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700481 Run& runIdx = cacheBlob->fRuns[run];
482 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
483 subRun.fUseLCDText = runPaint.isLCDRenderText();
484 subRun.fDrawAsDistanceFields = true;
joshualitt9a27e632015-04-06 10:53:36 -0700485
joshualittfcfb9fc2015-04-21 07:35:10 -0700486 SkTDArray<char> fallbackTxt;
487 SkTDArray<SkScalar> fallbackPos;
488 SkPoint dfOffset;
489 int scalarsPerPosition = 2;
490 switch (it.positioning()) {
491 case SkTextBlob::kDefault_Positioning: {
jvanverth157e6482015-09-09 08:05:12 -0700492 this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700493 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800494 x + offset.x(), y + offset.y(), textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700495 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
496 break;
497 }
498 case SkTextBlob::kHorizontal_Positioning: {
499 scalarsPerPosition = 1;
500 dfOffset = SkPoint::Make(x, y + offset.y());
jvanverth157e6482015-09-09 08:05:12 -0700501 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700502 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800503 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700504 &fallbackTxt, &fallbackPos);
505 break;
506 }
507 case SkTextBlob::kFull_Positioning: {
508 dfOffset = SkPoint::Make(x, y);
jvanverth157e6482015-09-09 08:05:12 -0700509 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700510 (const char*)it.glyphs(), textLen, it.pos(),
robertphillips6ee690e2015-12-02 08:57:50 -0800511 scalarsPerPosition, dfOffset, textRatio,
joshualittfcfb9fc2015-04-21 07:35:10 -0700512 &fallbackTxt, &fallbackPos);
513 break;
514 }
515 }
516 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800517 this->fallbackDrawPosText(cacheBlob, run, clip, color, runPaint, viewMatrix,
518 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset);
joshualittfcfb9fc2015-04-21 07:35:10 -0700519 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700520 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
521 cacheBlob->fRuns[run].fDrawAsPaths = true;
522 } else {
523 cacheBlob->setHasBitmap();
524 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
525 false);
526 switch (it.positioning()) {
527 case SkTextBlob::kDefault_Positioning:
528 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
529 (const char *)it.glyphs(), textLen,
robertphillips6ee690e2015-12-02 08:57:50 -0800530 x + offset.x(), y + offset.y());
joshualittfcfb9fc2015-04-21 07:35:10 -0700531 break;
532 case SkTextBlob::kHorizontal_Positioning:
533 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
534 (const char*)it.glyphs(), textLen, it.pos(), 1,
robertphillips6ee690e2015-12-02 08:57:50 -0800535 SkPoint::Make(x, y + offset.y()));
joshualittfcfb9fc2015-04-21 07:35:10 -0700536 break;
537 case SkTextBlob::kFull_Positioning:
538 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
539 (const char*)it.glyphs(), textLen, it.pos(), 2,
robertphillips6ee690e2015-12-02 08:57:50 -0800540 SkPoint::Make(x, y));
joshualittfcfb9fc2015-04-21 07:35:10 -0700541 break;
542 }
543 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700544 }
545
546 if (drawFilter) {
547 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
548 runPaint = skPaint;
549 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700550 }
551}
552
joshualitt374b2f72015-07-21 08:05:03 -0700553inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700554 SkPaint* skPaint,
555 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700556 const SkMatrix& viewMatrix) {
557 // getMaxScale doesn't support perspective, so neither do we at the moment
558 SkASSERT(!viewMatrix.hasPerspective());
559 SkScalar maxScale = viewMatrix.getMaxScale();
560 SkScalar textSize = skPaint->getTextSize();
561 SkScalar scaledTextSize = textSize;
562 // if we have non-unity scale, we need to choose our base text size
563 // based on the SkPaint's text size multiplied by the max scale factor
564 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
565 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
566 scaledTextSize *= maxScale;
567 }
568
joshualitt64c99cc2015-04-21 09:43:03 -0700569 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
570 // and ceiling. A scale outside of this range would require regenerating the distance fields
571 SkScalar dfMaskScaleFloor;
572 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700573 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700574 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700575 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700576 *textRatio = textSize / kSmallDFFontSize;
577 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
578 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700579 dfMaskScaleFloor = kSmallDFFontLimit;
580 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700581 *textRatio = textSize / kMediumDFFontSize;
582 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
583 } else {
joshualitta7c63892015-04-21 13:24:37 -0700584 dfMaskScaleFloor = kMediumDFFontLimit;
585 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700586 *textRatio = textSize / kLargeDFFontSize;
587 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
588 }
589
joshualitt64c99cc2015-04-21 09:43:03 -0700590 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
591 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
592 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
593 // tolerate before we'd have to move to a large mip size. When we actually test these values
594 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
595 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
596 // level)
joshualitta7c63892015-04-21 13:24:37 -0700597 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700598 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
599 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
600
joshualitt9bd2daf2015-04-17 09:30:06 -0700601 skPaint->setLCDRenderText(false);
602 skPaint->setAutohinted(false);
603 skPaint->setHinting(SkPaint::kNormal_Hinting);
604 skPaint->setSubpixelText(true);
605}
606
joshualitt374b2f72015-07-21 08:05:03 -0700607inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700608 int runIndex,
robertphillips6ee690e2015-12-02 08:57:50 -0800609 const GrClip& clip,
jvanverth0628a522015-08-18 07:44:22 -0700610 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700611 const SkPaint& skPaint,
612 const SkMatrix& viewMatrix,
613 const SkTDArray<char>& fallbackTxt,
614 const SkTDArray<SkScalar>& fallbackPos,
615 int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800616 const SkPoint& offset) {
joshualittfec19e12015-04-17 10:32:32 -0700617 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700618 blob->setHasBitmap();
619 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700620 // Push back a new subrun to fill and set the override descriptor
621 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700622 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitt97202d22015-04-22 13:47:02 -0700623 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700624 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700625 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700626 run.fOverrideDescriptor->getDesc());
jvanverth0628a522015-08-18 07:44:22 -0700627 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700628 fallbackTxt.begin(), fallbackTxt.count(),
robertphillips6ee690e2015-12-02 08:57:50 -0800629 fallbackPos.begin(), scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700630 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700631}
632
joshualitt374b2f72015-07-21 08:05:03 -0700633inline GrAtlasTextBlob*
herbe59124e2015-11-18 10:54:39 -0800634GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
635 const SkMatrix& viewMatrix, SkPaint* dfPaint,
jvanverth157e6482015-09-09 08:05:12 -0700636 SkScalar* textRatio) {
joshualitta751c972015-11-20 13:37:32 -0800637 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBatch::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700638
639 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700640 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700641 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700642 Run& run = blob->fRuns[0];
643 PerSubRunInfo& subRun = run.fSubRunInfo.back();
644 subRun.fUseLCDText = origPaint.isLCDRenderText();
645 subRun.fDrawAsDistanceFields = true;
joshualitt9bd2daf2015-04-17 09:30:06 -0700646
joshualitt9bd2daf2015-04-17 09:30:06 -0700647 return blob;
648}
649
joshualitt374b2f72015-07-21 08:05:03 -0700650inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800651GrAtlasTextContext::createDrawTextBlob(const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700652 const GrPaint& paint, const SkPaint& skPaint,
653 const SkMatrix& viewMatrix,
654 const char text[], size_t byteLength,
655 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700656 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700657
joshualitt374b2f72015-07-21 08:05:03 -0700658 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700659 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
660 SkPaint dfPaint;
661 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700662 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700663
joshualitt9bd2daf2015-04-17 09:30:06 -0700664 SkTDArray<char> fallbackTxt;
665 SkTDArray<SkScalar> fallbackPos;
666 SkPoint offset;
jvanverth157e6482015-09-09 08:05:12 -0700667 this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800668 byteLength, x, y, textRatio, &fallbackTxt, &fallbackPos,
joshualitt9bd2daf2015-04-17 09:30:06 -0700669 &offset, skPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700670 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800671 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
672 fallbackTxt, fallbackPos, 2, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700673 }
674 } else {
joshualitta751c972015-11-20 13:37:32 -0800675 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBatch::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700676 blob->fViewMatrix = viewMatrix;
677
678 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
679 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800680 byteLength, x, y);
joshualitt9bd2daf2015-04-17 09:30:06 -0700681 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700682 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700683 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700684}
685
joshualitt374b2f72015-07-21 08:05:03 -0700686inline GrAtlasTextBlob*
robertphillips6ee690e2015-12-02 08:57:50 -0800687GrAtlasTextContext::createDrawPosTextBlob(const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700688 const GrPaint& paint, const SkPaint& skPaint,
689 const SkMatrix& viewMatrix,
690 const char text[], size_t byteLength,
691 const SkScalar pos[], int scalarsPerPosition,
692 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700693 int glyphCount = skPaint.countText(text, byteLength);
694
joshualitt374b2f72015-07-21 08:05:03 -0700695 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700696 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
697 SkPaint dfPaint;
698 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700699 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700700
701 SkTDArray<char> fallbackTxt;
702 SkTDArray<SkScalar> fallbackPos;
jvanverth157e6482015-09-09 08:05:12 -0700703 this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800704 byteLength, pos, scalarsPerPosition, offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700705 textRatio, &fallbackTxt, &fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700706 if (fallbackTxt.count()) {
robertphillips6ee690e2015-12-02 08:57:50 -0800707 this->fallbackDrawPosText(blob, 0, clip, paint.getColor(), skPaint, viewMatrix,
708 fallbackTxt, fallbackPos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700709 }
710 } else {
joshualitta751c972015-11-20 13:37:32 -0800711 blob = fCache->createBlob(glyphCount, 1, GrAtlasTextBatch::kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700712 blob->fViewMatrix = viewMatrix;
713 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
714 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
robertphillips6ee690e2015-12-02 08:57:50 -0800715 byteLength, pos, scalarsPerPosition, offset);
joshualitt9bd2daf2015-04-17 09:30:06 -0700716 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700717 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700718 return blob;
719}
720
robertphillipsf6703fa2015-09-01 05:36:47 -0700721void GrAtlasTextContext::onDrawText(GrDrawContext* dc, GrRenderTarget* rt,
herbe59124e2015-11-18 10:54:39 -0800722 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700723 const GrPaint& paint, const SkPaint& skPaint,
724 const SkMatrix& viewMatrix,
725 const char text[], size_t byteLength,
726 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700727 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800728 this->createDrawTextBlob(clip, paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700729 text, byteLength, x, y, regionClipBounds));
robertphillipsf6703fa2015-09-01 05:36:47 -0700730 this->flush(blob, dc, rt, skPaint, paint, clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700731}
732
robertphillipsf6703fa2015-09-01 05:36:47 -0700733void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700734 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700735 const GrPaint& paint, const SkPaint& skPaint,
736 const SkMatrix& viewMatrix,
737 const char text[], size_t byteLength,
738 const SkScalar pos[], int scalarsPerPosition,
739 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700740 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -0800741 this->createDrawPosTextBlob(clip, paint, skPaint, viewMatrix,
robertphillips2334fb62015-06-17 05:43:33 -0700742 text, byteLength,
743 pos, scalarsPerPosition,
744 offset, regionClipBounds));
joshualitt79dfb2b2015-05-11 08:58:08 -0700745
robertphillipsf6703fa2015-09-01 05:36:47 -0700746 this->flush(blob, dc, rt, skPaint, paint, clip, regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700747}
748
joshualitt374b2f72015-07-21 08:05:03 -0700749void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700750 SkGlyphCache* cache, const SkPaint& skPaint,
751 GrColor color,
752 const SkMatrix& viewMatrix,
753 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800754 SkScalar x, SkScalar y) {
halcanary96fcdcc2015-08-27 07:41:13 -0700755 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700756
757 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700758 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700759 return;
760 }
761
halcanary96fcdcc2015-08-27 07:41:13 -0700762 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700763
764 // Get GrFontScaler from cache
765 GrFontScaler* fontScaler = GetGrFontScaler(cache);
766
herbe59124e2015-11-18 10:54:39 -0800767 SkFindAndPlaceGlyph::ProcessText(
herb4c11b3f2015-11-20 13:53:12 -0800768 skPaint.getTextEncoding(), text, byteLength,
769 {x, y}, viewMatrix, skPaint.getTextAlign(),
770 cache,
herbe59124e2015-11-18 10:54:39 -0800771 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
772 position += rounding;
773 this->bmpAppendGlyph(
774 blob, runIndex, glyph,
775 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800776 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700777 }
herbe59124e2015-11-18 10:54:39 -0800778 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700779}
780
joshualitt374b2f72015-07-21 08:05:03 -0700781void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700782 SkGlyphCache* cache, const SkPaint& skPaint,
783 GrColor color,
784 const SkMatrix& viewMatrix,
785 const char text[], size_t byteLength,
786 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800787 const SkPoint& offset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700788 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700789 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
790
791 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700792 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700793 return;
794 }
795
halcanary96fcdcc2015-08-27 07:41:13 -0700796 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700797
798 // Get GrFontScaler from cache
799 GrFontScaler* fontScaler = GetGrFontScaler(cache);
800
herb9be5ff62015-11-11 11:30:11 -0800801 SkFindAndPlaceGlyph::ProcessPosText(
herb4c11b3f2015-11-20 13:53:12 -0800802 skPaint.getTextEncoding(), text, byteLength,
803 offset, viewMatrix, pos, scalarsPerPosition,
804 skPaint.getTextAlign(), cache,
herb9be5ff62015-11-11 11:30:11 -0800805 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
806 position += rounding;
807 this->bmpAppendGlyph(
808 blob, runIndex, glyph,
809 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
robertphillips6ee690e2015-12-02 08:57:50 -0800810 color, fontScaler);
joshualitt1d89e8d2015-04-01 12:40:54 -0700811 }
herb9be5ff62015-11-11 11:30:11 -0800812 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700813}
814
joshualitt374b2f72015-07-21 08:05:03 -0700815void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700816 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700817 const SkMatrix& viewMatrix,
818 const char text[], size_t byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800819 SkScalar x, SkScalar y,
joshualitt9bd2daf2015-04-17 09:30:06 -0700820 SkScalar textRatio,
821 SkTDArray<char>* fallbackTxt,
822 SkTDArray<SkScalar>* fallbackPos,
823 SkPoint* offset,
824 const SkPaint& origPaint) {
halcanary96fcdcc2015-08-27 07:41:13 -0700825 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700826
827 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700828 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700829 return;
830 }
831
832 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
833 SkAutoDescriptor desc;
halcanary96fcdcc2015-08-27 07:41:13 -0700834 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
herbe59124e2015-11-18 10:54:39 -0800835 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
joshualitt9bd2daf2015-04-17 09:30:06 -0700836 desc.getDesc());
837
838 SkTArray<SkScalar> positions;
839
840 const char* textPtr = text;
841 SkFixed stopX = 0;
842 SkFixed stopY = 0;
843 SkFixed origin = 0;
844 switch (origPaint.getTextAlign()) {
845 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
846 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
847 case SkPaint::kLeft_Align: origin = 0; break;
848 }
849
850 SkAutoKern autokern;
851 const char* stop = text + byteLength;
852 while (textPtr < stop) {
853 // don't need x, y here, since all subpixel variants will have the
854 // same advance
855 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
856
857 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
858 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
859
860 SkFixed height = glyph.fAdvanceY;
861 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
862
863 stopX += width;
864 stopY += height;
865 }
866 SkASSERT(textPtr == stop);
867
jvanverth157e6482015-09-09 08:05:12 -0700868 SkGlyphCache::AttachCache(origPaintCache);
869
joshualitt9bd2daf2015-04-17 09:30:06 -0700870 // now adjust starting point depending on alignment
871 SkScalar alignX = SkFixedToScalar(stopX);
872 SkScalar alignY = SkFixedToScalar(stopY);
873 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
874 alignX = SkScalarHalf(alignX);
875 alignY = SkScalarHalf(alignY);
876 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
877 alignX = 0;
878 alignY = 0;
879 }
880 x -= alignX;
881 y -= alignY;
882 *offset = SkPoint::Make(x, y);
883
jvanverth157e6482015-09-09 08:05:12 -0700884 this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength,
robertphillips6ee690e2015-12-02 08:57:50 -0800885 positions.begin(), 2, *offset, textRatio, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700886 fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700887}
888
joshualitt374b2f72015-07-21 08:05:03 -0700889void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700890 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700891 const SkMatrix& viewMatrix,
892 const char text[], size_t byteLength,
893 const SkScalar pos[], int scalarsPerPosition,
robertphillips6ee690e2015-12-02 08:57:50 -0800894 const SkPoint& offset,
joshualitt9bd2daf2015-04-17 09:30:06 -0700895 SkScalar textRatio,
896 SkTDArray<char>* fallbackTxt,
897 SkTDArray<SkScalar>* fallbackPos) {
898
halcanary96fcdcc2015-08-27 07:41:13 -0700899 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700900 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
901
902 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700903 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700904 return;
905 }
906
halcanary96fcdcc2015-08-27 07:41:13 -0700907 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -0700908
909 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
jvanverth157e6482015-09-09 08:05:12 -0700910 SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700911 GrFontScaler* fontScaler = GetGrFontScaler(cache);
912
913 const char* stop = text + byteLength;
914
915 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
916 while (text < stop) {
917 const char* lastText = text;
918 // the last 2 parameters are ignored
919 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
920
921 if (glyph.fWidth) {
922 SkScalar x = offset.x() + pos[0];
923 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
924
925 if (!this->dfAppendGlyph(blob,
926 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700927 glyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800928 x, y, color, fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700929 textRatio, viewMatrix)) {
930 // couldn't append, send to fallback
931 fallbackTxt->append(SkToInt(text-lastText), lastText);
932 *fallbackPos->append() = pos[0];
933 if (2 == scalarsPerPosition) {
934 *fallbackPos->append() = pos[1];
935 }
936 }
937 }
938 pos += scalarsPerPosition;
939 }
940 } else {
941 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
942 : SK_Scalar1;
943 while (text < stop) {
944 const char* lastText = text;
945 // the last 2 parameters are ignored
946 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
947
948 if (glyph.fWidth) {
949 SkScalar x = offset.x() + pos[0];
950 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
951
952 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
953 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
954
955 if (!this->dfAppendGlyph(blob,
956 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700957 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700958 x - advanceX, y - advanceY, color,
959 fontScaler,
joshualitt9bd2daf2015-04-17 09:30:06 -0700960 textRatio,
961 viewMatrix)) {
962 // couldn't append, send to fallback
963 fallbackTxt->append(SkToInt(text-lastText), lastText);
964 *fallbackPos->append() = pos[0];
965 if (2 == scalarsPerPosition) {
966 *fallbackPos->append() = pos[1];
967 }
968 }
969 }
970 pos += scalarsPerPosition;
971 }
972 }
jvanverth157e6482015-09-09 08:05:12 -0700973
974 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700975}
976
joshualitt374b2f72015-07-21 08:05:03 -0700977void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700978 const SkGlyph& skGlyph,
robertphillips6ee690e2015-12-02 08:57:50 -0800979 int vx, int vy, GrColor color, GrFontScaler* scaler) {
joshualittae32c102015-04-21 09:37:57 -0700980 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -0700981 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700982 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
983 }
984
joshualitt6c2c2b02015-07-24 10:37:00 -0700985 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
986 skGlyph.getSubXFixed(),
987 skGlyph.getSubYFixed(),
988 GrGlyph::kCoverage_MaskStyle);
989 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -0700990 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700991 return;
992 }
993
994 int x = vx + glyph->fBounds.fLeft;
995 int y = vy + glyph->fBounds.fTop;
996
997 // keep them as ints until we've done the clip-test
998 int width = glyph->fBounds.width();
999 int height = glyph->fBounds.height();
1000
joshualitt1d89e8d2015-04-01 12:40:54 -07001001 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001002 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001003 this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy));
joshualitt1d89e8d2015-04-01 12:40:54 -07001004 return;
1005 }
1006
joshualitt1d89e8d2015-04-01 12:40:54 -07001007 GrMaskFormat format = glyph->fMaskFormat;
1008
1009 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
1010 if (run.fInitialized && subRun->fMaskFormat != format) {
joshualittd9f13ae2015-07-24 11:24:31 -07001011 subRun = &run.push_back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001012 subRun->fStrike.reset(SkRef(fCurrStrike));
1013 } else if (!run.fInitialized) {
1014 subRun->fStrike.reset(SkRef(fCurrStrike));
joshualitt1d89e8d2015-04-01 12:40:54 -07001015 }
1016
1017 run.fInitialized = true;
joshualitt1d89e8d2015-04-01 12:40:54 -07001018
joshualitta751c972015-11-20 13:37:32 -08001019 size_t vertexStride = GrAtlasTextBatch::GetVertexStride(format);
joshualitt1d89e8d2015-04-01 12:40:54 -07001020
1021 SkRect r;
1022 r.fLeft = SkIntToScalar(x);
1023 r.fTop = SkIntToScalar(y);
1024 r.fRight = r.fLeft + SkIntToScalar(width);
1025 r.fBottom = r.fTop + SkIntToScalar(height);
joshualitt9bd2daf2015-04-17 09:30:06 -07001026 subRun->fMaskFormat = format;
1027 this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format,
joshualittae32c102015-04-21 09:37:57 -07001028 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001029}
joshualitt1d89e8d2015-04-01 12:40:54 -07001030
joshualitt374b2f72015-07-21 08:05:03 -07001031bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001032 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001033 SkScalar sx, SkScalar sy, GrColor color,
1034 GrFontScaler* scaler,
joshualitt9bd2daf2015-04-17 09:30:06 -07001035 SkScalar textRatio, const SkMatrix& viewMatrix) {
joshualittae32c102015-04-21 09:37:57 -07001036 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001037 if (!fCurrStrike) {
1038 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1039 }
1040
joshualitt6c2c2b02015-07-24 10:37:00 -07001041 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1042 skGlyph.getSubXFixed(),
1043 skGlyph.getSubYFixed(),
1044 GrGlyph::kDistance_MaskStyle);
1045 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001046 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001047 return true;
1048 }
1049
1050 // fallback to color glyph support
1051 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
1052 return false;
1053 }
1054
1055 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
1056 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
1057 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
1058 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
1059
1060 SkScalar scale = textRatio;
1061 dx *= scale;
1062 dy *= scale;
1063 width *= scale;
1064 height *= scale;
1065 sx += dx;
1066 sy += dy;
1067 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
1068
joshualitt9bd2daf2015-04-17 09:30:06 -07001069 // TODO combine with the above
1070 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001071 if (glyph->fTooLargeForAtlas) {
joshualitt0fe04a22015-08-25 12:05:50 -07001072 this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -07001073 return true;
1074 }
1075
joshualitt9bd2daf2015-04-17 09:30:06 -07001076 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001077 if (!run.fInitialized) {
1078 subRun->fStrike.reset(SkRef(fCurrStrike));
1079 }
1080 run.fInitialized = true;
joshualitt9bd2daf2015-04-17 09:30:06 -07001081 SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat);
1082 subRun->fMaskFormat = kA8_GrMaskFormat;
1083
joshualitta751c972015-11-20 13:37:32 -08001084 size_t vertexStride = GrAtlasTextBatch::GetVertexStrideDf(kA8_GrMaskFormat,
1085 subRun->fUseLCDText);
joshualitt9bd2daf2015-04-17 09:30:06 -07001086
1087 bool useColorVerts = !subRun->fUseLCDText;
1088 this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts,
joshualittae32c102015-04-21 09:37:57 -07001089 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001090 return true;
1091}
1092
joshualitt374b2f72015-07-21 08:05:03 -07001093inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001094 GrFontScaler* scaler, const SkGlyph& skGlyph,
joshualitt0fe04a22015-08-25 12:05:50 -07001095 SkScalar x, SkScalar y, SkScalar scale,
1096 bool applyVM) {
halcanary96fcdcc2015-08-27 07:41:13 -07001097 if (nullptr == glyph->fPath) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001098 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
1099 if (!glyphPath) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001100 return;
1101 }
joshualitt6c2c2b02015-07-24 10:37:00 -07001102
halcanary385fe4d2015-08-26 13:07:48 -07001103 glyph->fPath = new SkPath(*glyphPath);
joshualitt9bd2daf2015-04-17 09:30:06 -07001104 }
joshualitt0fe04a22015-08-25 12:05:50 -07001105 blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
joshualitt9bd2daf2015-04-17 09:30:06 -07001106}
1107
joshualitt374b2f72015-07-21 08:05:03 -07001108inline void GrAtlasTextContext::appendGlyphCommon(GrAtlasTextBlob* blob, Run* run,
joshualitt9bd2daf2015-04-17 09:30:06 -07001109 Run::SubRunInfo* subRun,
1110 const SkRect& positions, GrColor color,
1111 size_t vertexStride, bool useVertexColor,
joshualittae32c102015-04-21 09:37:57 -07001112 GrGlyph* glyph) {
1113 blob->fGlyphs[subRun->fGlyphEndIndex] = glyph;
joshualitt9bd2daf2015-04-17 09:30:06 -07001114 run->fVertexBounds.joinNonEmptyArg(positions);
1115 run->fColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -07001116
1117 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->fVertexEndIndex);
1118
joshualitt9bd2daf2015-04-17 09:30:06 -07001119 if (useVertexColor) {
joshualitt010db532015-04-21 10:07:26 -07001120 // V0
1121 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1122 position->set(positions.fLeft, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001123 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
1124 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001125 vertex += vertexStride;
joshualitt9bd2daf2015-04-17 09:30:06 -07001126
joshualitt010db532015-04-21 10:07:26 -07001127 // V1
1128 position = reinterpret_cast<SkPoint*>(vertex);
1129 position->set(positions.fLeft, positions.fBottom);
1130 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001131 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001132 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001133
joshualitt010db532015-04-21 10:07:26 -07001134 // V2
1135 position = reinterpret_cast<SkPoint*>(vertex);
1136 position->set(positions.fRight, positions.fBottom);
1137 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001138 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001139 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001140
joshualitt010db532015-04-21 10:07:26 -07001141 // V3
1142 position = reinterpret_cast<SkPoint*>(vertex);
1143 position->set(positions.fRight, positions.fTop);
1144 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001145 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001146 } else {
1147 // V0
1148 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1149 position->set(positions.fLeft, positions.fTop);
1150 vertex += vertexStride;
1151
1152 // V1
1153 position = reinterpret_cast<SkPoint*>(vertex);
1154 position->set(positions.fLeft, positions.fBottom);
1155 vertex += vertexStride;
1156
1157 // V2
1158 position = reinterpret_cast<SkPoint*>(vertex);
1159 position->set(positions.fRight, positions.fBottom);
1160 vertex += vertexStride;
1161
1162 // V3
1163 position = reinterpret_cast<SkPoint*>(vertex);
1164 position->set(positions.fRight, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001165 }
1166
1167 subRun->fGlyphEndIndex++;
joshualitta751c972015-11-20 13:37:32 -08001168 subRun->fVertexEndIndex += vertexStride * GrAtlasTextBatch::kVerticesPerGlyph;
joshualitt1d89e8d2015-04-01 12:40:54 -07001169}
1170
robertphillips7bceedc2015-12-01 12:51:26 -08001171void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc,
herbe59124e2015-11-18 10:54:39 -08001172 const SkTextBlobRunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -07001173 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07001174 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
1175 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
1176 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07001177
joshualitt9a27e632015-04-06 10:53:36 -07001178 size_t textLen = it.glyphCount() * sizeof(uint16_t);
1179 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07001180
joshualitt9a27e632015-04-06 10:53:36 -07001181 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07001182
joshualitt9a27e632015-04-06 10:53:36 -07001183 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
1184 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07001185 }
1186
robertphillipsfcf78292015-06-19 11:49:52 -07001187 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07001188
1189 switch (it.positioning()) {
1190 case SkTextBlob::kDefault_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -08001191 this->drawTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001192 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001193 textLen, x + offset.x(), y + offset.y(), clipBounds);
1194 break;
1195 case SkTextBlob::kHorizontal_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -08001196 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001197 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001198 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
1199 clipBounds);
1200 break;
1201 case SkTextBlob::kFull_Positioning:
robertphillips7bceedc2015-12-01 12:51:26 -08001202 this->drawPosTextAsPath(dc, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001203 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001204 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
1205 break;
1206 }
1207}
1208
bsalomonabd30f52015-08-13 13:34:48 -07001209inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -07001210GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -07001211 int glyphCount, int run, int subRun,
1212 GrColor color, SkScalar transX, SkScalar transY,
1213 const SkPaint& skPaint) {
1214 GrMaskFormat format = info.fMaskFormat;
1215 GrColor subRunColor;
1216 if (kARGB_GrMaskFormat == format) {
1217 uint8_t paintAlpha = skPaint.getAlpha();
1218 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
1219 } else {
1220 subRunColor = color;
1221 }
1222
joshualitta751c972015-11-20 13:37:32 -08001223 GrAtlasTextBatch* batch;
joshualitt79dfb2b2015-05-11 08:58:08 -07001224 if (info.fDrawAsDistanceFields) {
1225 SkColor filteredColor;
1226 SkColorFilter* colorFilter = skPaint.getColorFilter();
1227 if (colorFilter) {
1228 filteredColor = colorFilter->filterColor(skPaint.getColor());
1229 } else {
1230 filteredColor = skPaint.getColor();
1231 }
robertphillipsfcf78292015-06-19 11:49:52 -07001232 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
joshualitta751c972015-11-20 13:37:32 -08001233 batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
1234 fDistanceAdjustTable, filteredColor,
1235 info.fUseLCDText, useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07001236 } else {
joshualitta751c972015-11-20 13:37:32 -08001237 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07001238 }
joshualitta751c972015-11-20 13:37:32 -08001239 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07001240 geometry.fBlob = SkRef(cacheBlob);
1241 geometry.fRun = run;
1242 geometry.fSubRun = subRun;
1243 geometry.fColor = subRunColor;
1244 geometry.fTransX = transX;
1245 geometry.fTransY = transY;
1246 batch->init();
1247
1248 return batch;
1249}
1250
robertphillipsf6703fa2015-09-01 05:36:47 -07001251inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07001252 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07001253 SkScalar transX, SkScalar transY,
1254 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07001255 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
1256 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
1257 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1258 if (0 == glyphCount) {
1259 continue;
1260 }
1261
bsalomonabd30f52015-08-13 13:34:48 -07001262 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
1263 subRun, color, transX, transY,
1264 skPaint));
robertphillipsf6703fa2015-09-01 05:36:47 -07001265 dc->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07001266 }
1267}
1268
herbe59124e2015-11-18 10:54:39 -08001269inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob,
robertphillips7bceedc2015-12-01 12:51:26 -08001270 GrDrawContext* dc,
robertphillipsccb1b572015-05-27 11:02:55 -07001271 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07001272 SkScalar transX, SkScalar transY,
1273 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07001274 if (!cacheBlob->fBigGlyphs.count()) {
1275 return;
1276 }
1277
joshualitt9a27e632015-04-06 10:53:36 -07001278 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07001279 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07001280 bigGlyph.fVx += transX;
1281 bigGlyph.fVy += transY;
joshualitt0fe04a22015-08-25 12:05:50 -07001282 SkMatrix ctm;
1283 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
1284 ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
1285 if (bigGlyph.fApplyVM) {
1286 ctm.postConcat(cacheBlob->fViewMatrix);
1287 }
joshualittfc072562015-05-13 12:15:06 -07001288
robertphillips7bceedc2015-12-01 12:51:26 -08001289 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, clip, bigGlyph.fPath,
joshualitt0fe04a22015-08-25 12:05:50 -07001290 skPaint, ctm, nullptr, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07001291 }
1292}
joshualitt9a27e632015-04-06 10:53:36 -07001293
robertphillips2334fb62015-06-17 05:43:33 -07001294void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -07001295 GrAtlasTextBlob* cacheBlob,
herbe59124e2015-11-18 10:54:39 -08001296 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07001297 GrRenderTarget* rt,
1298 const SkPaint& skPaint,
1299 const GrPaint& grPaint,
1300 SkDrawFilter* drawFilter,
1301 const GrClip& clip,
1302 const SkMatrix& viewMatrix,
1303 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07001304 SkScalar x, SkScalar y,
1305 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07001306 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
1307 // it as paths
joshualitt7b670db2015-07-09 13:25:02 -07001308 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07001309
1310 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001311
halcanary33779752015-10-27 14:01:05 -07001312 SkTextBlobRunIterator it(blob);
joshualitt9a27e632015-04-06 10:53:36 -07001313 for (int run = 0; !it.done(); it.next(), run++) {
1314 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillips7bceedc2015-12-01 12:51:26 -08001315 this->flushRunAsPaths(dc, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07001316 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07001317 continue;
1318 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001319 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillipsf6703fa2015-09-01 05:36:47 -07001320 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07001321 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001322 }
1323
1324 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001325 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001326}
1327
joshualitt374b2f72015-07-21 08:05:03 -07001328void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07001329 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07001330 GrRenderTarget* rt,
1331 const SkPaint& skPaint,
1332 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07001333 const GrClip& clip,
1334 const SkIRect& clipBounds) {
joshualitt7b670db2015-07-09 13:25:02 -07001335 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07001336
1337 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07001338 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillipsf6703fa2015-09-01 05:36:47 -07001339 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07001340 }
1341
1342 // Now flush big glyphs
robertphillips7bceedc2015-12-01 12:51:26 -08001343 this->flushBigGlyphs(cacheBlob, dc, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07001344}
joshualitt79dfb2b2015-05-11 08:58:08 -07001345
1346///////////////////////////////////////////////////////////////////////////////////////////////////
1347
1348#ifdef GR_TEST_UTILS
1349
bsalomonabd30f52015-08-13 13:34:48 -07001350DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001351 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -07001352 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -07001353 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07001354
1355 if (context->uniqueID() != gContextID) {
1356 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -07001357 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -07001358
joshualitt79dfb2b2015-05-11 08:58:08 -07001359 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
1360 // because we don't really want to have a gpu device here.
1361 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -07001362 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07001363 }
1364
joshualitt79dfb2b2015-05-11 08:58:08 -07001365 // Setup dummy SkPaint / GrPaint
1366 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07001367 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07001368 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07001369 skPaint.setColor(color);
1370 skPaint.setLCDRenderText(random->nextBool());
1371 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
1372 skPaint.setSubpixelText(random->nextBool());
1373
1374 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001375 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -07001376 SkFAIL("couldn't convert paint\n");
1377 }
1378
1379 const char* text = "The quick brown fox jumps over the lazy dog.";
1380 int textLen = (int)strlen(text);
1381
1382 // Setup clip
1383 GrClip clip;
1384 SkIRect noClip = SkIRect::MakeLargest();
1385
1386 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
1387 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07001388 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips6ee690e2015-12-02 08:57:50 -08001389 gTextContext->createDrawTextBlob(clip, grPaint, skPaint, viewMatrix, text,
joshualitt79dfb2b2015-05-11 08:58:08 -07001390 static_cast<size_t>(textLen), 0, 0, noClip));
1391
1392 SkScalar transX = static_cast<SkScalar>(random->nextU());
1393 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07001394 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07001395 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
1396}
1397
1398#endif