blob: 37fe092496d0f4521d4f8ce77a36544ac880072d [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
joshualitt1d89e8d2015-04-01 12:40:54 -07009#include "GrBatchFontCache.h"
bsalomon75398562015-08-17 12:55:38 -070010#include "GrBatchFlushState.h"
joshualitt79dfb2b2015-05-11 08:58:08 -070011#include "GrBatchTest.h"
robertphillipsccb1b572015-05-27 11:02:55 -070012#include "GrBlurUtils.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070013#include "GrDefaultGeoProcFactory.h"
robertphillipsea461502015-05-26 11:38:03 -070014#include "GrDrawContext.h"
robertphillips2334fb62015-06-17 05:43:33 -070015#include "GrDrawTarget.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070016#include "GrFontScaler.h"
bsalomoned0bcad2015-05-04 10:36:42 -070017#include "GrResourceProvider.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070018#include "GrStrokeInfo.h"
joshualittb7133be2015-04-08 09:08:31 -070019#include "GrTextBlobCache.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070020#include "GrTexturePriv.h"
bsalomon72e3ae42015-04-28 08:08:46 -070021#include "GrVertexBuffer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070022
23#include "SkAutoKern.h"
24#include "SkColorPriv.h"
joshualitt9bd2daf2015-04-17 09:30:06 -070025#include "SkColorFilter.h"
26#include "SkDistanceFieldGen.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070027#include "SkDraw.h"
28#include "SkDrawFilter.h"
29#include "SkDrawProcs.h"
herb9be5ff62015-11-11 11:30:11 -080030#include "SkFindAndPlaceGlyph.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070031#include "SkGlyphCache.h"
32#include "SkGpuDevice.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070033#include "SkGrPriv.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070034#include "SkPath.h"
35#include "SkRTConf.h"
36#include "SkStrokeRec.h"
37#include "SkTextBlob.h"
38#include "SkTextMapStateProc.h"
39
bsalomon16b99132015-08-13 14:55:50 -070040#include "batches/GrVertexBatch.h"
joshualitt74417822015-08-07 11:42:16 -070041
joshualitt1d89e8d2015-04-01 12:40:54 -070042#include "effects/GrBitmapTextGeoProc.h"
joshualitt9bd2daf2015-04-17 09:30:06 -070043#include "effects/GrDistanceFieldGeoProc.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070044
45namespace {
46static const size_t kLCDTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
47
48// position + local coord
49static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
50
51static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
52
joshualitt9bd2daf2015-04-17 09:30:06 -070053static const int kMinDFFontSize = 18;
54static const int kSmallDFFontSize = 32;
55static const int kSmallDFFontLimit = 32;
56static const int kMediumDFFontSize = 72;
57static const int kMediumDFFontLimit = 72;
58static const int kLargeDFFontSize = 162;
jvanverth97c595f2015-06-19 11:06:28 -070059#ifdef SK_BUILD_FOR_ANDROID
60static const int kLargeDFFontLimit = 384;
61#else
joshualitta7c63892015-04-21 13:24:37 -070062static const int kLargeDFFontLimit = 2 * kLargeDFFontSize;
jvanverth97c595f2015-06-19 11:06:28 -070063#endif
joshualitt9bd2daf2015-04-17 09:30:06 -070064
65SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize = 8;)
66static const int kDistanceAdjustLumShift = 5;
67
joshualitt1d89e8d2015-04-01 12:40:54 -070068static const int kVerticesPerGlyph = 4;
69static const int kIndicesPerGlyph = 6;
70
71static size_t get_vertex_stride(GrMaskFormat maskFormat) {
72 switch (maskFormat) {
73 case kA8_GrMaskFormat:
74 return kGrayTextVASize;
75 case kARGB_GrMaskFormat:
76 return kColorTextVASize;
77 default:
78 return kLCDTextVASize;
79 }
80}
81
joshualitt9bd2daf2015-04-17 09:30:06 -070082static size_t get_vertex_stride_df(GrMaskFormat maskFormat, bool useLCDText) {
83 SkASSERT(maskFormat == kA8_GrMaskFormat);
84 if (useLCDText) {
85 return kLCDTextVASize;
86 } else {
87 return kGrayTextVASize;
88 }
89}
90
91static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
92 unsigned r = SkColorGetR(c);
93 unsigned g = SkColorGetG(c);
94 unsigned b = SkColorGetB(c);
95 return GrColorPackRGBA(r, g, b, 0xff);
96}
97
joshualitt1d89e8d2015-04-01 12:40:54 -070098};
99
robertphillipsf6703fa2015-09-01 05:36:47 -0700100GrAtlasTextContext::GrAtlasTextContext(GrContext* context, const SkSurfaceProps& surfaceProps)
101 : INHERITED(context, surfaceProps)
102 , fDistanceAdjustTable(new DistanceAdjustTable) {
joshualittb7133be2015-04-08 09:08:31 -0700103 // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest
104 // vertexStride
bungeman99fe8222015-08-20 07:57:51 -0700105 static_assert(kGrayTextVASize >= kColorTextVASize && kGrayTextVASize >= kLCDTextVASize,
106 "vertex_attribute_changed");
halcanary96fcdcc2015-08-27 07:41:13 -0700107 fCurrStrike = nullptr;
joshualittb7133be2015-04-08 09:08:31 -0700108 fCache = context->getTextBlobCache();
joshualitt9bd2daf2015-04-17 09:30:06 -0700109}
110
robertphillips9fc82752015-06-19 04:46:45 -0700111void GrAtlasTextContext::DistanceAdjustTable::buildDistanceAdjustTable() {
joshualitt9bd2daf2015-04-17 09:30:06 -0700112
113 // This is used for an approximation of the mask gamma hack, used by raster and bitmap
114 // text. The mask gamma hack is based off of guessing what the blend color is going to
115 // be, and adjusting the mask so that when run through the linear blend will
116 // produce the value closest to the desired result. However, in practice this means
117 // that the 'adjusted' mask is just increasing or decreasing the coverage of
118 // the mask depending on what it is thought it will blit against. For black (on
119 // assumed white) this means that coverages are decreased (on a curve). For white (on
120 // assumed black) this means that coverages are increased (on a a curve). At
121 // middle (perceptual) gray (which could be blit against anything) the coverages
122 // remain the same.
123 //
124 // The idea here is that instead of determining the initial (real) coverage and
125 // then adjusting that coverage, we determine an adjusted coverage directly by
126 // essentially manipulating the geometry (in this case, the distance to the glyph
127 // edge). So for black (on assumed white) this thins a bit; for white (on
128 // assumed black) this fake bolds the geometry a bit.
129 //
130 // The distance adjustment is calculated by determining the actual coverage value which
131 // when fed into in the mask gamma table gives us an 'adjusted coverage' value of 0.5. This
132 // actual coverage value (assuming it's between 0 and 1) corresponds to a distance from the
133 // actual edge. So by subtracting this distance adjustment and computing without the
134 // the coverage adjustment we should get 0.5 coverage at the same point.
135 //
136 // This has several implications:
137 // For non-gray lcd smoothed text, each subpixel essentially is using a
138 // slightly different geometry.
139 //
140 // For black (on assumed white) this may not cover some pixels which were
141 // previously covered; however those pixels would have been only slightly
142 // covered and that slight coverage would have been decreased anyway. Also, some pixels
143 // which were previously fully covered may no longer be fully covered.
144 //
145 // For white (on assumed black) this may cover some pixels which weren't
146 // previously covered at all.
147
148 int width, height;
149 size_t size;
150
151#ifdef SK_GAMMA_CONTRAST
152 SkScalar contrast = SK_GAMMA_CONTRAST;
153#else
154 SkScalar contrast = 0.5f;
155#endif
robertphillips9fc82752015-06-19 04:46:45 -0700156 SkScalar paintGamma = SK_GAMMA_EXPONENT;
157 SkScalar deviceGamma = SK_GAMMA_EXPONENT;
joshualitt9bd2daf2015-04-17 09:30:06 -0700158
159 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
160 &width, &height);
161
162 SkASSERT(kExpectedDistanceAdjustTableSize == height);
halcanary385fe4d2015-08-26 13:07:48 -0700163 fTable = new SkScalar[height];
joshualitt9bd2daf2015-04-17 09:30:06 -0700164
165 SkAutoTArray<uint8_t> data((int)size);
166 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
167
168 // find the inverse points where we cross 0.5
169 // binsearch might be better, but we only need to do this once on creation
170 for (int row = 0; row < height; ++row) {
171 uint8_t* rowPtr = data.get() + row*width;
172 for (int col = 0; col < width - 1; ++col) {
173 if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) {
174 // compute point where a mask value will give us a result of 0.5
175 float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPtr[col]);
176 float borderAlpha = (col + interp) / 255.f;
177
178 // compute t value for that alpha
179 // this is an approximate inverse for smoothstep()
180 float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5.0f) / 3.0f;
181
182 // compute distance which gives us that t value
183 const float kDistanceFieldAAFactor = 0.65f; // should match SK_DistanceFieldAAFactor
184 float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor;
185
186 fTable[row] = d;
187 break;
188 }
189 }
190 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700191}
192
joshualittdbd35932015-04-02 09:19:04 -0700193GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context,
robertphillipsfcf78292015-06-19 11:49:52 -0700194 const SkSurfaceProps& surfaceProps) {
robertphillipsf6703fa2015-09-01 05:36:47 -0700195 return new GrAtlasTextContext(context, surfaceProps);
joshualitt1d89e8d2015-04-01 12:40:54 -0700196}
197
joshualittdbd35932015-04-02 09:19:04 -0700198bool GrAtlasTextContext::canDraw(const GrRenderTarget*,
199 const GrClip&,
200 const GrPaint&,
201 const SkPaint& skPaint,
202 const SkMatrix& viewMatrix) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700203 return this->canDrawAsDistanceFields(skPaint, viewMatrix) ||
204 !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -0700205}
206
joshualitt9e36c1a2015-04-14 12:17:27 -0700207GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
208 GrColor canonicalColor = paint.computeLuminanceColor();
209 if (lcd) {
210 // This is the correct computation, but there are tons of cases where LCD can be overridden.
211 // For now we just regenerate if any run in a textblob has LCD.
212 // TODO figure out where all of these overrides are and see if we can incorporate that logic
213 // at a higher level *OR* use sRGB
214 SkASSERT(false);
215 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
216 } else {
217 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
218 // gamma corrected masks anyways, nor color
219 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
220 SkColorGetG(canonicalColor),
221 SkColorGetB(canonicalColor));
222 // reduce to our finite number of bits
223 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
224 }
225 return canonicalColor;
226}
227
228// TODO if this function ever shows up in profiling, then we can compute this value when the
229// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
230// run so this is not a big deal to compute here.
231bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -0700232 SkTextBlobRunIterator it(blob);
joshualitt9e36c1a2015-04-14 12:17:27 -0700233 for (; !it.done(); it.next()) {
234 if (it.isLCD()) {
235 return true;
236 }
237 }
238 return false;
239}
240
joshualitt2a0e9f32015-04-13 06:12:21 -0700241bool GrAtlasTextContext::MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY,
joshualitt374b2f72015-07-21 08:05:03 -0700242 const GrAtlasTextBlob& blob, const SkPaint& paint,
jvanverth0628a522015-08-18 07:44:22 -0700243 GrColor color, const SkMaskFilter::BlurRec& blurRec,
joshualittdbd35932015-04-02 09:19:04 -0700244 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700245 // If we have LCD text then our canonical color will be set to transparent, in this case we have
246 // to regenerate the blob on any color change
jvanverth0628a522015-08-18 07:44:22 -0700247 // We use the grPaint to get any color filter effects
248 if (blob.fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
249 blob.fPaintColor != color) {
joshualitt2a0e9f32015-04-13 06:12:21 -0700250 return true;
251 }
252
253 if (blob.fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
254 return true;
255 }
256
257 if (blob.fViewMatrix.hasPerspective() && !blob.fViewMatrix.cheapEqualTo(viewMatrix)) {
258 return true;
259 }
260
joshualitt53b5f442015-04-13 06:33:59 -0700261 // We only cache one masked version
262 if (blob.fKey.fHasBlur &&
263 (blob.fBlurRec.fSigma != blurRec.fSigma ||
264 blob.fBlurRec.fStyle != blurRec.fStyle ||
265 blob.fBlurRec.fQuality != blurRec.fQuality)) {
266 return true;
267 }
268
269 // Similarly, we only cache one version for each style
270 if (blob.fKey.fStyle != SkPaint::kFill_Style &&
271 (blob.fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
272 blob.fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
273 blob.fStrokeInfo.fJoin != paint.getStrokeJoin())) {
274 return true;
275 }
276
joshualittfcfb9fc2015-04-21 07:35:10 -0700277 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
278 // for mixed blobs if this becomes an issue.
279 if (blob.hasBitmap() && blob.hasDistanceField()) {
joshualitt473ffa12015-04-22 18:23:15 -0700280 // Identical viewmatrices and we can reuse in all cases
281 if (blob.fViewMatrix.cheapEqualTo(viewMatrix) && x == blob.fX && y == blob.fY) {
282 return false;
283 }
joshualitt2a0e9f32015-04-13 06:12:21 -0700284 return true;
285 }
286
joshualittfcfb9fc2015-04-21 07:35:10 -0700287 if (blob.hasBitmap()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700288 if (blob.fViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
289 blob.fViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
290 blob.fViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
291 blob.fViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
292 return true;
293 }
294
joshualittfcfb9fc2015-04-21 07:35:10 -0700295 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
296 // but only for integer translations.
297 // This cool bit of math will determine the necessary translation to apply to the already
298 // generated vertex coordinates to move them to the correct position
299 SkScalar transX = viewMatrix.getTranslateX() +
300 viewMatrix.getScaleX() * (x - blob.fX) +
301 viewMatrix.getSkewX() * (y - blob.fY) -
302 blob.fViewMatrix.getTranslateX();
303 SkScalar transY = viewMatrix.getTranslateY() +
304 viewMatrix.getSkewY() * (x - blob.fX) +
305 viewMatrix.getScaleY() * (y - blob.fY) -
306 blob.fViewMatrix.getTranslateY();
joshualittf0c000d2015-04-27 09:36:55 -0700307 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700308 return true;
309 }
310
joshualittfcfb9fc2015-04-21 07:35:10 -0700311 (*outTransX) = transX;
312 (*outTransY) = transY;
joshualitta7c63892015-04-21 13:24:37 -0700313 } else if (blob.hasDistanceField()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700314 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
315 // distance field being generated, so we have to regenerate in those cases
316 SkScalar newMaxScale = viewMatrix.getMaxScale();
317 SkScalar oldMaxScale = blob.fViewMatrix.getMaxScale();
318 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
319 if (scaleAdjust < blob.fMaxMinScale || scaleAdjust > blob.fMinMaxScale) {
320 return true;
321 }
322
323 (*outTransX) = x - blob.fX;
324 (*outTransY) = y - blob.fY;
joshualittfcfb9fc2015-04-21 07:35:10 -0700325 }
joshualitt374b2f72015-07-21 08:05:03 -0700326
joshualitta7c63892015-04-21 13:24:37 -0700327 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
328 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
329 // the blob anyways at flush time, so no need to regenerate explicitly
joshualitt2a0e9f32015-04-13 06:12:21 -0700330 return false;
joshualitt1d89e8d2015-04-01 12:40:54 -0700331}
332
333
joshualitt374b2f72015-07-21 08:05:03 -0700334inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run,
joshualittdbd35932015-04-02 09:19:04 -0700335 const SkPaint& skPaint,
joshualitt9bd2daf2015-04-17 09:30:06 -0700336 const SkMatrix* viewMatrix,
337 bool noGamma) {
robertphillipsfcf78292015-06-19 11:49:52 -0700338 skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma);
joshualitt1d89e8d2015-04-01 12:40:54 -0700339 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
340 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc());
341}
342
robertphillipsf6703fa2015-09-01 05:36:47 -0700343void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700344 const GrClip& clip, const SkPaint& skPaint,
345 const SkMatrix& viewMatrix, const SkTextBlob* blob,
346 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700347 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700348 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700349 if (fContext->abandoned()) {
350 return;
351 }
352
joshualitt374b2f72015-07-21 08:05:03 -0700353 SkAutoTUnref<GrAtlasTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700354 SkMaskFilter::BlurRec blurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700355 GrAtlasTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700356 // It might be worth caching these things, but its not clear at this time
357 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
358 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700359 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700360 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700361 drawFilter);
362
363 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700364 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700365
366 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
robertphillipsfcf78292015-06-19 11:49:52 -0700367 SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700368 kUnknown_SkPixelGeometry;
369
joshualitt9e36c1a2015-04-14 12:17:27 -0700370 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
371 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
372 // ensure we always match the same key
373 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
374 ComputeCanonicalColor(skPaint, hasLCD);
375
joshualitte4cee1f2015-05-11 13:04:28 -0700376 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700377 key.fUniqueID = blob->uniqueID();
378 key.fStyle = skPaint.getStyle();
379 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700380 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700381 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700382 }
383
joshualitt1d89e8d2015-04-01 12:40:54 -0700384 SkIRect clipRect;
385 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
386
joshualitt2a0e9f32015-04-13 06:12:21 -0700387 SkScalar transX = 0.f;
388 SkScalar transY = 0.f;
389
joshualitt9e36c1a2015-04-14 12:17:27 -0700390 // Though for the time being runs in the textblob can override the paint, they only touch font
391 // info.
392 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700393 if (!SkPaintToGrPaint(fContext, skPaint, viewMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700394 return;
395 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700396
joshualittb7133be2015-04-08 09:08:31 -0700397 if (cacheBlob) {
jvanverth0628a522015-08-18 07:44:22 -0700398 if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, grPaint.getColor(), blurRec,
399 viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700400 // We have to remake the blob because changes may invalidate our masks.
401 // TODO we could probably get away reuse most of the time if the pointer is unique,
402 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700403 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700404 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
405 kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700406 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700407 blob, x, y, drawFilter, clipRect, rt, clip);
joshualittb7133be2015-04-08 09:08:31 -0700408 } else {
joshualitt9e36c1a2015-04-14 12:17:27 -0700409 // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y
joshualitt7e7b5c52015-07-21 12:56:56 -0700410 // offsets. Note, we offset the vertex bounds right before flushing
joshualitt2a0e9f32015-04-13 06:12:21 -0700411 cacheBlob->fViewMatrix = viewMatrix;
412 cacheBlob->fX = x;
413 cacheBlob->fY = y;
joshualittb7133be2015-04-08 09:08:31 -0700414 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700415#ifdef CACHE_SANITY_CHECK
416 {
417 int glyphCount = 0;
418 int runCount = 0;
419 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
420 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
421 kGrayTextVASize));
422 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
423 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700424 blob, x, y, drawFilter, clipRect, rt, clip);
joshualitt259fbf12015-07-21 11:39:34 -0700425 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
426 }
427
428#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700429 }
430 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700431 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700432 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
433 kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700434 } else {
435 cacheBlob.reset(fCache->createBlob(blob, kGrayTextVASize));
436 }
robertphillips9c240a12015-05-28 07:45:59 -0700437 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700438 blob, x, y, drawFilter, clipRect, rt, clip);
joshualitt1d89e8d2015-04-01 12:40:54 -0700439 }
440
robertphillipsf6703fa2015-09-01 05:36:47 -0700441 this->flush(blob, cacheBlob, dc, rt, skPaint, grPaint, drawFilter,
joshualitt2a0e9f32015-04-13 06:12:21 -0700442 clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700443}
444
joshualitt9bd2daf2015-04-17 09:30:06 -0700445inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
446 const SkMatrix& viewMatrix) {
447 // TODO: support perspective (need getMaxScale replacement)
448 if (viewMatrix.hasPerspective()) {
449 return false;
450 }
451
452 SkScalar maxScale = viewMatrix.getMaxScale();
453 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
454 // Hinted text looks far better at small resolutions
455 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700456 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700457 return false;
458 }
459
bsalomonafcd7cd2015-08-31 12:39:41 -0700460 bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700461#if SK_FORCE_DISTANCE_FIELD_TEXT
462 useDFT = true;
463#endif
464
jvanverth4854d132015-06-22 06:46:56 -0700465 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700466 return false;
467 }
468
469 // rasterizers and mask filters modify alpha, which doesn't
470 // translate well to distance
471 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700472 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700473 return false;
474 }
475
476 // TODO: add some stroking support
477 if (skPaint.getStyle() != SkPaint::kFill_Style) {
478 return false;
479 }
480
481 return true;
482}
483
joshualitt374b2f72015-07-21 08:05:03 -0700484void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700485 const SkPaint& skPaint, GrColor color,
486 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700487 const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualittfcfb9fc2015-04-21 07:35:10 -0700488 SkDrawFilter* drawFilter, const SkIRect& clipRect,
jvanverth0628a522015-08-18 07:44:22 -0700489 GrRenderTarget* rt, const GrClip& clip) {
herbe59124e2015-11-18 10:54:39 -0800490 // The color here is the GrPaint color, and it is used to determine whether we
jvanverth0628a522015-08-18 07:44:22 -0700491 // have to regenerate LCD text blobs.
492 // We use this color vs the SkPaint color because it has the colorfilter applied.
493 cacheBlob->fPaintColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -0700494 cacheBlob->fViewMatrix = viewMatrix;
495 cacheBlob->fX = x;
496 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700497
498 // Regenerate textblob
499 SkPaint runPaint = skPaint;
halcanary33779752015-10-27 14:01:05 -0700500 SkTextBlobRunIterator it(blob);
joshualitt1d89e8d2015-04-01 12:40:54 -0700501 for (int run = 0; !it.done(); it.next(), run++) {
502 int glyphCount = it.glyphCount();
503 size_t textLen = glyphCount * sizeof(uint16_t);
504 const SkPoint& offset = it.offset();
505 // applyFontToPaint() always overwrites the exact same attributes,
506 // so it is safe to not re-seed the paint for this reason.
507 it.applyFontToPaint(&runPaint);
508
509 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
510 // A false return from filter() means we should abort the current draw.
511 runPaint = skPaint;
512 continue;
513 }
514
robertphillipsfcf78292015-06-19 11:49:52 -0700515 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700516
joshualitt1d89e8d2015-04-01 12:40:54 -0700517 // setup vertex / glyphIndex for the new run
518 if (run > 0) {
519 PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back();
520 PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back();
521
522 newRun.fVertexStartIndex = lastRun.fVertexEndIndex;
523 newRun.fVertexEndIndex = lastRun.fVertexEndIndex;
524
525 newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex;
526 newRun.fGlyphEndIndex = lastRun.fGlyphEndIndex;
527 }
528
joshualittfcfb9fc2015-04-21 07:35:10 -0700529 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
530 cacheBlob->setHasDistanceField();
531 SkPaint dfPaint = runPaint;
532 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700533 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700534 Run& runIdx = cacheBlob->fRuns[run];
535 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
536 subRun.fUseLCDText = runPaint.isLCDRenderText();
537 subRun.fDrawAsDistanceFields = true;
joshualitt9a27e632015-04-06 10:53:36 -0700538
joshualittfcfb9fc2015-04-21 07:35:10 -0700539 SkTDArray<char> fallbackTxt;
540 SkTDArray<SkScalar> fallbackPos;
541 SkPoint dfOffset;
542 int scalarsPerPosition = 2;
543 switch (it.positioning()) {
544 case SkTextBlob::kDefault_Positioning: {
jvanverth157e6482015-09-09 08:05:12 -0700545 this->internalDrawDFText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700546 (const char *)it.glyphs(), textLen,
547 x + offset.x(), y + offset.y(), clipRect, textRatio,
548 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
549 break;
550 }
551 case SkTextBlob::kHorizontal_Positioning: {
552 scalarsPerPosition = 1;
553 dfOffset = SkPoint::Make(x, y + offset.y());
jvanverth157e6482015-09-09 08:05:12 -0700554 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700555 (const char*)it.glyphs(), textLen, it.pos(),
556 scalarsPerPosition, dfOffset, clipRect, textRatio,
557 &fallbackTxt, &fallbackPos);
558 break;
559 }
560 case SkTextBlob::kFull_Positioning: {
561 dfOffset = SkPoint::Make(x, y);
jvanverth157e6482015-09-09 08:05:12 -0700562 this->internalDrawDFPosText(cacheBlob, run, dfPaint, color, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700563 (const char*)it.glyphs(), textLen, it.pos(),
564 scalarsPerPosition, dfOffset, clipRect, textRatio,
565 &fallbackTxt, &fallbackPos);
566 break;
567 }
568 }
569 if (fallbackTxt.count()) {
jvanverth0628a522015-08-18 07:44:22 -0700570 this->fallbackDrawPosText(cacheBlob, run, rt, clip, color, runPaint, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700571 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset,
572 clipRect);
573 }
joshualittfcfb9fc2015-04-21 07:35:10 -0700574 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
575 cacheBlob->fRuns[run].fDrawAsPaths = true;
576 } else {
577 cacheBlob->setHasBitmap();
578 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
579 false);
580 switch (it.positioning()) {
581 case SkTextBlob::kDefault_Positioning:
582 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
583 (const char *)it.glyphs(), textLen,
584 x + offset.x(), y + offset.y(), clipRect);
585 break;
586 case SkTextBlob::kHorizontal_Positioning:
587 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
588 (const char*)it.glyphs(), textLen, it.pos(), 1,
589 SkPoint::Make(x, y + offset.y()), clipRect);
590 break;
591 case SkTextBlob::kFull_Positioning:
592 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
593 (const char*)it.glyphs(), textLen, it.pos(), 2,
594 SkPoint::Make(x, y), clipRect);
595 break;
596 }
597 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700598 }
599
600 if (drawFilter) {
601 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
602 runPaint = skPaint;
603 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700604 }
605}
606
joshualitt374b2f72015-07-21 08:05:03 -0700607inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700608 SkPaint* skPaint,
609 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700610 const SkMatrix& viewMatrix) {
611 // getMaxScale doesn't support perspective, so neither do we at the moment
612 SkASSERT(!viewMatrix.hasPerspective());
613 SkScalar maxScale = viewMatrix.getMaxScale();
614 SkScalar textSize = skPaint->getTextSize();
615 SkScalar scaledTextSize = textSize;
616 // if we have non-unity scale, we need to choose our base text size
617 // based on the SkPaint's text size multiplied by the max scale factor
618 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
619 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
620 scaledTextSize *= maxScale;
621 }
622
joshualitt64c99cc2015-04-21 09:43:03 -0700623 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
624 // and ceiling. A scale outside of this range would require regenerating the distance fields
625 SkScalar dfMaskScaleFloor;
626 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700627 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700628 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700629 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700630 *textRatio = textSize / kSmallDFFontSize;
631 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
632 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700633 dfMaskScaleFloor = kSmallDFFontLimit;
634 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700635 *textRatio = textSize / kMediumDFFontSize;
636 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
637 } else {
joshualitta7c63892015-04-21 13:24:37 -0700638 dfMaskScaleFloor = kMediumDFFontLimit;
639 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700640 *textRatio = textSize / kLargeDFFontSize;
641 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
642 }
643
joshualitt64c99cc2015-04-21 09:43:03 -0700644 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
645 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
646 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
647 // tolerate before we'd have to move to a large mip size. When we actually test these values
648 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
649 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
650 // level)
joshualitta7c63892015-04-21 13:24:37 -0700651 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700652 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
653 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
654
joshualitt9bd2daf2015-04-17 09:30:06 -0700655 skPaint->setLCDRenderText(false);
656 skPaint->setAutohinted(false);
657 skPaint->setHinting(SkPaint::kNormal_Hinting);
658 skPaint->setSubpixelText(true);
659}
660
joshualitt374b2f72015-07-21 08:05:03 -0700661inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700662 int runIndex,
joshualittfec19e12015-04-17 10:32:32 -0700663 GrRenderTarget* rt, const GrClip& clip,
jvanverth0628a522015-08-18 07:44:22 -0700664 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700665 const SkPaint& skPaint,
666 const SkMatrix& viewMatrix,
667 const SkTDArray<char>& fallbackTxt,
668 const SkTDArray<SkScalar>& fallbackPos,
669 int scalarsPerPosition,
670 const SkPoint& offset,
671 const SkIRect& clipRect) {
joshualittfec19e12015-04-17 10:32:32 -0700672 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700673 blob->setHasBitmap();
674 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700675 // Push back a new subrun to fill and set the override descriptor
676 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700677 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitt97202d22015-04-22 13:47:02 -0700678 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700679 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700680 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700681 run.fOverrideDescriptor->getDesc());
jvanverth0628a522015-08-18 07:44:22 -0700682 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700683 fallbackTxt.begin(), fallbackTxt.count(),
684 fallbackPos.begin(), scalarsPerPosition, offset, clipRect);
685 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700686}
687
joshualitt374b2f72015-07-21 08:05:03 -0700688inline GrAtlasTextBlob*
herbe59124e2015-11-18 10:54:39 -0800689GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
690 const SkMatrix& viewMatrix, SkPaint* dfPaint,
jvanverth157e6482015-09-09 08:05:12 -0700691 SkScalar* textRatio) {
joshualitt374b2f72015-07-21 08:05:03 -0700692 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700693
694 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700695 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700696 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700697 Run& run = blob->fRuns[0];
698 PerSubRunInfo& subRun = run.fSubRunInfo.back();
699 subRun.fUseLCDText = origPaint.isLCDRenderText();
700 subRun.fDrawAsDistanceFields = true;
joshualitt9bd2daf2015-04-17 09:30:06 -0700701
joshualitt9bd2daf2015-04-17 09:30:06 -0700702 return blob;
703}
704
joshualitt374b2f72015-07-21 08:05:03 -0700705inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700706GrAtlasTextContext::createDrawTextBlob(GrRenderTarget* rt, const GrClip& clip,
707 const GrPaint& paint, const SkPaint& skPaint,
708 const SkMatrix& viewMatrix,
709 const char text[], size_t byteLength,
710 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700711 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700712 SkIRect clipRect;
713 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
714
joshualitt374b2f72015-07-21 08:05:03 -0700715 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700716 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
717 SkPaint dfPaint;
718 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700719 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700720
joshualitt9bd2daf2015-04-17 09:30:06 -0700721 SkTDArray<char> fallbackTxt;
722 SkTDArray<SkScalar> fallbackPos;
723 SkPoint offset;
jvanverth157e6482015-09-09 08:05:12 -0700724 this->internalDrawDFText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
joshualitt9bd2daf2015-04-17 09:30:06 -0700725 byteLength, x, y, clipRect, textRatio, &fallbackTxt, &fallbackPos,
726 &offset, skPaint);
joshualitt9bd2daf2015-04-17 09:30:06 -0700727 if (fallbackTxt.count()) {
herbe59124e2015-11-18 10:54:39 -0800728 this->fallbackDrawPosText(blob, 0, rt, clip, paint.getColor(), skPaint, viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700729 fallbackTxt, fallbackPos, 2, offset, clipRect);
joshualitt9bd2daf2015-04-17 09:30:06 -0700730 }
731 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700732 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700733 blob->fViewMatrix = viewMatrix;
734
735 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
736 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
737 byteLength, x, y, clipRect);
738 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700739 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700740 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700741}
742
joshualitt374b2f72015-07-21 08:05:03 -0700743inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700744GrAtlasTextContext::createDrawPosTextBlob(GrRenderTarget* rt, const GrClip& clip,
745 const GrPaint& paint, const SkPaint& skPaint,
746 const SkMatrix& viewMatrix,
747 const char text[], size_t byteLength,
748 const SkScalar pos[], int scalarsPerPosition,
749 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700750 int glyphCount = skPaint.countText(text, byteLength);
751
752 SkIRect clipRect;
753 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
754
joshualitt374b2f72015-07-21 08:05:03 -0700755 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700756 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
757 SkPaint dfPaint;
758 SkScalar textRatio;
jvanverth157e6482015-09-09 08:05:12 -0700759 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700760
761 SkTDArray<char> fallbackTxt;
762 SkTDArray<SkScalar> fallbackPos;
jvanverth157e6482015-09-09 08:05:12 -0700763 this->internalDrawDFPosText(blob, 0, dfPaint, paint.getColor(), viewMatrix, text,
joshualitt9bd2daf2015-04-17 09:30:06 -0700764 byteLength, pos, scalarsPerPosition, offset, clipRect,
765 textRatio, &fallbackTxt, &fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700766 if (fallbackTxt.count()) {
herbe59124e2015-11-18 10:54:39 -0800767 this->fallbackDrawPosText(blob, 0, rt, clip, paint.getColor(), skPaint, viewMatrix,
768 fallbackTxt, fallbackPos, scalarsPerPosition, offset,
jvanverth0628a522015-08-18 07:44:22 -0700769 clipRect);
joshualitt9bd2daf2015-04-17 09:30:06 -0700770 }
771 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700772 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700773 blob->fViewMatrix = viewMatrix;
774 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
775 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
776 byteLength, pos, scalarsPerPosition, offset, clipRect);
777 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700778 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700779 return blob;
780}
781
robertphillipsf6703fa2015-09-01 05:36:47 -0700782void GrAtlasTextContext::onDrawText(GrDrawContext* dc, GrRenderTarget* rt,
herbe59124e2015-11-18 10:54:39 -0800783 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700784 const GrPaint& paint, const SkPaint& skPaint,
785 const SkMatrix& viewMatrix,
786 const char text[], size_t byteLength,
787 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700788 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips2334fb62015-06-17 05:43:33 -0700789 this->createDrawTextBlob(rt, clip, paint, skPaint, viewMatrix,
790 text, byteLength, x, y, regionClipBounds));
robertphillipsf6703fa2015-09-01 05:36:47 -0700791 this->flush(blob, dc, rt, skPaint, paint, clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700792}
793
robertphillipsf6703fa2015-09-01 05:36:47 -0700794void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700795 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700796 const GrPaint& paint, const SkPaint& skPaint,
797 const SkMatrix& viewMatrix,
798 const char text[], size_t byteLength,
799 const SkScalar pos[], int scalarsPerPosition,
800 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700801 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips2334fb62015-06-17 05:43:33 -0700802 this->createDrawPosTextBlob(rt, clip, paint, skPaint, viewMatrix,
803 text, byteLength,
804 pos, scalarsPerPosition,
805 offset, regionClipBounds));
joshualitt79dfb2b2015-05-11 08:58:08 -0700806
robertphillipsf6703fa2015-09-01 05:36:47 -0700807 this->flush(blob, dc, rt, skPaint, paint, clip, regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700808}
809
joshualitt374b2f72015-07-21 08:05:03 -0700810void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700811 SkGlyphCache* cache, const SkPaint& skPaint,
812 GrColor color,
813 const SkMatrix& viewMatrix,
814 const char text[], size_t byteLength,
815 SkScalar x, SkScalar y, const SkIRect& clipRect) {
halcanary96fcdcc2015-08-27 07:41:13 -0700816 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700817
818 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700819 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700820 return;
821 }
822
halcanary96fcdcc2015-08-27 07:41:13 -0700823 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700824 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
825
826 // Get GrFontScaler from cache
827 GrFontScaler* fontScaler = GetGrFontScaler(cache);
828
herbe59124e2015-11-18 10:54:39 -0800829 SkFindAndPlaceGlyph::ProcessText(
830 text, byteLength, {x, y}, viewMatrix, skPaint.getTextAlign(), glyphCacheProc, cache,
831 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
832 position += rounding;
833 this->bmpAppendGlyph(
834 blob, runIndex, glyph,
835 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
836 color, fontScaler, clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700837 }
herbe59124e2015-11-18 10:54:39 -0800838 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700839}
840
joshualitt374b2f72015-07-21 08:05:03 -0700841void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700842 SkGlyphCache* cache, const SkPaint& skPaint,
843 GrColor color,
844 const SkMatrix& viewMatrix,
845 const char text[], size_t byteLength,
846 const SkScalar pos[], int scalarsPerPosition,
847 const SkPoint& offset, const SkIRect& clipRect) {
halcanary96fcdcc2015-08-27 07:41:13 -0700848 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700849 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
850
851 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700852 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700853 return;
854 }
855
halcanary96fcdcc2015-08-27 07:41:13 -0700856 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700857 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
858
859 // Get GrFontScaler from cache
860 GrFontScaler* fontScaler = GetGrFontScaler(cache);
861
herb9be5ff62015-11-11 11:30:11 -0800862 SkFindAndPlaceGlyph::ProcessPosText(
863 text, byteLength, offset, viewMatrix, pos, scalarsPerPosition,
864 skPaint.getTextAlign(), glyphCacheProc, cache,
865 [&](const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
866 position += rounding;
867 this->bmpAppendGlyph(
868 blob, runIndex, glyph,
869 SkScalarFloorToInt(position.fX), SkScalarFloorToInt(position.fY),
870 color, fontScaler, clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700871 }
herb9be5ff62015-11-11 11:30:11 -0800872 );
joshualitt1d89e8d2015-04-01 12:40:54 -0700873}
874
joshualitt374b2f72015-07-21 08:05:03 -0700875void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700876 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700877 const SkMatrix& viewMatrix,
878 const char text[], size_t byteLength,
879 SkScalar x, SkScalar y, const SkIRect& clipRect,
880 SkScalar textRatio,
881 SkTDArray<char>* fallbackTxt,
882 SkTDArray<SkScalar>* fallbackPos,
883 SkPoint* offset,
884 const SkPaint& origPaint) {
halcanary96fcdcc2015-08-27 07:41:13 -0700885 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700886
887 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700888 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700889 return;
890 }
891
892 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
893 SkAutoDescriptor desc;
halcanary96fcdcc2015-08-27 07:41:13 -0700894 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
herbe59124e2015-11-18 10:54:39 -0800895 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
joshualitt9bd2daf2015-04-17 09:30:06 -0700896 desc.getDesc());
897
898 SkTArray<SkScalar> positions;
899
900 const char* textPtr = text;
901 SkFixed stopX = 0;
902 SkFixed stopY = 0;
903 SkFixed origin = 0;
904 switch (origPaint.getTextAlign()) {
905 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
906 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
907 case SkPaint::kLeft_Align: origin = 0; break;
908 }
909
910 SkAutoKern autokern;
911 const char* stop = text + byteLength;
912 while (textPtr < stop) {
913 // don't need x, y here, since all subpixel variants will have the
914 // same advance
915 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
916
917 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
918 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
919
920 SkFixed height = glyph.fAdvanceY;
921 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
922
923 stopX += width;
924 stopY += height;
925 }
926 SkASSERT(textPtr == stop);
927
jvanverth157e6482015-09-09 08:05:12 -0700928 SkGlyphCache::AttachCache(origPaintCache);
929
joshualitt9bd2daf2015-04-17 09:30:06 -0700930 // now adjust starting point depending on alignment
931 SkScalar alignX = SkFixedToScalar(stopX);
932 SkScalar alignY = SkFixedToScalar(stopY);
933 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
934 alignX = SkScalarHalf(alignX);
935 alignY = SkScalarHalf(alignY);
936 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
937 alignX = 0;
938 alignY = 0;
939 }
940 x -= alignX;
941 y -= alignY;
942 *offset = SkPoint::Make(x, y);
943
jvanverth157e6482015-09-09 08:05:12 -0700944 this->internalDrawDFPosText(blob, runIndex, skPaint, color, viewMatrix, text, byteLength,
joshualitt9bd2daf2015-04-17 09:30:06 -0700945 positions.begin(), 2, *offset, clipRect, textRatio, fallbackTxt,
946 fallbackPos);
joshualitt9bd2daf2015-04-17 09:30:06 -0700947}
948
joshualitt374b2f72015-07-21 08:05:03 -0700949void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
jvanverth157e6482015-09-09 08:05:12 -0700950 const SkPaint& skPaint, GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700951 const SkMatrix& viewMatrix,
952 const char text[], size_t byteLength,
953 const SkScalar pos[], int scalarsPerPosition,
954 const SkPoint& offset, const SkIRect& clipRect,
955 SkScalar textRatio,
956 SkTDArray<char>* fallbackTxt,
957 SkTDArray<SkScalar>* fallbackPos) {
958
halcanary96fcdcc2015-08-27 07:41:13 -0700959 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -0700960 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
961
962 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700963 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700964 return;
965 }
966
halcanary96fcdcc2015-08-27 07:41:13 -0700967 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -0700968
969 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
jvanverth157e6482015-09-09 08:05:12 -0700970 SkGlyphCache* cache = this->setupCache(&blob->fRuns[runIndex], skPaint, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700971 GrFontScaler* fontScaler = GetGrFontScaler(cache);
972
973 const char* stop = text + byteLength;
974
975 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
976 while (text < stop) {
977 const char* lastText = text;
978 // the last 2 parameters are ignored
979 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
980
981 if (glyph.fWidth) {
982 SkScalar x = offset.x() + pos[0];
983 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
984
985 if (!this->dfAppendGlyph(blob,
986 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700987 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700988 x, y, color, fontScaler, clipRect,
989 textRatio, viewMatrix)) {
990 // couldn't append, send to fallback
991 fallbackTxt->append(SkToInt(text-lastText), lastText);
992 *fallbackPos->append() = pos[0];
993 if (2 == scalarsPerPosition) {
994 *fallbackPos->append() = pos[1];
995 }
996 }
997 }
998 pos += scalarsPerPosition;
999 }
1000 } else {
1001 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
1002 : SK_Scalar1;
1003 while (text < stop) {
1004 const char* lastText = text;
1005 // the last 2 parameters are ignored
1006 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1007
1008 if (glyph.fWidth) {
1009 SkScalar x = offset.x() + pos[0];
1010 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
1011
1012 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
1013 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
1014
1015 if (!this->dfAppendGlyph(blob,
1016 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001017 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001018 x - advanceX, y - advanceY, color,
1019 fontScaler,
1020 clipRect,
1021 textRatio,
1022 viewMatrix)) {
1023 // couldn't append, send to fallback
1024 fallbackTxt->append(SkToInt(text-lastText), lastText);
1025 *fallbackPos->append() = pos[0];
1026 if (2 == scalarsPerPosition) {
1027 *fallbackPos->append() = pos[1];
1028 }
1029 }
1030 }
1031 pos += scalarsPerPosition;
1032 }
1033 }
jvanverth157e6482015-09-09 08:05:12 -07001034
1035 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -07001036}
1037
joshualitt374b2f72015-07-21 08:05:03 -07001038void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001039 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001040 int vx, int vy, GrColor color, GrFontScaler* scaler,
1041 const SkIRect& clipRect) {
joshualittae32c102015-04-21 09:37:57 -07001042 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001043 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001044 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1045 }
1046
joshualitt6c2c2b02015-07-24 10:37:00 -07001047 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1048 skGlyph.getSubXFixed(),
1049 skGlyph.getSubYFixed(),
1050 GrGlyph::kCoverage_MaskStyle);
1051 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001052 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001053 return;
1054 }
1055
1056 int x = vx + glyph->fBounds.fLeft;
1057 int y = vy + glyph->fBounds.fTop;
1058
1059 // keep them as ints until we've done the clip-test
1060 int width = glyph->fBounds.width();
1061 int height = glyph->fBounds.height();
1062
joshualitt2a0e9f32015-04-13 06:12:21 -07001063#if 0
1064 // Not checking the clip bounds might introduce a performance regression. However, its not
1065 // clear if this is still true today with the larger tiles we use in Chrome. For repositionable
1066 // blobs, we want to make sure we have all of the glyphs, so clipping them out is not ideal.
1067 // We could store the cliprect in the key, but then we'd lose the ability to do integer scrolls
1068 // TODO verify this
joshualitt1d89e8d2015-04-01 12:40:54 -07001069 // check if we clipped out
1070 if (clipRect.quickReject(x, y, x + width, y + height)) {
1071 return;
1072 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001073#endif
joshualitt1d89e8d2015-04-01 12:40:54 -07001074
1075 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001076 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001077 this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy));
joshualitt1d89e8d2015-04-01 12:40:54 -07001078 return;
1079 }
1080
joshualitt1d89e8d2015-04-01 12:40:54 -07001081 GrMaskFormat format = glyph->fMaskFormat;
1082
1083 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
1084 if (run.fInitialized && subRun->fMaskFormat != format) {
joshualittd9f13ae2015-07-24 11:24:31 -07001085 subRun = &run.push_back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001086 subRun->fStrike.reset(SkRef(fCurrStrike));
1087 } else if (!run.fInitialized) {
1088 subRun->fStrike.reset(SkRef(fCurrStrike));
joshualitt1d89e8d2015-04-01 12:40:54 -07001089 }
1090
1091 run.fInitialized = true;
joshualitt1d89e8d2015-04-01 12:40:54 -07001092
1093 size_t vertexStride = get_vertex_stride(format);
1094
1095 SkRect r;
1096 r.fLeft = SkIntToScalar(x);
1097 r.fTop = SkIntToScalar(y);
1098 r.fRight = r.fLeft + SkIntToScalar(width);
1099 r.fBottom = r.fTop + SkIntToScalar(height);
joshualitt9bd2daf2015-04-17 09:30:06 -07001100 subRun->fMaskFormat = format;
1101 this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format,
joshualittae32c102015-04-21 09:37:57 -07001102 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001103}
joshualitt1d89e8d2015-04-01 12:40:54 -07001104
joshualitt374b2f72015-07-21 08:05:03 -07001105bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001106 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001107 SkScalar sx, SkScalar sy, GrColor color,
1108 GrFontScaler* scaler,
1109 const SkIRect& clipRect,
1110 SkScalar textRatio, const SkMatrix& viewMatrix) {
joshualittae32c102015-04-21 09:37:57 -07001111 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001112 if (!fCurrStrike) {
1113 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1114 }
1115
joshualitt6c2c2b02015-07-24 10:37:00 -07001116 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1117 skGlyph.getSubXFixed(),
1118 skGlyph.getSubYFixed(),
1119 GrGlyph::kDistance_MaskStyle);
1120 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001121 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001122 return true;
1123 }
1124
1125 // fallback to color glyph support
1126 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
1127 return false;
1128 }
1129
1130 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
1131 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
1132 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
1133 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
1134
1135 SkScalar scale = textRatio;
1136 dx *= scale;
1137 dy *= scale;
1138 width *= scale;
1139 height *= scale;
1140 sx += dx;
1141 sy += dy;
1142 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
1143
1144#if 0
1145 // check if we clipped out
1146 SkRect dstRect;
1147 viewMatrix.mapRect(&dstRect, glyphRect);
1148 if (clipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
1149 SkScalarTruncToInt(dstRect.top()),
1150 SkScalarTruncToInt(dstRect.right()),
1151 SkScalarTruncToInt(dstRect.bottom()))) {
1152 return true;
1153 }
1154#endif
1155
1156 // TODO combine with the above
1157 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001158 if (glyph->fTooLargeForAtlas) {
joshualitt0fe04a22015-08-25 12:05:50 -07001159 this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -07001160 return true;
1161 }
1162
joshualitt9bd2daf2015-04-17 09:30:06 -07001163 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001164 if (!run.fInitialized) {
1165 subRun->fStrike.reset(SkRef(fCurrStrike));
1166 }
1167 run.fInitialized = true;
joshualitt9bd2daf2015-04-17 09:30:06 -07001168 SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat);
1169 subRun->fMaskFormat = kA8_GrMaskFormat;
1170
1171 size_t vertexStride = get_vertex_stride_df(kA8_GrMaskFormat, subRun->fUseLCDText);
1172
1173 bool useColorVerts = !subRun->fUseLCDText;
1174 this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts,
joshualittae32c102015-04-21 09:37:57 -07001175 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001176 return true;
1177}
1178
joshualitt374b2f72015-07-21 08:05:03 -07001179inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001180 GrFontScaler* scaler, const SkGlyph& skGlyph,
joshualitt0fe04a22015-08-25 12:05:50 -07001181 SkScalar x, SkScalar y, SkScalar scale,
1182 bool applyVM) {
halcanary96fcdcc2015-08-27 07:41:13 -07001183 if (nullptr == glyph->fPath) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001184 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
1185 if (!glyphPath) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001186 return;
1187 }
joshualitt6c2c2b02015-07-24 10:37:00 -07001188
halcanary385fe4d2015-08-26 13:07:48 -07001189 glyph->fPath = new SkPath(*glyphPath);
joshualitt9bd2daf2015-04-17 09:30:06 -07001190 }
joshualitt0fe04a22015-08-25 12:05:50 -07001191 blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
joshualitt9bd2daf2015-04-17 09:30:06 -07001192}
1193
joshualitt374b2f72015-07-21 08:05:03 -07001194inline void GrAtlasTextContext::appendGlyphCommon(GrAtlasTextBlob* blob, Run* run,
joshualitt9bd2daf2015-04-17 09:30:06 -07001195 Run::SubRunInfo* subRun,
1196 const SkRect& positions, GrColor color,
1197 size_t vertexStride, bool useVertexColor,
joshualittae32c102015-04-21 09:37:57 -07001198 GrGlyph* glyph) {
1199 blob->fGlyphs[subRun->fGlyphEndIndex] = glyph;
joshualitt9bd2daf2015-04-17 09:30:06 -07001200 run->fVertexBounds.joinNonEmptyArg(positions);
1201 run->fColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -07001202
1203 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->fVertexEndIndex);
1204
joshualitt9bd2daf2015-04-17 09:30:06 -07001205 if (useVertexColor) {
joshualitt010db532015-04-21 10:07:26 -07001206 // V0
1207 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1208 position->set(positions.fLeft, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001209 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
1210 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001211 vertex += vertexStride;
joshualitt9bd2daf2015-04-17 09:30:06 -07001212
joshualitt010db532015-04-21 10:07:26 -07001213 // V1
1214 position = reinterpret_cast<SkPoint*>(vertex);
1215 position->set(positions.fLeft, positions.fBottom);
1216 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001217 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001218 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001219
joshualitt010db532015-04-21 10:07:26 -07001220 // V2
1221 position = reinterpret_cast<SkPoint*>(vertex);
1222 position->set(positions.fRight, positions.fBottom);
1223 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001224 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001225 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001226
joshualitt010db532015-04-21 10:07:26 -07001227 // V3
1228 position = reinterpret_cast<SkPoint*>(vertex);
1229 position->set(positions.fRight, positions.fTop);
1230 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001231 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001232 } else {
1233 // V0
1234 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1235 position->set(positions.fLeft, positions.fTop);
1236 vertex += vertexStride;
1237
1238 // V1
1239 position = reinterpret_cast<SkPoint*>(vertex);
1240 position->set(positions.fLeft, positions.fBottom);
1241 vertex += vertexStride;
1242
1243 // V2
1244 position = reinterpret_cast<SkPoint*>(vertex);
1245 position->set(positions.fRight, positions.fBottom);
1246 vertex += vertexStride;
1247
1248 // V3
1249 position = reinterpret_cast<SkPoint*>(vertex);
1250 position->set(positions.fRight, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001251 }
1252
1253 subRun->fGlyphEndIndex++;
1254 subRun->fVertexEndIndex += vertexStride * kVerticesPerGlyph;
1255}
1256
bsalomonabd30f52015-08-13 13:34:48 -07001257class TextBatch : public GrVertexBatch {
joshualitt1d89e8d2015-04-01 12:40:54 -07001258public:
reed1b55a962015-09-17 20:16:13 -07001259 DEFINE_BATCH_CLASS_ID
1260
joshualitt9bd2daf2015-04-17 09:30:06 -07001261 typedef GrAtlasTextContext::DistanceAdjustTable DistanceAdjustTable;
joshualitt374b2f72015-07-21 08:05:03 -07001262 typedef GrAtlasTextBlob Blob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001263 typedef Blob::Run Run;
1264 typedef Run::SubRunInfo TextInfo;
1265 struct Geometry {
joshualittad802c62015-04-15 05:31:57 -07001266 Blob* fBlob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001267 int fRun;
1268 int fSubRun;
1269 GrColor fColor;
joshualitt2a0e9f32015-04-13 06:12:21 -07001270 SkScalar fTransX;
1271 SkScalar fTransY;
joshualitt1d89e8d2015-04-01 12:40:54 -07001272 };
1273
bsalomon265697d2015-07-22 10:17:26 -07001274 static TextBatch* CreateBitmap(GrMaskFormat maskFormat, int glyphCount,
joshualittad802c62015-04-15 05:31:57 -07001275 GrBatchFontCache* fontCache) {
halcanary385fe4d2015-08-26 13:07:48 -07001276 TextBatch* batch = new TextBatch;
bsalomon265697d2015-07-22 10:17:26 -07001277
bsalomon265697d2015-07-22 10:17:26 -07001278 batch->fFontCache = fontCache;
1279 switch (maskFormat) {
1280 case kA8_GrMaskFormat:
1281 batch->fMaskType = kGrayscaleCoverageMask_MaskType;
1282 break;
1283 case kA565_GrMaskFormat:
1284 batch->fMaskType = kLCDCoverageMask_MaskType;
1285 break;
1286 case kARGB_GrMaskFormat:
1287 batch->fMaskType = kColorBitmapMask_MaskType;
1288 break;
1289 }
1290 batch->fBatch.fNumGlyphs = glyphCount;
bsalomond602f4d2015-07-27 06:12:01 -07001291 batch->fGeoCount = 1;
bsalomon265697d2015-07-22 10:17:26 -07001292 batch->fFilteredColor = 0;
1293 batch->fFontCache = fontCache;
1294 batch->fUseBGR = false;
1295 return batch;
joshualitt1d89e8d2015-04-01 12:40:54 -07001296 }
1297
bsalomon265697d2015-07-22 10:17:26 -07001298 static TextBatch* CreateDistanceField(int glyphCount, GrBatchFontCache* fontCache,
robertphillipsf6703fa2015-09-01 05:36:47 -07001299 const DistanceAdjustTable* distanceAdjustTable,
bsalomon265697d2015-07-22 10:17:26 -07001300 SkColor filteredColor, bool isLCD,
1301 bool useBGR) {
halcanary385fe4d2015-08-26 13:07:48 -07001302 TextBatch* batch = new TextBatch;
reed1b55a962015-09-17 20:16:13 -07001303
bsalomon265697d2015-07-22 10:17:26 -07001304 batch->fFontCache = fontCache;
1305 batch->fMaskType = isLCD ? kLCDDistanceField_MaskType : kGrayscaleDistanceField_MaskType;
1306 batch->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
1307 batch->fFilteredColor = filteredColor;
1308 batch->fUseBGR = useBGR;
1309 batch->fBatch.fNumGlyphs = glyphCount;
bsalomond602f4d2015-07-27 06:12:01 -07001310 batch->fGeoCount = 1;
bsalomon265697d2015-07-22 10:17:26 -07001311 return batch;
joshualitt9bd2daf2015-04-17 09:30:06 -07001312 }
1313
bsalomone46f9fe2015-08-18 06:05:14 -07001314 // to avoid even the initial copy of the struct, we have a getter for the first item which
1315 // is used to seed the batch with its initial geometry. After seeding, the client should call
1316 // init() so the Batch can initialize itself
1317 Geometry& geometry() { return fGeoData[0]; }
1318
1319 void init() {
1320 const Geometry& geo = fGeoData[0];
1321 fBatch.fColor = geo.fColor;
1322 fBatch.fViewMatrix = geo.fBlob->fViewMatrix;
1323
1324 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
1325 // into device space
1326 const Run& run = geo.fBlob->fRuns[geo.fRun];
1327 if (run.fSubRunInfo[geo.fSubRun].fDrawAsDistanceFields) {
1328 SkRect bounds = run.fVertexBounds;
1329 fBatch.fViewMatrix.mapRect(&bounds);
1330 this->setBounds(bounds);
1331 } else {
1332 this->setBounds(run.fVertexBounds);
1333 }
1334 }
1335
bsalomon265697d2015-07-22 10:17:26 -07001336 const char* name() const override { return "TextBatch"; }
joshualitt1d89e8d2015-04-01 12:40:54 -07001337
1338 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001339 if (kColorBitmapMask_MaskType == fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001340 out->setUnknownFourComponents();
1341 } else {
1342 out->setKnownFourComponents(fBatch.fColor);
1343 }
1344 }
1345
1346 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001347 switch (fMaskType) {
1348 case kGrayscaleDistanceField_MaskType:
1349 case kGrayscaleCoverageMask_MaskType:
joshualitt1d89e8d2015-04-01 12:40:54 -07001350 out->setUnknownSingleComponent();
bsalomon265697d2015-07-22 10:17:26 -07001351 break;
1352 case kLCDCoverageMask_MaskType:
1353 case kLCDDistanceField_MaskType:
1354 out->setUnknownOpaqueFourComponents();
joshualitt1d89e8d2015-04-01 12:40:54 -07001355 out->setUsingLCDCoverage();
bsalomon265697d2015-07-22 10:17:26 -07001356 break;
1357 case kColorBitmapMask_MaskType:
1358 out->setKnownSingleComponent(0xff);
joshualitt1d89e8d2015-04-01 12:40:54 -07001359 }
1360 }
1361
bsalomone46f9fe2015-08-18 06:05:14 -07001362private:
bsalomon91d844d2015-08-10 10:47:29 -07001363 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001364 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -07001365 if (!opt.readsColor()) {
joshualitt416e14f2015-07-10 09:05:57 -07001366 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001367 }
bsalomon91d844d2015-08-10 10:47:29 -07001368 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt1d89e8d2015-04-01 12:40:54 -07001369
1370 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -07001371 fBatch.fColorIgnored = !opt.readsColor();
joshualitt416e14f2015-07-10 09:05:57 -07001372 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -07001373 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
1374 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt1d89e8d2015-04-01 12:40:54 -07001375 }
1376
bsalomonb5238a72015-05-05 07:49:49 -07001377 struct FlushInfo {
1378 SkAutoTUnref<const GrVertexBuffer> fVertexBuffer;
1379 SkAutoTUnref<const GrIndexBuffer> fIndexBuffer;
1380 int fGlyphsToFlush;
1381 int fVertexOffset;
1382 };
1383
bsalomon75398562015-08-17 12:55:38 -07001384 void onPrepareDraws(Target* target) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001385 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
1386 // TODO actually only invert if we don't have RGBA
1387 SkMatrix localMatrix;
1388 if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) {
1389 SkDebugf("Cannot invert viewmatrix\n");
1390 return;
1391 }
1392
bsalomon265697d2015-07-22 10:17:26 -07001393 GrTexture* texture = fFontCache->getTexture(this->maskFormat());
joshualitt62db8ba2015-04-09 08:22:37 -07001394 if (!texture) {
1395 SkDebugf("Could not allocate backing texture for atlas\n");
1396 return;
1397 }
1398
bsalomon265697d2015-07-22 10:17:26 -07001399 bool usesDistanceFields = this->usesDistanceFields();
1400 GrMaskFormat maskFormat = this->maskFormat();
1401 bool isLCD = this->isLCD();
1402
joshualitt9bd2daf2015-04-17 09:30:06 -07001403 SkAutoTUnref<const GrGeometryProcessor> gp;
bsalomon265697d2015-07-22 10:17:26 -07001404 if (usesDistanceFields) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001405 gp.reset(this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(),
1406 texture));
1407 } else {
1408 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
joshualitt9bd2daf2015-04-17 09:30:06 -07001409 gp.reset(GrBitmapTextGeoProc::Create(this->color(),
1410 texture,
1411 params,
bsalomon265697d2015-07-22 10:17:26 -07001412 maskFormat,
joshualittb8c241a2015-05-19 08:23:30 -07001413 localMatrix,
1414 this->usesLocalCoords()));
joshualitt9bd2daf2015-04-17 09:30:06 -07001415 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001416
bsalomonb5238a72015-05-05 07:49:49 -07001417 FlushInfo flushInfo;
1418 flushInfo.fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001419 size_t vertexStride = gp->getVertexStride();
bsalomon265697d2015-07-22 10:17:26 -07001420 SkASSERT(vertexStride == (usesDistanceFields ?
1421 get_vertex_stride_df(maskFormat, isLCD) :
1422 get_vertex_stride(maskFormat)));
joshualitt1d89e8d2015-04-01 12:40:54 -07001423
bsalomon75398562015-08-17 12:55:38 -07001424 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001425
1426 int glyphCount = this->numGlyphs();
bsalomon8415abe2015-05-04 11:41:41 -07001427 const GrVertexBuffer* vertexBuffer;
bsalomonb5238a72015-05-05 07:49:49 -07001428
bsalomon75398562015-08-17 12:55:38 -07001429 void* vertices = target->makeVertexSpace(vertexStride,
1430 glyphCount * kVerticesPerGlyph,
1431 &vertexBuffer,
1432 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -07001433 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
bsalomon75398562015-08-17 12:55:38 -07001434 flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
bsalomonb5238a72015-05-05 07:49:49 -07001435 if (!vertices || !flushInfo.fVertexBuffer) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001436 SkDebugf("Could not allocate vertices\n");
1437 return;
1438 }
1439
1440 unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices);
1441
joshualitt25ba7ea2015-04-21 07:49:49 -07001442 // We cache some values to avoid going to the glyphcache for the same fontScaler twice
1443 // in a row
halcanary96fcdcc2015-08-27 07:41:13 -07001444 const SkDescriptor* desc = nullptr;
1445 SkGlyphCache* cache = nullptr;
1446 GrFontScaler* scaler = nullptr;
1447 SkTypeface* typeface = nullptr;
joshualitt25ba7ea2015-04-21 07:49:49 -07001448
bsalomond602f4d2015-07-27 06:12:01 -07001449 for (int i = 0; i < fGeoCount; i++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001450 Geometry& args = fGeoData[i];
1451 Blob* blob = args.fBlob;
1452 Run& run = blob->fRuns[args.fRun];
1453 TextInfo& info = run.fSubRunInfo[args.fSubRun];
1454
bsalomon265697d2015-07-22 10:17:26 -07001455 uint64_t currentAtlasGen = fFontCache->atlasGeneration(maskFormat);
joshualitt7a9c45c2015-05-26 12:32:23 -07001456 bool regenerateTextureCoords = info.fAtlasGeneration != currentAtlasGen ||
joshualitt7e97b0b2015-07-31 15:18:08 -07001457 info.fStrike->isAbandoned();
joshualitt9bd2daf2015-04-17 09:30:06 -07001458 bool regenerateColors;
bsalomon265697d2015-07-22 10:17:26 -07001459 if (usesDistanceFields) {
1460 regenerateColors = !isLCD && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001461 } else {
bsalomon265697d2015-07-22 10:17:26 -07001462 regenerateColors = kA8_GrMaskFormat == maskFormat && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001463 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001464 bool regeneratePositions = args.fTransX != 0.f || args.fTransY != 0.f;
joshualitt1d89e8d2015-04-01 12:40:54 -07001465 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1466
1467 // We regenerate both texture coords and colors in the blob itself, and update the
1468 // atlas generation. If we don't end up purging any unused plots, we can avoid
1469 // regenerating the coords. We could take a finer grained approach to updating texture
1470 // coords but its not clear if the extra bookkeeping would offset any gains.
1471 // To avoid looping over the glyphs twice, we do one loop and conditionally update color
1472 // or coords as needed. One final note, if we have to break a run for an atlas eviction
1473 // then we can't really trust the atlas has all of the correct data. Atlas evictions
1474 // should be pretty rare, so we just always regenerate in those cases
joshualitt2a0e9f32015-04-13 06:12:21 -07001475 if (regenerateTextureCoords || regenerateColors || regeneratePositions) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001476 // first regenerate texture coordinates / colors if need be
joshualitt1d89e8d2015-04-01 12:40:54 -07001477 bool brokenRun = false;
joshualittae32c102015-04-21 09:37:57 -07001478
1479 // Because the GrBatchFontCache may evict the strike a blob depends on using for
1480 // generating its texture coords, we have to track whether or not the strike has
1481 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
1482 // otherwise we have to get the new strike, and use that to get the correct glyphs.
1483 // Because we do not have the packed ids, and thus can't look up our glyphs in the
1484 // new strike, we instead keep our ref to the old strike and use the packed ids from
1485 // it. These ids will still be valid as long as we hold the ref. When we are done
1486 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
1487 bool regenerateGlyphs = false;
halcanary96fcdcc2015-08-27 07:41:13 -07001488 GrBatchTextStrike* strike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -07001489 if (regenerateTextureCoords) {
joshualittb4c507e2015-04-08 08:07:59 -07001490 info.fBulkUseToken.reset();
joshualitt25ba7ea2015-04-21 07:49:49 -07001491
1492 // We can reuse if we have a valid strike and our descriptors / typeface are the
joshualitt94469302015-09-02 06:13:39 -07001493 // same. The override descriptor is only for the non distance field text within
1494 // a run
1495 const SkDescriptor* newDesc = (run.fOverrideDescriptor && !usesDistanceFields) ?
joshualitt97202d22015-04-22 13:47:02 -07001496 run.fOverrideDescriptor->getDesc() :
joshualitt25ba7ea2015-04-21 07:49:49 -07001497 run.fDescriptor.getDesc();
1498 if (!cache || !SkTypeface::Equal(typeface, run.fTypeface) ||
1499 !(desc->equals(*newDesc))) {
1500 if (cache) {
1501 SkGlyphCache::AttachCache(cache);
1502 }
1503 desc = newDesc;
1504 cache = SkGlyphCache::DetachCache(run.fTypeface, desc);
1505 scaler = GrTextContext::GetGrFontScaler(cache);
joshualitt7e97b0b2015-07-31 15:18:08 -07001506 strike = info.fStrike;
joshualitt25ba7ea2015-04-21 07:49:49 -07001507 typeface = run.fTypeface;
1508 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001509
joshualitt7e97b0b2015-07-31 15:18:08 -07001510 if (info.fStrike->isAbandoned()) {
joshualittae32c102015-04-21 09:37:57 -07001511 regenerateGlyphs = true;
1512 strike = fFontCache->getStrike(scaler);
1513 } else {
joshualitt7e97b0b2015-07-31 15:18:08 -07001514 strike = info.fStrike;
joshualittae32c102015-04-21 09:37:57 -07001515 }
1516 }
1517
1518 for (int glyphIdx = 0; glyphIdx < glyphCount; glyphIdx++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001519 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001520 size_t glyphOffset = glyphIdx + info.fGlyphStartIndex;
joshualitt6c2c2b02015-07-24 10:37:00 -07001521
1522 GrGlyph* glyph = blob->fGlyphs[glyphOffset];
1523 GrGlyph::PackedID id = glyph->fPackedID;
1524 const SkGlyph& skGlyph = scaler->grToSkGlyph(id);
joshualittae32c102015-04-21 09:37:57 -07001525 if (regenerateGlyphs) {
1526 // Get the id from the old glyph, and use the new strike to lookup
1527 // the glyph.
joshualitt76cc6572015-07-31 05:51:45 -07001528 blob->fGlyphs[glyphOffset] = strike->getGlyph(skGlyph, id, maskFormat,
1529 scaler);
joshualittae32c102015-04-21 09:37:57 -07001530 }
1531 glyph = blob->fGlyphs[glyphOffset];
joshualitt1d89e8d2015-04-01 12:40:54 -07001532 SkASSERT(glyph);
joshualitt65e96b42015-07-31 11:45:22 -07001533 SkASSERT(id == glyph->fPackedID);
1534 // We want to be able to assert this but cannot for testing purposes.
1535 // once skbug:4143 has landed we can revist this assert
1536 //SkASSERT(glyph->fMaskFormat == this->maskFormat());
joshualitt1d89e8d2015-04-01 12:40:54 -07001537
1538 if (!fFontCache->hasGlyph(glyph) &&
bsalomon75398562015-08-17 12:55:38 -07001539 !strike->addGlyphToAtlas(target, glyph, scaler, skGlyph, maskFormat)) {
1540 this->flush(target, &flushInfo);
1541 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001542 brokenRun = glyphIdx > 0;
1543
bsalomon75398562015-08-17 12:55:38 -07001544 SkDEBUGCODE(bool success =) strike->addGlyphToAtlas(target,
joshualittae32c102015-04-21 09:37:57 -07001545 glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001546 scaler,
joshualitt4f19ca32015-07-30 07:59:20 -07001547 skGlyph,
1548 maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001549 SkASSERT(success);
1550 }
joshualittb4c507e2015-04-08 08:07:59 -07001551 fFontCache->addGlyphToBulkAndSetUseToken(&info.fBulkUseToken, glyph,
bsalomon75398562015-08-17 12:55:38 -07001552 target->currentToken());
joshualitt1d89e8d2015-04-01 12:40:54 -07001553
1554 // Texture coords are the last vertex attribute so we get a pointer to the
1555 // first one and then map with stride in regenerateTextureCoords
1556 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1557 vertex += info.fVertexStartIndex;
1558 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1559 vertex += vertexStride - sizeof(SkIPoint16);
1560
1561 this->regenerateTextureCoords(glyph, vertex, vertexStride);
1562 }
1563
1564 if (regenerateColors) {
1565 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1566 vertex += info.fVertexStartIndex;
1567 vertex += vertexStride * glyphIdx * kVerticesPerGlyph + sizeof(SkPoint);
1568 this->regenerateColors(vertex, vertexStride, args.fColor);
1569 }
1570
joshualitt2a0e9f32015-04-13 06:12:21 -07001571 if (regeneratePositions) {
1572 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1573 vertex += info.fVertexStartIndex;
1574 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1575 SkScalar transX = args.fTransX;
1576 SkScalar transY = args.fTransY;
1577 this->regeneratePositions(vertex, vertexStride, transX, transY);
1578 }
bsalomonb5238a72015-05-05 07:49:49 -07001579 flushInfo.fGlyphsToFlush++;
joshualitt1d89e8d2015-04-01 12:40:54 -07001580 }
1581
joshualitt2a0e9f32015-04-13 06:12:21 -07001582 // We my have changed the color so update it here
1583 run.fColor = args.fColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07001584 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001585 if (regenerateGlyphs) {
joshualitt7e97b0b2015-07-31 15:18:08 -07001586 info.fStrike.reset(SkRef(strike));
joshualittae32c102015-04-21 09:37:57 -07001587 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001588 info.fAtlasGeneration = brokenRun ? GrBatchAtlas::kInvalidAtlasGeneration :
bsalomon265697d2015-07-22 10:17:26 -07001589 fFontCache->atlasGeneration(maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001590 }
1591 } else {
bsalomonb5238a72015-05-05 07:49:49 -07001592 flushInfo.fGlyphsToFlush += glyphCount;
joshualittb4c507e2015-04-08 08:07:59 -07001593
1594 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1595 // have a valid atlas generation
bsalomon75398562015-08-17 12:55:38 -07001596 fFontCache->setUseTokenBulk(info.fBulkUseToken, target->currentToken(), maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001597 }
1598
1599 // now copy all vertices
1600 size_t byteCount = info.fVertexEndIndex - info.fVertexStartIndex;
1601 memcpy(currVertex, blob->fVertices + info.fVertexStartIndex, byteCount);
1602
1603 currVertex += byteCount;
1604 }
joshualitt25ba7ea2015-04-21 07:49:49 -07001605 // Make sure to attach the last cache if applicable
1606 if (cache) {
1607 SkGlyphCache::AttachCache(cache);
1608 }
bsalomon75398562015-08-17 12:55:38 -07001609 this->flush(target, &flushInfo);
joshualitt1d89e8d2015-04-01 12:40:54 -07001610 }
1611
reed1b55a962015-09-17 20:16:13 -07001612 TextBatch() : INHERITED(ClassID()) {} // initialized in factory functions.
joshualittad802c62015-04-15 05:31:57 -07001613
bsalomon265697d2015-07-22 10:17:26 -07001614 ~TextBatch() {
bsalomond602f4d2015-07-27 06:12:01 -07001615 for (int i = 0; i < fGeoCount; i++) {
joshualittad802c62015-04-15 05:31:57 -07001616 fGeoData[i].fBlob->unref();
1617 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001618 }
1619
bsalomon265697d2015-07-22 10:17:26 -07001620 GrMaskFormat maskFormat() const {
1621 switch (fMaskType) {
1622 case kLCDCoverageMask_MaskType:
1623 return kA565_GrMaskFormat;
1624 case kColorBitmapMask_MaskType:
1625 return kARGB_GrMaskFormat;
1626 case kGrayscaleCoverageMask_MaskType:
1627 case kGrayscaleDistanceField_MaskType:
1628 case kLCDDistanceField_MaskType:
1629 return kA8_GrMaskFormat;
1630 }
1631 return kA8_GrMaskFormat; // suppress warning
1632 }
1633
1634 bool usesDistanceFields() const {
1635 return kGrayscaleDistanceField_MaskType == fMaskType ||
1636 kLCDDistanceField_MaskType == fMaskType;
1637 }
1638
1639 bool isLCD() const {
1640 return kLCDCoverageMask_MaskType == fMaskType ||
1641 kLCDDistanceField_MaskType == fMaskType;
1642 }
1643
joshualitt1d89e8d2015-04-01 12:40:54 -07001644 void regenerateTextureCoords(GrGlyph* glyph, intptr_t vertex, size_t vertexStride) {
1645 int width = glyph->fBounds.width();
1646 int height = glyph->fBounds.height();
joshualitt1d89e8d2015-04-01 12:40:54 -07001647
joshualitt9bd2daf2015-04-17 09:30:06 -07001648 int u0, v0, u1, v1;
bsalomon265697d2015-07-22 10:17:26 -07001649 if (this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001650 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
1651 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
1652 u1 = u0 + width - 2 * SK_DistanceFieldInset;
1653 v1 = v0 + height - 2 * SK_DistanceFieldInset;
1654 } else {
1655 u0 = glyph->fAtlasLocation.fX;
1656 v0 = glyph->fAtlasLocation.fY;
1657 u1 = u0 + width;
1658 v1 = v0 + height;
1659 }
1660
joshualitt1d89e8d2015-04-01 12:40:54 -07001661 SkIPoint16* textureCoords;
1662 // V0
1663 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1664 textureCoords->set(u0, v0);
1665 vertex += vertexStride;
1666
1667 // V1
1668 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1669 textureCoords->set(u0, v1);
1670 vertex += vertexStride;
1671
1672 // V2
1673 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1674 textureCoords->set(u1, v1);
1675 vertex += vertexStride;
1676
1677 // V3
1678 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1679 textureCoords->set(u1, v0);
1680 }
1681
1682 void regenerateColors(intptr_t vertex, size_t vertexStride, GrColor color) {
1683 for (int i = 0; i < kVerticesPerGlyph; i++) {
1684 SkColor* vcolor = reinterpret_cast<SkColor*>(vertex);
1685 *vcolor = color;
1686 vertex += vertexStride;
1687 }
1688 }
1689
joshualitt2a0e9f32015-04-13 06:12:21 -07001690 void regeneratePositions(intptr_t vertex, size_t vertexStride, SkScalar transX,
1691 SkScalar transY) {
1692 for (int i = 0; i < kVerticesPerGlyph; i++) {
1693 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
1694 point->fX += transX;
1695 point->fY += transY;
1696 vertex += vertexStride;
1697 }
1698 }
1699
bsalomon75398562015-08-17 12:55:38 -07001700 void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) {
bsalomoncb8979d2015-05-05 09:51:38 -07001701 GrVertices vertices;
bsalomonb5238a72015-05-05 07:49:49 -07001702 int maxGlyphsPerDraw = flushInfo->fIndexBuffer->maxQuads();
bsalomoncb8979d2015-05-05 09:51:38 -07001703 vertices.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001704 flushInfo->fIndexBuffer, flushInfo->fVertexOffset,
bsalomone64eb572015-05-07 11:35:55 -07001705 kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
bsalomonb5238a72015-05-05 07:49:49 -07001706 maxGlyphsPerDraw);
bsalomon75398562015-08-17 12:55:38 -07001707 target->draw(vertices);
bsalomonb5238a72015-05-05 07:49:49 -07001708 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
1709 flushInfo->fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001710 }
1711
1712 GrColor color() const { return fBatch.fColor; }
1713 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1714 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1715 int numGlyphs() const { return fBatch.fNumGlyphs; }
1716
bsalomoncb02b382015-08-12 11:14:50 -07001717 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001718 TextBatch* that = t->cast<TextBatch>();
1719 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1720 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001721 return false;
1722 }
1723
bsalomon265697d2015-07-22 10:17:26 -07001724 if (fMaskType != that->fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001725 return false;
1726 }
1727
bsalomon265697d2015-07-22 10:17:26 -07001728 if (!this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001729 // TODO we can often batch across LCD text if we have dual source blending and don't
1730 // have to use the blend constant
bsalomon265697d2015-07-22 10:17:26 -07001731 if (kGrayscaleCoverageMask_MaskType != fMaskType && this->color() != that->color()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001732 return false;
1733 }
joshualitt9bd2daf2015-04-17 09:30:06 -07001734 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1735 return false;
1736 }
1737 } else {
joshualitt9bd2daf2015-04-17 09:30:06 -07001738 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1739 return false;
1740 }
1741
1742 if (fFilteredColor != that->fFilteredColor) {
1743 return false;
1744 }
1745
joshualitt9bd2daf2015-04-17 09:30:06 -07001746 if (fUseBGR != that->fUseBGR) {
1747 return false;
1748 }
1749
joshualitt9bd2daf2015-04-17 09:30:06 -07001750 // TODO see note above
bsalomon265697d2015-07-22 10:17:26 -07001751 if (kLCDDistanceField_MaskType == fMaskType && this->color() != that->color()) {
joshualitt0fe04a22015-08-25 12:05:50 -07001752 return false;
joshualitt9bd2daf2015-04-17 09:30:06 -07001753 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001754 }
1755
1756 fBatch.fNumGlyphs += that->numGlyphs();
joshualittad802c62015-04-15 05:31:57 -07001757
bsalomond602f4d2015-07-27 06:12:01 -07001758 // Reallocate space for geo data if necessary and then import that's geo data.
1759 int newGeoCount = that->fGeoCount + fGeoCount;
1760 // We assume (and here enforce) that the allocation size is the smallest power of two that
1761 // is greater than or equal to the number of geometries (and at least
1762 // kMinGeometryAllocated).
1763 int newAllocSize = GrNextPow2(newGeoCount);
1764 int currAllocSize = SkTMax<int>(kMinGeometryAllocated, GrNextPow2(fGeoCount));
1765
bsalomon16ed6ad2015-07-29 06:54:33 -07001766 if (newGeoCount > currAllocSize) {
bsalomond602f4d2015-07-27 06:12:01 -07001767 fGeoData.realloc(newAllocSize);
joshualittad802c62015-04-15 05:31:57 -07001768 }
1769
bsalomond602f4d2015-07-27 06:12:01 -07001770 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
bsalomon1c634362015-07-27 07:00:00 -07001771 // We steal the ref on the blobs from the other TextBatch and set its count to 0 so that
1772 // it doesn't try to unref them.
1773#ifdef SK_DEBUG
1774 for (int i = 0; i < that->fGeoCount; ++i) {
1775 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
joshualittad802c62015-04-15 05:31:57 -07001776 }
bsalomon1c634362015-07-27 07:00:00 -07001777#endif
1778 that->fGeoCount = 0;
bsalomond602f4d2015-07-27 06:12:01 -07001779 fGeoCount = newGeoCount;
joshualitt99c7c072015-05-01 13:43:30 -07001780
1781 this->joinBounds(that->bounds());
joshualitt1d89e8d2015-04-01 12:40:54 -07001782 return true;
1783 }
1784
joshualitt9bd2daf2015-04-17 09:30:06 -07001785 // TODO just use class params
1786 // TODO trying to figure out why lcd is so whack
1787 GrGeometryProcessor* setupDfProcessor(const SkMatrix& viewMatrix, SkColor filteredColor,
1788 GrColor color, GrTexture* texture) {
1789 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
bsalomon265697d2015-07-22 10:17:26 -07001790 bool isLCD = this->isLCD();
joshualitt9bd2daf2015-04-17 09:30:06 -07001791 // set up any flags
bsalomon265697d2015-07-22 10:17:26 -07001792 uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
joshualitt9bd2daf2015-04-17 09:30:06 -07001793
1794 // see if we need to create a new effect
bsalomon265697d2015-07-22 10:17:26 -07001795 if (isLCD) {
1796 flags |= kUseLCD_DistanceFieldEffectFlag;
1797 flags |= viewMatrix.rectStaysRect() ? kRectToRect_DistanceFieldEffectFlag : 0;
1798 flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0;
1799
joshualitt9bd2daf2015-04-17 09:30:06 -07001800 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
1801
1802 float redCorrection =
1803 (*fDistanceAdjustTable)[GrColorUnpackR(colorNoPreMul) >> kDistanceAdjustLumShift];
1804 float greenCorrection =
1805 (*fDistanceAdjustTable)[GrColorUnpackG(colorNoPreMul) >> kDistanceAdjustLumShift];
1806 float blueCorrection =
1807 (*fDistanceAdjustTable)[GrColorUnpackB(colorNoPreMul) >> kDistanceAdjustLumShift];
1808 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
1809 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(redCorrection,
1810 greenCorrection,
1811 blueCorrection);
1812
1813 return GrDistanceFieldLCDTextGeoProc::Create(color,
1814 viewMatrix,
1815 texture,
1816 params,
1817 widthAdjust,
joshualittb8c241a2015-05-19 08:23:30 -07001818 flags,
1819 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07001820 } else {
1821 flags |= kColorAttr_DistanceFieldEffectFlag;
joshualitt9bd2daf2015-04-17 09:30:06 -07001822#ifdef SK_GAMMA_APPLY_TO_A8
robertphillips9fc82752015-06-19 04:46:45 -07001823 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, filteredColor);
joshualitt9bd2daf2015-04-17 09:30:06 -07001824 float correction = (*fDistanceAdjustTable)[lum >> kDistanceAdjustLumShift];
1825 return GrDistanceFieldA8TextGeoProc::Create(color,
1826 viewMatrix,
1827 texture,
1828 params,
1829 correction,
joshualittb8c241a2015-05-19 08:23:30 -07001830 flags,
1831 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07001832#else
1833 return GrDistanceFieldA8TextGeoProc::Create(color,
1834 viewMatrix,
1835 texture,
1836 params,
joshualittb8c241a2015-05-19 08:23:30 -07001837 flags,
1838 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07001839#endif
1840 }
1841
1842 }
1843
joshualitt1d89e8d2015-04-01 12:40:54 -07001844 struct BatchTracker {
1845 GrColor fColor;
1846 SkMatrix fViewMatrix;
1847 bool fUsesLocalCoords;
1848 bool fColorIgnored;
1849 bool fCoverageIgnored;
1850 int fNumGlyphs;
1851 };
1852
1853 BatchTracker fBatch;
bsalomond602f4d2015-07-27 06:12:01 -07001854 // The minimum number of Geometry we will try to allocate.
1855 enum { kMinGeometryAllocated = 4 };
1856 SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData;
1857 int fGeoCount;
bsalomon265697d2015-07-22 10:17:26 -07001858
1859 enum MaskType {
1860 kGrayscaleCoverageMask_MaskType,
1861 kLCDCoverageMask_MaskType,
1862 kColorBitmapMask_MaskType,
1863 kGrayscaleDistanceField_MaskType,
1864 kLCDDistanceField_MaskType,
1865 } fMaskType;
1866 bool fUseBGR; // fold this into the enum?
1867
joshualitt1d89e8d2015-04-01 12:40:54 -07001868 GrBatchFontCache* fFontCache;
joshualitt9bd2daf2015-04-17 09:30:06 -07001869
1870 // Distance field properties
robertphillips9fc82752015-06-19 04:46:45 -07001871 SkAutoTUnref<const DistanceAdjustTable> fDistanceAdjustTable;
joshualitt9bd2daf2015-04-17 09:30:06 -07001872 SkColor fFilteredColor;
reed1b55a962015-09-17 20:16:13 -07001873
1874 typedef GrVertexBatch INHERITED;
joshualitt1d89e8d2015-04-01 12:40:54 -07001875};
1876
robertphillipsf6703fa2015-09-01 05:36:47 -07001877void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc, GrRenderTarget* rt,
herbe59124e2015-11-18 10:54:39 -08001878 const SkTextBlobRunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -07001879 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07001880 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
1881 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
1882 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07001883
joshualitt9a27e632015-04-06 10:53:36 -07001884 size_t textLen = it.glyphCount() * sizeof(uint16_t);
1885 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07001886
joshualitt9a27e632015-04-06 10:53:36 -07001887 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07001888
joshualitt9a27e632015-04-06 10:53:36 -07001889 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
1890 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07001891 }
1892
robertphillipsfcf78292015-06-19 11:49:52 -07001893 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07001894
1895 switch (it.positioning()) {
1896 case SkTextBlob::kDefault_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -07001897 this->drawTextAsPath(dc, rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001898 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001899 textLen, x + offset.x(), y + offset.y(), clipBounds);
1900 break;
1901 case SkTextBlob::kHorizontal_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -07001902 this->drawPosTextAsPath(dc, rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001903 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001904 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
1905 clipBounds);
1906 break;
1907 case SkTextBlob::kFull_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -07001908 this->drawPosTextAsPath(dc, rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001909 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001910 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
1911 break;
1912 }
1913}
1914
bsalomonabd30f52015-08-13 13:34:48 -07001915inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -07001916GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -07001917 int glyphCount, int run, int subRun,
1918 GrColor color, SkScalar transX, SkScalar transY,
1919 const SkPaint& skPaint) {
1920 GrMaskFormat format = info.fMaskFormat;
1921 GrColor subRunColor;
1922 if (kARGB_GrMaskFormat == format) {
1923 uint8_t paintAlpha = skPaint.getAlpha();
1924 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
1925 } else {
1926 subRunColor = color;
1927 }
1928
bsalomon265697d2015-07-22 10:17:26 -07001929 TextBatch* batch;
joshualitt79dfb2b2015-05-11 08:58:08 -07001930 if (info.fDrawAsDistanceFields) {
1931 SkColor filteredColor;
1932 SkColorFilter* colorFilter = skPaint.getColorFilter();
1933 if (colorFilter) {
1934 filteredColor = colorFilter->filterColor(skPaint.getColor());
1935 } else {
1936 filteredColor = skPaint.getColor();
1937 }
robertphillipsfcf78292015-06-19 11:49:52 -07001938 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
bsalomon265697d2015-07-22 10:17:26 -07001939 batch = TextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
1940 fDistanceAdjustTable, filteredColor,
1941 info.fUseLCDText, useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07001942 } else {
bsalomon265697d2015-07-22 10:17:26 -07001943 batch = TextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07001944 }
bsalomon265697d2015-07-22 10:17:26 -07001945 TextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07001946 geometry.fBlob = SkRef(cacheBlob);
1947 geometry.fRun = run;
1948 geometry.fSubRun = subRun;
1949 geometry.fColor = subRunColor;
1950 geometry.fTransX = transX;
1951 geometry.fTransY = transY;
1952 batch->init();
1953
1954 return batch;
1955}
1956
robertphillipsf6703fa2015-09-01 05:36:47 -07001957inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07001958 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07001959 SkScalar transX, SkScalar transY,
1960 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07001961 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
1962 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
1963 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1964 if (0 == glyphCount) {
1965 continue;
1966 }
1967
bsalomonabd30f52015-08-13 13:34:48 -07001968 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
1969 subRun, color, transX, transY,
1970 skPaint));
robertphillipsf6703fa2015-09-01 05:36:47 -07001971 dc->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07001972 }
1973}
1974
herbe59124e2015-11-18 10:54:39 -08001975inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07001976 GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -07001977 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07001978 SkScalar transX, SkScalar transY,
1979 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07001980 if (!cacheBlob->fBigGlyphs.count()) {
1981 return;
1982 }
1983
joshualitt9a27e632015-04-06 10:53:36 -07001984 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07001985 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07001986 bigGlyph.fVx += transX;
1987 bigGlyph.fVy += transY;
joshualitt0fe04a22015-08-25 12:05:50 -07001988 SkMatrix ctm;
1989 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
1990 ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
1991 if (bigGlyph.fApplyVM) {
1992 ctm.postConcat(cacheBlob->fViewMatrix);
1993 }
joshualittfc072562015-05-13 12:15:06 -07001994
robertphillipsf6703fa2015-09-01 05:36:47 -07001995 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, bigGlyph.fPath,
joshualitt0fe04a22015-08-25 12:05:50 -07001996 skPaint, ctm, nullptr, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07001997 }
1998}
joshualitt9a27e632015-04-06 10:53:36 -07001999
robertphillips2334fb62015-06-17 05:43:33 -07002000void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -07002001 GrAtlasTextBlob* cacheBlob,
herbe59124e2015-11-18 10:54:39 -08002002 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07002003 GrRenderTarget* rt,
2004 const SkPaint& skPaint,
2005 const GrPaint& grPaint,
2006 SkDrawFilter* drawFilter,
2007 const GrClip& clip,
2008 const SkMatrix& viewMatrix,
2009 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07002010 SkScalar x, SkScalar y,
2011 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07002012 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
2013 // it as paths
joshualitt7b670db2015-07-09 13:25:02 -07002014 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002015
2016 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002017
halcanary33779752015-10-27 14:01:05 -07002018 SkTextBlobRunIterator it(blob);
joshualitt9a27e632015-04-06 10:53:36 -07002019 for (int run = 0; !it.done(); it.next(), run++) {
2020 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillipsf6703fa2015-09-01 05:36:47 -07002021 this->flushRunAsPaths(dc, rt, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07002022 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07002023 continue;
2024 }
joshualitt2a0e9f32015-04-13 06:12:21 -07002025 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillipsf6703fa2015-09-01 05:36:47 -07002026 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07002027 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002028 }
2029
2030 // Now flush big glyphs
robertphillipsf6703fa2015-09-01 05:36:47 -07002031 this->flushBigGlyphs(cacheBlob, dc, rt, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002032}
2033
joshualitt374b2f72015-07-21 08:05:03 -07002034void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07002035 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07002036 GrRenderTarget* rt,
2037 const SkPaint& skPaint,
2038 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07002039 const GrClip& clip,
2040 const SkIRect& clipBounds) {
joshualitt7b670db2015-07-09 13:25:02 -07002041 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002042
2043 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002044 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillipsf6703fa2015-09-01 05:36:47 -07002045 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002046 }
2047
2048 // Now flush big glyphs
robertphillipsf6703fa2015-09-01 05:36:47 -07002049 this->flushBigGlyphs(cacheBlob, dc, rt, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002050}
joshualitt79dfb2b2015-05-11 08:58:08 -07002051
2052///////////////////////////////////////////////////////////////////////////////////////////////////
2053
2054#ifdef GR_TEST_UTILS
2055
bsalomonabd30f52015-08-13 13:34:48 -07002056DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07002057 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -07002058 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -07002059 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07002060
2061 if (context->uniqueID() != gContextID) {
2062 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -07002063 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -07002064
joshualitt79dfb2b2015-05-11 08:58:08 -07002065 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
2066 // because we don't really want to have a gpu device here.
2067 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -07002068 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07002069 }
2070
2071 // create dummy render target
2072 GrSurfaceDesc desc;
2073 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2074 desc.fWidth = 1024;
2075 desc.fHeight = 1024;
2076 desc.fConfig = kRGBA_8888_GrPixelConfig;
joshualitt15732062015-05-13 12:15:14 -07002077 desc.fSampleCnt = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07002078 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, true, nullptr, 0));
joshualitt79dfb2b2015-05-11 08:58:08 -07002079 SkASSERT(texture);
halcanary96fcdcc2015-08-27 07:41:13 -07002080 SkASSERT(nullptr != texture->asRenderTarget());
joshualitt79dfb2b2015-05-11 08:58:08 -07002081 GrRenderTarget* rt = texture->asRenderTarget();
2082
2083 // Setup dummy SkPaint / GrPaint
2084 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07002085 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07002086 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07002087 skPaint.setColor(color);
2088 skPaint.setLCDRenderText(random->nextBool());
2089 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
2090 skPaint.setSubpixelText(random->nextBool());
2091
2092 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07002093 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -07002094 SkFAIL("couldn't convert paint\n");
2095 }
2096
2097 const char* text = "The quick brown fox jumps over the lazy dog.";
2098 int textLen = (int)strlen(text);
2099
2100 // Setup clip
2101 GrClip clip;
2102 SkIRect noClip = SkIRect::MakeLargest();
2103
2104 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
2105 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07002106 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt79dfb2b2015-05-11 08:58:08 -07002107 gTextContext->createDrawTextBlob(rt, clip, grPaint, skPaint, viewMatrix, text,
2108 static_cast<size_t>(textLen), 0, 0, noClip));
2109
2110 SkScalar transX = static_cast<SkScalar>(random->nextU());
2111 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07002112 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07002113 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
2114}
2115
2116#endif