blob: a556128be5f9f928b64844bc5ccf6958b5d55e60 [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,
jvanverth0628a522015-08-18 07:44:22 -0700245 GrColor color, 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
jvanverth0628a522015-08-18 07:44:22 -0700249 // We use the grPaint to get any color filter effects
250 if (blob.fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
251 blob.fPaintColor != color) {
joshualitt2a0e9f32015-04-13 06:12:21 -0700252 return true;
253 }
254
255 if (blob.fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
256 return true;
257 }
258
259 if (blob.fViewMatrix.hasPerspective() && !blob.fViewMatrix.cheapEqualTo(viewMatrix)) {
260 return true;
261 }
262
joshualitt53b5f442015-04-13 06:33:59 -0700263 // We only cache one masked version
264 if (blob.fKey.fHasBlur &&
265 (blob.fBlurRec.fSigma != blurRec.fSigma ||
266 blob.fBlurRec.fStyle != blurRec.fStyle ||
267 blob.fBlurRec.fQuality != blurRec.fQuality)) {
268 return true;
269 }
270
271 // Similarly, we only cache one version for each style
272 if (blob.fKey.fStyle != SkPaint::kFill_Style &&
273 (blob.fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
274 blob.fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
275 blob.fStrokeInfo.fJoin != paint.getStrokeJoin())) {
276 return true;
277 }
278
joshualittfcfb9fc2015-04-21 07:35:10 -0700279 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
280 // for mixed blobs if this becomes an issue.
281 if (blob.hasBitmap() && blob.hasDistanceField()) {
joshualitt473ffa12015-04-22 18:23:15 -0700282 // Identical viewmatrices and we can reuse in all cases
283 if (blob.fViewMatrix.cheapEqualTo(viewMatrix) && x == blob.fX && y == blob.fY) {
284 return false;
285 }
joshualitt2a0e9f32015-04-13 06:12:21 -0700286 return true;
287 }
288
joshualittfcfb9fc2015-04-21 07:35:10 -0700289 if (blob.hasBitmap()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700290 if (blob.fViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
291 blob.fViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
292 blob.fViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
293 blob.fViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
294 return true;
295 }
296
joshualittfcfb9fc2015-04-21 07:35:10 -0700297 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
298 // but only for integer translations.
299 // This cool bit of math will determine the necessary translation to apply to the already
300 // generated vertex coordinates to move them to the correct position
301 SkScalar transX = viewMatrix.getTranslateX() +
302 viewMatrix.getScaleX() * (x - blob.fX) +
303 viewMatrix.getSkewX() * (y - blob.fY) -
304 blob.fViewMatrix.getTranslateX();
305 SkScalar transY = viewMatrix.getTranslateY() +
306 viewMatrix.getSkewY() * (x - blob.fX) +
307 viewMatrix.getScaleY() * (y - blob.fY) -
308 blob.fViewMatrix.getTranslateY();
joshualittf0c000d2015-04-27 09:36:55 -0700309 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700310 return true;
311 }
312
joshualittfcfb9fc2015-04-21 07:35:10 -0700313 (*outTransX) = transX;
314 (*outTransY) = transY;
joshualitta7c63892015-04-21 13:24:37 -0700315 } else if (blob.hasDistanceField()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700316 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
317 // distance field being generated, so we have to regenerate in those cases
318 SkScalar newMaxScale = viewMatrix.getMaxScale();
319 SkScalar oldMaxScale = blob.fViewMatrix.getMaxScale();
320 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
321 if (scaleAdjust < blob.fMaxMinScale || scaleAdjust > blob.fMinMaxScale) {
322 return true;
323 }
324
325 (*outTransX) = x - blob.fX;
326 (*outTransY) = y - blob.fY;
joshualittfcfb9fc2015-04-21 07:35:10 -0700327 }
joshualitt374b2f72015-07-21 08:05:03 -0700328
joshualitta7c63892015-04-21 13:24:37 -0700329 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
330 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
331 // the blob anyways at flush time, so no need to regenerate explicitly
joshualitt2a0e9f32015-04-13 06:12:21 -0700332 return false;
joshualitt1d89e8d2015-04-01 12:40:54 -0700333}
334
335
joshualitt374b2f72015-07-21 08:05:03 -0700336inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run,
joshualittdbd35932015-04-02 09:19:04 -0700337 const SkPaint& skPaint,
joshualitt9bd2daf2015-04-17 09:30:06 -0700338 const SkMatrix* viewMatrix,
339 bool noGamma) {
robertphillipsfcf78292015-06-19 11:49:52 -0700340 skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma);
joshualitt1d89e8d2015-04-01 12:40:54 -0700341 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
342 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc());
343}
344
robertphillips9c240a12015-05-28 07:45:59 -0700345void GrAtlasTextContext::drawTextBlob(GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700346 const GrClip& clip, const SkPaint& skPaint,
347 const SkMatrix& viewMatrix, const SkTextBlob* blob,
348 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700349 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700350 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700351 if (fContext->abandoned()) {
352 return;
353 }
354
joshualitt374b2f72015-07-21 08:05:03 -0700355 SkAutoTUnref<GrAtlasTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700356 SkMaskFilter::BlurRec blurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700357 GrAtlasTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700358 // It might be worth caching these things, but its not clear at this time
359 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
360 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700361 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700362 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700363 drawFilter);
364
365 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700366 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700367
368 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
robertphillipsfcf78292015-06-19 11:49:52 -0700369 SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700370 kUnknown_SkPixelGeometry;
371
joshualitt9e36c1a2015-04-14 12:17:27 -0700372 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
373 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
374 // ensure we always match the same key
375 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
376 ComputeCanonicalColor(skPaint, hasLCD);
377
joshualitte4cee1f2015-05-11 13:04:28 -0700378 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700379 key.fUniqueID = blob->uniqueID();
380 key.fStyle = skPaint.getStyle();
381 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700382 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700383 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700384 }
385
joshualitt1d89e8d2015-04-01 12:40:54 -0700386 SkIRect clipRect;
387 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
388
joshualitt2a0e9f32015-04-13 06:12:21 -0700389 SkScalar transX = 0.f;
390 SkScalar transY = 0.f;
391
joshualitt9e36c1a2015-04-14 12:17:27 -0700392 // Though for the time being runs in the textblob can override the paint, they only touch font
393 // info.
394 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700395 if (!SkPaint2GrPaint(fContext, rt, skPaint, viewMatrix, true, &grPaint)) {
396 return;
397 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700398
joshualittb7133be2015-04-08 09:08:31 -0700399 if (cacheBlob) {
jvanverth0628a522015-08-18 07:44:22 -0700400 if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, grPaint.getColor(), blurRec,
401 viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700402 // We have to remake the blob because changes may invalidate our masks.
403 // TODO we could probably get away reuse most of the time if the pointer is unique,
404 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700405 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700406 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
407 kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700408 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700409 blob, x, y, drawFilter, clipRect, rt, clip);
joshualittb7133be2015-04-08 09:08:31 -0700410 } else {
joshualitt9e36c1a2015-04-14 12:17:27 -0700411 // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y
joshualitt7e7b5c52015-07-21 12:56:56 -0700412 // offsets. Note, we offset the vertex bounds right before flushing
joshualitt2a0e9f32015-04-13 06:12:21 -0700413 cacheBlob->fViewMatrix = viewMatrix;
414 cacheBlob->fX = x;
415 cacheBlob->fY = y;
joshualittb7133be2015-04-08 09:08:31 -0700416 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700417#ifdef CACHE_SANITY_CHECK
418 {
419 int glyphCount = 0;
420 int runCount = 0;
421 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
422 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
423 kGrayTextVASize));
424 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
425 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700426 blob, x, y, drawFilter, clipRect, rt, clip);
joshualitt259fbf12015-07-21 11:39:34 -0700427 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
428 }
429
430#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700431 }
432 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700433 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700434 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
435 kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700436 } else {
437 cacheBlob.reset(fCache->createBlob(blob, kGrayTextVASize));
438 }
robertphillips9c240a12015-05-28 07:45:59 -0700439 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700440 blob, x, y, drawFilter, clipRect, rt, clip);
joshualitt1d89e8d2015-04-01 12:40:54 -0700441 }
442
robertphillips2334fb62015-06-17 05:43:33 -0700443 this->flush(blob, cacheBlob, rt, skPaint, grPaint, drawFilter,
joshualitt2a0e9f32015-04-13 06:12:21 -0700444 clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700445}
446
joshualitt9bd2daf2015-04-17 09:30:06 -0700447inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
448 const SkMatrix& viewMatrix) {
449 // TODO: support perspective (need getMaxScale replacement)
450 if (viewMatrix.hasPerspective()) {
451 return false;
452 }
453
454 SkScalar maxScale = viewMatrix.getMaxScale();
455 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
456 // Hinted text looks far better at small resolutions
457 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700458 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700459 return false;
460 }
461
robertphillipsfcf78292015-06-19 11:49:52 -0700462 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700463#if SK_FORCE_DISTANCE_FIELD_TEXT
464 useDFT = true;
465#endif
466
jvanverth4854d132015-06-22 06:46:56 -0700467 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700468 return false;
469 }
470
471 // rasterizers and mask filters modify alpha, which doesn't
472 // translate well to distance
473 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700474 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700475 return false;
476 }
477
478 // TODO: add some stroking support
479 if (skPaint.getStyle() != SkPaint::kFill_Style) {
480 return false;
481 }
482
483 return true;
484}
485
joshualitt374b2f72015-07-21 08:05:03 -0700486void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700487 const SkPaint& skPaint, GrColor color,
488 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700489 const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualittfcfb9fc2015-04-21 07:35:10 -0700490 SkDrawFilter* drawFilter, const SkIRect& clipRect,
jvanverth0628a522015-08-18 07:44:22 -0700491 GrRenderTarget* rt, const GrClip& clip) {
492 // The color here is the GrPaint color, and it is used to determine whether we
493 // have to regenerate LCD text blobs.
494 // We use this color vs the SkPaint color because it has the colorfilter applied.
495 cacheBlob->fPaintColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -0700496 cacheBlob->fViewMatrix = viewMatrix;
497 cacheBlob->fX = x;
498 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700499
500 // Regenerate textblob
501 SkPaint runPaint = skPaint;
502 SkTextBlob::RunIterator it(blob);
503 for (int run = 0; !it.done(); it.next(), run++) {
504 int glyphCount = it.glyphCount();
505 size_t textLen = glyphCount * sizeof(uint16_t);
506 const SkPoint& offset = it.offset();
507 // applyFontToPaint() always overwrites the exact same attributes,
508 // so it is safe to not re-seed the paint for this reason.
509 it.applyFontToPaint(&runPaint);
510
511 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
512 // A false return from filter() means we should abort the current draw.
513 runPaint = skPaint;
514 continue;
515 }
516
robertphillipsfcf78292015-06-19 11:49:52 -0700517 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700518
joshualitt1d89e8d2015-04-01 12:40:54 -0700519 // setup vertex / glyphIndex for the new run
520 if (run > 0) {
521 PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back();
522 PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back();
523
524 newRun.fVertexStartIndex = lastRun.fVertexEndIndex;
525 newRun.fVertexEndIndex = lastRun.fVertexEndIndex;
526
527 newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex;
528 newRun.fGlyphEndIndex = lastRun.fGlyphEndIndex;
529 }
530
joshualittfcfb9fc2015-04-21 07:35:10 -0700531 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
532 cacheBlob->setHasDistanceField();
533 SkPaint dfPaint = runPaint;
534 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700535 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700536 Run& runIdx = cacheBlob->fRuns[run];
537 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
538 subRun.fUseLCDText = runPaint.isLCDRenderText();
539 subRun.fDrawAsDistanceFields = true;
joshualitt9a27e632015-04-06 10:53:36 -0700540
joshualittfcfb9fc2015-04-21 07:35:10 -0700541 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], dfPaint, NULL, true);
542
543 SkTDArray<char> fallbackTxt;
544 SkTDArray<SkScalar> fallbackPos;
545 SkPoint dfOffset;
546 int scalarsPerPosition = 2;
547 switch (it.positioning()) {
548 case SkTextBlob::kDefault_Positioning: {
549 this->internalDrawDFText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
550 (const char *)it.glyphs(), textLen,
551 x + offset.x(), y + offset.y(), clipRect, textRatio,
552 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
553 break;
554 }
555 case SkTextBlob::kHorizontal_Positioning: {
556 scalarsPerPosition = 1;
557 dfOffset = SkPoint::Make(x, y + offset.y());
558 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
559 (const char*)it.glyphs(), textLen, it.pos(),
560 scalarsPerPosition, dfOffset, clipRect, textRatio,
561 &fallbackTxt, &fallbackPos);
562 break;
563 }
564 case SkTextBlob::kFull_Positioning: {
565 dfOffset = SkPoint::Make(x, y);
566 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
567 (const char*)it.glyphs(), textLen, it.pos(),
568 scalarsPerPosition, dfOffset, clipRect, textRatio,
569 &fallbackTxt, &fallbackPos);
570 break;
571 }
572 }
573 if (fallbackTxt.count()) {
jvanverth0628a522015-08-18 07:44:22 -0700574 this->fallbackDrawPosText(cacheBlob, run, rt, clip, color, runPaint, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700575 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset,
576 clipRect);
577 }
578
579 SkGlyphCache::AttachCache(cache);
580 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
581 cacheBlob->fRuns[run].fDrawAsPaths = true;
582 } else {
583 cacheBlob->setHasBitmap();
584 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
585 false);
586 switch (it.positioning()) {
587 case SkTextBlob::kDefault_Positioning:
588 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
589 (const char *)it.glyphs(), textLen,
590 x + offset.x(), y + offset.y(), clipRect);
591 break;
592 case SkTextBlob::kHorizontal_Positioning:
593 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
594 (const char*)it.glyphs(), textLen, it.pos(), 1,
595 SkPoint::Make(x, y + offset.y()), clipRect);
596 break;
597 case SkTextBlob::kFull_Positioning:
598 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
599 (const char*)it.glyphs(), textLen, it.pos(), 2,
600 SkPoint::Make(x, y), clipRect);
601 break;
602 }
603 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700604 }
605
606 if (drawFilter) {
607 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
608 runPaint = skPaint;
609 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700610 }
611}
612
joshualitt374b2f72015-07-21 08:05:03 -0700613inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700614 SkPaint* skPaint,
615 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700616 const SkMatrix& viewMatrix) {
617 // getMaxScale doesn't support perspective, so neither do we at the moment
618 SkASSERT(!viewMatrix.hasPerspective());
619 SkScalar maxScale = viewMatrix.getMaxScale();
620 SkScalar textSize = skPaint->getTextSize();
621 SkScalar scaledTextSize = textSize;
622 // if we have non-unity scale, we need to choose our base text size
623 // based on the SkPaint's text size multiplied by the max scale factor
624 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
625 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
626 scaledTextSize *= maxScale;
627 }
628
joshualitt64c99cc2015-04-21 09:43:03 -0700629 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
630 // and ceiling. A scale outside of this range would require regenerating the distance fields
631 SkScalar dfMaskScaleFloor;
632 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700633 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700634 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700635 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700636 *textRatio = textSize / kSmallDFFontSize;
637 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
638 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700639 dfMaskScaleFloor = kSmallDFFontLimit;
640 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700641 *textRatio = textSize / kMediumDFFontSize;
642 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
643 } else {
joshualitta7c63892015-04-21 13:24:37 -0700644 dfMaskScaleFloor = kMediumDFFontLimit;
645 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700646 *textRatio = textSize / kLargeDFFontSize;
647 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
648 }
649
joshualitt64c99cc2015-04-21 09:43:03 -0700650 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
651 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
652 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
653 // tolerate before we'd have to move to a large mip size. When we actually test these values
654 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
655 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
656 // level)
joshualitta7c63892015-04-21 13:24:37 -0700657 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700658 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
659 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
660
joshualitt9bd2daf2015-04-17 09:30:06 -0700661 skPaint->setLCDRenderText(false);
662 skPaint->setAutohinted(false);
663 skPaint->setHinting(SkPaint::kNormal_Hinting);
664 skPaint->setSubpixelText(true);
665}
666
joshualitt374b2f72015-07-21 08:05:03 -0700667inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700668 int runIndex,
joshualittfec19e12015-04-17 10:32:32 -0700669 GrRenderTarget* rt, const GrClip& clip,
jvanverth0628a522015-08-18 07:44:22 -0700670 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700671 const SkPaint& skPaint,
672 const SkMatrix& viewMatrix,
673 const SkTDArray<char>& fallbackTxt,
674 const SkTDArray<SkScalar>& fallbackPos,
675 int scalarsPerPosition,
676 const SkPoint& offset,
677 const SkIRect& clipRect) {
joshualittfec19e12015-04-17 10:32:32 -0700678 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700679 blob->setHasBitmap();
680 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700681 // Push back a new subrun to fill and set the override descriptor
682 run.push_back();
683 run.fOverrideDescriptor.reset(SkNEW(SkAutoDescriptor));
684 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700685 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700686 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700687 run.fOverrideDescriptor->getDesc());
jvanverth0628a522015-08-18 07:44:22 -0700688 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700689 fallbackTxt.begin(), fallbackTxt.count(),
690 fallbackPos.begin(), scalarsPerPosition, offset, clipRect);
691 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700692}
693
joshualitt374b2f72015-07-21 08:05:03 -0700694inline GrAtlasTextBlob*
joshualitt9bd2daf2015-04-17 09:30:06 -0700695GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
696 const SkMatrix& viewMatrix, SkGlyphCache** cache,
697 SkPaint* dfPaint, SkScalar* textRatio) {
joshualitt374b2f72015-07-21 08:05:03 -0700698 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700699
700 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700701 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700702 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700703 Run& run = blob->fRuns[0];
704 PerSubRunInfo& subRun = run.fSubRunInfo.back();
705 subRun.fUseLCDText = origPaint.isLCDRenderText();
706 subRun.fDrawAsDistanceFields = true;
joshualitt9bd2daf2015-04-17 09:30:06 -0700707
708 *cache = this->setupCache(&blob->fRuns[0], *dfPaint, NULL, true);
709 return blob;
710}
711
joshualitt374b2f72015-07-21 08:05:03 -0700712inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700713GrAtlasTextContext::createDrawTextBlob(GrRenderTarget* rt, const GrClip& clip,
714 const GrPaint& paint, const SkPaint& skPaint,
715 const SkMatrix& viewMatrix,
716 const char text[], size_t byteLength,
717 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700718 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700719 SkIRect clipRect;
720 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
721
joshualitt374b2f72015-07-21 08:05:03 -0700722 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700723 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
724 SkPaint dfPaint;
725 SkScalar textRatio;
726 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700727 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700728
joshualitt9bd2daf2015-04-17 09:30:06 -0700729 SkTDArray<char> fallbackTxt;
730 SkTDArray<SkScalar> fallbackPos;
731 SkPoint offset;
732 this->internalDrawDFText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
733 byteLength, x, y, clipRect, textRatio, &fallbackTxt, &fallbackPos,
734 &offset, skPaint);
735 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700736 if (fallbackTxt.count()) {
jvanverth0628a522015-08-18 07:44:22 -0700737 this->fallbackDrawPosText(blob, 0, rt, clip, paint.getColor(), skPaint, viewMatrix,
738 fallbackTxt, fallbackPos, 2, offset, clipRect);
joshualitt9bd2daf2015-04-17 09:30:06 -0700739 }
740 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700741 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700742 blob->fViewMatrix = viewMatrix;
743
744 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
745 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
746 byteLength, x, y, clipRect);
747 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700748 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700749 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700750}
751
joshualitt374b2f72015-07-21 08:05:03 -0700752inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700753GrAtlasTextContext::createDrawPosTextBlob(GrRenderTarget* rt, const GrClip& clip,
754 const GrPaint& paint, const SkPaint& skPaint,
755 const SkMatrix& viewMatrix,
756 const char text[], size_t byteLength,
757 const SkScalar pos[], int scalarsPerPosition,
758 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700759 int glyphCount = skPaint.countText(text, byteLength);
760
761 SkIRect clipRect;
762 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
763
joshualitt374b2f72015-07-21 08:05:03 -0700764 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700765 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
766 SkPaint dfPaint;
767 SkScalar textRatio;
768 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700769 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700770
771 SkTDArray<char> fallbackTxt;
772 SkTDArray<SkScalar> fallbackPos;
773 this->internalDrawDFPosText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
774 byteLength, pos, scalarsPerPosition, offset, clipRect,
775 textRatio, &fallbackTxt, &fallbackPos);
776 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700777 if (fallbackTxt.count()) {
jvanverth0628a522015-08-18 07:44:22 -0700778 this->fallbackDrawPosText(blob, 0, rt, clip, paint.getColor(), skPaint, viewMatrix,
779 fallbackTxt, fallbackPos, scalarsPerPosition, offset,
780 clipRect);
joshualitt9bd2daf2015-04-17 09:30:06 -0700781 }
782 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700783 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700784 blob->fViewMatrix = viewMatrix;
785 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
786 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
787 byteLength, pos, scalarsPerPosition, offset, clipRect);
788 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700789 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700790 return blob;
791}
792
robertphillips2334fb62015-06-17 05:43:33 -0700793void GrAtlasTextContext::onDrawText(GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700794 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700795 const GrPaint& paint, const SkPaint& skPaint,
796 const SkMatrix& viewMatrix,
797 const char text[], size_t byteLength,
798 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700799 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips2334fb62015-06-17 05:43:33 -0700800 this->createDrawTextBlob(rt, clip, paint, skPaint, viewMatrix,
801 text, byteLength, x, y, regionClipBounds));
802 this->flush(blob, rt, skPaint, paint, clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700803}
804
robertphillips2334fb62015-06-17 05:43:33 -0700805void GrAtlasTextContext::onDrawPosText(GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700806 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700807 const GrPaint& paint, const SkPaint& skPaint,
808 const SkMatrix& viewMatrix,
809 const char text[], size_t byteLength,
810 const SkScalar pos[], int scalarsPerPosition,
811 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700812 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips2334fb62015-06-17 05:43:33 -0700813 this->createDrawPosTextBlob(rt, clip, paint, skPaint, viewMatrix,
814 text, byteLength,
815 pos, scalarsPerPosition,
816 offset, regionClipBounds));
joshualitt79dfb2b2015-05-11 08:58:08 -0700817
robertphillips2334fb62015-06-17 05:43:33 -0700818 this->flush(blob, rt, skPaint, paint, clip, regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700819}
820
joshualitt374b2f72015-07-21 08:05:03 -0700821void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700822 SkGlyphCache* cache, const SkPaint& skPaint,
823 GrColor color,
824 const SkMatrix& viewMatrix,
825 const char text[], size_t byteLength,
826 SkScalar x, SkScalar y, const SkIRect& clipRect) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700827 SkASSERT(byteLength == 0 || text != NULL);
828
829 // nothing to draw
830 if (text == NULL || byteLength == 0) {
831 return;
832 }
833
834 fCurrStrike = NULL;
835 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
836
837 // Get GrFontScaler from cache
838 GrFontScaler* fontScaler = GetGrFontScaler(cache);
839
840 // transform our starting point
841 {
842 SkPoint loc;
843 viewMatrix.mapXY(x, y, &loc);
844 x = loc.fX;
845 y = loc.fY;
846 }
847
848 // need to measure first
849 if (skPaint.getTextAlign() != SkPaint::kLeft_Align) {
850 SkVector stopVector;
851 MeasureText(cache, glyphCacheProc, text, byteLength, &stopVector);
852
853 SkScalar stopX = stopVector.fX;
854 SkScalar stopY = stopVector.fY;
855
856 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
857 stopX = SkScalarHalf(stopX);
858 stopY = SkScalarHalf(stopY);
859 }
860 x -= stopX;
861 y -= stopY;
862 }
863
864 const char* stop = text + byteLength;
865
866 SkAutoKern autokern;
867
868 SkFixed fxMask = ~0;
869 SkFixed fyMask = ~0;
870 SkScalar halfSampleX, halfSampleY;
871 if (cache->isSubpixel()) {
872 halfSampleX = halfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
873 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
874 if (kX_SkAxisAlignment == baseline) {
875 fyMask = 0;
876 halfSampleY = SK_ScalarHalf;
877 } else if (kY_SkAxisAlignment == baseline) {
878 fxMask = 0;
879 halfSampleX = SK_ScalarHalf;
880 }
881 } else {
882 halfSampleX = halfSampleY = SK_ScalarHalf;
883 }
884
885 Sk48Dot16 fx = SkScalarTo48Dot16(x + halfSampleX);
886 Sk48Dot16 fy = SkScalarTo48Dot16(y + halfSampleY);
887
888 while (text < stop) {
889 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
890
891 fx += autokern.adjust(glyph);
892
893 if (glyph.fWidth) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700894 this->bmpAppendGlyph(blob,
895 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700896 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700897 Sk48Dot16FloorToInt(fx),
898 Sk48Dot16FloorToInt(fy),
899 color,
900 fontScaler,
901 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700902 }
903
904 fx += glyph.fAdvanceX;
905 fy += glyph.fAdvanceY;
906 }
907}
908
joshualitt374b2f72015-07-21 08:05:03 -0700909void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700910 SkGlyphCache* cache, const SkPaint& skPaint,
911 GrColor color,
912 const SkMatrix& viewMatrix,
913 const char text[], size_t byteLength,
914 const SkScalar pos[], int scalarsPerPosition,
915 const SkPoint& offset, const SkIRect& clipRect) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700916 SkASSERT(byteLength == 0 || text != NULL);
917 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
918
919 // nothing to draw
920 if (text == NULL || byteLength == 0) {
921 return;
922 }
923
924 fCurrStrike = NULL;
925 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
926
927 // Get GrFontScaler from cache
928 GrFontScaler* fontScaler = GetGrFontScaler(cache);
929
930 const char* stop = text + byteLength;
931 SkTextAlignProc alignProc(skPaint.getTextAlign());
932 SkTextMapStateProc tmsProc(viewMatrix, offset, scalarsPerPosition);
933
934 if (cache->isSubpixel()) {
935 // maybe we should skip the rounding if linearText is set
936 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
937
938 SkFixed fxMask = ~0;
939 SkFixed fyMask = ~0;
940 SkScalar halfSampleX = SkFixedToScalar(SkGlyph::kSubpixelRound);
941 SkScalar halfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
942 if (kX_SkAxisAlignment == baseline) {
943 fyMask = 0;
944 halfSampleY = SK_ScalarHalf;
945 } else if (kY_SkAxisAlignment == baseline) {
946 fxMask = 0;
947 halfSampleX = SK_ScalarHalf;
948 }
949
950 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
951 while (text < stop) {
952 SkPoint tmsLoc;
953 tmsProc(pos, &tmsLoc);
954 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + halfSampleX);
955 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + halfSampleY);
956
957 const SkGlyph& glyph = glyphCacheProc(cache, &text,
958 fx & fxMask, fy & fyMask);
959
960 if (glyph.fWidth) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700961 this->bmpAppendGlyph(blob,
962 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700963 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700964 Sk48Dot16FloorToInt(fx),
965 Sk48Dot16FloorToInt(fy),
966 color,
967 fontScaler,
968 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700969 }
970 pos += scalarsPerPosition;
971 }
972 } else {
973 while (text < stop) {
974 const char* currentText = text;
975 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
976
977 if (metricGlyph.fWidth) {
978 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
979 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
980 SkPoint tmsLoc;
981 tmsProc(pos, &tmsLoc);
982 SkPoint alignLoc;
983 alignProc(tmsLoc, metricGlyph, &alignLoc);
984
985 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + halfSampleX);
986 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + halfSampleY);
987
988 // have to call again, now that we've been "aligned"
989 const SkGlyph& glyph = glyphCacheProc(cache, &currentText,
990 fx & fxMask, fy & fyMask);
991 // the assumption is that the metrics haven't changed
992 SkASSERT(prevAdvX == glyph.fAdvanceX);
993 SkASSERT(prevAdvY == glyph.fAdvanceY);
994 SkASSERT(glyph.fWidth);
995
joshualitt9bd2daf2015-04-17 09:30:06 -0700996 this->bmpAppendGlyph(blob,
997 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700998 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700999 Sk48Dot16FloorToInt(fx),
1000 Sk48Dot16FloorToInt(fy),
1001 color,
1002 fontScaler,
1003 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001004 }
1005 pos += scalarsPerPosition;
1006 }
1007 }
1008 } else { // not subpixel
1009
1010 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
1011 while (text < stop) {
1012 // the last 2 parameters are ignored
1013 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1014
1015 if (glyph.fWidth) {
1016 SkPoint tmsLoc;
1017 tmsProc(pos, &tmsLoc);
1018
1019 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + SK_ScalarHalf); //halfSampleX;
1020 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + SK_ScalarHalf); //halfSampleY;
joshualitt9bd2daf2015-04-17 09:30:06 -07001021 this->bmpAppendGlyph(blob,
1022 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001023 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001024 Sk48Dot16FloorToInt(fx),
1025 Sk48Dot16FloorToInt(fy),
1026 color,
1027 fontScaler,
1028 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001029 }
1030 pos += scalarsPerPosition;
1031 }
1032 } else {
1033 while (text < stop) {
1034 // the last 2 parameters are ignored
1035 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1036
1037 if (glyph.fWidth) {
1038 SkPoint tmsLoc;
1039 tmsProc(pos, &tmsLoc);
1040
1041 SkPoint alignLoc;
1042 alignProc(tmsLoc, glyph, &alignLoc);
1043
1044 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + SK_ScalarHalf); //halfSampleX;
1045 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + SK_ScalarHalf); //halfSampleY;
joshualitt9bd2daf2015-04-17 09:30:06 -07001046 this->bmpAppendGlyph(blob,
1047 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001048 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001049 Sk48Dot16FloorToInt(fx),
1050 Sk48Dot16FloorToInt(fy),
1051 color,
1052 fontScaler,
1053 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001054 }
1055 pos += scalarsPerPosition;
1056 }
1057 }
1058 }
1059}
1060
joshualitt9bd2daf2015-04-17 09:30:06 -07001061
joshualitt374b2f72015-07-21 08:05:03 -07001062void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -07001063 SkGlyphCache* cache, const SkPaint& skPaint,
1064 GrColor color,
1065 const SkMatrix& viewMatrix,
1066 const char text[], size_t byteLength,
1067 SkScalar x, SkScalar y, const SkIRect& clipRect,
1068 SkScalar textRatio,
1069 SkTDArray<char>* fallbackTxt,
1070 SkTDArray<SkScalar>* fallbackPos,
1071 SkPoint* offset,
1072 const SkPaint& origPaint) {
1073 SkASSERT(byteLength == 0 || text != NULL);
1074
1075 // nothing to draw
1076 if (text == NULL || byteLength == 0) {
1077 return;
1078 }
1079
1080 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
1081 SkAutoDescriptor desc;
robertphillipsfcf78292015-06-19 11:49:52 -07001082 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, NULL, true);
joshualitt9bd2daf2015-04-17 09:30:06 -07001083 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
1084 desc.getDesc());
1085
1086 SkTArray<SkScalar> positions;
1087
1088 const char* textPtr = text;
1089 SkFixed stopX = 0;
1090 SkFixed stopY = 0;
1091 SkFixed origin = 0;
1092 switch (origPaint.getTextAlign()) {
1093 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
1094 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
1095 case SkPaint::kLeft_Align: origin = 0; break;
1096 }
1097
1098 SkAutoKern autokern;
1099 const char* stop = text + byteLength;
1100 while (textPtr < stop) {
1101 // don't need x, y here, since all subpixel variants will have the
1102 // same advance
1103 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
1104
1105 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
1106 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
1107
1108 SkFixed height = glyph.fAdvanceY;
1109 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
1110
1111 stopX += width;
1112 stopY += height;
1113 }
1114 SkASSERT(textPtr == stop);
1115
1116 // now adjust starting point depending on alignment
1117 SkScalar alignX = SkFixedToScalar(stopX);
1118 SkScalar alignY = SkFixedToScalar(stopY);
1119 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
1120 alignX = SkScalarHalf(alignX);
1121 alignY = SkScalarHalf(alignY);
1122 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
1123 alignX = 0;
1124 alignY = 0;
1125 }
1126 x -= alignX;
1127 y -= alignY;
1128 *offset = SkPoint::Make(x, y);
1129
1130 this->internalDrawDFPosText(blob, runIndex, cache, skPaint, color, viewMatrix, text, byteLength,
1131 positions.begin(), 2, *offset, clipRect, textRatio, fallbackTxt,
1132 fallbackPos);
1133 SkGlyphCache::AttachCache(origPaintCache);
1134}
1135
joshualitt374b2f72015-07-21 08:05:03 -07001136void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -07001137 SkGlyphCache* cache, const SkPaint& skPaint,
1138 GrColor color,
1139 const SkMatrix& viewMatrix,
1140 const char text[], size_t byteLength,
1141 const SkScalar pos[], int scalarsPerPosition,
1142 const SkPoint& offset, const SkIRect& clipRect,
1143 SkScalar textRatio,
1144 SkTDArray<char>* fallbackTxt,
1145 SkTDArray<SkScalar>* fallbackPos) {
1146
1147 SkASSERT(byteLength == 0 || text != NULL);
1148 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1149
1150 // nothing to draw
1151 if (text == NULL || byteLength == 0) {
1152 return;
1153 }
1154
1155 fCurrStrike = NULL;
1156
1157 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
1158 GrFontScaler* fontScaler = GetGrFontScaler(cache);
1159
1160 const char* stop = text + byteLength;
1161
1162 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
1163 while (text < stop) {
1164 const char* lastText = text;
1165 // the last 2 parameters are ignored
1166 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1167
1168 if (glyph.fWidth) {
1169 SkScalar x = offset.x() + pos[0];
1170 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
1171
1172 if (!this->dfAppendGlyph(blob,
1173 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001174 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001175 x, y, color, fontScaler, clipRect,
1176 textRatio, viewMatrix)) {
1177 // couldn't append, send to fallback
1178 fallbackTxt->append(SkToInt(text-lastText), lastText);
1179 *fallbackPos->append() = pos[0];
1180 if (2 == scalarsPerPosition) {
1181 *fallbackPos->append() = pos[1];
1182 }
1183 }
1184 }
1185 pos += scalarsPerPosition;
1186 }
1187 } else {
1188 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
1189 : SK_Scalar1;
1190 while (text < stop) {
1191 const char* lastText = text;
1192 // the last 2 parameters are ignored
1193 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1194
1195 if (glyph.fWidth) {
1196 SkScalar x = offset.x() + pos[0];
1197 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
1198
1199 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
1200 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
1201
1202 if (!this->dfAppendGlyph(blob,
1203 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001204 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001205 x - advanceX, y - advanceY, color,
1206 fontScaler,
1207 clipRect,
1208 textRatio,
1209 viewMatrix)) {
1210 // couldn't append, send to fallback
1211 fallbackTxt->append(SkToInt(text-lastText), lastText);
1212 *fallbackPos->append() = pos[0];
1213 if (2 == scalarsPerPosition) {
1214 *fallbackPos->append() = pos[1];
1215 }
1216 }
1217 }
1218 pos += scalarsPerPosition;
1219 }
1220 }
1221}
1222
joshualitt374b2f72015-07-21 08:05:03 -07001223void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001224 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001225 int vx, int vy, GrColor color, GrFontScaler* scaler,
1226 const SkIRect& clipRect) {
joshualittae32c102015-04-21 09:37:57 -07001227 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001228 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001229 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1230 }
1231
joshualitt6c2c2b02015-07-24 10:37:00 -07001232 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1233 skGlyph.getSubXFixed(),
1234 skGlyph.getSubYFixed(),
1235 GrGlyph::kCoverage_MaskStyle);
1236 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001237 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001238 return;
1239 }
1240
1241 int x = vx + glyph->fBounds.fLeft;
1242 int y = vy + glyph->fBounds.fTop;
1243
1244 // keep them as ints until we've done the clip-test
1245 int width = glyph->fBounds.width();
1246 int height = glyph->fBounds.height();
1247
joshualitt2a0e9f32015-04-13 06:12:21 -07001248#if 0
1249 // Not checking the clip bounds might introduce a performance regression. However, its not
1250 // clear if this is still true today with the larger tiles we use in Chrome. For repositionable
1251 // blobs, we want to make sure we have all of the glyphs, so clipping them out is not ideal.
1252 // We could store the cliprect in the key, but then we'd lose the ability to do integer scrolls
1253 // TODO verify this
joshualitt1d89e8d2015-04-01 12:40:54 -07001254 // check if we clipped out
1255 if (clipRect.quickReject(x, y, x + width, y + height)) {
1256 return;
1257 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001258#endif
joshualitt1d89e8d2015-04-01 12:40:54 -07001259
1260 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001261 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001262 this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy));
joshualitt1d89e8d2015-04-01 12:40:54 -07001263 return;
1264 }
1265
joshualitt1d89e8d2015-04-01 12:40:54 -07001266 GrMaskFormat format = glyph->fMaskFormat;
1267
1268 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
1269 if (run.fInitialized && subRun->fMaskFormat != format) {
joshualittd9f13ae2015-07-24 11:24:31 -07001270 subRun = &run.push_back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001271 subRun->fStrike.reset(SkRef(fCurrStrike));
1272 } else if (!run.fInitialized) {
1273 subRun->fStrike.reset(SkRef(fCurrStrike));
joshualitt1d89e8d2015-04-01 12:40:54 -07001274 }
1275
1276 run.fInitialized = true;
joshualitt1d89e8d2015-04-01 12:40:54 -07001277
1278 size_t vertexStride = get_vertex_stride(format);
1279
1280 SkRect r;
1281 r.fLeft = SkIntToScalar(x);
1282 r.fTop = SkIntToScalar(y);
1283 r.fRight = r.fLeft + SkIntToScalar(width);
1284 r.fBottom = r.fTop + SkIntToScalar(height);
joshualitt9bd2daf2015-04-17 09:30:06 -07001285 subRun->fMaskFormat = format;
1286 this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format,
joshualittae32c102015-04-21 09:37:57 -07001287 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001288}
joshualitt1d89e8d2015-04-01 12:40:54 -07001289
joshualitt374b2f72015-07-21 08:05:03 -07001290bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001291 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001292 SkScalar sx, SkScalar sy, GrColor color,
1293 GrFontScaler* scaler,
1294 const SkIRect& clipRect,
1295 SkScalar textRatio, const SkMatrix& viewMatrix) {
joshualittae32c102015-04-21 09:37:57 -07001296 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001297 if (!fCurrStrike) {
1298 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1299 }
1300
joshualitt6c2c2b02015-07-24 10:37:00 -07001301 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1302 skGlyph.getSubXFixed(),
1303 skGlyph.getSubYFixed(),
1304 GrGlyph::kDistance_MaskStyle);
1305 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001306 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001307 return true;
1308 }
1309
1310 // fallback to color glyph support
1311 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
1312 return false;
1313 }
1314
1315 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
1316 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
1317 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
1318 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
1319
1320 SkScalar scale = textRatio;
1321 dx *= scale;
1322 dy *= scale;
1323 width *= scale;
1324 height *= scale;
1325 sx += dx;
1326 sy += dy;
1327 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
1328
1329#if 0
1330 // check if we clipped out
1331 SkRect dstRect;
1332 viewMatrix.mapRect(&dstRect, glyphRect);
1333 if (clipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
1334 SkScalarTruncToInt(dstRect.top()),
1335 SkScalarTruncToInt(dstRect.right()),
1336 SkScalarTruncToInt(dstRect.bottom()))) {
1337 return true;
1338 }
1339#endif
1340
1341 // TODO combine with the above
1342 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001343 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001344 this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy);
joshualitt9bd2daf2015-04-17 09:30:06 -07001345 return true;
1346 }
1347
joshualitt9bd2daf2015-04-17 09:30:06 -07001348 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001349 if (!run.fInitialized) {
1350 subRun->fStrike.reset(SkRef(fCurrStrike));
1351 }
1352 run.fInitialized = true;
joshualitt9bd2daf2015-04-17 09:30:06 -07001353 SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat);
1354 subRun->fMaskFormat = kA8_GrMaskFormat;
1355
1356 size_t vertexStride = get_vertex_stride_df(kA8_GrMaskFormat, subRun->fUseLCDText);
1357
1358 bool useColorVerts = !subRun->fUseLCDText;
1359 this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts,
joshualittae32c102015-04-21 09:37:57 -07001360 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001361 return true;
1362}
1363
joshualitt374b2f72015-07-21 08:05:03 -07001364inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001365 GrFontScaler* scaler, const SkGlyph& skGlyph,
1366 SkScalar x, SkScalar y) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001367 if (NULL == glyph->fPath) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001368 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
1369 if (!glyphPath) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001370 return;
1371 }
joshualitt6c2c2b02015-07-24 10:37:00 -07001372
1373 glyph->fPath = SkNEW_ARGS(SkPath, (*glyphPath));
joshualitt9bd2daf2015-04-17 09:30:06 -07001374 }
joshualitt374b2f72015-07-21 08:05:03 -07001375 blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y));
joshualitt9bd2daf2015-04-17 09:30:06 -07001376}
1377
joshualitt374b2f72015-07-21 08:05:03 -07001378inline void GrAtlasTextContext::appendGlyphCommon(GrAtlasTextBlob* blob, Run* run,
joshualitt9bd2daf2015-04-17 09:30:06 -07001379 Run::SubRunInfo* subRun,
1380 const SkRect& positions, GrColor color,
1381 size_t vertexStride, bool useVertexColor,
joshualittae32c102015-04-21 09:37:57 -07001382 GrGlyph* glyph) {
1383 blob->fGlyphs[subRun->fGlyphEndIndex] = glyph;
joshualitt9bd2daf2015-04-17 09:30:06 -07001384 run->fVertexBounds.joinNonEmptyArg(positions);
1385 run->fColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -07001386
1387 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->fVertexEndIndex);
1388
joshualitt9bd2daf2015-04-17 09:30:06 -07001389 if (useVertexColor) {
joshualitt010db532015-04-21 10:07:26 -07001390 // V0
1391 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1392 position->set(positions.fLeft, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001393 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
1394 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001395 vertex += vertexStride;
joshualitt9bd2daf2015-04-17 09:30:06 -07001396
joshualitt010db532015-04-21 10:07:26 -07001397 // V1
1398 position = reinterpret_cast<SkPoint*>(vertex);
1399 position->set(positions.fLeft, positions.fBottom);
1400 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001401 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001402 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001403
joshualitt010db532015-04-21 10:07:26 -07001404 // V2
1405 position = reinterpret_cast<SkPoint*>(vertex);
1406 position->set(positions.fRight, positions.fBottom);
1407 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001408 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001409 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001410
joshualitt010db532015-04-21 10:07:26 -07001411 // V3
1412 position = reinterpret_cast<SkPoint*>(vertex);
1413 position->set(positions.fRight, positions.fTop);
1414 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001415 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001416 } else {
1417 // V0
1418 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1419 position->set(positions.fLeft, positions.fTop);
1420 vertex += vertexStride;
1421
1422 // V1
1423 position = reinterpret_cast<SkPoint*>(vertex);
1424 position->set(positions.fLeft, positions.fBottom);
1425 vertex += vertexStride;
1426
1427 // V2
1428 position = reinterpret_cast<SkPoint*>(vertex);
1429 position->set(positions.fRight, positions.fBottom);
1430 vertex += vertexStride;
1431
1432 // V3
1433 position = reinterpret_cast<SkPoint*>(vertex);
1434 position->set(positions.fRight, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001435 }
1436
1437 subRun->fGlyphEndIndex++;
1438 subRun->fVertexEndIndex += vertexStride * kVerticesPerGlyph;
1439}
1440
bsalomonabd30f52015-08-13 13:34:48 -07001441class TextBatch : public GrVertexBatch {
joshualitt1d89e8d2015-04-01 12:40:54 -07001442public:
joshualitt9bd2daf2015-04-17 09:30:06 -07001443 typedef GrAtlasTextContext::DistanceAdjustTable DistanceAdjustTable;
joshualitt374b2f72015-07-21 08:05:03 -07001444 typedef GrAtlasTextBlob Blob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001445 typedef Blob::Run Run;
1446 typedef Run::SubRunInfo TextInfo;
1447 struct Geometry {
joshualittad802c62015-04-15 05:31:57 -07001448 Blob* fBlob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001449 int fRun;
1450 int fSubRun;
1451 GrColor fColor;
joshualitt2a0e9f32015-04-13 06:12:21 -07001452 SkScalar fTransX;
1453 SkScalar fTransY;
joshualitt1d89e8d2015-04-01 12:40:54 -07001454 };
1455
bsalomon265697d2015-07-22 10:17:26 -07001456 static TextBatch* CreateBitmap(GrMaskFormat maskFormat, int glyphCount,
joshualittad802c62015-04-15 05:31:57 -07001457 GrBatchFontCache* fontCache) {
bsalomon265697d2015-07-22 10:17:26 -07001458 TextBatch* batch = SkNEW(TextBatch);
1459
1460 batch->initClassID<TextBatch>();
1461 batch->fFontCache = fontCache;
1462 switch (maskFormat) {
1463 case kA8_GrMaskFormat:
1464 batch->fMaskType = kGrayscaleCoverageMask_MaskType;
1465 break;
1466 case kA565_GrMaskFormat:
1467 batch->fMaskType = kLCDCoverageMask_MaskType;
1468 break;
1469 case kARGB_GrMaskFormat:
1470 batch->fMaskType = kColorBitmapMask_MaskType;
1471 break;
1472 }
1473 batch->fBatch.fNumGlyphs = glyphCount;
bsalomond602f4d2015-07-27 06:12:01 -07001474 batch->fGeoCount = 1;
bsalomon265697d2015-07-22 10:17:26 -07001475 batch->fFilteredColor = 0;
1476 batch->fFontCache = fontCache;
1477 batch->fUseBGR = false;
1478 return batch;
joshualitt1d89e8d2015-04-01 12:40:54 -07001479 }
1480
bsalomon265697d2015-07-22 10:17:26 -07001481 static TextBatch* CreateDistanceField(int glyphCount, GrBatchFontCache* fontCache,
1482 DistanceAdjustTable* distanceAdjustTable,
1483 SkColor filteredColor, bool isLCD,
1484 bool useBGR) {
1485 TextBatch* batch = SkNEW(TextBatch);
1486 batch->initClassID<TextBatch>();
1487 batch->fFontCache = fontCache;
1488 batch->fMaskType = isLCD ? kLCDDistanceField_MaskType : kGrayscaleDistanceField_MaskType;
1489 batch->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
1490 batch->fFilteredColor = filteredColor;
1491 batch->fUseBGR = useBGR;
1492 batch->fBatch.fNumGlyphs = glyphCount;
bsalomond602f4d2015-07-27 06:12:01 -07001493 batch->fGeoCount = 1;
bsalomon265697d2015-07-22 10:17:26 -07001494 return batch;
joshualitt9bd2daf2015-04-17 09:30:06 -07001495 }
1496
bsalomone46f9fe2015-08-18 06:05:14 -07001497 // to avoid even the initial copy of the struct, we have a getter for the first item which
1498 // is used to seed the batch with its initial geometry. After seeding, the client should call
1499 // init() so the Batch can initialize itself
1500 Geometry& geometry() { return fGeoData[0]; }
1501
1502 void init() {
1503 const Geometry& geo = fGeoData[0];
1504 fBatch.fColor = geo.fColor;
1505 fBatch.fViewMatrix = geo.fBlob->fViewMatrix;
1506
1507 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
1508 // into device space
1509 const Run& run = geo.fBlob->fRuns[geo.fRun];
1510 if (run.fSubRunInfo[geo.fSubRun].fDrawAsDistanceFields) {
1511 SkRect bounds = run.fVertexBounds;
1512 fBatch.fViewMatrix.mapRect(&bounds);
1513 this->setBounds(bounds);
1514 } else {
1515 this->setBounds(run.fVertexBounds);
1516 }
1517 }
1518
bsalomon265697d2015-07-22 10:17:26 -07001519 const char* name() const override { return "TextBatch"; }
joshualitt1d89e8d2015-04-01 12:40:54 -07001520
1521 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001522 if (kColorBitmapMask_MaskType == fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001523 out->setUnknownFourComponents();
1524 } else {
1525 out->setKnownFourComponents(fBatch.fColor);
1526 }
1527 }
1528
1529 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001530 switch (fMaskType) {
1531 case kGrayscaleDistanceField_MaskType:
1532 case kGrayscaleCoverageMask_MaskType:
joshualitt1d89e8d2015-04-01 12:40:54 -07001533 out->setUnknownSingleComponent();
bsalomon265697d2015-07-22 10:17:26 -07001534 break;
1535 case kLCDCoverageMask_MaskType:
1536 case kLCDDistanceField_MaskType:
1537 out->setUnknownOpaqueFourComponents();
joshualitt1d89e8d2015-04-01 12:40:54 -07001538 out->setUsingLCDCoverage();
bsalomon265697d2015-07-22 10:17:26 -07001539 break;
1540 case kColorBitmapMask_MaskType:
1541 out->setKnownSingleComponent(0xff);
joshualitt1d89e8d2015-04-01 12:40:54 -07001542 }
1543 }
1544
bsalomone46f9fe2015-08-18 06:05:14 -07001545private:
bsalomon91d844d2015-08-10 10:47:29 -07001546 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001547 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -07001548 if (!opt.readsColor()) {
joshualitt416e14f2015-07-10 09:05:57 -07001549 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001550 }
bsalomon91d844d2015-08-10 10:47:29 -07001551 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt1d89e8d2015-04-01 12:40:54 -07001552
1553 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -07001554 fBatch.fColorIgnored = !opt.readsColor();
joshualitt416e14f2015-07-10 09:05:57 -07001555 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -07001556 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
1557 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt1d89e8d2015-04-01 12:40:54 -07001558 }
1559
bsalomonb5238a72015-05-05 07:49:49 -07001560 struct FlushInfo {
1561 SkAutoTUnref<const GrVertexBuffer> fVertexBuffer;
1562 SkAutoTUnref<const GrIndexBuffer> fIndexBuffer;
1563 int fGlyphsToFlush;
1564 int fVertexOffset;
1565 };
1566
bsalomon75398562015-08-17 12:55:38 -07001567 void onPrepareDraws(Target* target) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001568 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
1569 // TODO actually only invert if we don't have RGBA
1570 SkMatrix localMatrix;
1571 if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) {
1572 SkDebugf("Cannot invert viewmatrix\n");
1573 return;
1574 }
1575
bsalomon265697d2015-07-22 10:17:26 -07001576 GrTexture* texture = fFontCache->getTexture(this->maskFormat());
joshualitt62db8ba2015-04-09 08:22:37 -07001577 if (!texture) {
1578 SkDebugf("Could not allocate backing texture for atlas\n");
1579 return;
1580 }
1581
bsalomon265697d2015-07-22 10:17:26 -07001582 bool usesDistanceFields = this->usesDistanceFields();
1583 GrMaskFormat maskFormat = this->maskFormat();
1584 bool isLCD = this->isLCD();
1585
joshualitt9bd2daf2015-04-17 09:30:06 -07001586 SkAutoTUnref<const GrGeometryProcessor> gp;
bsalomon265697d2015-07-22 10:17:26 -07001587 if (usesDistanceFields) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001588 gp.reset(this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(),
1589 texture));
1590 } else {
1591 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
joshualitt9bd2daf2015-04-17 09:30:06 -07001592 gp.reset(GrBitmapTextGeoProc::Create(this->color(),
1593 texture,
1594 params,
bsalomon265697d2015-07-22 10:17:26 -07001595 maskFormat,
joshualittb8c241a2015-05-19 08:23:30 -07001596 localMatrix,
1597 this->usesLocalCoords()));
joshualitt9bd2daf2015-04-17 09:30:06 -07001598 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001599
bsalomonb5238a72015-05-05 07:49:49 -07001600 FlushInfo flushInfo;
1601 flushInfo.fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001602 size_t vertexStride = gp->getVertexStride();
bsalomon265697d2015-07-22 10:17:26 -07001603 SkASSERT(vertexStride == (usesDistanceFields ?
1604 get_vertex_stride_df(maskFormat, isLCD) :
1605 get_vertex_stride(maskFormat)));
joshualitt1d89e8d2015-04-01 12:40:54 -07001606
bsalomon75398562015-08-17 12:55:38 -07001607 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001608
1609 int glyphCount = this->numGlyphs();
bsalomon8415abe2015-05-04 11:41:41 -07001610 const GrVertexBuffer* vertexBuffer;
bsalomonb5238a72015-05-05 07:49:49 -07001611
bsalomon75398562015-08-17 12:55:38 -07001612 void* vertices = target->makeVertexSpace(vertexStride,
1613 glyphCount * kVerticesPerGlyph,
1614 &vertexBuffer,
1615 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -07001616 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
bsalomon75398562015-08-17 12:55:38 -07001617 flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
bsalomonb5238a72015-05-05 07:49:49 -07001618 if (!vertices || !flushInfo.fVertexBuffer) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001619 SkDebugf("Could not allocate vertices\n");
1620 return;
1621 }
1622
1623 unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices);
1624
joshualitt25ba7ea2015-04-21 07:49:49 -07001625 // We cache some values to avoid going to the glyphcache for the same fontScaler twice
1626 // in a row
1627 const SkDescriptor* desc = NULL;
1628 SkGlyphCache* cache = NULL;
1629 GrFontScaler* scaler = NULL;
joshualitt25ba7ea2015-04-21 07:49:49 -07001630 SkTypeface* typeface = NULL;
1631
bsalomond602f4d2015-07-27 06:12:01 -07001632 for (int i = 0; i < fGeoCount; i++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001633 Geometry& args = fGeoData[i];
1634 Blob* blob = args.fBlob;
1635 Run& run = blob->fRuns[args.fRun];
1636 TextInfo& info = run.fSubRunInfo[args.fSubRun];
1637
bsalomon265697d2015-07-22 10:17:26 -07001638 uint64_t currentAtlasGen = fFontCache->atlasGeneration(maskFormat);
joshualitt7a9c45c2015-05-26 12:32:23 -07001639 bool regenerateTextureCoords = info.fAtlasGeneration != currentAtlasGen ||
joshualitt7e97b0b2015-07-31 15:18:08 -07001640 info.fStrike->isAbandoned();
joshualitt9bd2daf2015-04-17 09:30:06 -07001641 bool regenerateColors;
bsalomon265697d2015-07-22 10:17:26 -07001642 if (usesDistanceFields) {
1643 regenerateColors = !isLCD && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001644 } else {
bsalomon265697d2015-07-22 10:17:26 -07001645 regenerateColors = kA8_GrMaskFormat == maskFormat && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001646 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001647 bool regeneratePositions = args.fTransX != 0.f || args.fTransY != 0.f;
joshualitt1d89e8d2015-04-01 12:40:54 -07001648 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1649
1650 // We regenerate both texture coords and colors in the blob itself, and update the
1651 // atlas generation. If we don't end up purging any unused plots, we can avoid
1652 // regenerating the coords. We could take a finer grained approach to updating texture
1653 // coords but its not clear if the extra bookkeeping would offset any gains.
1654 // To avoid looping over the glyphs twice, we do one loop and conditionally update color
1655 // or coords as needed. One final note, if we have to break a run for an atlas eviction
1656 // then we can't really trust the atlas has all of the correct data. Atlas evictions
1657 // should be pretty rare, so we just always regenerate in those cases
joshualitt2a0e9f32015-04-13 06:12:21 -07001658 if (regenerateTextureCoords || regenerateColors || regeneratePositions) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001659 // first regenerate texture coordinates / colors if need be
joshualitt1d89e8d2015-04-01 12:40:54 -07001660 bool brokenRun = false;
joshualittae32c102015-04-21 09:37:57 -07001661
1662 // Because the GrBatchFontCache may evict the strike a blob depends on using for
1663 // generating its texture coords, we have to track whether or not the strike has
1664 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
1665 // otherwise we have to get the new strike, and use that to get the correct glyphs.
1666 // Because we do not have the packed ids, and thus can't look up our glyphs in the
1667 // new strike, we instead keep our ref to the old strike and use the packed ids from
1668 // it. These ids will still be valid as long as we hold the ref. When we are done
1669 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
1670 bool regenerateGlyphs = false;
1671 GrBatchTextStrike* strike = NULL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001672 if (regenerateTextureCoords) {
joshualittb4c507e2015-04-08 08:07:59 -07001673 info.fBulkUseToken.reset();
joshualitt25ba7ea2015-04-21 07:49:49 -07001674
1675 // We can reuse if we have a valid strike and our descriptors / typeface are the
1676 // same
joshualitt97202d22015-04-22 13:47:02 -07001677 const SkDescriptor* newDesc = run.fOverrideDescriptor ?
1678 run.fOverrideDescriptor->getDesc() :
joshualitt25ba7ea2015-04-21 07:49:49 -07001679 run.fDescriptor.getDesc();
1680 if (!cache || !SkTypeface::Equal(typeface, run.fTypeface) ||
1681 !(desc->equals(*newDesc))) {
1682 if (cache) {
1683 SkGlyphCache::AttachCache(cache);
1684 }
1685 desc = newDesc;
1686 cache = SkGlyphCache::DetachCache(run.fTypeface, desc);
1687 scaler = GrTextContext::GetGrFontScaler(cache);
joshualitt7e97b0b2015-07-31 15:18:08 -07001688 strike = info.fStrike;
joshualitt25ba7ea2015-04-21 07:49:49 -07001689 typeface = run.fTypeface;
1690 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001691
joshualitt7e97b0b2015-07-31 15:18:08 -07001692 if (info.fStrike->isAbandoned()) {
joshualittae32c102015-04-21 09:37:57 -07001693 regenerateGlyphs = true;
1694 strike = fFontCache->getStrike(scaler);
1695 } else {
joshualitt7e97b0b2015-07-31 15:18:08 -07001696 strike = info.fStrike;
joshualittae32c102015-04-21 09:37:57 -07001697 }
1698 }
1699
1700 for (int glyphIdx = 0; glyphIdx < glyphCount; glyphIdx++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001701 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001702 size_t glyphOffset = glyphIdx + info.fGlyphStartIndex;
joshualitt6c2c2b02015-07-24 10:37:00 -07001703
1704 GrGlyph* glyph = blob->fGlyphs[glyphOffset];
1705 GrGlyph::PackedID id = glyph->fPackedID;
1706 const SkGlyph& skGlyph = scaler->grToSkGlyph(id);
joshualittae32c102015-04-21 09:37:57 -07001707 if (regenerateGlyphs) {
1708 // Get the id from the old glyph, and use the new strike to lookup
1709 // the glyph.
joshualitt76cc6572015-07-31 05:51:45 -07001710 blob->fGlyphs[glyphOffset] = strike->getGlyph(skGlyph, id, maskFormat,
1711 scaler);
joshualittae32c102015-04-21 09:37:57 -07001712 }
1713 glyph = blob->fGlyphs[glyphOffset];
joshualitt1d89e8d2015-04-01 12:40:54 -07001714 SkASSERT(glyph);
joshualitt65e96b42015-07-31 11:45:22 -07001715 SkASSERT(id == glyph->fPackedID);
1716 // We want to be able to assert this but cannot for testing purposes.
1717 // once skbug:4143 has landed we can revist this assert
1718 //SkASSERT(glyph->fMaskFormat == this->maskFormat());
joshualitt1d89e8d2015-04-01 12:40:54 -07001719
1720 if (!fFontCache->hasGlyph(glyph) &&
bsalomon75398562015-08-17 12:55:38 -07001721 !strike->addGlyphToAtlas(target, glyph, scaler, skGlyph, maskFormat)) {
1722 this->flush(target, &flushInfo);
1723 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001724 brokenRun = glyphIdx > 0;
1725
bsalomon75398562015-08-17 12:55:38 -07001726 SkDEBUGCODE(bool success =) strike->addGlyphToAtlas(target,
joshualittae32c102015-04-21 09:37:57 -07001727 glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001728 scaler,
joshualitt4f19ca32015-07-30 07:59:20 -07001729 skGlyph,
1730 maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001731 SkASSERT(success);
1732 }
joshualittb4c507e2015-04-08 08:07:59 -07001733 fFontCache->addGlyphToBulkAndSetUseToken(&info.fBulkUseToken, glyph,
bsalomon75398562015-08-17 12:55:38 -07001734 target->currentToken());
joshualitt1d89e8d2015-04-01 12:40:54 -07001735
1736 // Texture coords are the last vertex attribute so we get a pointer to the
1737 // first one and then map with stride in regenerateTextureCoords
1738 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1739 vertex += info.fVertexStartIndex;
1740 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1741 vertex += vertexStride - sizeof(SkIPoint16);
1742
1743 this->regenerateTextureCoords(glyph, vertex, vertexStride);
1744 }
1745
1746 if (regenerateColors) {
1747 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1748 vertex += info.fVertexStartIndex;
1749 vertex += vertexStride * glyphIdx * kVerticesPerGlyph + sizeof(SkPoint);
1750 this->regenerateColors(vertex, vertexStride, args.fColor);
1751 }
1752
joshualitt2a0e9f32015-04-13 06:12:21 -07001753 if (regeneratePositions) {
1754 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1755 vertex += info.fVertexStartIndex;
1756 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1757 SkScalar transX = args.fTransX;
1758 SkScalar transY = args.fTransY;
1759 this->regeneratePositions(vertex, vertexStride, transX, transY);
1760 }
bsalomonb5238a72015-05-05 07:49:49 -07001761 flushInfo.fGlyphsToFlush++;
joshualitt1d89e8d2015-04-01 12:40:54 -07001762 }
1763
joshualitt2a0e9f32015-04-13 06:12:21 -07001764 // We my have changed the color so update it here
1765 run.fColor = args.fColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07001766 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001767 if (regenerateGlyphs) {
joshualitt7e97b0b2015-07-31 15:18:08 -07001768 info.fStrike.reset(SkRef(strike));
joshualittae32c102015-04-21 09:37:57 -07001769 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001770 info.fAtlasGeneration = brokenRun ? GrBatchAtlas::kInvalidAtlasGeneration :
bsalomon265697d2015-07-22 10:17:26 -07001771 fFontCache->atlasGeneration(maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001772 }
1773 } else {
bsalomonb5238a72015-05-05 07:49:49 -07001774 flushInfo.fGlyphsToFlush += glyphCount;
joshualittb4c507e2015-04-08 08:07:59 -07001775
1776 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1777 // have a valid atlas generation
bsalomon75398562015-08-17 12:55:38 -07001778 fFontCache->setUseTokenBulk(info.fBulkUseToken, target->currentToken(), maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001779 }
1780
1781 // now copy all vertices
1782 size_t byteCount = info.fVertexEndIndex - info.fVertexStartIndex;
1783 memcpy(currVertex, blob->fVertices + info.fVertexStartIndex, byteCount);
1784
1785 currVertex += byteCount;
1786 }
joshualitt25ba7ea2015-04-21 07:49:49 -07001787 // Make sure to attach the last cache if applicable
1788 if (cache) {
1789 SkGlyphCache::AttachCache(cache);
1790 }
bsalomon75398562015-08-17 12:55:38 -07001791 this->flush(target, &flushInfo);
joshualitt1d89e8d2015-04-01 12:40:54 -07001792 }
1793
bsalomon265697d2015-07-22 10:17:26 -07001794 TextBatch() {} // initialized in factory functions.
joshualittad802c62015-04-15 05:31:57 -07001795
bsalomon265697d2015-07-22 10:17:26 -07001796 ~TextBatch() {
bsalomond602f4d2015-07-27 06:12:01 -07001797 for (int i = 0; i < fGeoCount; i++) {
joshualittad802c62015-04-15 05:31:57 -07001798 fGeoData[i].fBlob->unref();
1799 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001800 }
1801
bsalomon265697d2015-07-22 10:17:26 -07001802 GrMaskFormat maskFormat() const {
1803 switch (fMaskType) {
1804 case kLCDCoverageMask_MaskType:
1805 return kA565_GrMaskFormat;
1806 case kColorBitmapMask_MaskType:
1807 return kARGB_GrMaskFormat;
1808 case kGrayscaleCoverageMask_MaskType:
1809 case kGrayscaleDistanceField_MaskType:
1810 case kLCDDistanceField_MaskType:
1811 return kA8_GrMaskFormat;
1812 }
1813 return kA8_GrMaskFormat; // suppress warning
1814 }
1815
1816 bool usesDistanceFields() const {
1817 return kGrayscaleDistanceField_MaskType == fMaskType ||
1818 kLCDDistanceField_MaskType == fMaskType;
1819 }
1820
1821 bool isLCD() const {
1822 return kLCDCoverageMask_MaskType == fMaskType ||
1823 kLCDDistanceField_MaskType == fMaskType;
1824 }
1825
joshualitt1d89e8d2015-04-01 12:40:54 -07001826 void regenerateTextureCoords(GrGlyph* glyph, intptr_t vertex, size_t vertexStride) {
1827 int width = glyph->fBounds.width();
1828 int height = glyph->fBounds.height();
joshualitt1d89e8d2015-04-01 12:40:54 -07001829
joshualitt9bd2daf2015-04-17 09:30:06 -07001830 int u0, v0, u1, v1;
bsalomon265697d2015-07-22 10:17:26 -07001831 if (this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001832 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
1833 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
1834 u1 = u0 + width - 2 * SK_DistanceFieldInset;
1835 v1 = v0 + height - 2 * SK_DistanceFieldInset;
1836 } else {
1837 u0 = glyph->fAtlasLocation.fX;
1838 v0 = glyph->fAtlasLocation.fY;
1839 u1 = u0 + width;
1840 v1 = v0 + height;
1841 }
1842
joshualitt1d89e8d2015-04-01 12:40:54 -07001843 SkIPoint16* textureCoords;
1844 // V0
1845 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1846 textureCoords->set(u0, v0);
1847 vertex += vertexStride;
1848
1849 // V1
1850 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1851 textureCoords->set(u0, v1);
1852 vertex += vertexStride;
1853
1854 // V2
1855 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1856 textureCoords->set(u1, v1);
1857 vertex += vertexStride;
1858
1859 // V3
1860 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1861 textureCoords->set(u1, v0);
1862 }
1863
1864 void regenerateColors(intptr_t vertex, size_t vertexStride, GrColor color) {
1865 for (int i = 0; i < kVerticesPerGlyph; i++) {
1866 SkColor* vcolor = reinterpret_cast<SkColor*>(vertex);
1867 *vcolor = color;
1868 vertex += vertexStride;
1869 }
1870 }
1871
joshualitt2a0e9f32015-04-13 06:12:21 -07001872 void regeneratePositions(intptr_t vertex, size_t vertexStride, SkScalar transX,
1873 SkScalar transY) {
1874 for (int i = 0; i < kVerticesPerGlyph; i++) {
1875 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
1876 point->fX += transX;
1877 point->fY += transY;
1878 vertex += vertexStride;
1879 }
1880 }
1881
bsalomon75398562015-08-17 12:55:38 -07001882 void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) {
bsalomoncb8979d2015-05-05 09:51:38 -07001883 GrVertices vertices;
bsalomonb5238a72015-05-05 07:49:49 -07001884 int maxGlyphsPerDraw = flushInfo->fIndexBuffer->maxQuads();
bsalomoncb8979d2015-05-05 09:51:38 -07001885 vertices.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001886 flushInfo->fIndexBuffer, flushInfo->fVertexOffset,
bsalomone64eb572015-05-07 11:35:55 -07001887 kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
bsalomonb5238a72015-05-05 07:49:49 -07001888 maxGlyphsPerDraw);
bsalomon75398562015-08-17 12:55:38 -07001889 target->draw(vertices);
bsalomonb5238a72015-05-05 07:49:49 -07001890 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
1891 flushInfo->fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001892 }
1893
1894 GrColor color() const { return fBatch.fColor; }
1895 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1896 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1897 int numGlyphs() const { return fBatch.fNumGlyphs; }
1898
bsalomoncb02b382015-08-12 11:14:50 -07001899 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001900 TextBatch* that = t->cast<TextBatch>();
1901 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1902 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001903 return false;
1904 }
1905
bsalomon265697d2015-07-22 10:17:26 -07001906 if (fMaskType != that->fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001907 return false;
1908 }
1909
bsalomon265697d2015-07-22 10:17:26 -07001910 if (!this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001911 // TODO we can often batch across LCD text if we have dual source blending and don't
1912 // have to use the blend constant
bsalomon265697d2015-07-22 10:17:26 -07001913 if (kGrayscaleCoverageMask_MaskType != fMaskType && this->color() != that->color()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001914 return false;
1915 }
joshualitt9bd2daf2015-04-17 09:30:06 -07001916 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1917 return false;
1918 }
1919 } else {
joshualitt9bd2daf2015-04-17 09:30:06 -07001920 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1921 return false;
1922 }
1923
1924 if (fFilteredColor != that->fFilteredColor) {
1925 return false;
1926 }
1927
joshualitt9bd2daf2015-04-17 09:30:06 -07001928 if (fUseBGR != that->fUseBGR) {
1929 return false;
1930 }
1931
joshualitt9bd2daf2015-04-17 09:30:06 -07001932 // TODO see note above
bsalomon265697d2015-07-22 10:17:26 -07001933 if (kLCDDistanceField_MaskType == fMaskType && this->color() != that->color()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001934 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001935 }
1936
1937 fBatch.fNumGlyphs += that->numGlyphs();
joshualittad802c62015-04-15 05:31:57 -07001938
bsalomond602f4d2015-07-27 06:12:01 -07001939 // Reallocate space for geo data if necessary and then import that's geo data.
1940 int newGeoCount = that->fGeoCount + fGeoCount;
1941 // We assume (and here enforce) that the allocation size is the smallest power of two that
1942 // is greater than or equal to the number of geometries (and at least
1943 // kMinGeometryAllocated).
1944 int newAllocSize = GrNextPow2(newGeoCount);
1945 int currAllocSize = SkTMax<int>(kMinGeometryAllocated, GrNextPow2(fGeoCount));
1946
bsalomon16ed6ad2015-07-29 06:54:33 -07001947 if (newGeoCount > currAllocSize) {
bsalomond602f4d2015-07-27 06:12:01 -07001948 fGeoData.realloc(newAllocSize);
joshualittad802c62015-04-15 05:31:57 -07001949 }
1950
bsalomond602f4d2015-07-27 06:12:01 -07001951 memcpy(&fGeoData[fGeoCount], that->fGeoData.get(), that->fGeoCount * sizeof(Geometry));
bsalomon1c634362015-07-27 07:00:00 -07001952 // We steal the ref on the blobs from the other TextBatch and set its count to 0 so that
1953 // it doesn't try to unref them.
1954#ifdef SK_DEBUG
1955 for (int i = 0; i < that->fGeoCount; ++i) {
1956 that->fGeoData.get()[i].fBlob = (Blob*)0x1;
joshualittad802c62015-04-15 05:31:57 -07001957 }
bsalomon1c634362015-07-27 07:00:00 -07001958#endif
1959 that->fGeoCount = 0;
bsalomond602f4d2015-07-27 06:12:01 -07001960 fGeoCount = newGeoCount;
joshualitt99c7c072015-05-01 13:43:30 -07001961
1962 this->joinBounds(that->bounds());
joshualitt1d89e8d2015-04-01 12:40:54 -07001963 return true;
1964 }
1965
joshualitt9bd2daf2015-04-17 09:30:06 -07001966 // TODO just use class params
1967 // TODO trying to figure out why lcd is so whack
1968 GrGeometryProcessor* setupDfProcessor(const SkMatrix& viewMatrix, SkColor filteredColor,
1969 GrColor color, GrTexture* texture) {
1970 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
bsalomon265697d2015-07-22 10:17:26 -07001971 bool isLCD = this->isLCD();
joshualitt9bd2daf2015-04-17 09:30:06 -07001972 // set up any flags
bsalomon265697d2015-07-22 10:17:26 -07001973 uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
joshualitt9bd2daf2015-04-17 09:30:06 -07001974
1975 // see if we need to create a new effect
bsalomon265697d2015-07-22 10:17:26 -07001976 if (isLCD) {
1977 flags |= kUseLCD_DistanceFieldEffectFlag;
1978 flags |= viewMatrix.rectStaysRect() ? kRectToRect_DistanceFieldEffectFlag : 0;
1979 flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0;
1980
joshualitt9bd2daf2015-04-17 09:30:06 -07001981 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
1982
1983 float redCorrection =
1984 (*fDistanceAdjustTable)[GrColorUnpackR(colorNoPreMul) >> kDistanceAdjustLumShift];
1985 float greenCorrection =
1986 (*fDistanceAdjustTable)[GrColorUnpackG(colorNoPreMul) >> kDistanceAdjustLumShift];
1987 float blueCorrection =
1988 (*fDistanceAdjustTable)[GrColorUnpackB(colorNoPreMul) >> kDistanceAdjustLumShift];
1989 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
1990 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(redCorrection,
1991 greenCorrection,
1992 blueCorrection);
1993
1994 return GrDistanceFieldLCDTextGeoProc::Create(color,
1995 viewMatrix,
1996 texture,
1997 params,
1998 widthAdjust,
joshualittb8c241a2015-05-19 08:23:30 -07001999 flags,
2000 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07002001 } else {
2002 flags |= kColorAttr_DistanceFieldEffectFlag;
joshualitt9bd2daf2015-04-17 09:30:06 -07002003#ifdef SK_GAMMA_APPLY_TO_A8
robertphillips9fc82752015-06-19 04:46:45 -07002004 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, filteredColor);
joshualitt9bd2daf2015-04-17 09:30:06 -07002005 float correction = (*fDistanceAdjustTable)[lum >> kDistanceAdjustLumShift];
2006 return GrDistanceFieldA8TextGeoProc::Create(color,
2007 viewMatrix,
2008 texture,
2009 params,
2010 correction,
joshualittb8c241a2015-05-19 08:23:30 -07002011 flags,
2012 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07002013#else
2014 return GrDistanceFieldA8TextGeoProc::Create(color,
2015 viewMatrix,
2016 texture,
2017 params,
joshualittb8c241a2015-05-19 08:23:30 -07002018 flags,
2019 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07002020#endif
2021 }
2022
2023 }
2024
joshualitt1d89e8d2015-04-01 12:40:54 -07002025 struct BatchTracker {
2026 GrColor fColor;
2027 SkMatrix fViewMatrix;
2028 bool fUsesLocalCoords;
2029 bool fColorIgnored;
2030 bool fCoverageIgnored;
2031 int fNumGlyphs;
2032 };
2033
2034 BatchTracker fBatch;
bsalomond602f4d2015-07-27 06:12:01 -07002035 // The minimum number of Geometry we will try to allocate.
2036 enum { kMinGeometryAllocated = 4 };
2037 SkAutoSTMalloc<kMinGeometryAllocated, Geometry> fGeoData;
2038 int fGeoCount;
bsalomon265697d2015-07-22 10:17:26 -07002039
2040 enum MaskType {
2041 kGrayscaleCoverageMask_MaskType,
2042 kLCDCoverageMask_MaskType,
2043 kColorBitmapMask_MaskType,
2044 kGrayscaleDistanceField_MaskType,
2045 kLCDDistanceField_MaskType,
2046 } fMaskType;
2047 bool fUseBGR; // fold this into the enum?
2048
joshualitt1d89e8d2015-04-01 12:40:54 -07002049 GrBatchFontCache* fFontCache;
joshualitt9bd2daf2015-04-17 09:30:06 -07002050
2051 // Distance field properties
robertphillips9fc82752015-06-19 04:46:45 -07002052 SkAutoTUnref<const DistanceAdjustTable> fDistanceAdjustTable;
joshualitt9bd2daf2015-04-17 09:30:06 -07002053 SkColor fFilteredColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07002054};
2055
robertphillips2334fb62015-06-17 05:43:33 -07002056void GrAtlasTextContext::flushRunAsPaths(GrRenderTarget* rt, const SkTextBlob::RunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -07002057 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07002058 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
2059 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
2060 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07002061
joshualitt9a27e632015-04-06 10:53:36 -07002062 size_t textLen = it.glyphCount() * sizeof(uint16_t);
2063 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07002064
joshualitt9a27e632015-04-06 10:53:36 -07002065 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07002066
joshualitt9a27e632015-04-06 10:53:36 -07002067 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
2068 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07002069 }
2070
robertphillipsfcf78292015-06-19 11:49:52 -07002071 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07002072
2073 switch (it.positioning()) {
2074 case SkTextBlob::kDefault_Positioning:
robertphillips2334fb62015-06-17 05:43:33 -07002075 this->drawTextAsPath(rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002076 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002077 textLen, x + offset.x(), y + offset.y(), clipBounds);
2078 break;
2079 case SkTextBlob::kHorizontal_Positioning:
robertphillips2334fb62015-06-17 05:43:33 -07002080 this->drawPosTextAsPath(rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002081 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002082 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
2083 clipBounds);
2084 break;
2085 case SkTextBlob::kFull_Positioning:
robertphillips2334fb62015-06-17 05:43:33 -07002086 this->drawPosTextAsPath(rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002087 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002088 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
2089 break;
2090 }
2091}
2092
bsalomonabd30f52015-08-13 13:34:48 -07002093inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -07002094GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -07002095 int glyphCount, int run, int subRun,
2096 GrColor color, SkScalar transX, SkScalar transY,
2097 const SkPaint& skPaint) {
2098 GrMaskFormat format = info.fMaskFormat;
2099 GrColor subRunColor;
2100 if (kARGB_GrMaskFormat == format) {
2101 uint8_t paintAlpha = skPaint.getAlpha();
2102 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
2103 } else {
2104 subRunColor = color;
2105 }
2106
bsalomon265697d2015-07-22 10:17:26 -07002107 TextBatch* batch;
joshualitt79dfb2b2015-05-11 08:58:08 -07002108 if (info.fDrawAsDistanceFields) {
2109 SkColor filteredColor;
2110 SkColorFilter* colorFilter = skPaint.getColorFilter();
2111 if (colorFilter) {
2112 filteredColor = colorFilter->filterColor(skPaint.getColor());
2113 } else {
2114 filteredColor = skPaint.getColor();
2115 }
robertphillipsfcf78292015-06-19 11:49:52 -07002116 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
bsalomon265697d2015-07-22 10:17:26 -07002117 batch = TextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
2118 fDistanceAdjustTable, filteredColor,
2119 info.fUseLCDText, useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07002120 } else {
bsalomon265697d2015-07-22 10:17:26 -07002121 batch = TextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07002122 }
bsalomon265697d2015-07-22 10:17:26 -07002123 TextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07002124 geometry.fBlob = SkRef(cacheBlob);
2125 geometry.fRun = run;
2126 geometry.fSubRun = subRun;
2127 geometry.fColor = subRunColor;
2128 geometry.fTransX = transX;
2129 geometry.fTransY = transY;
2130 batch->init();
2131
2132 return batch;
2133}
2134
robertphillips2334fb62015-06-17 05:43:33 -07002135inline void GrAtlasTextContext::flushRun(GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07002136 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07002137 SkScalar transX, SkScalar transY,
2138 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07002139 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
2140 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
2141 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
2142 if (0 == glyphCount) {
2143 continue;
2144 }
2145
bsalomonabd30f52015-08-13 13:34:48 -07002146 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
2147 subRun, color, transX, transY,
2148 skPaint));
robertphillips2334fb62015-06-17 05:43:33 -07002149 fDrawContext->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07002150 }
2151}
2152
joshualitt374b2f72015-07-21 08:05:03 -07002153inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -07002154 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07002155 SkScalar transX, SkScalar transY,
2156 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07002157 if (!cacheBlob->fBigGlyphs.count()) {
2158 return;
2159 }
2160
2161 SkMatrix pathMatrix;
2162 if (!cacheBlob->fViewMatrix.invert(&pathMatrix)) {
2163 SkDebugf("could not invert viewmatrix\n");
2164 return;
2165 }
2166
joshualitt9a27e632015-04-06 10:53:36 -07002167 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07002168 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07002169 bigGlyph.fVx += transX;
2170 bigGlyph.fVy += transY;
joshualittfc072562015-05-13 12:15:06 -07002171 SkMatrix translate = cacheBlob->fViewMatrix;
2172 translate.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
2173
robertphillips2334fb62015-06-17 05:43:33 -07002174 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, rt, clip, bigGlyph.fPath,
robertphillipsccb1b572015-05-27 11:02:55 -07002175 skPaint, translate, &pathMatrix, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07002176 }
2177}
joshualitt9a27e632015-04-06 10:53:36 -07002178
robertphillips2334fb62015-06-17 05:43:33 -07002179void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -07002180 GrAtlasTextBlob* cacheBlob,
joshualitt9a27e632015-04-06 10:53:36 -07002181 GrRenderTarget* rt,
2182 const SkPaint& skPaint,
2183 const GrPaint& grPaint,
2184 SkDrawFilter* drawFilter,
2185 const GrClip& clip,
2186 const SkMatrix& viewMatrix,
2187 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07002188 SkScalar x, SkScalar y,
2189 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07002190 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
2191 // it as paths
joshualitt7b670db2015-07-09 13:25:02 -07002192 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002193
2194 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002195
2196 SkTextBlob::RunIterator it(blob);
2197 for (int run = 0; !it.done(); it.next(), run++) {
2198 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillips2334fb62015-06-17 05:43:33 -07002199 this->flushRunAsPaths(rt, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07002200 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07002201 continue;
2202 }
joshualitt2a0e9f32015-04-13 06:12:21 -07002203 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillips2334fb62015-06-17 05:43:33 -07002204 this->flushRun(&pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07002205 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002206 }
2207
2208 // Now flush big glyphs
robertphillips2334fb62015-06-17 05:43:33 -07002209 this->flushBigGlyphs(cacheBlob, rt, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002210}
2211
joshualitt374b2f72015-07-21 08:05:03 -07002212void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
joshualitt9a27e632015-04-06 10:53:36 -07002213 GrRenderTarget* rt,
2214 const SkPaint& skPaint,
2215 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07002216 const GrClip& clip,
2217 const SkIRect& clipBounds) {
joshualitt7b670db2015-07-09 13:25:02 -07002218 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002219
2220 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002221 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillips2334fb62015-06-17 05:43:33 -07002222 this->flushRun(&pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002223 }
2224
2225 // Now flush big glyphs
robertphillips2334fb62015-06-17 05:43:33 -07002226 this->flushBigGlyphs(cacheBlob, rt, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002227}
joshualitt79dfb2b2015-05-11 08:58:08 -07002228
2229///////////////////////////////////////////////////////////////////////////////////////////////////
2230
2231#ifdef GR_TEST_UTILS
2232
bsalomonabd30f52015-08-13 13:34:48 -07002233DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07002234 static uint32_t gContextID = SK_InvalidGenID;
2235 static GrAtlasTextContext* gTextContext = NULL;
robertphillipsfcf78292015-06-19 11:49:52 -07002236 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07002237
2238 if (context->uniqueID() != gContextID) {
2239 gContextID = context->uniqueID();
2240 SkDELETE(gTextContext);
robertphillips2334fb62015-06-17 05:43:33 -07002241
joshualitt79dfb2b2015-05-11 08:58:08 -07002242 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
2243 // because we don't really want to have a gpu device here.
2244 // We enable distance fields by twiddling a knob on the paint
robertphillipsfcf78292015-06-19 11:49:52 -07002245 GrDrawContext* drawContext = context->drawContext(&gSurfaceProps);
robertphillips2334fb62015-06-17 05:43:33 -07002246
robertphillipsfcf78292015-06-19 11:49:52 -07002247 gTextContext = GrAtlasTextContext::Create(context, drawContext, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07002248 }
2249
2250 // create dummy render target
2251 GrSurfaceDesc desc;
2252 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2253 desc.fWidth = 1024;
2254 desc.fHeight = 1024;
2255 desc.fConfig = kRGBA_8888_GrPixelConfig;
joshualitt15732062015-05-13 12:15:14 -07002256 desc.fSampleCnt = 0;
joshualitt79dfb2b2015-05-11 08:58:08 -07002257 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, true, NULL, 0));
2258 SkASSERT(texture);
2259 SkASSERT(NULL != texture->asRenderTarget());
2260 GrRenderTarget* rt = texture->asRenderTarget();
2261
2262 // Setup dummy SkPaint / GrPaint
2263 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07002264 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07002265 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07002266 skPaint.setColor(color);
2267 skPaint.setLCDRenderText(random->nextBool());
2268 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
2269 skPaint.setSubpixelText(random->nextBool());
2270
2271 GrPaint grPaint;
2272 if (!SkPaint2GrPaint(context, rt, skPaint, viewMatrix, true, &grPaint)) {
2273 SkFAIL("couldn't convert paint\n");
2274 }
2275
2276 const char* text = "The quick brown fox jumps over the lazy dog.";
2277 int textLen = (int)strlen(text);
2278
2279 // Setup clip
2280 GrClip clip;
2281 SkIRect noClip = SkIRect::MakeLargest();
2282
2283 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
2284 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07002285 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt79dfb2b2015-05-11 08:58:08 -07002286 gTextContext->createDrawTextBlob(rt, clip, grPaint, skPaint, viewMatrix, text,
2287 static_cast<size_t>(textLen), 0, 0, noClip));
2288
2289 SkScalar transX = static_cast<SkScalar>(random->nextU());
2290 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07002291 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07002292 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
2293}
2294
2295#endif