blob: 9ab9c1230d0cdd362521a316f20f7a2d6ce010eb [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;
bsalomon8b6fa5e2016-05-19 16:23:47 -070061 SkScalerContextEffects effects;
62 skPaint.getScalerContextDescriptor(&effects, desc, props, scalerContextFlags, viewMatrix);
joshualitte76b4bb2015-12-28 07:23:58 -080063 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
bsalomon8b6fa5e2016-05-19 16:23:47 -070064 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
65 run->fRasterizer = sk_ref_sp(effects.fRasterizer);
66 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
67 return SkGlyphCache::DetachCache(run->fTypeface, effects, desc->getDesc());
joshualitte76b4bb2015-12-28 07:23:58 -080068}
69
joshualittf528e0d2015-12-09 06:42:52 -080070void GrAtlasTextBlob::appendGlyph(int runIndex,
71 const SkRect& positions,
72 GrColor color,
73 GrBatchTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080074 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070075 SkGlyphCache* cache, const SkGlyph& skGlyph,
joshualitta06e6ab2015-12-10 08:54:41 -080076 SkScalar x, SkScalar y, SkScalar scale, bool applyVM) {
77
78 // If the glyph is too large we fall back to paths
79 if (glyph->fTooLargeForAtlas) {
bsalomonc2878e22016-05-17 13:18:03 -070080 this->appendLargeGlyph(glyph, cache, skGlyph, x, y, scale, applyVM);
joshualitta06e6ab2015-12-10 08:54:41 -080081 return;
82 }
83
joshualittf528e0d2015-12-09 06:42:52 -080084 Run& run = fRuns[runIndex];
85 GrMaskFormat format = glyph->fMaskFormat;
86
87 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
88 if (run.fInitialized && subRun->maskFormat() != format) {
89 subRun = &run.push_back();
90 subRun->setStrike(strike);
91 } else if (!run.fInitialized) {
92 subRun->setStrike(strike);
93 }
94
95 run.fInitialized = true;
96
97 size_t vertexStride = GetVertexStride(format);
98
99 subRun->setMaskFormat(format);
100
joshualitt7481e752016-01-22 06:08:48 -0800101 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800102 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800103
104 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
105
joshualittf528e0d2015-12-09 06:42:52 -0800106 if (kARGB_GrMaskFormat != glyph->fMaskFormat) {
joshualitt18b072d2015-12-07 12:26:12 -0800107 // V0
108 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
109 position->set(positions.fLeft, positions.fTop);
110 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
111 *colorPtr = color;
112 vertex += vertexStride;
113
114 // V1
115 position = reinterpret_cast<SkPoint*>(vertex);
116 position->set(positions.fLeft, positions.fBottom);
117 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
118 *colorPtr = color;
119 vertex += vertexStride;
120
121 // V2
122 position = reinterpret_cast<SkPoint*>(vertex);
123 position->set(positions.fRight, positions.fBottom);
124 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
125 *colorPtr = color;
126 vertex += vertexStride;
127
128 // V3
129 position = reinterpret_cast<SkPoint*>(vertex);
130 position->set(positions.fRight, positions.fTop);
131 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
132 *colorPtr = color;
133 } else {
134 // V0
135 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
136 position->set(positions.fLeft, positions.fTop);
137 vertex += vertexStride;
138
139 // V1
140 position = reinterpret_cast<SkPoint*>(vertex);
141 position->set(positions.fLeft, positions.fBottom);
142 vertex += vertexStride;
143
144 // V2
145 position = reinterpret_cast<SkPoint*>(vertex);
146 position->set(positions.fRight, positions.fBottom);
147 vertex += vertexStride;
148
149 // V3
150 position = reinterpret_cast<SkPoint*>(vertex);
151 position->set(positions.fRight, positions.fTop);
152 }
153 subRun->appendVertices(vertexStride);
154 fGlyphs[subRun->glyphEndIndex()] = glyph;
155 subRun->glyphAppended();
156}
157
bsalomonc2878e22016-05-17 13:18:03 -0700158void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
joshualitta06e6ab2015-12-10 08:54:41 -0800159 SkScalar x, SkScalar y, SkScalar scale, bool applyVM) {
160 if (nullptr == glyph->fPath) {
bsalomonc2878e22016-05-17 13:18:03 -0700161 const SkPath* glyphPath = cache->findPath(skGlyph);
joshualitta06e6ab2015-12-10 08:54:41 -0800162 if (!glyphPath) {
163 return;
164 }
165
166 glyph->fPath = new SkPath(*glyphPath);
167 }
168 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
169}
170
joshualitt8e0ef292016-02-19 14:13:03 -0800171bool GrAtlasTextBlob::mustRegenerate(const SkPaint& paint,
joshualittfd5f6c12015-12-10 07:44:50 -0800172 GrColor color, const SkMaskFilter::BlurRec& blurRec,
173 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
174 // If we have LCD text then our canonical color will be set to transparent, in this case we have
175 // to regenerate the blob on any color change
176 // We use the grPaint to get any color filter effects
177 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
178 fPaintColor != color) {
179 return true;
180 }
181
joshualitt8e0ef292016-02-19 14:13:03 -0800182 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800183 return true;
184 }
185
joshualitt8e0ef292016-02-19 14:13:03 -0800186 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800187 return true;
188 }
189
190 // We only cache one masked version
191 if (fKey.fHasBlur &&
192 (fBlurRec.fSigma != blurRec.fSigma ||
193 fBlurRec.fStyle != blurRec.fStyle ||
194 fBlurRec.fQuality != blurRec.fQuality)) {
195 return true;
196 }
197
198 // Similarly, we only cache one version for each style
199 if (fKey.fStyle != SkPaint::kFill_Style &&
200 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
201 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
202 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
203 return true;
204 }
205
206 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
207 // for mixed blobs if this becomes an issue.
208 if (this->hasBitmap() && this->hasDistanceField()) {
209 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800210 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800211 return false;
212 }
213 return true;
214 }
215
216 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800217 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
218 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
219 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
220 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800221 return true;
222 }
223
224 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
225 // but only for integer translations.
226 // This cool bit of math will determine the necessary translation to apply to the already
227 // generated vertex coordinates to move them to the correct position
228 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800229 viewMatrix.getScaleX() * (x - fInitialX) +
230 viewMatrix.getSkewX() * (y - fInitialY) -
231 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800232 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800233 viewMatrix.getSkewY() * (x - fInitialX) +
234 viewMatrix.getScaleY() * (y - fInitialY) -
235 fInitialViewMatrix.getTranslateY();
236 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800237 return true;
238 }
joshualittfd5f6c12015-12-10 07:44:50 -0800239 } else if (this->hasDistanceField()) {
240 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
241 // distance field being generated, so we have to regenerate in those cases
242 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800243 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800244 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
245 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
246 return true;
247 }
joshualittfd5f6c12015-12-10 07:44:50 -0800248 }
249
joshualittfd5f6c12015-12-10 07:44:50 -0800250 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
251 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
252 // the blob anyways at flush time, so no need to regenerate explicitly
253 return false;
254}
255
joshualitt323c2eb2016-01-20 06:48:47 -0800256inline GrDrawBatch* GrAtlasTextBlob::createBatch(
257 const Run::SubRunInfo& info,
258 int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800259 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
260 GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800261 const SkPaint& skPaint, const SkSurfaceProps& props,
262 const GrDistanceFieldAdjustTable* distanceAdjustTable,
brianosmanb461d342016-04-13 13:10:14 -0700263 bool useGammaCorrectDistanceTable,
joshualitt323c2eb2016-01-20 06:48:47 -0800264 GrBatchFontCache* cache) {
joshualitt2e2202e2015-12-10 11:22:08 -0800265 GrMaskFormat format = info.maskFormat();
266 GrColor subRunColor;
267 if (kARGB_GrMaskFormat == format) {
268 uint8_t paintAlpha = skPaint.getAlpha();
269 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
270 } else {
271 subRunColor = color;
272 }
273
274 GrAtlasTextBatch* batch;
275 if (info.drawAsDistanceFields()) {
276 SkColor filteredColor;
277 SkColorFilter* colorFilter = skPaint.getColorFilter();
278 if (colorFilter) {
279 filteredColor = colorFilter->filterColor(skPaint.getColor());
280 } else {
281 filteredColor = skPaint.getColor();
282 }
283 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
284 batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, cache,
brianosmanb461d342016-04-13 13:10:14 -0700285 distanceAdjustTable,
286 useGammaCorrectDistanceTable,
brianosman0586f5c2016-04-12 12:48:21 -0700287 filteredColor, info.hasUseLCDText(), useBGR);
joshualitt2e2202e2015-12-10 11:22:08 -0800288 } else {
289 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, cache);
290 }
291 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800292 geometry.fViewMatrix = viewMatrix;
joshualitt2e2202e2015-12-10 11:22:08 -0800293 geometry.fBlob = SkRef(this);
294 geometry.fRun = run;
295 geometry.fSubRun = subRun;
296 geometry.fColor = subRunColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800297 geometry.fX = x;
298 geometry.fY = y;
joshualitt2e2202e2015-12-10 11:22:08 -0800299 batch->init();
300
301 return batch;
302}
303
304inline
robertphillips640789d2016-07-18 14:56:06 -0700305void GrAtlasTextBlob::flushRun(GrDrawContext* dc, const GrPaint& grPaint,
cdalton862cff32016-05-12 15:09:48 -0700306 const GrClip& clip, int run, const SkMatrix& viewMatrix, SkScalar x,
robertphillips640789d2016-07-18 14:56:06 -0700307 SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800308 const SkPaint& skPaint, const SkSurfaceProps& props,
309 const GrDistanceFieldAdjustTable* distanceAdjustTable,
310 GrBatchFontCache* cache) {
311 for (int subRun = 0; subRun < fRuns[run].fSubRunInfo.count(); subRun++) {
312 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
313 int glyphCount = info.glyphCount();
314 if (0 == glyphCount) {
315 continue;
316 }
317
robertphillips640789d2016-07-18 14:56:06 -0700318 GrColor color = grPaint.getColor();
319
joshualitt2e2202e2015-12-10 11:22:08 -0800320 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(info, glyphCount, run,
joshualitt8e0ef292016-02-19 14:13:03 -0800321 subRun, viewMatrix, x, y, color,
joshualitt2e2202e2015-12-10 11:22:08 -0800322 skPaint, props,
brianosman5280dcb2016-04-14 06:02:59 -0700323 distanceAdjustTable, dc->isGammaCorrect(),
brianosman0586f5c2016-04-12 12:48:21 -0700324 cache));
robertphillips640789d2016-07-18 14:56:06 -0700325
326 GrPipelineBuilder pipelineBuilder(grPaint, dc->mustUseHWAA(grPaint));
327
328 dc->drawBatch(pipelineBuilder, clip, batch);
joshualitt2e2202e2015-12-10 11:22:08 -0800329 }
330}
331
joshualitt8e0ef292016-02-19 14:13:03 -0800332static void calculate_translation(bool applyVM,
333 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
334 const SkMatrix& currentViewMatrix, SkScalar currentX,
335 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
336 if (applyVM) {
337 *transX = newViewMatrix.getTranslateX() +
338 newViewMatrix.getScaleX() * (newX - currentX) +
339 newViewMatrix.getSkewX() * (newY - currentY) -
340 currentViewMatrix.getTranslateX();
341
342 *transY = newViewMatrix.getTranslateY() +
343 newViewMatrix.getSkewY() * (newX - currentX) +
344 newViewMatrix.getScaleY() * (newY - currentY) -
345 currentViewMatrix.getTranslateY();
346 } else {
347 *transX = newX - currentX;
348 *transY = newY - currentY;
349 }
350}
351
352
joshualitt2e2202e2015-12-10 11:22:08 -0800353void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrDrawContext* dc,
354 const GrClip& clip, const SkPaint& skPaint,
joshualitt8e0ef292016-02-19 14:13:03 -0800355 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800356 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800357 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800358 for (int i = 0; i < fBigGlyphs.count(); i++) {
359 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
joshualitt8e0ef292016-02-19 14:13:03 -0800360 calculate_translation(bigGlyph.fApplyVM, viewMatrix, x, y,
361 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800362 SkMatrix ctm;
363 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800364 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800365 if (bigGlyph.fApplyVM) {
joshualitt8e0ef292016-02-19 14:13:03 -0800366 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800367 }
368
369 GrBlurUtils::drawPathWithMaskFilter(context, dc, clip, bigGlyph.fPath,
370 skPaint, ctm, nullptr, clipBounds, false);
371 }
372}
373
joshualitt0a42e682015-12-10 13:20:58 -0800374void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrDrawContext* dc,
joshualitt2e2202e2015-12-10 11:22:08 -0800375 const SkSurfaceProps& props,
376 const SkTextBlobRunIterator& it,
377 const GrClip& clip, const SkPaint& skPaint,
378 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
379 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
380 SkPaint runPaint = skPaint;
381
382 size_t textLen = it.glyphCount() * sizeof(uint16_t);
383 const SkPoint& offset = it.offset();
384
385 it.applyFontToPaint(&runPaint);
386
387 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
388 return;
389 }
390
joshualitt8e84a1e2016-02-16 11:09:25 -0800391 runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint));
joshualitt2e2202e2015-12-10 11:22:08 -0800392
393 switch (it.positioning()) {
394 case SkTextBlob::kDefault_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800395 GrTextUtils::DrawTextAsPath(context, dc, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800396 (const char *)it.glyphs(),
397 textLen, x + offset.x(), y + offset.y(), clipBounds);
398 break;
399 case SkTextBlob::kHorizontal_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800400 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800401 (const char*)it.glyphs(),
402 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
403 clipBounds);
404 break;
405 case SkTextBlob::kFull_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800406 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800407 (const char*)it.glyphs(),
408 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
409 break;
410 }
411}
412
joshualitt0a42e682015-12-10 13:20:58 -0800413void GrAtlasTextBlob::flushCached(GrContext* context,
joshualitt2e2202e2015-12-10 11:22:08 -0800414 GrDrawContext* dc,
joshualitt0a42e682015-12-10 13:20:58 -0800415 const SkTextBlob* blob,
joshualitt2e2202e2015-12-10 11:22:08 -0800416 const SkSurfaceProps& props,
417 const GrDistanceFieldAdjustTable* distanceAdjustTable,
418 const SkPaint& skPaint,
419 const GrPaint& grPaint,
420 SkDrawFilter* drawFilter,
421 const GrClip& clip,
422 const SkMatrix& viewMatrix,
423 const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800424 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800425 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
426 // it as paths
joshualitt2e2202e2015-12-10 11:22:08 -0800427 SkTextBlobRunIterator it(blob);
428 for (int run = 0; !it.done(); it.next(), run++) {
429 if (fRuns[run].fDrawAsPaths) {
joshualitt0a42e682015-12-10 13:20:58 -0800430 this->flushRunAsPaths(context, dc, props, it, clip, skPaint,
joshualitt2e2202e2015-12-10 11:22:08 -0800431 drawFilter, viewMatrix, clipBounds, x, y);
432 continue;
433 }
robertphillips640789d2016-07-18 14:56:06 -0700434 this->flushRun(dc, grPaint, clip, run, viewMatrix, x, y, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800435 distanceAdjustTable, context->getBatchFontCache());
436 }
437
438 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800439 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800440}
441
442void GrAtlasTextBlob::flushThrowaway(GrContext* context,
443 GrDrawContext* dc,
444 const SkSurfaceProps& props,
445 const GrDistanceFieldAdjustTable* distanceAdjustTable,
446 const SkPaint& skPaint,
447 const GrPaint& grPaint,
448 const GrClip& clip,
joshualitt8e0ef292016-02-19 14:13:03 -0800449 const SkMatrix& viewMatrix,
450 const SkIRect& clipBounds,
451 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800452 for (int run = 0; run < fRunCount; run++) {
robertphillips640789d2016-07-18 14:56:06 -0700453 this->flushRun(dc, grPaint, clip, run, viewMatrix, x, y, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800454 distanceAdjustTable, context->getBatchFontCache());
455 }
456
457 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800458 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800459}
460
joshualitt323c2eb2016-01-20 06:48:47 -0800461GrDrawBatch* GrAtlasTextBlob::test_createBatch(
462 int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800463 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
464 GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800465 const SkPaint& skPaint, const SkSurfaceProps& props,
466 const GrDistanceFieldAdjustTable* distanceAdjustTable,
467 GrBatchFontCache* cache) {
joshualitt7481e752016-01-22 06:08:48 -0800468 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
joshualitt8e0ef292016-02-19 14:13:03 -0800469 return this->createBatch(info, glyphCount, run, subRun, viewMatrix, x, y, color, skPaint,
brianosman0586f5c2016-04-12 12:48:21 -0700470 props, distanceAdjustTable, false, cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800471}
joshualitt2e2202e2015-12-10 11:22:08 -0800472
joshualitt259fbf12015-07-21 11:39:34 -0700473void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800474 SkASSERT_RELEASE(l.fSize == r.fSize);
475 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700476
joshualitt2f2ee832016-02-10 08:52:24 -0800477 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
478 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
479 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700480
joshualitt2f2ee832016-02-10 08:52:24 -0800481 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
482 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
483 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700484
joshualitt2f2ee832016-02-10 08:52:24 -0800485 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700486 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
487 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
488 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
489
joshualitt2f2ee832016-02-10 08:52:24 -0800490 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700491 // We can't assert that these have the same translations
492 }
493
joshualitt2f2ee832016-02-10 08:52:24 -0800494 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800495 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
496 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
497 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
498 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700499
joshualitt2f2ee832016-02-10 08:52:24 -0800500 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700501 for (int i = 0; i < l.fRunCount; i++) {
502 const Run& lRun = l.fRuns[i];
503 const Run& rRun = r.fRuns[i];
504
joshualitt259fbf12015-07-21 11:39:34 -0700505 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800506 SkASSERT_RELEASE(rRun.fTypeface.get());
507 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface, rRun.fTypeface));
joshualitt259fbf12015-07-21 11:39:34 -0700508 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800509 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700510 }
511
joshualitt259fbf12015-07-21 11:39:34 -0700512
joshualitt2f2ee832016-02-10 08:52:24 -0800513 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
514 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700515 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700516
517 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800518 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
519 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700520 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
521 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700522 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800523 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700524 }
525
526 // color can be changed
527 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800528 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
529 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700530
joshualitt2f2ee832016-02-10 08:52:24 -0800531 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700532 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
533 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
534 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
535
joshualitt2f2ee832016-02-10 08:52:24 -0800536 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
537 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700538
joshualitt2f2ee832016-02-10 08:52:24 -0800539 if (lSubRun.strike()) {
540 SkASSERT_RELEASE(rSubRun.strike());
541 SkASSERT_RELEASE(GrBatchTextStrike::GetKey(*lSubRun.strike()) ==
542 GrBatchTextStrike::GetKey(*rSubRun.strike()));
543
544 } else {
545 SkASSERT_RELEASE(!rSubRun.strike());
546 }
547
548 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
549 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
550 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
551 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
552 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
553 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
554 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700555 }
556 }
557}
joshualitt8e0ef292016-02-19 14:13:03 -0800558
559void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
560 SkScalar x, SkScalar y, SkScalar* transX,
561 SkScalar* transY) {
562 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
563 fCurrentViewMatrix, fX, fY, transX, transY);
564 fCurrentViewMatrix = viewMatrix;
565 fX = x;
566 fY = y;
567}