blob: 4b339d4adbd4c7302fcab551875378aea17ae9ef [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;
brianosmana1e8f8d2016-04-08 06:47:54 -070062 skPaint.getScalerContextDescriptor(desc, props, scalerContextFlags, viewMatrix);
joshualitte76b4bb2015-12-28 07:23:58 -080063 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
64 return SkGlyphCache::DetachCache(run->fTypeface, desc->getDesc());
65}
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,
260 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,
281 distanceAdjustTable, filteredColor,
282 info.hasUseLCDText(), useBGR);
283 } else {
284 batch = GrAtlasTextBatch::CreateBitmap(format, glyphCount, cache);
285 }
286 GrAtlasTextBatch::Geometry& geometry = batch->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800287 geometry.fViewMatrix = viewMatrix;
joshualitt2e2202e2015-12-10 11:22:08 -0800288 geometry.fBlob = SkRef(this);
289 geometry.fRun = run;
290 geometry.fSubRun = subRun;
291 geometry.fColor = subRunColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800292 geometry.fX = x;
293 geometry.fY = y;
joshualitt2e2202e2015-12-10 11:22:08 -0800294 batch->init();
295
296 return batch;
297}
298
299inline
300void GrAtlasTextBlob::flushRun(GrDrawContext* dc, GrPipelineBuilder* pipelineBuilder,
joshualitt8e0ef292016-02-19 14:13:03 -0800301 int run, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
302 GrColor color,
joshualitt2e2202e2015-12-10 11:22:08 -0800303 const SkPaint& skPaint, const SkSurfaceProps& props,
304 const GrDistanceFieldAdjustTable* distanceAdjustTable,
305 GrBatchFontCache* cache) {
306 for (int subRun = 0; subRun < fRuns[run].fSubRunInfo.count(); subRun++) {
307 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
308 int glyphCount = info.glyphCount();
309 if (0 == glyphCount) {
310 continue;
311 }
312
313 SkAutoTUnref<GrDrawBatch> batch(this->createBatch(info, glyphCount, run,
joshualitt8e0ef292016-02-19 14:13:03 -0800314 subRun, viewMatrix, x, y, color,
joshualitt2e2202e2015-12-10 11:22:08 -0800315 skPaint, props,
316 distanceAdjustTable, cache));
317 dc->drawBatch(pipelineBuilder, batch);
318 }
319}
320
joshualitt8e0ef292016-02-19 14:13:03 -0800321static void calculate_translation(bool applyVM,
322 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
323 const SkMatrix& currentViewMatrix, SkScalar currentX,
324 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
325 if (applyVM) {
326 *transX = newViewMatrix.getTranslateX() +
327 newViewMatrix.getScaleX() * (newX - currentX) +
328 newViewMatrix.getSkewX() * (newY - currentY) -
329 currentViewMatrix.getTranslateX();
330
331 *transY = newViewMatrix.getTranslateY() +
332 newViewMatrix.getSkewY() * (newX - currentX) +
333 newViewMatrix.getScaleY() * (newY - currentY) -
334 currentViewMatrix.getTranslateY();
335 } else {
336 *transX = newX - currentX;
337 *transY = newY - currentY;
338 }
339}
340
341
joshualitt2e2202e2015-12-10 11:22:08 -0800342void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrDrawContext* dc,
343 const GrClip& clip, const SkPaint& skPaint,
joshualitt8e0ef292016-02-19 14:13:03 -0800344 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800345 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800346 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800347 for (int i = 0; i < fBigGlyphs.count(); i++) {
348 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
joshualitt8e0ef292016-02-19 14:13:03 -0800349 calculate_translation(bigGlyph.fApplyVM, viewMatrix, x, y,
350 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800351 SkMatrix ctm;
352 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800353 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800354 if (bigGlyph.fApplyVM) {
joshualitt8e0ef292016-02-19 14:13:03 -0800355 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800356 }
357
358 GrBlurUtils::drawPathWithMaskFilter(context, dc, clip, bigGlyph.fPath,
359 skPaint, ctm, nullptr, clipBounds, false);
360 }
361}
362
joshualitt0a42e682015-12-10 13:20:58 -0800363void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrDrawContext* dc,
joshualitt2e2202e2015-12-10 11:22:08 -0800364 const SkSurfaceProps& props,
365 const SkTextBlobRunIterator& it,
366 const GrClip& clip, const SkPaint& skPaint,
367 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
368 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
369 SkPaint runPaint = skPaint;
370
371 size_t textLen = it.glyphCount() * sizeof(uint16_t);
372 const SkPoint& offset = it.offset();
373
374 it.applyFontToPaint(&runPaint);
375
376 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
377 return;
378 }
379
joshualitt8e84a1e2016-02-16 11:09:25 -0800380 runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint));
joshualitt2e2202e2015-12-10 11:22:08 -0800381
382 switch (it.positioning()) {
383 case SkTextBlob::kDefault_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800384 GrTextUtils::DrawTextAsPath(context, dc, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800385 (const char *)it.glyphs(),
386 textLen, x + offset.x(), y + offset.y(), clipBounds);
387 break;
388 case SkTextBlob::kHorizontal_Positioning:
joshualitt0a42e682015-12-10 13:20:58 -0800389 GrTextUtils::DrawPosTextAsPath(context, dc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800390 (const char*)it.glyphs(),
391 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
392 clipBounds);
393 break;
394 case SkTextBlob::kFull_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(), 2, SkPoint::Make(x, y), clipBounds);
398 break;
399 }
400}
401
joshualitt0a42e682015-12-10 13:20:58 -0800402void GrAtlasTextBlob::flushCached(GrContext* context,
joshualitt2e2202e2015-12-10 11:22:08 -0800403 GrDrawContext* dc,
joshualitt0a42e682015-12-10 13:20:58 -0800404 const SkTextBlob* blob,
joshualitt2e2202e2015-12-10 11:22:08 -0800405 const SkSurfaceProps& props,
406 const GrDistanceFieldAdjustTable* distanceAdjustTable,
407 const SkPaint& skPaint,
408 const GrPaint& grPaint,
409 SkDrawFilter* drawFilter,
410 const GrClip& clip,
411 const SkMatrix& viewMatrix,
412 const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800413 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800414 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
415 // it as paths
416 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
417
418 GrColor color = grPaint.getColor();
419
420 SkTextBlobRunIterator it(blob);
421 for (int run = 0; !it.done(); it.next(), run++) {
422 if (fRuns[run].fDrawAsPaths) {
joshualitt0a42e682015-12-10 13:20:58 -0800423 this->flushRunAsPaths(context, dc, props, it, clip, skPaint,
joshualitt2e2202e2015-12-10 11:22:08 -0800424 drawFilter, viewMatrix, clipBounds, x, y);
425 continue;
426 }
joshualitt8e0ef292016-02-19 14:13:03 -0800427 this->flushRun(dc, &pipelineBuilder, run, viewMatrix, x, y, color, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800428 distanceAdjustTable, context->getBatchFontCache());
429 }
430
431 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800432 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800433}
434
435void GrAtlasTextBlob::flushThrowaway(GrContext* context,
436 GrDrawContext* dc,
437 const SkSurfaceProps& props,
438 const GrDistanceFieldAdjustTable* distanceAdjustTable,
439 const SkPaint& skPaint,
440 const GrPaint& grPaint,
441 const GrClip& clip,
joshualitt8e0ef292016-02-19 14:13:03 -0800442 const SkMatrix& viewMatrix,
443 const SkIRect& clipBounds,
444 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800445 GrPipelineBuilder pipelineBuilder(grPaint, dc->accessRenderTarget(), clip);
446
447 GrColor color = grPaint.getColor();
448 for (int run = 0; run < fRunCount; run++) {
joshualitt8e0ef292016-02-19 14:13:03 -0800449 this->flushRun(dc, &pipelineBuilder, run, viewMatrix, x, y, color, skPaint, props,
joshualitt2e2202e2015-12-10 11:22:08 -0800450 distanceAdjustTable, context->getBatchFontCache());
451 }
452
453 // Now flush big glyphs
joshualitt8e0ef292016-02-19 14:13:03 -0800454 this->flushBigGlyphs(context, dc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800455}
456
joshualitt323c2eb2016-01-20 06:48:47 -0800457GrDrawBatch* GrAtlasTextBlob::test_createBatch(
458 int glyphCount, int run, int subRun,
joshualitt8e0ef292016-02-19 14:13:03 -0800459 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
460 GrColor color,
joshualitt323c2eb2016-01-20 06:48:47 -0800461 const SkPaint& skPaint, const SkSurfaceProps& props,
462 const GrDistanceFieldAdjustTable* distanceAdjustTable,
463 GrBatchFontCache* cache) {
joshualitt7481e752016-01-22 06:08:48 -0800464 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
joshualitt8e0ef292016-02-19 14:13:03 -0800465 return this->createBatch(info, glyphCount, run, subRun, viewMatrix, x, y, color, skPaint,
joshualitt323c2eb2016-01-20 06:48:47 -0800466 props, distanceAdjustTable, cache);
467}
joshualitt2e2202e2015-12-10 11:22:08 -0800468
joshualitt259fbf12015-07-21 11:39:34 -0700469void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800470 SkASSERT_RELEASE(l.fSize == r.fSize);
471 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700472
joshualitt2f2ee832016-02-10 08:52:24 -0800473 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
474 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
475 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700476
joshualitt2f2ee832016-02-10 08:52:24 -0800477 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
478 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
479 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700480
joshualitt2f2ee832016-02-10 08:52:24 -0800481 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700482 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
483 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
484 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
485
joshualitt2f2ee832016-02-10 08:52:24 -0800486 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700487 // We can't assert that these have the same translations
488 }
489
joshualitt2f2ee832016-02-10 08:52:24 -0800490 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800491 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
492 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
493 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
494 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700495
joshualitt2f2ee832016-02-10 08:52:24 -0800496 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700497 for (int i = 0; i < l.fRunCount; i++) {
498 const Run& lRun = l.fRuns[i];
499 const Run& rRun = r.fRuns[i];
500
joshualitt259fbf12015-07-21 11:39:34 -0700501 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800502 SkASSERT_RELEASE(rRun.fTypeface.get());
503 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface, rRun.fTypeface));
joshualitt259fbf12015-07-21 11:39:34 -0700504 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800505 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700506 }
507
joshualitt259fbf12015-07-21 11:39:34 -0700508
joshualitt2f2ee832016-02-10 08:52:24 -0800509 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
510 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
511 SkASSERT_RELEASE(lRun.fDescriptor.getDesc()->equals(*rRun.fDescriptor.getDesc()));
joshualitt259fbf12015-07-21 11:39:34 -0700512
513 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800514 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
515 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
516 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc()->equals(
joshualitt259fbf12015-07-21 11:39:34 -0700517 *rRun.fOverrideDescriptor->getDesc()));
518 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800519 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700520 }
521
522 // color can be changed
523 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800524 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
525 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700526
joshualitt2f2ee832016-02-10 08:52:24 -0800527 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700528 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
529 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
530 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
531
joshualitt2f2ee832016-02-10 08:52:24 -0800532 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
533 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700534
joshualitt2f2ee832016-02-10 08:52:24 -0800535 if (lSubRun.strike()) {
536 SkASSERT_RELEASE(rSubRun.strike());
537 SkASSERT_RELEASE(GrBatchTextStrike::GetKey(*lSubRun.strike()) ==
538 GrBatchTextStrike::GetKey(*rSubRun.strike()));
539
540 } else {
541 SkASSERT_RELEASE(!rSubRun.strike());
542 }
543
544 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
545 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
546 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
547 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
548 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
549 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
550 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700551 }
552 }
553}
joshualitt8e0ef292016-02-19 14:13:03 -0800554
555void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
556 SkScalar x, SkScalar y, SkScalar* transX,
557 SkScalar* transY) {
558 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
559 fCurrentViewMatrix, fX, fY, transX, transY);
560 fCurrentViewMatrix = viewMatrix;
561 fX = x;
562 fY = y;
563}