blob: 8105be74e699ea447ba2606b3f20bec757435dfd [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
robertphillipsf6703fa2015-09-01 05:36:47 -070099GrAtlasTextContext::GrAtlasTextContext(GrContext* context, const SkSurfaceProps& surfaceProps)
100 : INHERITED(context, surfaceProps)
101 , fDistanceAdjustTable(new DistanceAdjustTable) {
joshualittb7133be2015-04-08 09:08:31 -0700102 // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest
103 // vertexStride
bungeman99fe8222015-08-20 07:57:51 -0700104 static_assert(kGrayTextVASize >= kColorTextVASize && kGrayTextVASize >= kLCDTextVASize,
105 "vertex_attribute_changed");
halcanary96fcdcc2015-08-27 07:41:13 -0700106 fCurrStrike = nullptr;
joshualittb7133be2015-04-08 09:08:31 -0700107 fCache = context->getTextBlobCache();
joshualitt9bd2daf2015-04-17 09:30:06 -0700108}
109
robertphillips9fc82752015-06-19 04:46:45 -0700110void GrAtlasTextContext::DistanceAdjustTable::buildDistanceAdjustTable() {
joshualitt9bd2daf2015-04-17 09:30:06 -0700111
112 // This is used for an approximation of the mask gamma hack, used by raster and bitmap
113 // text. The mask gamma hack is based off of guessing what the blend color is going to
114 // be, and adjusting the mask so that when run through the linear blend will
115 // produce the value closest to the desired result. However, in practice this means
116 // that the 'adjusted' mask is just increasing or decreasing the coverage of
117 // the mask depending on what it is thought it will blit against. For black (on
118 // assumed white) this means that coverages are decreased (on a curve). For white (on
119 // assumed black) this means that coverages are increased (on a a curve). At
120 // middle (perceptual) gray (which could be blit against anything) the coverages
121 // remain the same.
122 //
123 // The idea here is that instead of determining the initial (real) coverage and
124 // then adjusting that coverage, we determine an adjusted coverage directly by
125 // essentially manipulating the geometry (in this case, the distance to the glyph
126 // edge). So for black (on assumed white) this thins a bit; for white (on
127 // assumed black) this fake bolds the geometry a bit.
128 //
129 // The distance adjustment is calculated by determining the actual coverage value which
130 // when fed into in the mask gamma table gives us an 'adjusted coverage' value of 0.5. This
131 // actual coverage value (assuming it's between 0 and 1) corresponds to a distance from the
132 // actual edge. So by subtracting this distance adjustment and computing without the
133 // the coverage adjustment we should get 0.5 coverage at the same point.
134 //
135 // This has several implications:
136 // For non-gray lcd smoothed text, each subpixel essentially is using a
137 // slightly different geometry.
138 //
139 // For black (on assumed white) this may not cover some pixels which were
140 // previously covered; however those pixels would have been only slightly
141 // covered and that slight coverage would have been decreased anyway. Also, some pixels
142 // which were previously fully covered may no longer be fully covered.
143 //
144 // For white (on assumed black) this may cover some pixels which weren't
145 // previously covered at all.
146
147 int width, height;
148 size_t size;
149
150#ifdef SK_GAMMA_CONTRAST
151 SkScalar contrast = SK_GAMMA_CONTRAST;
152#else
153 SkScalar contrast = 0.5f;
154#endif
robertphillips9fc82752015-06-19 04:46:45 -0700155 SkScalar paintGamma = SK_GAMMA_EXPONENT;
156 SkScalar deviceGamma = SK_GAMMA_EXPONENT;
joshualitt9bd2daf2015-04-17 09:30:06 -0700157
158 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
159 &width, &height);
160
161 SkASSERT(kExpectedDistanceAdjustTableSize == height);
halcanary385fe4d2015-08-26 13:07:48 -0700162 fTable = new SkScalar[height];
joshualitt9bd2daf2015-04-17 09:30:06 -0700163
164 SkAutoTArray<uint8_t> data((int)size);
165 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
166
167 // find the inverse points where we cross 0.5
168 // binsearch might be better, but we only need to do this once on creation
169 for (int row = 0; row < height; ++row) {
170 uint8_t* rowPtr = data.get() + row*width;
171 for (int col = 0; col < width - 1; ++col) {
172 if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) {
173 // compute point where a mask value will give us a result of 0.5
174 float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPtr[col]);
175 float borderAlpha = (col + interp) / 255.f;
176
177 // compute t value for that alpha
178 // this is an approximate inverse for smoothstep()
179 float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5.0f) / 3.0f;
180
181 // compute distance which gives us that t value
182 const float kDistanceFieldAAFactor = 0.65f; // should match SK_DistanceFieldAAFactor
183 float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor;
184
185 fTable[row] = d;
186 break;
187 }
188 }
189 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700190}
191
joshualittdbd35932015-04-02 09:19:04 -0700192GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context,
robertphillipsfcf78292015-06-19 11:49:52 -0700193 const SkSurfaceProps& surfaceProps) {
robertphillipsf6703fa2015-09-01 05:36:47 -0700194 return new GrAtlasTextContext(context, surfaceProps);
joshualitt1d89e8d2015-04-01 12:40:54 -0700195}
196
joshualittdbd35932015-04-02 09:19:04 -0700197bool GrAtlasTextContext::canDraw(const GrRenderTarget*,
198 const GrClip&,
199 const GrPaint&,
200 const SkPaint& skPaint,
201 const SkMatrix& viewMatrix) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700202 return this->canDrawAsDistanceFields(skPaint, viewMatrix) ||
203 !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -0700204}
205
joshualitt9e36c1a2015-04-14 12:17:27 -0700206GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
207 GrColor canonicalColor = paint.computeLuminanceColor();
208 if (lcd) {
209 // This is the correct computation, but there are tons of cases where LCD can be overridden.
210 // For now we just regenerate if any run in a textblob has LCD.
211 // TODO figure out where all of these overrides are and see if we can incorporate that logic
212 // at a higher level *OR* use sRGB
213 SkASSERT(false);
214 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
215 } else {
216 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
217 // gamma corrected masks anyways, nor color
218 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
219 SkColorGetG(canonicalColor),
220 SkColorGetB(canonicalColor));
221 // reduce to our finite number of bits
222 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
223 }
224 return canonicalColor;
225}
226
227// TODO if this function ever shows up in profiling, then we can compute this value when the
228// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
229// run so this is not a big deal to compute here.
230bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) {
231 SkTextBlob::RunIterator it(blob);
232 for (; !it.done(); it.next()) {
233 if (it.isLCD()) {
234 return true;
235 }
236 }
237 return false;
238}
239
joshualitt2a0e9f32015-04-13 06:12:21 -0700240bool GrAtlasTextContext::MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY,
joshualitt374b2f72015-07-21 08:05:03 -0700241 const GrAtlasTextBlob& blob, const SkPaint& paint,
jvanverth0628a522015-08-18 07:44:22 -0700242 GrColor color, const SkMaskFilter::BlurRec& blurRec,
joshualittdbd35932015-04-02 09:19:04 -0700243 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700244 // If we have LCD text then our canonical color will be set to transparent, in this case we have
245 // to regenerate the blob on any color change
jvanverth0628a522015-08-18 07:44:22 -0700246 // We use the grPaint to get any color filter effects
247 if (blob.fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
248 blob.fPaintColor != color) {
joshualitt2a0e9f32015-04-13 06:12:21 -0700249 return true;
250 }
251
252 if (blob.fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
253 return true;
254 }
255
256 if (blob.fViewMatrix.hasPerspective() && !blob.fViewMatrix.cheapEqualTo(viewMatrix)) {
257 return true;
258 }
259
joshualitt53b5f442015-04-13 06:33:59 -0700260 // We only cache one masked version
261 if (blob.fKey.fHasBlur &&
262 (blob.fBlurRec.fSigma != blurRec.fSigma ||
263 blob.fBlurRec.fStyle != blurRec.fStyle ||
264 blob.fBlurRec.fQuality != blurRec.fQuality)) {
265 return true;
266 }
267
268 // Similarly, we only cache one version for each style
269 if (blob.fKey.fStyle != SkPaint::kFill_Style &&
270 (blob.fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
271 blob.fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
272 blob.fStrokeInfo.fJoin != paint.getStrokeJoin())) {
273 return true;
274 }
275
joshualittfcfb9fc2015-04-21 07:35:10 -0700276 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
277 // for mixed blobs if this becomes an issue.
278 if (blob.hasBitmap() && blob.hasDistanceField()) {
joshualitt473ffa12015-04-22 18:23:15 -0700279 // Identical viewmatrices and we can reuse in all cases
280 if (blob.fViewMatrix.cheapEqualTo(viewMatrix) && x == blob.fX && y == blob.fY) {
281 return false;
282 }
joshualitt2a0e9f32015-04-13 06:12:21 -0700283 return true;
284 }
285
joshualittfcfb9fc2015-04-21 07:35:10 -0700286 if (blob.hasBitmap()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700287 if (blob.fViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
288 blob.fViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
289 blob.fViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
290 blob.fViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
291 return true;
292 }
293
joshualittfcfb9fc2015-04-21 07:35:10 -0700294 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
295 // but only for integer translations.
296 // This cool bit of math will determine the necessary translation to apply to the already
297 // generated vertex coordinates to move them to the correct position
298 SkScalar transX = viewMatrix.getTranslateX() +
299 viewMatrix.getScaleX() * (x - blob.fX) +
300 viewMatrix.getSkewX() * (y - blob.fY) -
301 blob.fViewMatrix.getTranslateX();
302 SkScalar transY = viewMatrix.getTranslateY() +
303 viewMatrix.getSkewY() * (x - blob.fX) +
304 viewMatrix.getScaleY() * (y - blob.fY) -
305 blob.fViewMatrix.getTranslateY();
joshualittf0c000d2015-04-27 09:36:55 -0700306 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700307 return true;
308 }
309
joshualittfcfb9fc2015-04-21 07:35:10 -0700310 (*outTransX) = transX;
311 (*outTransY) = transY;
joshualitta7c63892015-04-21 13:24:37 -0700312 } else if (blob.hasDistanceField()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700313 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
314 // distance field being generated, so we have to regenerate in those cases
315 SkScalar newMaxScale = viewMatrix.getMaxScale();
316 SkScalar oldMaxScale = blob.fViewMatrix.getMaxScale();
317 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
318 if (scaleAdjust < blob.fMaxMinScale || scaleAdjust > blob.fMinMaxScale) {
319 return true;
320 }
321
322 (*outTransX) = x - blob.fX;
323 (*outTransY) = y - blob.fY;
joshualittfcfb9fc2015-04-21 07:35:10 -0700324 }
joshualitt374b2f72015-07-21 08:05:03 -0700325
joshualitta7c63892015-04-21 13:24:37 -0700326 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
327 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
328 // the blob anyways at flush time, so no need to regenerate explicitly
joshualitt2a0e9f32015-04-13 06:12:21 -0700329 return false;
joshualitt1d89e8d2015-04-01 12:40:54 -0700330}
331
332
joshualitt374b2f72015-07-21 08:05:03 -0700333inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run,
joshualittdbd35932015-04-02 09:19:04 -0700334 const SkPaint& skPaint,
joshualitt9bd2daf2015-04-17 09:30:06 -0700335 const SkMatrix* viewMatrix,
336 bool noGamma) {
robertphillipsfcf78292015-06-19 11:49:52 -0700337 skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma);
joshualitt1d89e8d2015-04-01 12:40:54 -0700338 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
339 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc());
340}
341
robertphillipsf6703fa2015-09-01 05:36:47 -0700342void GrAtlasTextContext::drawTextBlob(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700343 const GrClip& clip, const SkPaint& skPaint,
344 const SkMatrix& viewMatrix, const SkTextBlob* blob,
345 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700346 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700347 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700348 if (fContext->abandoned()) {
349 return;
350 }
351
joshualitt374b2f72015-07-21 08:05:03 -0700352 SkAutoTUnref<GrAtlasTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700353 SkMaskFilter::BlurRec blurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700354 GrAtlasTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700355 // It might be worth caching these things, but its not clear at this time
356 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
357 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700358 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700359 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700360 drawFilter);
361
362 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700363 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700364
365 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
robertphillipsfcf78292015-06-19 11:49:52 -0700366 SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700367 kUnknown_SkPixelGeometry;
368
joshualitt9e36c1a2015-04-14 12:17:27 -0700369 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
370 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
371 // ensure we always match the same key
372 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
373 ComputeCanonicalColor(skPaint, hasLCD);
374
joshualitte4cee1f2015-05-11 13:04:28 -0700375 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700376 key.fUniqueID = blob->uniqueID();
377 key.fStyle = skPaint.getStyle();
378 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700379 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700380 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700381 }
382
joshualitt1d89e8d2015-04-01 12:40:54 -0700383 SkIRect clipRect;
384 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
385
joshualitt2a0e9f32015-04-13 06:12:21 -0700386 SkScalar transX = 0.f;
387 SkScalar transY = 0.f;
388
joshualitt9e36c1a2015-04-14 12:17:27 -0700389 // Though for the time being runs in the textblob can override the paint, they only touch font
390 // info.
391 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700392 if (!SkPaint2GrPaint(fContext, rt, skPaint, viewMatrix, true, &grPaint)) {
393 return;
394 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700395
joshualittb7133be2015-04-08 09:08:31 -0700396 if (cacheBlob) {
jvanverth0628a522015-08-18 07:44:22 -0700397 if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, grPaint.getColor(), blurRec,
398 viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700399 // We have to remake the blob because changes may invalidate our masks.
400 // TODO we could probably get away reuse most of the time if the pointer is unique,
401 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700402 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700403 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
404 kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700405 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700406 blob, x, y, drawFilter, clipRect, rt, clip);
joshualittb7133be2015-04-08 09:08:31 -0700407 } else {
joshualitt9e36c1a2015-04-14 12:17:27 -0700408 // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y
joshualitt7e7b5c52015-07-21 12:56:56 -0700409 // offsets. Note, we offset the vertex bounds right before flushing
joshualitt2a0e9f32015-04-13 06:12:21 -0700410 cacheBlob->fViewMatrix = viewMatrix;
411 cacheBlob->fX = x;
412 cacheBlob->fY = y;
joshualittb7133be2015-04-08 09:08:31 -0700413 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700414#ifdef CACHE_SANITY_CHECK
415 {
416 int glyphCount = 0;
417 int runCount = 0;
418 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
419 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
420 kGrayTextVASize));
421 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
422 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700423 blob, x, y, drawFilter, clipRect, rt, clip);
joshualitt259fbf12015-07-21 11:39:34 -0700424 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
425 }
426
427#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700428 }
429 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700430 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700431 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
432 kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700433 } else {
434 cacheBlob.reset(fCache->createBlob(blob, kGrayTextVASize));
435 }
robertphillips9c240a12015-05-28 07:45:59 -0700436 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
jvanverth0628a522015-08-18 07:44:22 -0700437 blob, x, y, drawFilter, clipRect, rt, clip);
joshualitt1d89e8d2015-04-01 12:40:54 -0700438 }
439
robertphillipsf6703fa2015-09-01 05:36:47 -0700440 this->flush(blob, cacheBlob, dc, rt, skPaint, grPaint, drawFilter,
joshualitt2a0e9f32015-04-13 06:12:21 -0700441 clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700442}
443
joshualitt9bd2daf2015-04-17 09:30:06 -0700444inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
445 const SkMatrix& viewMatrix) {
446 // TODO: support perspective (need getMaxScale replacement)
447 if (viewMatrix.hasPerspective()) {
448 return false;
449 }
450
451 SkScalar maxScale = viewMatrix.getMaxScale();
452 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
453 // Hinted text looks far better at small resolutions
454 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700455 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700456 return false;
457 }
458
bsalomonafcd7cd2015-08-31 12:39:41 -0700459 bool useDFT = fSurfaceProps.isUseDeviceIndependentFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700460#if SK_FORCE_DISTANCE_FIELD_TEXT
461 useDFT = true;
462#endif
463
jvanverth4854d132015-06-22 06:46:56 -0700464 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700465 return false;
466 }
467
468 // rasterizers and mask filters modify alpha, which doesn't
469 // translate well to distance
470 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700471 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700472 return false;
473 }
474
475 // TODO: add some stroking support
476 if (skPaint.getStyle() != SkPaint::kFill_Style) {
477 return false;
478 }
479
480 return true;
481}
482
joshualitt374b2f72015-07-21 08:05:03 -0700483void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700484 const SkPaint& skPaint, GrColor color,
485 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700486 const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualittfcfb9fc2015-04-21 07:35:10 -0700487 SkDrawFilter* drawFilter, const SkIRect& clipRect,
jvanverth0628a522015-08-18 07:44:22 -0700488 GrRenderTarget* rt, const GrClip& clip) {
489 // The color here is the GrPaint color, and it is used to determine whether we
490 // have to regenerate LCD text blobs.
491 // We use this color vs the SkPaint color because it has the colorfilter applied.
492 cacheBlob->fPaintColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -0700493 cacheBlob->fViewMatrix = viewMatrix;
494 cacheBlob->fX = x;
495 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700496
497 // Regenerate textblob
498 SkPaint runPaint = skPaint;
499 SkTextBlob::RunIterator it(blob);
500 for (int run = 0; !it.done(); it.next(), run++) {
501 int glyphCount = it.glyphCount();
502 size_t textLen = glyphCount * sizeof(uint16_t);
503 const SkPoint& offset = it.offset();
504 // applyFontToPaint() always overwrites the exact same attributes,
505 // so it is safe to not re-seed the paint for this reason.
506 it.applyFontToPaint(&runPaint);
507
508 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
509 // A false return from filter() means we should abort the current draw.
510 runPaint = skPaint;
511 continue;
512 }
513
robertphillipsfcf78292015-06-19 11:49:52 -0700514 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700515
joshualitt1d89e8d2015-04-01 12:40:54 -0700516 // setup vertex / glyphIndex for the new run
517 if (run > 0) {
518 PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back();
519 PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back();
520
521 newRun.fVertexStartIndex = lastRun.fVertexEndIndex;
522 newRun.fVertexEndIndex = lastRun.fVertexEndIndex;
523
524 newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex;
525 newRun.fGlyphEndIndex = lastRun.fGlyphEndIndex;
526 }
527
joshualittfcfb9fc2015-04-21 07:35:10 -0700528 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
529 cacheBlob->setHasDistanceField();
530 SkPaint dfPaint = runPaint;
531 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700532 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700533 Run& runIdx = cacheBlob->fRuns[run];
534 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
535 subRun.fUseLCDText = runPaint.isLCDRenderText();
536 subRun.fDrawAsDistanceFields = true;
joshualitt9a27e632015-04-06 10:53:36 -0700537
halcanary96fcdcc2015-08-27 07:41:13 -0700538 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], dfPaint, nullptr, true);
joshualittfcfb9fc2015-04-21 07:35:10 -0700539
540 SkTDArray<char> fallbackTxt;
541 SkTDArray<SkScalar> fallbackPos;
542 SkPoint dfOffset;
543 int scalarsPerPosition = 2;
544 switch (it.positioning()) {
545 case SkTextBlob::kDefault_Positioning: {
546 this->internalDrawDFText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
547 (const char *)it.glyphs(), textLen,
548 x + offset.x(), y + offset.y(), clipRect, textRatio,
549 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
550 break;
551 }
552 case SkTextBlob::kHorizontal_Positioning: {
553 scalarsPerPosition = 1;
554 dfOffset = SkPoint::Make(x, y + offset.y());
555 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
556 (const char*)it.glyphs(), textLen, it.pos(),
557 scalarsPerPosition, dfOffset, clipRect, textRatio,
558 &fallbackTxt, &fallbackPos);
559 break;
560 }
561 case SkTextBlob::kFull_Positioning: {
562 dfOffset = SkPoint::Make(x, y);
563 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
564 (const char*)it.glyphs(), textLen, it.pos(),
565 scalarsPerPosition, dfOffset, clipRect, textRatio,
566 &fallbackTxt, &fallbackPos);
567 break;
568 }
569 }
570 if (fallbackTxt.count()) {
jvanverth0628a522015-08-18 07:44:22 -0700571 this->fallbackDrawPosText(cacheBlob, run, rt, clip, color, runPaint, viewMatrix,
joshualittfcfb9fc2015-04-21 07:35:10 -0700572 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset,
573 clipRect);
574 }
575
576 SkGlyphCache::AttachCache(cache);
577 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
578 cacheBlob->fRuns[run].fDrawAsPaths = true;
579 } else {
580 cacheBlob->setHasBitmap();
581 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
582 false);
583 switch (it.positioning()) {
584 case SkTextBlob::kDefault_Positioning:
585 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
586 (const char *)it.glyphs(), textLen,
587 x + offset.x(), y + offset.y(), clipRect);
588 break;
589 case SkTextBlob::kHorizontal_Positioning:
590 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
591 (const char*)it.glyphs(), textLen, it.pos(), 1,
592 SkPoint::Make(x, y + offset.y()), clipRect);
593 break;
594 case SkTextBlob::kFull_Positioning:
595 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
596 (const char*)it.glyphs(), textLen, it.pos(), 2,
597 SkPoint::Make(x, y), clipRect);
598 break;
599 }
600 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700601 }
602
603 if (drawFilter) {
604 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
605 runPaint = skPaint;
606 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700607 }
608}
609
joshualitt374b2f72015-07-21 08:05:03 -0700610inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700611 SkPaint* skPaint,
612 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700613 const SkMatrix& viewMatrix) {
614 // getMaxScale doesn't support perspective, so neither do we at the moment
615 SkASSERT(!viewMatrix.hasPerspective());
616 SkScalar maxScale = viewMatrix.getMaxScale();
617 SkScalar textSize = skPaint->getTextSize();
618 SkScalar scaledTextSize = textSize;
619 // if we have non-unity scale, we need to choose our base text size
620 // based on the SkPaint's text size multiplied by the max scale factor
621 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
622 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
623 scaledTextSize *= maxScale;
624 }
625
joshualitt64c99cc2015-04-21 09:43:03 -0700626 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
627 // and ceiling. A scale outside of this range would require regenerating the distance fields
628 SkScalar dfMaskScaleFloor;
629 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700630 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700631 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700632 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700633 *textRatio = textSize / kSmallDFFontSize;
634 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
635 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700636 dfMaskScaleFloor = kSmallDFFontLimit;
637 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700638 *textRatio = textSize / kMediumDFFontSize;
639 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
640 } else {
joshualitta7c63892015-04-21 13:24:37 -0700641 dfMaskScaleFloor = kMediumDFFontLimit;
642 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700643 *textRatio = textSize / kLargeDFFontSize;
644 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
645 }
646
joshualitt64c99cc2015-04-21 09:43:03 -0700647 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
648 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
649 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
650 // tolerate before we'd have to move to a large mip size. When we actually test these values
651 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
652 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
653 // level)
joshualitta7c63892015-04-21 13:24:37 -0700654 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700655 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
656 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
657
joshualitt9bd2daf2015-04-17 09:30:06 -0700658 skPaint->setLCDRenderText(false);
659 skPaint->setAutohinted(false);
660 skPaint->setHinting(SkPaint::kNormal_Hinting);
661 skPaint->setSubpixelText(true);
662}
663
joshualitt374b2f72015-07-21 08:05:03 -0700664inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700665 int runIndex,
joshualittfec19e12015-04-17 10:32:32 -0700666 GrRenderTarget* rt, const GrClip& clip,
jvanverth0628a522015-08-18 07:44:22 -0700667 GrColor color,
joshualitt9bd2daf2015-04-17 09:30:06 -0700668 const SkPaint& skPaint,
669 const SkMatrix& viewMatrix,
670 const SkTDArray<char>& fallbackTxt,
671 const SkTDArray<SkScalar>& fallbackPos,
672 int scalarsPerPosition,
673 const SkPoint& offset,
674 const SkIRect& clipRect) {
joshualittfec19e12015-04-17 10:32:32 -0700675 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700676 blob->setHasBitmap();
677 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700678 // Push back a new subrun to fill and set the override descriptor
679 run.push_back();
halcanary385fe4d2015-08-26 13:07:48 -0700680 run.fOverrideDescriptor.reset(new SkAutoDescriptor);
joshualitt97202d22015-04-22 13:47:02 -0700681 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700682 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700683 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700684 run.fOverrideDescriptor->getDesc());
jvanverth0628a522015-08-18 07:44:22 -0700685 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, color, viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700686 fallbackTxt.begin(), fallbackTxt.count(),
687 fallbackPos.begin(), scalarsPerPosition, offset, clipRect);
688 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700689}
690
joshualitt374b2f72015-07-21 08:05:03 -0700691inline GrAtlasTextBlob*
joshualitt9bd2daf2015-04-17 09:30:06 -0700692GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
693 const SkMatrix& viewMatrix, SkGlyphCache** cache,
694 SkPaint* dfPaint, SkScalar* textRatio) {
joshualitt374b2f72015-07-21 08:05:03 -0700695 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700696
697 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700698 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700699 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700700 Run& run = blob->fRuns[0];
701 PerSubRunInfo& subRun = run.fSubRunInfo.back();
702 subRun.fUseLCDText = origPaint.isLCDRenderText();
703 subRun.fDrawAsDistanceFields = true;
joshualitt9bd2daf2015-04-17 09:30:06 -0700704
halcanary96fcdcc2015-08-27 07:41:13 -0700705 *cache = this->setupCache(&blob->fRuns[0], *dfPaint, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -0700706 return blob;
707}
708
joshualitt374b2f72015-07-21 08:05:03 -0700709inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700710GrAtlasTextContext::createDrawTextBlob(GrRenderTarget* rt, const GrClip& clip,
711 const GrPaint& paint, const SkPaint& skPaint,
712 const SkMatrix& viewMatrix,
713 const char text[], size_t byteLength,
714 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700715 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700716 SkIRect clipRect;
717 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
718
joshualitt374b2f72015-07-21 08:05:03 -0700719 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700720 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
721 SkPaint dfPaint;
722 SkScalar textRatio;
723 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700724 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700725
joshualitt9bd2daf2015-04-17 09:30:06 -0700726 SkTDArray<char> fallbackTxt;
727 SkTDArray<SkScalar> fallbackPos;
728 SkPoint offset;
729 this->internalDrawDFText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
730 byteLength, x, y, clipRect, textRatio, &fallbackTxt, &fallbackPos,
731 &offset, skPaint);
732 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700733 if (fallbackTxt.count()) {
jvanverth0628a522015-08-18 07:44:22 -0700734 this->fallbackDrawPosText(blob, 0, rt, clip, paint.getColor(), skPaint, viewMatrix,
735 fallbackTxt, fallbackPos, 2, offset, clipRect);
joshualitt9bd2daf2015-04-17 09:30:06 -0700736 }
737 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700738 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700739 blob->fViewMatrix = viewMatrix;
740
741 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
742 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
743 byteLength, x, y, clipRect);
744 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700745 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700746 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700747}
748
joshualitt374b2f72015-07-21 08:05:03 -0700749inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700750GrAtlasTextContext::createDrawPosTextBlob(GrRenderTarget* rt, const GrClip& clip,
751 const GrPaint& paint, const SkPaint& skPaint,
752 const SkMatrix& viewMatrix,
753 const char text[], size_t byteLength,
754 const SkScalar pos[], int scalarsPerPosition,
755 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700756 int glyphCount = skPaint.countText(text, byteLength);
757
758 SkIRect clipRect;
759 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
760
joshualitt374b2f72015-07-21 08:05:03 -0700761 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700762 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
763 SkPaint dfPaint;
764 SkScalar textRatio;
765 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700766 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700767
768 SkTDArray<char> fallbackTxt;
769 SkTDArray<SkScalar> fallbackPos;
770 this->internalDrawDFPosText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
771 byteLength, pos, scalarsPerPosition, offset, clipRect,
772 textRatio, &fallbackTxt, &fallbackPos);
773 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700774 if (fallbackTxt.count()) {
jvanverth0628a522015-08-18 07:44:22 -0700775 this->fallbackDrawPosText(blob, 0, rt, clip, paint.getColor(), skPaint, viewMatrix,
776 fallbackTxt, fallbackPos, scalarsPerPosition, offset,
777 clipRect);
joshualitt9bd2daf2015-04-17 09:30:06 -0700778 }
779 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700780 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700781 blob->fViewMatrix = viewMatrix;
782 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
783 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
784 byteLength, pos, scalarsPerPosition, offset, clipRect);
785 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700786 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700787 return blob;
788}
789
robertphillipsf6703fa2015-09-01 05:36:47 -0700790void GrAtlasTextContext::onDrawText(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700791 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700792 const GrPaint& paint, const SkPaint& skPaint,
793 const SkMatrix& viewMatrix,
794 const char text[], size_t byteLength,
795 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700796 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips2334fb62015-06-17 05:43:33 -0700797 this->createDrawTextBlob(rt, clip, paint, skPaint, viewMatrix,
798 text, byteLength, x, y, regionClipBounds));
robertphillipsf6703fa2015-09-01 05:36:47 -0700799 this->flush(blob, dc, rt, skPaint, paint, clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700800}
801
robertphillipsf6703fa2015-09-01 05:36:47 -0700802void GrAtlasTextContext::onDrawPosText(GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700803 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700804 const GrPaint& paint, const SkPaint& skPaint,
805 const SkMatrix& viewMatrix,
806 const char text[], size_t byteLength,
807 const SkScalar pos[], int scalarsPerPosition,
808 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt374b2f72015-07-21 08:05:03 -0700809 SkAutoTUnref<GrAtlasTextBlob> blob(
robertphillips2334fb62015-06-17 05:43:33 -0700810 this->createDrawPosTextBlob(rt, clip, paint, skPaint, viewMatrix,
811 text, byteLength,
812 pos, scalarsPerPosition,
813 offset, regionClipBounds));
joshualitt79dfb2b2015-05-11 08:58:08 -0700814
robertphillipsf6703fa2015-09-01 05:36:47 -0700815 this->flush(blob, dc, rt, skPaint, paint, clip, regionClipBounds);
joshualitt9bd2daf2015-04-17 09:30:06 -0700816}
817
joshualitt374b2f72015-07-21 08:05:03 -0700818void GrAtlasTextContext::internalDrawBMPText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700819 SkGlyphCache* cache, const SkPaint& skPaint,
820 GrColor color,
821 const SkMatrix& viewMatrix,
822 const char text[], size_t byteLength,
823 SkScalar x, SkScalar y, const SkIRect& clipRect) {
halcanary96fcdcc2015-08-27 07:41:13 -0700824 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700825
826 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700827 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700828 return;
829 }
830
halcanary96fcdcc2015-08-27 07:41:13 -0700831 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700832 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
833
834 // Get GrFontScaler from cache
835 GrFontScaler* fontScaler = GetGrFontScaler(cache);
836
837 // transform our starting point
838 {
839 SkPoint loc;
840 viewMatrix.mapXY(x, y, &loc);
841 x = loc.fX;
842 y = loc.fY;
843 }
844
845 // need to measure first
846 if (skPaint.getTextAlign() != SkPaint::kLeft_Align) {
847 SkVector stopVector;
848 MeasureText(cache, glyphCacheProc, text, byteLength, &stopVector);
849
850 SkScalar stopX = stopVector.fX;
851 SkScalar stopY = stopVector.fY;
852
853 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
854 stopX = SkScalarHalf(stopX);
855 stopY = SkScalarHalf(stopY);
856 }
857 x -= stopX;
858 y -= stopY;
859 }
860
861 const char* stop = text + byteLength;
862
863 SkAutoKern autokern;
864
865 SkFixed fxMask = ~0;
866 SkFixed fyMask = ~0;
867 SkScalar halfSampleX, halfSampleY;
868 if (cache->isSubpixel()) {
869 halfSampleX = halfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
870 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
871 if (kX_SkAxisAlignment == baseline) {
872 fyMask = 0;
873 halfSampleY = SK_ScalarHalf;
874 } else if (kY_SkAxisAlignment == baseline) {
875 fxMask = 0;
876 halfSampleX = SK_ScalarHalf;
877 }
878 } else {
879 halfSampleX = halfSampleY = SK_ScalarHalf;
880 }
881
882 Sk48Dot16 fx = SkScalarTo48Dot16(x + halfSampleX);
883 Sk48Dot16 fy = SkScalarTo48Dot16(y + halfSampleY);
884
885 while (text < stop) {
886 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
887
888 fx += autokern.adjust(glyph);
889
890 if (glyph.fWidth) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700891 this->bmpAppendGlyph(blob,
892 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700893 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700894 Sk48Dot16FloorToInt(fx),
895 Sk48Dot16FloorToInt(fy),
896 color,
897 fontScaler,
898 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700899 }
900
901 fx += glyph.fAdvanceX;
902 fy += glyph.fAdvanceY;
903 }
904}
905
joshualitt374b2f72015-07-21 08:05:03 -0700906void GrAtlasTextContext::internalDrawBMPPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -0700907 SkGlyphCache* cache, const SkPaint& skPaint,
908 GrColor color,
909 const SkMatrix& viewMatrix,
910 const char text[], size_t byteLength,
911 const SkScalar pos[], int scalarsPerPosition,
912 const SkPoint& offset, const SkIRect& clipRect) {
halcanary96fcdcc2015-08-27 07:41:13 -0700913 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt1d89e8d2015-04-01 12:40:54 -0700914 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
915
916 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -0700917 if (text == nullptr || byteLength == 0) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700918 return;
919 }
920
halcanary96fcdcc2015-08-27 07:41:13 -0700921 fCurrStrike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -0700922 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
923
924 // Get GrFontScaler from cache
925 GrFontScaler* fontScaler = GetGrFontScaler(cache);
926
927 const char* stop = text + byteLength;
928 SkTextAlignProc alignProc(skPaint.getTextAlign());
929 SkTextMapStateProc tmsProc(viewMatrix, offset, scalarsPerPosition);
930
931 if (cache->isSubpixel()) {
932 // maybe we should skip the rounding if linearText is set
933 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
934
935 SkFixed fxMask = ~0;
936 SkFixed fyMask = ~0;
937 SkScalar halfSampleX = SkFixedToScalar(SkGlyph::kSubpixelRound);
938 SkScalar halfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
939 if (kX_SkAxisAlignment == baseline) {
940 fyMask = 0;
941 halfSampleY = SK_ScalarHalf;
942 } else if (kY_SkAxisAlignment == baseline) {
943 fxMask = 0;
944 halfSampleX = SK_ScalarHalf;
945 }
946
947 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
948 while (text < stop) {
949 SkPoint tmsLoc;
950 tmsProc(pos, &tmsLoc);
951 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + halfSampleX);
952 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + halfSampleY);
953
954 const SkGlyph& glyph = glyphCacheProc(cache, &text,
955 fx & fxMask, fy & fyMask);
956
957 if (glyph.fWidth) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700958 this->bmpAppendGlyph(blob,
959 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700960 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700961 Sk48Dot16FloorToInt(fx),
962 Sk48Dot16FloorToInt(fy),
963 color,
964 fontScaler,
965 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700966 }
967 pos += scalarsPerPosition;
968 }
969 } else {
970 while (text < stop) {
971 const char* currentText = text;
972 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
973
974 if (metricGlyph.fWidth) {
975 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
976 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
977 SkPoint tmsLoc;
978 tmsProc(pos, &tmsLoc);
979 SkPoint alignLoc;
980 alignProc(tmsLoc, metricGlyph, &alignLoc);
981
982 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + halfSampleX);
983 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + halfSampleY);
984
985 // have to call again, now that we've been "aligned"
986 const SkGlyph& glyph = glyphCacheProc(cache, &currentText,
987 fx & fxMask, fy & fyMask);
988 // the assumption is that the metrics haven't changed
989 SkASSERT(prevAdvX == glyph.fAdvanceX);
990 SkASSERT(prevAdvY == glyph.fAdvanceY);
991 SkASSERT(glyph.fWidth);
992
joshualitt9bd2daf2015-04-17 09:30:06 -0700993 this->bmpAppendGlyph(blob,
994 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -0700995 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -0700996 Sk48Dot16FloorToInt(fx),
997 Sk48Dot16FloorToInt(fy),
998 color,
999 fontScaler,
1000 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001001 }
1002 pos += scalarsPerPosition;
1003 }
1004 }
1005 } else { // not subpixel
1006
1007 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
1008 while (text < stop) {
1009 // the last 2 parameters are ignored
1010 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1011
1012 if (glyph.fWidth) {
1013 SkPoint tmsLoc;
1014 tmsProc(pos, &tmsLoc);
1015
1016 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + SK_ScalarHalf); //halfSampleX;
1017 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + SK_ScalarHalf); //halfSampleY;
joshualitt9bd2daf2015-04-17 09:30:06 -07001018 this->bmpAppendGlyph(blob,
1019 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001020 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001021 Sk48Dot16FloorToInt(fx),
1022 Sk48Dot16FloorToInt(fy),
1023 color,
1024 fontScaler,
1025 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001026 }
1027 pos += scalarsPerPosition;
1028 }
1029 } else {
1030 while (text < stop) {
1031 // the last 2 parameters are ignored
1032 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1033
1034 if (glyph.fWidth) {
1035 SkPoint tmsLoc;
1036 tmsProc(pos, &tmsLoc);
1037
1038 SkPoint alignLoc;
1039 alignProc(tmsLoc, glyph, &alignLoc);
1040
1041 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + SK_ScalarHalf); //halfSampleX;
1042 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + SK_ScalarHalf); //halfSampleY;
joshualitt9bd2daf2015-04-17 09:30:06 -07001043 this->bmpAppendGlyph(blob,
1044 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001045 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001046 Sk48Dot16FloorToInt(fx),
1047 Sk48Dot16FloorToInt(fy),
1048 color,
1049 fontScaler,
1050 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001051 }
1052 pos += scalarsPerPosition;
1053 }
1054 }
1055 }
1056}
1057
joshualitt9bd2daf2015-04-17 09:30:06 -07001058
joshualitt374b2f72015-07-21 08:05:03 -07001059void GrAtlasTextContext::internalDrawDFText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -07001060 SkGlyphCache* cache, const SkPaint& skPaint,
1061 GrColor color,
1062 const SkMatrix& viewMatrix,
1063 const char text[], size_t byteLength,
1064 SkScalar x, SkScalar y, const SkIRect& clipRect,
1065 SkScalar textRatio,
1066 SkTDArray<char>* fallbackTxt,
1067 SkTDArray<SkScalar>* fallbackPos,
1068 SkPoint* offset,
1069 const SkPaint& origPaint) {
halcanary96fcdcc2015-08-27 07:41:13 -07001070 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -07001071
1072 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -07001073 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001074 return;
1075 }
1076
1077 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
1078 SkAutoDescriptor desc;
halcanary96fcdcc2015-08-27 07:41:13 -07001079 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, nullptr, true);
joshualitt9bd2daf2015-04-17 09:30:06 -07001080 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
1081 desc.getDesc());
1082
1083 SkTArray<SkScalar> positions;
1084
1085 const char* textPtr = text;
1086 SkFixed stopX = 0;
1087 SkFixed stopY = 0;
1088 SkFixed origin = 0;
1089 switch (origPaint.getTextAlign()) {
1090 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
1091 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
1092 case SkPaint::kLeft_Align: origin = 0; break;
1093 }
1094
1095 SkAutoKern autokern;
1096 const char* stop = text + byteLength;
1097 while (textPtr < stop) {
1098 // don't need x, y here, since all subpixel variants will have the
1099 // same advance
1100 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
1101
1102 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
1103 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
1104
1105 SkFixed height = glyph.fAdvanceY;
1106 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
1107
1108 stopX += width;
1109 stopY += height;
1110 }
1111 SkASSERT(textPtr == stop);
1112
1113 // now adjust starting point depending on alignment
1114 SkScalar alignX = SkFixedToScalar(stopX);
1115 SkScalar alignY = SkFixedToScalar(stopY);
1116 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
1117 alignX = SkScalarHalf(alignX);
1118 alignY = SkScalarHalf(alignY);
1119 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
1120 alignX = 0;
1121 alignY = 0;
1122 }
1123 x -= alignX;
1124 y -= alignY;
1125 *offset = SkPoint::Make(x, y);
1126
1127 this->internalDrawDFPosText(blob, runIndex, cache, skPaint, color, viewMatrix, text, byteLength,
1128 positions.begin(), 2, *offset, clipRect, textRatio, fallbackTxt,
1129 fallbackPos);
1130 SkGlyphCache::AttachCache(origPaintCache);
1131}
1132
joshualitt374b2f72015-07-21 08:05:03 -07001133void GrAtlasTextContext::internalDrawDFPosText(GrAtlasTextBlob* blob, int runIndex,
joshualitt9bd2daf2015-04-17 09:30:06 -07001134 SkGlyphCache* cache, const SkPaint& skPaint,
1135 GrColor color,
1136 const SkMatrix& viewMatrix,
1137 const char text[], size_t byteLength,
1138 const SkScalar pos[], int scalarsPerPosition,
1139 const SkPoint& offset, const SkIRect& clipRect,
1140 SkScalar textRatio,
1141 SkTDArray<char>* fallbackTxt,
1142 SkTDArray<SkScalar>* fallbackPos) {
1143
halcanary96fcdcc2015-08-27 07:41:13 -07001144 SkASSERT(byteLength == 0 || text != nullptr);
joshualitt9bd2daf2015-04-17 09:30:06 -07001145 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1146
1147 // nothing to draw
halcanary96fcdcc2015-08-27 07:41:13 -07001148 if (text == nullptr || byteLength == 0) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001149 return;
1150 }
1151
halcanary96fcdcc2015-08-27 07:41:13 -07001152 fCurrStrike = nullptr;
joshualitt9bd2daf2015-04-17 09:30:06 -07001153
1154 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
1155 GrFontScaler* fontScaler = GetGrFontScaler(cache);
1156
1157 const char* stop = text + byteLength;
1158
1159 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
1160 while (text < stop) {
1161 const char* lastText = text;
1162 // the last 2 parameters are ignored
1163 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1164
1165 if (glyph.fWidth) {
1166 SkScalar x = offset.x() + pos[0];
1167 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
1168
1169 if (!this->dfAppendGlyph(blob,
1170 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001171 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001172 x, y, color, fontScaler, clipRect,
1173 textRatio, viewMatrix)) {
1174 // couldn't append, send to fallback
1175 fallbackTxt->append(SkToInt(text-lastText), lastText);
1176 *fallbackPos->append() = pos[0];
1177 if (2 == scalarsPerPosition) {
1178 *fallbackPos->append() = pos[1];
1179 }
1180 }
1181 }
1182 pos += scalarsPerPosition;
1183 }
1184 } else {
1185 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
1186 : SK_Scalar1;
1187 while (text < stop) {
1188 const char* lastText = text;
1189 // the last 2 parameters are ignored
1190 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1191
1192 if (glyph.fWidth) {
1193 SkScalar x = offset.x() + pos[0];
1194 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
1195
1196 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
1197 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
1198
1199 if (!this->dfAppendGlyph(blob,
1200 runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001201 glyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001202 x - advanceX, y - advanceY, color,
1203 fontScaler,
1204 clipRect,
1205 textRatio,
1206 viewMatrix)) {
1207 // couldn't append, send to fallback
1208 fallbackTxt->append(SkToInt(text-lastText), lastText);
1209 *fallbackPos->append() = pos[0];
1210 if (2 == scalarsPerPosition) {
1211 *fallbackPos->append() = pos[1];
1212 }
1213 }
1214 }
1215 pos += scalarsPerPosition;
1216 }
1217 }
1218}
1219
joshualitt374b2f72015-07-21 08:05:03 -07001220void GrAtlasTextContext::bmpAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001221 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001222 int vx, int vy, GrColor color, GrFontScaler* scaler,
1223 const SkIRect& clipRect) {
joshualittae32c102015-04-21 09:37:57 -07001224 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001225 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001226 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1227 }
1228
joshualitt6c2c2b02015-07-24 10:37:00 -07001229 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1230 skGlyph.getSubXFixed(),
1231 skGlyph.getSubYFixed(),
1232 GrGlyph::kCoverage_MaskStyle);
1233 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001234 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001235 return;
1236 }
1237
1238 int x = vx + glyph->fBounds.fLeft;
1239 int y = vy + glyph->fBounds.fTop;
1240
1241 // keep them as ints until we've done the clip-test
1242 int width = glyph->fBounds.width();
1243 int height = glyph->fBounds.height();
1244
joshualitt2a0e9f32015-04-13 06:12:21 -07001245#if 0
1246 // Not checking the clip bounds might introduce a performance regression. However, its not
1247 // clear if this is still true today with the larger tiles we use in Chrome. For repositionable
1248 // blobs, we want to make sure we have all of the glyphs, so clipping them out is not ideal.
1249 // We could store the cliprect in the key, but then we'd lose the ability to do integer scrolls
1250 // TODO verify this
joshualitt1d89e8d2015-04-01 12:40:54 -07001251 // check if we clipped out
1252 if (clipRect.quickReject(x, y, x + width, y + height)) {
1253 return;
1254 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001255#endif
joshualitt1d89e8d2015-04-01 12:40:54 -07001256
1257 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001258 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001259 this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy));
joshualitt1d89e8d2015-04-01 12:40:54 -07001260 return;
1261 }
1262
joshualitt1d89e8d2015-04-01 12:40:54 -07001263 GrMaskFormat format = glyph->fMaskFormat;
1264
1265 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
1266 if (run.fInitialized && subRun->fMaskFormat != format) {
joshualittd9f13ae2015-07-24 11:24:31 -07001267 subRun = &run.push_back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001268 subRun->fStrike.reset(SkRef(fCurrStrike));
1269 } else if (!run.fInitialized) {
1270 subRun->fStrike.reset(SkRef(fCurrStrike));
joshualitt1d89e8d2015-04-01 12:40:54 -07001271 }
1272
1273 run.fInitialized = true;
joshualitt1d89e8d2015-04-01 12:40:54 -07001274
1275 size_t vertexStride = get_vertex_stride(format);
1276
1277 SkRect r;
1278 r.fLeft = SkIntToScalar(x);
1279 r.fTop = SkIntToScalar(y);
1280 r.fRight = r.fLeft + SkIntToScalar(width);
1281 r.fBottom = r.fTop + SkIntToScalar(height);
joshualitt9bd2daf2015-04-17 09:30:06 -07001282 subRun->fMaskFormat = format;
1283 this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format,
joshualittae32c102015-04-21 09:37:57 -07001284 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001285}
joshualitt1d89e8d2015-04-01 12:40:54 -07001286
joshualitt374b2f72015-07-21 08:05:03 -07001287bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001288 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001289 SkScalar sx, SkScalar sy, GrColor color,
1290 GrFontScaler* scaler,
1291 const SkIRect& clipRect,
1292 SkScalar textRatio, const SkMatrix& viewMatrix) {
joshualittae32c102015-04-21 09:37:57 -07001293 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001294 if (!fCurrStrike) {
1295 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
1296 }
1297
joshualitt6c2c2b02015-07-24 10:37:00 -07001298 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1299 skGlyph.getSubXFixed(),
1300 skGlyph.getSubYFixed(),
1301 GrGlyph::kDistance_MaskStyle);
1302 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001303 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001304 return true;
1305 }
1306
1307 // fallback to color glyph support
1308 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
1309 return false;
1310 }
1311
1312 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
1313 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
1314 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
1315 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
1316
1317 SkScalar scale = textRatio;
1318 dx *= scale;
1319 dy *= scale;
1320 width *= scale;
1321 height *= scale;
1322 sx += dx;
1323 sy += dy;
1324 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
1325
1326#if 0
1327 // check if we clipped out
1328 SkRect dstRect;
1329 viewMatrix.mapRect(&dstRect, glyphRect);
1330 if (clipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
1331 SkScalarTruncToInt(dstRect.top()),
1332 SkScalarTruncToInt(dstRect.right()),
1333 SkScalarTruncToInt(dstRect.bottom()))) {
1334 return true;
1335 }
1336#endif
1337
1338 // TODO combine with the above
1339 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001340 if (glyph->fTooLargeForAtlas) {
joshualitt0fe04a22015-08-25 12:05:50 -07001341 this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy, scale, true);
joshualitt9bd2daf2015-04-17 09:30:06 -07001342 return true;
1343 }
1344
joshualitt9bd2daf2015-04-17 09:30:06 -07001345 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
joshualitt7e97b0b2015-07-31 15:18:08 -07001346 if (!run.fInitialized) {
1347 subRun->fStrike.reset(SkRef(fCurrStrike));
1348 }
1349 run.fInitialized = true;
joshualitt9bd2daf2015-04-17 09:30:06 -07001350 SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat);
1351 subRun->fMaskFormat = kA8_GrMaskFormat;
1352
1353 size_t vertexStride = get_vertex_stride_df(kA8_GrMaskFormat, subRun->fUseLCDText);
1354
1355 bool useColorVerts = !subRun->fUseLCDText;
1356 this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts,
joshualittae32c102015-04-21 09:37:57 -07001357 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001358 return true;
1359}
1360
joshualitt374b2f72015-07-21 08:05:03 -07001361inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001362 GrFontScaler* scaler, const SkGlyph& skGlyph,
joshualitt0fe04a22015-08-25 12:05:50 -07001363 SkScalar x, SkScalar y, SkScalar scale,
1364 bool applyVM) {
halcanary96fcdcc2015-08-27 07:41:13 -07001365 if (nullptr == glyph->fPath) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001366 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
1367 if (!glyphPath) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001368 return;
1369 }
joshualitt6c2c2b02015-07-24 10:37:00 -07001370
halcanary385fe4d2015-08-26 13:07:48 -07001371 glyph->fPath = new SkPath(*glyphPath);
joshualitt9bd2daf2015-04-17 09:30:06 -07001372 }
joshualitt0fe04a22015-08-25 12:05:50 -07001373 blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
joshualitt9bd2daf2015-04-17 09:30:06 -07001374}
1375
joshualitt374b2f72015-07-21 08:05:03 -07001376inline void GrAtlasTextContext::appendGlyphCommon(GrAtlasTextBlob* blob, Run* run,
joshualitt9bd2daf2015-04-17 09:30:06 -07001377 Run::SubRunInfo* subRun,
1378 const SkRect& positions, GrColor color,
1379 size_t vertexStride, bool useVertexColor,
joshualittae32c102015-04-21 09:37:57 -07001380 GrGlyph* glyph) {
1381 blob->fGlyphs[subRun->fGlyphEndIndex] = glyph;
joshualitt9bd2daf2015-04-17 09:30:06 -07001382 run->fVertexBounds.joinNonEmptyArg(positions);
1383 run->fColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -07001384
1385 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->fVertexEndIndex);
1386
joshualitt9bd2daf2015-04-17 09:30:06 -07001387 if (useVertexColor) {
joshualitt010db532015-04-21 10:07:26 -07001388 // V0
1389 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1390 position->set(positions.fLeft, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001391 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
1392 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001393 vertex += vertexStride;
joshualitt9bd2daf2015-04-17 09:30:06 -07001394
joshualitt010db532015-04-21 10:07:26 -07001395 // V1
1396 position = reinterpret_cast<SkPoint*>(vertex);
1397 position->set(positions.fLeft, positions.fBottom);
1398 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001399 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001400 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001401
joshualitt010db532015-04-21 10:07:26 -07001402 // V2
1403 position = reinterpret_cast<SkPoint*>(vertex);
1404 position->set(positions.fRight, positions.fBottom);
1405 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001406 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001407 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001408
joshualitt010db532015-04-21 10:07:26 -07001409 // V3
1410 position = reinterpret_cast<SkPoint*>(vertex);
1411 position->set(positions.fRight, positions.fTop);
1412 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001413 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001414 } else {
1415 // V0
1416 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1417 position->set(positions.fLeft, positions.fTop);
1418 vertex += vertexStride;
1419
1420 // V1
1421 position = reinterpret_cast<SkPoint*>(vertex);
1422 position->set(positions.fLeft, positions.fBottom);
1423 vertex += vertexStride;
1424
1425 // V2
1426 position = reinterpret_cast<SkPoint*>(vertex);
1427 position->set(positions.fRight, positions.fBottom);
1428 vertex += vertexStride;
1429
1430 // V3
1431 position = reinterpret_cast<SkPoint*>(vertex);
1432 position->set(positions.fRight, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001433 }
1434
1435 subRun->fGlyphEndIndex++;
1436 subRun->fVertexEndIndex += vertexStride * kVerticesPerGlyph;
1437}
1438
bsalomonabd30f52015-08-13 13:34:48 -07001439class TextBatch : public GrVertexBatch {
joshualitt1d89e8d2015-04-01 12:40:54 -07001440public:
joshualitt9bd2daf2015-04-17 09:30:06 -07001441 typedef GrAtlasTextContext::DistanceAdjustTable DistanceAdjustTable;
joshualitt374b2f72015-07-21 08:05:03 -07001442 typedef GrAtlasTextBlob Blob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001443 typedef Blob::Run Run;
1444 typedef Run::SubRunInfo TextInfo;
1445 struct Geometry {
joshualittad802c62015-04-15 05:31:57 -07001446 Blob* fBlob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001447 int fRun;
1448 int fSubRun;
1449 GrColor fColor;
joshualitt2a0e9f32015-04-13 06:12:21 -07001450 SkScalar fTransX;
1451 SkScalar fTransY;
joshualitt1d89e8d2015-04-01 12:40:54 -07001452 };
1453
bsalomon265697d2015-07-22 10:17:26 -07001454 static TextBatch* CreateBitmap(GrMaskFormat maskFormat, int glyphCount,
joshualittad802c62015-04-15 05:31:57 -07001455 GrBatchFontCache* fontCache) {
halcanary385fe4d2015-08-26 13:07:48 -07001456 TextBatch* batch = new TextBatch;
bsalomon265697d2015-07-22 10:17:26 -07001457
1458 batch->initClassID<TextBatch>();
1459 batch->fFontCache = fontCache;
1460 switch (maskFormat) {
1461 case kA8_GrMaskFormat:
1462 batch->fMaskType = kGrayscaleCoverageMask_MaskType;
1463 break;
1464 case kA565_GrMaskFormat:
1465 batch->fMaskType = kLCDCoverageMask_MaskType;
1466 break;
1467 case kARGB_GrMaskFormat:
1468 batch->fMaskType = kColorBitmapMask_MaskType;
1469 break;
1470 }
1471 batch->fBatch.fNumGlyphs = glyphCount;
bsalomond602f4d2015-07-27 06:12:01 -07001472 batch->fGeoCount = 1;
bsalomon265697d2015-07-22 10:17:26 -07001473 batch->fFilteredColor = 0;
1474 batch->fFontCache = fontCache;
1475 batch->fUseBGR = false;
1476 return batch;
joshualitt1d89e8d2015-04-01 12:40:54 -07001477 }
1478
bsalomon265697d2015-07-22 10:17:26 -07001479 static TextBatch* CreateDistanceField(int glyphCount, GrBatchFontCache* fontCache,
robertphillipsf6703fa2015-09-01 05:36:47 -07001480 const DistanceAdjustTable* distanceAdjustTable,
bsalomon265697d2015-07-22 10:17:26 -07001481 SkColor filteredColor, bool isLCD,
1482 bool useBGR) {
halcanary385fe4d2015-08-26 13:07:48 -07001483 TextBatch* batch = new TextBatch;
bsalomon265697d2015-07-22 10:17:26 -07001484 batch->initClassID<TextBatch>();
1485 batch->fFontCache = fontCache;
1486 batch->fMaskType = isLCD ? kLCDDistanceField_MaskType : kGrayscaleDistanceField_MaskType;
1487 batch->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
1488 batch->fFilteredColor = filteredColor;
1489 batch->fUseBGR = useBGR;
1490 batch->fBatch.fNumGlyphs = glyphCount;
bsalomond602f4d2015-07-27 06:12:01 -07001491 batch->fGeoCount = 1;
bsalomon265697d2015-07-22 10:17:26 -07001492 return batch;
joshualitt9bd2daf2015-04-17 09:30:06 -07001493 }
1494
bsalomone46f9fe2015-08-18 06:05:14 -07001495 // to avoid even the initial copy of the struct, we have a getter for the first item which
1496 // is used to seed the batch with its initial geometry. After seeding, the client should call
1497 // init() so the Batch can initialize itself
1498 Geometry& geometry() { return fGeoData[0]; }
1499
1500 void init() {
1501 const Geometry& geo = fGeoData[0];
1502 fBatch.fColor = geo.fColor;
1503 fBatch.fViewMatrix = geo.fBlob->fViewMatrix;
1504
1505 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
1506 // into device space
1507 const Run& run = geo.fBlob->fRuns[geo.fRun];
1508 if (run.fSubRunInfo[geo.fSubRun].fDrawAsDistanceFields) {
1509 SkRect bounds = run.fVertexBounds;
1510 fBatch.fViewMatrix.mapRect(&bounds);
1511 this->setBounds(bounds);
1512 } else {
1513 this->setBounds(run.fVertexBounds);
1514 }
1515 }
1516
bsalomon265697d2015-07-22 10:17:26 -07001517 const char* name() const override { return "TextBatch"; }
joshualitt1d89e8d2015-04-01 12:40:54 -07001518
1519 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001520 if (kColorBitmapMask_MaskType == fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001521 out->setUnknownFourComponents();
1522 } else {
1523 out->setKnownFourComponents(fBatch.fColor);
1524 }
1525 }
1526
1527 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001528 switch (fMaskType) {
1529 case kGrayscaleDistanceField_MaskType:
1530 case kGrayscaleCoverageMask_MaskType:
joshualitt1d89e8d2015-04-01 12:40:54 -07001531 out->setUnknownSingleComponent();
bsalomon265697d2015-07-22 10:17:26 -07001532 break;
1533 case kLCDCoverageMask_MaskType:
1534 case kLCDDistanceField_MaskType:
1535 out->setUnknownOpaqueFourComponents();
joshualitt1d89e8d2015-04-01 12:40:54 -07001536 out->setUsingLCDCoverage();
bsalomon265697d2015-07-22 10:17:26 -07001537 break;
1538 case kColorBitmapMask_MaskType:
1539 out->setKnownSingleComponent(0xff);
joshualitt1d89e8d2015-04-01 12:40:54 -07001540 }
1541 }
1542
bsalomone46f9fe2015-08-18 06:05:14 -07001543private:
bsalomon91d844d2015-08-10 10:47:29 -07001544 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001545 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -07001546 if (!opt.readsColor()) {
joshualitt416e14f2015-07-10 09:05:57 -07001547 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001548 }
bsalomon91d844d2015-08-10 10:47:29 -07001549 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt1d89e8d2015-04-01 12:40:54 -07001550
1551 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -07001552 fBatch.fColorIgnored = !opt.readsColor();
joshualitt416e14f2015-07-10 09:05:57 -07001553 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -07001554 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
1555 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt1d89e8d2015-04-01 12:40:54 -07001556 }
1557
bsalomonb5238a72015-05-05 07:49:49 -07001558 struct FlushInfo {
1559 SkAutoTUnref<const GrVertexBuffer> fVertexBuffer;
1560 SkAutoTUnref<const GrIndexBuffer> fIndexBuffer;
1561 int fGlyphsToFlush;
1562 int fVertexOffset;
1563 };
1564
bsalomon75398562015-08-17 12:55:38 -07001565 void onPrepareDraws(Target* target) override {
joshualitt1d89e8d2015-04-01 12:40:54 -07001566 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
1567 // TODO actually only invert if we don't have RGBA
1568 SkMatrix localMatrix;
1569 if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) {
1570 SkDebugf("Cannot invert viewmatrix\n");
1571 return;
1572 }
1573
bsalomon265697d2015-07-22 10:17:26 -07001574 GrTexture* texture = fFontCache->getTexture(this->maskFormat());
joshualitt62db8ba2015-04-09 08:22:37 -07001575 if (!texture) {
1576 SkDebugf("Could not allocate backing texture for atlas\n");
1577 return;
1578 }
1579
bsalomon265697d2015-07-22 10:17:26 -07001580 bool usesDistanceFields = this->usesDistanceFields();
1581 GrMaskFormat maskFormat = this->maskFormat();
1582 bool isLCD = this->isLCD();
1583
joshualitt9bd2daf2015-04-17 09:30:06 -07001584 SkAutoTUnref<const GrGeometryProcessor> gp;
bsalomon265697d2015-07-22 10:17:26 -07001585 if (usesDistanceFields) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001586 gp.reset(this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(),
1587 texture));
1588 } else {
1589 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
joshualitt9bd2daf2015-04-17 09:30:06 -07001590 gp.reset(GrBitmapTextGeoProc::Create(this->color(),
1591 texture,
1592 params,
bsalomon265697d2015-07-22 10:17:26 -07001593 maskFormat,
joshualittb8c241a2015-05-19 08:23:30 -07001594 localMatrix,
1595 this->usesLocalCoords()));
joshualitt9bd2daf2015-04-17 09:30:06 -07001596 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001597
bsalomonb5238a72015-05-05 07:49:49 -07001598 FlushInfo flushInfo;
1599 flushInfo.fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001600 size_t vertexStride = gp->getVertexStride();
bsalomon265697d2015-07-22 10:17:26 -07001601 SkASSERT(vertexStride == (usesDistanceFields ?
1602 get_vertex_stride_df(maskFormat, isLCD) :
1603 get_vertex_stride(maskFormat)));
joshualitt1d89e8d2015-04-01 12:40:54 -07001604
bsalomon75398562015-08-17 12:55:38 -07001605 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001606
1607 int glyphCount = this->numGlyphs();
bsalomon8415abe2015-05-04 11:41:41 -07001608 const GrVertexBuffer* vertexBuffer;
bsalomonb5238a72015-05-05 07:49:49 -07001609
bsalomon75398562015-08-17 12:55:38 -07001610 void* vertices = target->makeVertexSpace(vertexStride,
1611 glyphCount * kVerticesPerGlyph,
1612 &vertexBuffer,
1613 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -07001614 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
bsalomon75398562015-08-17 12:55:38 -07001615 flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
bsalomonb5238a72015-05-05 07:49:49 -07001616 if (!vertices || !flushInfo.fVertexBuffer) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001617 SkDebugf("Could not allocate vertices\n");
1618 return;
1619 }
1620
1621 unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices);
1622
joshualitt25ba7ea2015-04-21 07:49:49 -07001623 // We cache some values to avoid going to the glyphcache for the same fontScaler twice
1624 // in a row
halcanary96fcdcc2015-08-27 07:41:13 -07001625 const SkDescriptor* desc = nullptr;
1626 SkGlyphCache* cache = nullptr;
1627 GrFontScaler* scaler = nullptr;
1628 SkTypeface* typeface = nullptr;
joshualitt25ba7ea2015-04-21 07:49:49 -07001629
bsalomond602f4d2015-07-27 06:12:01 -07001630 for (int i = 0; i < fGeoCount; i++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001631 Geometry& args = fGeoData[i];
1632 Blob* blob = args.fBlob;
1633 Run& run = blob->fRuns[args.fRun];
1634 TextInfo& info = run.fSubRunInfo[args.fSubRun];
1635
bsalomon265697d2015-07-22 10:17:26 -07001636 uint64_t currentAtlasGen = fFontCache->atlasGeneration(maskFormat);
joshualitt7a9c45c2015-05-26 12:32:23 -07001637 bool regenerateTextureCoords = info.fAtlasGeneration != currentAtlasGen ||
joshualitt7e97b0b2015-07-31 15:18:08 -07001638 info.fStrike->isAbandoned();
joshualitt9bd2daf2015-04-17 09:30:06 -07001639 bool regenerateColors;
bsalomon265697d2015-07-22 10:17:26 -07001640 if (usesDistanceFields) {
1641 regenerateColors = !isLCD && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001642 } else {
bsalomon265697d2015-07-22 10:17:26 -07001643 regenerateColors = kA8_GrMaskFormat == maskFormat && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001644 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001645 bool regeneratePositions = args.fTransX != 0.f || args.fTransY != 0.f;
joshualitt1d89e8d2015-04-01 12:40:54 -07001646 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1647
1648 // We regenerate both texture coords and colors in the blob itself, and update the
1649 // atlas generation. If we don't end up purging any unused plots, we can avoid
1650 // regenerating the coords. We could take a finer grained approach to updating texture
1651 // coords but its not clear if the extra bookkeeping would offset any gains.
1652 // To avoid looping over the glyphs twice, we do one loop and conditionally update color
1653 // or coords as needed. One final note, if we have to break a run for an atlas eviction
1654 // then we can't really trust the atlas has all of the correct data. Atlas evictions
1655 // should be pretty rare, so we just always regenerate in those cases
joshualitt2a0e9f32015-04-13 06:12:21 -07001656 if (regenerateTextureCoords || regenerateColors || regeneratePositions) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001657 // first regenerate texture coordinates / colors if need be
joshualitt1d89e8d2015-04-01 12:40:54 -07001658 bool brokenRun = false;
joshualittae32c102015-04-21 09:37:57 -07001659
1660 // Because the GrBatchFontCache may evict the strike a blob depends on using for
1661 // generating its texture coords, we have to track whether or not the strike has
1662 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
1663 // otherwise we have to get the new strike, and use that to get the correct glyphs.
1664 // Because we do not have the packed ids, and thus can't look up our glyphs in the
1665 // new strike, we instead keep our ref to the old strike and use the packed ids from
1666 // it. These ids will still be valid as long as we hold the ref. When we are done
1667 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
1668 bool regenerateGlyphs = false;
halcanary96fcdcc2015-08-27 07:41:13 -07001669 GrBatchTextStrike* strike = nullptr;
joshualitt1d89e8d2015-04-01 12:40:54 -07001670 if (regenerateTextureCoords) {
joshualittb4c507e2015-04-08 08:07:59 -07001671 info.fBulkUseToken.reset();
joshualitt25ba7ea2015-04-21 07:49:49 -07001672
1673 // We can reuse if we have a valid strike and our descriptors / typeface are the
joshualitt94469302015-09-02 06:13:39 -07001674 // same. The override descriptor is only for the non distance field text within
1675 // a run
1676 const SkDescriptor* newDesc = (run.fOverrideDescriptor && !usesDistanceFields) ?
joshualitt97202d22015-04-22 13:47:02 -07001677 run.fOverrideDescriptor->getDesc() :
joshualitt25ba7ea2015-04-21 07:49:49 -07001678 run.fDescriptor.getDesc();
1679 if (!cache || !SkTypeface::Equal(typeface, run.fTypeface) ||
1680 !(desc->equals(*newDesc))) {
1681 if (cache) {
1682 SkGlyphCache::AttachCache(cache);
1683 }
1684 desc = newDesc;
1685 cache = SkGlyphCache::DetachCache(run.fTypeface, desc);
1686 scaler = GrTextContext::GetGrFontScaler(cache);
joshualitt7e97b0b2015-07-31 15:18:08 -07001687 strike = info.fStrike;
joshualitt25ba7ea2015-04-21 07:49:49 -07001688 typeface = run.fTypeface;
1689 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001690
joshualitt7e97b0b2015-07-31 15:18:08 -07001691 if (info.fStrike->isAbandoned()) {
joshualittae32c102015-04-21 09:37:57 -07001692 regenerateGlyphs = true;
1693 strike = fFontCache->getStrike(scaler);
1694 } else {
joshualitt7e97b0b2015-07-31 15:18:08 -07001695 strike = info.fStrike;
joshualittae32c102015-04-21 09:37:57 -07001696 }
1697 }
1698
1699 for (int glyphIdx = 0; glyphIdx < glyphCount; glyphIdx++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001700 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001701 size_t glyphOffset = glyphIdx + info.fGlyphStartIndex;
joshualitt6c2c2b02015-07-24 10:37:00 -07001702
1703 GrGlyph* glyph = blob->fGlyphs[glyphOffset];
1704 GrGlyph::PackedID id = glyph->fPackedID;
1705 const SkGlyph& skGlyph = scaler->grToSkGlyph(id);
joshualittae32c102015-04-21 09:37:57 -07001706 if (regenerateGlyphs) {
1707 // Get the id from the old glyph, and use the new strike to lookup
1708 // the glyph.
joshualitt76cc6572015-07-31 05:51:45 -07001709 blob->fGlyphs[glyphOffset] = strike->getGlyph(skGlyph, id, maskFormat,
1710 scaler);
joshualittae32c102015-04-21 09:37:57 -07001711 }
1712 glyph = blob->fGlyphs[glyphOffset];
joshualitt1d89e8d2015-04-01 12:40:54 -07001713 SkASSERT(glyph);
joshualitt65e96b42015-07-31 11:45:22 -07001714 SkASSERT(id == glyph->fPackedID);
1715 // We want to be able to assert this but cannot for testing purposes.
1716 // once skbug:4143 has landed we can revist this assert
1717 //SkASSERT(glyph->fMaskFormat == this->maskFormat());
joshualitt1d89e8d2015-04-01 12:40:54 -07001718
1719 if (!fFontCache->hasGlyph(glyph) &&
bsalomon75398562015-08-17 12:55:38 -07001720 !strike->addGlyphToAtlas(target, glyph, scaler, skGlyph, maskFormat)) {
1721 this->flush(target, &flushInfo);
1722 target->initDraw(gp, this->pipeline());
joshualitt1d89e8d2015-04-01 12:40:54 -07001723 brokenRun = glyphIdx > 0;
1724
bsalomon75398562015-08-17 12:55:38 -07001725 SkDEBUGCODE(bool success =) strike->addGlyphToAtlas(target,
joshualittae32c102015-04-21 09:37:57 -07001726 glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001727 scaler,
joshualitt4f19ca32015-07-30 07:59:20 -07001728 skGlyph,
1729 maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001730 SkASSERT(success);
1731 }
joshualittb4c507e2015-04-08 08:07:59 -07001732 fFontCache->addGlyphToBulkAndSetUseToken(&info.fBulkUseToken, glyph,
bsalomon75398562015-08-17 12:55:38 -07001733 target->currentToken());
joshualitt1d89e8d2015-04-01 12:40:54 -07001734
1735 // Texture coords are the last vertex attribute so we get a pointer to the
1736 // first one and then map with stride in regenerateTextureCoords
1737 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1738 vertex += info.fVertexStartIndex;
1739 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1740 vertex += vertexStride - sizeof(SkIPoint16);
1741
1742 this->regenerateTextureCoords(glyph, vertex, vertexStride);
1743 }
1744
1745 if (regenerateColors) {
1746 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1747 vertex += info.fVertexStartIndex;
1748 vertex += vertexStride * glyphIdx * kVerticesPerGlyph + sizeof(SkPoint);
1749 this->regenerateColors(vertex, vertexStride, args.fColor);
1750 }
1751
joshualitt2a0e9f32015-04-13 06:12:21 -07001752 if (regeneratePositions) {
1753 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1754 vertex += info.fVertexStartIndex;
1755 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1756 SkScalar transX = args.fTransX;
1757 SkScalar transY = args.fTransY;
1758 this->regeneratePositions(vertex, vertexStride, transX, transY);
1759 }
bsalomonb5238a72015-05-05 07:49:49 -07001760 flushInfo.fGlyphsToFlush++;
joshualitt1d89e8d2015-04-01 12:40:54 -07001761 }
1762
joshualitt2a0e9f32015-04-13 06:12:21 -07001763 // We my have changed the color so update it here
1764 run.fColor = args.fColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07001765 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001766 if (regenerateGlyphs) {
joshualitt7e97b0b2015-07-31 15:18:08 -07001767 info.fStrike.reset(SkRef(strike));
joshualittae32c102015-04-21 09:37:57 -07001768 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001769 info.fAtlasGeneration = brokenRun ? GrBatchAtlas::kInvalidAtlasGeneration :
bsalomon265697d2015-07-22 10:17:26 -07001770 fFontCache->atlasGeneration(maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001771 }
1772 } else {
bsalomonb5238a72015-05-05 07:49:49 -07001773 flushInfo.fGlyphsToFlush += glyphCount;
joshualittb4c507e2015-04-08 08:07:59 -07001774
1775 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1776 // have a valid atlas generation
bsalomon75398562015-08-17 12:55:38 -07001777 fFontCache->setUseTokenBulk(info.fBulkUseToken, target->currentToken(), maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001778 }
1779
1780 // now copy all vertices
1781 size_t byteCount = info.fVertexEndIndex - info.fVertexStartIndex;
1782 memcpy(currVertex, blob->fVertices + info.fVertexStartIndex, byteCount);
1783
1784 currVertex += byteCount;
1785 }
joshualitt25ba7ea2015-04-21 07:49:49 -07001786 // Make sure to attach the last cache if applicable
1787 if (cache) {
1788 SkGlyphCache::AttachCache(cache);
1789 }
bsalomon75398562015-08-17 12:55:38 -07001790 this->flush(target, &flushInfo);
joshualitt1d89e8d2015-04-01 12:40:54 -07001791 }
1792
bsalomon265697d2015-07-22 10:17:26 -07001793 TextBatch() {} // initialized in factory functions.
joshualittad802c62015-04-15 05:31:57 -07001794
bsalomon265697d2015-07-22 10:17:26 -07001795 ~TextBatch() {
bsalomond602f4d2015-07-27 06:12:01 -07001796 for (int i = 0; i < fGeoCount; i++) {
joshualittad802c62015-04-15 05:31:57 -07001797 fGeoData[i].fBlob->unref();
1798 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001799 }
1800
bsalomon265697d2015-07-22 10:17:26 -07001801 GrMaskFormat maskFormat() const {
1802 switch (fMaskType) {
1803 case kLCDCoverageMask_MaskType:
1804 return kA565_GrMaskFormat;
1805 case kColorBitmapMask_MaskType:
1806 return kARGB_GrMaskFormat;
1807 case kGrayscaleCoverageMask_MaskType:
1808 case kGrayscaleDistanceField_MaskType:
1809 case kLCDDistanceField_MaskType:
1810 return kA8_GrMaskFormat;
1811 }
1812 return kA8_GrMaskFormat; // suppress warning
1813 }
1814
1815 bool usesDistanceFields() const {
1816 return kGrayscaleDistanceField_MaskType == fMaskType ||
1817 kLCDDistanceField_MaskType == fMaskType;
1818 }
1819
1820 bool isLCD() const {
1821 return kLCDCoverageMask_MaskType == fMaskType ||
1822 kLCDDistanceField_MaskType == fMaskType;
1823 }
1824
joshualitt1d89e8d2015-04-01 12:40:54 -07001825 void regenerateTextureCoords(GrGlyph* glyph, intptr_t vertex, size_t vertexStride) {
1826 int width = glyph->fBounds.width();
1827 int height = glyph->fBounds.height();
joshualitt1d89e8d2015-04-01 12:40:54 -07001828
joshualitt9bd2daf2015-04-17 09:30:06 -07001829 int u0, v0, u1, v1;
bsalomon265697d2015-07-22 10:17:26 -07001830 if (this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001831 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
1832 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
1833 u1 = u0 + width - 2 * SK_DistanceFieldInset;
1834 v1 = v0 + height - 2 * SK_DistanceFieldInset;
1835 } else {
1836 u0 = glyph->fAtlasLocation.fX;
1837 v0 = glyph->fAtlasLocation.fY;
1838 u1 = u0 + width;
1839 v1 = v0 + height;
1840 }
1841
joshualitt1d89e8d2015-04-01 12:40:54 -07001842 SkIPoint16* textureCoords;
1843 // V0
1844 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1845 textureCoords->set(u0, v0);
1846 vertex += vertexStride;
1847
1848 // V1
1849 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1850 textureCoords->set(u0, v1);
1851 vertex += vertexStride;
1852
1853 // V2
1854 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1855 textureCoords->set(u1, v1);
1856 vertex += vertexStride;
1857
1858 // V3
1859 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1860 textureCoords->set(u1, v0);
1861 }
1862
1863 void regenerateColors(intptr_t vertex, size_t vertexStride, GrColor color) {
1864 for (int i = 0; i < kVerticesPerGlyph; i++) {
1865 SkColor* vcolor = reinterpret_cast<SkColor*>(vertex);
1866 *vcolor = color;
1867 vertex += vertexStride;
1868 }
1869 }
1870
joshualitt2a0e9f32015-04-13 06:12:21 -07001871 void regeneratePositions(intptr_t vertex, size_t vertexStride, SkScalar transX,
1872 SkScalar transY) {
1873 for (int i = 0; i < kVerticesPerGlyph; i++) {
1874 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
1875 point->fX += transX;
1876 point->fY += transY;
1877 vertex += vertexStride;
1878 }
1879 }
1880
bsalomon75398562015-08-17 12:55:38 -07001881 void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) {
bsalomoncb8979d2015-05-05 09:51:38 -07001882 GrVertices vertices;
bsalomonb5238a72015-05-05 07:49:49 -07001883 int maxGlyphsPerDraw = flushInfo->fIndexBuffer->maxQuads();
bsalomoncb8979d2015-05-05 09:51:38 -07001884 vertices.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001885 flushInfo->fIndexBuffer, flushInfo->fVertexOffset,
bsalomone64eb572015-05-07 11:35:55 -07001886 kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
bsalomonb5238a72015-05-05 07:49:49 -07001887 maxGlyphsPerDraw);
bsalomon75398562015-08-17 12:55:38 -07001888 target->draw(vertices);
bsalomonb5238a72015-05-05 07:49:49 -07001889 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
1890 flushInfo->fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001891 }
1892
1893 GrColor color() const { return fBatch.fColor; }
1894 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1895 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1896 int numGlyphs() const { return fBatch.fNumGlyphs; }
1897
bsalomoncb02b382015-08-12 11:14:50 -07001898 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -07001899 TextBatch* that = t->cast<TextBatch>();
1900 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
1901 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -07001902 return false;
1903 }
1904
bsalomon265697d2015-07-22 10:17:26 -07001905 if (fMaskType != that->fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001906 return false;
1907 }
1908
bsalomon265697d2015-07-22 10:17:26 -07001909 if (!this->usesDistanceFields()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001910 // TODO we can often batch across LCD text if we have dual source blending and don't
1911 // have to use the blend constant
bsalomon265697d2015-07-22 10:17:26 -07001912 if (kGrayscaleCoverageMask_MaskType != fMaskType && this->color() != that->color()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001913 return false;
1914 }
joshualitt9bd2daf2015-04-17 09:30:06 -07001915 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1916 return false;
1917 }
1918 } else {
joshualitt9bd2daf2015-04-17 09:30:06 -07001919 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1920 return false;
1921 }
1922
1923 if (fFilteredColor != that->fFilteredColor) {
1924 return false;
1925 }
1926
joshualitt9bd2daf2015-04-17 09:30:06 -07001927 if (fUseBGR != that->fUseBGR) {
1928 return false;
1929 }
1930
joshualitt9bd2daf2015-04-17 09:30:06 -07001931 // TODO see note above
bsalomon265697d2015-07-22 10:17:26 -07001932 if (kLCDDistanceField_MaskType == fMaskType && this->color() != that->color()) {
joshualitt0fe04a22015-08-25 12:05:50 -07001933 return false;
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
robertphillipsf6703fa2015-09-01 05:36:47 -07002056void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* dc, GrRenderTarget* rt,
2057 const SkTextBlob::RunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -07002058 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07002059 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
2060 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
2061 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07002062
joshualitt9a27e632015-04-06 10:53:36 -07002063 size_t textLen = it.glyphCount() * sizeof(uint16_t);
2064 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07002065
joshualitt9a27e632015-04-06 10:53:36 -07002066 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07002067
joshualitt9a27e632015-04-06 10:53:36 -07002068 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
2069 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07002070 }
2071
robertphillipsfcf78292015-06-19 11:49:52 -07002072 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07002073
2074 switch (it.positioning()) {
2075 case SkTextBlob::kDefault_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -07002076 this->drawTextAsPath(dc, rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002077 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002078 textLen, x + offset.x(), y + offset.y(), clipBounds);
2079 break;
2080 case SkTextBlob::kHorizontal_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -07002081 this->drawPosTextAsPath(dc, rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002082 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002083 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
2084 clipBounds);
2085 break;
2086 case SkTextBlob::kFull_Positioning:
robertphillipsf6703fa2015-09-01 05:36:47 -07002087 this->drawPosTextAsPath(dc, rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002088 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002089 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
2090 break;
2091 }
2092}
2093
bsalomonabd30f52015-08-13 13:34:48 -07002094inline GrDrawBatch*
joshualitt374b2f72015-07-21 08:05:03 -07002095GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -07002096 int glyphCount, int run, int subRun,
2097 GrColor color, SkScalar transX, SkScalar transY,
2098 const SkPaint& skPaint) {
2099 GrMaskFormat format = info.fMaskFormat;
2100 GrColor subRunColor;
2101 if (kARGB_GrMaskFormat == format) {
2102 uint8_t paintAlpha = skPaint.getAlpha();
2103 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
2104 } else {
2105 subRunColor = color;
2106 }
2107
bsalomon265697d2015-07-22 10:17:26 -07002108 TextBatch* batch;
joshualitt79dfb2b2015-05-11 08:58:08 -07002109 if (info.fDrawAsDistanceFields) {
2110 SkColor filteredColor;
2111 SkColorFilter* colorFilter = skPaint.getColorFilter();
2112 if (colorFilter) {
2113 filteredColor = colorFilter->filterColor(skPaint.getColor());
2114 } else {
2115 filteredColor = skPaint.getColor();
2116 }
robertphillipsfcf78292015-06-19 11:49:52 -07002117 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
bsalomon265697d2015-07-22 10:17:26 -07002118 batch = TextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
2119 fDistanceAdjustTable, filteredColor,
2120 info.fUseLCDText, useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07002121 } else {
bsalomon265697d2015-07-22 10:17:26 -07002122 batch = TextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07002123 }
bsalomon265697d2015-07-22 10:17:26 -07002124 TextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07002125 geometry.fBlob = SkRef(cacheBlob);
2126 geometry.fRun = run;
2127 geometry.fSubRun = subRun;
2128 geometry.fColor = subRunColor;
2129 geometry.fTransX = transX;
2130 geometry.fTransY = transY;
2131 batch->init();
2132
2133 return batch;
2134}
2135
robertphillipsf6703fa2015-09-01 05:36:47 -07002136inline void GrAtlasTextContext::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07002137 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07002138 SkScalar transX, SkScalar transY,
2139 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07002140 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
2141 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
2142 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
2143 if (0 == glyphCount) {
2144 continue;
2145 }
2146
bsalomonabd30f52015-08-13 13:34:48 -07002147 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
2148 subRun, color, transX, transY,
2149 skPaint));
robertphillipsf6703fa2015-09-01 05:36:47 -07002150 dc->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07002151 }
2152}
2153
robertphillipsf6703fa2015-09-01 05:36:47 -07002154inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob,
2155 GrDrawContext* dc, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -07002156 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07002157 SkScalar transX, SkScalar transY,
2158 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07002159 if (!cacheBlob->fBigGlyphs.count()) {
2160 return;
2161 }
2162
joshualitt9a27e632015-04-06 10:53:36 -07002163 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07002164 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07002165 bigGlyph.fVx += transX;
2166 bigGlyph.fVy += transY;
joshualitt0fe04a22015-08-25 12:05:50 -07002167 SkMatrix ctm;
2168 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
2169 ctm.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
2170 if (bigGlyph.fApplyVM) {
2171 ctm.postConcat(cacheBlob->fViewMatrix);
2172 }
joshualittfc072562015-05-13 12:15:06 -07002173
robertphillipsf6703fa2015-09-01 05:36:47 -07002174 GrBlurUtils::drawPathWithMaskFilter(fContext, dc, rt, clip, bigGlyph.fPath,
joshualitt0fe04a22015-08-25 12:05:50 -07002175 skPaint, ctm, nullptr, 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,
robertphillipsf6703fa2015-09-01 05:36:47 -07002181 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07002182 GrRenderTarget* rt,
2183 const SkPaint& skPaint,
2184 const GrPaint& grPaint,
2185 SkDrawFilter* drawFilter,
2186 const GrClip& clip,
2187 const SkMatrix& viewMatrix,
2188 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07002189 SkScalar x, SkScalar y,
2190 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07002191 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
2192 // it as paths
joshualitt7b670db2015-07-09 13:25:02 -07002193 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002194
2195 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002196
2197 SkTextBlob::RunIterator it(blob);
2198 for (int run = 0; !it.done(); it.next(), run++) {
2199 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillipsf6703fa2015-09-01 05:36:47 -07002200 this->flushRunAsPaths(dc, rt, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07002201 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07002202 continue;
2203 }
joshualitt2a0e9f32015-04-13 06:12:21 -07002204 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillipsf6703fa2015-09-01 05:36:47 -07002205 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07002206 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002207 }
2208
2209 // Now flush big glyphs
robertphillipsf6703fa2015-09-01 05:36:47 -07002210 this->flushBigGlyphs(cacheBlob, dc, rt, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002211}
2212
joshualitt374b2f72015-07-21 08:05:03 -07002213void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
robertphillipsf6703fa2015-09-01 05:36:47 -07002214 GrDrawContext* dc,
joshualitt9a27e632015-04-06 10:53:36 -07002215 GrRenderTarget* rt,
2216 const SkPaint& skPaint,
2217 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07002218 const GrClip& clip,
2219 const SkIRect& clipBounds) {
joshualitt7b670db2015-07-09 13:25:02 -07002220 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002221
2222 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002223 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillipsf6703fa2015-09-01 05:36:47 -07002224 this->flushRun(dc, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002225 }
2226
2227 // Now flush big glyphs
robertphillipsf6703fa2015-09-01 05:36:47 -07002228 this->flushBigGlyphs(cacheBlob, dc, rt, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002229}
joshualitt79dfb2b2015-05-11 08:58:08 -07002230
2231///////////////////////////////////////////////////////////////////////////////////////////////////
2232
2233#ifdef GR_TEST_UTILS
2234
bsalomonabd30f52015-08-13 13:34:48 -07002235DRAW_BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07002236 static uint32_t gContextID = SK_InvalidGenID;
halcanary96fcdcc2015-08-27 07:41:13 -07002237 static GrAtlasTextContext* gTextContext = nullptr;
robertphillipsfcf78292015-06-19 11:49:52 -07002238 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07002239
2240 if (context->uniqueID() != gContextID) {
2241 gContextID = context->uniqueID();
halcanary385fe4d2015-08-26 13:07:48 -07002242 delete gTextContext;
robertphillips2334fb62015-06-17 05:43:33 -07002243
joshualitt79dfb2b2015-05-11 08:58:08 -07002244 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
2245 // because we don't really want to have a gpu device here.
2246 // We enable distance fields by twiddling a knob on the paint
robertphillipsf6703fa2015-09-01 05:36:47 -07002247 gTextContext = GrAtlasTextContext::Create(context, 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;
halcanary96fcdcc2015-08-27 07:41:13 -07002257 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, true, nullptr, 0));
joshualitt79dfb2b2015-05-11 08:58:08 -07002258 SkASSERT(texture);
halcanary96fcdcc2015-08-27 07:41:13 -07002259 SkASSERT(nullptr != texture->asRenderTarget());
joshualitt79dfb2b2015-05-11 08:58:08 -07002260 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