blob: 8725aa5c1791cfbec1d69372f456b1a762a0afae [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 "GrBatch.h"
10#include "GrBatchFontCache.h"
11#include "GrBatchTarget.h"
joshualitt79dfb2b2015-05-11 08:58:08 -070012#include "GrBatchTest.h"
robertphillipsccb1b572015-05-27 11:02:55 -070013#include "GrBlurUtils.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070014#include "GrDefaultGeoProcFactory.h"
robertphillipsea461502015-05-26 11:38:03 -070015#include "GrDrawContext.h"
robertphillips2334fb62015-06-17 05:43:33 -070016#include "GrDrawTarget.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070017#include "GrFontScaler.h"
18#include "GrIndexBuffer.h"
bsalomoned0bcad2015-05-04 10:36:42 -070019#include "GrResourceProvider.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070020#include "GrStrokeInfo.h"
joshualittb7133be2015-04-08 09:08:31 -070021#include "GrTextBlobCache.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070022#include "GrTexturePriv.h"
bsalomon72e3ae42015-04-28 08:08:46 -070023#include "GrVertexBuffer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070024
25#include "SkAutoKern.h"
26#include "SkColorPriv.h"
joshualitt9bd2daf2015-04-17 09:30:06 -070027#include "SkColorFilter.h"
28#include "SkDistanceFieldGen.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070029#include "SkDraw.h"
30#include "SkDrawFilter.h"
31#include "SkDrawProcs.h"
32#include "SkGlyphCache.h"
33#include "SkGpuDevice.h"
34#include "SkGr.h"
35#include "SkPath.h"
36#include "SkRTConf.h"
37#include "SkStrokeRec.h"
38#include "SkTextBlob.h"
39#include "SkTextMapStateProc.h"
40
41#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
99// TODO
joshualitt9bd2daf2015-04-17 09:30:06 -0700100// Distance field text in textblobs
joshualitt1d89e8d2015-04-01 12:40:54 -0700101
joshualittdbd35932015-04-02 09:19:04 -0700102GrAtlasTextContext::GrAtlasTextContext(GrContext* context,
robertphillips2334fb62015-06-17 05:43:33 -0700103 GrDrawContext* drawContext,
robertphillipsfcf78292015-06-19 11:49:52 -0700104 const SkSurfaceProps& surfaceProps)
105 : INHERITED(context, drawContext, surfaceProps)
robertphillips9fc82752015-06-19 04:46:45 -0700106 , fDistanceAdjustTable(SkNEW(DistanceAdjustTable)) {
joshualittb7133be2015-04-08 09:08:31 -0700107 // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest
108 // vertexStride
109 SK_COMPILE_ASSERT(kGrayTextVASize >= kColorTextVASize && kGrayTextVASize >= kLCDTextVASize,
110 vertex_attribute_changed);
joshualitt1d89e8d2015-04-01 12:40:54 -0700111 fCurrStrike = NULL;
joshualittb7133be2015-04-08 09:08:31 -0700112 fCache = context->getTextBlobCache();
joshualitt9bd2daf2015-04-17 09:30:06 -0700113}
114
robertphillips9fc82752015-06-19 04:46:45 -0700115void GrAtlasTextContext::DistanceAdjustTable::buildDistanceAdjustTable() {
joshualitt9bd2daf2015-04-17 09:30:06 -0700116
117 // This is used for an approximation of the mask gamma hack, used by raster and bitmap
118 // text. The mask gamma hack is based off of guessing what the blend color is going to
119 // be, and adjusting the mask so that when run through the linear blend will
120 // produce the value closest to the desired result. However, in practice this means
121 // that the 'adjusted' mask is just increasing or decreasing the coverage of
122 // the mask depending on what it is thought it will blit against. For black (on
123 // assumed white) this means that coverages are decreased (on a curve). For white (on
124 // assumed black) this means that coverages are increased (on a a curve). At
125 // middle (perceptual) gray (which could be blit against anything) the coverages
126 // remain the same.
127 //
128 // The idea here is that instead of determining the initial (real) coverage and
129 // then adjusting that coverage, we determine an adjusted coverage directly by
130 // essentially manipulating the geometry (in this case, the distance to the glyph
131 // edge). So for black (on assumed white) this thins a bit; for white (on
132 // assumed black) this fake bolds the geometry a bit.
133 //
134 // The distance adjustment is calculated by determining the actual coverage value which
135 // when fed into in the mask gamma table gives us an 'adjusted coverage' value of 0.5. This
136 // actual coverage value (assuming it's between 0 and 1) corresponds to a distance from the
137 // actual edge. So by subtracting this distance adjustment and computing without the
138 // the coverage adjustment we should get 0.5 coverage at the same point.
139 //
140 // This has several implications:
141 // For non-gray lcd smoothed text, each subpixel essentially is using a
142 // slightly different geometry.
143 //
144 // For black (on assumed white) this may not cover some pixels which were
145 // previously covered; however those pixels would have been only slightly
146 // covered and that slight coverage would have been decreased anyway. Also, some pixels
147 // which were previously fully covered may no longer be fully covered.
148 //
149 // For white (on assumed black) this may cover some pixels which weren't
150 // previously covered at all.
151
152 int width, height;
153 size_t size;
154
155#ifdef SK_GAMMA_CONTRAST
156 SkScalar contrast = SK_GAMMA_CONTRAST;
157#else
158 SkScalar contrast = 0.5f;
159#endif
robertphillips9fc82752015-06-19 04:46:45 -0700160 SkScalar paintGamma = SK_GAMMA_EXPONENT;
161 SkScalar deviceGamma = SK_GAMMA_EXPONENT;
joshualitt9bd2daf2015-04-17 09:30:06 -0700162
163 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
164 &width, &height);
165
166 SkASSERT(kExpectedDistanceAdjustTableSize == height);
167 fTable = SkNEW_ARRAY(SkScalar, height);
168
169 SkAutoTArray<uint8_t> data((int)size);
170 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
171
172 // find the inverse points where we cross 0.5
173 // binsearch might be better, but we only need to do this once on creation
174 for (int row = 0; row < height; ++row) {
175 uint8_t* rowPtr = data.get() + row*width;
176 for (int col = 0; col < width - 1; ++col) {
177 if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) {
178 // compute point where a mask value will give us a result of 0.5
179 float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPtr[col]);
180 float borderAlpha = (col + interp) / 255.f;
181
182 // compute t value for that alpha
183 // this is an approximate inverse for smoothstep()
184 float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5.0f) / 3.0f;
185
186 // compute distance which gives us that t value
187 const float kDistanceFieldAAFactor = 0.65f; // should match SK_DistanceFieldAAFactor
188 float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor;
189
190 fTable[row] = d;
191 break;
192 }
193 }
194 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700195}
196
joshualittdbd35932015-04-02 09:19:04 -0700197GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context,
robertphillips2334fb62015-06-17 05:43:33 -0700198 GrDrawContext* drawContext,
robertphillipsfcf78292015-06-19 11:49:52 -0700199 const SkSurfaceProps& surfaceProps) {
200 return SkNEW_ARGS(GrAtlasTextContext, (context, drawContext, surfaceProps));
joshualitt1d89e8d2015-04-01 12:40:54 -0700201}
202
joshualittdbd35932015-04-02 09:19:04 -0700203bool GrAtlasTextContext::canDraw(const GrRenderTarget*,
204 const GrClip&,
205 const GrPaint&,
206 const SkPaint& skPaint,
207 const SkMatrix& viewMatrix) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700208 return this->canDrawAsDistanceFields(skPaint, viewMatrix) ||
209 !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -0700210}
211
joshualitt9e36c1a2015-04-14 12:17:27 -0700212GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
213 GrColor canonicalColor = paint.computeLuminanceColor();
214 if (lcd) {
215 // This is the correct computation, but there are tons of cases where LCD can be overridden.
216 // For now we just regenerate if any run in a textblob has LCD.
217 // TODO figure out where all of these overrides are and see if we can incorporate that logic
218 // at a higher level *OR* use sRGB
219 SkASSERT(false);
220 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
221 } else {
222 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
223 // gamma corrected masks anyways, nor color
224 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
225 SkColorGetG(canonicalColor),
226 SkColorGetB(canonicalColor));
227 // reduce to our finite number of bits
228 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
229 }
230 return canonicalColor;
231}
232
233// TODO if this function ever shows up in profiling, then we can compute this value when the
234// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
235// run so this is not a big deal to compute here.
236bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) {
237 SkTextBlob::RunIterator it(blob);
238 for (; !it.done(); it.next()) {
239 if (it.isLCD()) {
240 return true;
241 }
242 }
243 return false;
244}
245
joshualitt2a0e9f32015-04-13 06:12:21 -0700246bool GrAtlasTextContext::MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY,
joshualitt374b2f72015-07-21 08:05:03 -0700247 const GrAtlasTextBlob& blob, const SkPaint& paint,
joshualitt53b5f442015-04-13 06:33:59 -0700248 const SkMaskFilter::BlurRec& blurRec,
joshualittdbd35932015-04-02 09:19:04 -0700249 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700250 // If we have LCD text then our canonical color will be set to transparent, in this case we have
251 // to regenerate the blob on any color change
252 if (blob.fKey.fCanonicalColor == SK_ColorTRANSPARENT && blob.fPaintColor != paint.getColor()) {
joshualitt2a0e9f32015-04-13 06:12:21 -0700253 return true;
254 }
255
256 if (blob.fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
257 return true;
258 }
259
260 if (blob.fViewMatrix.hasPerspective() && !blob.fViewMatrix.cheapEqualTo(viewMatrix)) {
261 return true;
262 }
263
joshualitt53b5f442015-04-13 06:33:59 -0700264 // We only cache one masked version
265 if (blob.fKey.fHasBlur &&
266 (blob.fBlurRec.fSigma != blurRec.fSigma ||
267 blob.fBlurRec.fStyle != blurRec.fStyle ||
268 blob.fBlurRec.fQuality != blurRec.fQuality)) {
269 return true;
270 }
271
272 // Similarly, we only cache one version for each style
273 if (blob.fKey.fStyle != SkPaint::kFill_Style &&
274 (blob.fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
275 blob.fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
276 blob.fStrokeInfo.fJoin != paint.getStrokeJoin())) {
277 return true;
278 }
279
joshualittfcfb9fc2015-04-21 07:35:10 -0700280 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
281 // for mixed blobs if this becomes an issue.
282 if (blob.hasBitmap() && blob.hasDistanceField()) {
joshualitt473ffa12015-04-22 18:23:15 -0700283 // Identical viewmatrices and we can reuse in all cases
284 if (blob.fViewMatrix.cheapEqualTo(viewMatrix) && x == blob.fX && y == blob.fY) {
285 return false;
286 }
joshualitt2a0e9f32015-04-13 06:12:21 -0700287 return true;
288 }
289
joshualittfcfb9fc2015-04-21 07:35:10 -0700290 if (blob.hasBitmap()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700291 if (blob.fViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
292 blob.fViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
293 blob.fViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
294 blob.fViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
295 return true;
296 }
297
joshualittfcfb9fc2015-04-21 07:35:10 -0700298 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
299 // but only for integer translations.
300 // This cool bit of math will determine the necessary translation to apply to the already
301 // generated vertex coordinates to move them to the correct position
302 SkScalar transX = viewMatrix.getTranslateX() +
303 viewMatrix.getScaleX() * (x - blob.fX) +
304 viewMatrix.getSkewX() * (y - blob.fY) -
305 blob.fViewMatrix.getTranslateX();
306 SkScalar transY = viewMatrix.getTranslateY() +
307 viewMatrix.getSkewY() * (x - blob.fX) +
308 viewMatrix.getScaleY() * (y - blob.fY) -
309 blob.fViewMatrix.getTranslateY();
joshualittf0c000d2015-04-27 09:36:55 -0700310 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700311 return true;
312 }
313
joshualittfcfb9fc2015-04-21 07:35:10 -0700314 (*outTransX) = transX;
315 (*outTransY) = transY;
joshualitta7c63892015-04-21 13:24:37 -0700316 } else if (blob.hasDistanceField()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700317 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
318 // distance field being generated, so we have to regenerate in those cases
319 SkScalar newMaxScale = viewMatrix.getMaxScale();
320 SkScalar oldMaxScale = blob.fViewMatrix.getMaxScale();
321 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
322 if (scaleAdjust < blob.fMaxMinScale || scaleAdjust > blob.fMinMaxScale) {
323 return true;
324 }
325
326 (*outTransX) = x - blob.fX;
327 (*outTransY) = y - blob.fY;
joshualittfcfb9fc2015-04-21 07:35:10 -0700328 }
joshualitt374b2f72015-07-21 08:05:03 -0700329
joshualitta7c63892015-04-21 13:24:37 -0700330 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
331 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
332 // the blob anyways at flush time, so no need to regenerate explicitly
joshualitt2a0e9f32015-04-13 06:12:21 -0700333 return false;
joshualitt1d89e8d2015-04-01 12:40:54 -0700334}
335
336
joshualitt374b2f72015-07-21 08:05:03 -0700337inline SkGlyphCache* GrAtlasTextContext::setupCache(GrAtlasTextBlob::Run* run,
joshualittdbd35932015-04-02 09:19:04 -0700338 const SkPaint& skPaint,
joshualitt9bd2daf2015-04-17 09:30:06 -0700339 const SkMatrix* viewMatrix,
340 bool noGamma) {
robertphillipsfcf78292015-06-19 11:49:52 -0700341 skPaint.getScalerContextDescriptor(&run->fDescriptor, fSurfaceProps, viewMatrix, noGamma);
joshualitt1d89e8d2015-04-01 12:40:54 -0700342 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
343 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc());
344}
345
robertphillips9c240a12015-05-28 07:45:59 -0700346void GrAtlasTextContext::drawTextBlob(GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700347 const GrClip& clip, const SkPaint& skPaint,
348 const SkMatrix& viewMatrix, const SkTextBlob* blob,
349 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700350 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700351 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700352 if (fContext->abandoned()) {
353 return;
354 }
355
joshualitt374b2f72015-07-21 08:05:03 -0700356 SkAutoTUnref<GrAtlasTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700357 SkMaskFilter::BlurRec blurRec;
joshualitt374b2f72015-07-21 08:05:03 -0700358 GrAtlasTextBlob::Key key;
joshualitt53b5f442015-04-13 06:33:59 -0700359 // It might be worth caching these things, but its not clear at this time
360 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
361 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700362 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700363 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700364 drawFilter);
365
366 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700367 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700368
369 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
robertphillipsfcf78292015-06-19 11:49:52 -0700370 SkPixelGeometry pixelGeometry = hasLCD ? fSurfaceProps.pixelGeometry() :
joshualitte4cee1f2015-05-11 13:04:28 -0700371 kUnknown_SkPixelGeometry;
372
joshualitt9e36c1a2015-04-14 12:17:27 -0700373 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
374 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
375 // ensure we always match the same key
376 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
377 ComputeCanonicalColor(skPaint, hasLCD);
378
joshualitte4cee1f2015-05-11 13:04:28 -0700379 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700380 key.fUniqueID = blob->uniqueID();
381 key.fStyle = skPaint.getStyle();
382 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700383 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700384 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700385 }
386
joshualitt1d89e8d2015-04-01 12:40:54 -0700387 SkIRect clipRect;
388 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
389
joshualitt2a0e9f32015-04-13 06:12:21 -0700390 SkScalar transX = 0.f;
391 SkScalar transY = 0.f;
392
joshualitt9e36c1a2015-04-14 12:17:27 -0700393 // Though for the time being runs in the textblob can override the paint, they only touch font
394 // info.
395 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700396 if (!SkPaint2GrPaint(fContext, rt, skPaint, viewMatrix, true, &grPaint)) {
397 return;
398 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700399
joshualittb7133be2015-04-08 09:08:31 -0700400 if (cacheBlob) {
joshualitt53b5f442015-04-13 06:33:59 -0700401 if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, blurRec, viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700402 // We have to remake the blob because changes may invalidate our masks.
403 // TODO we could probably get away reuse most of the time if the pointer is unique,
404 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700405 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700406 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
407 kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700408 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700409 blob, x, y, drawFilter, clipRect, rt, clip, grPaint);
joshualittb7133be2015-04-08 09:08:31 -0700410 } else {
joshualitt9e36c1a2015-04-14 12:17:27 -0700411 // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y
joshualitt7e7b5c52015-07-21 12:56:56 -0700412 // offsets. Note, we offset the vertex bounds right before flushing
joshualitt2a0e9f32015-04-13 06:12:21 -0700413 cacheBlob->fViewMatrix = viewMatrix;
414 cacheBlob->fX = x;
415 cacheBlob->fY = y;
joshualittb7133be2015-04-08 09:08:31 -0700416 fCache->makeMRU(cacheBlob);
joshualitt259fbf12015-07-21 11:39:34 -0700417#ifdef CACHE_SANITY_CHECK
418 {
419 int glyphCount = 0;
420 int runCount = 0;
421 GrTextBlobCache::BlobGlyphCount(&glyphCount, &runCount, blob);
422 SkAutoTUnref<GrAtlasTextBlob> sanityBlob(fCache->createBlob(glyphCount, runCount,
423 kGrayTextVASize));
424 GrTextBlobCache::SetupCacheBlobKey(sanityBlob, key, blurRec, skPaint);
425 this->regenerateTextBlob(sanityBlob, skPaint, grPaint.getColor(), viewMatrix,
426 blob, x, y, drawFilter, clipRect, rt, clip, grPaint);
427 GrAtlasTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
428 }
429
430#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700431 }
432 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700433 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700434 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
435 kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700436 } else {
437 cacheBlob.reset(fCache->createBlob(blob, kGrayTextVASize));
438 }
robertphillips9c240a12015-05-28 07:45:59 -0700439 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700440 blob, x, y, drawFilter, clipRect, rt, clip, grPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -0700441 }
442
robertphillips2334fb62015-06-17 05:43:33 -0700443 this->flush(blob, cacheBlob, rt, skPaint, grPaint, drawFilter,
joshualitt2a0e9f32015-04-13 06:12:21 -0700444 clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700445}
446
joshualitt9bd2daf2015-04-17 09:30:06 -0700447inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
448 const SkMatrix& viewMatrix) {
449 // TODO: support perspective (need getMaxScale replacement)
450 if (viewMatrix.hasPerspective()) {
451 return false;
452 }
453
454 SkScalar maxScale = viewMatrix.getMaxScale();
455 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
456 // Hinted text looks far better at small resolutions
457 // Scaling up beyond 2x yields undesireable artifacts
jvanverth34d72882015-06-22 08:08:09 -0700458 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700459 return false;
460 }
461
robertphillipsfcf78292015-06-19 11:49:52 -0700462 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
robertphillipsbcd7ab52015-06-18 05:27:18 -0700463#if SK_FORCE_DISTANCE_FIELD_TEXT
464 useDFT = true;
465#endif
466
jvanverth4854d132015-06-22 06:46:56 -0700467 if (!useDFT && scaledTextSize < kLargeDFFontSize) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700468 return false;
469 }
470
471 // rasterizers and mask filters modify alpha, which doesn't
472 // translate well to distance
473 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
bsalomon76228632015-05-29 08:02:10 -0700474 !fContext->caps()->shaderCaps()->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700475 return false;
476 }
477
478 // TODO: add some stroking support
479 if (skPaint.getStyle() != SkPaint::kFill_Style) {
480 return false;
481 }
482
483 return true;
484}
485
joshualitt374b2f72015-07-21 08:05:03 -0700486void GrAtlasTextContext::regenerateTextBlob(GrAtlasTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700487 const SkPaint& skPaint, GrColor color,
488 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700489 const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualittfcfb9fc2015-04-21 07:35:10 -0700490 SkDrawFilter* drawFilter, const SkIRect& clipRect,
491 GrRenderTarget* rt, const GrClip& clip,
492 const GrPaint& paint) {
joshualitt259fbf12015-07-21 11:39:34 -0700493 cacheBlob->fPaintColor = skPaint.getColor();
joshualitt1d89e8d2015-04-01 12:40:54 -0700494 cacheBlob->fViewMatrix = viewMatrix;
495 cacheBlob->fX = x;
496 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700497
498 // Regenerate textblob
499 SkPaint runPaint = skPaint;
500 SkTextBlob::RunIterator it(blob);
501 for (int run = 0; !it.done(); it.next(), run++) {
502 int glyphCount = it.glyphCount();
503 size_t textLen = glyphCount * sizeof(uint16_t);
504 const SkPoint& offset = it.offset();
505 // applyFontToPaint() always overwrites the exact same attributes,
506 // so it is safe to not re-seed the paint for this reason.
507 it.applyFontToPaint(&runPaint);
508
509 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
510 // A false return from filter() means we should abort the current draw.
511 runPaint = skPaint;
512 continue;
513 }
514
robertphillipsfcf78292015-06-19 11:49:52 -0700515 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700516
joshualitt1d89e8d2015-04-01 12:40:54 -0700517 // setup vertex / glyphIndex for the new run
518 if (run > 0) {
519 PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back();
520 PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back();
521
522 newRun.fVertexStartIndex = lastRun.fVertexEndIndex;
523 newRun.fVertexEndIndex = lastRun.fVertexEndIndex;
524
525 newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex;
526 newRun.fGlyphEndIndex = lastRun.fGlyphEndIndex;
527 }
528
joshualittfcfb9fc2015-04-21 07:35:10 -0700529 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
530 cacheBlob->setHasDistanceField();
531 SkPaint dfPaint = runPaint;
532 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700533 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700534 Run& runIdx = cacheBlob->fRuns[run];
535 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
536 subRun.fUseLCDText = runPaint.isLCDRenderText();
537 subRun.fDrawAsDistanceFields = true;
joshualitt9a27e632015-04-06 10:53:36 -0700538
joshualittfcfb9fc2015-04-21 07:35:10 -0700539 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], dfPaint, NULL, true);
540
541 SkTDArray<char> fallbackTxt;
542 SkTDArray<SkScalar> fallbackPos;
543 SkPoint dfOffset;
544 int scalarsPerPosition = 2;
545 switch (it.positioning()) {
546 case SkTextBlob::kDefault_Positioning: {
547 this->internalDrawDFText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
548 (const char *)it.glyphs(), textLen,
549 x + offset.x(), y + offset.y(), clipRect, textRatio,
550 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
551 break;
552 }
553 case SkTextBlob::kHorizontal_Positioning: {
554 scalarsPerPosition = 1;
555 dfOffset = SkPoint::Make(x, y + offset.y());
556 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
557 (const char*)it.glyphs(), textLen, it.pos(),
558 scalarsPerPosition, dfOffset, clipRect, textRatio,
559 &fallbackTxt, &fallbackPos);
560 break;
561 }
562 case SkTextBlob::kFull_Positioning: {
563 dfOffset = SkPoint::Make(x, y);
564 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
565 (const char*)it.glyphs(), textLen, it.pos(),
566 scalarsPerPosition, dfOffset, clipRect, textRatio,
567 &fallbackTxt, &fallbackPos);
568 break;
569 }
570 }
571 if (fallbackTxt.count()) {
572 this->fallbackDrawPosText(cacheBlob, run, rt, clip, paint, runPaint, viewMatrix,
573 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset,
574 clipRect);
575 }
576
577 SkGlyphCache::AttachCache(cache);
578 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
579 cacheBlob->fRuns[run].fDrawAsPaths = true;
580 } else {
581 cacheBlob->setHasBitmap();
582 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
583 false);
584 switch (it.positioning()) {
585 case SkTextBlob::kDefault_Positioning:
586 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
587 (const char *)it.glyphs(), textLen,
588 x + offset.x(), y + offset.y(), clipRect);
589 break;
590 case SkTextBlob::kHorizontal_Positioning:
591 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
592 (const char*)it.glyphs(), textLen, it.pos(), 1,
593 SkPoint::Make(x, y + offset.y()), clipRect);
594 break;
595 case SkTextBlob::kFull_Positioning:
596 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
597 (const char*)it.glyphs(), textLen, it.pos(), 2,
598 SkPoint::Make(x, y), clipRect);
599 break;
600 }
601 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700602 }
603
604 if (drawFilter) {
605 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
606 runPaint = skPaint;
607 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700608 }
609}
610
joshualitt374b2f72015-07-21 08:05:03 -0700611inline void GrAtlasTextContext::initDistanceFieldPaint(GrAtlasTextBlob* blob,
joshualitt64c99cc2015-04-21 09:43:03 -0700612 SkPaint* skPaint,
613 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700614 const SkMatrix& viewMatrix) {
615 // getMaxScale doesn't support perspective, so neither do we at the moment
616 SkASSERT(!viewMatrix.hasPerspective());
617 SkScalar maxScale = viewMatrix.getMaxScale();
618 SkScalar textSize = skPaint->getTextSize();
619 SkScalar scaledTextSize = textSize;
620 // if we have non-unity scale, we need to choose our base text size
621 // based on the SkPaint's text size multiplied by the max scale factor
622 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
623 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
624 scaledTextSize *= maxScale;
625 }
626
joshualitt64c99cc2015-04-21 09:43:03 -0700627 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
628 // and ceiling. A scale outside of this range would require regenerating the distance fields
629 SkScalar dfMaskScaleFloor;
630 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700631 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700632 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700633 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700634 *textRatio = textSize / kSmallDFFontSize;
635 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
636 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700637 dfMaskScaleFloor = kSmallDFFontLimit;
638 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700639 *textRatio = textSize / kMediumDFFontSize;
640 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
641 } else {
joshualitta7c63892015-04-21 13:24:37 -0700642 dfMaskScaleFloor = kMediumDFFontLimit;
643 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700644 *textRatio = textSize / kLargeDFFontSize;
645 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
646 }
647
joshualitt64c99cc2015-04-21 09:43:03 -0700648 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
649 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
650 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
651 // tolerate before we'd have to move to a large mip size. When we actually test these values
652 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
653 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
654 // level)
joshualitta7c63892015-04-21 13:24:37 -0700655 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700656 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
657 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
658
joshualitt9bd2daf2015-04-17 09:30:06 -0700659 skPaint->setLCDRenderText(false);
660 skPaint->setAutohinted(false);
661 skPaint->setHinting(SkPaint::kNormal_Hinting);
662 skPaint->setSubpixelText(true);
663}
664
joshualitt374b2f72015-07-21 08:05:03 -0700665inline void GrAtlasTextContext::fallbackDrawPosText(GrAtlasTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700666 int runIndex,
joshualittfec19e12015-04-17 10:32:32 -0700667 GrRenderTarget* rt, const GrClip& clip,
joshualitt9bd2daf2015-04-17 09:30:06 -0700668 const GrPaint& paint,
669 const SkPaint& skPaint,
670 const SkMatrix& viewMatrix,
671 const SkTDArray<char>& fallbackTxt,
672 const SkTDArray<SkScalar>& fallbackPos,
673 int scalarsPerPosition,
674 const SkPoint& offset,
675 const SkIRect& clipRect) {
joshualittfec19e12015-04-17 10:32:32 -0700676 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700677 blob->setHasBitmap();
678 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700679 // Push back a new subrun to fill and set the override descriptor
680 run.push_back();
681 run.fOverrideDescriptor.reset(SkNEW(SkAutoDescriptor));
682 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
robertphillipsfcf78292015-06-19 11:49:52 -0700683 fSurfaceProps, &viewMatrix, false);
joshualittfec19e12015-04-17 10:32:32 -0700684 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700685 run.fOverrideDescriptor->getDesc());
joshualittfcfb9fc2015-04-21 07:35:10 -0700686 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, paint.getColor(), viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700687 fallbackTxt.begin(), fallbackTxt.count(),
688 fallbackPos.begin(), scalarsPerPosition, offset, clipRect);
689 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700690}
691
joshualitt374b2f72015-07-21 08:05:03 -0700692inline GrAtlasTextBlob*
joshualitt9bd2daf2015-04-17 09:30:06 -0700693GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
694 const SkMatrix& viewMatrix, SkGlyphCache** cache,
695 SkPaint* dfPaint, SkScalar* textRatio) {
joshualitt374b2f72015-07-21 08:05:03 -0700696 GrAtlasTextBlob* blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700697
698 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700699 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700700 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700701 Run& run = blob->fRuns[0];
702 PerSubRunInfo& subRun = run.fSubRunInfo.back();
703 subRun.fUseLCDText = origPaint.isLCDRenderText();
704 subRun.fDrawAsDistanceFields = true;
joshualitt9bd2daf2015-04-17 09:30:06 -0700705
706 *cache = this->setupCache(&blob->fRuns[0], *dfPaint, NULL, true);
707 return blob;
708}
709
joshualitt374b2f72015-07-21 08:05:03 -0700710inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700711GrAtlasTextContext::createDrawTextBlob(GrRenderTarget* rt, const GrClip& clip,
712 const GrPaint& paint, const SkPaint& skPaint,
713 const SkMatrix& viewMatrix,
714 const char text[], size_t byteLength,
715 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700716 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700717 SkIRect clipRect;
718 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
719
joshualitt374b2f72015-07-21 08:05:03 -0700720 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700721 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
722 SkPaint dfPaint;
723 SkScalar textRatio;
724 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700725 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700726
joshualitt9bd2daf2015-04-17 09:30:06 -0700727 SkTDArray<char> fallbackTxt;
728 SkTDArray<SkScalar> fallbackPos;
729 SkPoint offset;
730 this->internalDrawDFText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
731 byteLength, x, y, clipRect, textRatio, &fallbackTxt, &fallbackPos,
732 &offset, skPaint);
733 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700734 if (fallbackTxt.count()) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700735 this->fallbackDrawPosText(blob, 0, rt, clip, paint, skPaint, viewMatrix, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700736 fallbackPos, 2, offset, clipRect);
737 }
738 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700739 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700740 blob->fViewMatrix = viewMatrix;
741
742 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
743 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
744 byteLength, x, y, clipRect);
745 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700746 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700747 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700748}
749
joshualitt374b2f72015-07-21 08:05:03 -0700750inline GrAtlasTextBlob*
joshualitt79dfb2b2015-05-11 08:58:08 -0700751GrAtlasTextContext::createDrawPosTextBlob(GrRenderTarget* rt, const GrClip& clip,
752 const GrPaint& paint, const SkPaint& skPaint,
753 const SkMatrix& viewMatrix,
754 const char text[], size_t byteLength,
755 const SkScalar pos[], int scalarsPerPosition,
756 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700757 int glyphCount = skPaint.countText(text, byteLength);
758
759 SkIRect clipRect;
760 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
761
joshualitt374b2f72015-07-21 08:05:03 -0700762 GrAtlasTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700763 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
764 SkPaint dfPaint;
765 SkScalar textRatio;
766 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700767 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700768
769 SkTDArray<char> fallbackTxt;
770 SkTDArray<SkScalar> fallbackPos;
771 this->internalDrawDFPosText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
772 byteLength, pos, scalarsPerPosition, offset, clipRect,
773 textRatio, &fallbackTxt, &fallbackPos);
774 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700775 if (fallbackTxt.count()) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700776 this->fallbackDrawPosText(blob, 0, rt, clip, paint, skPaint, viewMatrix, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700777 fallbackPos, scalarsPerPosition, offset, clipRect);
778 }
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
robertphillips2334fb62015-06-17 05:43:33 -0700790void GrAtlasTextContext::onDrawText(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));
799 this->flush(blob, rt, skPaint, paint, clip, regionClipBounds);
joshualitt79dfb2b2015-05-11 08:58:08 -0700800}
801
robertphillips2334fb62015-06-17 05:43:33 -0700802void GrAtlasTextContext::onDrawPosText(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
robertphillips2334fb62015-06-17 05:43:33 -0700815 this->flush(blob, 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) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700824 SkASSERT(byteLength == 0 || text != NULL);
825
826 // nothing to draw
827 if (text == NULL || byteLength == 0) {
828 return;
829 }
830
831 fCurrStrike = NULL;
832 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) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700913 SkASSERT(byteLength == 0 || text != NULL);
914 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
915
916 // nothing to draw
917 if (text == NULL || byteLength == 0) {
918 return;
919 }
920
921 fCurrStrike = NULL;
922 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) {
1070 SkASSERT(byteLength == 0 || text != NULL);
1071
1072 // nothing to draw
1073 if (text == NULL || byteLength == 0) {
1074 return;
1075 }
1076
1077 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
1078 SkAutoDescriptor desc;
robertphillipsfcf78292015-06-19 11:49:52 -07001079 origPaint.getScalerContextDescriptor(&desc, fSurfaceProps, NULL, 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
1144 SkASSERT(byteLength == 0 || text != NULL);
1145 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1146
1147 // nothing to draw
1148 if (text == NULL || byteLength == 0) {
1149 return;
1150 }
1151
1152 fCurrStrike = NULL;
1153
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);
joshualittae32c102015-04-21 09:37:57 -07001227 run.fStrike.reset(SkRef(fCurrStrike));
joshualitt1d89e8d2015-04-01 12:40:54 -07001228 }
1229
joshualitt6c2c2b02015-07-24 10:37:00 -07001230 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1231 skGlyph.getSubXFixed(),
1232 skGlyph.getSubYFixed(),
1233 GrGlyph::kCoverage_MaskStyle);
1234 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001235 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001236 return;
1237 }
1238
1239 int x = vx + glyph->fBounds.fLeft;
1240 int y = vy + glyph->fBounds.fTop;
1241
1242 // keep them as ints until we've done the clip-test
1243 int width = glyph->fBounds.width();
1244 int height = glyph->fBounds.height();
1245
joshualitt2a0e9f32015-04-13 06:12:21 -07001246#if 0
1247 // Not checking the clip bounds might introduce a performance regression. However, its not
1248 // clear if this is still true today with the larger tiles we use in Chrome. For repositionable
1249 // blobs, we want to make sure we have all of the glyphs, so clipping them out is not ideal.
1250 // We could store the cliprect in the key, but then we'd lose the ability to do integer scrolls
1251 // TODO verify this
joshualitt1d89e8d2015-04-01 12:40:54 -07001252 // check if we clipped out
1253 if (clipRect.quickReject(x, y, x + width, y + height)) {
1254 return;
1255 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001256#endif
joshualitt1d89e8d2015-04-01 12:40:54 -07001257
1258 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001259 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001260 this->appendGlyphPath(blob, glyph, scaler, skGlyph, SkIntToScalar(vx), SkIntToScalar(vy));
joshualitt1d89e8d2015-04-01 12:40:54 -07001261 return;
1262 }
1263
joshualitt1d89e8d2015-04-01 12:40:54 -07001264 GrMaskFormat format = glyph->fMaskFormat;
1265
1266 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
1267 if (run.fInitialized && subRun->fMaskFormat != format) {
joshualittd9f13ae2015-07-24 11:24:31 -07001268 subRun = &run.push_back();
joshualitt1d89e8d2015-04-01 12:40:54 -07001269 }
1270
1271 run.fInitialized = true;
joshualitt1d89e8d2015-04-01 12:40:54 -07001272
1273 size_t vertexStride = get_vertex_stride(format);
1274
1275 SkRect r;
1276 r.fLeft = SkIntToScalar(x);
1277 r.fTop = SkIntToScalar(y);
1278 r.fRight = r.fLeft + SkIntToScalar(width);
1279 r.fBottom = r.fTop + SkIntToScalar(height);
joshualitt9bd2daf2015-04-17 09:30:06 -07001280 subRun->fMaskFormat = format;
1281 this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format,
joshualittae32c102015-04-21 09:37:57 -07001282 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001283}
joshualitt1d89e8d2015-04-01 12:40:54 -07001284
joshualitt374b2f72015-07-21 08:05:03 -07001285bool GrAtlasTextContext::dfAppendGlyph(GrAtlasTextBlob* blob, int runIndex,
joshualitt6c2c2b02015-07-24 10:37:00 -07001286 const SkGlyph& skGlyph,
joshualitt9bd2daf2015-04-17 09:30:06 -07001287 SkScalar sx, SkScalar sy, GrColor color,
1288 GrFontScaler* scaler,
1289 const SkIRect& clipRect,
1290 SkScalar textRatio, const SkMatrix& viewMatrix) {
joshualittae32c102015-04-21 09:37:57 -07001291 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001292 if (!fCurrStrike) {
1293 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
joshualittae32c102015-04-21 09:37:57 -07001294 run.fStrike.reset(SkRef(fCurrStrike));
joshualitt9bd2daf2015-04-17 09:30:06 -07001295 }
1296
joshualitt6c2c2b02015-07-24 10:37:00 -07001297 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
1298 skGlyph.getSubXFixed(),
1299 skGlyph.getSubYFixed(),
1300 GrGlyph::kDistance_MaskStyle);
1301 GrGlyph* glyph = fCurrStrike->getGlyph(skGlyph, id, scaler);
joshualitt010db532015-04-21 10:07:26 -07001302 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001303 return true;
1304 }
1305
1306 // fallback to color glyph support
1307 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
1308 return false;
1309 }
1310
1311 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
1312 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
1313 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
1314 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
1315
1316 SkScalar scale = textRatio;
1317 dx *= scale;
1318 dy *= scale;
1319 width *= scale;
1320 height *= scale;
1321 sx += dx;
1322 sy += dy;
1323 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
1324
1325#if 0
1326 // check if we clipped out
1327 SkRect dstRect;
1328 viewMatrix.mapRect(&dstRect, glyphRect);
1329 if (clipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
1330 SkScalarTruncToInt(dstRect.top()),
1331 SkScalarTruncToInt(dstRect.right()),
1332 SkScalarTruncToInt(dstRect.bottom()))) {
1333 return true;
1334 }
1335#endif
1336
1337 // TODO combine with the above
1338 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001339 if (glyph->fTooLargeForAtlas) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001340 this->appendGlyphPath(blob, glyph, scaler, skGlyph, sx - dx, sy - dy);
joshualitt9bd2daf2015-04-17 09:30:06 -07001341 return true;
1342 }
1343
joshualitt9bd2daf2015-04-17 09:30:06 -07001344 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
1345 SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat);
1346 subRun->fMaskFormat = kA8_GrMaskFormat;
1347
1348 size_t vertexStride = get_vertex_stride_df(kA8_GrMaskFormat, subRun->fUseLCDText);
1349
1350 bool useColorVerts = !subRun->fUseLCDText;
1351 this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts,
joshualittae32c102015-04-21 09:37:57 -07001352 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001353 return true;
1354}
1355
joshualitt374b2f72015-07-21 08:05:03 -07001356inline void GrAtlasTextContext::appendGlyphPath(GrAtlasTextBlob* blob, GrGlyph* glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001357 GrFontScaler* scaler, const SkGlyph& skGlyph,
1358 SkScalar x, SkScalar y) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001359 if (NULL == glyph->fPath) {
joshualitt6c2c2b02015-07-24 10:37:00 -07001360 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
1361 if (!glyphPath) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001362 return;
1363 }
joshualitt6c2c2b02015-07-24 10:37:00 -07001364
1365 glyph->fPath = SkNEW_ARGS(SkPath, (*glyphPath));
joshualitt9bd2daf2015-04-17 09:30:06 -07001366 }
joshualitt374b2f72015-07-21 08:05:03 -07001367 blob->fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y));
joshualitt9bd2daf2015-04-17 09:30:06 -07001368}
1369
joshualitt374b2f72015-07-21 08:05:03 -07001370inline void GrAtlasTextContext::appendGlyphCommon(GrAtlasTextBlob* blob, Run* run,
joshualitt9bd2daf2015-04-17 09:30:06 -07001371 Run::SubRunInfo* subRun,
1372 const SkRect& positions, GrColor color,
1373 size_t vertexStride, bool useVertexColor,
joshualittae32c102015-04-21 09:37:57 -07001374 GrGlyph* glyph) {
1375 blob->fGlyphs[subRun->fGlyphEndIndex] = glyph;
joshualitt9bd2daf2015-04-17 09:30:06 -07001376 run->fVertexBounds.joinNonEmptyArg(positions);
1377 run->fColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -07001378
1379 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->fVertexEndIndex);
1380
joshualitt9bd2daf2015-04-17 09:30:06 -07001381 if (useVertexColor) {
joshualitt010db532015-04-21 10:07:26 -07001382 // V0
1383 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1384 position->set(positions.fLeft, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001385 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
1386 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001387 vertex += vertexStride;
joshualitt9bd2daf2015-04-17 09:30:06 -07001388
joshualitt010db532015-04-21 10:07:26 -07001389 // V1
1390 position = reinterpret_cast<SkPoint*>(vertex);
1391 position->set(positions.fLeft, positions.fBottom);
1392 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001393 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001394 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001395
joshualitt010db532015-04-21 10:07:26 -07001396 // V2
1397 position = reinterpret_cast<SkPoint*>(vertex);
1398 position->set(positions.fRight, positions.fBottom);
1399 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001400 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001401 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001402
joshualitt010db532015-04-21 10:07:26 -07001403 // V3
1404 position = reinterpret_cast<SkPoint*>(vertex);
1405 position->set(positions.fRight, positions.fTop);
1406 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001407 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001408 } else {
1409 // V0
1410 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1411 position->set(positions.fLeft, positions.fTop);
1412 vertex += vertexStride;
1413
1414 // V1
1415 position = reinterpret_cast<SkPoint*>(vertex);
1416 position->set(positions.fLeft, positions.fBottom);
1417 vertex += vertexStride;
1418
1419 // V2
1420 position = reinterpret_cast<SkPoint*>(vertex);
1421 position->set(positions.fRight, positions.fBottom);
1422 vertex += vertexStride;
1423
1424 // V3
1425 position = reinterpret_cast<SkPoint*>(vertex);
1426 position->set(positions.fRight, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001427 }
1428
1429 subRun->fGlyphEndIndex++;
1430 subRun->fVertexEndIndex += vertexStride * kVerticesPerGlyph;
1431}
1432
bsalomon265697d2015-07-22 10:17:26 -07001433class TextBatch : public GrBatch {
joshualitt1d89e8d2015-04-01 12:40:54 -07001434public:
joshualitt9bd2daf2015-04-17 09:30:06 -07001435 typedef GrAtlasTextContext::DistanceAdjustTable DistanceAdjustTable;
joshualitt374b2f72015-07-21 08:05:03 -07001436 typedef GrAtlasTextBlob Blob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001437 typedef Blob::Run Run;
1438 typedef Run::SubRunInfo TextInfo;
1439 struct Geometry {
joshualittad802c62015-04-15 05:31:57 -07001440 Blob* fBlob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001441 int fRun;
1442 int fSubRun;
1443 GrColor fColor;
joshualitt2a0e9f32015-04-13 06:12:21 -07001444 SkScalar fTransX;
1445 SkScalar fTransY;
joshualitt1d89e8d2015-04-01 12:40:54 -07001446 };
1447
bsalomon265697d2015-07-22 10:17:26 -07001448 static TextBatch* CreateBitmap(GrMaskFormat maskFormat, int glyphCount,
joshualittad802c62015-04-15 05:31:57 -07001449 GrBatchFontCache* fontCache) {
bsalomon265697d2015-07-22 10:17:26 -07001450 TextBatch* batch = SkNEW(TextBatch);
1451
1452 batch->initClassID<TextBatch>();
1453 batch->fFontCache = fontCache;
1454 switch (maskFormat) {
1455 case kA8_GrMaskFormat:
1456 batch->fMaskType = kGrayscaleCoverageMask_MaskType;
1457 break;
1458 case kA565_GrMaskFormat:
1459 batch->fMaskType = kLCDCoverageMask_MaskType;
1460 break;
1461 case kARGB_GrMaskFormat:
1462 batch->fMaskType = kColorBitmapMask_MaskType;
1463 break;
1464 }
1465 batch->fBatch.fNumGlyphs = glyphCount;
1466 batch->fInstanceCount = 1;
1467 batch->fAllocatedCount = kMinAllocated;
1468 batch->fFilteredColor = 0;
1469 batch->fFontCache = fontCache;
1470 batch->fUseBGR = false;
1471 return batch;
joshualitt1d89e8d2015-04-01 12:40:54 -07001472 }
1473
bsalomon265697d2015-07-22 10:17:26 -07001474 static TextBatch* CreateDistanceField(int glyphCount, GrBatchFontCache* fontCache,
1475 DistanceAdjustTable* distanceAdjustTable,
1476 SkColor filteredColor, bool isLCD,
1477 bool useBGR) {
1478 TextBatch* batch = SkNEW(TextBatch);
1479 batch->initClassID<TextBatch>();
1480 batch->fFontCache = fontCache;
1481 batch->fMaskType = isLCD ? kLCDDistanceField_MaskType : kGrayscaleDistanceField_MaskType;
1482 batch->fDistanceAdjustTable.reset(SkRef(distanceAdjustTable));
1483 batch->fFilteredColor = filteredColor;
1484 batch->fUseBGR = useBGR;
1485 batch->fBatch.fNumGlyphs = glyphCount;
1486 batch->fInstanceCount = 1;
1487 batch->fAllocatedCount = kMinAllocated;
1488 return batch;
joshualitt9bd2daf2015-04-17 09:30:06 -07001489 }
1490
bsalomon265697d2015-07-22 10:17:26 -07001491 const char* name() const override { return "TextBatch"; }
joshualitt1d89e8d2015-04-01 12:40:54 -07001492
1493 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001494 if (kColorBitmapMask_MaskType == fMaskType) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001495 out->setUnknownFourComponents();
1496 } else {
1497 out->setKnownFourComponents(fBatch.fColor);
1498 }
1499 }
1500
1501 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
bsalomon265697d2015-07-22 10:17:26 -07001502 switch (fMaskType) {
1503 case kGrayscaleDistanceField_MaskType:
1504 case kGrayscaleCoverageMask_MaskType:
joshualitt1d89e8d2015-04-01 12:40:54 -07001505 out->setUnknownSingleComponent();
bsalomon265697d2015-07-22 10:17:26 -07001506 break;
1507 case kLCDCoverageMask_MaskType:
1508 case kLCDDistanceField_MaskType:
1509 out->setUnknownOpaqueFourComponents();
joshualitt1d89e8d2015-04-01 12:40:54 -07001510 out->setUsingLCDCoverage();
bsalomon265697d2015-07-22 10:17:26 -07001511 break;
1512 case kColorBitmapMask_MaskType:
1513 out->setKnownSingleComponent(0xff);
joshualitt1d89e8d2015-04-01 12:40:54 -07001514 }
1515 }
1516
1517 void initBatchTracker(const GrPipelineInfo& init) override {
1518 // Handle any color overrides
bsalomon7765a472015-07-08 11:26:37 -07001519 if (!init.readsColor()) {
joshualitt416e14f2015-07-10 09:05:57 -07001520 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001521 }
joshualitt416e14f2015-07-10 09:05:57 -07001522 init.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt1d89e8d2015-04-01 12:40:54 -07001523
1524 // setup batch properties
bsalomon7765a472015-07-08 11:26:37 -07001525 fBatch.fColorIgnored = !init.readsColor();
joshualitt416e14f2015-07-10 09:05:57 -07001526 fBatch.fColor = fGeoData[0].fColor;
bsalomon7765a472015-07-08 11:26:37 -07001527 fBatch.fUsesLocalCoords = init.readsLocalCoords();
1528 fBatch.fCoverageIgnored = !init.readsCoverage();
joshualitt1d89e8d2015-04-01 12:40:54 -07001529 }
1530
bsalomonb5238a72015-05-05 07:49:49 -07001531 struct FlushInfo {
1532 SkAutoTUnref<const GrVertexBuffer> fVertexBuffer;
1533 SkAutoTUnref<const GrIndexBuffer> fIndexBuffer;
1534 int fGlyphsToFlush;
1535 int fVertexOffset;
1536 };
1537
joshualitt1d89e8d2015-04-01 12:40:54 -07001538 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
1539 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
1540 // TODO actually only invert if we don't have RGBA
1541 SkMatrix localMatrix;
1542 if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) {
1543 SkDebugf("Cannot invert viewmatrix\n");
1544 return;
1545 }
1546
bsalomon265697d2015-07-22 10:17:26 -07001547 GrTexture* texture = fFontCache->getTexture(this->maskFormat());
joshualitt62db8ba2015-04-09 08:22:37 -07001548 if (!texture) {
1549 SkDebugf("Could not allocate backing texture for atlas\n");
1550 return;
1551 }
1552
bsalomon265697d2015-07-22 10:17:26 -07001553 bool usesDistanceFields = this->usesDistanceFields();
1554 GrMaskFormat maskFormat = this->maskFormat();
1555 bool isLCD = this->isLCD();
1556
joshualitt9bd2daf2015-04-17 09:30:06 -07001557 SkAutoTUnref<const GrGeometryProcessor> gp;
bsalomon265697d2015-07-22 10:17:26 -07001558 if (usesDistanceFields) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001559 gp.reset(this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(),
1560 texture));
1561 } else {
1562 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
joshualitt9bd2daf2015-04-17 09:30:06 -07001563 gp.reset(GrBitmapTextGeoProc::Create(this->color(),
1564 texture,
1565 params,
bsalomon265697d2015-07-22 10:17:26 -07001566 maskFormat,
joshualittb8c241a2015-05-19 08:23:30 -07001567 localMatrix,
1568 this->usesLocalCoords()));
joshualitt9bd2daf2015-04-17 09:30:06 -07001569 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001570
bsalomonb5238a72015-05-05 07:49:49 -07001571 FlushInfo flushInfo;
1572 flushInfo.fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001573 size_t vertexStride = gp->getVertexStride();
bsalomon265697d2015-07-22 10:17:26 -07001574 SkASSERT(vertexStride == (usesDistanceFields ?
1575 get_vertex_stride_df(maskFormat, isLCD) :
1576 get_vertex_stride(maskFormat)));
joshualitt1d89e8d2015-04-01 12:40:54 -07001577
joshualittb8c241a2015-05-19 08:23:30 -07001578 batchTarget->initDraw(gp, pipeline);
joshualitt1d89e8d2015-04-01 12:40:54 -07001579
1580 int glyphCount = this->numGlyphs();
joshualittad802c62015-04-15 05:31:57 -07001581 int instanceCount = fInstanceCount;
bsalomon8415abe2015-05-04 11:41:41 -07001582 const GrVertexBuffer* vertexBuffer;
bsalomonb5238a72015-05-05 07:49:49 -07001583
robertphillipse40d3972015-05-07 09:51:43 -07001584 void* vertices = batchTarget->makeVertSpace(vertexStride,
1585 glyphCount * kVerticesPerGlyph,
1586 &vertexBuffer,
1587 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -07001588 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
1589 flushInfo.fIndexBuffer.reset(batchTarget->resourceProvider()->refQuadIndexBuffer());
1590 if (!vertices || !flushInfo.fVertexBuffer) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001591 SkDebugf("Could not allocate vertices\n");
1592 return;
1593 }
1594
1595 unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices);
1596
joshualitt25ba7ea2015-04-21 07:49:49 -07001597 // We cache some values to avoid going to the glyphcache for the same fontScaler twice
1598 // in a row
1599 const SkDescriptor* desc = NULL;
1600 SkGlyphCache* cache = NULL;
1601 GrFontScaler* scaler = NULL;
joshualitt25ba7ea2015-04-21 07:49:49 -07001602 SkTypeface* typeface = NULL;
1603
joshualitt1d89e8d2015-04-01 12:40:54 -07001604 for (int i = 0; i < instanceCount; i++) {
1605 Geometry& args = fGeoData[i];
1606 Blob* blob = args.fBlob;
1607 Run& run = blob->fRuns[args.fRun];
1608 TextInfo& info = run.fSubRunInfo[args.fSubRun];
1609
bsalomon265697d2015-07-22 10:17:26 -07001610 uint64_t currentAtlasGen = fFontCache->atlasGeneration(maskFormat);
joshualitt7a9c45c2015-05-26 12:32:23 -07001611 bool regenerateTextureCoords = info.fAtlasGeneration != currentAtlasGen ||
1612 run.fStrike->isAbandoned();
joshualitt9bd2daf2015-04-17 09:30:06 -07001613 bool regenerateColors;
bsalomon265697d2015-07-22 10:17:26 -07001614 if (usesDistanceFields) {
1615 regenerateColors = !isLCD && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001616 } else {
bsalomon265697d2015-07-22 10:17:26 -07001617 regenerateColors = kA8_GrMaskFormat == maskFormat && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001618 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001619 bool regeneratePositions = args.fTransX != 0.f || args.fTransY != 0.f;
joshualitt1d89e8d2015-04-01 12:40:54 -07001620 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1621
1622 // We regenerate both texture coords and colors in the blob itself, and update the
1623 // atlas generation. If we don't end up purging any unused plots, we can avoid
1624 // regenerating the coords. We could take a finer grained approach to updating texture
1625 // coords but its not clear if the extra bookkeeping would offset any gains.
1626 // To avoid looping over the glyphs twice, we do one loop and conditionally update color
1627 // or coords as needed. One final note, if we have to break a run for an atlas eviction
1628 // then we can't really trust the atlas has all of the correct data. Atlas evictions
1629 // should be pretty rare, so we just always regenerate in those cases
joshualitt2a0e9f32015-04-13 06:12:21 -07001630 if (regenerateTextureCoords || regenerateColors || regeneratePositions) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001631 // first regenerate texture coordinates / colors if need be
joshualitt1d89e8d2015-04-01 12:40:54 -07001632 bool brokenRun = false;
joshualittae32c102015-04-21 09:37:57 -07001633
1634 // Because the GrBatchFontCache may evict the strike a blob depends on using for
1635 // generating its texture coords, we have to track whether or not the strike has
1636 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
1637 // otherwise we have to get the new strike, and use that to get the correct glyphs.
1638 // Because we do not have the packed ids, and thus can't look up our glyphs in the
1639 // new strike, we instead keep our ref to the old strike and use the packed ids from
1640 // it. These ids will still be valid as long as we hold the ref. When we are done
1641 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
1642 bool regenerateGlyphs = false;
1643 GrBatchTextStrike* strike = NULL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001644 if (regenerateTextureCoords) {
joshualittb4c507e2015-04-08 08:07:59 -07001645 info.fBulkUseToken.reset();
joshualitt25ba7ea2015-04-21 07:49:49 -07001646
1647 // We can reuse if we have a valid strike and our descriptors / typeface are the
1648 // same
joshualitt97202d22015-04-22 13:47:02 -07001649 const SkDescriptor* newDesc = run.fOverrideDescriptor ?
1650 run.fOverrideDescriptor->getDesc() :
joshualitt25ba7ea2015-04-21 07:49:49 -07001651 run.fDescriptor.getDesc();
1652 if (!cache || !SkTypeface::Equal(typeface, run.fTypeface) ||
1653 !(desc->equals(*newDesc))) {
1654 if (cache) {
1655 SkGlyphCache::AttachCache(cache);
1656 }
1657 desc = newDesc;
1658 cache = SkGlyphCache::DetachCache(run.fTypeface, desc);
1659 scaler = GrTextContext::GetGrFontScaler(cache);
joshualittae32c102015-04-21 09:37:57 -07001660 strike = run.fStrike;
joshualitt25ba7ea2015-04-21 07:49:49 -07001661 typeface = run.fTypeface;
1662 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001663
joshualittae32c102015-04-21 09:37:57 -07001664 if (run.fStrike->isAbandoned()) {
1665 regenerateGlyphs = true;
1666 strike = fFontCache->getStrike(scaler);
1667 } else {
1668 strike = run.fStrike;
1669 }
1670 }
1671
1672 for (int glyphIdx = 0; glyphIdx < glyphCount; glyphIdx++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001673 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001674 size_t glyphOffset = glyphIdx + info.fGlyphStartIndex;
joshualitt6c2c2b02015-07-24 10:37:00 -07001675
1676 GrGlyph* glyph = blob->fGlyphs[glyphOffset];
1677 GrGlyph::PackedID id = glyph->fPackedID;
1678 const SkGlyph& skGlyph = scaler->grToSkGlyph(id);
joshualittae32c102015-04-21 09:37:57 -07001679 if (regenerateGlyphs) {
1680 // Get the id from the old glyph, and use the new strike to lookup
1681 // the glyph.
joshualitt6c2c2b02015-07-24 10:37:00 -07001682 blob->fGlyphs[glyphOffset] = strike->getGlyph(skGlyph, id, scaler);
joshualittae32c102015-04-21 09:37:57 -07001683 }
1684 glyph = blob->fGlyphs[glyphOffset];
joshualitt1d89e8d2015-04-01 12:40:54 -07001685 SkASSERT(glyph);
joshualitt6c2c2b02015-07-24 10:37:00 -07001686 SkASSERT(id == glyph->fPackedID &&
1687 glyph->fMaskFormat == this->maskFormat());
joshualitt1d89e8d2015-04-01 12:40:54 -07001688
1689 if (!fFontCache->hasGlyph(glyph) &&
joshualitt6c2c2b02015-07-24 10:37:00 -07001690 !strike->addGlyphToAtlas(batchTarget, glyph, scaler, skGlyph)) {
bsalomonb5238a72015-05-05 07:49:49 -07001691 this->flush(batchTarget, &flushInfo);
joshualittb8c241a2015-05-19 08:23:30 -07001692 batchTarget->initDraw(gp, pipeline);
joshualitt1d89e8d2015-04-01 12:40:54 -07001693 brokenRun = glyphIdx > 0;
1694
joshualittae32c102015-04-21 09:37:57 -07001695 SkDEBUGCODE(bool success =) strike->addGlyphToAtlas(batchTarget,
1696 glyph,
joshualitt6c2c2b02015-07-24 10:37:00 -07001697 scaler,
1698 skGlyph);
joshualitt1d89e8d2015-04-01 12:40:54 -07001699 SkASSERT(success);
1700 }
joshualittb4c507e2015-04-08 08:07:59 -07001701 fFontCache->addGlyphToBulkAndSetUseToken(&info.fBulkUseToken, glyph,
1702 batchTarget->currentToken());
joshualitt1d89e8d2015-04-01 12:40:54 -07001703
1704 // Texture coords are the last vertex attribute so we get a pointer to the
1705 // first one and then map with stride in regenerateTextureCoords
1706 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1707 vertex += info.fVertexStartIndex;
1708 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1709 vertex += vertexStride - sizeof(SkIPoint16);
1710
1711 this->regenerateTextureCoords(glyph, vertex, vertexStride);
1712 }
1713
1714 if (regenerateColors) {
1715 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1716 vertex += info.fVertexStartIndex;
1717 vertex += vertexStride * glyphIdx * kVerticesPerGlyph + sizeof(SkPoint);
1718 this->regenerateColors(vertex, vertexStride, args.fColor);
1719 }
1720
joshualitt2a0e9f32015-04-13 06:12:21 -07001721 if (regeneratePositions) {
1722 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1723 vertex += info.fVertexStartIndex;
1724 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1725 SkScalar transX = args.fTransX;
1726 SkScalar transY = args.fTransY;
1727 this->regeneratePositions(vertex, vertexStride, transX, transY);
1728 }
bsalomonb5238a72015-05-05 07:49:49 -07001729 flushInfo.fGlyphsToFlush++;
joshualitt1d89e8d2015-04-01 12:40:54 -07001730 }
1731
joshualitt2a0e9f32015-04-13 06:12:21 -07001732 // We my have changed the color so update it here
1733 run.fColor = args.fColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07001734 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001735 if (regenerateGlyphs) {
1736 run.fStrike.reset(SkRef(strike));
1737 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001738 info.fAtlasGeneration = brokenRun ? GrBatchAtlas::kInvalidAtlasGeneration :
bsalomon265697d2015-07-22 10:17:26 -07001739 fFontCache->atlasGeneration(maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001740 }
1741 } else {
bsalomonb5238a72015-05-05 07:49:49 -07001742 flushInfo.fGlyphsToFlush += glyphCount;
joshualittb4c507e2015-04-08 08:07:59 -07001743
1744 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1745 // have a valid atlas generation
1746 fFontCache->setUseTokenBulk(info.fBulkUseToken,
1747 batchTarget->currentToken(),
bsalomon265697d2015-07-22 10:17:26 -07001748 maskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001749 }
1750
1751 // now copy all vertices
1752 size_t byteCount = info.fVertexEndIndex - info.fVertexStartIndex;
1753 memcpy(currVertex, blob->fVertices + info.fVertexStartIndex, byteCount);
1754
1755 currVertex += byteCount;
1756 }
joshualitt25ba7ea2015-04-21 07:49:49 -07001757 // Make sure to attach the last cache if applicable
1758 if (cache) {
1759 SkGlyphCache::AttachCache(cache);
1760 }
bsalomonb5238a72015-05-05 07:49:49 -07001761 this->flush(batchTarget, &flushInfo);
joshualitt1d89e8d2015-04-01 12:40:54 -07001762 }
1763
joshualittad802c62015-04-15 05:31:57 -07001764 // The minimum number of Geometry we will try to allocate.
bsalomon965b36c2015-07-24 08:10:43 -07001765 static const int kMinAllocated = 4;
joshualittad802c62015-04-15 05:31:57 -07001766
1767 // Total number of Geometry this Batch owns
1768 int instanceCount() const { return fInstanceCount; }
1769 SkAutoSTMalloc<kMinAllocated, Geometry>* geoData() { return &fGeoData; }
1770
1771 // to avoid even the initial copy of the struct, we have a getter for the first item which
1772 // is used to seed the batch with its initial geometry. After seeding, the client should call
1773 // init() so the Batch can initialize itself
1774 Geometry& geometry() { return fGeoData[0]; }
1775 void init() {
joshualitt444987f2015-05-06 06:46:01 -07001776 const Geometry& geo = fGeoData[0];
1777 fBatch.fColor = geo.fColor;
1778 fBatch.fViewMatrix = geo.fBlob->fViewMatrix;
1779
1780 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
1781 // into device space
1782 const Run& run = geo.fBlob->fRuns[geo.fRun];
1783 if (run.fSubRunInfo[geo.fSubRun].fDrawAsDistanceFields) {
1784 SkRect bounds = run.fVertexBounds;
1785 fBatch.fViewMatrix.mapRect(&bounds);
1786 this->setBounds(bounds);
1787 } else {
1788 this->setBounds(run.fVertexBounds);
1789 }
joshualittad802c62015-04-15 05:31:57 -07001790 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001791
1792private:
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() {
joshualittad802c62015-04-15 05:31:57 -07001796 for (int i = 0; i < fInstanceCount; i++) {
1797 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
bsalomonb5238a72015-05-05 07:49:49 -07001881 void flush(GrBatchTarget* batchTarget, 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);
bsalomone64eb572015-05-07 11:35:55 -07001888 batchTarget->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
1898 bool onCombineIfPossible(GrBatch* t) override {
joshualitt8cab9a72015-07-16 09:13:50 -07001899 if (!this->pipeline()->isEqual(*t->pipeline())) {
1900 return false;
1901 }
1902
bsalomon265697d2015-07-22 10:17:26 -07001903 TextBatch* that = t->cast<TextBatch>();
joshualitt1d89e8d2015-04-01 12:40:54 -07001904
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()) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001933 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001934 }
1935
1936 fBatch.fNumGlyphs += that->numGlyphs();
joshualittad802c62015-04-15 05:31:57 -07001937
1938 // copy that->geoData(). We do this manually for performance reasons
1939 SkAutoSTMalloc<kMinAllocated, Geometry>* otherGeoData = that->geoData();
1940 int otherInstanceCount = that->instanceCount();
1941 int allocSize = otherInstanceCount + fInstanceCount;
1942 if (allocSize > fAllocatedCount) {
1943 while (allocSize > fAllocatedCount) {
1944 fAllocatedCount = fAllocatedCount << 1;
1945 }
1946 fGeoData.realloc(fAllocatedCount);
1947 }
1948
1949 memcpy(&fGeoData[fInstanceCount], otherGeoData->get(),
1950 otherInstanceCount * sizeof(Geometry));
1951 int total = fInstanceCount + otherInstanceCount;
1952 for (int i = fInstanceCount; i < total; i++) {
1953 fGeoData[i].fBlob->ref();
1954 }
1955 fInstanceCount = total;
joshualitt99c7c072015-05-01 13:43:30 -07001956
1957 this->joinBounds(that->bounds());
joshualitt1d89e8d2015-04-01 12:40:54 -07001958 return true;
1959 }
1960
joshualitt9bd2daf2015-04-17 09:30:06 -07001961 // TODO just use class params
1962 // TODO trying to figure out why lcd is so whack
1963 GrGeometryProcessor* setupDfProcessor(const SkMatrix& viewMatrix, SkColor filteredColor,
1964 GrColor color, GrTexture* texture) {
1965 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
bsalomon265697d2015-07-22 10:17:26 -07001966 bool isLCD = this->isLCD();
joshualitt9bd2daf2015-04-17 09:30:06 -07001967 // set up any flags
bsalomon265697d2015-07-22 10:17:26 -07001968 uint32_t flags = viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
joshualitt9bd2daf2015-04-17 09:30:06 -07001969
1970 // see if we need to create a new effect
bsalomon265697d2015-07-22 10:17:26 -07001971 if (isLCD) {
1972 flags |= kUseLCD_DistanceFieldEffectFlag;
1973 flags |= viewMatrix.rectStaysRect() ? kRectToRect_DistanceFieldEffectFlag : 0;
1974 flags |= fUseBGR ? kBGR_DistanceFieldEffectFlag : 0;
1975
joshualitt9bd2daf2015-04-17 09:30:06 -07001976 GrColor colorNoPreMul = skcolor_to_grcolor_nopremultiply(filteredColor);
1977
1978 float redCorrection =
1979 (*fDistanceAdjustTable)[GrColorUnpackR(colorNoPreMul) >> kDistanceAdjustLumShift];
1980 float greenCorrection =
1981 (*fDistanceAdjustTable)[GrColorUnpackG(colorNoPreMul) >> kDistanceAdjustLumShift];
1982 float blueCorrection =
1983 (*fDistanceAdjustTable)[GrColorUnpackB(colorNoPreMul) >> kDistanceAdjustLumShift];
1984 GrDistanceFieldLCDTextGeoProc::DistanceAdjust widthAdjust =
1985 GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(redCorrection,
1986 greenCorrection,
1987 blueCorrection);
1988
1989 return GrDistanceFieldLCDTextGeoProc::Create(color,
1990 viewMatrix,
1991 texture,
1992 params,
1993 widthAdjust,
joshualittb8c241a2015-05-19 08:23:30 -07001994 flags,
1995 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07001996 } else {
1997 flags |= kColorAttr_DistanceFieldEffectFlag;
joshualitt9bd2daf2015-04-17 09:30:06 -07001998#ifdef SK_GAMMA_APPLY_TO_A8
robertphillips9fc82752015-06-19 04:46:45 -07001999 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT, filteredColor);
joshualitt9bd2daf2015-04-17 09:30:06 -07002000 float correction = (*fDistanceAdjustTable)[lum >> kDistanceAdjustLumShift];
2001 return GrDistanceFieldA8TextGeoProc::Create(color,
2002 viewMatrix,
2003 texture,
2004 params,
2005 correction,
joshualittb8c241a2015-05-19 08:23:30 -07002006 flags,
2007 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07002008#else
2009 return GrDistanceFieldA8TextGeoProc::Create(color,
2010 viewMatrix,
2011 texture,
2012 params,
joshualittb8c241a2015-05-19 08:23:30 -07002013 flags,
2014 this->usesLocalCoords());
joshualitt9bd2daf2015-04-17 09:30:06 -07002015#endif
2016 }
2017
2018 }
2019
joshualitt1d89e8d2015-04-01 12:40:54 -07002020 struct BatchTracker {
2021 GrColor fColor;
2022 SkMatrix fViewMatrix;
2023 bool fUsesLocalCoords;
2024 bool fColorIgnored;
2025 bool fCoverageIgnored;
2026 int fNumGlyphs;
2027 };
2028
2029 BatchTracker fBatch;
joshualittad802c62015-04-15 05:31:57 -07002030 SkAutoSTMalloc<kMinAllocated, Geometry> fGeoData;
2031 int fInstanceCount;
2032 int fAllocatedCount;
bsalomon265697d2015-07-22 10:17:26 -07002033
2034 enum MaskType {
2035 kGrayscaleCoverageMask_MaskType,
2036 kLCDCoverageMask_MaskType,
2037 kColorBitmapMask_MaskType,
2038 kGrayscaleDistanceField_MaskType,
2039 kLCDDistanceField_MaskType,
2040 } fMaskType;
2041 bool fUseBGR; // fold this into the enum?
2042
joshualitt1d89e8d2015-04-01 12:40:54 -07002043 GrBatchFontCache* fFontCache;
joshualitt9bd2daf2015-04-17 09:30:06 -07002044
2045 // Distance field properties
robertphillips9fc82752015-06-19 04:46:45 -07002046 SkAutoTUnref<const DistanceAdjustTable> fDistanceAdjustTable;
joshualitt9bd2daf2015-04-17 09:30:06 -07002047 SkColor fFilteredColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07002048};
2049
robertphillips2334fb62015-06-17 05:43:33 -07002050void GrAtlasTextContext::flushRunAsPaths(GrRenderTarget* rt, const SkTextBlob::RunIterator& it,
robertphillipsccb1b572015-05-27 11:02:55 -07002051 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07002052 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
2053 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
2054 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07002055
joshualitt9a27e632015-04-06 10:53:36 -07002056 size_t textLen = it.glyphCount() * sizeof(uint16_t);
2057 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07002058
joshualitt9a27e632015-04-06 10:53:36 -07002059 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07002060
joshualitt9a27e632015-04-06 10:53:36 -07002061 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
2062 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07002063 }
2064
robertphillipsfcf78292015-06-19 11:49:52 -07002065 runPaint.setFlags(FilterTextFlags(fSurfaceProps, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07002066
2067 switch (it.positioning()) {
2068 case SkTextBlob::kDefault_Positioning:
robertphillips2334fb62015-06-17 05:43:33 -07002069 this->drawTextAsPath(rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002070 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002071 textLen, x + offset.x(), y + offset.y(), clipBounds);
2072 break;
2073 case SkTextBlob::kHorizontal_Positioning:
robertphillips2334fb62015-06-17 05:43:33 -07002074 this->drawPosTextAsPath(rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002075 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002076 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
2077 clipBounds);
2078 break;
2079 case SkTextBlob::kFull_Positioning:
robertphillips2334fb62015-06-17 05:43:33 -07002080 this->drawPosTextAsPath(rt, clip, runPaint, viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07002081 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002082 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
2083 break;
2084 }
2085}
2086
bsalomon265697d2015-07-22 10:17:26 -07002087inline GrBatch*
joshualitt374b2f72015-07-21 08:05:03 -07002088GrAtlasTextContext::createBatch(GrAtlasTextBlob* cacheBlob, const PerSubRunInfo& info,
joshualitt79dfb2b2015-05-11 08:58:08 -07002089 int glyphCount, int run, int subRun,
2090 GrColor color, SkScalar transX, SkScalar transY,
2091 const SkPaint& skPaint) {
2092 GrMaskFormat format = info.fMaskFormat;
2093 GrColor subRunColor;
2094 if (kARGB_GrMaskFormat == format) {
2095 uint8_t paintAlpha = skPaint.getAlpha();
2096 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
2097 } else {
2098 subRunColor = color;
2099 }
2100
bsalomon265697d2015-07-22 10:17:26 -07002101 TextBatch* batch;
joshualitt79dfb2b2015-05-11 08:58:08 -07002102 if (info.fDrawAsDistanceFields) {
2103 SkColor filteredColor;
2104 SkColorFilter* colorFilter = skPaint.getColorFilter();
2105 if (colorFilter) {
2106 filteredColor = colorFilter->filterColor(skPaint.getColor());
2107 } else {
2108 filteredColor = skPaint.getColor();
2109 }
robertphillipsfcf78292015-06-19 11:49:52 -07002110 bool useBGR = SkPixelGeometryIsBGR(fSurfaceProps.pixelGeometry());
bsalomon265697d2015-07-22 10:17:26 -07002111 batch = TextBatch::CreateDistanceField(glyphCount, fContext->getBatchFontCache(),
2112 fDistanceAdjustTable, filteredColor,
2113 info.fUseLCDText, useBGR);
joshualitt79dfb2b2015-05-11 08:58:08 -07002114 } else {
bsalomon265697d2015-07-22 10:17:26 -07002115 batch = TextBatch::CreateBitmap(format, glyphCount, fContext->getBatchFontCache());
joshualitt79dfb2b2015-05-11 08:58:08 -07002116 }
bsalomon265697d2015-07-22 10:17:26 -07002117 TextBatch::Geometry& geometry = batch->geometry();
joshualitt79dfb2b2015-05-11 08:58:08 -07002118 geometry.fBlob = SkRef(cacheBlob);
2119 geometry.fRun = run;
2120 geometry.fSubRun = subRun;
2121 geometry.fColor = subRunColor;
2122 geometry.fTransX = transX;
2123 geometry.fTransY = transY;
2124 batch->init();
2125
2126 return batch;
2127}
2128
robertphillips2334fb62015-06-17 05:43:33 -07002129inline void GrAtlasTextContext::flushRun(GrPipelineBuilder* pipelineBuilder,
joshualitt374b2f72015-07-21 08:05:03 -07002130 GrAtlasTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07002131 SkScalar transX, SkScalar transY,
2132 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07002133 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
2134 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
2135 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
2136 if (0 == glyphCount) {
2137 continue;
2138 }
2139
bsalomon265697d2015-07-22 10:17:26 -07002140 SkAutoTUnref<GrBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
2141 subRun, color, transX, transY,
2142 skPaint));
robertphillips2334fb62015-06-17 05:43:33 -07002143 fDrawContext->drawBatch(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07002144 }
2145}
2146
joshualitt374b2f72015-07-21 08:05:03 -07002147inline void GrAtlasTextContext::flushBigGlyphs(GrAtlasTextBlob* cacheBlob, GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -07002148 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07002149 SkScalar transX, SkScalar transY,
2150 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07002151 if (!cacheBlob->fBigGlyphs.count()) {
2152 return;
2153 }
2154
2155 SkMatrix pathMatrix;
2156 if (!cacheBlob->fViewMatrix.invert(&pathMatrix)) {
2157 SkDebugf("could not invert viewmatrix\n");
2158 return;
2159 }
2160
joshualitt9a27e632015-04-06 10:53:36 -07002161 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt374b2f72015-07-21 08:05:03 -07002162 GrAtlasTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07002163 bigGlyph.fVx += transX;
2164 bigGlyph.fVy += transY;
joshualittfc072562015-05-13 12:15:06 -07002165 SkMatrix translate = cacheBlob->fViewMatrix;
2166 translate.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
2167
robertphillips2334fb62015-06-17 05:43:33 -07002168 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, rt, clip, bigGlyph.fPath,
robertphillipsccb1b572015-05-27 11:02:55 -07002169 skPaint, translate, &pathMatrix, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07002170 }
2171}
joshualitt9a27e632015-04-06 10:53:36 -07002172
robertphillips2334fb62015-06-17 05:43:33 -07002173void GrAtlasTextContext::flush(const SkTextBlob* blob,
joshualitt374b2f72015-07-21 08:05:03 -07002174 GrAtlasTextBlob* cacheBlob,
joshualitt9a27e632015-04-06 10:53:36 -07002175 GrRenderTarget* rt,
2176 const SkPaint& skPaint,
2177 const GrPaint& grPaint,
2178 SkDrawFilter* drawFilter,
2179 const GrClip& clip,
2180 const SkMatrix& viewMatrix,
2181 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07002182 SkScalar x, SkScalar y,
2183 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07002184 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
2185 // it as paths
joshualitt7b670db2015-07-09 13:25:02 -07002186 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002187
2188 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002189
2190 SkTextBlob::RunIterator it(blob);
2191 for (int run = 0; !it.done(); it.next(), run++) {
2192 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillips2334fb62015-06-17 05:43:33 -07002193 this->flushRunAsPaths(rt, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07002194 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07002195 continue;
2196 }
joshualitt2a0e9f32015-04-13 06:12:21 -07002197 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillips2334fb62015-06-17 05:43:33 -07002198 this->flushRun(&pipelineBuilder, cacheBlob, run, color,
robertphillipsea461502015-05-26 11:38:03 -07002199 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002200 }
2201
2202 // Now flush big glyphs
robertphillips2334fb62015-06-17 05:43:33 -07002203 this->flushBigGlyphs(cacheBlob, rt, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002204}
2205
joshualitt374b2f72015-07-21 08:05:03 -07002206void GrAtlasTextContext::flush(GrAtlasTextBlob* cacheBlob,
joshualitt9a27e632015-04-06 10:53:36 -07002207 GrRenderTarget* rt,
2208 const SkPaint& skPaint,
2209 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07002210 const GrClip& clip,
2211 const SkIRect& clipBounds) {
joshualitt7b670db2015-07-09 13:25:02 -07002212 GrPipelineBuilder pipelineBuilder(grPaint, rt, clip);
joshualitt9a27e632015-04-06 10:53:36 -07002213
2214 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002215 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillips2334fb62015-06-17 05:43:33 -07002216 this->flushRun(&pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002217 }
2218
2219 // Now flush big glyphs
robertphillips2334fb62015-06-17 05:43:33 -07002220 this->flushBigGlyphs(cacheBlob, rt, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002221}
joshualitt79dfb2b2015-05-11 08:58:08 -07002222
2223///////////////////////////////////////////////////////////////////////////////////////////////////
2224
2225#ifdef GR_TEST_UTILS
2226
joshualitt6c891102015-05-13 08:51:49 -07002227BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07002228 static uint32_t gContextID = SK_InvalidGenID;
2229 static GrAtlasTextContext* gTextContext = NULL;
robertphillipsfcf78292015-06-19 11:49:52 -07002230 static SkSurfaceProps gSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
joshualitt79dfb2b2015-05-11 08:58:08 -07002231
2232 if (context->uniqueID() != gContextID) {
2233 gContextID = context->uniqueID();
2234 SkDELETE(gTextContext);
robertphillips2334fb62015-06-17 05:43:33 -07002235
joshualitt79dfb2b2015-05-11 08:58:08 -07002236 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
2237 // because we don't really want to have a gpu device here.
2238 // We enable distance fields by twiddling a knob on the paint
robertphillipsfcf78292015-06-19 11:49:52 -07002239 GrDrawContext* drawContext = context->drawContext(&gSurfaceProps);
robertphillips2334fb62015-06-17 05:43:33 -07002240
robertphillipsfcf78292015-06-19 11:49:52 -07002241 gTextContext = GrAtlasTextContext::Create(context, drawContext, gSurfaceProps);
joshualitt79dfb2b2015-05-11 08:58:08 -07002242 }
2243
2244 // create dummy render target
2245 GrSurfaceDesc desc;
2246 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2247 desc.fWidth = 1024;
2248 desc.fHeight = 1024;
2249 desc.fConfig = kRGBA_8888_GrPixelConfig;
joshualitt15732062015-05-13 12:15:14 -07002250 desc.fSampleCnt = 0;
joshualitt79dfb2b2015-05-11 08:58:08 -07002251 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, true, NULL, 0));
2252 SkASSERT(texture);
2253 SkASSERT(NULL != texture->asRenderTarget());
2254 GrRenderTarget* rt = texture->asRenderTarget();
2255
2256 // Setup dummy SkPaint / GrPaint
2257 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07002258 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07002259 SkPaint skPaint;
joshualitt79dfb2b2015-05-11 08:58:08 -07002260 skPaint.setColor(color);
2261 skPaint.setLCDRenderText(random->nextBool());
2262 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
2263 skPaint.setSubpixelText(random->nextBool());
2264
2265 GrPaint grPaint;
2266 if (!SkPaint2GrPaint(context, rt, skPaint, viewMatrix, true, &grPaint)) {
2267 SkFAIL("couldn't convert paint\n");
2268 }
2269
2270 const char* text = "The quick brown fox jumps over the lazy dog.";
2271 int textLen = (int)strlen(text);
2272
2273 // Setup clip
2274 GrClip clip;
2275 SkIRect noClip = SkIRect::MakeLargest();
2276
2277 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
2278 // intend to test the batch with this unit test, that is okay.
joshualitt374b2f72015-07-21 08:05:03 -07002279 SkAutoTUnref<GrAtlasTextBlob> blob(
joshualitt79dfb2b2015-05-11 08:58:08 -07002280 gTextContext->createDrawTextBlob(rt, clip, grPaint, skPaint, viewMatrix, text,
2281 static_cast<size_t>(textLen), 0, 0, noClip));
2282
2283 SkScalar transX = static_cast<SkScalar>(random->nextU());
2284 SkScalar transY = static_cast<SkScalar>(random->nextU());
joshualitt374b2f72015-07-21 08:05:03 -07002285 const GrAtlasTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
joshualitt79dfb2b2015-05-11 08:58:08 -07002286 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
2287}
2288
2289#endif