blob: 54af7af87a2d32944fb39f73466b5c6e2486bd67 [file] [log] [blame]
joshualitt259fbf12015-07-21 11:39:34 -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
8#include "GrAtlasTextBlob.h"
9
joshualitt2e2202e2015-12-10 11:22:08 -080010#include "GrBlurUtils.h"
11#include "GrContext.h"
12#include "GrDrawContext.h"
joshualitt0a42e682015-12-10 13:20:58 -080013#include "GrTextUtils.h"
joshualitt2e2202e2015-12-10 11:22:08 -080014#include "SkColorFilter.h"
15#include "SkDrawFilter.h"
joshualitte76b4bb2015-12-28 07:23:58 -080016#include "SkGlyphCache.h"
joshualitt2e2202e2015-12-10 11:22:08 -080017#include "SkTextBlobRunIterator.h"
18#include "batches/GrAtlasTextBatch.h"
19
joshualitt92303772016-02-10 11:55:52 -080020GrAtlasTextBlob* GrAtlasTextBlob::Create(GrMemoryPool* pool, int glyphCount, int runCount) {
21 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
22 // and size for the glyphIds array.
23 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
24 size_t size = sizeof(GrAtlasTextBlob) +
25 verticesCount +
26 glyphCount * sizeof(GrGlyph**) +
27 sizeof(GrAtlasTextBlob::Run) * runCount;
28
29 void* allocation = pool->allocate(size);
30 if (CACHE_SANITY_CHECK) {
31 sk_bzero(allocation, size);
32 }
33
34 GrAtlasTextBlob* cacheBlob = new (allocation) GrAtlasTextBlob;
35 cacheBlob->fSize = size;
36
37 // setup offsets for vertices / glyphs
38 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<unsigned char*>(cacheBlob);
39 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
40 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
41
42 // Initialize runs
43 for (int i = 0; i < runCount; i++) {
44 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
45 }
46 cacheBlob->fRunCount = runCount;
47 cacheBlob->fPool = pool;
48 return cacheBlob;
49}
50
joshualitte76b4bb2015-12-28 07:23:58 -080051SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
52 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -070053 uint32_t scalerContextFlags,
joshualitte76b4bb2015-12-28 07:23:58 -080054 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -080055 const SkMatrix* viewMatrix) {
joshualitte76b4bb2015-12-28 07:23:58 -080056 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
57
58 // if we have an override descriptor for the run, then we should use that
59 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
60 &run->fDescriptor;
reeda9322c22016-04-12 06:47:05 -070061 skPaint.getScalerContextDescriptor(&run->fEffects, desc, props, scalerContextFlags, viewMatrix);
joshualitte76b4bb2015-12-28 07:23:58 -080062 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
reeda9322c22016-04-12 06:47:05 -070063 return SkGlyphCache::DetachCache(run->fTypeface, run->fEffects, desc->getDesc());
joshualitte76b4bb2015-12-28 07:23:58 -080064}
65
joshualittf528e0d2015-12-09 06:42:52 -080066void GrAtlasTextBlob::appendGlyph(int runIndex,
67 const SkRect& positions,
68 GrColor color,
69 GrBatchTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080070 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070071 SkGlyphCache* cache, const SkGlyph& skGlyph,
joshualitta06e6ab2015-12-10 08:54:41 -080072 SkScalar x, SkScalar y, SkScalar scale, bool applyVM) {
73
74 // If the glyph is too large we fall back to paths
75 if (glyph->fTooLargeForAtlas) {
bsalomonc2878e22016-05-17 13:18:03 -070076 this->appendLargeGlyph(glyph, cache, skGlyph, x, y, scale, applyVM);
joshualitta06e6ab2015-12-10 08:54:41 -080077 return;
78 }
79
joshualittf528e0d2015-12-09 06:42:52 -080080 Run& run = fRuns[runIndex];
81 GrMaskFormat format = glyph->fMaskFormat;
82
83 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
84 if (run.fInitialized && subRun->maskFormat() != format) {
85 subRun = &run.push_back();
86 subRun->setStrike(strike);
87 } else if (!run.fInitialized) {
88 subRun->setStrike(strike);
89 }
90
91 run.fInitialized = true;
92
93 size_t vertexStride = GetVertexStride(format);
94
95 subRun->setMaskFormat(format);
96
joshualitt7481e752016-01-22 06:08:48 -080097 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -080098 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -080099
100 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
101
joshualittf528e0d2015-12-09 06:42:52 -0800102 if (kARGB_GrMaskFormat != glyph->fMaskFormat) {
joshualitt18b072d2015-12-07 12:26:12 -0800103 // V0
104 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
105 position->set(positions.fLeft, positions.fTop);
106 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
107 *colorPtr = color;
108 vertex += vertexStride;
109
110 // V1
111 position = reinterpret_cast<SkPoint*>(vertex);
112 position->set(positions.fLeft, positions.fBottom);
113 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
114 *colorPtr = color;
115 vertex += vertexStride;
116
117 // V2
118 position = reinterpret_cast<SkPoint*>(vertex);
119 position->set(positions.fRight, positions.fBottom);
120 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
121 *colorPtr = color;
122 vertex += vertexStride;
123
124 // V3
125 position = reinterpret_cast<SkPoint*>(vertex);
126 position->set(positions.fRight, positions.fTop);
127 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
128 *colorPtr = color;
129 } else {
130 // V0
131 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
132 position->set(positions.fLeft, positions.fTop);
133 vertex += vertexStride;
134
135 // V1
136 position = reinterpret_cast<SkPoint*>(vertex);
137 position->set(positions.fLeft, positions.fBottom);
138 vertex += vertexStride;
139
140 // V2
141 position = reinterpret_cast<SkPoint*>(vertex);
142 position->set(positions.fRight, positions.fBottom);
143 vertex += vertexStride;
144
145 // V3
146 position = reinterpret_cast<SkPoint*>(vertex);
147 position->set(positions.fRight, positions.fTop);
148 }
149 subRun->appendVertices(vertexStride);
150 fGlyphs[subRun->glyphEndIndex()] = glyph;
151 subRun->glyphAppended();
152}
153
bsalomonc2878e22016-05-17 13:18:03 -0700154void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
joshualitta06e6ab2015-12-10 08:54:41 -0800155 SkScalar x, SkScalar y, SkScalar scale, bool applyVM) {
156 if (nullptr == glyph->fPath) {
bsalomonc2878e22016-05-17 13:18:03 -0700157 const SkPath* glyphPath = cache->findPath(skGlyph);
joshualitta06e6ab2015-12-10 08:54:41 -0800158 if (!glyphPath) {
159 return;
160 }
161
162 glyph->fPath = new SkPath(*glyphPath);
163 }
164 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
165}
166
joshualitt8e0ef292016-02-19 14:13:03 -0800167bool GrAtlasTextBlob::mustRegenerate(const SkPaint& paint,
joshualittfd5f6c12015-12-10 07:44:50 -0800168 GrColor color, const SkMaskFilter::BlurRec& blurRec,
169 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
170 // If we have LCD text then our canonical color will be set to transparent, in this case we have
171 // to regenerate the blob on any color change
172 // We use the grPaint to get any color filter effects
173 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
174 fPaintColor != color) {
175 return true;
176 }
177
joshualitt8e0ef292016-02-19 14:13:03 -0800178 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800179 return true;
180 }
181
joshualitt8e0ef292016-02-19 14:13:03 -0800182 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800183 return true;
184 }
185
186 // We only cache one masked version
187 if (fKey.fHasBlur &&
188 (fBlurRec.fSigma != blurRec.fSigma ||
189 fBlurRec.fStyle != blurRec.fStyle ||
190 fBlurRec.fQuality != blurRec.fQuality)) {
191 return true;
192 }
193
194 // Similarly, we only cache one version for each style
195 if (fKey.fStyle != SkPaint::kFill_Style &&
196 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
197 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
198 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
199 return true;
200 }
201
202 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
203 // for mixed blobs if this becomes an issue.
204 if (this->hasBitmap() && this->hasDistanceField()) {
205 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800206 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800207 return false;
208 }
209 return true;
210 }
211
212 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800213 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
214 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
215 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
216 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800217 return true;
218 }
219
220 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
221 // but only for integer translations.
222 // This cool bit of math will determine the necessary translation to apply to the already
223 // generated vertex coordinates to move them to the correct position
224 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800225 viewMatrix.getScaleX() * (x - fInitialX) +
226 viewMatrix.getSkewX() * (y - fInitialY) -
227 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800228 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800229 viewMatrix.getSkewY() * (x - fInitialX) +
230 viewMatrix.getScaleY() * (y - fInitialY) -
231 fInitialViewMatrix.getTranslateY();
232 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800233 return true;
234 }
joshualittfd5f6c12015-12-10 07:44:50 -0800235 } else if (this->hasDistanceField()) {
236 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
237 // distance field being generated, so we have to regenerate in those cases
238 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800239 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800240 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
241 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
242 return true;
243 }
joshualittfd5f6c12015-12-10 07:44:50 -0800244 }
245
joshualittfd5f6c12015-12-10 07:44:50 -0800246 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
247 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
248 // the blob anyways at flush time, so no need to regenerate explicitly
249 return false;
250}
251
joshualitt323c2eb2016-01-20 06:48:47 -0800252inline GrDrawBatch* GrAtlasTextBlob::createBatch(
253 const Run::SubRunInfo& info,
254 int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800255 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
256 GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800257 const SkPaint& skPaint, const SkSurfaceProps& props,
258 const GrDistanceFieldAdjustTable* distanceAdjustTable,
brianosmanb461d342016-04-13 13:10:14 -0700259 bool useGammaCorrectDistanceTable,
joshualitt323c2eb2016-01-20 06:48:47 -0800260 GrBatchFontCache* cache) {
joshualitt2e2202e2015-12-10 11:22:08 -0800261 GrMaskFormat format = info.maskFormat();
262 GrColor subRunColor;
263 if (kARGB_GrMaskFormat == format) {
264 uint8_t paintAlpha = skPaint.getAlpha();
265 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
266 } else {
267 subRunColor = color;
268 }
269
270 GrAtlasTextBatch* batch;
271 if (info.drawAsDistanceFields()) {
272 SkColor filteredColor;
273 SkColorFilter* colorFilter = skPaint.getColorFilter();
274 if (colorFilter) {
275 filteredColor = colorFilter->filterColor(skPaint.getColor());
276 } else {
277 filteredColor = skPaint.getColor();
278 }
279 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
280 batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, cache,
brianosmanb461d342016-04-13 13:10:14 -0700281 distanceAdjustTable,
282 useGammaCorrectDistanceTable,
brianosman0586f5c2016-04-12 12:48:21 -0700283 filteredColor, info.hasUseLCDText(), useBGR);
joshualitt2e2202e2015-12-10 11:22:08 -0800284 } else {
285 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, cache);
286 }
287 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800288 geometry.fViewMatrix = viewMatrix;
joshualitt2e2202e2015-12-10 11:22:08 -0800289 geometry.fBlob = SkRef(this);
290 geometry.fRun = run;
291 geometry.fSubRun = subRun;
292 geometry.fColor = subRunColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800293 geometry.fX = x;
294 geometry.fY = y;
joshualitt2e2202e2015-12-10 11:22:08 -0800295 batch->init();
296
297 return batch;
298}
299
300inline
301void GrAtlasTextBlob::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
cdalton862cff32016-05-12 15:09:48 -0700302 const GrClip& clip, int run, const SkMatrix& viewMatrix, SkScalar x,
303 SkScalar y, GrColor color,
joshualitt2e2202e2015-12-10 11:22:08 -0800304 const SkPaint& skPaint, const SkSurfaceProps& props,
305 const GrDistanceFieldAdjustTable* distanceAdjustTable,
306 GrBatchFontCache* cache) {
307 for (int subRun = 0; subRun < fRuns[run].fSubRunInfo.count(); subRun++) {
308 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
309 int glyphCount = info.glyphCount();
310 if (0 == glyphCount) {
311 continue;
312 }
313
314 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(info, glyphCount, run,
joshualitt8e0ef292016-02-19 14:13:03 -0800315 subRun, viewMatrix, x, y, color,
joshualitt2e2202e2015-12-10 11:22:08 -0800316 skPaint, props,
brianosman5280dcb2016-04-14 06:02:59 -0700317 distanceAdjustTable, dc->isGammaCorrect(),
brianosman0586f5c2016-04-12 12:48:21 -0700318 cache));
cdalton862cff32016-05-12 15:09:48 -0700319 dc->drawBatch(pipelineBuilder, clip, batch);
joshualitt2e2202e2015-12-10 11:22:08 -0800320 }
321}
322
joshualitt8e0ef292016-02-19 14:13:03 -0800323static void calculate_translation(bool applyVM,
324 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
325 const SkMatrix& currentViewMatrix, SkScalar currentX,
326 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
327 if (applyVM) {
328 *transX = newViewMatrix.getTranslateX() +
329 newViewMatrix.getScaleX() * (newX - currentX) +
330 newViewMatrix.getSkewX() * (newY - currentY) -
331 currentViewMatrix.getTranslateX();
332
333 *transY = newViewMatrix.getTranslateY() +
334 newViewMatrix.getSkewY() * (newX - currentX) +
335 newViewMatrix.getScaleY() * (newY - currentY) -
336 currentViewMatrix.getTranslateY();
337 } else {
338 *transX = newX - currentX;
339 *transY = newY - currentY;
340 }
341}
342
343
joshualitt2e2202e2015-12-10 11:22:08 -0800344void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrDrawContext* dc,
345 const GrClip& clip, const SkPaint& skPaint,
joshualitt8e0ef292016-02-19 14:13:03 -0800346 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800347 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800348 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800349 for (int i = 0; i < fBigGlyphs.count(); i++) {
350 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
joshualitt8e0ef292016-02-19 14:13:03 -0800351 calculate_translation(bigGlyph.fApplyVM, viewMatrix, x, y,
352 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800353 SkMatrix ctm;
354 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800355 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800356 if (bigGlyph.fApplyVM) {
joshualitt8e0ef292016-02-19 14:13:03 -0800357 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800358 }
359
360 GrBlurUtils::drawPathWithMaskFilter(context, dc, clip, bigGlyph.fPath,
361 skPaint, ctm, nullptr, clipBounds, false);
362 }
363}
364
joshualitt0a42e682015-12-10 13:20:58 -0800365void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrDrawContext* dc,
joshualitt2e2202e2015-12-10 11:22:08 -0800366 const SkSurfaceProps& props,
367 const SkTextBlobRunIterator& it,
368 const GrClip& clip, const SkPaint& skPaint,
369 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
370 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
371 SkPaint runPaint = skPaint;
372
373 size_t textLen = it.glyphCount() * sizeof(uint16_t);
374 const SkPoint& offset = it.offset();
375
376 it.applyFontToPaint(&runPaint);
377
378 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
379 return;
380 }
381
joshualitt8e84a1e2016-02-16 11:09:25 -0800382 runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint));
joshualitt2e2202e2015-12-10 11:22:08 -0800383
384 switch (it.positioning()) {
385 case SkTextBlob::kDefault_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800386 GrTextUtils::DrawTextAsPath(context, dc, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800387 (const char *)it.glyphs(),
388 textLen, x + offset.x(), y + offset.y(), clipBounds);
389 break;
390 case SkTextBlob::kHorizontal_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800391 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800392 (const char*)it.glyphs(),
393 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
394 clipBounds);
395 break;
396 case SkTextBlob::kFull_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800397 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800398 (const char*)it.glyphs(),
399 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
400 break;
401 }
402}
403
joshualitt0a42e682015-12-10 13:20:58 -0800404void GrAtlasTextBlob::flushCached(GrContext* context,
joshualitt2e2202e2015-12-10 11:22:08 -0800405 GrDrawContext* dc,
joshualitt0a42e682015-12-10 13:20:58 -0800406 const SkTextBlob* blob,
joshualitt2e2202e2015-12-10 11:22:08 -0800407 const SkSurfaceProps& props,
408 const GrDistanceFieldAdjustTable* distanceAdjustTable,
409 const SkPaint& skPaint,
410 const GrPaint& grPaint,
411 SkDrawFilter* drawFilter,
412 const GrClip& clip,
413 const SkMatrix& viewMatrix,
414 const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800415 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800416 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
417 // it as paths
cdalton862cff32016-05-12 15:09:48 -0700418 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget());
joshualitt2e2202e2015-12-10 11:22:08 -0800419
420 GrColor color = grPaint.getColor();
421
422 SkTextBlobRunIterator it(blob);
423 for (int run = 0; !it.done(); it.next(), run++) {
424 if (fRuns[run].fDrawAsPaths) {
joshualitt0a42e682015-12-10 13:20:58 -0800425 this->flushRunAsPaths(context, dc, props, it, clip, skPaint,
joshualitt2e2202e2015-12-10 11:22:08 -0800426 drawFilter, viewMatrix, clipBounds, x, y);
427 continue;
428 }
cdalton862cff32016-05-12 15:09:48 -0700429 this->flushRun(dc, &pipelineBuilder, clip, run, viewMatrix, x, y, color, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800430 distanceAdjustTable, context->getBatchFontCache());
431 }
432
433 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800434 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800435}
436
437void GrAtlasTextBlob::flushThrowaway(GrContext* context,
438 GrDrawContext* dc,
439 const SkSurfaceProps& props,
440 const GrDistanceFieldAdjustTable* distanceAdjustTable,
441 const SkPaint& skPaint,
442 const GrPaint& grPaint,
443 const GrClip& clip,
joshualitt8e0ef292016-02-19 14:13:03 -0800444 const SkMatrix& viewMatrix,
445 const SkIRect& clipBounds,
446 SkScalar x, SkScalar y) {
cdalton862cff32016-05-12 15:09:48 -0700447 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget());
joshualitt2e2202e2015-12-10 11:22:08 -0800448
449 GrColor color = grPaint.getColor();
450 for (int run = 0; run < fRunCount; run++) {
cdalton862cff32016-05-12 15:09:48 -0700451 this->flushRun(dc, &pipelineBuilder, clip, run, viewMatrix, x, y, color, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800452 distanceAdjustTable, context->getBatchFontCache());
453 }
454
455 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800456 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800457}
458
joshualitt323c2eb2016-01-20 06:48:47 -0800459GrDrawBatch* GrAtlasTextBlob::test_createBatch(
460 int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800461 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
462 GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800463 const SkPaint& skPaint, const SkSurfaceProps& props,
464 const GrDistanceFieldAdjustTable* distanceAdjustTable,
465 GrBatchFontCache* cache) {
joshualitt7481e752016-01-22 06:08:48 -0800466 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
joshualitt8e0ef292016-02-19 14:13:03 -0800467 return this->createBatch(info, glyphCount, run, subRun, viewMatrix, x, y, color, skPaint,
brianosman0586f5c2016-04-12 12:48:21 -0700468 props, distanceAdjustTable, false, cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800469}
joshualitt2e2202e2015-12-10 11:22:08 -0800470
joshualitt259fbf12015-07-21 11:39:34 -0700471void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800472 SkASSERT_RELEASE(l.fSize == r.fSize);
473 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700474
joshualitt2f2ee832016-02-10 08:52:24 -0800475 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
476 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
477 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700478
joshualitt2f2ee832016-02-10 08:52:24 -0800479 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
480 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
481 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700482
joshualitt2f2ee832016-02-10 08:52:24 -0800483 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700484 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
485 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
486 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
487
joshualitt2f2ee832016-02-10 08:52:24 -0800488 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700489 // We can't assert that these have the same translations
490 }
491
joshualitt2f2ee832016-02-10 08:52:24 -0800492 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800493 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
494 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
495 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
496 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700497
joshualitt2f2ee832016-02-10 08:52:24 -0800498 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700499 for (int i = 0; i < l.fRunCount; i++) {
500 const Run& lRun = l.fRuns[i];
501 const Run& rRun = r.fRuns[i];
502
joshualitt259fbf12015-07-21 11:39:34 -0700503 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800504 SkASSERT_RELEASE(rRun.fTypeface.get());
505 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface, rRun.fTypeface));
joshualitt259fbf12015-07-21 11:39:34 -0700506 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800507 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700508 }
509
joshualitt259fbf12015-07-21 11:39:34 -0700510
joshualitt2f2ee832016-02-10 08:52:24 -0800511 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
512 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700513 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700514
515 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800516 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
517 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700518 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
519 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700520 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800521 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700522 }
523
524 // color can be changed
525 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800526 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
527 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700528
joshualitt2f2ee832016-02-10 08:52:24 -0800529 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700530 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
531 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
532 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
533
joshualitt2f2ee832016-02-10 08:52:24 -0800534 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
535 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700536
joshualitt2f2ee832016-02-10 08:52:24 -0800537 if (lSubRun.strike()) {
538 SkASSERT_RELEASE(rSubRun.strike());
539 SkASSERT_RELEASE(GrBatchTextStrike::GetKey(*lSubRun.strike()) ==
540 GrBatchTextStrike::GetKey(*rSubRun.strike()));
541
542 } else {
543 SkASSERT_RELEASE(!rSubRun.strike());
544 }
545
546 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
547 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
548 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
549 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
550 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
551 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
552 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700553 }
554 }
555}
joshualitt8e0ef292016-02-19 14:13:03 -0800556
557void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
558 SkScalar x, SkScalar y, SkScalar* transX,
559 SkScalar* transY) {
560 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
561 fCurrentViewMatrix, fX, fY, transX, transY);
562 fCurrentViewMatrix = viewMatrix;
563 fX = x;
564 fY = y;
565}