blob: fbfb2e1c94d26034ebf470444abfe23f4b5a6c68 [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"
30#include "SkGlyphCache.h"
31#include "SkGpuDevice.h"
32#include "SkGr.h"
33#include "SkPath.h"
34#include "SkRTConf.h"
35#include "SkStrokeRec.h"
36#include "SkTextBlob.h"
37#include "SkTextMapStateProc.h"
38
bsalomon16b99132015-08-13 14:55:50 -070039#include "batches/GrVertexBatch.h"
joshualitt74417822015-08-07 11:42:16 -070040
joshualitt1d89e8d2015-04-01 12:40:54 -070041#include "effects/GrBitmapTextGeoProc.h"
joshualitt9bd2daf2015-04-17 09:30:06 -070042#include "effects/GrDistanceFieldGeoProc.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070043
44namespace {
45static const size_t kLCDTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
46
47// position + local coord
48static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
49
50static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
51
joshualitt9bd2daf2015-04-17 09:30:06 -070052static const int kMinDFFontSize = 18;
53static const int kSmallDFFontSize = 32;
54static const int kSmallDFFontLimit = 32;
55static const int kMediumDFFontSize = 72;
56static const int kMediumDFFontLimit = 72;
57static const int kLargeDFFontSize = 162;
jvanverth97c595f2015-06-19 11:06:28 -070058#ifdef SK_BUILD_FOR_ANDROID
59static const int kLargeDFFontLimit = 384;
60#else
joshualitta7c63892015-04-21 13:24:37 -070061static const int kLargeDFFontLimit = 2 * kLargeDFFontSize;
jvanverth97c595f2015-06-19 11:06:28 -070062#endif
joshualitt9bd2daf2015-04-17 09:30:06 -070063
64SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize = 8;)
65static const int kDistanceAdjustLumShift = 5;
66
joshualitt1d89e8d2015-04-01 12:40:54 -070067static const int kVerticesPerGlyph = 4;
68static const int kIndicesPerGlyph = 6;
69
70static size_t get_vertex_stride(GrMaskFormat maskFormat) {
71 switch (maskFormat) {
72 case kA8_GrMaskFormat:
73 return kGrayTextVASize;
74 case kARGB_GrMaskFormat:
75 return kColorTextVASize;
76 default:
77 return kLCDTextVASize;
78 }
79}
80
joshualitt9bd2daf2015-04-17 09:30:06 -070081static size_t get_vertex_stride_df(GrMaskFormat maskFormat, bool useLCDText) {
82 SkASSERT(maskFormat == kA8_GrMaskFormat);
83 if (useLCDText) {
84 return kLCDTextVASize;
85 } else {
86 return kGrayTextVASize;
87 }
88}
89
90static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
91 unsigned r = SkColorGetR(c);
92 unsigned g = SkColorGetG(c);
93 unsigned b = SkColorGetB(c);
94 return GrColorPackRGBA(r, g, b, 0xff);
95}
96
joshualitt1d89e8d2015-04-01 12:40:54 -070097};
98
joshualittdbd35932015-04-02 09:19:04 -070099GrAtlasTextContext::GrAtlasTextContext(GrContext* context,
robertphillips2334fb62015-06-17 05:43:33 -0700100 GrDrawContext* drawContext,
robertphillipsfcf78292015-06-19 11:49:52 -0700101 const SkSurfaceProps& surfaceProps)
102 : INHERITED(context, drawContext, surfaceProps)
robertphillips9fc82752015-06-19 04:46:45 -0700103 , fDistanceAdjustTable(SkNEW(DistanceAdjustTable)) {
joshualittb7133be2015-04-08 09:08:31 -0700104 // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest
105 // vertexStride
106 SK_COMPILE_ASSERT(kGrayTextVASize >= kColorTextVASize && kGrayTextVASize >= kLCDTextVASize,
107 vertex_attribute_changed);
joshualitt1d89e8d2015-04-01 12:40:54 -0700108 fCurrStrike = NULL;
joshualittb7133be2015-04-08 09:08:31 -0700109 fCache = context->getTextBlobCache();
joshualitt9bd2daf2015-04-17 09:30:06 -0700110}
111
robertphillips9fc82752015-06-19 04:46:45 -0700112void GrAtlasTextContext::DistanceAdjustTable::buildDistanceAdjustTable() {
joshualitt9bd2daf2015-04-17 09:30:06 -0700113
114 // This is used for an approximation of the mask gamma hack, used by raster and bitmap
115 // text. The mask gamma hack is based off of guessing what the blend color is going to
116 // be, and adjusting the mask so that when run through the linear blend will
117 // produce the value closest to the desired result. However, in practice this means
118 // that the 'adjusted' mask is just increasing or decreasing the coverage of
119 // the mask depending on what it is thought it will blit against. For black (on
120 // assumed white) this means that coverages are decreased (on a curve). For white (on
121 // assumed black) this means that coverages are increased (on a a curve). At
122 // middle (perceptual) gray (which could be blit against anything) the coverages
123 // remain the same.
124 //
125 // The idea here is that instead of determining the initial (real) coverage and
126 // then adjusting that coverage, we determine an adjusted coverage directly by
127 // essentially manipulating the geometry (in this case, the distance to the glyph
128 // edge). So for black (on assumed white) this thins a bit; for white (on
129 // assumed black) this fake bolds the geometry a bit.
130 //
131 // The distance adjustment is calculated by determining the actual coverage value which
132 // when fed into in the mask gamma table gives us an 'adjusted coverage' value of 0.5. This
133 // actual coverage value (assuming it's between 0 and 1) corresponds to a distance from the
134 // actual edge. So by subtracting this distance adjustment and computing without the
135 // the coverage adjustment we should get 0.5 coverage at the same point.
136 //
137 // This has several implications:
138 // For non-gray lcd smoothed text, each subpixel essentially is using a
139 // slightly different geometry.
140 //
141 // For black (on assumed white) this may not cover some pixels which were
142 // previously covered; however those pixels would have been only slightly
143 // covered and that slight coverage would have been decreased anyway. Also, some pixels
144 // which were previously fully covered may no longer be fully covered.
145 //
146 // For white (on assumed black) this may cover some pixels which weren't
147 // previously covered at all.
148
149 int width, height;
150 size_t size;
151
152#ifdef SK_GAMMA_CONTRAST
153 SkScalar contrast = SK_GAMMA_CONTRAST;
154#else
155 SkScalar contrast = 0.5f;
156#endif
robertphillips9fc82752015-06-19 04:46:45 -0700157 SkScalar paintGamma = SK_GAMMA_EXPONENT;
158 SkScalar deviceGamma = SK_GAMMA_EXPONENT;
joshualitt9bd2daf2015-04-17 09:30:06 -0700159
160 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
161 &width, &height);
162
163 SkASSERT(kExpectedDistanceAdjustTableSize == height);
164 fTable = SkNEW_ARRAY(SkScalar, height);
165
166 SkAutoTArray<uint8_t> data((int)size);
167 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
168
169 // find the inverse points where we cross 0.5
170 // binsearch might be better, but we only need to do this once on creation
171 for (int row = 0; row < height; ++row) {
172 uint8_t* rowPtr = data.get() + row*width;
173 for (int col = 0; col < width - 1; ++col) {
174 if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) {
175 // compute point where a mask value will give us a result of 0.5
176 float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPtr[col]);
177 float borderAlpha = (col + interp) / 255.f;
178
179 // compute t value for that alpha
180 // this is an approximate inverse for smoothstep()
181 float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5.0f) / 3.0f;
182
183 // compute distance which gives us that t value
184 const float kDistanceFieldAAFactor = 0.65f; // should match SK_DistanceFieldAAFactor
185 float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor;
186
187 fTable[row] = d;
188 break;
189 }
190 }
191 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700192}
193
joshualittdbd35932015-04-02 09:19:04 -0700194GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context,
robertphillips2334fb62015-06-17 05:43:33 -0700195 GrDrawContext* drawContext,
robertphillipsfcf78292015-06-19 11:49:52 -0700196 const SkSurfaceProps& surfaceProps) {
197 return SkNEW_ARGS(GrAtlasTextContext, (context, drawContext, surfaceProps));
joshualitt1d89e8d2015-04-01 12:40:54 -0700198}
199
joshualittdbd35932015-04-02 09:19:04 -0700200bool GrAtlasTextContext::canDraw(const GrRenderTarget*,
201 const GrClip&,
202 const GrPaint&,
203 const SkPaint& skPaint,
204 const SkMatrix& viewMatrix) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700205 return this->canDrawAsDistanceFields(skPaint, viewMatrix) ||
206 !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -0700207}
208
joshualitt9e36c1a2015-04-14 12:17:27 -0700209GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
210 GrColor canonicalColor = paint.computeLuminanceColor();
211 if (lcd) {
212 // This is the correct computation, but there are tons of cases where LCD can be overridden.
213 // For now we just regenerate if any run in a textblob has LCD.
214 // TODO figure out where all of these overrides are and see if we can incorporate that logic
215 // at a higher level *OR* use sRGB
216 SkASSERT(false);
217 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
218 } else {
219 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
220 // gamma corrected masks anyways, nor color
221 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
222 SkColorGetG(canonicalColor),
223 SkColorGetB(canonicalColor));
224 // reduce to our finite number of bits
225 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
226 }
227 return canonicalColor;
228}
229
230// TODO if this function ever shows up in profiling, then we can compute this value when the
231// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
232// run so this is not a big deal to compute here.
233bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) {
234 SkTextBlob::RunIterator it(blob);
235 for (; !it.done(); it.next()) {
236 if (it.isLCD()) {
237 return true;
238 }
239 }
240 return false;
241}
242
joshualitt2a0e9f32015-04-13 06:12:21 -0700243bool GrAtlasTextContext::MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY,
joshualitt374b2f72015-07-21 08:05:03 -0700244 const GrAtlasTextBlob& blob, const SkPaint& paint,
joshualitt53b5f442015-04-13 06:33:59 -0700245 const SkMaskFilter::BlurRec& blurRec,
joshualittdbd35932015-04-02 09:19:04 -0700246 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700247 // If we have LCD text then our canonical color will be set to transparent, in this case we have
248 // to regenerate the blob on any color change
249 if (blob.fKey.fCanonicalColor == SK_ColorTRANSPARENT && blob.fPaintColor != paint.getColor()) {
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
robertphillips9c240a12015-05-28 07:45:59 -0700343void GrAtlasTextContext::drawTextBlob(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;
bsalomonbed83a62015-04-15 14:18:34 -0700393 if (!SkPaint2GrPaint(fContext, rt, skPaint, viewMatrix, true, &grPaint)) {
394 return;
395 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700396
joshualittb7133be2015-04-08 09:08:31 -0700397 if (cacheBlob) {
joshualitt53b5f442015-04-13 06:33:59 -0700398 if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, blurRec, viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700399 // We have to remake the blob because changes may invalidate our masks.
400 // TODO we could probably get away reuse most of the time if the pointer is unique,
401 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700402 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700403 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
404 kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700405 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700406 blob, x, y, drawFilter, clipRect, rt, clip, grPaint);
joshualittb7133be2015-04-08 09:08:31 -0700407 } else {
joshualitt9e36c1a2015-04-14 12:17:27 -0700408 // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y
joshualitt7e7b5c52015-07-21 12:56:56 -0700409 // offsets. Note, we offset the vertex bounds right before flushing
joshualitt2a0e9f32015-04-13 06:12:21 -0700410 cacheBlob->fViewMatrix = viewMatrix;
411 cacheBlob->fX = x;
412 cacheBlob->fY = y;
joshualittb7133be2015-04-08 09:08:31 -0700413 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700414#ifdef CACHE_SANITY_CHECK
415 {
416 int glyphCount = 0;
417 int runCount = 0;
418 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
419 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
420 kGrayTextVASize));
421 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
422 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
423 blob, x, y, drawFilter, clipRect, rt, clip, grPaint);
424 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
425 }
426
427#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700428 }
429 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700430 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700431 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
432 kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700433 } else {
434 cacheBlob.reset(fCache->createBlob(blob, kGrayTextVASize));
435 }
robertphillips9c240a12015-05-28 07:45:59 -0700436 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700437 blob, x, y, drawFilter, clipRect, rt, clip, grPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -0700438 }
439
robertphillips2334fb62015-06-17 05:43:33 -0700440 this->flush(blob, cacheBlob, rt, skPaint, grPaint, drawFilter,
joshualitt2a0e9f32015-04-13 06:12:21 -0700441 clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700442}
443
joshualitt9bd2daf2015-04-17 09:30:06 -0700444inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
445 const SkMatrix& viewMatrix) {
446 // TODO: support perspective (need getMaxScale replacement)
447 if (viewMatrix.hasPerspective()) {
448 return false;
449 }
450
451 SkScalar maxScale = viewMatrix.getMaxScale();
452 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
453 // Hinted text looks far better at small resolutions
454 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700455 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700456 return false;
457 }
458
robertphillipsfcf78292015-06-19 11:49:52 -0700459 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700460#if SK_FORCE_DISTANCE_FIELD_TEXT
461 useDFT = true;
462#endif
463
jvanverth4854d132015-06-22 06:46:56 -0700464 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700465 return false;
466 }
467
468 // rasterizers and mask filters modify alpha, which doesn't
469 // translate well to distance
470 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700471 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700472 return false;
473 }
474
475 // TODO: add some stroking support
476 if (skPaint.getStyle() != SkPaint::kFill_Style) {
477 return false;
478 }
479
480 return true;
481}
482
joshualitt374b2f72015-07-21 08:05:03 -0700483void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700484 const SkPaint& skPaint, GrColor color,
485 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700486 const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualittfcfb9fc2015-04-21 07:35:10 -0700487 SkDrawFilter* drawFilter, const SkIRect& clipRect,
488 GrRenderTarget* rt, const GrClip& clip,
489 const GrPaint& paint) {
joshualitt259fbf12015-07-21 11:39:34 -0700490 cacheBlob->fPaintColor = skPaint.getColor();
joshualitt1d89e8d2015-04-01 12:40:54 -0700491 cacheBlob->fViewMatrix = viewMatrix;
492 cacheBlob->fX = x;
493 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700494
495 // Regenerate textblob
496 SkPaint runPaint = skPaint;
497 SkTextBlob::RunIterator it(blob);
498 for (int run = 0; !it.done(); it.next(), run++) {
499 int glyphCount = it.glyphCount();
500 size_t textLen = glyphCount * sizeof(uint16_t);
501 const SkPoint& offset = it.offset();
502 // applyFontToPaint() always overwrites the exact same attributes,
503 // so it is safe to not re-seed the paint for this reason.
504 it.applyFontToPaint(&runPaint);
505
506 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
507 // A false return from filter() means we should abort the current draw.
508 runPaint = skPaint;
509 continue;
510 }
511
robertphillipsfcf78292015-06-19 11:49:52 -0700512 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700513
joshualitt1d89e8d2015-04-01 12:40:54 -0700514 // setup vertex / glyphIndex for the new run
515 if (run > 0) {
516 PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back();
517 PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back();
518
519 newRun.fVertexStartIndex = lastRun.fVertexEndIndex;
520 newRun.fVertexEndIndex = lastRun.fVertexEndIndex;
521
522 newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex;
523 newRun.fGlyphEndIndex = lastRun.fGlyphEndIndex;
524 }
525
joshualittfcfb9fc2015-04-21 07:35:10 -0700526 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
527 cacheBlob->setHasDistanceField();
528 SkPaint dfPaint = runPaint;
529 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700530 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700531 Run& runIdx = cacheBlob->fRuns[run];
532 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
533 subRun.fUseLCDText = runPaint.isLCDRenderText();
534 subRun.fDrawAsDistanceFields = true;
joshualitt9a27e632015-04-06 10:53:36 -0700535
joshualittfcfb9fc2015-04-21 07:35:10 -0700536 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], dfPaint, NULL, true);
537
538 SkTDArray<char> fallbackTxt;
539 SkTDArray<SkScalar> fallbackPos;
540 SkPoint dfOffset;
541 int scalarsPerPosition = 2;
542 switch (it.positioning()) {
543 case SkTextBlob::kDefault_Positioning: {
544 this->internalDrawDFText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
545 (const char *)it.glyphs(), textLen,
546 x + offset.x(), y + offset.y(), clipRect, textRatio,
547 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
548 break;
549 }
550 case SkTextBlob::kHorizontal_Positioning: {
551 scalarsPerPosition = 1;
552 dfOffset = SkPoint::Make(x, y + offset.y());
553 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
554 (const char*)it.glyphs(), textLen, it.pos(),
555 scalarsPerPosition, dfOffset, clipRect, textRatio,
556 &fallbackTxt, &fallbackPos);
557 break;
558 }
559 case SkTextBlob::kFull_Positioning: {
560 dfOffset = SkPoint::Make(x, y);
561 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
562 (const char*)it.glyphs(), textLen, it.pos(),
563 scalarsPerPosition, dfOffset, clipRect, textRatio,
564 &fallbackTxt, &fallbackPos);
565 break;
566 }
567 }
568 if (fallbackTxt.count()) {
569 this->fallbackDrawPosText(cacheBlob, run, rt, clip, paint, runPaint, viewMatrix,
570 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset,
571 clipRect);
572 }
573
574 SkGlyphCache::AttachCache(cache);
575 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
576 cacheBlob->fRuns[run].fDrawAsPaths = true;
577 } else {
578 cacheBlob->setHasBitmap();
579 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
580 false);
581 switch (it.positioning()) {
582 case SkTextBlob::kDefault_Positioning:
583 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
584 (const char *)it.glyphs(), textLen,
585 x + offset.x(), y + offset.y(), clipRect);
586 break;
587 case SkTextBlob::kHorizontal_Positioning:
588 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
589 (const char*)it.glyphs(), textLen, it.pos(), 1,
590 SkPoint::Make(x, y + offset.y()), clipRect);
591 break;
592 case SkTextBlob::kFull_Positioning:
593 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
594 (const char*)it.glyphs(), textLen, it.pos(), 2,
595 SkPoint::Make(x, y), clipRect);
596 break;
597 }
598 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700599 }
600
601 if (drawFilter) {
602 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
603 runPaint = skPaint;
604 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700605 }
606}
607
joshualitt374b2f72015-07-21 08:05:03 -0700608inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700609 SkPaint* skPaint,
610 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700611 const SkMatrix& viewMatrix) {
612 // getMaxScale doesn't support perspective, so neither do we at the moment
613 SkASSERT(!viewMatrix.hasPerspective());
614 SkScalar maxScale = viewMatrix.getMaxScale();
615 SkScalar textSize = skPaint->getTextSize();
616 SkScalar scaledTextSize = textSize;
617 // if we have non-unity scale, we need to choose our base text size
618 // based on the SkPaint's text size multiplied by the max scale factor
619 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
620 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
621 scaledTextSize *= maxScale;
622 }
623
joshualitt64c99cc2015-04-21 09:43:03 -0700624 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
625 // and ceiling. A scale outside of this range would require regenerating the distance fields
626 SkScalar dfMaskScaleFloor;
627 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700628 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700629 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700630 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700631 *textRatio = textSize / kSmallDFFontSize;
632 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
633 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700634 dfMaskScaleFloor = kSmallDFFontLimit;
635 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700636 *textRatio = textSize / kMediumDFFontSize;
637 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
638 } else {
joshualitta7c63892015-04-21 13:24:37 -0700639 dfMaskScaleFloor = kMediumDFFontLimit;
640 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700641 *textRatio = textSize / kLargeDFFontSize;
642 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
643 }
644
joshualitt64c99cc2015-04-21 09:43:03 -0700645 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
646 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
647 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
648 // tolerate before we'd have to move to a large mip size. When we actually test these values
649 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
650 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
651 // level)
joshualitta7c63892015-04-21 13:24:37 -0700652 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700653 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
654 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
655
joshualitt9bd2daf2015-04-17 09:30:06 -0700656 skPaint->setLCDRenderText(false);
657 skPaint->setAutohinted(false);
658 skPaint->setHinting(SkPaint::kNormal_Hinting);
659 skPaint->setSubpixelText(true);
660}
661
joshualitt374b2f72015-07-21 08:05:03 -0700662inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700663 int runIndex,
joshualittfec19e12015-04-17 10:32:32 -0700664 GrRenderTarget* rt, const GrClip& clip,
joshualitt9bd2daf2015-04-17 09:30:06 -0700665 const GrPaint& paint,
666 const SkPaint& skPaint,
667 const SkMatrix& viewMatrix,
668 const SkTDArray<char>& fallbackTxt,
669 const SkTDArray<SkScalar>& fallbackPos,
670 int scalarsPerPosition,
671 const SkPoint& offset,
672 const SkIRect& clipRect) {
joshualittfec19e12015-04-17 10:32:32 -0700673 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700674 blob->setHasBitmap();
675 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700676 // Push back a new subrun to fill and set the override descriptor
677 run.push_back();
678 run.fOverrideDescriptor.reset(SkNEW(SkAutoDescriptor));
679 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700680 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700681 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700682 run.fOverrideDescriptor->getDesc());
joshualittfcfb9fc2015-04-21 07:35:10 -0700683 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, paint.getColor(), viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700684 fallbackTxt.begin(), fallbackTxt.count(),
685 fallbackPos.begin(), scalarsPerPosition, offset, clipRect);
686 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700687}
688
joshualitt374b2f72015-07-21 08:05:03 -0700689inline GrAtlasTextBlob*
joshualitt9bd2daf2015-04-17 09:30:06 -0700690GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
691 const SkMatrix& viewMatrix, SkGlyphCache** cache,
692 SkPaint* dfPaint, SkScalar* textRatio) {
joshualitt374b2f72015-07-21 08:05:03 -0700693 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700694
695 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700696 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700697 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700698 Run& run = blob->fRuns[0];
699 PerSubRunInfo& subRun = run.fSubRunInfo.back();
700 subRun.fUseLCDText = origPaint.isLCDRenderText();
701 subRun.fDrawAsDistanceFields = true;
joshualitt9bd2daf2015-04-17 09:30:06 -0700702
703 *cache = this->setupCache(&blob->fRuns[0], *dfPaint, NULL, true);
704 return blob;
705}
706
joshualitt374b2f72015-07-21 08:05:03 -0700707inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700708GrAtlasTextContext::createDrawTextBlob(GrRenderTarget* rt, const GrClip& clip,
709 const GrPaint& paint, const SkPaint& skPaint,
710 const SkMatrix& viewMatrix,
711 const char text[], size_t byteLength,
712 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700713 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700714 SkIRect clipRect;
715 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
716
joshualitt374b2f72015-07-21 08:05:03 -0700717 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700718 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
719 SkPaint dfPaint;
720 SkScalar textRatio;
721 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700722 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700723
joshualitt9bd2daf2015-04-17 09:30:06 -0700724 SkTDArray<char> fallbackTxt;
725 SkTDArray<SkScalar> fallbackPos;
726 SkPoint offset;
727 this->internalDrawDFText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
728 byteLength, x, y, clipRect, textRatio, &fallbackTxt, &fallbackPos,
729 &offset, skPaint);
730 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700731 if (fallbackTxt.count()) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700732 this->fallbackDrawPosText(blob, 0, rt, clip, paint, skPaint, viewMatrix, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700733 fallbackPos, 2, offset, clipRect);
734 }
735 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700736 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700737 blob->fViewMatrix = viewMatrix;
738
739 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
740 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
741 byteLength, x, y, clipRect);
742 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700743 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700744 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700745}
746
joshualitt374b2f72015-07-21 08:05:03 -0700747inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700748GrAtlasTextContext::createDrawPosTextBlob(GrRenderTarget* rt, const GrClip& clip,
749 const GrPaint& paint, const SkPaint& skPaint,
750 const SkMatrix& viewMatrix,
751 const char text[], size_t byteLength,
752 const SkScalar pos[], int scalarsPerPosition,
753 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700754 int glyphCount = skPaint.countText(text, byteLength);
755
756 SkIRect clipRect;
757 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
758
joshualitt374b2f72015-07-21 08:05:03 -0700759 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700760 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
761 SkPaint dfPaint;
762 SkScalar textRatio;
763 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700764 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700765
766 SkTDArray<char> fallbackTxt;
767 SkTDArray<SkScalar> fallbackPos;
768 this->internalDrawDFPosText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
769 byteLength, pos, scalarsPerPosition, offset, clipRect,
770 textRatio, &fallbackTxt, &fallbackPos);
771 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700772 if (fallbackTxt.count()) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700773 this->fallbackDrawPosText(blob, 0, rt, clip, paint, skPaint, viewMatrix, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700774 fallbackPos, scalarsPerPosition, offset, clipRect);
775 }
776 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700777 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700778 blob->fViewMatrix = viewMatrix;
779 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
780 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
781 byteLength, pos, scalarsPerPosition, offset, clipRect);
782 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700783 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700784 return blob;
785}
786
robertphillips2334fb62015-06-17 05:43:33 -0700787void GrAtlasTextContext::onDrawText(GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700788 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700789 const GrPaint& paint, const SkPaint& skPaint,
790 const SkMatrix& viewMatrix,
791 const char text[], size_t byteLength,
792 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700793 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips2334fb62015-06-17 05:43:33 -0700794 this->createDrawTextBlob(rt, clip, paint, skPaint, viewMatrix,
795 text, byteLength, x, y, regionClipBounds));
796 this->flush(blob, rt, skPaint, paint, clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700797}
798
robertphillips2334fb62015-06-17 05:43:33 -0700799void GrAtlasTextContext::onDrawPosText(GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700800 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700801 const GrPaint& paint, const SkPaint& skPaint,
802 const SkMatrix& viewMatrix,
803 const char text[], size_t byteLength,
804 const SkScalar pos[], int scalarsPerPosition,
805 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700806 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips2334fb62015-06-17 05:43:33 -0700807 this->createDrawPosTextBlob(rt, clip, paint, skPaint, viewMatrix,
808 text, byteLength,
809 pos, scalarsPerPosition,
810 offset, regionClipBounds));
joshualitt79dfb2b2015-05-11 08:58:08 -0700811
robertphillips2334fb62015-06-17 05:43:33 -0700812 this->flush(blob, rt, skPaint, paint, clip, regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700813}
814
joshualitt374b2f72015-07-21 08:05:03 -0700815void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700816 SkGlyphCache* cache, const SkPaint& skPaint,
817 GrColor color,
818 const SkMatrix& viewMatrix,
819 const char text[], size_t byteLength,
820 SkScalar x, SkScalar y, const SkIRect& clipRect) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700821 SkASSERT(byteLength == 0 || text != NULL);
822
823 // nothing to draw
824 if (text == NULL || byteLength == 0) {
825 return;
826 }
827
828 fCurrStrike = NULL;
829 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
830
831 // Get GrFontScaler from cache
832 GrFontScaler* fontScaler = GetGrFontScaler(cache);
833
834 // transform our starting point
835 {
836 SkPoint loc;
837 viewMatrix.mapXY(x, y, &loc);
838 x = loc.fX;
839 y = loc.fY;
840 }
841
842 // need to measure first
843 if (skPaint.getTextAlign() != SkPaint::kLeft_Align) {
844 SkVector stopVector;
845 MeasureText(cache, glyphCacheProc, text, byteLength, &stopVector);
846
847 SkScalar stopX = stopVector.fX;
848 SkScalar stopY = stopVector.fY;
849
850 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
851 stopX = SkScalarHalf(stopX);
852 stopY = SkScalarHalf(stopY);
853 }
854 x -= stopX;
855 y -= stopY;
856 }
857
858 const char* stop = text + byteLength;
859
860 SkAutoKern autokern;
861
862 SkFixed fxMask = ~0;
863 SkFixed fyMask = ~0;
864 SkScalar halfSampleX, halfSampleY;
865 if (cache->isSubpixel()) {
866 halfSampleX = halfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
867 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
868 if (kX_SkAxisAlignment == baseline) {
869 fyMask = 0;
870 halfSampleY = SK_ScalarHalf;
871 } else if (kY_SkAxisAlignment == baseline) {
872 fxMask = 0;
873 halfSampleX = SK_ScalarHalf;
874 }
875 } else {
876 halfSampleX = halfSampleY = SK_ScalarHalf;
877 }
878
879 Sk48Dot16 fx = SkScalarTo48Dot16(x + halfSampleX);
880 Sk48Dot16 fy = SkScalarTo48Dot16(y + halfSampleY);
881
882 while (text < stop) {
883 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
884
885 fx += autokern.adjust(glyph);
886
887 if (glyph.fWidth) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700888 this->bmpAppendGlyph(blob,
889 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700890 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700891 Sk48Dot16FloorToInt(fx),
892 Sk48Dot16FloorToInt(fy),
893 color,
894 fontScaler,
895 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700896 }
897
898 fx += glyph.fAdvanceX;
899 fy += glyph.fAdvanceY;
900 }
901}
902
joshualitt374b2f72015-07-21 08:05:03 -0700903void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700904 SkGlyphCache* cache, const SkPaint& skPaint,
905 GrColor color,
906 const SkMatrix& viewMatrix,
907 const char text[], size_t byteLength,
908 const SkScalar pos[], int scalarsPerPosition,
909 const SkPoint& offset, const SkIRect& clipRect) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700910 SkASSERT(byteLength == 0 || text != NULL);
911 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
912
913 // nothing to draw
914 if (text == NULL || byteLength == 0) {
915 return;
916 }
917
918 fCurrStrike = NULL;
919 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
920
921 // Get GrFontScaler from cache
922 GrFontScaler* fontScaler = GetGrFontScaler(cache);
923
924 const char* stop = text + byteLength;
925 SkTextAlignProc alignProc(skPaint.getTextAlign());
926 SkTextMapStateProc tmsProc(viewMatrix, offset, scalarsPerPosition);
927
928 if (cache->isSubpixel()) {
929 // maybe we should skip the rounding if linearText is set
930 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
931
932 SkFixed fxMask = ~0;
933 SkFixed fyMask = ~0;
934 SkScalar halfSampleX = SkFixedToScalar(SkGlyph::kSubpixelRound);
935 SkScalar halfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
936 if (kX_SkAxisAlignment == baseline) {
937 fyMask = 0;
938 halfSampleY = SK_ScalarHalf;
939 } else if (kY_SkAxisAlignment == baseline) {
940 fxMask = 0;
941 halfSampleX = SK_ScalarHalf;
942 }
943
944 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
945 while (text < stop) {
946 SkPoint tmsLoc;
947 tmsProc(pos, &tmsLoc);
948 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + halfSampleX);
949 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + halfSampleY);
950
951 const SkGlyph& glyph = glyphCacheProc(cache, &text,
952 fx & fxMask, fy & fyMask);
953
954 if (glyph.fWidth) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700955 this->bmpAppendGlyph(blob,
956 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700957 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700958 Sk48Dot16FloorToInt(fx),
959 Sk48Dot16FloorToInt(fy),
960 color,
961 fontScaler,
962 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700963 }
964 pos += scalarsPerPosition;
965 }
966 } else {
967 while (text < stop) {
968 const char* currentText = text;
969 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
970
971 if (metricGlyph.fWidth) {
972 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
973 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
974 SkPoint tmsLoc;
975 tmsProc(pos, &tmsLoc);
976 SkPoint alignLoc;
977 alignProc(tmsLoc, metricGlyph, &alignLoc);
978
979 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + halfSampleX);
980 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + halfSampleY);
981
982 // have to call again, now that we've been "aligned"
983 const SkGlyph& glyph = glyphCacheProc(cache, &currentText,
984 fx & fxMask, fy & fyMask);
985 // the assumption is that the metrics haven't changed
986 SkASSERT(prevAdvX == glyph.fAdvanceX);
987 SkASSERT(prevAdvY == glyph.fAdvanceY);
988 SkASSERT(glyph.fWidth);
989
joshualitt9bd2daf2015-04-17 09:30:06 -0700990 this->bmpAppendGlyph(blob,
991 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700992 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700993 Sk48Dot16FloorToInt(fx),
994 Sk48Dot16FloorToInt(fy),
995 color,
996 fontScaler,
997 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700998 }
999 pos += scalarsPerPosition;
1000 }
1001 }
1002 } else { // not subpixel
1003
1004 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
1005 while (text < stop) {
1006 // the last 2 parameters are ignored
1007 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1008
1009 if (glyph.fWidth) {
1010 SkPoint tmsLoc;
1011 tmsProc(pos, &tmsLoc);
1012
1013 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + SK_ScalarHalf); //halfSampleX;
1014 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + SK_ScalarHalf); //halfSampleY;
joshualitt9bd2daf2015-04-17 09:30:06 -07001015 this->bmpAppendGlyph(blob,
1016 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001017 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001018 Sk48Dot16FloorToInt(fx),
1019 Sk48Dot16FloorToInt(fy),
1020 color,
1021 fontScaler,
1022 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001023 }
1024 pos += scalarsPerPosition;
1025 }
1026 } else {
1027 while (text < stop) {
1028 // the last 2 parameters are ignored
1029 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1030
1031 if (glyph.fWidth) {
1032 SkPoint tmsLoc;
1033 tmsProc(pos, &tmsLoc);
1034
1035 SkPoint alignLoc;
1036 alignProc(tmsLoc, glyph, &alignLoc);
1037
1038 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + SK_ScalarHalf); //halfSampleX;
1039 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + SK_ScalarHalf); //halfSampleY;
joshualitt9bd2daf2015-04-17 09:30:06 -07001040 this->bmpAppendGlyph(blob,
1041 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001042 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001043 Sk48Dot16FloorToInt(fx),
1044 Sk48Dot16FloorToInt(fy),
1045 color,
1046 fontScaler,
1047 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001048 }
1049 pos += scalarsPerPosition;
1050 }
1051 }
1052 }
1053}
1054
joshualitt9bd2daf2015-04-17 09:30:06 -07001055
joshualitt374b2f72015-07-21 08:05:03 -07001056void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -07001057 SkGlyphCache* cache, const SkPaint& skPaint,
1058 GrColor color,
1059 const SkMatrix& viewMatrix,
1060 const char text[], size_t byteLength,
1061 SkScalar x, SkScalar y, const SkIRect& clipRect,
1062 SkScalar textRatio,
1063 SkTDArray<char>* fallbackTxt,
1064 SkTDArray<SkScalar>* fallbackPos,
1065 SkPoint* offset,
1066 const SkPaint& origPaint) {
1067 SkASSERT(byteLength == 0 || text != NULL);
1068
1069 // nothing to draw
1070 if (text == NULL || byteLength == 0) {
1071 return;
1072 }
1073
1074 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
1075 SkAutoDescriptor desc;
robertphillipsfcf78292015-06-19 11:49:52 -07001076 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, NULL, true);
joshualitt9bd2daf2015-04-17 09:30:06 -07001077 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
1078 desc.getDesc());
1079
1080 SkTArray<SkScalar> positions;
1081
1082 const char* textPtr = text;
1083 SkFixed stopX = 0;
1084 SkFixed stopY = 0;
1085 SkFixed origin = 0;
1086 switch (origPaint.getTextAlign()) {
1087 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
1088 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
1089 case SkPaint::kLeft_Align: origin = 0; break;
1090 }
1091
1092 SkAutoKern autokern;
1093 const char* stop = text + byteLength;
1094 while (textPtr < stop) {
1095 // don't need x, y here, since all subpixel variants will have the
1096 // same advance
1097 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
1098
1099 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
1100 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
1101
1102 SkFixed height = glyph.fAdvanceY;
1103 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
1104
1105 stopX += width;
1106 stopY += height;
1107 }
1108 SkASSERT(textPtr == stop);
1109
1110 // now adjust starting point depending on alignment
1111 SkScalar alignX = SkFixedToScalar(stopX);
1112 SkScalar alignY = SkFixedToScalar(stopY);
1113 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
1114 alignX = SkScalarHalf(alignX);
1115 alignY = SkScalarHalf(alignY);
1116 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
1117 alignX = 0;
1118 alignY = 0;
1119 }
1120 x -= alignX;
1121 y -= alignY;
1122 *offset = SkPoint::Make(x, y);
1123
1124 this->internalDrawDFPosText(blob, runIndex, cache, skPaint, color, viewMatrix, text, byteLength,
1125 positions.begin(), 2, *offset, clipRect, textRatio, fallbackTxt,
1126 fallbackPos);
1127 SkGlyphCache::AttachCache(origPaintCache);
1128}
1129
joshualitt374b2f72015-07-21 08:05:03 -07001130void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -07001131 SkGlyphCache* cache, const SkPaint& skPaint,
1132 GrColor color,
1133 const SkMatrix& viewMatrix,
1134 const char text[], size_t byteLength,
1135 const SkScalar pos[], int scalarsPerPosition,
1136 const SkPoint& offset, const SkIRect& clipRect,
1137 SkScalar textRatio,
1138 SkTDArray<char>* fallbackTxt,
1139 SkTDArray<SkScalar>* fallbackPos) {
1140
1141 SkASSERT(byteLength == 0 || text != NULL);
1142 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1143
1144 // nothing to draw
1145 if (text == NULL || byteLength == 0) {
1146 return;
1147 }
1148
1149 fCurrStrike = NULL;
1150
1151 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
1152 GrFontScaler* fontScaler = GetGrFontScaler(cache);
1153
1154 const char* stop = text + byteLength;
1155
1156 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
1157 while (text < stop) {
1158 const char* lastText = text;
1159 // the last 2 parameters are ignored
1160 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1161
1162 if (glyph.fWidth) {
1163 SkScalar x = offset.x() + pos[0];
1164 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
1165
1166 if (!this->dfAppendGlyph(blob,
1167 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001168 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001169 x, y, color, fontScaler, clipRect,
1170 textRatio, viewMatrix)) {
1171 // couldn't append, send to fallback
1172 fallbackTxt->append(SkToInt(text-lastText), lastText);
1173 *fallbackPos->append() = pos[0];
1174 if (2 == scalarsPerPosition) {
1175 *fallbackPos->append() = pos[1];
1176 }
1177 }
1178 }
1179 pos += scalarsPerPosition;
1180 }
1181 } else {
1182 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
1183 : SK_Scalar1;
1184 while (text < stop) {
1185 const char* lastText = text;
1186 // the last 2 parameters are ignored
1187 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1188
1189 if (glyph.fWidth) {
1190 SkScalar x = offset.x() + pos[0];
1191 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
1192
1193 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
1194 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
1195
1196 if (!this->dfAppendGlyph(blob,
1197 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001198 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001199 x - advanceX, y - advanceY, color,
1200 fontScaler,
1201 clipRect,
1202 textRatio,
1203 viewMatrix)) {
1204 // couldn't append, send to fallback
1205 fallbackTxt->append(SkToInt(text-lastText), lastText);
1206 *fallbackPos->append() = pos[0];
1207 if (2 == scalarsPerPosition) {
1208 *fallbackPos->append() = pos[1];
1209 }
1210 }
1211 }
1212 pos += scalarsPerPosition;
1213 }
1214 }
1215}
1216
joshualitt374b2f72015-07-21 08:05:03 -07001217void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001218 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001219 int vx, int vy, GrColor color, GrFontScaler* scaler,
1220 const SkIRect& clipRect) {
joshualittae32c102015-04-21 09:37:57 -07001221 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001222 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001223 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1224 }
1225
joshualitt6c2c2b02015-07-24 10:37:00 -07001226 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1227 skGlyph.getSubXFixed(),
1228 skGlyph.getSubYFixed(),
1229 GrGlyph::kCoverage_MaskStyle);
1230 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001231 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001232 return;
1233 }
1234
1235 int x = vx + glyph->fBounds.fLeft;
1236 int y = vy + glyph->fBounds.fTop;
1237
1238 // keep them as ints until we've done the clip-test
1239 int width = glyph->fBounds.width();
1240 int height = glyph->fBounds.height();
1241
joshualitt2a0e9f32015-04-13 06:12:21 -07001242#if 0
1243 // Not checking the clip bounds might introduce a performance regression. However, its not
1244 // clear if this is still true today with the larger tiles we use in Chrome. For repositionable
1245 // blobs, we want to make sure we have all of the glyphs, so clipping them out is not ideal.
1246 // We could store the cliprect in the key, but then we'd lose the ability to do integer scrolls
1247 // TODO verify this
joshualitt1d89e8d2015-04-01 12:40:54 -07001248 // check if we clipped out
1249 if (clipRect.quickReject(x, y, x + width, y + height)) {
1250 return;
1251 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001252#endif
joshualitt1d89e8d2015-04-01 12:40:54 -07001253
1254 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001255 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001256 this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy));
joshualitt1d89e8d2015-04-01 12:40:54 -07001257 return;
1258 }
1259
joshualitt1d89e8d2015-04-01 12:40:54 -07001260 GrMaskFormat format = glyph->fMaskFormat;
1261
1262 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
1263 if (run.fInitialized && subRun->fMaskFormat != format) {
joshualittd9f13ae2015-07-24 11:24:31 -07001264 subRun = &run.push_back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001265 subRun->fStrike.reset(SkRef(fCurrStrike));
1266 } else if (!run.fInitialized) {
1267 subRun->fStrike.reset(SkRef(fCurrStrike));
joshualitt1d89e8d2015-04-01 12:40:54 -07001268 }
1269
1270 run.fInitialized = true;
joshualitt1d89e8d2015-04-01 12:40:54 -07001271
1272 size_t vertexStride = get_vertex_stride(format);
1273
1274 SkRect r;
1275 r.fLeft = SkIntToScalar(x);
1276 r.fTop = SkIntToScalar(y);
1277 r.fRight = r.fLeft + SkIntToScalar(width);
1278 r.fBottom = r.fTop + SkIntToScalar(height);
joshualitt9bd2daf2015-04-17 09:30:06 -07001279 subRun->fMaskFormat = format;
1280 this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format,
joshualittae32c102015-04-21 09:37:57 -07001281 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001282}
joshualitt1d89e8d2015-04-01 12:40:54 -07001283
joshualitt374b2f72015-07-21 08:05:03 -07001284bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001285 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001286 SkScalar sx, SkScalar sy, GrColor color,
1287 GrFontScaler* scaler,
1288 const SkIRect& clipRect,
1289 SkScalar textRatio, const SkMatrix& viewMatrix) {
joshualittae32c102015-04-21 09:37:57 -07001290 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001291 if (!fCurrStrike) {
1292 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1293 }
1294
joshualitt6c2c2b02015-07-24 10:37:00 -07001295 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1296 skGlyph.getSubXFixed(),
1297 skGlyph.getSubYFixed(),
1298 GrGlyph::kDistance_MaskStyle);
1299 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001300 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001301 return true;
1302 }
1303
1304 // fallback to color glyph support
1305 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
1306 return false;
1307 }
1308
1309 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
1310 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
1311 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
1312 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
1313
1314 SkScalar scale = textRatio;
1315 dx *= scale;
1316 dy *= scale;
1317 width *= scale;
1318 height *= scale;
1319 sx += dx;
1320 sy += dy;
1321 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
1322
1323#if 0
1324 // check if we clipped out
1325 SkRect dstRect;
1326 viewMatrix.mapRect(&dstRect, glyphRect);
1327 if (clipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
1328 SkScalarTruncToInt(dstRect.top()),
1329 SkScalarTruncToInt(dstRect.right()),
1330 SkScalarTruncToInt(dstRect.bottom()))) {
1331 return true;
1332 }
1333#endif
1334
1335 // TODO combine with the above
1336 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001337 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001338 this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy);
joshualitt9bd2daf2015-04-17 09:30:06 -07001339 return true;
1340 }
1341
joshualitt9bd2daf2015-04-17 09:30:06 -07001342 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001343 if (!run.fInitialized) {
1344 subRun->fStrike.reset(SkRef(fCurrStrike));
1345 }
1346 run.fInitialized = true;
joshualitt9bd2daf2015-04-17 09:30:06 -07001347 SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat);
1348 subRun->fMaskFormat = kA8_GrMaskFormat;
1349
1350 size_t vertexStride = get_vertex_stride_df(kA8_GrMaskFormat, subRun->fUseLCDText);
1351
1352 bool useColorVerts = !subRun->fUseLCDText;
1353 this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts,
joshualittae32c102015-04-21 09:37:57 -07001354 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001355 return true;
1356}
1357
joshualitt374b2f72015-07-21 08:05:03 -07001358inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001359 GrFontScaler* scaler, const SkGlyph& skGlyph,
1360 SkScalar x, SkScalar y) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001361 if (NULL == glyph->fPath) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001362 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
1363 if (!glyphPath) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001364 return;
1365 }
joshualitt6c2c2b02015-07-24 10:37:00 -07001366
1367 glyph->fPath = SkNEW_ARGS(SkPath, (*glyphPath));
joshualitt9bd2daf2015-04-17 09:30:06 -07001368 }
joshualitt374b2f72015-07-21 08:05:03 -07001369 blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y));
joshualitt9bd2daf2015-04-17 09:30:06 -07001370}
1371
joshualitt374b2f72015-07-21 08:05:03 -07001372inline void GrAtlasTextContext::appendGlyphCommon(GrAtlasTextBlob* blob, Run* run,
joshualitt9bd2daf2015-04-17 09:30:06 -07001373 Run::SubRunInfo* subRun,
1374 const SkRect& positions, GrColor color,
1375 size_t vertexStride, bool useVertexColor,
joshualittae32c102015-04-21 09:37:57 -07001376 GrGlyph* glyph) {
1377 blob->fGlyphs[subRun->fGlyphEndIndex] = glyph;
joshualitt9bd2daf2015-04-17 09:30:06 -07001378 run->fVertexBounds.joinNonEmptyArg(positions);
1379 run->fColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -07001380
1381 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->fVertexEndIndex);
1382
joshualitt9bd2daf2015-04-17 09:30:06 -07001383 if (useVertexColor) {
joshualitt010db532015-04-21 10:07:26 -07001384 // V0
1385 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1386 position->set(positions.fLeft, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001387 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
1388 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001389 vertex += vertexStride;
joshualitt9bd2daf2015-04-17 09:30:06 -07001390
joshualitt010db532015-04-21 10:07:26 -07001391 // V1
1392 position = reinterpret_cast<SkPoint*>(vertex);
1393 position->set(positions.fLeft, positions.fBottom);
1394 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001395 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001396 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001397
joshualitt010db532015-04-21 10:07:26 -07001398 // V2
1399 position = reinterpret_cast<SkPoint*>(vertex);
1400 position->set(positions.fRight, positions.fBottom);
1401 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001402 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001403 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001404
joshualitt010db532015-04-21 10:07:26 -07001405 // V3
1406 position = reinterpret_cast<SkPoint*>(vertex);
1407 position->set(positions.fRight, positions.fTop);
1408 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001409 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001410 } else {
1411 // V0
1412 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1413 position->set(positions.fLeft, positions.fTop);
1414 vertex += vertexStride;
1415
1416 // V1
1417 position = reinterpret_cast<SkPoint*>(vertex);
1418 position->set(positions.fLeft, positions.fBottom);
1419 vertex += vertexStride;
1420
1421 // V2
1422 position = reinterpret_cast<SkPoint*>(vertex);
1423 position->set(positions.fRight, positions.fBottom);
1424 vertex += vertexStride;
1425
1426 // V3
1427 position = reinterpret_cast<SkPoint*>(vertex);
1428 position->set(positions.fRight, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001429 }
1430
1431 subRun->fGlyphEndIndex++;
1432 subRun->fVertexEndIndex += vertexStride * kVerticesPerGlyph;
1433}
1434
bsalomonabd30f52015-08-13 13:34:48 -07001435class TextBatch : public GrVertexBatch {
joshualitt1d89e8d2015-04-01 12:40:54 -07001436public:
joshualitt9bd2daf2015-04-17 09:30:06 -07001437 typedef GrAtlasTextContext::DistanceAdjustTable DistanceAdjustTable;
joshualitt374b2f72015-07-21 08:05:03 -07001438 typedef GrAtlasTextBlob Blob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001439 typedef Blob::Run Run;
1440 typedef Run::SubRunInfo TextInfo;
1441 struct Geometry {
joshualittad802c62015-04-15 05:31:57 -07001442 Blob* fBlob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001443 int fRun;
1444 int fSubRun;
1445 GrColor fColor;
joshualitt2a0e9f32015-04-13 06:12:21 -07001446 SkScalar fTransX;
1447 SkScalar fTransY;
joshualitt1d89e8d2015-04-01 12:40:54 -07001448 };
1449
bsalomon265697d2015-07-22 10:17:26 -07001450 static TextBatch* CreateBitmap(GrMaskFormat maskFormat, int glyphCount,
joshualittad802c62015-04-15 05:31:57 -07001451 GrBatchFontCache* fontCache) {
bsalomon265697d2015-07-22 10:17:26 -07001452 TextBatch* batch = SkNEW(TextBatch);
1453
1454 batch->initClassID<TextBatch>();
1455 batch->fFontCache = fontCache;
1456 switch (maskFormat) {
1457 case kA8_GrMaskFormat:
1458 batch->fMaskType = kGrayscaleCoverageMask_MaskType;
1459 break;
1460 case kA565_GrMaskFormat:
1461 batch->fMaskType = kLCDCoverageMask_MaskType;
1462 break;
1463 case kARGB_GrMaskFormat:
1464 batch->fMaskType = kColorBitmapMask_MaskType;
1465 break;
1466 }
1467 batch->fBatch.fNumGlyphs = glyphCount;
bsalomond602f4d2015-07-27 06:12:01 -07001468 batch->fGeoCount = 1;
bsalomon265697d2015-07-22 10:17:26 -07001469 batch->fFilteredColor = 0;
1470 batch->fFontCache = fontCache;
1471 batch->fUseBGR = false;
1472 return batch;
joshualitt1d89e8d2015-04-01 12:40:54 -07001473 }
1474
bsalomon265697d2015-07-22 10:17:26 -07001475 static TextBatch* CreateDistanceField(int glyphCount, GrBatchFontCache* fontCache,
1476 DistanceAdjustTable* distanceAdjustTable,
1477 SkColor filteredColor, bool isLCD,
1478 bool useBGR) {
1479 TextBatch* batch = SkNEW(TextBatch);
1480 batch->initClassID<TextBatch>();
1481 batch->fFontCache = fontCache;
1482 batch->fMaskType = isLCD ? kLCDDistanceField_MaskType : kGrayscaleDistanceField_MaskType;
1483 batch->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
1484 batch->fFilteredColor = filteredColor;
1485 batch->fUseBGR = useBGR;
1486 batch->fBatch.fNumGlyphs = glyphCount;
bsalomond602f4d2015-07-27 06:12:01 -07001487 batch->fGeoCount = 1;
bsalomon265697d2015-07-22 10:17:26 -07001488 return batch;
joshualitt9bd2daf2015-04-17 09:30:06 -07001489 }
1490
bsalomon265697d2015-07-22 10:17:26 -07001491 const char* name() const override { return "TextBatch"; }
joshualitt1d89e8d2015-04-01 12:40:54 -07001492
1493 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001494 if (kColorBitmapMask_MaskType == fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001495 out->setUnknownFourComponents();
1496 } else {
1497 out->setKnownFourComponents(fBatch.fColor);
1498 }
1499 }
1500
1501 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001502 switch (fMaskType) {
1503 case kGrayscaleDistanceField_MaskType:
1504 case kGrayscaleCoverageMask_MaskType:
joshualitt1d89e8d2015-04-01 12:40:54 -07001505 out->setUnknownSingleComponent();
bsalomon265697d2015-07-22 10:17:26 -07001506 break;
1507 case kLCDCoverageMask_MaskType:
1508 case kLCDDistanceField_MaskType:
1509 out->setUnknownOpaqueFourComponents();
joshualitt1d89e8d2015-04-01 12:40:54 -07001510 out->setUsingLCDCoverage();
bsalomon265697d2015-07-22 10:17:26 -07001511 break;
1512 case kColorBitmapMask_MaskType:
1513 out->setKnownSingleComponent(0xff);
joshualitt1d89e8d2015-04-01 12:40:54 -07001514 }
1515 }
1516
bsalomon91d844d2015-08-10 10:47:29 -07001517 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001518 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -07001519 if (!opt.readsColor()) {
joshualitt416e14f2015-07-10 09:05:57 -07001520 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001521 }
bsalomon91d844d2015-08-10 10:47:29 -07001522 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt1d89e8d2015-04-01 12:40:54 -07001523
1524 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -07001525 fBatch.fColorIgnored = !opt.readsColor();
joshualitt416e14f2015-07-10 09:05:57 -07001526 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -07001527 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
1528 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt1d89e8d2015-04-01 12:40:54 -07001529 }
1530
bsalomonb5238a72015-05-05 07:49:49 -07001531 struct FlushInfo {
1532 SkAutoTUnref<const GrVertexBuffer> fVertexBuffer;
1533 SkAutoTUnref<const GrIndexBuffer> fIndexBuffer;
1534 int fGlyphsToFlush;
1535 int fVertexOffset;
1536 };
1537
bsalomon75398562015-08-17 12:55:38 -07001538 void onPrepareDraws(Target* target) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001539 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
1540 // TODO actually only invert if we don't have RGBA
1541 SkMatrix localMatrix;
1542 if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) {
1543 SkDebugf("Cannot invert viewmatrix\n");
1544 return;
1545 }
1546
bsalomon265697d2015-07-22 10:17:26 -07001547 GrTexture* texture = fFontCache->getTexture(this->maskFormat());
joshualitt62db8ba2015-04-09 08:22:37 -07001548 if (!texture) {
1549 SkDebugf("Could not allocate backing texture for atlas\n");
1550 return;
1551 }
1552
bsalomon265697d2015-07-22 10:17:26 -07001553 bool usesDistanceFields = this->usesDistanceFields();
1554 GrMaskFormat maskFormat = this->maskFormat();
1555 bool isLCD = this->isLCD();
1556
joshualitt9bd2daf2015-04-17 09:30:06 -07001557 SkAutoTUnref<const GrGeometryProcessor> gp;
bsalomon265697d2015-07-22 10:17:26 -07001558 if (usesDistanceFields) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001559 gp.reset(this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(),
1560 texture));
1561 } else {
1562 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
joshualitt9bd2daf2015-04-17 09:30:06 -07001563 gp.reset(GrBitmapTextGeoProc::Create(this->color(),
1564 texture,
1565 params,
bsalomon265697d2015-07-22 10:17:26 -07001566 maskFormat,
joshualittb8c241a2015-05-19 08:23:30 -07001567 localMatrix,
1568 this->usesLocalCoords()));
joshualitt9bd2daf2015-04-17 09:30:06 -07001569 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001570
bsalomonb5238a72015-05-05 07:49:49 -07001571 FlushInfo flushInfo;
1572 flushInfo.fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001573 size_t vertexStride = gp->getVertexStride();
bsalomon265697d2015-07-22 10:17:26 -07001574 SkASSERT(vertexStride == (usesDistanceFields ?
1575 get_vertex_stride_df(maskFormat, isLCD) :
1576 get_vertex_stride(maskFormat)));
joshualitt1d89e8d2015-04-01 12:40:54 -07001577
bsalomon75398562015-08-17 12:55:38 -07001578 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001579
1580 int glyphCount = this->numGlyphs();
bsalomon8415abe2015-05-04 11:41:41 -07001581 const GrVertexBuffer* vertexBuffer;
bsalomonb5238a72015-05-05 07:49:49 -07001582
bsalomon75398562015-08-17 12:55:38 -07001583 void* vertices = target->makeVertexSpace(vertexStride,
1584 glyphCount * kVerticesPerGlyph,
1585 &vertexBuffer,
1586 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -07001587 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
bsalomon75398562015-08-17 12:55:38 -07001588 flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
bsalomonb5238a72015-05-05 07:49:49 -07001589 if (!vertices || !flushInfo.fVertexBuffer) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001590 SkDebugf("Could not allocate vertices\n");
1591 return;
1592 }
1593
1594 unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices);
1595
joshualitt25ba7ea2015-04-21 07:49:49 -07001596 // We cache some values to avoid going to the glyphcache for the same fontScaler twice
1597 // in a row
1598 const SkDescriptor* desc = NULL;
1599 SkGlyphCache* cache = NULL;
1600 GrFontScaler* scaler = NULL;
joshualitt25ba7ea2015-04-21 07:49:49 -07001601 SkTypeface* typeface = NULL;
1602
bsalomond602f4d2015-07-27 06:12:01 -07001603 for (int i = 0; i < fGeoCount; i++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001604 Geometry& args = fGeoData[i];
1605 Blob* blob = args.fBlob;
1606 Run& run = blob->fRuns[args.fRun];
1607 TextInfo& info = run.fSubRunInfo[args.fSubRun];
1608
bsalomon265697d2015-07-22 10:17:26 -07001609 uint64_t currentAtlasGen = fFontCache->atlasGeneration(maskFormat);
joshualitt7a9c45c2015-05-26 12:32:23 -07001610 bool regenerateTextureCoords = info.fAtlasGeneration != currentAtlasGen ||
joshualitt7e97b0b2015-07-31 15:18:08 -07001611 info.fStrike->isAbandoned();
joshualitt9bd2daf2015-04-17 09:30:06 -07001612 bool regenerateColors;
bsalomon265697d2015-07-22 10:17:26 -07001613 if (usesDistanceFields) {
1614 regenerateColors = !isLCD && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001615 } else {
bsalomon265697d2015-07-22 10:17:26 -07001616 regenerateColors = kA8_GrMaskFormat == maskFormat && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001617 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001618 bool regeneratePositions = args.fTransX != 0.f || args.fTransY != 0.f;
joshualitt1d89e8d2015-04-01 12:40:54 -07001619 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1620
1621 // We regenerate both texture coords and colors in the blob itself, and update the
1622 // atlas generation. If we don't end up purging any unused plots, we can avoid
1623 // regenerating the coords. We could take a finer grained approach to updating texture
1624 // coords but its not clear if the extra bookkeeping would offset any gains.
1625 // To avoid looping over the glyphs twice, we do one loop and conditionally update color
1626 // or coords as needed. One final note, if we have to break a run for an atlas eviction
1627 // then we can't really trust the atlas has all of the correct data. Atlas evictions
1628 // should be pretty rare, so we just always regenerate in those cases
joshualitt2a0e9f32015-04-13 06:12:21 -07001629 if (regenerateTextureCoords || regenerateColors || regeneratePositions) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001630 // first regenerate texture coordinates / colors if need be
joshualitt1d89e8d2015-04-01 12:40:54 -07001631 bool brokenRun = false;
joshualittae32c102015-04-21 09:37:57 -07001632
1633 // Because the GrBatchFontCache may evict the strike a blob depends on using for
1634 // generating its texture coords, we have to track whether or not the strike has
1635 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
1636 // otherwise we have to get the new strike, and use that to get the correct glyphs.
1637 // Because we do not have the packed ids, and thus can't look up our glyphs in the
1638 // new strike, we instead keep our ref to the old strike and use the packed ids from
1639 // it. These ids will still be valid as long as we hold the ref. When we are done
1640 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
1641 bool regenerateGlyphs = false;
1642 GrBatchTextStrike* strike = NULL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001643 if (regenerateTextureCoords) {
joshualittb4c507e2015-04-08 08:07:59 -07001644 info.fBulkUseToken.reset();
joshualitt25ba7ea2015-04-21 07:49:49 -07001645
1646 // We can reuse if we have a valid strike and our descriptors / typeface are the
1647 // same
joshualitt97202d22015-04-22 13:47:02 -07001648 const SkDescriptor* newDesc = run.fOverrideDescriptor ?
1649 run.fOverrideDescriptor->getDesc() :
joshualitt25ba7ea2015-04-21 07:49:49 -07001650 run.fDescriptor.getDesc();
1651 if (!cache || !SkTypeface::Equal(typeface, run.fTypeface) ||
1652 !(desc->equals(*newDesc))) {
1653 if (cache) {
1654 SkGlyphCache::AttachCache(cache);
1655 }
1656 desc = newDesc;
1657 cache = SkGlyphCache::DetachCache(run.fTypeface, desc);
1658 scaler = GrTextContext::GetGrFontScaler(cache);
joshualitt7e97b0b2015-07-31 15:18:08 -07001659 strike = info.fStrike;
joshualitt25ba7ea2015-04-21 07:49:49 -07001660 typeface = run.fTypeface;
1661 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001662
joshualitt7e97b0b2015-07-31 15:18:08 -07001663 if (info.fStrike->isAbandoned()) {
joshualittae32c102015-04-21 09:37:57 -07001664 regenerateGlyphs = true;
1665 strike = fFontCache->getStrike(scaler);
1666 } else {
joshualitt7e97b0b2015-07-31 15:18:08 -07001667 strike = info.fStrike;
joshualittae32c102015-04-21 09:37:57 -07001668 }
1669 }
1670
1671 for (int glyphIdx = 0; glyphIdx < glyphCount; glyphIdx++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001672 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001673 size_t glyphOffset = glyphIdx + info.fGlyphStartIndex;
joshualitt6c2c2b02015-07-24 10:37:00 -07001674
1675 GrGlyph* glyph = blob->fGlyphs[glyphOffset];
1676 GrGlyph::PackedID id = glyph->fPackedID;
1677 const SkGlyph& skGlyph = scaler->grToSkGlyph(id);
joshualittae32c102015-04-21 09:37:57 -07001678 if (regenerateGlyphs) {
1679 // Get the id from the old glyph, and use the new strike to lookup
1680 // the glyph.
joshualitt76cc6572015-07-31 05:51:45 -07001681 blob->fGlyphs[glyphOffset] = strike->getGlyph(skGlyph, id, maskFormat,
1682 scaler);
joshualittae32c102015-04-21 09:37:57 -07001683 }
1684 glyph = blob->fGlyphs[glyphOffset];
joshualitt1d89e8d2015-04-01 12:40:54 -07001685 SkASSERT(glyph);
joshualitt65e96b42015-07-31 11:45:22 -07001686 SkASSERT(id == glyph->fPackedID);
1687 // We want to be able to assert this but cannot for testing purposes.
1688 // once skbug:4143 has landed we can revist this assert
1689 //SkASSERT(glyph->fMaskFormat == this->maskFormat());
joshualitt1d89e8d2015-04-01 12:40:54 -07001690
1691 if (!fFontCache->hasGlyph(glyph) &&
bsalomon75398562015-08-17 12:55:38 -07001692 !strike->addGlyphToAtlas(target, glyph, scaler, skGlyph, maskFormat)) {
1693 this->flush(target, &flushInfo);
1694 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001695 brokenRun = glyphIdx > 0;
1696
bsalomon75398562015-08-17 12:55:38 -07001697 SkDEBUGCODE(bool success =) strike->addGlyphToAtlas(target,
joshualittae32c102015-04-21 09:37:57 -07001698 glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001699 scaler,
joshualitt4f19ca32015-07-30 07:59:20 -07001700 skGlyph,
1701 maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001702 SkASSERT(success);
1703 }
joshualittb4c507e2015-04-08 08:07:59 -07001704 fFontCache->addGlyphToBulkAndSetUseToken(&info.fBulkUseToken, glyph,
bsalomon75398562015-08-17 12:55:38 -07001705 target->currentToken());
joshualitt1d89e8d2015-04-01 12:40:54 -07001706
1707 // Texture coords are the last vertex attribute so we get a pointer to the
1708 // first one and then map with stride in regenerateTextureCoords
1709 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1710 vertex += info.fVertexStartIndex;
1711 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1712 vertex += vertexStride - sizeof(SkIPoint16);
1713
1714 this->regenerateTextureCoords(glyph, vertex, vertexStride);
1715 }
1716
1717 if (regenerateColors) {
1718 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1719 vertex += info.fVertexStartIndex;
1720 vertex += vertexStride * glyphIdx * kVerticesPerGlyph + sizeof(SkPoint);
1721 this->regenerateColors(vertex, vertexStride, args.fColor);
1722 }
1723
joshualitt2a0e9f32015-04-13 06:12:21 -07001724 if (regeneratePositions) {
1725 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1726 vertex += info.fVertexStartIndex;
1727 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1728 SkScalar transX = args.fTransX;
1729 SkScalar transY = args.fTransY;
1730 this->regeneratePositions(vertex, vertexStride, transX, transY);
1731 }
bsalomonb5238a72015-05-05 07:49:49 -07001732 flushInfo.fGlyphsToFlush++;
joshualitt1d89e8d2015-04-01 12:40:54 -07001733 }
1734
joshualitt2a0e9f32015-04-13 06:12:21 -07001735 // We my have changed the color so update it here
1736 run.fColor = args.fColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07001737 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001738 if (regenerateGlyphs) {
joshualitt7e97b0b2015-07-31 15:18:08 -07001739 info.fStrike.reset(SkRef(strike));
joshualittae32c102015-04-21 09:37:57 -07001740 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001741 info.fAtlasGeneration = brokenRun ? GrBatchAtlas::kInvalidAtlasGeneration :
bsalomon265697d2015-07-22 10:17:26 -07001742 fFontCache->atlasGeneration(maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001743 }
1744 } else {
bsalomonb5238a72015-05-05 07:49:49 -07001745 flushInfo.fGlyphsToFlush += glyphCount;
joshualittb4c507e2015-04-08 08:07:59 -07001746
1747 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1748 // have a valid atlas generation
bsalomon75398562015-08-17 12:55:38 -07001749 fFontCache->setUseTokenBulk(info.fBulkUseToken, target->currentToken(), maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001750 }
1751
1752 // now copy all vertices
1753 size_t byteCount = info.fVertexEndIndex - info.fVertexStartIndex;
1754 memcpy(currVertex, blob->fVertices + info.fVertexStartIndex, byteCount);
1755
1756 currVertex += byteCount;
1757 }
joshualitt25ba7ea2015-04-21 07:49:49 -07001758 // Make sure to attach the last cache if applicable
1759 if (cache) {
1760 SkGlyphCache::AttachCache(cache);
1761 }
bsalomon75398562015-08-17 12:55:38 -07001762 this->flush(target, &flushInfo);
joshualitt1d89e8d2015-04-01 12:40:54 -07001763 }
1764
joshualittad802c62015-04-15 05:31:57 -07001765 // to avoid even the initial copy of the struct, we have a getter for the first item which
1766 // is used to seed the batch with its initial geometry. After seeding, the client should call
1767 // init() so the Batch can initialize itself
1768 Geometry& geometry() { return fGeoData[0]; }
1769 void init() {
joshualitt444987f2015-05-06 06:46:01 -07001770 const Geometry& geo = fGeoData[0];
1771 fBatch.fColor = geo.fColor;
1772 fBatch.fViewMatrix = geo.fBlob->fViewMatrix;
1773
1774 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
1775 // into device space
1776 const Run& run = geo.fBlob->fRuns[geo.fRun];
1777 if (run.fSubRunInfo[geo.fSubRun].fDrawAsDistanceFields) {
1778 SkRect bounds = run.fVertexBounds;
1779 fBatch.fViewMatrix.mapRect(&bounds);
1780 this->setBounds(bounds);
1781 } else {
1782 this->setBounds(run.fVertexBounds);
1783 }
joshualittad802c62015-04-15 05:31:57 -07001784 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001785
1786private:
bsalomon265697d2015-07-22 10:17:26 -07001787 TextBatch() {} // initialized in factory functions.
joshualittad802c62015-04-15 05:31:57 -07001788
bsalomon265697d2015-07-22 10:17:26 -07001789 ~TextBatch() {
bsalomond602f4d2015-07-27 06:12:01 -07001790 for (int i = 0; i < fGeoCount; i++) {
joshualittad802c62015-04-15 05:31:57 -07001791 fGeoData[i].fBlob->unref();
1792 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001793 }
1794
bsalomon265697d2015-07-22 10:17:26 -07001795 GrMaskFormat maskFormat() const {
1796 switch (fMaskType) {
1797 case kLCDCoverageMask_MaskType:
1798 return kA565_GrMaskFormat;
1799 case kColorBitmapMask_MaskType:
1800 return kARGB_GrMaskFormat;
1801 case kGrayscaleCoverageMask_MaskType:
1802 case kGrayscaleDistanceField_MaskType:
1803 case kLCDDistanceField_MaskType:
1804 return kA8_GrMaskFormat;
1805 }
1806 return kA8_GrMaskFormat; // suppress warning
1807 }
1808
1809 bool usesDistanceFields() const {
1810 return kGrayscaleDistanceField_MaskType == fMaskType ||
1811 kLCDDistanceField_MaskType == fMaskType;
1812 }
1813
1814 bool isLCD() const {
1815 return kLCDCoverageMask_MaskType == fMaskType ||
1816 kLCDDistanceField_MaskType == fMaskType;
1817 }
1818
joshualitt1d89e8d2015-04-01 12:40:54 -07001819 void regenerateTextureCoords(GrGlyph* glyph, intptr_t vertex, size_t vertexStride) {
1820 int width = glyph->fBounds.width();
1821 int height = glyph->fBounds.height();
joshualitt1d89e8d2015-04-01 12:40:54 -07001822
joshualitt9bd2daf2015-04-17 09:30:06 -07001823 int u0, v0, u1, v1;
bsalomon265697d2015-07-22 10:17:26 -07001824 if (this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001825 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
1826 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
1827 u1 = u0 + width - 2 * SK_DistanceFieldInset;
1828 v1 = v0 + height - 2 * SK_DistanceFieldInset;
1829 } else {
1830 u0 = glyph->fAtlasLocation.fX;
1831 v0 = glyph->fAtlasLocation.fY;
1832 u1 = u0 + width;
1833 v1 = v0 + height;
1834 }
1835
joshualitt1d89e8d2015-04-01 12:40:54 -07001836 SkIPoint16* textureCoords;
1837 // V0
1838 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1839 textureCoords->set(u0, v0);
1840 vertex += vertexStride;
1841
1842 // V1
1843 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1844 textureCoords->set(u0, v1);
1845 vertex += vertexStride;
1846
1847 // V2
1848 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1849 textureCoords->set(u1, v1);
1850 vertex += vertexStride;
1851
1852 // V3
1853 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1854 textureCoords->set(u1, v0);
1855 }
1856
1857 void regenerateColors(intptr_t vertex, size_t vertexStride, GrColor color) {
1858 for (int i = 0; i < kVerticesPerGlyph; i++) {
1859 SkColor* vcolor = reinterpret_cast<SkColor*>(vertex);
1860 *vcolor = color;
1861 vertex += vertexStride;
1862 }
1863 }
1864
joshualitt2a0e9f32015-04-13 06:12:21 -07001865 void regeneratePositions(intptr_t vertex, size_t vertexStride, SkScalar transX,
1866 SkScalar transY) {
1867 for (int i = 0; i < kVerticesPerGlyph; i++) {
1868 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
1869 point->fX += transX;
1870 point->fY += transY;
1871 vertex += vertexStride;
1872 }
1873 }
1874
bsalomon75398562015-08-17 12:55:38 -07001875 void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) {
bsalomoncb8979d2015-05-05 09:51:38 -07001876 GrVertices vertices;
bsalomonb5238a72015-05-05 07:49:49 -07001877 int maxGlyphsPerDraw = flushInfo->fIndexBuffer->maxQuads();
bsalomoncb8979d2015-05-05 09:51:38 -07001878 vertices.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001879 flushInfo->fIndexBuffer, flushInfo->fVertexOffset,
bsalomone64eb572015-05-07 11:35:55 -07001880 kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
bsalomonb5238a72015-05-05 07:49:49 -07001881 maxGlyphsPerDraw);
bsalomon75398562015-08-17 12:55:38 -07001882 target->draw(vertices);
bsalomonb5238a72015-05-05 07:49:49 -07001883 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
1884 flushInfo->fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001885 }
1886
1887 GrColor color() const { return fBatch.fColor; }
1888 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1889 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1890 int numGlyphs() const { return fBatch.fNumGlyphs; }
1891
bsalomoncb02b382015-08-12 11:14:50 -07001892 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001893 TextBatch* that = t->cast<TextBatch>();
1894 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1895 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001896 return false;
1897 }
1898
bsalomon265697d2015-07-22 10:17:26 -07001899 if (fMaskType != that->fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001900 return false;
1901 }
1902
bsalomon265697d2015-07-22 10:17:26 -07001903 if (!this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001904 // TODO we can often batch across LCD text if we have dual source blending and don't
1905 // have to use the blend constant
bsalomon265697d2015-07-22 10:17:26 -07001906 if (kGrayscaleCoverageMask_MaskType != fMaskType && this->color() != that->color()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001907 return false;
1908 }
joshualitt9bd2daf2015-04-17 09:30:06 -07001909 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1910 return false;
1911 }
1912 } else {
joshualitt9bd2daf2015-04-17 09:30:06 -07001913 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1914 return false;
1915 }
1916
1917 if (fFilteredColor != that->fFilteredColor) {
1918 return false;
1919 }
1920
joshualitt9bd2daf2015-04-17 09:30:06 -07001921 if (fUseBGR != that->fUseBGR) {
1922 return false;
1923 }
1924
joshualitt9bd2daf2015-04-17 09:30:06 -07001925 // TODO see note above
bsalomon265697d2015-07-22 10:17:26 -07001926 if (kLCDDistanceField_MaskType == fMaskType && this->color() != that->color()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001927 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001928 }
1929
1930 fBatch.fNumGlyphs += that->numGlyphs();
joshualittad802c62015-04-15 05:31:57 -07001931
bsalomond602f4d2015-07-27 06:12:01 -07001932 // Reallocate space for geo data if necessary and then import that's geo data.
1933 int newGeoCount = that->fGeoCount + fGeoCount;
1934 // We assume (and here enforce) that the allocation size is the smallest power of two that
1935 // is greater than or equal to the number of geometries (and at least
1936 // kMinGeometryAllocated).
1937 int newAllocSize = GrNextPow2(newGeoCount);
1938 int currAllocSize = SkTMax<int>(kMinGeometryAllocated, GrNextPow2(fGeoCount));
1939
bsalomon16ed6ad2015-07-29 06:54:33 -07001940 if (newGeoCount > currAllocSize) {
bsalomond602f4d2015-07-27 06:12:01 -07001941 fGeoData.realloc(newAllocSize);
joshualittad802c62015-04-15 05:31:57 -07001942 }
1943
bsalomond602f4d2015-07-27 06:12:01 -07001944 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
bsalomon1c634362015-07-27 07:00:00 -07001945 // We steal the ref on the blobs from the other TextBatch and set its count to 0 so that
1946 // it doesn't try to unref them.
1947#ifdef SK_DEBUG
1948 for (int i = 0; i < that->fGeoCount; ++i) {
1949 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
joshualittad802c62015-04-15 05:31:57 -07001950 }
bsalomon1c634362015-07-27 07:00:00 -07001951#endif
1952 that->fGeoCount = 0;
bsalomond602f4d2015-07-27 06:12:01 -07001953 fGeoCount = newGeoCount;
joshualitt99c7c072015-05-01 13:43:30 -07001954
1955 this->joinBounds(that->bounds());
joshualitt1d89e8d2015-04-01 12:40:54 -07001956 return true;
1957 }
1958
joshualitt9bd2daf2015-04-17 09:30:06 -07001959 // TODO just use class params
1960 // TODO trying to figure out why lcd is so whack
1961 GrGeometryProcessor* setupDfProcessor(const SkMatrix& viewMatrix, SkColor filteredColor,
1962 GrColor color, GrTexture* texture) {
1963 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
bsalomon265697d2015-07-22 10:17:26 -07001964 bool isLCD = this->isLCD();
joshualitt9bd2daf2015-04-17 09:30:06 -07001965 // set up any flags
bsalomon265697d2015-07-22 10:17:26 -07001966 uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
joshualitt9bd2daf2015-04-17 09:30:06 -07001967
1968 // see if we need to create a new effect
bsalomon265697d2015-07-22 10:17:26 -07001969 if (isLCD) {
1970 flags |= kUseLCD_DistanceFieldEffectFlag;
1971 flags |= viewMatrix.rectStaysRect() ? kRectToRect_DistanceFieldEffectFlag : 0;
1972 flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0;
1973
joshualitt9bd2daf2015-04-17 09:30:06 -07001974 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
1975
1976 float redCorrection =
1977 (*fDistanceAdjustTable)[GrColorUnpackR(colorNoPreMul) >> kDistanceAdjustLumShift];
1978 float greenCorrection =
1979 (*fDistanceAdjustTable)[GrColorUnpackG(colorNoPreMul) >> kDistanceAdjustLumShift];
1980 float blueCorrection =
1981 (*fDistanceAdjustTable)[GrColorUnpackB(colorNoPreMul) >> kDistanceAdjustLumShift];
1982 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
1983 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(redCorrection,
1984 greenCorrection,
1985 blueCorrection);
1986
1987 return GrDistanceFieldLCDTextGeoProc::Create(color,
1988 viewMatrix,
1989 texture,
1990 params,
1991 widthAdjust,
joshualittb8c241a2015-05-19 08:23:30 -07001992 flags,
1993 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07001994 } else {
1995 flags |= kColorAttr_DistanceFieldEffectFlag;
joshualitt9bd2daf2015-04-17 09:30:06 -07001996#ifdef SK_GAMMA_APPLY_TO_A8
robertphillips9fc82752015-06-19 04:46:45 -07001997 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, filteredColor);
joshualitt9bd2daf2015-04-17 09:30:06 -07001998 float correction = (*fDistanceAdjustTable)[lum >> kDistanceAdjustLumShift];
1999 return GrDistanceFieldA8TextGeoProc::Create(color,
2000 viewMatrix,
2001 texture,
2002 params,
2003 correction,
joshualittb8c241a2015-05-19 08:23:30 -07002004 flags,
2005 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07002006#else
2007 return GrDistanceFieldA8TextGeoProc::Create(color,
2008 viewMatrix,
2009 texture,
2010 params,
joshualittb8c241a2015-05-19 08:23:30 -07002011 flags,
2012 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07002013#endif
2014 }
2015
2016 }
2017
joshualitt1d89e8d2015-04-01 12:40:54 -07002018 struct BatchTracker {
2019 GrColor fColor;
2020 SkMatrix fViewMatrix;
2021 bool fUsesLocalCoords;
2022 bool fColorIgnored;
2023 bool fCoverageIgnored;
2024 int fNumGlyphs;
2025 };
2026
2027 BatchTracker fBatch;
bsalomond602f4d2015-07-27 06:12:01 -07002028 // The minimum number of Geometry we will try to allocate.
2029 enum { kMinGeometryAllocated = 4 };
2030 SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData;
2031 int fGeoCount;
bsalomon265697d2015-07-22 10:17:26 -07002032
2033 enum MaskType {
2034 kGrayscaleCoverageMask_MaskType,
2035 kLCDCoverageMask_MaskType,
2036 kColorBitmapMask_MaskType,
2037 kGrayscaleDistanceField_MaskType,
2038 kLCDDistanceField_MaskType,
2039 } fMaskType;
2040 bool fUseBGR; // fold this into the enum?
2041
joshualitt1d89e8d2015-04-01 12:40:54 -07002042 GrBatchFontCache* fFontCache;
joshualitt9bd2daf2015-04-17 09:30:06 -07002043
2044 // Distance field properties
robertphillips9fc82752015-06-19 04:46:45 -07002045 SkAutoTUnref<const DistanceAdjustTable> fDistanceAdjustTable;
joshualitt9bd2daf2015-04-17 09:30:06 -07002046 SkColor fFilteredColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07002047};
2048
robertphillips2334fb62015-06-17 05:43:33 -07002049void GrAtlasTextContext::flushRunAsPaths(GrRenderTarget* rt, const SkTextBlob::RunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -07002050 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07002051 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
2052 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
2053 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07002054
joshualitt9a27e632015-04-06 10:53:36 -07002055 size_t textLen = it.glyphCount() * sizeof(uint16_t);
2056 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07002057
joshualitt9a27e632015-04-06 10:53:36 -07002058 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07002059
joshualitt9a27e632015-04-06 10:53:36 -07002060 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
2061 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07002062 }
2063
robertphillipsfcf78292015-06-19 11:49:52 -07002064 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07002065
2066 switch (it.positioning()) {
2067 case SkTextBlob::kDefault_Positioning:
robertphillips2334fb62015-06-17 05:43:33 -07002068 this->drawTextAsPath(rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002069 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002070 textLen, x + offset.x(), y + offset.y(), clipBounds);
2071 break;
2072 case SkTextBlob::kHorizontal_Positioning:
robertphillips2334fb62015-06-17 05:43:33 -07002073 this->drawPosTextAsPath(rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002074 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002075 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
2076 clipBounds);
2077 break;
2078 case SkTextBlob::kFull_Positioning:
robertphillips2334fb62015-06-17 05:43:33 -07002079 this->drawPosTextAsPath(rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002080 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002081 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
2082 break;
2083 }
2084}
2085
bsalomonabd30f52015-08-13 13:34:48 -07002086inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -07002087GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -07002088 int glyphCount, int run, int subRun,
2089 GrColor color, SkScalar transX, SkScalar transY,
2090 const SkPaint& skPaint) {
2091 GrMaskFormat format = info.fMaskFormat;
2092 GrColor subRunColor;
2093 if (kARGB_GrMaskFormat == format) {
2094 uint8_t paintAlpha = skPaint.getAlpha();
2095 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
2096 } else {
2097 subRunColor = color;
2098 }
2099
bsalomon265697d2015-07-22 10:17:26 -07002100 TextBatch* batch;
joshualitt79dfb2b2015-05-11 08:58:08 -07002101 if (info.fDrawAsDistanceFields) {
2102 SkColor filteredColor;
2103 SkColorFilter* colorFilter = skPaint.getColorFilter();
2104 if (colorFilter) {
2105 filteredColor = colorFilter->filterColor(skPaint.getColor());
2106 } else {
2107 filteredColor = skPaint.getColor();
2108 }
robertphillipsfcf78292015-06-19 11:49:52 -07002109 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
bsalomon265697d2015-07-22 10:17:26 -07002110 batch = TextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
2111 fDistanceAdjustTable, filteredColor,
2112 info.fUseLCDText, useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07002113 } else {
bsalomon265697d2015-07-22 10:17:26 -07002114 batch = TextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07002115 }
bsalomon265697d2015-07-22 10:17:26 -07002116 TextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07002117 geometry.fBlob = SkRef(cacheBlob);
2118 geometry.fRun = run;
2119 geometry.fSubRun = subRun;
2120 geometry.fColor = subRunColor;
2121 geometry.fTransX = transX;
2122 geometry.fTransY = transY;
2123 batch->init();
2124
2125 return batch;
2126}
2127
robertphillips2334fb62015-06-17 05:43:33 -07002128inline void GrAtlasTextContext::flushRun(GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07002129 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07002130 SkScalar transX, SkScalar transY,
2131 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07002132 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
2133 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
2134 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
2135 if (0 == glyphCount) {
2136 continue;
2137 }
2138
bsalomonabd30f52015-08-13 13:34:48 -07002139 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
2140 subRun, color, transX, transY,
2141 skPaint));
robertphillips2334fb62015-06-17 05:43:33 -07002142 fDrawContext->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07002143 }
2144}
2145
joshualitt374b2f72015-07-21 08:05:03 -07002146inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -07002147 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07002148 SkScalar transX, SkScalar transY,
2149 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07002150 if (!cacheBlob->fBigGlyphs.count()) {
2151 return;
2152 }
2153
2154 SkMatrix pathMatrix;
2155 if (!cacheBlob->fViewMatrix.invert(&pathMatrix)) {
2156 SkDebugf("could not invert viewmatrix\n");
2157 return;
2158 }
2159
joshualitt9a27e632015-04-06 10:53:36 -07002160 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07002161 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07002162 bigGlyph.fVx += transX;
2163 bigGlyph.fVy += transY;
joshualittfc072562015-05-13 12:15:06 -07002164 SkMatrix translate = cacheBlob->fViewMatrix;
2165 translate.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
2166
robertphillips2334fb62015-06-17 05:43:33 -07002167 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, rt, clip, bigGlyph.fPath,
robertphillipsccb1b572015-05-27 11:02:55 -07002168 skPaint, translate, &pathMatrix, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07002169 }
2170}
joshualitt9a27e632015-04-06 10:53:36 -07002171
robertphillips2334fb62015-06-17 05:43:33 -07002172void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -07002173 GrAtlasTextBlob* cacheBlob,
joshualitt9a27e632015-04-06 10:53:36 -07002174 GrRenderTarget* rt,
2175 const SkPaint& skPaint,
2176 const GrPaint& grPaint,
2177 SkDrawFilter* drawFilter,
2178 const GrClip& clip,
2179 const SkMatrix& viewMatrix,
2180 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07002181 SkScalar x, SkScalar y,
2182 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07002183 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
2184 // it as paths
joshualitt7b670db2015-07-09 13:25:02 -07002185 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002186
2187 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002188
2189 SkTextBlob::RunIterator it(blob);
2190 for (int run = 0; !it.done(); it.next(), run++) {
2191 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillips2334fb62015-06-17 05:43:33 -07002192 this->flushRunAsPaths(rt, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07002193 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07002194 continue;
2195 }
joshualitt2a0e9f32015-04-13 06:12:21 -07002196 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillips2334fb62015-06-17 05:43:33 -07002197 this->flushRun(&pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07002198 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002199 }
2200
2201 // Now flush big glyphs
robertphillips2334fb62015-06-17 05:43:33 -07002202 this->flushBigGlyphs(cacheBlob, rt, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002203}
2204
joshualitt374b2f72015-07-21 08:05:03 -07002205void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
joshualitt9a27e632015-04-06 10:53:36 -07002206 GrRenderTarget* rt,
2207 const SkPaint& skPaint,
2208 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07002209 const GrClip& clip,
2210 const SkIRect& clipBounds) {
joshualitt7b670db2015-07-09 13:25:02 -07002211 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002212
2213 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002214 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillips2334fb62015-06-17 05:43:33 -07002215 this->flushRun(&pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002216 }
2217
2218 // Now flush big glyphs
robertphillips2334fb62015-06-17 05:43:33 -07002219 this->flushBigGlyphs(cacheBlob, rt, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002220}
joshualitt79dfb2b2015-05-11 08:58:08 -07002221
2222///////////////////////////////////////////////////////////////////////////////////////////////////
2223
2224#ifdef GR_TEST_UTILS
2225
bsalomonabd30f52015-08-13 13:34:48 -07002226DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07002227 static uint32_t gContextID = SK_InvalidGenID;
2228 static GrAtlasTextContext* gTextContext = NULL;
robertphillipsfcf78292015-06-19 11:49:52 -07002229 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07002230
2231 if (context->uniqueID() != gContextID) {
2232 gContextID = context->uniqueID();
2233 SkDELETE(gTextContext);
robertphillips2334fb62015-06-17 05:43:33 -07002234
joshualitt79dfb2b2015-05-11 08:58:08 -07002235 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
2236 // because we don't really want to have a gpu device here.
2237 // We enable distance fields by twiddling a knob on the paint
robertphillipsfcf78292015-06-19 11:49:52 -07002238 GrDrawContext* drawContext = context->drawContext(&gSurfaceProps);
robertphillips2334fb62015-06-17 05:43:33 -07002239
robertphillipsfcf78292015-06-19 11:49:52 -07002240 gTextContext = GrAtlasTextContext::Create(context, drawContext, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07002241 }
2242
2243 // create dummy render target
2244 GrSurfaceDesc desc;
2245 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2246 desc.fWidth = 1024;
2247 desc.fHeight = 1024;
2248 desc.fConfig = kRGBA_8888_GrPixelConfig;
joshualitt15732062015-05-13 12:15:14 -07002249 desc.fSampleCnt = 0;
joshualitt79dfb2b2015-05-11 08:58:08 -07002250 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, true, NULL, 0));
2251 SkASSERT(texture);
2252 SkASSERT(NULL != texture->asRenderTarget());
2253 GrRenderTarget* rt = texture->asRenderTarget();
2254
2255 // Setup dummy SkPaint / GrPaint
2256 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07002257 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07002258 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07002259 skPaint.setColor(color);
2260 skPaint.setLCDRenderText(random->nextBool());
2261 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
2262 skPaint.setSubpixelText(random->nextBool());
2263
2264 GrPaint grPaint;
2265 if (!SkPaint2GrPaint(context, rt, skPaint, viewMatrix, true, &grPaint)) {
2266 SkFAIL("couldn't convert paint\n");
2267 }
2268
2269 const char* text = "The quick brown fox jumps over the lazy dog.";
2270 int textLen = (int)strlen(text);
2271
2272 // Setup clip
2273 GrClip clip;
2274 SkIRect noClip = SkIRect::MakeLargest();
2275
2276 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
2277 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07002278 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt79dfb2b2015-05-11 08:58:08 -07002279 gTextContext->createDrawTextBlob(rt, clip, grPaint, skPaint, viewMatrix, text,
2280 static_cast<size_t>(textLen), 0, 0, noClip));
2281
2282 SkScalar transX = static_cast<SkScalar>(random->nextU());
2283 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07002284 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07002285 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
2286}
2287
2288#endif