blob: 7fce4ba8563eca0c861f73c0d593a187295951dc [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"
joshualitt2e2202e2015-12-10 11:22:08 -08009#include "GrBlurUtils.h"
Jim Van Verth58c3cce2017-10-19 15:50:24 -040010#include "GrClip.h"
joshualitt2e2202e2015-12-10 11:22:08 -080011#include "GrContext.h"
joshualitt0a42e682015-12-10 13:20:58 -080012#include "GrTextUtils.h"
joshualitt2e2202e2015-12-10 11:22:08 -080013#include "SkColorFilter.h"
14#include "SkDrawFilter.h"
joshualitte76b4bb32015-12-28 07:23:58 -080015#include "SkGlyphCache.h"
Mike Reed80747ef2018-01-23 15:29:32 -050016#include "SkMaskFilterBase.h"
joshualitt2e2202e2015-12-10 11:22:08 -080017#include "SkTextBlobRunIterator.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "ops/GrAtlasTextOp.h"
joshualitt2e2202e2015-12-10 11:22:08 -080019
Florin Malitac337c9e2017-03-10 18:02:29 +000020sk_sp<GrAtlasTextBlob> GrAtlasTextBlob::Make(GrMemoryPool* pool, int glyphCount, int runCount) {
joshualitt92303772016-02-10 11:55:52 -080021 // 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
Florin Malitac337c9e2017-03-10 18:02:29 +000034 sk_sp<GrAtlasTextBlob> cacheBlob(new (allocation) GrAtlasTextBlob);
joshualitt92303772016-02-10 11:55:52 -080035 cacheBlob->fSize = size;
36
37 // setup offsets for vertices / glyphs
Brian Salomon18923f92017-11-06 16:26:02 -050038 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080039 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
joshualitte76b4bb32015-12-28 07:23:58 -080051SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
52 const SkSurfaceProps& props,
Herb Derbyd8327a82018-01-22 14:39:27 -050053 SkScalerContextFlags scalerContextFlags,
joshualitte76b4bb32015-12-28 07:23:58 -080054 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -080055 const SkMatrix* viewMatrix) {
joshualitte76b4bb32015-12-28 07:23:58 -080056 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
57
58 // if we have an override descriptor for the run, then we should use that
59 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
60 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070061 SkScalerContextEffects effects;
Herb Derbyd8327a82018-01-22 14:39:27 -050062 skPaint.getScalerContextDescriptor(&effects, desc, &props, scalerContextFlags, viewMatrix);
joshualitte76b4bb32015-12-28 07:23:58 -080063 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
bsalomon8b6fa5e2016-05-19 16:23:47 -070064 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070065 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Hal Canary144caf52016-11-07 17:57:18 -050066 return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
joshualitte76b4bb32015-12-28 07:23:58 -080067}
68
joshualittf528e0d2015-12-09 06:42:52 -080069void GrAtlasTextBlob::appendGlyph(int runIndex,
70 const SkRect& positions,
71 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -050072 GrAtlasTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080073 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070074 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -050075 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
Lee Salzman26d3f212017-01-27 14:31:10 -050076 if (positions.isEmpty()) {
77 return;
78 }
joshualitta06e6ab2015-12-10 08:54:41 -080079
80 // If the glyph is too large we fall back to paths
81 if (glyph->fTooLargeForAtlas) {
Jim Van Verthf4c13162018-01-11 16:40:24 -050082 this->appendBigGlyph(glyph, cache, skGlyph, x, y, scale, treatAsBMP);
joshualitta06e6ab2015-12-10 08:54:41 -080083 return;
84 }
85
joshualittf528e0d2015-12-09 06:42:52 -080086 Run& run = fRuns[runIndex];
87 GrMaskFormat format = glyph->fMaskFormat;
88
89 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
90 if (run.fInitialized && subRun->maskFormat() != format) {
91 subRun = &run.push_back();
92 subRun->setStrike(strike);
93 } else if (!run.fInitialized) {
94 subRun->setStrike(strike);
95 }
96
97 run.fInitialized = true;
98
Brian Salomon5c6ac642017-12-19 11:09:32 -050099 bool hasW = subRun->hasWCoord();
100 // DF glyphs drawn in perspective must always have a w coord.
101 SkASSERT(hasW || !subRun->drawAsDistanceFields() || !fInitialViewMatrix.hasPerspective());
102 // Non-DF glyphs should never have a w coord.
103 SkASSERT(!hasW || subRun->drawAsDistanceFields());
104
105 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800106
107 subRun->setMaskFormat(format);
108
joshualitt7481e752016-01-22 06:08:48 -0800109 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800110 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800111
112 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
113
Brian Salomon5c6ac642017-12-19 11:09:32 -0500114 // We always write the third position component used by SDFs. If it is unused it gets
115 // overwritten. Similarly, we always write the color and the blob will later overwrite it
116 // with texture coords if it is unused.
117 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
118 // V0
119 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
120 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
121 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800122
Brian Salomon5c6ac642017-12-19 11:09:32 -0500123 // V1
124 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
125 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
126 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800127
Brian Salomon5c6ac642017-12-19 11:09:32 -0500128 // V2
129 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
130 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
131 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800132
Brian Salomon5c6ac642017-12-19 11:09:32 -0500133 // V3
134 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
135 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800136
joshualitt18b072d2015-12-07 12:26:12 -0800137 subRun->appendVertices(vertexStride);
138 fGlyphs[subRun->glyphEndIndex()] = glyph;
139 subRun->glyphAppended();
140}
141
Jim Van Verthf4c13162018-01-11 16:40:24 -0500142void GrAtlasTextBlob::appendBigGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500143 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
joshualitta06e6ab2015-12-10 08:54:41 -0800144 if (nullptr == glyph->fPath) {
bsalomonc2878e22016-05-17 13:18:03 -0700145 const SkPath* glyphPath = cache->findPath(skGlyph);
joshualitta06e6ab2015-12-10 08:54:41 -0800146 if (!glyphPath) {
147 return;
148 }
149
150 glyph->fPath = new SkPath(*glyphPath);
151 }
Jim Van Verth08576e62016-11-16 10:15:23 -0500152 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, treatAsBMP));
joshualitta06e6ab2015-12-10 08:54:41 -0800153}
154
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500155bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500156 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800157 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
158 // If we have LCD text then our canonical color will be set to transparent, in this case we have
159 // to regenerate the blob on any color change
160 // We use the grPaint to get any color filter effects
161 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400162 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800163 return true;
164 }
165
joshualitt8e0ef292016-02-19 14:13:03 -0800166 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800167 return true;
168 }
169
Brian Salomon5c6ac642017-12-19 11:09:32 -0500170 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800171 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800172 return true;
173 }
174
175 // We only cache one masked version
176 if (fKey.fHasBlur &&
177 (fBlurRec.fSigma != blurRec.fSigma ||
178 fBlurRec.fStyle != blurRec.fStyle ||
179 fBlurRec.fQuality != blurRec.fQuality)) {
180 return true;
181 }
182
183 // Similarly, we only cache one version for each style
184 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500185 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
186 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
187 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800188 return true;
189 }
190
191 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
192 // for mixed blobs if this becomes an issue.
193 if (this->hasBitmap() && this->hasDistanceField()) {
194 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800195 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800196 return false;
197 }
198 return true;
199 }
200
201 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800202 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
203 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
204 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
205 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800206 return true;
207 }
208
209 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
210 // but only for integer translations.
211 // This cool bit of math will determine the necessary translation to apply to the already
212 // generated vertex coordinates to move them to the correct position
213 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800214 viewMatrix.getScaleX() * (x - fInitialX) +
215 viewMatrix.getSkewX() * (y - fInitialY) -
216 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800217 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800218 viewMatrix.getSkewY() * (x - fInitialX) +
219 viewMatrix.getScaleY() * (y - fInitialY) -
220 fInitialViewMatrix.getTranslateY();
221 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800222 return true;
223 }
joshualittfd5f6c12015-12-10 07:44:50 -0800224 } else if (this->hasDistanceField()) {
225 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
226 // distance field being generated, so we have to regenerate in those cases
227 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800228 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800229 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
230 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
231 return true;
232 }
joshualittfd5f6c12015-12-10 07:44:50 -0800233 }
234
joshualittfd5f6c12015-12-10 07:44:50 -0800235 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
236 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
237 // the blob anyways at flush time, so no need to regenerate explicitly
238 return false;
239}
240
Brian Salomonf18b1d82017-10-27 11:30:49 -0400241inline std::unique_ptr<GrAtlasTextOp> GrAtlasTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400242 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400243 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
244 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400245 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
246 GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800247 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800248
Brian Salomon44acb5b2017-07-18 19:59:24 -0400249 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400250 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500251 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800252 if (info.drawAsDistanceFields()) {
joshualitt2e2202e2015-12-10 11:22:08 -0800253 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
Brian Salomon44acb5b2017-07-18 19:59:24 -0400254 op = GrAtlasTextOp::MakeDistanceField(
255 std::move(grPaint), glyphCount, cache, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400256 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400257 info.hasUseLCDText(), useBGR, info.isAntiAliased());
joshualitt2e2202e2015-12-10 11:22:08 -0800258 } else {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400259 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount, cache);
joshualitt2e2202e2015-12-10 11:22:08 -0800260 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500261 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800262 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400263 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800264 geometry.fBlob = SkRef(this);
265 geometry.fRun = run;
266 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500267 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400268 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800269 geometry.fX = x;
270 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500271 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400272 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800273}
274
Brian Salomonf18b1d82017-10-27 11:30:49 -0400275inline void GrAtlasTextBlob::flushRun(GrTextUtils::Target* target, const GrClip& clip, int run,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500276 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
277 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon82f44312017-01-11 13:42:54 -0500278 const GrDistanceFieldAdjustTable* distanceAdjustTable,
279 GrAtlasGlyphCache* cache) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400280 // GrAtlasTextBlob::makeOp only takes uint16_t values for run and subRun indices.
281 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
282 if (run >= (1 << 16)) {
283 return;
284 }
285 int lastRun = SkTMin(fRuns[run].fSubRunInfo.count(), 1 << 16) - 1;
Brian Salomon82f44312017-01-11 13:42:54 -0500286 for (int subRun = 0; subRun <= lastRun; subRun++) {
joshualitt2e2202e2015-12-10 11:22:08 -0800287 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
288 int glyphCount = info.glyphCount();
289 if (0 == glyphCount) {
290 continue;
291 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400292
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400293 bool skipClip = false;
294 bool submitOp = true;
295 SkIRect clipRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400296 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400297 SkRRect clipRRect;
298 GrAA aa;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400299 // We can clip geometrically if we're not using SDFs,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400300 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400301 if (!info.drawAsDistanceFields() && clip.isRRect(rtBounds, &clipRRect, &aa) &&
302 clipRRect.isRect() && GrAA::kNo == aa) {
303 skipClip = true;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400304 // We only need to do clipping work if the subrun isn't contained by the clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400305 SkRect subRunBounds;
306 this->computeSubRunBounds(&subRunBounds, run, subRun, viewMatrix, x, y);
307 if (!clipRRect.getBounds().contains(subRunBounds)) {
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400308 // If the subrun is completely outside, don't add an op for it
309 if (!clipRRect.getBounds().intersects(subRunBounds)) {
310 submitOp = false;
311 } else {
312 clipRRect.getBounds().round(&clipRect);
313 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400314 }
315 }
316
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400317 if (submitOp) {
318 auto op = this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, clipRect,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400319 std::move(paint), props, distanceAdjustTable, cache, target);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400320 if (op) {
321 if (skipClip) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400322 target->addDrawOp(GrNoClip(), std::move(op));
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400323 } else {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400324 target->addDrawOp(clip, std::move(op));
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400325 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400326 }
Brian Salomon44acb5b2017-07-18 19:59:24 -0400327 }
joshualitt2e2202e2015-12-10 11:22:08 -0800328 }
329}
330
joshualitt8e0ef292016-02-19 14:13:03 -0800331static void calculate_translation(bool applyVM,
332 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
333 const SkMatrix& currentViewMatrix, SkScalar currentX,
334 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
335 if (applyVM) {
336 *transX = newViewMatrix.getTranslateX() +
337 newViewMatrix.getScaleX() * (newX - currentX) +
338 newViewMatrix.getSkewX() * (newY - currentY) -
339 currentViewMatrix.getTranslateX();
340
341 *transY = newViewMatrix.getTranslateY() +
342 newViewMatrix.getSkewY() * (newX - currentX) +
343 newViewMatrix.getScaleY() * (newY - currentY) -
344 currentViewMatrix.getTranslateY();
345 } else {
346 *transX = newX - currentX;
347 *transY = newY - currentY;
348 }
349}
350
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500351void GrAtlasTextBlob::flushBigGlyphs(GrTextUtils::Target* target,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500352 const GrClip& clip, const SkPaint& paint,
joshualitt8e0ef292016-02-19 14:13:03 -0800353 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800354 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800355 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800356 for (int i = 0; i < fBigGlyphs.count(); i++) {
357 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
Jim Van Verth08576e62016-11-16 10:15:23 -0500358 calculate_translation(bigGlyph.fTreatAsBMP, viewMatrix, x, y,
joshualitt8e0ef292016-02-19 14:13:03 -0800359 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800360 SkMatrix ctm;
361 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800362 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
Jim Van Verth08576e62016-11-16 10:15:23 -0500363 if (!bigGlyph.fTreatAsBMP) {
joshualitt8e0ef292016-02-19 14:13:03 -0800364 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800365 }
Brian Salomonf18b1d82017-10-27 11:30:49 -0400366 target->drawPath(clip, bigGlyph.fPath, paint, ctm, nullptr, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800367 }
368}
369
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500370void GrAtlasTextBlob::flushBigRun(GrTextUtils::Target* target,
371 const SkSurfaceProps& props, const SkTextBlobRunIterator& it,
372 const GrClip& clip, const GrTextUtils::Paint& paint,
373 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
374 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800375 size_t textLen = it.glyphCount() * sizeof(uint16_t);
376 const SkPoint& offset = it.offset();
377
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500378 GrTextUtils::RunPaint runPaint(&paint, drawFilter, props);
379 if (!runPaint.modifyForRun(it)) {
joshualitt2e2202e2015-12-10 11:22:08 -0800380 return;
381 }
382
joshualitt2e2202e2015-12-10 11:22:08 -0800383 switch (it.positioning()) {
384 case SkTextBlob::kDefault_Positioning:
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500385 GrTextUtils::DrawBigText(target, clip, runPaint, viewMatrix,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500386 (const char*)it.glyphs(), textLen, x + offset.x(),
387 y + offset.y(), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800388 break;
389 case SkTextBlob::kHorizontal_Positioning:
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500390 GrTextUtils::DrawBigPosText(target, props, clip, runPaint, viewMatrix,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500391 (const char*)it.glyphs(), textLen, it.pos(), 1,
392 SkPoint::Make(x, y + offset.y()), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800393 break;
394 case SkTextBlob::kFull_Positioning:
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500395 GrTextUtils::DrawBigPosText(target, props, clip, runPaint, viewMatrix,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500396 (const char*)it.glyphs(), textLen, it.pos(), 2,
397 SkPoint::Make(x, y), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800398 break;
399 }
400}
401
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500402void GrAtlasTextBlob::flushCached(GrAtlasGlyphCache* atlasGlyphCache, GrTextUtils::Target* target,
Brian Salomon82f44312017-01-11 13:42:54 -0500403 const SkTextBlob* blob, const SkSurfaceProps& props,
joshualitt2e2202e2015-12-10 11:22:08 -0800404 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500405 const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter,
406 const GrClip& clip, const SkMatrix& viewMatrix,
407 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800408 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
409 // it as paths
joshualitt2e2202e2015-12-10 11:22:08 -0800410 SkTextBlobRunIterator it(blob);
411 for (int run = 0; !it.done(); it.next(), run++) {
Jim Van Verthf4c13162018-01-11 16:40:24 -0500412 if (fRuns[run].fTooBigForAtlas) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500413 this->flushBigRun(target, props, it, clip, paint, drawFilter, viewMatrix,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500414 clipBounds, x, y);
joshualitt2e2202e2015-12-10 11:22:08 -0800415 continue;
416 }
Brian Salomonf18b1d82017-10-27 11:30:49 -0400417 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500418 atlasGlyphCache);
joshualitt2e2202e2015-12-10 11:22:08 -0800419 }
420
421 // Now flush big glyphs
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500422 this->flushBigGlyphs(target, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800423}
424
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500425void GrAtlasTextBlob::flushThrowaway(GrAtlasGlyphCache* atlasGlyphCache,
426 GrTextUtils::Target* target,
joshualitt2e2202e2015-12-10 11:22:08 -0800427 const SkSurfaceProps& props,
428 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500429 const GrTextUtils::Paint& paint, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500430 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800431 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800432 for (int run = 0; run < fRunCount; run++) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400433 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500434 atlasGlyphCache);
joshualitt2e2202e2015-12-10 11:22:08 -0800435 }
436
437 // Now flush big glyphs
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500438 this->flushBigGlyphs(target, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800439}
440
Brian Salomon44acb5b2017-07-18 19:59:24 -0400441std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400442 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
443 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400444 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400445 GrTextUtils::Target* target) {
joshualitt7481e752016-01-22 06:08:48 -0800446 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400447 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400448 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
449 distanceAdjustTable, cache, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800450}
joshualitt2e2202e2015-12-10 11:22:08 -0800451
joshualitt259fbf12015-07-21 11:39:34 -0700452void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800453 SkASSERT_RELEASE(l.fSize == r.fSize);
454 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700455
joshualitt2f2ee832016-02-10 08:52:24 -0800456 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
457 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
458 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700459
joshualitt2f2ee832016-02-10 08:52:24 -0800460 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
461 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
462 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700463
joshualitt2f2ee832016-02-10 08:52:24 -0800464 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700465 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
466 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
467 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
468
joshualitt2f2ee832016-02-10 08:52:24 -0800469 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700470 // We can't assert that these have the same translations
471 }
472
joshualitt2f2ee832016-02-10 08:52:24 -0800473 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800474 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
475 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
476 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
477 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700478
joshualitt2f2ee832016-02-10 08:52:24 -0800479 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700480 for (int i = 0; i < l.fRunCount; i++) {
481 const Run& lRun = l.fRuns[i];
482 const Run& rRun = r.fRuns[i];
483
joshualitt259fbf12015-07-21 11:39:34 -0700484 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800485 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500486 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700487 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800488 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700489 }
490
joshualitt259fbf12015-07-21 11:39:34 -0700491
joshualitt2f2ee832016-02-10 08:52:24 -0800492 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
493 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700494 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700495
496 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800497 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
498 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700499 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
500 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700501 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800502 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700503 }
504
505 // color can be changed
506 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800507 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500508 SkASSERT_RELEASE(lRun.fTooBigForAtlas == rRun.fTooBigForAtlas);
joshualitt259fbf12015-07-21 11:39:34 -0700509
joshualitt2f2ee832016-02-10 08:52:24 -0800510 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700511 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
512 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
513 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
514
joshualitt2f2ee832016-02-10 08:52:24 -0800515 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
516 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700517
joshualitt2f2ee832016-02-10 08:52:24 -0800518 if (lSubRun.strike()) {
519 SkASSERT_RELEASE(rSubRun.strike());
Brian Salomonf856fd12016-12-16 14:24:34 -0500520 SkASSERT_RELEASE(GrAtlasTextStrike::GetKey(*lSubRun.strike()) ==
521 GrAtlasTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800522
523 } else {
524 SkASSERT_RELEASE(!rSubRun.strike());
525 }
526
527 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
528 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
529 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
530 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
531 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
532 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
533 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700534 }
535 }
536}
joshualitt8e0ef292016-02-19 14:13:03 -0800537
538void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
539 SkScalar x, SkScalar y, SkScalar* transX,
540 SkScalar* transY) {
541 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
542 fCurrentViewMatrix, fX, fY, transX, transY);
543 fCurrentViewMatrix = viewMatrix;
544 fX = x;
545 fY = y;
546}