blob: 65b84ddbc8fe57b99c51d4cdd923724db117014a [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,
joshualitt8e0ef292016-02-19 14:13:03 -0800303 int run, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
304 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
brianosmanb461d342016-04-13 13:10:14 -0700315 bool useGammaCorrectTable = GrPixelConfigIsSRGB(dc->accessRenderTarget()->config()) &&
brianosman0586f5c2016-04-12 12:48:21 -0700316 !pipelineBuilder->getDisableOutputConversionToSRGB();
317
joshualitt2e2202e2015-12-10 11:22:08 -0800318 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(info, glyphCount, run,
joshualitt8e0ef292016-02-19 14:13:03 -0800319 subRun, viewMatrix, x, y, color,
joshualitt2e2202e2015-12-10 11:22:08 -0800320 skPaint, props,
brianosmanb461d342016-04-13 13:10:14 -0700321 distanceAdjustTable, useGammaCorrectTable,
brianosman0586f5c2016-04-12 12:48:21 -0700322 cache));
joshualitt2e2202e2015-12-10 11:22:08 -0800323 dc->drawBatch(pipelineBuilder, batch);
324 }
325}
326
joshualitt8e0ef292016-02-19 14:13:03 -0800327static void calculate_translation(bool applyVM,
328 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
329 const SkMatrix& currentViewMatrix, SkScalar currentX,
330 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
331 if (applyVM) {
332 *transX = newViewMatrix.getTranslateX() +
333 newViewMatrix.getScaleX() * (newX - currentX) +
334 newViewMatrix.getSkewX() * (newY - currentY) -
335 currentViewMatrix.getTranslateX();
336
337 *transY = newViewMatrix.getTranslateY() +
338 newViewMatrix.getSkewY() * (newX - currentX) +
339 newViewMatrix.getScaleY() * (newY - currentY) -
340 currentViewMatrix.getTranslateY();
341 } else {
342 *transX = newX - currentX;
343 *transY = newY - currentY;
344 }
345}
346
347
joshualitt2e2202e2015-12-10 11:22:08 -0800348void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrDrawContext* dc,
349 const GrClip& clip, const SkPaint& skPaint,
joshualitt8e0ef292016-02-19 14:13:03 -0800350 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800351 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800352 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800353 for (int i = 0; i < fBigGlyphs.count(); i++) {
354 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
joshualitt8e0ef292016-02-19 14:13:03 -0800355 calculate_translation(bigGlyph.fApplyVM, viewMatrix, x, y,
356 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800357 SkMatrix ctm;
358 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800359 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800360 if (bigGlyph.fApplyVM) {
joshualitt8e0ef292016-02-19 14:13:03 -0800361 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800362 }
363
364 GrBlurUtils::drawPathWithMaskFilter(context, dc, clip, bigGlyph.fPath,
365 skPaint, ctm, nullptr, clipBounds, false);
366 }
367}
368
joshualitt0a42e682015-12-10 13:20:58 -0800369void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrDrawContext* dc,
joshualitt2e2202e2015-12-10 11:22:08 -0800370 const SkSurfaceProps& props,
371 const SkTextBlobRunIterator& it,
372 const GrClip& clip, const SkPaint& skPaint,
373 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
374 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
375 SkPaint runPaint = skPaint;
376
377 size_t textLen = it.glyphCount() * sizeof(uint16_t);
378 const SkPoint& offset = it.offset();
379
380 it.applyFontToPaint(&runPaint);
381
382 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
383 return;
384 }
385
joshualitt8e84a1e2016-02-16 11:09:25 -0800386 runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint));
joshualitt2e2202e2015-12-10 11:22:08 -0800387
388 switch (it.positioning()) {
389 case SkTextBlob::kDefault_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800390 GrTextUtils::DrawTextAsPath(context, dc, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800391 (const char *)it.glyphs(),
392 textLen, x + offset.x(), y + offset.y(), clipBounds);
393 break;
394 case SkTextBlob::kHorizontal_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800395 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800396 (const char*)it.glyphs(),
397 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
398 clipBounds);
399 break;
400 case SkTextBlob::kFull_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800401 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800402 (const char*)it.glyphs(),
403 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
404 break;
405 }
406}
407
joshualitt0a42e682015-12-10 13:20:58 -0800408void GrAtlasTextBlob::flushCached(GrContext* context,
joshualitt2e2202e2015-12-10 11:22:08 -0800409 GrDrawContext* dc,
joshualitt0a42e682015-12-10 13:20:58 -0800410 const SkTextBlob* blob,
joshualitt2e2202e2015-12-10 11:22:08 -0800411 const SkSurfaceProps& props,
412 const GrDistanceFieldAdjustTable* distanceAdjustTable,
413 const SkPaint& skPaint,
414 const GrPaint& grPaint,
415 SkDrawFilter* drawFilter,
416 const GrClip& clip,
417 const SkMatrix& viewMatrix,
418 const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800419 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800420 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
421 // it as paths
422 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
423
424 GrColor color = grPaint.getColor();
425
426 SkTextBlobRunIterator it(blob);
427 for (int run = 0; !it.done(); it.next(), run++) {
428 if (fRuns[run].fDrawAsPaths) {
joshualitt0a42e682015-12-10 13:20:58 -0800429 this->flushRunAsPaths(context, dc, props, it, clip, skPaint,
joshualitt2e2202e2015-12-10 11:22:08 -0800430 drawFilter, viewMatrix, clipBounds, x, y);
431 continue;
432 }
joshualitt8e0ef292016-02-19 14:13:03 -0800433 this->flushRun(dc, &pipelineBuilder, run, viewMatrix, x, y, color, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800434 distanceAdjustTable, context->getBatchFontCache());
435 }
436
437 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800438 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800439}
440
441void GrAtlasTextBlob::flushThrowaway(GrContext* context,
442 GrDrawContext* dc,
443 const SkSurfaceProps& props,
444 const GrDistanceFieldAdjustTable* distanceAdjustTable,
445 const SkPaint& skPaint,
446 const GrPaint& grPaint,
447 const GrClip& clip,
joshualitt8e0ef292016-02-19 14:13:03 -0800448 const SkMatrix& viewMatrix,
449 const SkIRect& clipBounds,
450 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800451 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
452
453 GrColor color = grPaint.getColor();
454 for (int run = 0; run < fRunCount; run++) {
joshualitt8e0ef292016-02-19 14:13:03 -0800455 this->flushRun(dc, &pipelineBuilder, run, viewMatrix, x, y, color, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800456 distanceAdjustTable, context->getBatchFontCache());
457 }
458
459 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800460 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800461}
462
joshualitt323c2eb2016-01-20 06:48:47 -0800463GrDrawBatch* GrAtlasTextBlob::test_createBatch(
464 int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800465 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
466 GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800467 const SkPaint& skPaint, const SkSurfaceProps& props,
468 const GrDistanceFieldAdjustTable* distanceAdjustTable,
469 GrBatchFontCache* cache) {
joshualitt7481e752016-01-22 06:08:48 -0800470 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
joshualitt8e0ef292016-02-19 14:13:03 -0800471 return this->createBatch(info, glyphCount, run, subRun, viewMatrix, x, y, color, skPaint,
brianosman0586f5c2016-04-12 12:48:21 -0700472 props, distanceAdjustTable, false, cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800473}
joshualitt2e2202e2015-12-10 11:22:08 -0800474
joshualitt259fbf12015-07-21 11:39:34 -0700475void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800476 SkASSERT_RELEASE(l.fSize == r.fSize);
477 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700478
joshualitt2f2ee832016-02-10 08:52:24 -0800479 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
480 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
481 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700482
joshualitt2f2ee832016-02-10 08:52:24 -0800483 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
484 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
485 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700486
joshualitt2f2ee832016-02-10 08:52:24 -0800487 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700488 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
489 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
490 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
491
joshualitt2f2ee832016-02-10 08:52:24 -0800492 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700493 // We can't assert that these have the same translations
494 }
495
joshualitt2f2ee832016-02-10 08:52:24 -0800496 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800497 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
498 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
499 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
500 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700501
joshualitt2f2ee832016-02-10 08:52:24 -0800502 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700503 for (int i = 0; i < l.fRunCount; i++) {
504 const Run& lRun = l.fRuns[i];
505 const Run& rRun = r.fRuns[i];
506
joshualitt259fbf12015-07-21 11:39:34 -0700507 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800508 SkASSERT_RELEASE(rRun.fTypeface.get());
509 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface, rRun.fTypeface));
joshualitt259fbf12015-07-21 11:39:34 -0700510 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800511 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700512 }
513
joshualitt259fbf12015-07-21 11:39:34 -0700514
joshualitt2f2ee832016-02-10 08:52:24 -0800515 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
516 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
517 SkASSERT_RELEASE(lRun.fDescriptor.getDesc()->equals(*rRun.fDescriptor.getDesc()));
joshualitt259fbf12015-07-21 11:39:34 -0700518
519 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800520 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
521 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
522 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc()->equals(
joshualitt259fbf12015-07-21 11:39:34 -0700523 *rRun.fOverrideDescriptor->getDesc()));
524 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800525 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700526 }
527
528 // color can be changed
529 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800530 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
531 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700532
joshualitt2f2ee832016-02-10 08:52:24 -0800533 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700534 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
535 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
536 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
537
joshualitt2f2ee832016-02-10 08:52:24 -0800538 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
539 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700540
joshualitt2f2ee832016-02-10 08:52:24 -0800541 if (lSubRun.strike()) {
542 SkASSERT_RELEASE(rSubRun.strike());
543 SkASSERT_RELEASE(GrBatchTextStrike::GetKey(*lSubRun.strike()) ==
544 GrBatchTextStrike::GetKey(*rSubRun.strike()));
545
546 } else {
547 SkASSERT_RELEASE(!rSubRun.strike());
548 }
549
550 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
551 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
552 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
553 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
554 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
555 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
556 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700557 }
558 }
559}
joshualitt8e0ef292016-02-19 14:13:03 -0800560
561void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
562 SkScalar x, SkScalar y, SkScalar* transX,
563 SkScalar* transY) {
564 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
565 fCurrentViewMatrix, fX, fY, transX, transY);
566 fCurrentViewMatrix = viewMatrix;
567 fX = x;
568 fY = y;
569}