blob: 03260a6e522233236527c93085a530d4603cf00c [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
51
joshualitte76b4bb2015-12-28 07:23:58 -080052SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
53 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -070054 uint32_t scalerContextFlags,
joshualitte76b4bb2015-12-28 07:23:58 -080055 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -080056 const SkMatrix* viewMatrix) {
joshualitte76b4bb2015-12-28 07:23:58 -080057 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
58
59 // if we have an override descriptor for the run, then we should use that
60 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
61 &run->fDescriptor;
reeda9322c22016-04-12 06:47:05 -070062 skPaint.getScalerContextDescriptor(&run->fEffects, desc, props, scalerContextFlags, viewMatrix);
joshualitte76b4bb2015-12-28 07:23:58 -080063 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
reeda9322c22016-04-12 06:47:05 -070064 return SkGlyphCache::DetachCache(run->fTypeface, run->fEffects, desc->getDesc());
joshualitte76b4bb2015-12-28 07:23:58 -080065}
66
joshualittf528e0d2015-12-09 06:42:52 -080067void GrAtlasTextBlob::appendGlyph(int runIndex,
68 const SkRect& positions,
69 GrColor color,
70 GrBatchTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080071 GrGlyph* glyph,
72 GrFontScaler* scaler, const SkGlyph& skGlyph,
73 SkScalar x, SkScalar y, SkScalar scale, bool applyVM) {
74
75 // If the glyph is too large we fall back to paths
76 if (glyph->fTooLargeForAtlas) {
77 this->appendLargeGlyph(glyph, scaler, skGlyph, x, y, scale, applyVM);
78 return;
79 }
80
joshualittf528e0d2015-12-09 06:42:52 -080081 Run& run = fRuns[runIndex];
82 GrMaskFormat format = glyph->fMaskFormat;
83
84 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
85 if (run.fInitialized && subRun->maskFormat() != format) {
86 subRun = &run.push_back();
87 subRun->setStrike(strike);
88 } else if (!run.fInitialized) {
89 subRun->setStrike(strike);
90 }
91
92 run.fInitialized = true;
93
94 size_t vertexStride = GetVertexStride(format);
95
96 subRun->setMaskFormat(format);
97
joshualitt7481e752016-01-22 06:08:48 -080098 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -080099 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800100
101 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
102
joshualittf528e0d2015-12-09 06:42:52 -0800103 if (kARGB_GrMaskFormat != glyph->fMaskFormat) {
joshualitt18b072d2015-12-07 12:26:12 -0800104 // V0
105 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
106 position->set(positions.fLeft, positions.fTop);
107 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
108 *colorPtr = color;
109 vertex += vertexStride;
110
111 // V1
112 position = reinterpret_cast<SkPoint*>(vertex);
113 position->set(positions.fLeft, positions.fBottom);
114 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
115 *colorPtr = color;
116 vertex += vertexStride;
117
118 // V2
119 position = reinterpret_cast<SkPoint*>(vertex);
120 position->set(positions.fRight, positions.fBottom);
121 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
122 *colorPtr = color;
123 vertex += vertexStride;
124
125 // V3
126 position = reinterpret_cast<SkPoint*>(vertex);
127 position->set(positions.fRight, positions.fTop);
128 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
129 *colorPtr = color;
130 } else {
131 // V0
132 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
133 position->set(positions.fLeft, positions.fTop);
134 vertex += vertexStride;
135
136 // V1
137 position = reinterpret_cast<SkPoint*>(vertex);
138 position->set(positions.fLeft, positions.fBottom);
139 vertex += vertexStride;
140
141 // V2
142 position = reinterpret_cast<SkPoint*>(vertex);
143 position->set(positions.fRight, positions.fBottom);
144 vertex += vertexStride;
145
146 // V3
147 position = reinterpret_cast<SkPoint*>(vertex);
148 position->set(positions.fRight, positions.fTop);
149 }
150 subRun->appendVertices(vertexStride);
151 fGlyphs[subRun->glyphEndIndex()] = glyph;
152 subRun->glyphAppended();
153}
154
joshualitta06e6ab2015-12-10 08:54:41 -0800155void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, GrFontScaler* scaler, const SkGlyph& skGlyph,
156 SkScalar x, SkScalar y, SkScalar scale, bool applyVM) {
157 if (nullptr == glyph->fPath) {
158 const SkPath* glyphPath = scaler->getGlyphPath(skGlyph);
159 if (!glyphPath) {
160 return;
161 }
162
163 glyph->fPath = new SkPath(*glyphPath);
164 }
165 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, applyVM));
166}
167
joshualitt8e0ef292016-02-19 14:13:03 -0800168bool GrAtlasTextBlob::mustRegenerate(const SkPaint& paint,
joshualittfd5f6c12015-12-10 07:44:50 -0800169 GrColor color, const SkMaskFilter::BlurRec& blurRec,
170 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
171 // If we have LCD text then our canonical color will be set to transparent, in this case we have
172 // to regenerate the blob on any color change
173 // We use the grPaint to get any color filter effects
174 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
175 fPaintColor != color) {
176 return true;
177 }
178
joshualitt8e0ef292016-02-19 14:13:03 -0800179 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800180 return true;
181 }
182
joshualitt8e0ef292016-02-19 14:13:03 -0800183 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800184 return true;
185 }
186
187 // We only cache one masked version
188 if (fKey.fHasBlur &&
189 (fBlurRec.fSigma != blurRec.fSigma ||
190 fBlurRec.fStyle != blurRec.fStyle ||
191 fBlurRec.fQuality != blurRec.fQuality)) {
192 return true;
193 }
194
195 // Similarly, we only cache one version for each style
196 if (fKey.fStyle != SkPaint::kFill_Style &&
197 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
198 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
199 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
200 return true;
201 }
202
203 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
204 // for mixed blobs if this becomes an issue.
205 if (this->hasBitmap() && this->hasDistanceField()) {
206 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800207 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800208 return false;
209 }
210 return true;
211 }
212
213 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800214 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
215 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
216 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
217 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800218 return true;
219 }
220
221 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
222 // but only for integer translations.
223 // This cool bit of math will determine the necessary translation to apply to the already
224 // generated vertex coordinates to move them to the correct position
225 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800226 viewMatrix.getScaleX() * (x - fInitialX) +
227 viewMatrix.getSkewX() * (y - fInitialY) -
228 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800229 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800230 viewMatrix.getSkewY() * (x - fInitialX) +
231 viewMatrix.getScaleY() * (y - fInitialY) -
232 fInitialViewMatrix.getTranslateY();
233 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800234 return true;
235 }
joshualittfd5f6c12015-12-10 07:44:50 -0800236 } else if (this->hasDistanceField()) {
237 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
238 // distance field being generated, so we have to regenerate in those cases
239 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800240 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800241 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
242 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
243 return true;
244 }
joshualittfd5f6c12015-12-10 07:44:50 -0800245 }
246
joshualittfd5f6c12015-12-10 07:44:50 -0800247 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
248 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
249 // the blob anyways at flush time, so no need to regenerate explicitly
250 return false;
251}
252
joshualitt323c2eb2016-01-20 06:48:47 -0800253inline GrDrawBatch* GrAtlasTextBlob::createBatch(
254 const Run::SubRunInfo& info,
255 int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800256 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
257 GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800258 const SkPaint& skPaint, const SkSurfaceProps& props,
259 const GrDistanceFieldAdjustTable* distanceAdjustTable,
brianosmanb461d342016-04-13 13:10:14 -0700260 bool useGammaCorrectDistanceTable,
joshualitt323c2eb2016-01-20 06:48:47 -0800261 GrBatchFontCache* cache) {
joshualitt2e2202e2015-12-10 11:22:08 -0800262 GrMaskFormat format = info.maskFormat();
263 GrColor subRunColor;
264 if (kARGB_GrMaskFormat == format) {
265 uint8_t paintAlpha = skPaint.getAlpha();
266 subRunColor = SkColorSetARGB(paintAlpha, paintAlpha, paintAlpha, paintAlpha);
267 } else {
268 subRunColor = color;
269 }
270
271 GrAtlasTextBatch* batch;
272 if (info.drawAsDistanceFields()) {
273 SkColor filteredColor;
274 SkColorFilter* colorFilter = skPaint.getColorFilter();
275 if (colorFilter) {
276 filteredColor = colorFilter->filterColor(skPaint.getColor());
277 } else {
278 filteredColor = skPaint.getColor();
279 }
280 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
281 batch = GrAtlasTextBatch::CreateDistanceField(glyphCount, cache,
brianosmanb461d342016-04-13 13:10:14 -0700282 distanceAdjustTable,
283 useGammaCorrectDistanceTable,
brianosman0586f5c2016-04-12 12:48:21 -0700284 filteredColor, info.hasUseLCDText(), useBGR);
joshualitt2e2202e2015-12-10 11:22:08 -0800285 } else {
286 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, cache);
287 }
288 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800289 geometry.fViewMatrix = viewMatrix;
joshualitt2e2202e2015-12-10 11:22:08 -0800290 geometry.fBlob = SkRef(this);
291 geometry.fRun = run;
292 geometry.fSubRun = subRun;
293 geometry.fColor = subRunColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800294 geometry.fX = x;
295 geometry.fY = y;
joshualitt2e2202e2015-12-10 11:22:08 -0800296 batch->init();
297
298 return batch;
299}
300
301inline
302void GrAtlasTextBlob::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
cdalton862cff32016-05-12 15:09:48 -0700303 const GrClip& clip, int run, const SkMatrix& viewMatrix, SkScalar x,
304 SkScalar y, GrColor color,
joshualitt2e2202e2015-12-10 11:22:08 -0800305 const SkPaint& skPaint, const SkSurfaceProps& props,
306 const GrDistanceFieldAdjustTable* distanceAdjustTable,
307 GrBatchFontCache* cache) {
308 for (int subRun = 0; subRun < fRuns[run].fSubRunInfo.count(); subRun++) {
309 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
310 int glyphCount = info.glyphCount();
311 if (0 == glyphCount) {
312 continue;
313 }
314
315 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(info, glyphCount, run,
joshualitt8e0ef292016-02-19 14:13:03 -0800316 subRun, viewMatrix, x, y, color,
joshualitt2e2202e2015-12-10 11:22:08 -0800317 skPaint, props,
brianosman5280dcb2016-04-14 06:02:59 -0700318 distanceAdjustTable, dc->isGammaCorrect(),
brianosman0586f5c2016-04-12 12:48:21 -0700319 cache));
cdalton862cff32016-05-12 15:09:48 -0700320 dc->drawBatch(pipelineBuilder, clip, batch);
joshualitt2e2202e2015-12-10 11:22:08 -0800321 }
322}
323
joshualitt8e0ef292016-02-19 14:13:03 -0800324static void calculate_translation(bool applyVM,
325 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
326 const SkMatrix& currentViewMatrix, SkScalar currentX,
327 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
328 if (applyVM) {
329 *transX = newViewMatrix.getTranslateX() +
330 newViewMatrix.getScaleX() * (newX - currentX) +
331 newViewMatrix.getSkewX() * (newY - currentY) -
332 currentViewMatrix.getTranslateX();
333
334 *transY = newViewMatrix.getTranslateY() +
335 newViewMatrix.getSkewY() * (newX - currentX) +
336 newViewMatrix.getScaleY() * (newY - currentY) -
337 currentViewMatrix.getTranslateY();
338 } else {
339 *transX = newX - currentX;
340 *transY = newY - currentY;
341 }
342}
343
344
joshualitt2e2202e2015-12-10 11:22:08 -0800345void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrDrawContext* dc,
346 const GrClip& clip, const SkPaint& skPaint,
joshualitt8e0ef292016-02-19 14:13:03 -0800347 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800348 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800349 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800350 for (int i = 0; i < fBigGlyphs.count(); i++) {
351 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
joshualitt8e0ef292016-02-19 14:13:03 -0800352 calculate_translation(bigGlyph.fApplyVM, viewMatrix, x, y,
353 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800354 SkMatrix ctm;
355 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800356 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800357 if (bigGlyph.fApplyVM) {
joshualitt8e0ef292016-02-19 14:13:03 -0800358 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800359 }
360
361 GrBlurUtils::drawPathWithMaskFilter(context, dc, clip, bigGlyph.fPath,
362 skPaint, ctm, nullptr, clipBounds, false);
363 }
364}
365
joshualitt0a42e682015-12-10 13:20:58 -0800366void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrDrawContext* dc,
joshualitt2e2202e2015-12-10 11:22:08 -0800367 const SkSurfaceProps& props,
368 const SkTextBlobRunIterator& it,
369 const GrClip& clip, const SkPaint& skPaint,
370 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
371 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
372 SkPaint runPaint = skPaint;
373
374 size_t textLen = it.glyphCount() * sizeof(uint16_t);
375 const SkPoint& offset = it.offset();
376
377 it.applyFontToPaint(&runPaint);
378
379 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
380 return;
381 }
382
joshualitt8e84a1e2016-02-16 11:09:25 -0800383 runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint));
joshualitt2e2202e2015-12-10 11:22:08 -0800384
385 switch (it.positioning()) {
386 case SkTextBlob::kDefault_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800387 GrTextUtils::DrawTextAsPath(context, dc, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800388 (const char *)it.glyphs(),
389 textLen, x + offset.x(), y + offset.y(), clipBounds);
390 break;
391 case SkTextBlob::kHorizontal_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800392 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800393 (const char*)it.glyphs(),
394 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
395 clipBounds);
396 break;
397 case SkTextBlob::kFull_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800398 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800399 (const char*)it.glyphs(),
400 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
401 break;
402 }
403}
404
joshualitt0a42e682015-12-10 13:20:58 -0800405void GrAtlasTextBlob::flushCached(GrContext* context,
joshualitt2e2202e2015-12-10 11:22:08 -0800406 GrDrawContext* dc,
joshualitt0a42e682015-12-10 13:20:58 -0800407 const SkTextBlob* blob,
joshualitt2e2202e2015-12-10 11:22:08 -0800408 const SkSurfaceProps& props,
409 const GrDistanceFieldAdjustTable* distanceAdjustTable,
410 const SkPaint& skPaint,
411 const GrPaint& grPaint,
412 SkDrawFilter* drawFilter,
413 const GrClip& clip,
414 const SkMatrix& viewMatrix,
415 const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800416 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800417 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
418 // it as paths
cdalton862cff32016-05-12 15:09:48 -0700419 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget());
joshualitt2e2202e2015-12-10 11:22:08 -0800420
421 GrColor color = grPaint.getColor();
422
423 SkTextBlobRunIterator it(blob);
424 for (int run = 0; !it.done(); it.next(), run++) {
425 if (fRuns[run].fDrawAsPaths) {
joshualitt0a42e682015-12-10 13:20:58 -0800426 this->flushRunAsPaths(context, dc, props, it, clip, skPaint,
joshualitt2e2202e2015-12-10 11:22:08 -0800427 drawFilter, viewMatrix, clipBounds, x, y);
428 continue;
429 }
cdalton862cff32016-05-12 15:09:48 -0700430 this->flushRun(dc, &pipelineBuilder, clip, run, viewMatrix, x, y, color, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800431 distanceAdjustTable, context->getBatchFontCache());
432 }
433
434 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800435 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800436}
437
438void GrAtlasTextBlob::flushThrowaway(GrContext* context,
439 GrDrawContext* dc,
440 const SkSurfaceProps& props,
441 const GrDistanceFieldAdjustTable* distanceAdjustTable,
442 const SkPaint& skPaint,
443 const GrPaint& grPaint,
444 const GrClip& clip,
joshualitt8e0ef292016-02-19 14:13:03 -0800445 const SkMatrix& viewMatrix,
446 const SkIRect& clipBounds,
447 SkScalar x, SkScalar y) {
cdalton862cff32016-05-12 15:09:48 -0700448 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget());
joshualitt2e2202e2015-12-10 11:22:08 -0800449
450 GrColor color = grPaint.getColor();
451 for (int run = 0; run < fRunCount; run++) {
cdalton862cff32016-05-12 15:09:48 -0700452 this->flushRun(dc, &pipelineBuilder, clip, run, viewMatrix, x, y, color, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800453 distanceAdjustTable, context->getBatchFontCache());
454 }
455
456 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800457 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800458}
459
joshualitt323c2eb2016-01-20 06:48:47 -0800460GrDrawBatch* GrAtlasTextBlob::test_createBatch(
461 int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800462 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
463 GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800464 const SkPaint& skPaint, const SkSurfaceProps& props,
465 const GrDistanceFieldAdjustTable* distanceAdjustTable,
466 GrBatchFontCache* cache) {
joshualitt7481e752016-01-22 06:08:48 -0800467 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
joshualitt8e0ef292016-02-19 14:13:03 -0800468 return this->createBatch(info, glyphCount, run, subRun, viewMatrix, x, y, color, skPaint,
brianosman0586f5c2016-04-12 12:48:21 -0700469 props, distanceAdjustTable, false, cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800470}
joshualitt2e2202e2015-12-10 11:22:08 -0800471
joshualitt259fbf12015-07-21 11:39:34 -0700472void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800473 SkASSERT_RELEASE(l.fSize == r.fSize);
474 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700475
joshualitt2f2ee832016-02-10 08:52:24 -0800476 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
477 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
478 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700479
joshualitt2f2ee832016-02-10 08:52:24 -0800480 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
481 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
482 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700483
joshualitt2f2ee832016-02-10 08:52:24 -0800484 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700485 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
486 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
487 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
488
joshualitt2f2ee832016-02-10 08:52:24 -0800489 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700490 // We can't assert that these have the same translations
491 }
492
joshualitt2f2ee832016-02-10 08:52:24 -0800493 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800494 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
495 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
496 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
497 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700498
joshualitt2f2ee832016-02-10 08:52:24 -0800499 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700500 for (int i = 0; i < l.fRunCount; i++) {
501 const Run& lRun = l.fRuns[i];
502 const Run& rRun = r.fRuns[i];
503
joshualitt259fbf12015-07-21 11:39:34 -0700504 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800505 SkASSERT_RELEASE(rRun.fTypeface.get());
506 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface, rRun.fTypeface));
joshualitt259fbf12015-07-21 11:39:34 -0700507 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800508 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700509 }
510
joshualitt259fbf12015-07-21 11:39:34 -0700511
joshualitt2f2ee832016-02-10 08:52:24 -0800512 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
513 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700514 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700515
516 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800517 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
518 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700519 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
520 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700521 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800522 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700523 }
524
525 // color can be changed
526 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800527 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
528 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700529
joshualitt2f2ee832016-02-10 08:52:24 -0800530 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700531 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
532 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
533 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
534
joshualitt2f2ee832016-02-10 08:52:24 -0800535 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
536 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700537
joshualitt2f2ee832016-02-10 08:52:24 -0800538 if (lSubRun.strike()) {
539 SkASSERT_RELEASE(rSubRun.strike());
540 SkASSERT_RELEASE(GrBatchTextStrike::GetKey(*lSubRun.strike()) ==
541 GrBatchTextStrike::GetKey(*rSubRun.strike()));
542
543 } else {
544 SkASSERT_RELEASE(!rSubRun.strike());
545 }
546
547 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
548 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
549 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
550 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
551 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
552 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
553 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700554 }
555 }
556}
joshualitt8e0ef292016-02-19 14:13:03 -0800557
558void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
559 SkScalar x, SkScalar y, SkScalar* transX,
560 SkScalar* transY) {
561 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
562 fCurrentViewMatrix, fX, fY, transX, transY);
563 fCurrentViewMatrix = viewMatrix;
564 fX = x;
565 fY = y;
566}