blob: 09190c495b692527d10d5f927bac9699b44265e7 [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
robertphillips783a4da2015-11-19 14:00:02 -08001338 SkString dumpInfo() const override {
1339 SkString str;
1340
1341 for (int i = 0; i < fGeoCount; ++i) {
1342 str.appendf("%d: Color: 0x%08x Trans: %.2f,%.2f Runs: %d\n",
1343 i,
1344 fGeoData[i].fColor,
1345 fGeoData[i].fTransX,
1346 fGeoData[i].fTransY,
1347 fGeoData[i].fBlob->fRunCount);
1348 }
1349
1350 str.append(INHERITED::dumpInfo());
1351 return str;
1352 }
1353
joshualitt1d89e8d2015-04-01 12:40:54 -07001354 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001355 if (kColorBitmapMask_MaskType == fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001356 out->setUnknownFourComponents();
1357 } else {
1358 out->setKnownFourComponents(fBatch.fColor);
1359 }
1360 }
1361
1362 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001363 switch (fMaskType) {
1364 case kGrayscaleDistanceField_MaskType:
1365 case kGrayscaleCoverageMask_MaskType:
joshualitt1d89e8d2015-04-01 12:40:54 -07001366 out->setUnknownSingleComponent();
bsalomon265697d2015-07-22 10:17:26 -07001367 break;
1368 case kLCDCoverageMask_MaskType:
1369 case kLCDDistanceField_MaskType:
1370 out->setUnknownOpaqueFourComponents();
joshualitt1d89e8d2015-04-01 12:40:54 -07001371 out->setUsingLCDCoverage();
bsalomon265697d2015-07-22 10:17:26 -07001372 break;
1373 case kColorBitmapMask_MaskType:
1374 out->setKnownSingleComponent(0xff);
joshualitt1d89e8d2015-04-01 12:40:54 -07001375 }
1376 }
1377
bsalomone46f9fe2015-08-18 06:05:14 -07001378private:
bsalomon91d844d2015-08-10 10:47:29 -07001379 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001380 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -07001381 if (!opt.readsColor()) {
joshualitt416e14f2015-07-10 09:05:57 -07001382 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001383 }
bsalomon91d844d2015-08-10 10:47:29 -07001384 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt1d89e8d2015-04-01 12:40:54 -07001385
1386 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -07001387 fBatch.fColorIgnored = !opt.readsColor();
joshualitt416e14f2015-07-10 09:05:57 -07001388 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -07001389 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
1390 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt1d89e8d2015-04-01 12:40:54 -07001391 }
1392
bsalomonb5238a72015-05-05 07:49:49 -07001393 struct FlushInfo {
1394 SkAutoTUnref<const GrVertexBuffer> fVertexBuffer;
1395 SkAutoTUnref<const GrIndexBuffer> fIndexBuffer;
1396 int fGlyphsToFlush;
1397 int fVertexOffset;
1398 };
1399
bsalomon75398562015-08-17 12:55:38 -07001400 void onPrepareDraws(Target* target) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001401 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
1402 // TODO actually only invert if we don't have RGBA
1403 SkMatrix localMatrix;
1404 if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) {
1405 SkDebugf("Cannot invert viewmatrix\n");
1406 return;
1407 }
1408
bsalomon265697d2015-07-22 10:17:26 -07001409 GrTexture* texture = fFontCache->getTexture(this->maskFormat());
joshualitt62db8ba2015-04-09 08:22:37 -07001410 if (!texture) {
1411 SkDebugf("Could not allocate backing texture for atlas\n");
1412 return;
1413 }
1414
bsalomon265697d2015-07-22 10:17:26 -07001415 bool usesDistanceFields = this->usesDistanceFields();
1416 GrMaskFormat maskFormat = this->maskFormat();
1417 bool isLCD = this->isLCD();
1418
joshualitt9bd2daf2015-04-17 09:30:06 -07001419 SkAutoTUnref<const GrGeometryProcessor> gp;
bsalomon265697d2015-07-22 10:17:26 -07001420 if (usesDistanceFields) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001421 gp.reset(this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(),
1422 texture));
1423 } else {
1424 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
joshualitt9bd2daf2015-04-17 09:30:06 -07001425 gp.reset(GrBitmapTextGeoProc::Create(this->color(),
1426 texture,
1427 params,
bsalomon265697d2015-07-22 10:17:26 -07001428 maskFormat,
joshualittb8c241a2015-05-19 08:23:30 -07001429 localMatrix,
1430 this->usesLocalCoords()));
joshualitt9bd2daf2015-04-17 09:30:06 -07001431 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001432
bsalomonb5238a72015-05-05 07:49:49 -07001433 FlushInfo flushInfo;
1434 flushInfo.fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001435 size_t vertexStride = gp->getVertexStride();
bsalomon265697d2015-07-22 10:17:26 -07001436 SkASSERT(vertexStride == (usesDistanceFields ?
1437 get_vertex_stride_df(maskFormat, isLCD) :
1438 get_vertex_stride(maskFormat)));
joshualitt1d89e8d2015-04-01 12:40:54 -07001439
bsalomon75398562015-08-17 12:55:38 -07001440 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001441
1442 int glyphCount = this->numGlyphs();
bsalomon8415abe2015-05-04 11:41:41 -07001443 const GrVertexBuffer* vertexBuffer;
bsalomonb5238a72015-05-05 07:49:49 -07001444
bsalomon75398562015-08-17 12:55:38 -07001445 void* vertices = target->makeVertexSpace(vertexStride,
1446 glyphCount * kVerticesPerGlyph,
1447 &vertexBuffer,
1448 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -07001449 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
bsalomon75398562015-08-17 12:55:38 -07001450 flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
bsalomonb5238a72015-05-05 07:49:49 -07001451 if (!vertices || !flushInfo.fVertexBuffer) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001452 SkDebugf("Could not allocate vertices\n");
1453 return;
1454 }
1455
1456 unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices);
1457
joshualitt25ba7ea2015-04-21 07:49:49 -07001458 // We cache some values to avoid going to the glyphcache for the same fontScaler twice
1459 // in a row
halcanary96fcdcc2015-08-27 07:41:13 -07001460 const SkDescriptor* desc = nullptr;
1461 SkGlyphCache* cache = nullptr;
1462 GrFontScaler* scaler = nullptr;
1463 SkTypeface* typeface = nullptr;
joshualitt25ba7ea2015-04-21 07:49:49 -07001464
bsalomond602f4d2015-07-27 06:12:01 -07001465 for (int i = 0; i < fGeoCount; i++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001466 Geometry& args = fGeoData[i];
1467 Blob* blob = args.fBlob;
1468 Run& run = blob->fRuns[args.fRun];
1469 TextInfo& info = run.fSubRunInfo[args.fSubRun];
1470
bsalomon265697d2015-07-22 10:17:26 -07001471 uint64_t currentAtlasGen = fFontCache->atlasGeneration(maskFormat);
joshualitt7a9c45c2015-05-26 12:32:23 -07001472 bool regenerateTextureCoords = info.fAtlasGeneration != currentAtlasGen ||
joshualitt7e97b0b2015-07-31 15:18:08 -07001473 info.fStrike->isAbandoned();
joshualitt9bd2daf2015-04-17 09:30:06 -07001474 bool regenerateColors;
bsalomon265697d2015-07-22 10:17:26 -07001475 if (usesDistanceFields) {
1476 regenerateColors = !isLCD && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001477 } else {
bsalomon265697d2015-07-22 10:17:26 -07001478 regenerateColors = kA8_GrMaskFormat == maskFormat && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001479 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001480 bool regeneratePositions = args.fTransX != 0.f || args.fTransY != 0.f;
joshualitt1d89e8d2015-04-01 12:40:54 -07001481 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1482
1483 // We regenerate both texture coords and colors in the blob itself, and update the
1484 // atlas generation. If we don't end up purging any unused plots, we can avoid
1485 // regenerating the coords. We could take a finer grained approach to updating texture
1486 // coords but its not clear if the extra bookkeeping would offset any gains.
1487 // To avoid looping over the glyphs twice, we do one loop and conditionally update color
1488 // or coords as needed. One final note, if we have to break a run for an atlas eviction
1489 // then we can't really trust the atlas has all of the correct data. Atlas evictions
1490 // should be pretty rare, so we just always regenerate in those cases
joshualitt2a0e9f32015-04-13 06:12:21 -07001491 if (regenerateTextureCoords || regenerateColors || regeneratePositions) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001492 // first regenerate texture coordinates / colors if need be
joshualitt1d89e8d2015-04-01 12:40:54 -07001493 bool brokenRun = false;
joshualittae32c102015-04-21 09:37:57 -07001494
1495 // Because the GrBatchFontCache may evict the strike a blob depends on using for
1496 // generating its texture coords, we have to track whether or not the strike has
1497 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
1498 // otherwise we have to get the new strike, and use that to get the correct glyphs.
1499 // Because we do not have the packed ids, and thus can't look up our glyphs in the
1500 // new strike, we instead keep our ref to the old strike and use the packed ids from
1501 // it. These ids will still be valid as long as we hold the ref. When we are done
1502 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
1503 bool regenerateGlyphs = false;
halcanary96fcdcc2015-08-27 07:41:13 -07001504 GrBatchTextStrike* strike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -07001505 if (regenerateTextureCoords) {
joshualittb4c507e2015-04-08 08:07:59 -07001506 info.fBulkUseToken.reset();
joshualitt25ba7ea2015-04-21 07:49:49 -07001507
1508 // We can reuse if we have a valid strike and our descriptors / typeface are the
joshualitt94469302015-09-02 06:13:39 -07001509 // same. The override descriptor is only for the non distance field text within
1510 // a run
1511 const SkDescriptor* newDesc = (run.fOverrideDescriptor && !usesDistanceFields) ?
joshualitt97202d22015-04-22 13:47:02 -07001512 run.fOverrideDescriptor->getDesc() :
joshualitt25ba7ea2015-04-21 07:49:49 -07001513 run.fDescriptor.getDesc();
1514 if (!cache || !SkTypeface::Equal(typeface, run.fTypeface) ||
1515 !(desc->equals(*newDesc))) {
1516 if (cache) {
1517 SkGlyphCache::AttachCache(cache);
1518 }
1519 desc = newDesc;
1520 cache = SkGlyphCache::DetachCache(run.fTypeface, desc);
1521 scaler = GrTextContext::GetGrFontScaler(cache);
joshualitt7e97b0b2015-07-31 15:18:08 -07001522 strike = info.fStrike;
joshualitt25ba7ea2015-04-21 07:49:49 -07001523 typeface = run.fTypeface;
1524 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001525
joshualitt7e97b0b2015-07-31 15:18:08 -07001526 if (info.fStrike->isAbandoned()) {
joshualittae32c102015-04-21 09:37:57 -07001527 regenerateGlyphs = true;
1528 strike = fFontCache->getStrike(scaler);
1529 } else {
joshualitt7e97b0b2015-07-31 15:18:08 -07001530 strike = info.fStrike;
joshualittae32c102015-04-21 09:37:57 -07001531 }
1532 }
1533
1534 for (int glyphIdx = 0; glyphIdx < glyphCount; glyphIdx++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001535 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001536 size_t glyphOffset = glyphIdx + info.fGlyphStartIndex;
joshualitt6c2c2b02015-07-24 10:37:00 -07001537
1538 GrGlyph* glyph = blob->fGlyphs[glyphOffset];
1539 GrGlyph::PackedID id = glyph->fPackedID;
1540 const SkGlyph& skGlyph = scaler->grToSkGlyph(id);
joshualittae32c102015-04-21 09:37:57 -07001541 if (regenerateGlyphs) {
1542 // Get the id from the old glyph, and use the new strike to lookup
1543 // the glyph.
joshualitt76cc6572015-07-31 05:51:45 -07001544 blob->fGlyphs[glyphOffset] = strike->getGlyph(skGlyph, id, maskFormat,
1545 scaler);
joshualittae32c102015-04-21 09:37:57 -07001546 }
1547 glyph = blob->fGlyphs[glyphOffset];
joshualitt1d89e8d2015-04-01 12:40:54 -07001548 SkASSERT(glyph);
joshualitt65e96b42015-07-31 11:45:22 -07001549 SkASSERT(id == glyph->fPackedID);
1550 // We want to be able to assert this but cannot for testing purposes.
1551 // once skbug:4143 has landed we can revist this assert
1552 //SkASSERT(glyph->fMaskFormat == this->maskFormat());
joshualitt1d89e8d2015-04-01 12:40:54 -07001553
1554 if (!fFontCache->hasGlyph(glyph) &&
bsalomon75398562015-08-17 12:55:38 -07001555 !strike->addGlyphToAtlas(target, glyph, scaler, skGlyph, maskFormat)) {
1556 this->flush(target, &flushInfo);
1557 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001558 brokenRun = glyphIdx > 0;
1559
bsalomon75398562015-08-17 12:55:38 -07001560 SkDEBUGCODE(bool success =) strike->addGlyphToAtlas(target,
joshualittae32c102015-04-21 09:37:57 -07001561 glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001562 scaler,
joshualitt4f19ca32015-07-30 07:59:20 -07001563 skGlyph,
1564 maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001565 SkASSERT(success);
1566 }
joshualittb4c507e2015-04-08 08:07:59 -07001567 fFontCache->addGlyphToBulkAndSetUseToken(&info.fBulkUseToken, glyph,
bsalomon75398562015-08-17 12:55:38 -07001568 target->currentToken());
joshualitt1d89e8d2015-04-01 12:40:54 -07001569
1570 // Texture coords are the last vertex attribute so we get a pointer to the
1571 // first one and then map with stride in regenerateTextureCoords
1572 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1573 vertex += info.fVertexStartIndex;
1574 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1575 vertex += vertexStride - sizeof(SkIPoint16);
1576
1577 this->regenerateTextureCoords(glyph, vertex, vertexStride);
1578 }
1579
1580 if (regenerateColors) {
1581 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1582 vertex += info.fVertexStartIndex;
1583 vertex += vertexStride * glyphIdx * kVerticesPerGlyph + sizeof(SkPoint);
1584 this->regenerateColors(vertex, vertexStride, args.fColor);
1585 }
1586
joshualitt2a0e9f32015-04-13 06:12:21 -07001587 if (regeneratePositions) {
1588 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1589 vertex += info.fVertexStartIndex;
1590 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1591 SkScalar transX = args.fTransX;
1592 SkScalar transY = args.fTransY;
1593 this->regeneratePositions(vertex, vertexStride, transX, transY);
1594 }
bsalomonb5238a72015-05-05 07:49:49 -07001595 flushInfo.fGlyphsToFlush++;
joshualitt1d89e8d2015-04-01 12:40:54 -07001596 }
1597
joshualitt2a0e9f32015-04-13 06:12:21 -07001598 // We my have changed the color so update it here
1599 run.fColor = args.fColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07001600 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001601 if (regenerateGlyphs) {
joshualitt7e97b0b2015-07-31 15:18:08 -07001602 info.fStrike.reset(SkRef(strike));
joshualittae32c102015-04-21 09:37:57 -07001603 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001604 info.fAtlasGeneration = brokenRun ? GrBatchAtlas::kInvalidAtlasGeneration :
bsalomon265697d2015-07-22 10:17:26 -07001605 fFontCache->atlasGeneration(maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001606 }
1607 } else {
bsalomonb5238a72015-05-05 07:49:49 -07001608 flushInfo.fGlyphsToFlush += glyphCount;
joshualittb4c507e2015-04-08 08:07:59 -07001609
1610 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1611 // have a valid atlas generation
bsalomon75398562015-08-17 12:55:38 -07001612 fFontCache->setUseTokenBulk(info.fBulkUseToken, target->currentToken(), maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001613 }
1614
1615 // now copy all vertices
1616 size_t byteCount = info.fVertexEndIndex - info.fVertexStartIndex;
1617 memcpy(currVertex, blob->fVertices + info.fVertexStartIndex, byteCount);
1618
1619 currVertex += byteCount;
1620 }
joshualitt25ba7ea2015-04-21 07:49:49 -07001621 // Make sure to attach the last cache if applicable
1622 if (cache) {
1623 SkGlyphCache::AttachCache(cache);
1624 }
bsalomon75398562015-08-17 12:55:38 -07001625 this->flush(target, &flushInfo);
joshualitt1d89e8d2015-04-01 12:40:54 -07001626 }
1627
reed1b55a962015-09-17 20:16:13 -07001628 TextBatch() : INHERITED(ClassID()) {} // initialized in factory functions.
joshualittad802c62015-04-15 05:31:57 -07001629
bsalomon265697d2015-07-22 10:17:26 -07001630 ~TextBatch() {
bsalomond602f4d2015-07-27 06:12:01 -07001631 for (int i = 0; i < fGeoCount; i++) {
joshualittad802c62015-04-15 05:31:57 -07001632 fGeoData[i].fBlob->unref();
1633 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001634 }
1635
bsalomon265697d2015-07-22 10:17:26 -07001636 GrMaskFormat maskFormat() const {
1637 switch (fMaskType) {
1638 case kLCDCoverageMask_MaskType:
1639 return kA565_GrMaskFormat;
1640 case kColorBitmapMask_MaskType:
1641 return kARGB_GrMaskFormat;
1642 case kGrayscaleCoverageMask_MaskType:
1643 case kGrayscaleDistanceField_MaskType:
1644 case kLCDDistanceField_MaskType:
1645 return kA8_GrMaskFormat;
1646 }
1647 return kA8_GrMaskFormat; // suppress warning
1648 }
1649
1650 bool usesDistanceFields() const {
1651 return kGrayscaleDistanceField_MaskType == fMaskType ||
1652 kLCDDistanceField_MaskType == fMaskType;
1653 }
1654
1655 bool isLCD() const {
1656 return kLCDCoverageMask_MaskType == fMaskType ||
1657 kLCDDistanceField_MaskType == fMaskType;
1658 }
1659
joshualitt1d89e8d2015-04-01 12:40:54 -07001660 void regenerateTextureCoords(GrGlyph* glyph, intptr_t vertex, size_t vertexStride) {
1661 int width = glyph->fBounds.width();
1662 int height = glyph->fBounds.height();
joshualitt1d89e8d2015-04-01 12:40:54 -07001663
joshualitt9bd2daf2015-04-17 09:30:06 -07001664 int u0, v0, u1, v1;
bsalomon265697d2015-07-22 10:17:26 -07001665 if (this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001666 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
1667 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
1668 u1 = u0 + width - 2 * SK_DistanceFieldInset;
1669 v1 = v0 + height - 2 * SK_DistanceFieldInset;
1670 } else {
1671 u0 = glyph->fAtlasLocation.fX;
1672 v0 = glyph->fAtlasLocation.fY;
1673 u1 = u0 + width;
1674 v1 = v0 + height;
1675 }
1676
joshualitt1d89e8d2015-04-01 12:40:54 -07001677 SkIPoint16* textureCoords;
1678 // V0
1679 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1680 textureCoords->set(u0, v0);
1681 vertex += vertexStride;
1682
1683 // V1
1684 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1685 textureCoords->set(u0, v1);
1686 vertex += vertexStride;
1687
1688 // V2
1689 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1690 textureCoords->set(u1, v1);
1691 vertex += vertexStride;
1692
1693 // V3
1694 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1695 textureCoords->set(u1, v0);
1696 }
1697
1698 void regenerateColors(intptr_t vertex, size_t vertexStride, GrColor color) {
1699 for (int i = 0; i < kVerticesPerGlyph; i++) {
1700 SkColor* vcolor = reinterpret_cast<SkColor*>(vertex);
1701 *vcolor = color;
1702 vertex += vertexStride;
1703 }
1704 }
1705
joshualitt2a0e9f32015-04-13 06:12:21 -07001706 void regeneratePositions(intptr_t vertex, size_t vertexStride, SkScalar transX,
1707 SkScalar transY) {
1708 for (int i = 0; i < kVerticesPerGlyph; i++) {
1709 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
1710 point->fX += transX;
1711 point->fY += transY;
1712 vertex += vertexStride;
1713 }
1714 }
1715
bsalomon75398562015-08-17 12:55:38 -07001716 void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) {
bsalomoncb8979d2015-05-05 09:51:38 -07001717 GrVertices vertices;
bsalomonb5238a72015-05-05 07:49:49 -07001718 int maxGlyphsPerDraw = flushInfo->fIndexBuffer->maxQuads();
bsalomoncb8979d2015-05-05 09:51:38 -07001719 vertices.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001720 flushInfo->fIndexBuffer, flushInfo->fVertexOffset,
bsalomone64eb572015-05-07 11:35:55 -07001721 kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
bsalomonb5238a72015-05-05 07:49:49 -07001722 maxGlyphsPerDraw);
bsalomon75398562015-08-17 12:55:38 -07001723 target->draw(vertices);
bsalomonb5238a72015-05-05 07:49:49 -07001724 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
1725 flushInfo->fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001726 }
1727
1728 GrColor color() const { return fBatch.fColor; }
1729 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1730 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1731 int numGlyphs() const { return fBatch.fNumGlyphs; }
1732
bsalomoncb02b382015-08-12 11:14:50 -07001733 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001734 TextBatch* that = t->cast<TextBatch>();
1735 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1736 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001737 return false;
1738 }
1739
bsalomon265697d2015-07-22 10:17:26 -07001740 if (fMaskType != that->fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001741 return false;
1742 }
1743
bsalomon265697d2015-07-22 10:17:26 -07001744 if (!this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001745 // TODO we can often batch across LCD text if we have dual source blending and don't
1746 // have to use the blend constant
bsalomon265697d2015-07-22 10:17:26 -07001747 if (kGrayscaleCoverageMask_MaskType != fMaskType && this->color() != that->color()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001748 return false;
1749 }
joshualitt9bd2daf2015-04-17 09:30:06 -07001750 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1751 return false;
1752 }
1753 } else {
joshualitt9bd2daf2015-04-17 09:30:06 -07001754 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1755 return false;
1756 }
1757
1758 if (fFilteredColor != that->fFilteredColor) {
1759 return false;
1760 }
1761
joshualitt9bd2daf2015-04-17 09:30:06 -07001762 if (fUseBGR != that->fUseBGR) {
1763 return false;
1764 }
1765
joshualitt9bd2daf2015-04-17 09:30:06 -07001766 // TODO see note above
bsalomon265697d2015-07-22 10:17:26 -07001767 if (kLCDDistanceField_MaskType == fMaskType && this->color() != that->color()) {
joshualitt0fe04a22015-08-25 12:05:50 -07001768 return false;
joshualitt9bd2daf2015-04-17 09:30:06 -07001769 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001770 }
1771
1772 fBatch.fNumGlyphs += that->numGlyphs();
joshualittad802c62015-04-15 05:31:57 -07001773
bsalomond602f4d2015-07-27 06:12:01 -07001774 // Reallocate space for geo data if necessary and then import that's geo data.
1775 int newGeoCount = that->fGeoCount + fGeoCount;
1776 // We assume (and here enforce) that the allocation size is the smallest power of two that
1777 // is greater than or equal to the number of geometries (and at least
1778 // kMinGeometryAllocated).
1779 int newAllocSize = GrNextPow2(newGeoCount);
1780 int currAllocSize = SkTMax<int>(kMinGeometryAllocated, GrNextPow2(fGeoCount));
1781
bsalomon16ed6ad2015-07-29 06:54:33 -07001782 if (newGeoCount > currAllocSize) {
bsalomond602f4d2015-07-27 06:12:01 -07001783 fGeoData.realloc(newAllocSize);
joshualittad802c62015-04-15 05:31:57 -07001784 }
1785
bsalomond602f4d2015-07-27 06:12:01 -07001786 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
bsalomon1c634362015-07-27 07:00:00 -07001787 // We steal the ref on the blobs from the other TextBatch and set its count to 0 so that
1788 // it doesn't try to unref them.
1789#ifdef SK_DEBUG
1790 for (int i = 0; i < that->fGeoCount; ++i) {
1791 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
joshualittad802c62015-04-15 05:31:57 -07001792 }
bsalomon1c634362015-07-27 07:00:00 -07001793#endif
1794 that->fGeoCount = 0;
bsalomond602f4d2015-07-27 06:12:01 -07001795 fGeoCount = newGeoCount;
joshualitt99c7c072015-05-01 13:43:30 -07001796
1797 this->joinBounds(that->bounds());
joshualitt1d89e8d2015-04-01 12:40:54 -07001798 return true;
1799 }
1800
joshualitt9bd2daf2015-04-17 09:30:06 -07001801 // TODO just use class params
1802 // TODO trying to figure out why lcd is so whack
1803 GrGeometryProcessor* setupDfProcessor(const SkMatrix& viewMatrix, SkColor filteredColor,
1804 GrColor color, GrTexture* texture) {
1805 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
bsalomon265697d2015-07-22 10:17:26 -07001806 bool isLCD = this->isLCD();
joshualitt9bd2daf2015-04-17 09:30:06 -07001807 // set up any flags
bsalomon265697d2015-07-22 10:17:26 -07001808 uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
joshualitt9bd2daf2015-04-17 09:30:06 -07001809
1810 // see if we need to create a new effect
bsalomon265697d2015-07-22 10:17:26 -07001811 if (isLCD) {
1812 flags |= kUseLCD_DistanceFieldEffectFlag;
1813 flags |= viewMatrix.rectStaysRect() ? kRectToRect_DistanceFieldEffectFlag : 0;
1814 flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0;
1815
joshualitt9bd2daf2015-04-17 09:30:06 -07001816 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
1817
1818 float redCorrection =
1819 (*fDistanceAdjustTable)[GrColorUnpackR(colorNoPreMul) >> kDistanceAdjustLumShift];
1820 float greenCorrection =
1821 (*fDistanceAdjustTable)[GrColorUnpackG(colorNoPreMul) >> kDistanceAdjustLumShift];
1822 float blueCorrection =
1823 (*fDistanceAdjustTable)[GrColorUnpackB(colorNoPreMul) >> kDistanceAdjustLumShift];
1824 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
1825 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(redCorrection,
1826 greenCorrection,
1827 blueCorrection);
1828
1829 return GrDistanceFieldLCDTextGeoProc::Create(color,
1830 viewMatrix,
1831 texture,
1832 params,
1833 widthAdjust,
joshualittb8c241a2015-05-19 08:23:30 -07001834 flags,
1835 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07001836 } else {
1837 flags |= kColorAttr_DistanceFieldEffectFlag;
joshualitt9bd2daf2015-04-17 09:30:06 -07001838#ifdef SK_GAMMA_APPLY_TO_A8
robertphillips9fc82752015-06-19 04:46:45 -07001839 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, filteredColor);
joshualitt9bd2daf2015-04-17 09:30:06 -07001840 float correction = (*fDistanceAdjustTable)[lum >> kDistanceAdjustLumShift];
1841 return GrDistanceFieldA8TextGeoProc::Create(color,
1842 viewMatrix,
1843 texture,
1844 params,
1845 correction,
joshualittb8c241a2015-05-19 08:23:30 -07001846 flags,
1847 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07001848#else
1849 return GrDistanceFieldA8TextGeoProc::Create(color,
1850 viewMatrix,
1851 texture,
1852 params,
joshualittb8c241a2015-05-19 08:23:30 -07001853 flags,
1854 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07001855#endif
1856 }
1857
1858 }
1859
joshualitt1d89e8d2015-04-01 12:40:54 -07001860 struct BatchTracker {
1861 GrColor fColor;
1862 SkMatrix fViewMatrix;
1863 bool fUsesLocalCoords;
1864 bool fColorIgnored;
1865 bool fCoverageIgnored;
1866 int fNumGlyphs;
1867 };
1868
1869 BatchTracker fBatch;
bsalomond602f4d2015-07-27 06:12:01 -07001870 // The minimum number of Geometry we will try to allocate.
1871 enum { kMinGeometryAllocated = 4 };
1872 SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData;
1873 int fGeoCount;
bsalomon265697d2015-07-22 10:17:26 -07001874
1875 enum MaskType {
1876 kGrayscaleCoverageMask_MaskType,
1877 kLCDCoverageMask_MaskType,
1878 kColorBitmapMask_MaskType,
1879 kGrayscaleDistanceField_MaskType,
1880 kLCDDistanceField_MaskType,
1881 } fMaskType;
1882 bool fUseBGR; // fold this into the enum?
1883
joshualitt1d89e8d2015-04-01 12:40:54 -07001884 GrBatchFontCache* fFontCache;
joshualitt9bd2daf2015-04-17 09:30:06 -07001885
1886 // Distance field properties
robertphillips9fc82752015-06-19 04:46:45 -07001887 SkAutoTUnref<const DistanceAdjustTable> fDistanceAdjustTable;
joshualitt9bd2daf2015-04-17 09:30:06 -07001888 SkColor fFilteredColor;
reed1b55a962015-09-17 20:16:13 -07001889
1890 typedef GrVertexBatch INHERITED;
joshualitt1d89e8d2015-04-01 12:40:54 -07001891};
1892
robertphillipsf6703fa2015-09-01 05:36:47 -07001893void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc, GrRenderTarget* rt,
herbe59124e2015-11-18 10:54:39 -08001894 const SkTextBlobRunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -07001895 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07001896 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
1897 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
1898 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07001899
joshualitt9a27e632015-04-06 10:53:36 -07001900 size_t textLen = it.glyphCount() * sizeof(uint16_t);
1901 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07001902
joshualitt9a27e632015-04-06 10:53:36 -07001903 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07001904
joshualitt9a27e632015-04-06 10:53:36 -07001905 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
1906 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07001907 }
1908
robertphillipsfcf78292015-06-19 11:49:52 -07001909 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07001910
1911 switch (it.positioning()) {
1912 case SkTextBlob::kDefault_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -07001913 this->drawTextAsPath(dc, rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001914 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001915 textLen, x + offset.x(), y + offset.y(), clipBounds);
1916 break;
1917 case SkTextBlob::kHorizontal_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -07001918 this->drawPosTextAsPath(dc, rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001919 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001920 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
1921 clipBounds);
1922 break;
1923 case SkTextBlob::kFull_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -07001924 this->drawPosTextAsPath(dc, rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001925 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07001926 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
1927 break;
1928 }
1929}
1930
bsalomonabd30f52015-08-13 13:34:48 -07001931inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -07001932GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -07001933 int glyphCount, int run, int subRun,
1934 GrColor color, SkScalar transX, SkScalar transY,
1935 const SkPaint& skPaint) {
1936 GrMaskFormat format = info.fMaskFormat;
1937 GrColor subRunColor;
1938 if (kARGB_GrMaskFormat == format) {
1939 uint8_t paintAlpha = skPaint.getAlpha();
1940 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
1941 } else {
1942 subRunColor = color;
1943 }
1944
bsalomon265697d2015-07-22 10:17:26 -07001945 TextBatch* batch;
joshualitt79dfb2b2015-05-11 08:58:08 -07001946 if (info.fDrawAsDistanceFields) {
1947 SkColor filteredColor;
1948 SkColorFilter* colorFilter = skPaint.getColorFilter();
1949 if (colorFilter) {
1950 filteredColor = colorFilter->filterColor(skPaint.getColor());
1951 } else {
1952 filteredColor = skPaint.getColor();
1953 }
robertphillipsfcf78292015-06-19 11:49:52 -07001954 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
bsalomon265697d2015-07-22 10:17:26 -07001955 batch = TextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
1956 fDistanceAdjustTable, filteredColor,
1957 info.fUseLCDText, useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07001958 } else {
bsalomon265697d2015-07-22 10:17:26 -07001959 batch = TextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07001960 }
bsalomon265697d2015-07-22 10:17:26 -07001961 TextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07001962 geometry.fBlob = SkRef(cacheBlob);
1963 geometry.fRun = run;
1964 geometry.fSubRun = subRun;
1965 geometry.fColor = subRunColor;
1966 geometry.fTransX = transX;
1967 geometry.fTransY = transY;
1968 batch->init();
1969
1970 return batch;
1971}
1972
robertphillipsf6703fa2015-09-01 05:36:47 -07001973inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07001974 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07001975 SkScalar transX, SkScalar transY,
1976 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07001977 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
1978 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
1979 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1980 if (0 == glyphCount) {
1981 continue;
1982 }
1983
bsalomonabd30f52015-08-13 13:34:48 -07001984 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
1985 subRun, color, transX, transY,
1986 skPaint));
robertphillipsf6703fa2015-09-01 05:36:47 -07001987 dc->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07001988 }
1989}
1990
herbe59124e2015-11-18 10:54:39 -08001991inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07001992 GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -07001993 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07001994 SkScalar transX, SkScalar transY,
1995 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07001996 if (!cacheBlob->fBigGlyphs.count()) {
1997 return;
1998 }
1999
joshualitt9a27e632015-04-06 10:53:36 -07002000 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07002001 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07002002 bigGlyph.fVx += transX;
2003 bigGlyph.fVy += transY;
joshualitt0fe04a22015-08-25 12:05:50 -07002004 SkMatrix ctm;
2005 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
2006 ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
2007 if (bigGlyph.fApplyVM) {
2008 ctm.postConcat(cacheBlob->fViewMatrix);
2009 }
joshualittfc072562015-05-13 12:15:06 -07002010
robertphillipsf6703fa2015-09-01 05:36:47 -07002011 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, bigGlyph.fPath,
joshualitt0fe04a22015-08-25 12:05:50 -07002012 skPaint, ctm, nullptr, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07002013 }
2014}
joshualitt9a27e632015-04-06 10:53:36 -07002015
robertphillips2334fb62015-06-17 05:43:33 -07002016void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -07002017 GrAtlasTextBlob* cacheBlob,
herbe59124e2015-11-18 10:54:39 -08002018 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07002019 GrRenderTarget* rt,
2020 const SkPaint& skPaint,
2021 const GrPaint& grPaint,
2022 SkDrawFilter* drawFilter,
2023 const GrClip& clip,
2024 const SkMatrix& viewMatrix,
2025 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07002026 SkScalar x, SkScalar y,
2027 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07002028 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
2029 // it as paths
joshualitt7b670db2015-07-09 13:25:02 -07002030 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002031
2032 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002033
halcanary33779752015-10-27 14:01:05 -07002034 SkTextBlobRunIterator it(blob);
joshualitt9a27e632015-04-06 10:53:36 -07002035 for (int run = 0; !it.done(); it.next(), run++) {
2036 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillipsf6703fa2015-09-01 05:36:47 -07002037 this->flushRunAsPaths(dc, rt, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07002038 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07002039 continue;
2040 }
joshualitt2a0e9f32015-04-13 06:12:21 -07002041 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillipsf6703fa2015-09-01 05:36:47 -07002042 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07002043 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002044 }
2045
2046 // Now flush big glyphs
robertphillipsf6703fa2015-09-01 05:36:47 -07002047 this->flushBigGlyphs(cacheBlob, dc, rt, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002048}
2049
joshualitt374b2f72015-07-21 08:05:03 -07002050void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07002051 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07002052 GrRenderTarget* rt,
2053 const SkPaint& skPaint,
2054 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07002055 const GrClip& clip,
2056 const SkIRect& clipBounds) {
joshualitt7b670db2015-07-09 13:25:02 -07002057 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002058
2059 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002060 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillipsf6703fa2015-09-01 05:36:47 -07002061 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002062 }
2063
2064 // Now flush big glyphs
robertphillipsf6703fa2015-09-01 05:36:47 -07002065 this->flushBigGlyphs(cacheBlob, dc, rt, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002066}
joshualitt79dfb2b2015-05-11 08:58:08 -07002067
2068///////////////////////////////////////////////////////////////////////////////////////////////////
2069
2070#ifdef GR_TEST_UTILS
2071
bsalomonabd30f52015-08-13 13:34:48 -07002072DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07002073 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -07002074 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -07002075 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07002076
2077 if (context->uniqueID() != gContextID) {
2078 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -07002079 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -07002080
joshualitt79dfb2b2015-05-11 08:58:08 -07002081 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
2082 // because we don't really want to have a gpu device here.
2083 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -07002084 gTextContext = GrAtlasTextContext::Create(context, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07002085 }
2086
2087 // create dummy render target
2088 GrSurfaceDesc desc;
2089 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2090 desc.fWidth = 1024;
2091 desc.fHeight = 1024;
2092 desc.fConfig = kRGBA_8888_GrPixelConfig;
joshualitt15732062015-05-13 12:15:14 -07002093 desc.fSampleCnt = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07002094 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, true, nullptr, 0));
joshualitt79dfb2b2015-05-11 08:58:08 -07002095 SkASSERT(texture);
halcanary96fcdcc2015-08-27 07:41:13 -07002096 SkASSERT(nullptr != texture->asRenderTarget());
joshualitt79dfb2b2015-05-11 08:58:08 -07002097 GrRenderTarget* rt = texture->asRenderTarget();
2098
2099 // Setup dummy SkPaint / GrPaint
2100 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07002101 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07002102 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07002103 skPaint.setColor(color);
2104 skPaint.setLCDRenderText(random->nextBool());
2105 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
2106 skPaint.setSubpixelText(random->nextBool());
2107
2108 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07002109 if (!SkPaintToGrPaint(context, skPaint, viewMatrix, &grPaint)) {
joshualitt79dfb2b2015-05-11 08:58:08 -07002110 SkFAIL("couldn't convert paint\n");
2111 }
2112
2113 const char* text = "The quick brown fox jumps over the lazy dog.";
2114 int textLen = (int)strlen(text);
2115
2116 // Setup clip
2117 GrClip clip;
2118 SkIRect noClip = SkIRect::MakeLargest();
2119
2120 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
2121 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07002122 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt79dfb2b2015-05-11 08:58:08 -07002123 gTextContext->createDrawTextBlob(rt, clip, grPaint, skPaint, viewMatrix, text,
2124 static_cast<size_t>(textLen), 0, 0, noClip));
2125
2126 SkScalar transX = static_cast<SkScalar>(random->nextU());
2127 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07002128 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07002129 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
2130}
2131
2132#endif