blob: 9aca37a4364ae620c673b4a90ffc97c2c175bcca [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"
bsalomonbb243832016-07-22 07:10:19 -070012#include "GrPipelineBuilder.h"
Brian Salomon344ec422016-12-15 10:58:41 -050013#include "GrRenderTargetContext.h"
joshualitt0a42e682015-12-10 13:20:58 -080014#include "GrTextUtils.h"
joshualitt2e2202e2015-12-10 11:22:08 -080015#include "SkColorFilter.h"
16#include "SkDrawFilter.h"
joshualitte76b4bb2015-12-28 07:23:58 -080017#include "SkGlyphCache.h"
joshualitt2e2202e2015-12-10 11:22:08 -080018#include "SkTextBlobRunIterator.h"
Brian Salomon89527432016-12-16 09:52:16 -050019#include "ops/GrAtlasTextOp.h"
joshualitt2e2202e2015-12-10 11:22:08 -080020
joshualitt92303772016-02-10 11:55:52 -080021GrAtlasTextBlob* GrAtlasTextBlob::Create(GrMemoryPool* pool, int glyphCount, int runCount) {
22 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
23 // and size for the glyphIds array.
24 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
25 size_t size = sizeof(GrAtlasTextBlob) +
26 verticesCount +
27 glyphCount * sizeof(GrGlyph**) +
28 sizeof(GrAtlasTextBlob::Run) * runCount;
29
30 void* allocation = pool->allocate(size);
31 if (CACHE_SANITY_CHECK) {
32 sk_bzero(allocation, size);
33 }
34
35 GrAtlasTextBlob* cacheBlob = new (allocation) GrAtlasTextBlob;
36 cacheBlob->fSize = size;
37
38 // setup offsets for vertices / glyphs
39 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<unsigned char*>(cacheBlob);
40 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
41 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
42
43 // Initialize runs
44 for (int i = 0; i < runCount; i++) {
45 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
46 }
47 cacheBlob->fRunCount = runCount;
48 cacheBlob->fPool = pool;
49 return cacheBlob;
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;
bsalomon8b6fa5e2016-05-19 16:23:47 -070062 SkScalerContextEffects effects;
63 skPaint.getScalerContextDescriptor(&effects, desc, props, scalerContextFlags, viewMatrix);
joshualitte76b4bb2015-12-28 07:23:58 -080064 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
bsalomon8b6fa5e2016-05-19 16:23:47 -070065 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
66 run->fRasterizer = sk_ref_sp(effects.fRasterizer);
67 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Hal Canary144caf52016-11-07 17:57:18 -050068 return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
joshualitte76b4bb2015-12-28 07:23:58 -080069}
70
joshualittf528e0d2015-12-09 06:42:52 -080071void GrAtlasTextBlob::appendGlyph(int runIndex,
72 const SkRect& positions,
73 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -050074 GrAtlasTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080075 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070076 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -050077 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
joshualitta06e6ab2015-12-10 08:54:41 -080078
79 // If the glyph is too large we fall back to paths
80 if (glyph->fTooLargeForAtlas) {
Jim Van Verth08576e62016-11-16 10:15:23 -050081 this->appendLargeGlyph(glyph, cache, skGlyph, x, y, scale, treatAsBMP);
joshualitta06e6ab2015-12-10 08:54:41 -080082 return;
83 }
84
joshualittf528e0d2015-12-09 06:42:52 -080085 Run& run = fRuns[runIndex];
86 GrMaskFormat format = glyph->fMaskFormat;
87
88 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
89 if (run.fInitialized && subRun->maskFormat() != format) {
90 subRun = &run.push_back();
91 subRun->setStrike(strike);
92 } else if (!run.fInitialized) {
93 subRun->setStrike(strike);
94 }
95
96 run.fInitialized = true;
97
98 size_t vertexStride = GetVertexStride(format);
99
100 subRun->setMaskFormat(format);
101
joshualitt7481e752016-01-22 06:08:48 -0800102 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800103 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800104
105 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
106
joshualittf528e0d2015-12-09 06:42:52 -0800107 if (kARGB_GrMaskFormat != glyph->fMaskFormat) {
joshualitt18b072d2015-12-07 12:26:12 -0800108 // V0
109 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
110 position->set(positions.fLeft, positions.fTop);
111 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
112 *colorPtr = color;
113 vertex += vertexStride;
114
115 // V1
116 position = reinterpret_cast<SkPoint*>(vertex);
117 position->set(positions.fLeft, positions.fBottom);
118 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
119 *colorPtr = color;
120 vertex += vertexStride;
121
122 // V2
123 position = reinterpret_cast<SkPoint*>(vertex);
124 position->set(positions.fRight, positions.fBottom);
125 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
126 *colorPtr = color;
127 vertex += vertexStride;
128
129 // V3
130 position = reinterpret_cast<SkPoint*>(vertex);
131 position->set(positions.fRight, positions.fTop);
132 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
133 *colorPtr = color;
134 } else {
135 // V0
136 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
137 position->set(positions.fLeft, positions.fTop);
138 vertex += vertexStride;
139
140 // V1
141 position = reinterpret_cast<SkPoint*>(vertex);
142 position->set(positions.fLeft, positions.fBottom);
143 vertex += vertexStride;
144
145 // V2
146 position = reinterpret_cast<SkPoint*>(vertex);
147 position->set(positions.fRight, positions.fBottom);
148 vertex += vertexStride;
149
150 // V3
151 position = reinterpret_cast<SkPoint*>(vertex);
152 position->set(positions.fRight, positions.fTop);
153 }
154 subRun->appendVertices(vertexStride);
155 fGlyphs[subRun->glyphEndIndex()] = glyph;
156 subRun->glyphAppended();
157}
158
bsalomonc2878e22016-05-17 13:18:03 -0700159void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500160 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
joshualitta06e6ab2015-12-10 08:54:41 -0800161 if (nullptr == glyph->fPath) {
bsalomonc2878e22016-05-17 13:18:03 -0700162 const SkPath* glyphPath = cache->findPath(skGlyph);
joshualitta06e6ab2015-12-10 08:54:41 -0800163 if (!glyphPath) {
164 return;
165 }
166
167 glyph->fPath = new SkPath(*glyphPath);
168 }
Jim Van Verth08576e62016-11-16 10:15:23 -0500169 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, treatAsBMP));
joshualitta06e6ab2015-12-10 08:54:41 -0800170}
171
joshualitt8e0ef292016-02-19 14:13:03 -0800172bool GrAtlasTextBlob::mustRegenerate(const SkPaint& paint,
joshualittfd5f6c12015-12-10 07:44:50 -0800173 GrColor color, const SkMaskFilter::BlurRec& blurRec,
174 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
175 // If we have LCD text then our canonical color will be set to transparent, in this case we have
176 // to regenerate the blob on any color change
177 // We use the grPaint to get any color filter effects
178 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
179 fPaintColor != color) {
180 return true;
181 }
182
joshualitt8e0ef292016-02-19 14:13:03 -0800183 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800184 return true;
185 }
186
joshualitt8e0ef292016-02-19 14:13:03 -0800187 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800188 return true;
189 }
190
191 // We only cache one masked version
192 if (fKey.fHasBlur &&
193 (fBlurRec.fSigma != blurRec.fSigma ||
194 fBlurRec.fStyle != blurRec.fStyle ||
195 fBlurRec.fQuality != blurRec.fQuality)) {
196 return true;
197 }
198
199 // Similarly, we only cache one version for each style
200 if (fKey.fStyle != SkPaint::kFill_Style &&
201 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
202 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
203 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
204 return true;
205 }
206
207 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
208 // for mixed blobs if this becomes an issue.
209 if (this->hasBitmap() && this->hasDistanceField()) {
210 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800211 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800212 return false;
213 }
214 return true;
215 }
216
217 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800218 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
219 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
220 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
221 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800222 return true;
223 }
224
225 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
226 // but only for integer translations.
227 // This cool bit of math will determine the necessary translation to apply to the already
228 // generated vertex coordinates to move them to the correct position
229 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800230 viewMatrix.getScaleX() * (x - fInitialX) +
231 viewMatrix.getSkewX() * (y - fInitialY) -
232 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800233 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800234 viewMatrix.getSkewY() * (x - fInitialX) +
235 viewMatrix.getScaleY() * (y - fInitialY) -
236 fInitialViewMatrix.getTranslateY();
237 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800238 return true;
239 }
joshualittfd5f6c12015-12-10 07:44:50 -0800240 } else if (this->hasDistanceField()) {
241 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
242 // distance field being generated, so we have to regenerate in those cases
243 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800244 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800245 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
246 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
247 return true;
248 }
joshualittfd5f6c12015-12-10 07:44:50 -0800249 }
250
joshualittfd5f6c12015-12-10 07:44:50 -0800251 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
252 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
253 // the blob anyways at flush time, so no need to regenerate explicitly
254 return false;
255}
256
Brian Salomon344ec422016-12-15 10:58:41 -0500257inline sk_sp<GrDrawOp> GrAtlasTextBlob::makeOp(
258 const Run::SubRunInfo& info, int glyphCount, int run, int subRun,
259 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, GrColor color, const SkPaint& skPaint,
260 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomonf856fd12016-12-16 14:24:34 -0500261 bool useGammaCorrectDistanceTable, GrAtlasGlyphCache* 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
Brian Salomon09d994e2016-12-21 11:14:46 -0500271 sk_sp<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800272 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());
Brian Salomon09d994e2016-12-21 11:14:46 -0500281 op = GrAtlasTextOp::MakeDistanceField(glyphCount, cache, distanceAdjustTable,
282 useGammaCorrectDistanceTable, filteredColor,
283 info.hasUseLCDText(), useBGR);
joshualitt2e2202e2015-12-10 11:22:08 -0800284 } else {
Brian Salomon09d994e2016-12-21 11:14:46 -0500285 op = GrAtlasTextOp::MakeBitmap(format, glyphCount, cache);
joshualitt2e2202e2015-12-10 11:22:08 -0800286 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500287 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800288 geometry.fViewMatrix = viewMatrix;
joshualitt2e2202e2015-12-10 11:22:08 -0800289 geometry.fBlob = SkRef(this);
290 geometry.fRun = run;
291 geometry.fSubRun = subRun;
292 geometry.fColor = subRunColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800293 geometry.fX = x;
294 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500295 op->init();
joshualitt2e2202e2015-12-10 11:22:08 -0800296
Brian Salomon09d994e2016-12-21 11:14:46 -0500297 return std::move(op);
joshualitt2e2202e2015-12-10 11:22:08 -0800298}
299
300inline
Brian Osman11052242016-10-27 14:47:55 -0400301void GrAtlasTextBlob::flushRun(GrRenderTargetContext* rtc, const GrPaint& grPaint,
cdalton862cff32016-05-12 15:09:48 -0700302 const GrClip& clip, int run, const SkMatrix& viewMatrix, SkScalar x,
robertphillips640789d2016-07-18 14:56:06 -0700303 SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800304 const SkPaint& skPaint, const SkSurfaceProps& props,
305 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomonf856fd12016-12-16 14:24:34 -0500306 GrAtlasGlyphCache* cache) {
joshualitt2e2202e2015-12-10 11:22:08 -0800307 for (int subRun = 0; subRun < fRuns[run].fSubRunInfo.count(); subRun++) {
308 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
309 int glyphCount = info.glyphCount();
310 if (0 == glyphCount) {
311 continue;
312 }
313
robertphillips640789d2016-07-18 14:56:06 -0700314 GrColor color = grPaint.getColor();
315
Brian Salomon344ec422016-12-15 10:58:41 -0500316 sk_sp<GrDrawOp> op(this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, color,
317 skPaint, props, distanceAdjustTable, rtc->isGammaCorrect(),
318 cache));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500319 GrPipelineBuilder pipelineBuilder(grPaint, GrAAType::kNone);
bsalomonbb243832016-07-22 07:10:19 -0700320
Brian Salomon24f19782016-12-13 15:10:11 -0500321 rtc->addDrawOp(pipelineBuilder, clip, std::move(op));
joshualitt2e2202e2015-12-10 11:22:08 -0800322 }
323}
324
joshualitt8e0ef292016-02-19 14:13:03 -0800325static void calculate_translation(bool applyVM,
326 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
327 const SkMatrix& currentViewMatrix, SkScalar currentX,
328 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
329 if (applyVM) {
330 *transX = newViewMatrix.getTranslateX() +
331 newViewMatrix.getScaleX() * (newX - currentX) +
332 newViewMatrix.getSkewX() * (newY - currentY) -
333 currentViewMatrix.getTranslateX();
334
335 *transY = newViewMatrix.getTranslateY() +
336 newViewMatrix.getSkewY() * (newX - currentX) +
337 newViewMatrix.getScaleY() * (newY - currentY) -
338 currentViewMatrix.getTranslateY();
339 } else {
340 *transX = newX - currentX;
341 *transY = newY - currentY;
342 }
343}
344
345
Brian Osman11052242016-10-27 14:47:55 -0400346void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrRenderTargetContext* rtc,
joshualitt2e2202e2015-12-10 11:22:08 -0800347 const GrClip& clip, const SkPaint& skPaint,
joshualitt8e0ef292016-02-19 14:13:03 -0800348 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800349 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800350 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800351 for (int i = 0; i < fBigGlyphs.count(); i++) {
352 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
Jim Van Verth08576e62016-11-16 10:15:23 -0500353 calculate_translation(bigGlyph.fTreatAsBMP, viewMatrix, x, y,
joshualitt8e0ef292016-02-19 14:13:03 -0800354 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800355 SkMatrix ctm;
356 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800357 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
Jim Van Verth08576e62016-11-16 10:15:23 -0500358 if (!bigGlyph.fTreatAsBMP) {
joshualitt8e0ef292016-02-19 14:13:03 -0800359 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800360 }
361
Brian Osman11052242016-10-27 14:47:55 -0400362 GrBlurUtils::drawPathWithMaskFilter(context, rtc, clip, bigGlyph.fPath,
joshualitt2e2202e2015-12-10 11:22:08 -0800363 skPaint, ctm, nullptr, clipBounds, false);
364 }
365}
366
Brian Osman11052242016-10-27 14:47:55 -0400367void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrRenderTargetContext* rtc,
joshualitt2e2202e2015-12-10 11:22:08 -0800368 const SkSurfaceProps& props,
369 const SkTextBlobRunIterator& it,
370 const GrClip& clip, const SkPaint& skPaint,
371 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
372 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
373 SkPaint runPaint = skPaint;
374
375 size_t textLen = it.glyphCount() * sizeof(uint16_t);
376 const SkPoint& offset = it.offset();
377
378 it.applyFontToPaint(&runPaint);
379
380 if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
381 return;
382 }
383
joshualitt8e84a1e2016-02-16 11:09:25 -0800384 runPaint.setFlags(GrTextUtils::FilterTextFlags(props, runPaint));
joshualitt2e2202e2015-12-10 11:22:08 -0800385
386 switch (it.positioning()) {
387 case SkTextBlob::kDefault_Positioning:
Brian Osman11052242016-10-27 14:47:55 -0400388 GrTextUtils::DrawTextAsPath(context, rtc, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800389 (const char *)it.glyphs(),
390 textLen, x + offset.x(), y + offset.y(), clipBounds);
391 break;
392 case SkTextBlob::kHorizontal_Positioning:
Brian Osman11052242016-10-27 14:47:55 -0400393 GrTextUtils::DrawPosTextAsPath(context, rtc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800394 (const char*)it.glyphs(),
395 textLen, it.pos(), 1, SkPoint::Make(x, y + offset.y()),
396 clipBounds);
397 break;
398 case SkTextBlob::kFull_Positioning:
Brian Osman11052242016-10-27 14:47:55 -0400399 GrTextUtils::DrawPosTextAsPath(context, rtc, props, clip, runPaint, viewMatrix,
joshualitt2e2202e2015-12-10 11:22:08 -0800400 (const char*)it.glyphs(),
401 textLen, it.pos(), 2, SkPoint::Make(x, y), clipBounds);
402 break;
403 }
404}
405
joshualitt0a42e682015-12-10 13:20:58 -0800406void GrAtlasTextBlob::flushCached(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400407 GrRenderTargetContext* rtc,
joshualitt0a42e682015-12-10 13:20:58 -0800408 const SkTextBlob* blob,
joshualitt2e2202e2015-12-10 11:22:08 -0800409 const SkSurfaceProps& props,
410 const GrDistanceFieldAdjustTable* distanceAdjustTable,
411 const SkPaint& skPaint,
412 const GrPaint& grPaint,
413 SkDrawFilter* drawFilter,
414 const GrClip& clip,
415 const SkMatrix& viewMatrix,
416 const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800417 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800418 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
419 // it as paths
joshualitt2e2202e2015-12-10 11:22:08 -0800420 SkTextBlobRunIterator it(blob);
421 for (int run = 0; !it.done(); it.next(), run++) {
422 if (fRuns[run].fDrawAsPaths) {
Brian Osman11052242016-10-27 14:47:55 -0400423 this->flushRunAsPaths(context, rtc, props, it, clip, skPaint,
joshualitt2e2202e2015-12-10 11:22:08 -0800424 drawFilter, viewMatrix, clipBounds, x, y);
425 continue;
426 }
Brian Osman11052242016-10-27 14:47:55 -0400427 this->flushRun(rtc, grPaint, clip, run, viewMatrix, x, y, skPaint, props,
Brian Salomonf856fd12016-12-16 14:24:34 -0500428 distanceAdjustTable, context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800429 }
430
431 // Now flush big glyphs
Brian Osman11052242016-10-27 14:47:55 -0400432 this->flushBigGlyphs(context, rtc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800433}
434
435void GrAtlasTextBlob::flushThrowaway(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400436 GrRenderTargetContext* rtc,
joshualitt2e2202e2015-12-10 11:22:08 -0800437 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 for (int run = 0; run < fRunCount; run++) {
Brian Osman11052242016-10-27 14:47:55 -0400446 this->flushRun(rtc, grPaint, clip, run, viewMatrix, x, y, skPaint, props,
Brian Salomonf856fd12016-12-16 14:24:34 -0500447 distanceAdjustTable, context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800448 }
449
450 // Now flush big glyphs
Brian Osman11052242016-10-27 14:47:55 -0400451 this->flushBigGlyphs(context, rtc, clip, skPaint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800452}
453
Brian Salomon344ec422016-12-15 10:58:41 -0500454sk_sp<GrDrawOp> GrAtlasTextBlob::test_makeOp(int glyphCount, int run, int subRun,
455 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
456 GrColor color, const SkPaint& skPaint,
457 const SkSurfaceProps& props,
458 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomonf856fd12016-12-16 14:24:34 -0500459 GrAtlasGlyphCache* cache) {
joshualitt7481e752016-01-22 06:08:48 -0800460 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Brian Salomon344ec422016-12-15 10:58:41 -0500461 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, color, skPaint, props,
462 distanceAdjustTable, false, cache);
joshualitt323c2eb2016-01-20 06:48:47 -0800463}
joshualitt2e2202e2015-12-10 11:22:08 -0800464
joshualitt259fbf12015-07-21 11:39:34 -0700465void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800466 SkASSERT_RELEASE(l.fSize == r.fSize);
467 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700468
joshualitt2f2ee832016-02-10 08:52:24 -0800469 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
470 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
471 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700472
joshualitt2f2ee832016-02-10 08:52:24 -0800473 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
474 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
475 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700476
joshualitt2f2ee832016-02-10 08:52:24 -0800477 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700478 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
479 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
480 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
481
joshualitt2f2ee832016-02-10 08:52:24 -0800482 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700483 // We can't assert that these have the same translations
484 }
485
joshualitt2f2ee832016-02-10 08:52:24 -0800486 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800487 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
488 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
489 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
490 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700491
joshualitt2f2ee832016-02-10 08:52:24 -0800492 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700493 for (int i = 0; i < l.fRunCount; i++) {
494 const Run& lRun = l.fRuns[i];
495 const Run& rRun = r.fRuns[i];
496
joshualitt259fbf12015-07-21 11:39:34 -0700497 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800498 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500499 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700500 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800501 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700502 }
503
joshualitt259fbf12015-07-21 11:39:34 -0700504
joshualitt2f2ee832016-02-10 08:52:24 -0800505 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
506 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700507 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700508
509 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800510 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
511 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700512 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
513 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700514 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800515 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700516 }
517
518 // color can be changed
519 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800520 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
521 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700522
joshualitt2f2ee832016-02-10 08:52:24 -0800523 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700524 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
525 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
526 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
527
joshualitt2f2ee832016-02-10 08:52:24 -0800528 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
529 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700530
joshualitt2f2ee832016-02-10 08:52:24 -0800531 if (lSubRun.strike()) {
532 SkASSERT_RELEASE(rSubRun.strike());
Brian Salomonf856fd12016-12-16 14:24:34 -0500533 SkASSERT_RELEASE(GrAtlasTextStrike::GetKey(*lSubRun.strike()) ==
534 GrAtlasTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800535
536 } else {
537 SkASSERT_RELEASE(!rSubRun.strike());
538 }
539
540 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
541 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
542 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
543 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
544 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
545 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
546 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700547 }
548 }
549}
joshualitt8e0ef292016-02-19 14:13:03 -0800550
551void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
552 SkScalar x, SkScalar y, SkScalar* transX,
553 SkScalar* transY) {
554 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
555 fCurrentViewMatrix, fX, fY, transX, transY);
556 fCurrentViewMatrix = viewMatrix;
557 fX = x;
558 fY = y;
559}