blob: 1d2cc4c68b34bdbaf1d4ef1725793ad0fcedee29 [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"
joshualitt1d89e8d2015-04-01 12:40:54 -070016#include "GrFontScaler.h"
17#include "GrIndexBuffer.h"
bsalomoned0bcad2015-05-04 10:36:42 -070018#include "GrResourceProvider.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070019#include "GrStrokeInfo.h"
joshualittb7133be2015-04-08 09:08:31 -070020#include "GrTextBlobCache.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070021#include "GrTexturePriv.h"
bsalomon72e3ae42015-04-28 08:08:46 -070022#include "GrVertexBuffer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070023
24#include "SkAutoKern.h"
25#include "SkColorPriv.h"
joshualitt9bd2daf2015-04-17 09:30:06 -070026#include "SkColorFilter.h"
27#include "SkDistanceFieldGen.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070028#include "SkDraw.h"
29#include "SkDrawFilter.h"
30#include "SkDrawProcs.h"
31#include "SkGlyphCache.h"
32#include "SkGpuDevice.h"
33#include "SkGr.h"
34#include "SkPath.h"
35#include "SkRTConf.h"
36#include "SkStrokeRec.h"
37#include "SkTextBlob.h"
38#include "SkTextMapStateProc.h"
39
40#include "effects/GrBitmapTextGeoProc.h"
joshualitt9bd2daf2015-04-17 09:30:06 -070041#include "effects/GrDistanceFieldGeoProc.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070042
43namespace {
44static const size_t kLCDTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
45
46// position + local coord
47static const size_t kColorTextVASize = sizeof(SkPoint) + sizeof(SkIPoint16);
48
49static const size_t kGrayTextVASize = sizeof(SkPoint) + sizeof(GrColor) + sizeof(SkIPoint16);
50
joshualitt9bd2daf2015-04-17 09:30:06 -070051static const int kMinDFFontSize = 18;
52static const int kSmallDFFontSize = 32;
53static const int kSmallDFFontLimit = 32;
54static const int kMediumDFFontSize = 72;
55static const int kMediumDFFontLimit = 72;
56static const int kLargeDFFontSize = 162;
joshualitta7c63892015-04-21 13:24:37 -070057static const int kLargeDFFontLimit = 2 * kLargeDFFontSize;
joshualitt9bd2daf2015-04-17 09:30:06 -070058
59SkDEBUGCODE(static const int kExpectedDistanceAdjustTableSize = 8;)
60static const int kDistanceAdjustLumShift = 5;
61
joshualitt1d89e8d2015-04-01 12:40:54 -070062static const int kVerticesPerGlyph = 4;
63static const int kIndicesPerGlyph = 6;
64
65static size_t get_vertex_stride(GrMaskFormat maskFormat) {
66 switch (maskFormat) {
67 case kA8_GrMaskFormat:
68 return kGrayTextVASize;
69 case kARGB_GrMaskFormat:
70 return kColorTextVASize;
71 default:
72 return kLCDTextVASize;
73 }
74}
75
joshualitt9bd2daf2015-04-17 09:30:06 -070076static size_t get_vertex_stride_df(GrMaskFormat maskFormat, bool useLCDText) {
77 SkASSERT(maskFormat == kA8_GrMaskFormat);
78 if (useLCDText) {
79 return kLCDTextVASize;
80 } else {
81 return kGrayTextVASize;
82 }
83}
84
85static inline GrColor skcolor_to_grcolor_nopremultiply(SkColor c) {
86 unsigned r = SkColorGetR(c);
87 unsigned g = SkColorGetG(c);
88 unsigned b = SkColorGetB(c);
89 return GrColorPackRGBA(r, g, b, 0xff);
90}
91
joshualitt1d89e8d2015-04-01 12:40:54 -070092};
93
94// TODO
joshualitt9bd2daf2015-04-17 09:30:06 -070095// Distance field text in textblobs
joshualitt1d89e8d2015-04-01 12:40:54 -070096
joshualittdbd35932015-04-02 09:19:04 -070097GrAtlasTextContext::GrAtlasTextContext(GrContext* context,
joshualitt9bd2daf2015-04-17 09:30:06 -070098 const SkDeviceProperties& properties,
99 bool enableDistanceFields)
robertphillipsccb1b572015-05-27 11:02:55 -0700100 : INHERITED(context, properties)
joshualitt9bd2daf2015-04-17 09:30:06 -0700101 , fDistanceAdjustTable(SkNEW_ARGS(DistanceAdjustTable, (properties.gamma()))) {
joshualittb7133be2015-04-08 09:08:31 -0700102 // We overallocate vertices in our textblobs based on the assumption that A8 has the greatest
103 // vertexStride
104 SK_COMPILE_ASSERT(kGrayTextVASize >= kColorTextVASize && kGrayTextVASize >= kLCDTextVASize,
105 vertex_attribute_changed);
joshualitt1d89e8d2015-04-01 12:40:54 -0700106 fCurrStrike = NULL;
joshualittb7133be2015-04-08 09:08:31 -0700107 fCache = context->getTextBlobCache();
joshualitt9bd2daf2015-04-17 09:30:06 -0700108
109#if SK_FORCE_DISTANCE_FIELD_TEXT
110 fEnableDFRendering = true;
111#else
112 fEnableDFRendering = enableDistanceFields;
113#endif
114}
115
116void GrAtlasTextContext::DistanceAdjustTable::buildDistanceAdjustTable(float gamma) {
117
118 // This is used for an approximation of the mask gamma hack, used by raster and bitmap
119 // text. The mask gamma hack is based off of guessing what the blend color is going to
120 // be, and adjusting the mask so that when run through the linear blend will
121 // produce the value closest to the desired result. However, in practice this means
122 // that the 'adjusted' mask is just increasing or decreasing the coverage of
123 // the mask depending on what it is thought it will blit against. For black (on
124 // assumed white) this means that coverages are decreased (on a curve). For white (on
125 // assumed black) this means that coverages are increased (on a a curve). At
126 // middle (perceptual) gray (which could be blit against anything) the coverages
127 // remain the same.
128 //
129 // The idea here is that instead of determining the initial (real) coverage and
130 // then adjusting that coverage, we determine an adjusted coverage directly by
131 // essentially manipulating the geometry (in this case, the distance to the glyph
132 // edge). So for black (on assumed white) this thins a bit; for white (on
133 // assumed black) this fake bolds the geometry a bit.
134 //
135 // The distance adjustment is calculated by determining the actual coverage value which
136 // when fed into in the mask gamma table gives us an 'adjusted coverage' value of 0.5. This
137 // actual coverage value (assuming it's between 0 and 1) corresponds to a distance from the
138 // actual edge. So by subtracting this distance adjustment and computing without the
139 // the coverage adjustment we should get 0.5 coverage at the same point.
140 //
141 // This has several implications:
142 // For non-gray lcd smoothed text, each subpixel essentially is using a
143 // slightly different geometry.
144 //
145 // For black (on assumed white) this may not cover some pixels which were
146 // previously covered; however those pixels would have been only slightly
147 // covered and that slight coverage would have been decreased anyway. Also, some pixels
148 // which were previously fully covered may no longer be fully covered.
149 //
150 // For white (on assumed black) this may cover some pixels which weren't
151 // previously covered at all.
152
153 int width, height;
154 size_t size;
155
156#ifdef SK_GAMMA_CONTRAST
157 SkScalar contrast = SK_GAMMA_CONTRAST;
158#else
159 SkScalar contrast = 0.5f;
160#endif
161 SkScalar paintGamma = gamma;
162 SkScalar deviceGamma = gamma;
163
164 size = SkScalerContext::GetGammaLUTSize(contrast, paintGamma, deviceGamma,
165 &width, &height);
166
167 SkASSERT(kExpectedDistanceAdjustTableSize == height);
168 fTable = SkNEW_ARRAY(SkScalar, height);
169
170 SkAutoTArray<uint8_t> data((int)size);
171 SkScalerContext::GetGammaLUTData(contrast, paintGamma, deviceGamma, data.get());
172
173 // find the inverse points where we cross 0.5
174 // binsearch might be better, but we only need to do this once on creation
175 for (int row = 0; row < height; ++row) {
176 uint8_t* rowPtr = data.get() + row*width;
177 for (int col = 0; col < width - 1; ++col) {
178 if (rowPtr[col] <= 127 && rowPtr[col + 1] >= 128) {
179 // compute point where a mask value will give us a result of 0.5
180 float interp = (127.5f - rowPtr[col]) / (rowPtr[col + 1] - rowPtr[col]);
181 float borderAlpha = (col + interp) / 255.f;
182
183 // compute t value for that alpha
184 // this is an approximate inverse for smoothstep()
185 float t = borderAlpha*(borderAlpha*(4.0f*borderAlpha - 6.0f) + 5.0f) / 3.0f;
186
187 // compute distance which gives us that t value
188 const float kDistanceFieldAAFactor = 0.65f; // should match SK_DistanceFieldAAFactor
189 float d = 2.0f*kDistanceFieldAAFactor*t - kDistanceFieldAAFactor;
190
191 fTable[row] = d;
192 break;
193 }
194 }
195 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700196}
197
joshualittdbd35932015-04-02 09:19:04 -0700198GrAtlasTextContext* GrAtlasTextContext::Create(GrContext* context,
joshualitt9bd2daf2015-04-17 09:30:06 -0700199 const SkDeviceProperties& props,
200 bool enableDistanceFields) {
robertphillipsccb1b572015-05-27 11:02:55 -0700201 return SkNEW_ARGS(GrAtlasTextContext, (context, props, enableDistanceFields));
joshualitt1d89e8d2015-04-01 12:40:54 -0700202}
203
joshualittdbd35932015-04-02 09:19:04 -0700204bool GrAtlasTextContext::canDraw(const GrRenderTarget*,
205 const GrClip&,
206 const GrPaint&,
207 const SkPaint& skPaint,
208 const SkMatrix& viewMatrix) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700209 return this->canDrawAsDistanceFields(skPaint, viewMatrix) ||
210 !SkDraw::ShouldDrawTextAsPaths(skPaint, viewMatrix);
joshualitt1d89e8d2015-04-01 12:40:54 -0700211}
212
joshualitt9e36c1a2015-04-14 12:17:27 -0700213GrColor GrAtlasTextContext::ComputeCanonicalColor(const SkPaint& paint, bool lcd) {
214 GrColor canonicalColor = paint.computeLuminanceColor();
215 if (lcd) {
216 // This is the correct computation, but there are tons of cases where LCD can be overridden.
217 // For now we just regenerate if any run in a textblob has LCD.
218 // TODO figure out where all of these overrides are and see if we can incorporate that logic
219 // at a higher level *OR* use sRGB
220 SkASSERT(false);
221 //canonicalColor = SkMaskGamma::CanonicalColor(canonicalColor);
222 } else {
223 // A8, though can have mixed BMP text but it shouldn't matter because BMP text won't have
224 // gamma corrected masks anyways, nor color
225 U8CPU lum = SkComputeLuminance(SkColorGetR(canonicalColor),
226 SkColorGetG(canonicalColor),
227 SkColorGetB(canonicalColor));
228 // reduce to our finite number of bits
229 canonicalColor = SkMaskGamma::CanonicalColor(SkColorSetRGB(lum, lum, lum));
230 }
231 return canonicalColor;
232}
233
234// TODO if this function ever shows up in profiling, then we can compute this value when the
235// textblob is being built and cache it. However, for the time being textblobs mostly only have 1
236// run so this is not a big deal to compute here.
237bool GrAtlasTextContext::HasLCD(const SkTextBlob* blob) {
238 SkTextBlob::RunIterator it(blob);
239 for (; !it.done(); it.next()) {
240 if (it.isLCD()) {
241 return true;
242 }
243 }
244 return false;
245}
246
joshualitt2a0e9f32015-04-13 06:12:21 -0700247bool GrAtlasTextContext::MustRegenerateBlob(SkScalar* outTransX, SkScalar* outTransY,
248 const BitmapTextBlob& blob, const SkPaint& paint,
joshualitt53b5f442015-04-13 06:33:59 -0700249 const SkMaskFilter::BlurRec& blurRec,
joshualittdbd35932015-04-02 09:19:04 -0700250 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700251 // If we have LCD text then our canonical color will be set to transparent, in this case we have
252 // to regenerate the blob on any color change
253 if (blob.fKey.fCanonicalColor == SK_ColorTRANSPARENT && blob.fPaintColor != paint.getColor()) {
joshualitt2a0e9f32015-04-13 06:12:21 -0700254 return true;
255 }
256
257 if (blob.fViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
258 return true;
259 }
260
261 if (blob.fViewMatrix.hasPerspective() && !blob.fViewMatrix.cheapEqualTo(viewMatrix)) {
262 return true;
263 }
264
joshualitt53b5f442015-04-13 06:33:59 -0700265 // We only cache one masked version
266 if (blob.fKey.fHasBlur &&
267 (blob.fBlurRec.fSigma != blurRec.fSigma ||
268 blob.fBlurRec.fStyle != blurRec.fStyle ||
269 blob.fBlurRec.fQuality != blurRec.fQuality)) {
270 return true;
271 }
272
273 // Similarly, we only cache one version for each style
274 if (blob.fKey.fStyle != SkPaint::kFill_Style &&
275 (blob.fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
276 blob.fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
277 blob.fStrokeInfo.fJoin != paint.getStrokeJoin())) {
278 return true;
279 }
280
joshualittfcfb9fc2015-04-21 07:35:10 -0700281 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
282 // for mixed blobs if this becomes an issue.
283 if (blob.hasBitmap() && blob.hasDistanceField()) {
joshualitt473ffa12015-04-22 18:23:15 -0700284 // Identical viewmatrices and we can reuse in all cases
285 if (blob.fViewMatrix.cheapEqualTo(viewMatrix) && x == blob.fX && y == blob.fY) {
286 return false;
287 }
joshualitt2a0e9f32015-04-13 06:12:21 -0700288 return true;
289 }
290
joshualittfcfb9fc2015-04-21 07:35:10 -0700291 if (blob.hasBitmap()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700292 if (blob.fViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
293 blob.fViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
294 blob.fViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
295 blob.fViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
296 return true;
297 }
298
joshualittfcfb9fc2015-04-21 07:35:10 -0700299 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
300 // but only for integer translations.
301 // This cool bit of math will determine the necessary translation to apply to the already
302 // generated vertex coordinates to move them to the correct position
303 SkScalar transX = viewMatrix.getTranslateX() +
304 viewMatrix.getScaleX() * (x - blob.fX) +
305 viewMatrix.getSkewX() * (y - blob.fY) -
306 blob.fViewMatrix.getTranslateX();
307 SkScalar transY = viewMatrix.getTranslateY() +
308 viewMatrix.getSkewY() * (x - blob.fX) +
309 viewMatrix.getScaleY() * (y - blob.fY) -
310 blob.fViewMatrix.getTranslateY();
joshualittf0c000d2015-04-27 09:36:55 -0700311 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY) ) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700312 return true;
313 }
314
joshualittfcfb9fc2015-04-21 07:35:10 -0700315 (*outTransX) = transX;
316 (*outTransY) = transY;
joshualitta7c63892015-04-21 13:24:37 -0700317 } else if (blob.hasDistanceField()) {
joshualitt64c99cc2015-04-21 09:43:03 -0700318 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
319 // distance field being generated, so we have to regenerate in those cases
320 SkScalar newMaxScale = viewMatrix.getMaxScale();
321 SkScalar oldMaxScale = blob.fViewMatrix.getMaxScale();
322 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
323 if (scaleAdjust < blob.fMaxMinScale || scaleAdjust > blob.fMinMaxScale) {
324 return true;
325 }
326
327 (*outTransX) = x - blob.fX;
328 (*outTransY) = y - blob.fY;
joshualittfcfb9fc2015-04-21 07:35:10 -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
joshualittfcfb9fc2015-04-21 07:35:10 -0700333
joshualitt2a0e9f32015-04-13 06:12:21 -0700334 return false;
joshualitt1d89e8d2015-04-01 12:40:54 -0700335}
336
337
joshualittdbd35932015-04-02 09:19:04 -0700338inline SkGlyphCache* GrAtlasTextContext::setupCache(BitmapTextBlob::Run* run,
339 const SkPaint& skPaint,
joshualitt9bd2daf2015-04-17 09:30:06 -0700340 const SkMatrix* viewMatrix,
341 bool noGamma) {
342 skPaint.getScalerContextDescriptor(&run->fDescriptor, &fDeviceProperties, viewMatrix, noGamma);
joshualitt1d89e8d2015-04-01 12:40:54 -0700343 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
344 return SkGlyphCache::DetachCache(run->fTypeface, run->fDescriptor.getDesc());
345}
346
robertphillips9c240a12015-05-28 07:45:59 -0700347void GrAtlasTextContext::drawTextBlob(GrRenderTarget* rt,
robertphillipsccb1b572015-05-27 11:02:55 -0700348 const GrClip& clip, const SkPaint& skPaint,
349 const SkMatrix& viewMatrix, const SkTextBlob* blob,
350 SkScalar x, SkScalar y,
joshualittdbd35932015-04-02 09:19:04 -0700351 SkDrawFilter* drawFilter, const SkIRect& clipBounds) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700352 // If we have been abandoned, then don't draw
robertphillipsea461502015-05-26 11:38:03 -0700353 if (fContext->abandoned()) {
354 return;
355 }
356
357 GrDrawContext* drawContext = fContext->drawContext();
358 if (!drawContext) {
joshualitt9b8e79e2015-04-24 09:57:12 -0700359 return;
360 }
361
joshualitt2a0e9f32015-04-13 06:12:21 -0700362 SkAutoTUnref<BitmapTextBlob> cacheBlob;
joshualitt53b5f442015-04-13 06:33:59 -0700363 SkMaskFilter::BlurRec blurRec;
364 BitmapTextBlob::Key key;
365 // It might be worth caching these things, but its not clear at this time
366 // TODO for animated mask filters, this will fill up our cache. We need a safeguard here
367 const SkMaskFilter* mf = skPaint.getMaskFilter();
joshualitt2a0e9f32015-04-13 06:12:21 -0700368 bool canCache = !(skPaint.getPathEffect() ||
joshualitt53b5f442015-04-13 06:33:59 -0700369 (mf && !mf->asABlur(&blurRec)) ||
joshualitt2a0e9f32015-04-13 06:12:21 -0700370 drawFilter);
371
372 if (canCache) {
joshualitt9e36c1a2015-04-14 12:17:27 -0700373 bool hasLCD = HasLCD(blob);
joshualitte4cee1f2015-05-11 13:04:28 -0700374
375 // We canonicalize all non-lcd draws to use kUnknown_SkPixelGeometry
376 SkPixelGeometry pixelGeometry = hasLCD ? fDeviceProperties.pixelGeometry() :
377 kUnknown_SkPixelGeometry;
378
joshualitt9e36c1a2015-04-14 12:17:27 -0700379 // TODO we want to figure out a way to be able to use the canonical color on LCD text,
380 // see the note on ComputeCanonicalColor above. We pick a dummy value for LCD text to
381 // ensure we always match the same key
382 GrColor canonicalColor = hasLCD ? SK_ColorTRANSPARENT :
383 ComputeCanonicalColor(skPaint, hasLCD);
384
joshualitte4cee1f2015-05-11 13:04:28 -0700385 key.fPixelGeometry = pixelGeometry;
joshualitt53b5f442015-04-13 06:33:59 -0700386 key.fUniqueID = blob->uniqueID();
387 key.fStyle = skPaint.getStyle();
388 key.fHasBlur = SkToBool(mf);
joshualitt9e36c1a2015-04-14 12:17:27 -0700389 key.fCanonicalColor = canonicalColor;
joshualitt53b5f442015-04-13 06:33:59 -0700390 cacheBlob.reset(SkSafeRef(fCache->find(key)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700391 }
392
joshualitt1d89e8d2015-04-01 12:40:54 -0700393 SkIRect clipRect;
394 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
395
joshualitt2a0e9f32015-04-13 06:12:21 -0700396 SkScalar transX = 0.f;
397 SkScalar transY = 0.f;
398
joshualitt9e36c1a2015-04-14 12:17:27 -0700399 // Though for the time being runs in the textblob can override the paint, they only touch font
400 // info.
401 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700402 if (!SkPaint2GrPaint(fContext, rt, skPaint, viewMatrix, true, &grPaint)) {
403 return;
404 }
joshualitt9e36c1a2015-04-14 12:17:27 -0700405
joshualittb7133be2015-04-08 09:08:31 -0700406 if (cacheBlob) {
joshualitt53b5f442015-04-13 06:33:59 -0700407 if (MustRegenerateBlob(&transX, &transY, *cacheBlob, skPaint, blurRec, viewMatrix, x, y)) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700408 // We have to remake the blob because changes may invalidate our masks.
409 // TODO we could probably get away reuse most of the time if the pointer is unique,
410 // but we'd have to clear the subrun information
joshualittb7133be2015-04-08 09:08:31 -0700411 fCache->remove(cacheBlob);
joshualitt53b5f442015-04-13 06:33:59 -0700412 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
413 kGrayTextVASize)));
robertphillips9c240a12015-05-28 07:45:59 -0700414 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700415 blob, x, y, drawFilter, clipRect, rt, clip, grPaint);
joshualittb7133be2015-04-08 09:08:31 -0700416 } else {
joshualitt9e36c1a2015-04-14 12:17:27 -0700417 // If we can reuse the blob, then make sure we update the blob's viewmatrix, and x/y
418 // offsets
joshualitt2a0e9f32015-04-13 06:12:21 -0700419 cacheBlob->fViewMatrix = viewMatrix;
420 cacheBlob->fX = x;
421 cacheBlob->fY = y;
joshualittb7133be2015-04-08 09:08:31 -0700422 fCache->makeMRU(cacheBlob);
joshualitt1d89e8d2015-04-01 12:40:54 -0700423 }
424 } else {
joshualitt2a0e9f32015-04-13 06:12:21 -0700425 if (canCache) {
joshualitt53b5f442015-04-13 06:33:59 -0700426 cacheBlob.reset(SkRef(fCache->createCachedBlob(blob, key, blurRec, skPaint,
427 kGrayTextVASize)));
joshualitt2a0e9f32015-04-13 06:12:21 -0700428 } else {
429 cacheBlob.reset(fCache->createBlob(blob, kGrayTextVASize));
430 }
robertphillips9c240a12015-05-28 07:45:59 -0700431 this->regenerateTextBlob(cacheBlob, skPaint, grPaint.getColor(), viewMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -0700432 blob, x, y, drawFilter, clipRect, rt, clip, grPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -0700433 }
434
joshualitt9e36c1a2015-04-14 12:17:27 -0700435 cacheBlob->fPaintColor = skPaint.getColor();
robertphillips9c240a12015-05-28 07:45:59 -0700436 this->flush(drawContext, blob, cacheBlob, rt, skPaint, grPaint, drawFilter,
joshualitt2a0e9f32015-04-13 06:12:21 -0700437 clip, viewMatrix, clipBounds, x, y, transX, transY);
joshualitt1d89e8d2015-04-01 12:40:54 -0700438}
439
joshualitt9bd2daf2015-04-17 09:30:06 -0700440inline bool GrAtlasTextContext::canDrawAsDistanceFields(const SkPaint& skPaint,
441 const SkMatrix& viewMatrix) {
442 // TODO: support perspective (need getMaxScale replacement)
443 if (viewMatrix.hasPerspective()) {
444 return false;
445 }
446
447 SkScalar maxScale = viewMatrix.getMaxScale();
448 SkScalar scaledTextSize = maxScale*skPaint.getTextSize();
449 // Hinted text looks far better at small resolutions
450 // Scaling up beyond 2x yields undesireable artifacts
joshualitta7c63892015-04-21 13:24:37 -0700451 if (scaledTextSize < kMinDFFontSize || scaledTextSize > kLargeDFFontLimit) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700452 return false;
453 }
454
455 if (!fEnableDFRendering && !skPaint.isDistanceFieldTextTEMP() &&
456 scaledTextSize < kLargeDFFontSize) {
457 return false;
458 }
459
460 // rasterizers and mask filters modify alpha, which doesn't
461 // translate well to distance
462 if (skPaint.getRasterizer() || skPaint.getMaskFilter() ||
robertphillipsea461502015-05-26 11:38:03 -0700463 !fContext->shaderDerivativeSupport()) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700464 return false;
465 }
466
467 // TODO: add some stroking support
468 if (skPaint.getStyle() != SkPaint::kFill_Style) {
469 return false;
470 }
471
472 return true;
473}
474
robertphillips9c240a12015-05-28 07:45:59 -0700475void GrAtlasTextContext::regenerateTextBlob(BitmapTextBlob* cacheBlob,
joshualitt9e36c1a2015-04-14 12:17:27 -0700476 const SkPaint& skPaint, GrColor color,
477 const SkMatrix& viewMatrix,
joshualittdbd35932015-04-02 09:19:04 -0700478 const SkTextBlob* blob, SkScalar x, SkScalar y,
joshualittfcfb9fc2015-04-21 07:35:10 -0700479 SkDrawFilter* drawFilter, const SkIRect& clipRect,
480 GrRenderTarget* rt, const GrClip& clip,
481 const GrPaint& paint) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700482 cacheBlob->fViewMatrix = viewMatrix;
483 cacheBlob->fX = x;
484 cacheBlob->fY = y;
joshualitt1d89e8d2015-04-01 12:40:54 -0700485
486 // Regenerate textblob
487 SkPaint runPaint = skPaint;
488 SkTextBlob::RunIterator it(blob);
489 for (int run = 0; !it.done(); it.next(), run++) {
490 int glyphCount = it.glyphCount();
491 size_t textLen = glyphCount * sizeof(uint16_t);
492 const SkPoint& offset = it.offset();
493 // applyFontToPaint() always overwrites the exact same attributes,
494 // so it is safe to not re-seed the paint for this reason.
495 it.applyFontToPaint(&runPaint);
496
497 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
498 // A false return from filter() means we should abort the current draw.
499 runPaint = skPaint;
500 continue;
501 }
502
robertphillips9c240a12015-05-28 07:45:59 -0700503 runPaint.setFlags(FilterTextFlags(fDeviceProperties, runPaint));
joshualitt1d89e8d2015-04-01 12:40:54 -0700504
joshualitt1d89e8d2015-04-01 12:40:54 -0700505 // setup vertex / glyphIndex for the new run
506 if (run > 0) {
507 PerSubRunInfo& newRun = cacheBlob->fRuns[run].fSubRunInfo.back();
508 PerSubRunInfo& lastRun = cacheBlob->fRuns[run - 1].fSubRunInfo.back();
509
510 newRun.fVertexStartIndex = lastRun.fVertexEndIndex;
511 newRun.fVertexEndIndex = lastRun.fVertexEndIndex;
512
513 newRun.fGlyphStartIndex = lastRun.fGlyphEndIndex;
514 newRun.fGlyphEndIndex = lastRun.fGlyphEndIndex;
515 }
516
joshualittfcfb9fc2015-04-21 07:35:10 -0700517 if (this->canDrawAsDistanceFields(runPaint, viewMatrix)) {
518 cacheBlob->setHasDistanceField();
519 SkPaint dfPaint = runPaint;
520 SkScalar textRatio;
joshualitt64c99cc2015-04-21 09:43:03 -0700521 this->initDistanceFieldPaint(cacheBlob, &dfPaint, &textRatio, viewMatrix);
joshualittfcfb9fc2015-04-21 07:35:10 -0700522 Run& runIdx = cacheBlob->fRuns[run];
523 PerSubRunInfo& subRun = runIdx.fSubRunInfo.back();
524 subRun.fUseLCDText = runPaint.isLCDRenderText();
525 subRun.fDrawAsDistanceFields = true;
joshualitt9a27e632015-04-06 10:53:36 -0700526
joshualittfcfb9fc2015-04-21 07:35:10 -0700527 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], dfPaint, NULL, true);
528
529 SkTDArray<char> fallbackTxt;
530 SkTDArray<SkScalar> fallbackPos;
531 SkPoint dfOffset;
532 int scalarsPerPosition = 2;
533 switch (it.positioning()) {
534 case SkTextBlob::kDefault_Positioning: {
535 this->internalDrawDFText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
536 (const char *)it.glyphs(), textLen,
537 x + offset.x(), y + offset.y(), clipRect, textRatio,
538 &fallbackTxt, &fallbackPos, &dfOffset, runPaint);
539 break;
540 }
541 case SkTextBlob::kHorizontal_Positioning: {
542 scalarsPerPosition = 1;
543 dfOffset = SkPoint::Make(x, y + offset.y());
544 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
545 (const char*)it.glyphs(), textLen, it.pos(),
546 scalarsPerPosition, dfOffset, clipRect, textRatio,
547 &fallbackTxt, &fallbackPos);
548 break;
549 }
550 case SkTextBlob::kFull_Positioning: {
551 dfOffset = SkPoint::Make(x, y);
552 this->internalDrawDFPosText(cacheBlob, run, cache, dfPaint, color, viewMatrix,
553 (const char*)it.glyphs(), textLen, it.pos(),
554 scalarsPerPosition, dfOffset, clipRect, textRatio,
555 &fallbackTxt, &fallbackPos);
556 break;
557 }
558 }
559 if (fallbackTxt.count()) {
560 this->fallbackDrawPosText(cacheBlob, run, rt, clip, paint, runPaint, viewMatrix,
561 fallbackTxt, fallbackPos, scalarsPerPosition, dfOffset,
562 clipRect);
563 }
564
565 SkGlyphCache::AttachCache(cache);
566 } else if (SkDraw::ShouldDrawTextAsPaths(runPaint, viewMatrix)) {
567 cacheBlob->fRuns[run].fDrawAsPaths = true;
568 } else {
569 cacheBlob->setHasBitmap();
570 SkGlyphCache* cache = this->setupCache(&cacheBlob->fRuns[run], runPaint, &viewMatrix,
571 false);
572 switch (it.positioning()) {
573 case SkTextBlob::kDefault_Positioning:
574 this->internalDrawBMPText(cacheBlob, run, cache, runPaint, color, viewMatrix,
575 (const char *)it.glyphs(), textLen,
576 x + offset.x(), y + offset.y(), clipRect);
577 break;
578 case SkTextBlob::kHorizontal_Positioning:
579 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
580 (const char*)it.glyphs(), textLen, it.pos(), 1,
581 SkPoint::Make(x, y + offset.y()), clipRect);
582 break;
583 case SkTextBlob::kFull_Positioning:
584 this->internalDrawBMPPosText(cacheBlob, run, cache, runPaint, color, viewMatrix,
585 (const char*)it.glyphs(), textLen, it.pos(), 2,
586 SkPoint::Make(x, y), clipRect);
587 break;
588 }
589 SkGlyphCache::AttachCache(cache);
joshualitt1d89e8d2015-04-01 12:40:54 -0700590 }
591
592 if (drawFilter) {
593 // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
594 runPaint = skPaint;
595 }
joshualitt1d89e8d2015-04-01 12:40:54 -0700596 }
597}
598
joshualitt64c99cc2015-04-21 09:43:03 -0700599inline void GrAtlasTextContext::initDistanceFieldPaint(BitmapTextBlob* blob,
600 SkPaint* skPaint,
601 SkScalar* textRatio,
joshualitt9bd2daf2015-04-17 09:30:06 -0700602 const SkMatrix& viewMatrix) {
603 // getMaxScale doesn't support perspective, so neither do we at the moment
604 SkASSERT(!viewMatrix.hasPerspective());
605 SkScalar maxScale = viewMatrix.getMaxScale();
606 SkScalar textSize = skPaint->getTextSize();
607 SkScalar scaledTextSize = textSize;
608 // if we have non-unity scale, we need to choose our base text size
609 // based on the SkPaint's text size multiplied by the max scale factor
610 // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
611 if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
612 scaledTextSize *= maxScale;
613 }
614
joshualitt64c99cc2015-04-21 09:43:03 -0700615 // We have three sizes of distance field text, and within each size 'bucket' there is a floor
616 // and ceiling. A scale outside of this range would require regenerating the distance fields
617 SkScalar dfMaskScaleFloor;
618 SkScalar dfMaskScaleCeil;
joshualitt9bd2daf2015-04-17 09:30:06 -0700619 if (scaledTextSize <= kSmallDFFontLimit) {
joshualitt64c99cc2015-04-21 09:43:03 -0700620 dfMaskScaleFloor = kMinDFFontSize;
joshualitta7c63892015-04-21 13:24:37 -0700621 dfMaskScaleCeil = kSmallDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700622 *textRatio = textSize / kSmallDFFontSize;
623 skPaint->setTextSize(SkIntToScalar(kSmallDFFontSize));
624 } else if (scaledTextSize <= kMediumDFFontLimit) {
joshualitta7c63892015-04-21 13:24:37 -0700625 dfMaskScaleFloor = kSmallDFFontLimit;
626 dfMaskScaleCeil = kMediumDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700627 *textRatio = textSize / kMediumDFFontSize;
628 skPaint->setTextSize(SkIntToScalar(kMediumDFFontSize));
629 } else {
joshualitta7c63892015-04-21 13:24:37 -0700630 dfMaskScaleFloor = kMediumDFFontLimit;
631 dfMaskScaleCeil = kLargeDFFontLimit;
joshualitt9bd2daf2015-04-17 09:30:06 -0700632 *textRatio = textSize / kLargeDFFontSize;
633 skPaint->setTextSize(SkIntToScalar(kLargeDFFontSize));
634 }
635
joshualitt64c99cc2015-04-21 09:43:03 -0700636 // Because there can be multiple runs in the blob, we want the overall maxMinScale, and
637 // minMaxScale to make regeneration decisions. Specifically, we want the maximum minimum scale
638 // we can tolerate before we'd drop to a lower mip size, and the minimum maximum scale we can
639 // tolerate before we'd have to move to a large mip size. When we actually test these values
640 // we look at the delta in scale between the new viewmatrix and the old viewmatrix, and test
641 // against these values to decide if we can reuse or not(ie, will a given scale change our mip
642 // level)
joshualitta7c63892015-04-21 13:24:37 -0700643 SkASSERT(dfMaskScaleFloor <= scaledTextSize && scaledTextSize <= dfMaskScaleCeil);
joshualitt64c99cc2015-04-21 09:43:03 -0700644 blob->fMaxMinScale = SkMaxScalar(dfMaskScaleFloor / scaledTextSize, blob->fMaxMinScale);
645 blob->fMinMaxScale = SkMinScalar(dfMaskScaleCeil / scaledTextSize, blob->fMinMaxScale);
646
joshualitt9bd2daf2015-04-17 09:30:06 -0700647 skPaint->setLCDRenderText(false);
648 skPaint->setAutohinted(false);
649 skPaint->setHinting(SkPaint::kNormal_Hinting);
650 skPaint->setSubpixelText(true);
651}
652
joshualittfec19e12015-04-17 10:32:32 -0700653inline void GrAtlasTextContext::fallbackDrawPosText(BitmapTextBlob* blob,
joshualittfcfb9fc2015-04-21 07:35:10 -0700654 int runIndex,
joshualittfec19e12015-04-17 10:32:32 -0700655 GrRenderTarget* rt, const GrClip& clip,
joshualitt9bd2daf2015-04-17 09:30:06 -0700656 const GrPaint& paint,
657 const SkPaint& skPaint,
658 const SkMatrix& viewMatrix,
659 const SkTDArray<char>& fallbackTxt,
660 const SkTDArray<SkScalar>& fallbackPos,
661 int scalarsPerPosition,
662 const SkPoint& offset,
663 const SkIRect& clipRect) {
joshualittfec19e12015-04-17 10:32:32 -0700664 SkASSERT(fallbackTxt.count());
joshualittfcfb9fc2015-04-21 07:35:10 -0700665 blob->setHasBitmap();
666 Run& run = blob->fRuns[runIndex];
joshualitt97202d22015-04-22 13:47:02 -0700667 // Push back a new subrun to fill and set the override descriptor
668 run.push_back();
669 run.fOverrideDescriptor.reset(SkNEW(SkAutoDescriptor));
670 skPaint.getScalerContextDescriptor(run.fOverrideDescriptor,
joshualittfec19e12015-04-17 10:32:32 -0700671 &fDeviceProperties, &viewMatrix, false);
672 SkGlyphCache* cache = SkGlyphCache::DetachCache(run.fTypeface,
joshualitt97202d22015-04-22 13:47:02 -0700673 run.fOverrideDescriptor->getDesc());
joshualittfcfb9fc2015-04-21 07:35:10 -0700674 this->internalDrawBMPPosText(blob, runIndex, cache, skPaint, paint.getColor(), viewMatrix,
joshualitt9bd2daf2015-04-17 09:30:06 -0700675 fallbackTxt.begin(), fallbackTxt.count(),
676 fallbackPos.begin(), scalarsPerPosition, offset, clipRect);
677 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700678}
679
680inline GrAtlasTextContext::BitmapTextBlob*
681GrAtlasTextContext::setupDFBlob(int glyphCount, const SkPaint& origPaint,
682 const SkMatrix& viewMatrix, SkGlyphCache** cache,
683 SkPaint* dfPaint, SkScalar* textRatio) {
684 BitmapTextBlob* blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
685
686 *dfPaint = origPaint;
joshualitt64c99cc2015-04-21 09:43:03 -0700687 this->initDistanceFieldPaint(blob, dfPaint, textRatio, viewMatrix);
joshualitt9bd2daf2015-04-17 09:30:06 -0700688 blob->fViewMatrix = viewMatrix;
joshualittfcfb9fc2015-04-21 07:35:10 -0700689 Run& run = blob->fRuns[0];
690 PerSubRunInfo& subRun = run.fSubRunInfo.back();
691 subRun.fUseLCDText = origPaint.isLCDRenderText();
692 subRun.fDrawAsDistanceFields = true;
joshualitt9bd2daf2015-04-17 09:30:06 -0700693
694 *cache = this->setupCache(&blob->fRuns[0], *dfPaint, NULL, true);
695 return blob;
696}
697
joshualitt79dfb2b2015-05-11 08:58:08 -0700698inline GrAtlasTextContext::BitmapTextBlob*
699GrAtlasTextContext::createDrawTextBlob(GrRenderTarget* rt, const GrClip& clip,
700 const GrPaint& paint, const SkPaint& skPaint,
701 const SkMatrix& viewMatrix,
702 const char text[], size_t byteLength,
703 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700704 int glyphCount = skPaint.countText(text, byteLength);
joshualitt1d89e8d2015-04-01 12:40:54 -0700705 SkIRect clipRect;
706 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
707
joshualitt79dfb2b2015-05-11 08:58:08 -0700708 BitmapTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700709 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
710 SkPaint dfPaint;
711 SkScalar textRatio;
712 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700713 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt1d89e8d2015-04-01 12:40:54 -0700714
joshualitt9bd2daf2015-04-17 09:30:06 -0700715 SkTDArray<char> fallbackTxt;
716 SkTDArray<SkScalar> fallbackPos;
717 SkPoint offset;
718 this->internalDrawDFText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
719 byteLength, x, y, clipRect, textRatio, &fallbackTxt, &fallbackPos,
720 &offset, skPaint);
721 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700722 if (fallbackTxt.count()) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700723 this->fallbackDrawPosText(blob, 0, rt, clip, paint, skPaint, viewMatrix, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700724 fallbackPos, 2, offset, clipRect);
725 }
726 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700727 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700728 blob->fViewMatrix = viewMatrix;
729
730 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
731 this->internalDrawBMPText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
732 byteLength, x, y, clipRect);
733 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700734 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700735 return blob;
joshualitt1d89e8d2015-04-01 12:40:54 -0700736}
737
joshualitt79dfb2b2015-05-11 08:58:08 -0700738inline GrAtlasTextContext::BitmapTextBlob*
739GrAtlasTextContext::createDrawPosTextBlob(GrRenderTarget* rt, const GrClip& clip,
740 const GrPaint& paint, const SkPaint& skPaint,
741 const SkMatrix& viewMatrix,
742 const char text[], size_t byteLength,
743 const SkScalar pos[], int scalarsPerPosition,
744 const SkPoint& offset, const SkIRect& regionClipBounds) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700745 int glyphCount = skPaint.countText(text, byteLength);
746
747 SkIRect clipRect;
748 clip.getConservativeBounds(rt->width(), rt->height(), &clipRect);
749
joshualitt79dfb2b2015-05-11 08:58:08 -0700750 BitmapTextBlob* blob;
joshualitt9bd2daf2015-04-17 09:30:06 -0700751 if (this->canDrawAsDistanceFields(skPaint, viewMatrix)) {
752 SkPaint dfPaint;
753 SkScalar textRatio;
754 SkGlyphCache* cache;
joshualitt79dfb2b2015-05-11 08:58:08 -0700755 blob = this->setupDFBlob(glyphCount, skPaint, viewMatrix, &cache, &dfPaint, &textRatio);
joshualitt9bd2daf2015-04-17 09:30:06 -0700756
757 SkTDArray<char> fallbackTxt;
758 SkTDArray<SkScalar> fallbackPos;
759 this->internalDrawDFPosText(blob, 0, cache, dfPaint, paint.getColor(), viewMatrix, text,
760 byteLength, pos, scalarsPerPosition, offset, clipRect,
761 textRatio, &fallbackTxt, &fallbackPos);
762 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700763 if (fallbackTxt.count()) {
joshualittfcfb9fc2015-04-21 07:35:10 -0700764 this->fallbackDrawPosText(blob, 0, rt, clip, paint, skPaint, viewMatrix, fallbackTxt,
joshualitt9bd2daf2015-04-17 09:30:06 -0700765 fallbackPos, scalarsPerPosition, offset, clipRect);
766 }
767 } else {
joshualitt79dfb2b2015-05-11 08:58:08 -0700768 blob = fCache->createBlob(glyphCount, 1, kGrayTextVASize);
joshualitt9bd2daf2015-04-17 09:30:06 -0700769 blob->fViewMatrix = viewMatrix;
770 SkGlyphCache* cache = this->setupCache(&blob->fRuns[0], skPaint, &viewMatrix, false);
771 this->internalDrawBMPPosText(blob, 0, cache, skPaint, paint.getColor(), viewMatrix, text,
772 byteLength, pos, scalarsPerPosition, offset, clipRect);
773 SkGlyphCache::AttachCache(cache);
joshualitt9bd2daf2015-04-17 09:30:06 -0700774 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700775 return blob;
776}
777
robertphillipsccb1b572015-05-27 11:02:55 -0700778void GrAtlasTextContext::onDrawText(GrDrawContext* drawContext, GrRenderTarget* rt,
779 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700780 const GrPaint& paint, const SkPaint& skPaint,
781 const SkMatrix& viewMatrix,
782 const char text[], size_t byteLength,
783 SkScalar x, SkScalar y, const SkIRect& regionClipBounds) {
robertphillipsea461502015-05-26 11:38:03 -0700784 if (drawContext) {
785 SkAutoTUnref<BitmapTextBlob> blob(
joshualitt79dfb2b2015-05-11 08:58:08 -0700786 this->createDrawTextBlob(rt, clip, paint, skPaint, viewMatrix,
787 text, byteLength, x, y, regionClipBounds));
robertphillipsea461502015-05-26 11:38:03 -0700788 this->flush(drawContext, blob, rt, skPaint, paint, clip, regionClipBounds);
789 }
joshualitt79dfb2b2015-05-11 08:58:08 -0700790}
791
robertphillipsccb1b572015-05-27 11:02:55 -0700792void GrAtlasTextContext::onDrawPosText(GrDrawContext* drawContext, GrRenderTarget* rt,
793 const GrClip& clip,
joshualitt79dfb2b2015-05-11 08:58:08 -0700794 const GrPaint& paint, const SkPaint& skPaint,
795 const SkMatrix& viewMatrix,
796 const char text[], size_t byteLength,
797 const SkScalar pos[], int scalarsPerPosition,
798 const SkPoint& offset, const SkIRect& regionClipBounds) {
robertphillipsea461502015-05-26 11:38:03 -0700799 if (drawContext) {
800 SkAutoTUnref<BitmapTextBlob> blob(
joshualitt79dfb2b2015-05-11 08:58:08 -0700801 this->createDrawPosTextBlob(rt, clip, paint, skPaint, viewMatrix,
802 text, byteLength,
803 pos, scalarsPerPosition,
804 offset, regionClipBounds));
805
robertphillipsea461502015-05-26 11:38:03 -0700806 this->flush(drawContext, blob, rt, skPaint, paint, clip, regionClipBounds);
807 }
joshualitt9bd2daf2015-04-17 09:30:06 -0700808}
809
810void GrAtlasTextContext::internalDrawBMPText(BitmapTextBlob* blob, int runIndex,
811 SkGlyphCache* cache, const SkPaint& skPaint,
812 GrColor color,
813 const SkMatrix& viewMatrix,
814 const char text[], size_t byteLength,
815 SkScalar x, SkScalar y, const SkIRect& clipRect) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700816 SkASSERT(byteLength == 0 || text != NULL);
817
818 // nothing to draw
819 if (text == NULL || byteLength == 0) {
820 return;
821 }
822
823 fCurrStrike = NULL;
824 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
825
826 // Get GrFontScaler from cache
827 GrFontScaler* fontScaler = GetGrFontScaler(cache);
828
829 // transform our starting point
830 {
831 SkPoint loc;
832 viewMatrix.mapXY(x, y, &loc);
833 x = loc.fX;
834 y = loc.fY;
835 }
836
837 // need to measure first
838 if (skPaint.getTextAlign() != SkPaint::kLeft_Align) {
839 SkVector stopVector;
840 MeasureText(cache, glyphCacheProc, text, byteLength, &stopVector);
841
842 SkScalar stopX = stopVector.fX;
843 SkScalar stopY = stopVector.fY;
844
845 if (skPaint.getTextAlign() == SkPaint::kCenter_Align) {
846 stopX = SkScalarHalf(stopX);
847 stopY = SkScalarHalf(stopY);
848 }
849 x -= stopX;
850 y -= stopY;
851 }
852
853 const char* stop = text + byteLength;
854
855 SkAutoKern autokern;
856
857 SkFixed fxMask = ~0;
858 SkFixed fyMask = ~0;
859 SkScalar halfSampleX, halfSampleY;
860 if (cache->isSubpixel()) {
861 halfSampleX = halfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
862 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
863 if (kX_SkAxisAlignment == baseline) {
864 fyMask = 0;
865 halfSampleY = SK_ScalarHalf;
866 } else if (kY_SkAxisAlignment == baseline) {
867 fxMask = 0;
868 halfSampleX = SK_ScalarHalf;
869 }
870 } else {
871 halfSampleX = halfSampleY = SK_ScalarHalf;
872 }
873
874 Sk48Dot16 fx = SkScalarTo48Dot16(x + halfSampleX);
875 Sk48Dot16 fy = SkScalarTo48Dot16(y + halfSampleY);
876
877 while (text < stop) {
878 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
879
880 fx += autokern.adjust(glyph);
881
882 if (glyph.fWidth) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700883 this->bmpAppendGlyph(blob,
884 runIndex,
885 GrGlyph::Pack(glyph.getGlyphID(),
886 glyph.getSubXFixed(),
887 glyph.getSubYFixed(),
888 GrGlyph::kCoverage_MaskStyle),
889 Sk48Dot16FloorToInt(fx),
890 Sk48Dot16FloorToInt(fy),
891 color,
892 fontScaler,
893 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700894 }
895
896 fx += glyph.fAdvanceX;
897 fy += glyph.fAdvanceY;
898 }
899}
900
joshualitt9bd2daf2015-04-17 09:30:06 -0700901void GrAtlasTextContext::internalDrawBMPPosText(BitmapTextBlob* blob, int runIndex,
902 SkGlyphCache* cache, const SkPaint& skPaint,
903 GrColor color,
904 const SkMatrix& viewMatrix,
905 const char text[], size_t byteLength,
906 const SkScalar pos[], int scalarsPerPosition,
907 const SkPoint& offset, const SkIRect& clipRect) {
joshualitt1d89e8d2015-04-01 12:40:54 -0700908 SkASSERT(byteLength == 0 || text != NULL);
909 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
910
911 // nothing to draw
912 if (text == NULL || byteLength == 0) {
913 return;
914 }
915
916 fCurrStrike = NULL;
917 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
918
919 // Get GrFontScaler from cache
920 GrFontScaler* fontScaler = GetGrFontScaler(cache);
921
922 const char* stop = text + byteLength;
923 SkTextAlignProc alignProc(skPaint.getTextAlign());
924 SkTextMapStateProc tmsProc(viewMatrix, offset, scalarsPerPosition);
925
926 if (cache->isSubpixel()) {
927 // maybe we should skip the rounding if linearText is set
928 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(viewMatrix);
929
930 SkFixed fxMask = ~0;
931 SkFixed fyMask = ~0;
932 SkScalar halfSampleX = SkFixedToScalar(SkGlyph::kSubpixelRound);
933 SkScalar halfSampleY = SkFixedToScalar(SkGlyph::kSubpixelRound);
934 if (kX_SkAxisAlignment == baseline) {
935 fyMask = 0;
936 halfSampleY = SK_ScalarHalf;
937 } else if (kY_SkAxisAlignment == baseline) {
938 fxMask = 0;
939 halfSampleX = SK_ScalarHalf;
940 }
941
942 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
943 while (text < stop) {
944 SkPoint tmsLoc;
945 tmsProc(pos, &tmsLoc);
946 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + halfSampleX);
947 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + halfSampleY);
948
949 const SkGlyph& glyph = glyphCacheProc(cache, &text,
950 fx & fxMask, fy & fyMask);
951
952 if (glyph.fWidth) {
joshualitt9bd2daf2015-04-17 09:30:06 -0700953 this->bmpAppendGlyph(blob,
954 runIndex,
955 GrGlyph::Pack(glyph.getGlyphID(),
956 glyph.getSubXFixed(),
957 glyph.getSubYFixed(),
958 GrGlyph::kCoverage_MaskStyle),
959 Sk48Dot16FloorToInt(fx),
960 Sk48Dot16FloorToInt(fy),
961 color,
962 fontScaler,
963 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -0700964 }
965 pos += scalarsPerPosition;
966 }
967 } else {
968 while (text < stop) {
969 const char* currentText = text;
970 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
971
972 if (metricGlyph.fWidth) {
973 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
974 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
975 SkPoint tmsLoc;
976 tmsProc(pos, &tmsLoc);
977 SkPoint alignLoc;
978 alignProc(tmsLoc, metricGlyph, &alignLoc);
979
980 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + halfSampleX);
981 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + halfSampleY);
982
983 // have to call again, now that we've been "aligned"
984 const SkGlyph& glyph = glyphCacheProc(cache, &currentText,
985 fx & fxMask, fy & fyMask);
986 // the assumption is that the metrics haven't changed
987 SkASSERT(prevAdvX == glyph.fAdvanceX);
988 SkASSERT(prevAdvY == glyph.fAdvanceY);
989 SkASSERT(glyph.fWidth);
990
joshualitt9bd2daf2015-04-17 09:30:06 -0700991 this->bmpAppendGlyph(blob,
992 runIndex,
993 GrGlyph::Pack(glyph.getGlyphID(),
994 glyph.getSubXFixed(),
995 glyph.getSubYFixed(),
996 GrGlyph::kCoverage_MaskStyle),
997 Sk48Dot16FloorToInt(fx),
998 Sk48Dot16FloorToInt(fy),
999 color,
1000 fontScaler,
1001 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001002 }
1003 pos += scalarsPerPosition;
1004 }
1005 }
1006 } else { // not subpixel
1007
1008 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
1009 while (text < stop) {
1010 // the last 2 parameters are ignored
1011 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1012
1013 if (glyph.fWidth) {
1014 SkPoint tmsLoc;
1015 tmsProc(pos, &tmsLoc);
1016
1017 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + SK_ScalarHalf); //halfSampleX;
1018 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + SK_ScalarHalf); //halfSampleY;
joshualitt9bd2daf2015-04-17 09:30:06 -07001019 this->bmpAppendGlyph(blob,
1020 runIndex,
1021 GrGlyph::Pack(glyph.getGlyphID(),
1022 glyph.getSubXFixed(),
1023 glyph.getSubYFixed(),
1024 GrGlyph::kCoverage_MaskStyle),
1025 Sk48Dot16FloorToInt(fx),
1026 Sk48Dot16FloorToInt(fy),
1027 color,
1028 fontScaler,
1029 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001030 }
1031 pos += scalarsPerPosition;
1032 }
1033 } else {
1034 while (text < stop) {
1035 // the last 2 parameters are ignored
1036 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1037
1038 if (glyph.fWidth) {
1039 SkPoint tmsLoc;
1040 tmsProc(pos, &tmsLoc);
1041
1042 SkPoint alignLoc;
1043 alignProc(tmsLoc, glyph, &alignLoc);
1044
1045 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + SK_ScalarHalf); //halfSampleX;
1046 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + SK_ScalarHalf); //halfSampleY;
joshualitt9bd2daf2015-04-17 09:30:06 -07001047 this->bmpAppendGlyph(blob,
1048 runIndex,
1049 GrGlyph::Pack(glyph.getGlyphID(),
1050 glyph.getSubXFixed(),
1051 glyph.getSubYFixed(),
1052 GrGlyph::kCoverage_MaskStyle),
1053 Sk48Dot16FloorToInt(fx),
1054 Sk48Dot16FloorToInt(fy),
1055 color,
1056 fontScaler,
1057 clipRect);
joshualitt1d89e8d2015-04-01 12:40:54 -07001058 }
1059 pos += scalarsPerPosition;
1060 }
1061 }
1062 }
1063}
1064
joshualitt9bd2daf2015-04-17 09:30:06 -07001065
1066void GrAtlasTextContext::internalDrawDFText(BitmapTextBlob* blob, int runIndex,
1067 SkGlyphCache* cache, const SkPaint& skPaint,
1068 GrColor color,
1069 const SkMatrix& viewMatrix,
1070 const char text[], size_t byteLength,
1071 SkScalar x, SkScalar y, const SkIRect& clipRect,
1072 SkScalar textRatio,
1073 SkTDArray<char>* fallbackTxt,
1074 SkTDArray<SkScalar>* fallbackPos,
1075 SkPoint* offset,
1076 const SkPaint& origPaint) {
1077 SkASSERT(byteLength == 0 || text != NULL);
1078
1079 // nothing to draw
1080 if (text == NULL || byteLength == 0) {
1081 return;
1082 }
1083
1084 SkDrawCacheProc glyphCacheProc = origPaint.getDrawCacheProc();
1085 SkAutoDescriptor desc;
1086 origPaint.getScalerContextDescriptor(&desc, &fDeviceProperties, NULL, true);
1087 SkGlyphCache* origPaintCache = SkGlyphCache::DetachCache(origPaint.getTypeface(),
1088 desc.getDesc());
1089
1090 SkTArray<SkScalar> positions;
1091
1092 const char* textPtr = text;
1093 SkFixed stopX = 0;
1094 SkFixed stopY = 0;
1095 SkFixed origin = 0;
1096 switch (origPaint.getTextAlign()) {
1097 case SkPaint::kRight_Align: origin = SK_Fixed1; break;
1098 case SkPaint::kCenter_Align: origin = SK_FixedHalf; break;
1099 case SkPaint::kLeft_Align: origin = 0; break;
1100 }
1101
1102 SkAutoKern autokern;
1103 const char* stop = text + byteLength;
1104 while (textPtr < stop) {
1105 // don't need x, y here, since all subpixel variants will have the
1106 // same advance
1107 const SkGlyph& glyph = glyphCacheProc(origPaintCache, &textPtr, 0, 0);
1108
1109 SkFixed width = glyph.fAdvanceX + autokern.adjust(glyph);
1110 positions.push_back(SkFixedToScalar(stopX + SkFixedMul(origin, width)));
1111
1112 SkFixed height = glyph.fAdvanceY;
1113 positions.push_back(SkFixedToScalar(stopY + SkFixedMul(origin, height)));
1114
1115 stopX += width;
1116 stopY += height;
1117 }
1118 SkASSERT(textPtr == stop);
1119
1120 // now adjust starting point depending on alignment
1121 SkScalar alignX = SkFixedToScalar(stopX);
1122 SkScalar alignY = SkFixedToScalar(stopY);
1123 if (origPaint.getTextAlign() == SkPaint::kCenter_Align) {
1124 alignX = SkScalarHalf(alignX);
1125 alignY = SkScalarHalf(alignY);
1126 } else if (origPaint.getTextAlign() == SkPaint::kLeft_Align) {
1127 alignX = 0;
1128 alignY = 0;
1129 }
1130 x -= alignX;
1131 y -= alignY;
1132 *offset = SkPoint::Make(x, y);
1133
1134 this->internalDrawDFPosText(blob, runIndex, cache, skPaint, color, viewMatrix, text, byteLength,
1135 positions.begin(), 2, *offset, clipRect, textRatio, fallbackTxt,
1136 fallbackPos);
1137 SkGlyphCache::AttachCache(origPaintCache);
1138}
1139
1140void GrAtlasTextContext::internalDrawDFPosText(BitmapTextBlob* blob, int runIndex,
1141 SkGlyphCache* cache, const SkPaint& skPaint,
1142 GrColor color,
1143 const SkMatrix& viewMatrix,
1144 const char text[], size_t byteLength,
1145 const SkScalar pos[], int scalarsPerPosition,
1146 const SkPoint& offset, const SkIRect& clipRect,
1147 SkScalar textRatio,
1148 SkTDArray<char>* fallbackTxt,
1149 SkTDArray<SkScalar>* fallbackPos) {
1150
1151 SkASSERT(byteLength == 0 || text != NULL);
1152 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1153
1154 // nothing to draw
1155 if (text == NULL || byteLength == 0) {
1156 return;
1157 }
1158
1159 fCurrStrike = NULL;
1160
1161 SkDrawCacheProc glyphCacheProc = skPaint.getDrawCacheProc();
1162 GrFontScaler* fontScaler = GetGrFontScaler(cache);
1163
1164 const char* stop = text + byteLength;
1165
1166 if (SkPaint::kLeft_Align == skPaint.getTextAlign()) {
1167 while (text < stop) {
1168 const char* lastText = text;
1169 // the last 2 parameters are ignored
1170 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1171
1172 if (glyph.fWidth) {
1173 SkScalar x = offset.x() + pos[0];
1174 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
1175
1176 if (!this->dfAppendGlyph(blob,
1177 runIndex,
1178 GrGlyph::Pack(glyph.getGlyphID(),
1179 glyph.getSubXFixed(),
1180 glyph.getSubYFixed(),
1181 GrGlyph::kDistance_MaskStyle),
1182 x, y, color, fontScaler, clipRect,
1183 textRatio, viewMatrix)) {
1184 // couldn't append, send to fallback
1185 fallbackTxt->append(SkToInt(text-lastText), lastText);
1186 *fallbackPos->append() = pos[0];
1187 if (2 == scalarsPerPosition) {
1188 *fallbackPos->append() = pos[1];
1189 }
1190 }
1191 }
1192 pos += scalarsPerPosition;
1193 }
1194 } else {
1195 SkScalar alignMul = SkPaint::kCenter_Align == skPaint.getTextAlign() ? SK_ScalarHalf
1196 : SK_Scalar1;
1197 while (text < stop) {
1198 const char* lastText = text;
1199 // the last 2 parameters are ignored
1200 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1201
1202 if (glyph.fWidth) {
1203 SkScalar x = offset.x() + pos[0];
1204 SkScalar y = offset.y() + (2 == scalarsPerPosition ? pos[1] : 0);
1205
1206 SkScalar advanceX = SkFixedToScalar(glyph.fAdvanceX) * alignMul * textRatio;
1207 SkScalar advanceY = SkFixedToScalar(glyph.fAdvanceY) * alignMul * textRatio;
1208
1209 if (!this->dfAppendGlyph(blob,
1210 runIndex,
1211 GrGlyph::Pack(glyph.getGlyphID(),
1212 glyph.getSubXFixed(),
1213 glyph.getSubYFixed(),
1214 GrGlyph::kDistance_MaskStyle),
1215 x - advanceX, y - advanceY, color,
1216 fontScaler,
1217 clipRect,
1218 textRatio,
1219 viewMatrix)) {
1220 // couldn't append, send to fallback
1221 fallbackTxt->append(SkToInt(text-lastText), lastText);
1222 *fallbackPos->append() = pos[0];
1223 if (2 == scalarsPerPosition) {
1224 *fallbackPos->append() = pos[1];
1225 }
1226 }
1227 }
1228 pos += scalarsPerPosition;
1229 }
1230 }
1231}
1232
1233void GrAtlasTextContext::bmpAppendGlyph(BitmapTextBlob* blob, int runIndex,
1234 GrGlyph::PackedID packed,
1235 int vx, int vy, GrColor color, GrFontScaler* scaler,
1236 const SkIRect& clipRect) {
joshualittae32c102015-04-21 09:37:57 -07001237 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001238 if (!fCurrStrike) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001239 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
joshualittae32c102015-04-21 09:37:57 -07001240 run.fStrike.reset(SkRef(fCurrStrike));
joshualitt1d89e8d2015-04-01 12:40:54 -07001241 }
1242
1243 GrGlyph* glyph = fCurrStrike->getGlyph(packed, scaler);
joshualitt010db532015-04-21 10:07:26 -07001244 if (!glyph) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001245 return;
1246 }
1247
1248 int x = vx + glyph->fBounds.fLeft;
1249 int y = vy + glyph->fBounds.fTop;
1250
1251 // keep them as ints until we've done the clip-test
1252 int width = glyph->fBounds.width();
1253 int height = glyph->fBounds.height();
1254
joshualitt2a0e9f32015-04-13 06:12:21 -07001255#if 0
1256 // Not checking the clip bounds might introduce a performance regression. However, its not
1257 // clear if this is still true today with the larger tiles we use in Chrome. For repositionable
1258 // blobs, we want to make sure we have all of the glyphs, so clipping them out is not ideal.
1259 // We could store the cliprect in the key, but then we'd lose the ability to do integer scrolls
1260 // TODO verify this
joshualitt1d89e8d2015-04-01 12:40:54 -07001261 // check if we clipped out
1262 if (clipRect.quickReject(x, y, x + width, y + height)) {
1263 return;
1264 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001265#endif
joshualitt1d89e8d2015-04-01 12:40:54 -07001266
1267 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001268 if (glyph->fTooLargeForAtlas) {
joshualitt19e4c022015-05-13 11:23:03 -07001269 this->appendGlyphPath(blob, glyph, scaler, SkIntToScalar(vx), SkIntToScalar(vy));
joshualitt1d89e8d2015-04-01 12:40:54 -07001270 return;
1271 }
1272
joshualitt1d89e8d2015-04-01 12:40:54 -07001273 GrMaskFormat format = glyph->fMaskFormat;
1274
1275 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
1276 if (run.fInitialized && subRun->fMaskFormat != format) {
joshualittfec19e12015-04-17 10:32:32 -07001277 subRun = &run.fSubRunInfo.push_back();
joshualitt1d89e8d2015-04-01 12:40:54 -07001278 }
1279
1280 run.fInitialized = true;
joshualitt1d89e8d2015-04-01 12:40:54 -07001281
1282 size_t vertexStride = get_vertex_stride(format);
1283
1284 SkRect r;
1285 r.fLeft = SkIntToScalar(x);
1286 r.fTop = SkIntToScalar(y);
1287 r.fRight = r.fLeft + SkIntToScalar(width);
1288 r.fBottom = r.fTop + SkIntToScalar(height);
joshualitt9bd2daf2015-04-17 09:30:06 -07001289 subRun->fMaskFormat = format;
1290 this->appendGlyphCommon(blob, &run, subRun, r, color, vertexStride, kA8_GrMaskFormat == format,
joshualittae32c102015-04-21 09:37:57 -07001291 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001292}
joshualitt1d89e8d2015-04-01 12:40:54 -07001293
joshualitt9bd2daf2015-04-17 09:30:06 -07001294bool GrAtlasTextContext::dfAppendGlyph(BitmapTextBlob* blob, int runIndex,
1295 GrGlyph::PackedID packed,
1296 SkScalar sx, SkScalar sy, GrColor color,
1297 GrFontScaler* scaler,
1298 const SkIRect& clipRect,
1299 SkScalar textRatio, const SkMatrix& viewMatrix) {
joshualittae32c102015-04-21 09:37:57 -07001300 Run& run = blob->fRuns[runIndex];
joshualitt9bd2daf2015-04-17 09:30:06 -07001301 if (!fCurrStrike) {
1302 fCurrStrike = fContext->getBatchFontCache()->getStrike(scaler);
joshualittae32c102015-04-21 09:37:57 -07001303 run.fStrike.reset(SkRef(fCurrStrike));
joshualitt9bd2daf2015-04-17 09:30:06 -07001304 }
1305
1306 GrGlyph* glyph = fCurrStrike->getGlyph(packed, scaler);
joshualitt010db532015-04-21 10:07:26 -07001307 if (!glyph) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001308 return true;
1309 }
1310
1311 // fallback to color glyph support
1312 if (kA8_GrMaskFormat != glyph->fMaskFormat) {
1313 return false;
1314 }
1315
1316 SkScalar dx = SkIntToScalar(glyph->fBounds.fLeft + SK_DistanceFieldInset);
1317 SkScalar dy = SkIntToScalar(glyph->fBounds.fTop + SK_DistanceFieldInset);
1318 SkScalar width = SkIntToScalar(glyph->fBounds.width() - 2 * SK_DistanceFieldInset);
1319 SkScalar height = SkIntToScalar(glyph->fBounds.height() - 2 * SK_DistanceFieldInset);
1320
1321 SkScalar scale = textRatio;
1322 dx *= scale;
1323 dy *= scale;
1324 width *= scale;
1325 height *= scale;
1326 sx += dx;
1327 sy += dy;
1328 SkRect glyphRect = SkRect::MakeXYWH(sx, sy, width, height);
1329
1330#if 0
1331 // check if we clipped out
1332 SkRect dstRect;
1333 viewMatrix.mapRect(&dstRect, glyphRect);
1334 if (clipRect.quickReject(SkScalarTruncToInt(dstRect.left()),
1335 SkScalarTruncToInt(dstRect.top()),
1336 SkScalarTruncToInt(dstRect.right()),
1337 SkScalarTruncToInt(dstRect.bottom()))) {
1338 return true;
1339 }
1340#endif
1341
1342 // TODO combine with the above
1343 // If the glyph is too large we fall back to paths
joshualitt010db532015-04-21 10:07:26 -07001344 if (glyph->fTooLargeForAtlas) {
joshualitt19e4c022015-05-13 11:23:03 -07001345 this->appendGlyphPath(blob, glyph, scaler, sx - dx, sy - dy);
joshualitt9bd2daf2015-04-17 09:30:06 -07001346 return true;
1347 }
1348
joshualitt9bd2daf2015-04-17 09:30:06 -07001349 PerSubRunInfo* subRun = &run.fSubRunInfo.back();
1350 SkASSERT(glyph->fMaskFormat == kA8_GrMaskFormat);
1351 subRun->fMaskFormat = kA8_GrMaskFormat;
1352
1353 size_t vertexStride = get_vertex_stride_df(kA8_GrMaskFormat, subRun->fUseLCDText);
1354
1355 bool useColorVerts = !subRun->fUseLCDText;
1356 this->appendGlyphCommon(blob, &run, subRun, glyphRect, color, vertexStride, useColorVerts,
joshualittae32c102015-04-21 09:37:57 -07001357 glyph);
joshualitt9bd2daf2015-04-17 09:30:06 -07001358 return true;
1359}
1360
1361inline void GrAtlasTextContext::appendGlyphPath(BitmapTextBlob* blob, GrGlyph* glyph,
joshualitt19e4c022015-05-13 11:23:03 -07001362 GrFontScaler* scaler, SkScalar x, SkScalar y) {
joshualitt9bd2daf2015-04-17 09:30:06 -07001363 if (NULL == glyph->fPath) {
1364 SkPath* path = SkNEW(SkPath);
1365 if (!scaler->getGlyphPath(glyph->glyphID(), path)) {
1366 // flag the glyph as being dead?
1367 SkDELETE(path);
1368 return;
1369 }
1370 glyph->fPath = path;
1371 }
1372 SkASSERT(glyph->fPath);
1373 blob->fBigGlyphs.push_back(BitmapTextBlob::BigGlyph(*glyph->fPath, x, y));
1374}
1375
1376inline void GrAtlasTextContext::appendGlyphCommon(BitmapTextBlob* blob, Run* run,
1377 Run::SubRunInfo* subRun,
1378 const SkRect& positions, GrColor color,
1379 size_t vertexStride, bool useVertexColor,
joshualittae32c102015-04-21 09:37:57 -07001380 GrGlyph* glyph) {
1381 blob->fGlyphs[subRun->fGlyphEndIndex] = glyph;
joshualitt9bd2daf2015-04-17 09:30:06 -07001382 run->fVertexBounds.joinNonEmptyArg(positions);
1383 run->fColor = color;
joshualitt1d89e8d2015-04-01 12:40:54 -07001384
1385 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->fVertexEndIndex);
1386
joshualitt9bd2daf2015-04-17 09:30:06 -07001387 if (useVertexColor) {
joshualitt010db532015-04-21 10:07:26 -07001388 // V0
1389 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1390 position->set(positions.fLeft, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001391 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
1392 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001393 vertex += vertexStride;
joshualitt9bd2daf2015-04-17 09:30:06 -07001394
joshualitt010db532015-04-21 10:07:26 -07001395 // V1
1396 position = reinterpret_cast<SkPoint*>(vertex);
1397 position->set(positions.fLeft, positions.fBottom);
1398 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001399 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001400 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001401
joshualitt010db532015-04-21 10:07:26 -07001402 // V2
1403 position = reinterpret_cast<SkPoint*>(vertex);
1404 position->set(positions.fRight, positions.fBottom);
1405 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001406 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001407 vertex += vertexStride;
joshualitt1d89e8d2015-04-01 12:40:54 -07001408
joshualitt010db532015-04-21 10:07:26 -07001409 // V3
1410 position = reinterpret_cast<SkPoint*>(vertex);
1411 position->set(positions.fRight, positions.fTop);
1412 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
joshualitt1d89e8d2015-04-01 12:40:54 -07001413 *colorPtr = color;
joshualitt010db532015-04-21 10:07:26 -07001414 } else {
1415 // V0
1416 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
1417 position->set(positions.fLeft, positions.fTop);
1418 vertex += vertexStride;
1419
1420 // V1
1421 position = reinterpret_cast<SkPoint*>(vertex);
1422 position->set(positions.fLeft, positions.fBottom);
1423 vertex += vertexStride;
1424
1425 // V2
1426 position = reinterpret_cast<SkPoint*>(vertex);
1427 position->set(positions.fRight, positions.fBottom);
1428 vertex += vertexStride;
1429
1430 // V3
1431 position = reinterpret_cast<SkPoint*>(vertex);
1432 position->set(positions.fRight, positions.fTop);
joshualitt1d89e8d2015-04-01 12:40:54 -07001433 }
1434
1435 subRun->fGlyphEndIndex++;
1436 subRun->fVertexEndIndex += vertexStride * kVerticesPerGlyph;
1437}
1438
1439class BitmapTextBatch : public GrBatch {
1440public:
joshualitt9bd2daf2015-04-17 09:30:06 -07001441 typedef GrAtlasTextContext::DistanceAdjustTable DistanceAdjustTable;
joshualittdbd35932015-04-02 09:19:04 -07001442 typedef GrAtlasTextContext::BitmapTextBlob Blob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001443 typedef Blob::Run Run;
1444 typedef Run::SubRunInfo TextInfo;
1445 struct Geometry {
joshualittad802c62015-04-15 05:31:57 -07001446 Blob* fBlob;
joshualitt1d89e8d2015-04-01 12:40:54 -07001447 int fRun;
1448 int fSubRun;
1449 GrColor fColor;
joshualitt2a0e9f32015-04-13 06:12:21 -07001450 SkScalar fTransX;
1451 SkScalar fTransY;
joshualitt1d89e8d2015-04-01 12:40:54 -07001452 };
1453
joshualittad802c62015-04-15 05:31:57 -07001454 static BitmapTextBatch* Create(GrMaskFormat maskFormat, int glyphCount,
1455 GrBatchFontCache* fontCache) {
1456 return SkNEW_ARGS(BitmapTextBatch, (maskFormat, glyphCount, fontCache));
joshualitt1d89e8d2015-04-01 12:40:54 -07001457 }
1458
joshualitt9bd2daf2015-04-17 09:30:06 -07001459 static BitmapTextBatch* Create(GrMaskFormat maskFormat, int glyphCount,
1460 GrBatchFontCache* fontCache,
1461 DistanceAdjustTable* distanceAdjustTable,
1462 SkColor filteredColor, bool useLCDText,
1463 bool useBGR, float gamma) {
1464 return SkNEW_ARGS(BitmapTextBatch, (maskFormat, glyphCount, fontCache, distanceAdjustTable,
1465 filteredColor, useLCDText, useBGR, gamma));
1466 }
1467
joshualitt1d89e8d2015-04-01 12:40:54 -07001468 const char* name() const override { return "BitmapTextBatch"; }
1469
1470 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
1471 if (kARGB_GrMaskFormat == fMaskFormat) {
1472 out->setUnknownFourComponents();
1473 } else {
1474 out->setKnownFourComponents(fBatch.fColor);
1475 }
1476 }
1477
1478 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt9bd2daf2015-04-17 09:30:06 -07001479 if (!fUseDistanceFields) {
1480 // Bitmap Text
1481 if (kARGB_GrMaskFormat != fMaskFormat) {
1482 if (GrPixelConfigIsAlphaOnly(fPixelConfig)) {
1483 out->setUnknownSingleComponent();
1484 } else if (GrPixelConfigIsOpaque(fPixelConfig)) {
1485 out->setUnknownOpaqueFourComponents();
1486 out->setUsingLCDCoverage();
1487 } else {
1488 out->setUnknownFourComponents();
1489 out->setUsingLCDCoverage();
1490 }
1491 } else {
1492 out->setKnownSingleComponent(0xff);
1493 }
1494 } else {
1495 // Distance fields
1496 if (!fUseLCDText) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001497 out->setUnknownSingleComponent();
joshualitt1d89e8d2015-04-01 12:40:54 -07001498 } else {
1499 out->setUnknownFourComponents();
1500 out->setUsingLCDCoverage();
1501 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001502 }
1503 }
1504
1505 void initBatchTracker(const GrPipelineInfo& init) override {
1506 // Handle any color overrides
1507 if (init.fColorIgnored) {
1508 fBatch.fColor = GrColor_ILLEGAL;
1509 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
1510 fBatch.fColor = init.fOverrideColor;
1511 }
1512
1513 // setup batch properties
1514 fBatch.fColorIgnored = init.fColorIgnored;
1515 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
1516 fBatch.fCoverageIgnored = init.fCoverageIgnored;
1517 }
1518
bsalomonb5238a72015-05-05 07:49:49 -07001519 struct FlushInfo {
1520 SkAutoTUnref<const GrVertexBuffer> fVertexBuffer;
1521 SkAutoTUnref<const GrIndexBuffer> fIndexBuffer;
1522 int fGlyphsToFlush;
1523 int fVertexOffset;
1524 };
1525
joshualitt1d89e8d2015-04-01 12:40:54 -07001526 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
1527 // if we have RGB, then we won't have any SkShaders so no need to use a localmatrix.
1528 // TODO actually only invert if we don't have RGBA
1529 SkMatrix localMatrix;
1530 if (this->usesLocalCoords() && !this->viewMatrix().invert(&localMatrix)) {
1531 SkDebugf("Cannot invert viewmatrix\n");
1532 return;
1533 }
1534
joshualitt62db8ba2015-04-09 08:22:37 -07001535 GrTexture* texture = fFontCache->getTexture(fMaskFormat);
1536 if (!texture) {
1537 SkDebugf("Could not allocate backing texture for atlas\n");
1538 return;
1539 }
1540
joshualitt9bd2daf2015-04-17 09:30:06 -07001541 SkAutoTUnref<const GrGeometryProcessor> gp;
1542 if (fUseDistanceFields) {
1543 gp.reset(this->setupDfProcessor(this->viewMatrix(), fFilteredColor, this->color(),
1544 texture));
1545 } else {
1546 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
joshualitt9bd2daf2015-04-17 09:30:06 -07001547 gp.reset(GrBitmapTextGeoProc::Create(this->color(),
1548 texture,
1549 params,
1550 fMaskFormat,
joshualittb8c241a2015-05-19 08:23:30 -07001551 localMatrix,
1552 this->usesLocalCoords()));
joshualitt9bd2daf2015-04-17 09:30:06 -07001553 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001554
bsalomonb5238a72015-05-05 07:49:49 -07001555 FlushInfo flushInfo;
1556 flushInfo.fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001557 size_t vertexStride = gp->getVertexStride();
joshualitt9bd2daf2015-04-17 09:30:06 -07001558 SkASSERT(vertexStride == (fUseDistanceFields ?
1559 get_vertex_stride_df(fMaskFormat, fUseLCDText) :
1560 get_vertex_stride(fMaskFormat)));
joshualitt1d89e8d2015-04-01 12:40:54 -07001561
joshualittb8c241a2015-05-19 08:23:30 -07001562 batchTarget->initDraw(gp, pipeline);
joshualitt1d89e8d2015-04-01 12:40:54 -07001563
1564 int glyphCount = this->numGlyphs();
joshualittad802c62015-04-15 05:31:57 -07001565 int instanceCount = fInstanceCount;
bsalomon8415abe2015-05-04 11:41:41 -07001566 const GrVertexBuffer* vertexBuffer;
bsalomonb5238a72015-05-05 07:49:49 -07001567
robertphillipse40d3972015-05-07 09:51:43 -07001568 void* vertices = batchTarget->makeVertSpace(vertexStride,
1569 glyphCount * kVerticesPerGlyph,
1570 &vertexBuffer,
1571 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -07001572 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
1573 flushInfo.fIndexBuffer.reset(batchTarget->resourceProvider()->refQuadIndexBuffer());
1574 if (!vertices || !flushInfo.fVertexBuffer) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001575 SkDebugf("Could not allocate vertices\n");
1576 return;
1577 }
1578
1579 unsigned char* currVertex = reinterpret_cast<unsigned char*>(vertices);
1580
joshualitt25ba7ea2015-04-21 07:49:49 -07001581 // We cache some values to avoid going to the glyphcache for the same fontScaler twice
1582 // in a row
1583 const SkDescriptor* desc = NULL;
1584 SkGlyphCache* cache = NULL;
1585 GrFontScaler* scaler = NULL;
joshualitt25ba7ea2015-04-21 07:49:49 -07001586 SkTypeface* typeface = NULL;
1587
joshualitt1d89e8d2015-04-01 12:40:54 -07001588 for (int i = 0; i < instanceCount; i++) {
1589 Geometry& args = fGeoData[i];
1590 Blob* blob = args.fBlob;
1591 Run& run = blob->fRuns[args.fRun];
1592 TextInfo& info = run.fSubRunInfo[args.fSubRun];
1593
1594 uint64_t currentAtlasGen = fFontCache->atlasGeneration(fMaskFormat);
joshualitt7a9c45c2015-05-26 12:32:23 -07001595 bool regenerateTextureCoords = info.fAtlasGeneration != currentAtlasGen ||
1596 run.fStrike->isAbandoned();
joshualitt9bd2daf2015-04-17 09:30:06 -07001597 bool regenerateColors;
1598 if (fUseDistanceFields) {
joshualittfcfb9fc2015-04-21 07:35:10 -07001599 regenerateColors = !fUseLCDText && run.fColor != args.fColor;
joshualitt9bd2daf2015-04-17 09:30:06 -07001600 } else {
1601 regenerateColors = kA8_GrMaskFormat == fMaskFormat && run.fColor != args.fColor;
1602 }
joshualitt2a0e9f32015-04-13 06:12:21 -07001603 bool regeneratePositions = args.fTransX != 0.f || args.fTransY != 0.f;
joshualitt1d89e8d2015-04-01 12:40:54 -07001604 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
1605
1606 // We regenerate both texture coords and colors in the blob itself, and update the
1607 // atlas generation. If we don't end up purging any unused plots, we can avoid
1608 // regenerating the coords. We could take a finer grained approach to updating texture
1609 // coords but its not clear if the extra bookkeeping would offset any gains.
1610 // To avoid looping over the glyphs twice, we do one loop and conditionally update color
1611 // or coords as needed. One final note, if we have to break a run for an atlas eviction
1612 // then we can't really trust the atlas has all of the correct data. Atlas evictions
1613 // should be pretty rare, so we just always regenerate in those cases
joshualitt2a0e9f32015-04-13 06:12:21 -07001614 if (regenerateTextureCoords || regenerateColors || regeneratePositions) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001615 // first regenerate texture coordinates / colors if need be
joshualitt1d89e8d2015-04-01 12:40:54 -07001616 bool brokenRun = false;
joshualittae32c102015-04-21 09:37:57 -07001617
1618 // Because the GrBatchFontCache may evict the strike a blob depends on using for
1619 // generating its texture coords, we have to track whether or not the strike has
1620 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
1621 // otherwise we have to get the new strike, and use that to get the correct glyphs.
1622 // Because we do not have the packed ids, and thus can't look up our glyphs in the
1623 // new strike, we instead keep our ref to the old strike and use the packed ids from
1624 // it. These ids will still be valid as long as we hold the ref. When we are done
1625 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
1626 bool regenerateGlyphs = false;
1627 GrBatchTextStrike* strike = NULL;
joshualitt1d89e8d2015-04-01 12:40:54 -07001628 if (regenerateTextureCoords) {
joshualittb4c507e2015-04-08 08:07:59 -07001629 info.fBulkUseToken.reset();
joshualitt25ba7ea2015-04-21 07:49:49 -07001630
1631 // We can reuse if we have a valid strike and our descriptors / typeface are the
1632 // same
joshualitt97202d22015-04-22 13:47:02 -07001633 const SkDescriptor* newDesc = run.fOverrideDescriptor ?
1634 run.fOverrideDescriptor->getDesc() :
joshualitt25ba7ea2015-04-21 07:49:49 -07001635 run.fDescriptor.getDesc();
1636 if (!cache || !SkTypeface::Equal(typeface, run.fTypeface) ||
1637 !(desc->equals(*newDesc))) {
1638 if (cache) {
1639 SkGlyphCache::AttachCache(cache);
1640 }
1641 desc = newDesc;
1642 cache = SkGlyphCache::DetachCache(run.fTypeface, desc);
1643 scaler = GrTextContext::GetGrFontScaler(cache);
joshualittae32c102015-04-21 09:37:57 -07001644 strike = run.fStrike;
joshualitt25ba7ea2015-04-21 07:49:49 -07001645 typeface = run.fTypeface;
1646 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001647
joshualittae32c102015-04-21 09:37:57 -07001648 if (run.fStrike->isAbandoned()) {
1649 regenerateGlyphs = true;
1650 strike = fFontCache->getStrike(scaler);
1651 } else {
1652 strike = run.fStrike;
1653 }
1654 }
1655
1656 for (int glyphIdx = 0; glyphIdx < glyphCount; glyphIdx++) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001657 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001658 size_t glyphOffset = glyphIdx + info.fGlyphStartIndex;
1659 GrGlyph* glyph;
1660 if (regenerateGlyphs) {
1661 // Get the id from the old glyph, and use the new strike to lookup
1662 // the glyph.
1663 glyph = blob->fGlyphs[glyphOffset];
1664 blob->fGlyphs[glyphOffset] = strike->getGlyph(glyph->fPackedID,
1665 scaler);
1666 }
1667 glyph = blob->fGlyphs[glyphOffset];
joshualitt1d89e8d2015-04-01 12:40:54 -07001668 SkASSERT(glyph);
1669
1670 if (!fFontCache->hasGlyph(glyph) &&
1671 !strike->addGlyphToAtlas(batchTarget, glyph, scaler)) {
bsalomonb5238a72015-05-05 07:49:49 -07001672 this->flush(batchTarget, &flushInfo);
joshualittb8c241a2015-05-19 08:23:30 -07001673 batchTarget->initDraw(gp, pipeline);
joshualitt1d89e8d2015-04-01 12:40:54 -07001674 brokenRun = glyphIdx > 0;
1675
joshualittae32c102015-04-21 09:37:57 -07001676 SkDEBUGCODE(bool success =) strike->addGlyphToAtlas(batchTarget,
1677 glyph,
joshualitt1d89e8d2015-04-01 12:40:54 -07001678 scaler);
1679 SkASSERT(success);
1680 }
joshualittb4c507e2015-04-08 08:07:59 -07001681 fFontCache->addGlyphToBulkAndSetUseToken(&info.fBulkUseToken, glyph,
1682 batchTarget->currentToken());
joshualitt1d89e8d2015-04-01 12:40:54 -07001683
1684 // Texture coords are the last vertex attribute so we get a pointer to the
1685 // first one and then map with stride in regenerateTextureCoords
1686 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1687 vertex += info.fVertexStartIndex;
1688 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1689 vertex += vertexStride - sizeof(SkIPoint16);
1690
1691 this->regenerateTextureCoords(glyph, vertex, vertexStride);
1692 }
1693
1694 if (regenerateColors) {
1695 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1696 vertex += info.fVertexStartIndex;
1697 vertex += vertexStride * glyphIdx * kVerticesPerGlyph + sizeof(SkPoint);
1698 this->regenerateColors(vertex, vertexStride, args.fColor);
1699 }
1700
joshualitt2a0e9f32015-04-13 06:12:21 -07001701 if (regeneratePositions) {
1702 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices);
1703 vertex += info.fVertexStartIndex;
1704 vertex += vertexStride * glyphIdx * kVerticesPerGlyph;
1705 SkScalar transX = args.fTransX;
1706 SkScalar transY = args.fTransY;
1707 this->regeneratePositions(vertex, vertexStride, transX, transY);
1708 }
bsalomonb5238a72015-05-05 07:49:49 -07001709 flushInfo.fGlyphsToFlush++;
joshualitt1d89e8d2015-04-01 12:40:54 -07001710 }
1711
joshualitt2a0e9f32015-04-13 06:12:21 -07001712 // We my have changed the color so update it here
1713 run.fColor = args.fColor;
joshualitt1d89e8d2015-04-01 12:40:54 -07001714 if (regenerateTextureCoords) {
joshualittae32c102015-04-21 09:37:57 -07001715 if (regenerateGlyphs) {
1716 run.fStrike.reset(SkRef(strike));
1717 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001718 info.fAtlasGeneration = brokenRun ? GrBatchAtlas::kInvalidAtlasGeneration :
1719 fFontCache->atlasGeneration(fMaskFormat);
1720 }
1721 } else {
bsalomonb5238a72015-05-05 07:49:49 -07001722 flushInfo.fGlyphsToFlush += glyphCount;
joshualittb4c507e2015-04-08 08:07:59 -07001723
1724 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1725 // have a valid atlas generation
1726 fFontCache->setUseTokenBulk(info.fBulkUseToken,
1727 batchTarget->currentToken(),
1728 fMaskFormat);
joshualitt1d89e8d2015-04-01 12:40:54 -07001729 }
1730
1731 // now copy all vertices
1732 size_t byteCount = info.fVertexEndIndex - info.fVertexStartIndex;
1733 memcpy(currVertex, blob->fVertices + info.fVertexStartIndex, byteCount);
1734
1735 currVertex += byteCount;
1736 }
joshualitt25ba7ea2015-04-21 07:49:49 -07001737 // Make sure to attach the last cache if applicable
1738 if (cache) {
1739 SkGlyphCache::AttachCache(cache);
1740 }
bsalomonb5238a72015-05-05 07:49:49 -07001741 this->flush(batchTarget, &flushInfo);
joshualitt1d89e8d2015-04-01 12:40:54 -07001742 }
1743
joshualittad802c62015-04-15 05:31:57 -07001744 // The minimum number of Geometry we will try to allocate.
1745 static const int kMinAllocated = 32;
1746
1747 // Total number of Geometry this Batch owns
1748 int instanceCount() const { return fInstanceCount; }
1749 SkAutoSTMalloc<kMinAllocated, Geometry>* geoData() { return &fGeoData; }
1750
1751 // to avoid even the initial copy of the struct, we have a getter for the first item which
1752 // is used to seed the batch with its initial geometry. After seeding, the client should call
1753 // init() so the Batch can initialize itself
1754 Geometry& geometry() { return fGeoData[0]; }
1755 void init() {
joshualitt444987f2015-05-06 06:46:01 -07001756 const Geometry& geo = fGeoData[0];
1757 fBatch.fColor = geo.fColor;
1758 fBatch.fViewMatrix = geo.fBlob->fViewMatrix;
1759
1760 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
1761 // into device space
1762 const Run& run = geo.fBlob->fRuns[geo.fRun];
1763 if (run.fSubRunInfo[geo.fSubRun].fDrawAsDistanceFields) {
1764 SkRect bounds = run.fVertexBounds;
1765 fBatch.fViewMatrix.mapRect(&bounds);
1766 this->setBounds(bounds);
1767 } else {
1768 this->setBounds(run.fVertexBounds);
1769 }
joshualittad802c62015-04-15 05:31:57 -07001770 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001771
1772private:
joshualitt9bd2daf2015-04-17 09:30:06 -07001773 BitmapTextBatch(GrMaskFormat maskFormat, int glyphCount, GrBatchFontCache* fontCache)
joshualitt1d89e8d2015-04-01 12:40:54 -07001774 : fMaskFormat(maskFormat)
1775 , fPixelConfig(fontCache->getPixelConfig(maskFormat))
joshualitt9bd2daf2015-04-17 09:30:06 -07001776 , fFontCache(fontCache)
1777 , fUseDistanceFields(false) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001778 this->initClassID<BitmapTextBatch>();
joshualitt1d89e8d2015-04-01 12:40:54 -07001779 fBatch.fNumGlyphs = glyphCount;
joshualittad802c62015-04-15 05:31:57 -07001780 fInstanceCount = 1;
1781 fAllocatedCount = kMinAllocated;
1782 }
1783
joshualitt9bd2daf2015-04-17 09:30:06 -07001784 BitmapTextBatch(GrMaskFormat maskFormat, int glyphCount, GrBatchFontCache* fontCache,
1785 DistanceAdjustTable* distanceAdjustTable, SkColor filteredColor,
1786 bool useLCDText, bool useBGR, float gamma)
1787 : fMaskFormat(maskFormat)
1788 , fPixelConfig(fontCache->getPixelConfig(maskFormat))
1789 , fFontCache(fontCache)
1790 , fDistanceAdjustTable(SkRef(distanceAdjustTable))
1791 , fFilteredColor(filteredColor)
1792 , fUseDistanceFields(true)
1793 , fUseLCDText(useLCDText)
1794 , fUseBGR(useBGR)
1795 , fGamma(gamma) {
1796 this->initClassID<BitmapTextBatch>();
1797 fBatch.fNumGlyphs = glyphCount;
1798 fInstanceCount = 1;
1799 fAllocatedCount = kMinAllocated;
1800 SkASSERT(fMaskFormat == kA8_GrMaskFormat);
1801 }
1802
joshualittad802c62015-04-15 05:31:57 -07001803 ~BitmapTextBatch() {
1804 for (int i = 0; i < fInstanceCount; i++) {
1805 fGeoData[i].fBlob->unref();
1806 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001807 }
1808
1809 void regenerateTextureCoords(GrGlyph* glyph, intptr_t vertex, size_t vertexStride) {
1810 int width = glyph->fBounds.width();
1811 int height = glyph->fBounds.height();
joshualitt1d89e8d2015-04-01 12:40:54 -07001812
joshualitt9bd2daf2015-04-17 09:30:06 -07001813 int u0, v0, u1, v1;
1814 if (fUseDistanceFields) {
1815 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
1816 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
1817 u1 = u0 + width - 2 * SK_DistanceFieldInset;
1818 v1 = v0 + height - 2 * SK_DistanceFieldInset;
1819 } else {
1820 u0 = glyph->fAtlasLocation.fX;
1821 v0 = glyph->fAtlasLocation.fY;
1822 u1 = u0 + width;
1823 v1 = v0 + height;
1824 }
1825
joshualitt1d89e8d2015-04-01 12:40:54 -07001826 SkIPoint16* textureCoords;
1827 // V0
1828 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1829 textureCoords->set(u0, v0);
1830 vertex += vertexStride;
1831
1832 // V1
1833 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1834 textureCoords->set(u0, v1);
1835 vertex += vertexStride;
1836
1837 // V2
1838 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1839 textureCoords->set(u1, v1);
1840 vertex += vertexStride;
1841
1842 // V3
1843 textureCoords = reinterpret_cast<SkIPoint16*>(vertex);
1844 textureCoords->set(u1, v0);
1845 }
1846
1847 void regenerateColors(intptr_t vertex, size_t vertexStride, GrColor color) {
1848 for (int i = 0; i < kVerticesPerGlyph; i++) {
1849 SkColor* vcolor = reinterpret_cast<SkColor*>(vertex);
1850 *vcolor = color;
1851 vertex += vertexStride;
1852 }
1853 }
1854
joshualitt2a0e9f32015-04-13 06:12:21 -07001855 void regeneratePositions(intptr_t vertex, size_t vertexStride, SkScalar transX,
1856 SkScalar transY) {
1857 for (int i = 0; i < kVerticesPerGlyph; i++) {
1858 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
1859 point->fX += transX;
1860 point->fY += transY;
1861 vertex += vertexStride;
1862 }
1863 }
1864
bsalomonb5238a72015-05-05 07:49:49 -07001865 void flush(GrBatchTarget* batchTarget, FlushInfo* flushInfo) {
bsalomoncb8979d2015-05-05 09:51:38 -07001866 GrVertices vertices;
bsalomonb5238a72015-05-05 07:49:49 -07001867 int maxGlyphsPerDraw = flushInfo->fIndexBuffer->maxQuads();
bsalomoncb8979d2015-05-05 09:51:38 -07001868 vertices.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
bsalomonb5238a72015-05-05 07:49:49 -07001869 flushInfo->fIndexBuffer, flushInfo->fVertexOffset,
bsalomone64eb572015-05-07 11:35:55 -07001870 kVerticesPerGlyph, kIndicesPerGlyph, flushInfo->fGlyphsToFlush,
bsalomonb5238a72015-05-05 07:49:49 -07001871 maxGlyphsPerDraw);
bsalomone64eb572015-05-07 11:35:55 -07001872 batchTarget->draw(vertices);
bsalomonb5238a72015-05-05 07:49:49 -07001873 flushInfo->fVertexOffset += kVerticesPerGlyph * flushInfo->fGlyphsToFlush;
1874 flushInfo->fGlyphsToFlush = 0;
joshualitt1d89e8d2015-04-01 12:40:54 -07001875 }
1876
1877 GrColor color() const { return fBatch.fColor; }
1878 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1879 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1880 int numGlyphs() const { return fBatch.fNumGlyphs; }
1881
1882 bool onCombineIfPossible(GrBatch* t) override {
1883 BitmapTextBatch* that = t->cast<BitmapTextBatch>();
1884
joshualitt9bd2daf2015-04-17 09:30:06 -07001885 if (fUseDistanceFields != that->fUseDistanceFields) {
joshualitt1d89e8d2015-04-01 12:40:54 -07001886 return false;
1887 }
1888
joshualitt9bd2daf2015-04-17 09:30:06 -07001889 if (!fUseDistanceFields) {
1890 // Bitmap Text
1891 if (fMaskFormat != that->fMaskFormat) {
1892 return false;
1893 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001894
joshualitt9bd2daf2015-04-17 09:30:06 -07001895 // TODO we can often batch across LCD text if we have dual source blending and don't
1896 // have to use the blend constant
1897 if (fMaskFormat != kA8_GrMaskFormat && this->color() != that->color()) {
1898 return false;
1899 }
1900
1901 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1902 return false;
1903 }
1904 } else {
1905 // Distance Fields
1906 SkASSERT(this->fMaskFormat == that->fMaskFormat &&
1907 this->fMaskFormat == kA8_GrMaskFormat);
1908
1909 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1910 return false;
1911 }
1912
1913 if (fFilteredColor != that->fFilteredColor) {
1914 return false;
1915 }
1916
1917 if (fUseLCDText != that->fUseLCDText) {
1918 return false;
1919 }
1920
1921 if (fUseBGR != that->fUseBGR) {
1922 return false;
1923 }
1924
1925 if (fGamma != that->fGamma) {
1926 return false;
1927 }
1928
1929 // TODO see note above
1930 if (fUseLCDText && this->color() != that->color()) {
1931 return false;
1932 }
joshualitt1d89e8d2015-04-01 12:40:54 -07001933 }
1934
1935 fBatch.fNumGlyphs += that->numGlyphs();
joshualittad802c62015-04-15 05:31:57 -07001936
1937 // copy that->geoData(). We do this manually for performance reasons
1938 SkAutoSTMalloc<kMinAllocated, Geometry>* otherGeoData = that->geoData();
1939 int otherInstanceCount = that->instanceCount();
1940 int allocSize = otherInstanceCount + fInstanceCount;
1941 if (allocSize > fAllocatedCount) {
1942 while (allocSize > fAllocatedCount) {
1943 fAllocatedCount = fAllocatedCount << 1;
1944 }
1945 fGeoData.realloc(fAllocatedCount);
1946 }
1947
1948 memcpy(&fGeoData[fInstanceCount], otherGeoData->get(),
1949 otherInstanceCount * sizeof(Geometry));
1950 int total = fInstanceCount + otherInstanceCount;
1951 for (int i = fInstanceCount; i < total; i++) {
1952 fGeoData[i].fBlob->ref();
1953 }
1954 fInstanceCount = total;
joshualitt99c7c072015-05-01 13:43:30 -07001955
1956 this->joinBounds(that->bounds());
joshualitt1d89e8d2015-04-01 12:40:54 -07001957 return true;
1958 }
1959
joshualitt9bd2daf2015-04-17 09:30:06 -07001960 // TODO just use class params
1961 // TODO trying to figure out why lcd is so whack
1962 GrGeometryProcessor* setupDfProcessor(const SkMatrix& viewMatrix, SkColor filteredColor,
1963 GrColor color, GrTexture* texture) {
1964 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode);
1965
1966 // set up any flags
1967 uint32_t flags = 0;
1968 flags |= viewMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
1969 flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
1970 flags |= fUseLCDText && viewMatrix.rectStaysRect() ?
1971 kRectToRect_DistanceFieldEffectFlag : 0;
1972 flags |= fUseLCDText && fUseBGR ? kBGR_DistanceFieldEffectFlag : 0;
1973
1974 // see if we need to create a new effect
1975 if (fUseLCDText) {
1976 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
1999 U8CPU lum = SkColorSpaceLuminance::computeLuminance(fGamma, filteredColor);
2000 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;
joshualitt1d89e8d2015-04-01 12:40:54 -07002033 GrMaskFormat fMaskFormat;
2034 GrPixelConfig fPixelConfig;
2035 GrBatchFontCache* fFontCache;
joshualitt9bd2daf2015-04-17 09:30:06 -07002036
2037 // Distance field properties
2038 SkAutoTUnref<DistanceAdjustTable> fDistanceAdjustTable;
2039 SkColor fFilteredColor;
2040 bool fUseDistanceFields;
2041 bool fUseLCDText;
2042 bool fUseBGR;
2043 float fGamma;
joshualitt1d89e8d2015-04-01 12:40:54 -07002044};
2045
robertphillips9c240a12015-05-28 07:45:59 -07002046void GrAtlasTextContext::flushRunAsPaths(GrDrawContext* drawContext,
robertphillipsccb1b572015-05-27 11:02:55 -07002047 GrRenderTarget* rt, const SkTextBlob::RunIterator& it,
2048 const GrClip& clip, const SkPaint& skPaint,
joshualitt9a27e632015-04-06 10:53:36 -07002049 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
2050 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
2051 SkPaint runPaint = skPaint;
joshualitt1d89e8d2015-04-01 12:40:54 -07002052
joshualitt9a27e632015-04-06 10:53:36 -07002053 size_t textLen = it.glyphCount() * sizeof(uint16_t);
2054 const SkPoint& offset = it.offset();
joshualitt1d89e8d2015-04-01 12:40:54 -07002055
joshualitt9a27e632015-04-06 10:53:36 -07002056 it.applyFontToPaint(&runPaint);
joshualitt1d89e8d2015-04-01 12:40:54 -07002057
joshualitt9a27e632015-04-06 10:53:36 -07002058 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
2059 return;
joshualitt1d89e8d2015-04-01 12:40:54 -07002060 }
2061
robertphillips9c240a12015-05-28 07:45:59 -07002062 runPaint.setFlags(FilterTextFlags(fDeviceProperties, runPaint));
joshualitt9a27e632015-04-06 10:53:36 -07002063
2064 switch (it.positioning()) {
2065 case SkTextBlob::kDefault_Positioning:
robertphillipsccb1b572015-05-27 11:02:55 -07002066 this->drawTextAsPath(drawContext, rt, clip, runPaint, viewMatrix,
2067 (const char *)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002068 textLen, x + offset.x(), y + offset.y(), clipBounds);
2069 break;
2070 case SkTextBlob::kHorizontal_Positioning:
robertphillipsccb1b572015-05-27 11:02:55 -07002071 this->drawPosTextAsPath(drawContext, rt, clip, runPaint, viewMatrix,
2072 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002073 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
2074 clipBounds);
2075 break;
2076 case SkTextBlob::kFull_Positioning:
robertphillipsccb1b572015-05-27 11:02:55 -07002077 this->drawPosTextAsPath(drawContext, rt, clip, runPaint, viewMatrix,
2078 (const char*)it.glyphs(),
joshualitt9a27e632015-04-06 10:53:36 -07002079 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
2080 break;
2081 }
2082}
2083
joshualitt79dfb2b2015-05-11 08:58:08 -07002084
2085inline BitmapTextBatch*
2086GrAtlasTextContext::createBatch(BitmapTextBlob* cacheBlob, const PerSubRunInfo& info,
2087 int glyphCount, int run, int subRun,
2088 GrColor color, SkScalar transX, SkScalar transY,
2089 const SkPaint& skPaint) {
2090 GrMaskFormat format = info.fMaskFormat;
2091 GrColor subRunColor;
2092 if (kARGB_GrMaskFormat == format) {
2093 uint8_t paintAlpha = skPaint.getAlpha();
2094 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
2095 } else {
2096 subRunColor = color;
2097 }
2098
2099 BitmapTextBatch* batch;
2100 if (info.fDrawAsDistanceFields) {
2101 SkColor filteredColor;
2102 SkColorFilter* colorFilter = skPaint.getColorFilter();
2103 if (colorFilter) {
2104 filteredColor = colorFilter->filterColor(skPaint.getColor());
2105 } else {
2106 filteredColor = skPaint.getColor();
2107 }
2108 bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.pixelGeometry());
2109 float gamma = fDeviceProperties.gamma();
2110 batch = BitmapTextBatch::Create(format, glyphCount, fContext->getBatchFontCache(),
2111 fDistanceAdjustTable, filteredColor,
2112 info.fUseLCDText, useBGR,
2113 gamma);
2114 } else {
2115 batch = BitmapTextBatch::Create(format, glyphCount, fContext->getBatchFontCache());
2116 }
2117 BitmapTextBatch::Geometry& geometry = batch->geometry();
2118 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
robertphillipsea461502015-05-26 11:38:03 -07002129inline void GrAtlasTextContext::flushRun(GrDrawContext* drawContext,
2130 GrPipelineBuilder* pipelineBuilder,
joshualitt9a27e632015-04-06 10:53:36 -07002131 BitmapTextBlob* cacheBlob, int run, GrColor color,
robertphillipsea461502015-05-26 11:38:03 -07002132 SkScalar transX, SkScalar transY,
2133 const SkPaint& skPaint) {
joshualitt9a27e632015-04-06 10:53:36 -07002134 for (int subRun = 0; subRun < cacheBlob->fRuns[run].fSubRunInfo.count(); subRun++) {
2135 const PerSubRunInfo& info = cacheBlob->fRuns[run].fSubRunInfo[subRun];
2136 int glyphCount = info.fGlyphEndIndex - info.fGlyphStartIndex;
2137 if (0 == glyphCount) {
2138 continue;
2139 }
2140
joshualitt79dfb2b2015-05-11 08:58:08 -07002141 SkAutoTUnref<BitmapTextBatch> batch(this->createBatch(cacheBlob, info, glyphCount, run,
2142 subRun, color, transX, transY,
2143 skPaint));
robertphillipsea461502015-05-26 11:38:03 -07002144 drawContext->drawText(pipelineBuilder, batch);
joshualitt9a27e632015-04-06 10:53:36 -07002145 }
2146}
2147
robertphillipsccb1b572015-05-27 11:02:55 -07002148inline void GrAtlasTextContext::flushBigGlyphs(BitmapTextBlob* cacheBlob,
2149 GrDrawContext* drawContext, GrRenderTarget* rt,
2150 const GrClip& clip, const SkPaint& skPaint,
joshualitt1107e902015-05-11 14:52:11 -07002151 SkScalar transX, SkScalar transY,
2152 const SkIRect& clipBounds) {
joshualittfc072562015-05-13 12:15:06 -07002153 if (!cacheBlob->fBigGlyphs.count()) {
2154 return;
2155 }
2156
2157 SkMatrix pathMatrix;
2158 if (!cacheBlob->fViewMatrix.invert(&pathMatrix)) {
2159 SkDebugf("could not invert viewmatrix\n");
2160 return;
2161 }
2162
joshualitt9a27e632015-04-06 10:53:36 -07002163 for (int i = 0; i < cacheBlob->fBigGlyphs.count(); i++) {
joshualitt2a0e9f32015-04-13 06:12:21 -07002164 BitmapTextBlob::BigGlyph& bigGlyph = cacheBlob->fBigGlyphs[i];
joshualitt19e4c022015-05-13 11:23:03 -07002165 bigGlyph.fVx += transX;
2166 bigGlyph.fVy += transY;
joshualittfc072562015-05-13 12:15:06 -07002167 SkMatrix translate = cacheBlob->fViewMatrix;
2168 translate.postTranslate(bigGlyph.fVx, bigGlyph.fVy);
2169
robertphillipsccb1b572015-05-27 11:02:55 -07002170 GrBlurUtils::drawPathWithMaskFilter(fContext, drawContext, rt, clip, bigGlyph.fPath,
2171 skPaint, translate, &pathMatrix, clipBounds, false);
joshualitt1d89e8d2015-04-01 12:40:54 -07002172 }
2173}
joshualitt9a27e632015-04-06 10:53:36 -07002174
robertphillips9c240a12015-05-28 07:45:59 -07002175void GrAtlasTextContext::flush(GrDrawContext* drawContext,
joshualitt9a27e632015-04-06 10:53:36 -07002176 const SkTextBlob* blob,
2177 BitmapTextBlob* cacheBlob,
2178 GrRenderTarget* rt,
2179 const SkPaint& skPaint,
2180 const GrPaint& grPaint,
2181 SkDrawFilter* drawFilter,
2182 const GrClip& clip,
2183 const SkMatrix& viewMatrix,
2184 const SkIRect& clipBounds,
joshualitt2a0e9f32015-04-13 06:12:21 -07002185 SkScalar x, SkScalar y,
2186 SkScalar transX, SkScalar transY) {
joshualitt9a27e632015-04-06 10:53:36 -07002187 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
2188 // it as paths
2189 GrPipelineBuilder pipelineBuilder;
2190 pipelineBuilder.setFromPaint(grPaint, rt, clip);
2191
2192 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002193
2194 SkTextBlob::RunIterator it(blob);
2195 for (int run = 0; !it.done(); it.next(), run++) {
2196 if (cacheBlob->fRuns[run].fDrawAsPaths) {
robertphillips9c240a12015-05-28 07:45:59 -07002197 this->flushRunAsPaths(drawContext, rt, it, clip, skPaint,
robertphillipsccb1b572015-05-27 11:02:55 -07002198 drawFilter, viewMatrix, clipBounds, x, y);
joshualitt9a27e632015-04-06 10:53:36 -07002199 continue;
2200 }
joshualitt2a0e9f32015-04-13 06:12:21 -07002201 cacheBlob->fRuns[run].fVertexBounds.offset(transX, transY);
robertphillipsea461502015-05-26 11:38:03 -07002202 this->flushRun(drawContext, &pipelineBuilder, cacheBlob, run, color,
2203 transX, transY, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002204 }
2205
2206 // Now flush big glyphs
robertphillipsccb1b572015-05-27 11:02:55 -07002207 this->flushBigGlyphs(cacheBlob, drawContext, rt, clip, skPaint, transX, transY, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002208}
2209
robertphillipsea461502015-05-26 11:38:03 -07002210void GrAtlasTextContext::flush(GrDrawContext* drawContext,
joshualitt9a27e632015-04-06 10:53:36 -07002211 BitmapTextBlob* cacheBlob,
2212 GrRenderTarget* rt,
2213 const SkPaint& skPaint,
2214 const GrPaint& grPaint,
joshualitt1107e902015-05-11 14:52:11 -07002215 const GrClip& clip,
2216 const SkIRect& clipBounds) {
joshualitt9a27e632015-04-06 10:53:36 -07002217 GrPipelineBuilder pipelineBuilder;
2218 pipelineBuilder.setFromPaint(grPaint, rt, clip);
2219
2220 GrColor color = grPaint.getColor();
joshualitt9a27e632015-04-06 10:53:36 -07002221 for (int run = 0; run < cacheBlob->fRunCount; run++) {
robertphillipsea461502015-05-26 11:38:03 -07002222 this->flushRun(drawContext, &pipelineBuilder, cacheBlob, run, color, 0, 0, skPaint);
joshualitt9a27e632015-04-06 10:53:36 -07002223 }
2224
2225 // Now flush big glyphs
robertphillipsccb1b572015-05-27 11:02:55 -07002226 this->flushBigGlyphs(cacheBlob, drawContext, rt, clip, skPaint, 0, 0, clipBounds);
joshualitt9a27e632015-04-06 10:53:36 -07002227}
joshualitt79dfb2b2015-05-11 08:58:08 -07002228
2229///////////////////////////////////////////////////////////////////////////////////////////////////
2230
2231#ifdef GR_TEST_UTILS
2232
joshualitt6c891102015-05-13 08:51:49 -07002233BATCH_TEST_DEFINE(TextBlobBatch) {
joshualitt79dfb2b2015-05-11 08:58:08 -07002234 static uint32_t gContextID = SK_InvalidGenID;
2235 static GrAtlasTextContext* gTextContext = NULL;
2236 static SkDeviceProperties gDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
2237
2238 if (context->uniqueID() != gContextID) {
2239 gContextID = context->uniqueID();
2240 SkDELETE(gTextContext);
2241 // We don't yet test the fall back to paths in the GrTextContext base class. This is mostly
2242 // because we don't really want to have a gpu device here.
2243 // We enable distance fields by twiddling a knob on the paint
robertphillipsccb1b572015-05-27 11:02:55 -07002244 gTextContext = GrAtlasTextContext::Create(context, gDeviceProperties, false);
joshualitt79dfb2b2015-05-11 08:58:08 -07002245 }
2246
2247 // create dummy render target
2248 GrSurfaceDesc desc;
2249 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2250 desc.fWidth = 1024;
2251 desc.fHeight = 1024;
2252 desc.fConfig = kRGBA_8888_GrPixelConfig;
joshualitt15732062015-05-13 12:15:14 -07002253 desc.fSampleCnt = 0;
joshualitt79dfb2b2015-05-11 08:58:08 -07002254 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, true, NULL, 0));
2255 SkASSERT(texture);
2256 SkASSERT(NULL != texture->asRenderTarget());
2257 GrRenderTarget* rt = texture->asRenderTarget();
2258
2259 // Setup dummy SkPaint / GrPaint
2260 GrColor color = GrRandomColor(random);
joshualitt6c891102015-05-13 08:51:49 -07002261 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
joshualitt79dfb2b2015-05-11 08:58:08 -07002262 SkPaint skPaint;
2263 skPaint.setDistanceFieldTextTEMP(random->nextBool());
2264 skPaint.setColor(color);
2265 skPaint.setLCDRenderText(random->nextBool());
2266 skPaint.setAntiAlias(skPaint.isLCDRenderText() ? true : random->nextBool());
2267 skPaint.setSubpixelText(random->nextBool());
2268
2269 GrPaint grPaint;
2270 if (!SkPaint2GrPaint(context, rt, skPaint, viewMatrix, true, &grPaint)) {
2271 SkFAIL("couldn't convert paint\n");
2272 }
2273
2274 const char* text = "The quick brown fox jumps over the lazy dog.";
2275 int textLen = (int)strlen(text);
2276
2277 // Setup clip
2278 GrClip clip;
2279 SkIRect noClip = SkIRect::MakeLargest();
2280
2281 // right now we don't handle textblobs, nor do we handle drawPosText. Since we only
2282 // intend to test the batch with this unit test, that is okay.
2283 SkAutoTUnref<GrAtlasTextContext::BitmapTextBlob> blob(
2284 gTextContext->createDrawTextBlob(rt, clip, grPaint, skPaint, viewMatrix, text,
2285 static_cast<size_t>(textLen), 0, 0, noClip));
2286
2287 SkScalar transX = static_cast<SkScalar>(random->nextU());
2288 SkScalar transY = static_cast<SkScalar>(random->nextU());
2289 const GrAtlasTextContext::BitmapTextBlob::Run::SubRunInfo& info = blob->fRuns[0].fSubRunInfo[0];
2290 return gTextContext->createBatch(blob, info, textLen, 0, 0, color, transX, transY, skPaint);
2291}
2292
2293#endif