blob: 9e9f58a4d630bccfcbcbe4327b4ffe09b0c1a3fe [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"
joshualitte76b4bb2015-12-28 07:23:58 -080015#include "SkGlyphCache.h"
joshualitt2e2202e2015-12-10 11:22:08 -080016#include "SkTextBlobRunIterator.h"
Brian Salomon89527432016-12-16 09:52:16 -050017#include "ops/GrAtlasTextOp.h"
joshualitt2e2202e2015-12-10 11:22:08 -080018
Florin Malitac337c9e2017-03-10 18:02:29 +000019sk_sp<GrAtlasTextBlob> GrAtlasTextBlob::Make(GrMemoryPool* pool, int glyphCount, int runCount) {
joshualitt92303772016-02-10 11:55:52 -080020 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
21 // and size for the glyphIds array.
22 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
23 size_t size = sizeof(GrAtlasTextBlob) +
24 verticesCount +
25 glyphCount * sizeof(GrGlyph**) +
26 sizeof(GrAtlasTextBlob::Run) * runCount;
27
28 void* allocation = pool->allocate(size);
29 if (CACHE_SANITY_CHECK) {
30 sk_bzero(allocation, size);
31 }
32
Florin Malitac337c9e2017-03-10 18:02:29 +000033 sk_sp<GrAtlasTextBlob> cacheBlob(new (allocation) GrAtlasTextBlob);
joshualitt92303772016-02-10 11:55:52 -080034 cacheBlob->fSize = size;
35
36 // setup offsets for vertices / glyphs
Brian Salomon18923f92017-11-06 16:26:02 -050037 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080038 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
39 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
40
41 // Initialize runs
42 for (int i = 0; i < runCount; i++) {
43 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
44 }
45 cacheBlob->fRunCount = runCount;
46 cacheBlob->fPool = pool;
47 return cacheBlob;
48}
49
joshualitte76b4bb2015-12-28 07:23:58 -080050SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
51 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -070052 uint32_t scalerContextFlags,
joshualitte76b4bb2015-12-28 07:23:58 -080053 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -080054 const SkMatrix* viewMatrix) {
joshualitte76b4bb2015-12-28 07:23:58 -080055 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
56
57 // if we have an override descriptor for the run, then we should use that
58 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
59 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070060 SkScalerContextEffects effects;
61 skPaint.getScalerContextDescriptor(&effects, desc, props, scalerContextFlags, viewMatrix);
joshualitte76b4bb2015-12-28 07:23:58 -080062 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
bsalomon8b6fa5e2016-05-19 16:23:47 -070063 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070064 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Hal Canary144caf52016-11-07 17:57:18 -050065 return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
joshualitte76b4bb2015-12-28 07:23:58 -080066}
67
joshualittf528e0d2015-12-09 06:42:52 -080068void GrAtlasTextBlob::appendGlyph(int runIndex,
69 const SkRect& positions,
70 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -050071 GrAtlasTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080072 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070073 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -050074 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
Lee Salzman26d3f212017-01-27 14:31:10 -050075 if (positions.isEmpty()) {
76 return;
77 }
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 Verthf4c13162018-01-11 16:40:24 -050081 this->appendBigGlyph(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
Brian Salomon5c6ac642017-12-19 11:09:32 -050098 bool hasW = subRun->hasWCoord();
99 // DF glyphs drawn in perspective must always have a w coord.
100 SkASSERT(hasW || !subRun->drawAsDistanceFields() || !fInitialViewMatrix.hasPerspective());
101 // Non-DF glyphs should never have a w coord.
102 SkASSERT(!hasW || subRun->drawAsDistanceFields());
103
104 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800105
106 subRun->setMaskFormat(format);
107
joshualitt7481e752016-01-22 06:08:48 -0800108 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800109 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800110
111 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
112
Brian Salomon5c6ac642017-12-19 11:09:32 -0500113 // We always write the third position component used by SDFs. If it is unused it gets
114 // overwritten. Similarly, we always write the color and the blob will later overwrite it
115 // with texture coords if it is unused.
116 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
117 // V0
118 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
119 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
120 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800121
Brian Salomon5c6ac642017-12-19 11:09:32 -0500122 // V1
123 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
124 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
125 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800126
Brian Salomon5c6ac642017-12-19 11:09:32 -0500127 // V2
128 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
129 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
130 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800131
Brian Salomon5c6ac642017-12-19 11:09:32 -0500132 // V3
133 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
134 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800135
joshualitt18b072d2015-12-07 12:26:12 -0800136 subRun->appendVertices(vertexStride);
137 fGlyphs[subRun->glyphEndIndex()] = glyph;
138 subRun->glyphAppended();
139}
140
Jim Van Verthf4c13162018-01-11 16:40:24 -0500141void GrAtlasTextBlob::appendBigGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500142 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
joshualitta06e6ab2015-12-10 08:54:41 -0800143 if (nullptr == glyph->fPath) {
bsalomonc2878e22016-05-17 13:18:03 -0700144 const SkPath* glyphPath = cache->findPath(skGlyph);
joshualitta06e6ab2015-12-10 08:54:41 -0800145 if (!glyphPath) {
146 return;
147 }
148
149 glyph->fPath = new SkPath(*glyphPath);
150 }
Jim Van Verth08576e62016-11-16 10:15:23 -0500151 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, treatAsBMP));
joshualitta06e6ab2015-12-10 08:54:41 -0800152}
153
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500154bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
155 const SkMaskFilter::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800156 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
157 // If we have LCD text then our canonical color will be set to transparent, in this case we have
158 // to regenerate the blob on any color change
159 // We use the grPaint to get any color filter effects
160 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400161 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800162 return true;
163 }
164
joshualitt8e0ef292016-02-19 14:13:03 -0800165 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800166 return true;
167 }
168
Brian Salomon5c6ac642017-12-19 11:09:32 -0500169 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800170 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800171 return true;
172 }
173
174 // We only cache one masked version
175 if (fKey.fHasBlur &&
176 (fBlurRec.fSigma != blurRec.fSigma ||
177 fBlurRec.fStyle != blurRec.fStyle ||
178 fBlurRec.fQuality != blurRec.fQuality)) {
179 return true;
180 }
181
182 // Similarly, we only cache one version for each style
183 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500184 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
185 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
186 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800187 return true;
188 }
189
190 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
191 // for mixed blobs if this becomes an issue.
192 if (this->hasBitmap() && this->hasDistanceField()) {
193 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800194 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800195 return false;
196 }
197 return true;
198 }
199
200 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800201 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
202 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
203 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
204 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800205 return true;
206 }
207
208 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
209 // but only for integer translations.
210 // This cool bit of math will determine the necessary translation to apply to the already
211 // generated vertex coordinates to move them to the correct position
212 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800213 viewMatrix.getScaleX() * (x - fInitialX) +
214 viewMatrix.getSkewX() * (y - fInitialY) -
215 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800216 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800217 viewMatrix.getSkewY() * (x - fInitialX) +
218 viewMatrix.getScaleY() * (y - fInitialY) -
219 fInitialViewMatrix.getTranslateY();
220 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800221 return true;
222 }
joshualittfd5f6c12015-12-10 07:44:50 -0800223 } else if (this->hasDistanceField()) {
224 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
225 // distance field being generated, so we have to regenerate in those cases
226 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800227 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800228 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
229 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
230 return true;
231 }
joshualittfd5f6c12015-12-10 07:44:50 -0800232 }
233
joshualittfd5f6c12015-12-10 07:44:50 -0800234 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
235 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
236 // the blob anyways at flush time, so no need to regenerate explicitly
237 return false;
238}
239
Brian Salomonf18b1d82017-10-27 11:30:49 -0400240inline std::unique_ptr<GrAtlasTextOp> GrAtlasTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400241 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400242 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
243 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400244 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
245 GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800246 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800247
Brian Salomon44acb5b2017-07-18 19:59:24 -0400248 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400249 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500250 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800251 if (info.drawAsDistanceFields()) {
joshualitt2e2202e2015-12-10 11:22:08 -0800252 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
Brian Salomon44acb5b2017-07-18 19:59:24 -0400253 op = GrAtlasTextOp::MakeDistanceField(
254 std::move(grPaint), glyphCount, cache, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400255 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400256 info.hasUseLCDText(), useBGR, info.isAntiAliased());
joshualitt2e2202e2015-12-10 11:22:08 -0800257 } else {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400258 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount, cache);
joshualitt2e2202e2015-12-10 11:22:08 -0800259 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500260 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800261 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400262 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800263 geometry.fBlob = SkRef(this);
264 geometry.fRun = run;
265 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500266 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400267 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800268 geometry.fX = x;
269 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500270 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400271 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800272}
273
Brian Salomonf18b1d82017-10-27 11:30:49 -0400274inline void GrAtlasTextBlob::flushRun(GrTextUtils::Target* target, const GrClip& clip, int run,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500275 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
276 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon82f44312017-01-11 13:42:54 -0500277 const GrDistanceFieldAdjustTable* distanceAdjustTable,
278 GrAtlasGlyphCache* cache) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400279 // GrAtlasTextBlob::makeOp only takes uint16_t values for run and subRun indices.
280 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
281 if (run >= (1 << 16)) {
282 return;
283 }
284 int lastRun = SkTMin(fRuns[run].fSubRunInfo.count(), 1 << 16) - 1;
Brian Salomon82f44312017-01-11 13:42:54 -0500285 for (int subRun = 0; subRun <= lastRun; subRun++) {
joshualitt2e2202e2015-12-10 11:22:08 -0800286 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
287 int glyphCount = info.glyphCount();
288 if (0 == glyphCount) {
289 continue;
290 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400291
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400292 bool skipClip = false;
293 bool submitOp = true;
294 SkIRect clipRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400295 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400296 SkRRect clipRRect;
297 GrAA aa;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400298 // We can clip geometrically if we're not using SDFs,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400299 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400300 if (!info.drawAsDistanceFields() && clip.isRRect(rtBounds, &clipRRect, &aa) &&
301 clipRRect.isRect() && GrAA::kNo == aa) {
302 skipClip = true;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400303 // We only need to do clipping work if the subrun isn't contained by the clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400304 SkRect subRunBounds;
305 this->computeSubRunBounds(&subRunBounds, run, subRun, viewMatrix, x, y);
306 if (!clipRRect.getBounds().contains(subRunBounds)) {
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400307 // If the subrun is completely outside, don't add an op for it
308 if (!clipRRect.getBounds().intersects(subRunBounds)) {
309 submitOp = false;
310 } else {
311 clipRRect.getBounds().round(&clipRect);
312 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400313 }
314 }
315
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400316 if (submitOp) {
317 auto op = this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, clipRect,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400318 std::move(paint), props, distanceAdjustTable, cache, target);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400319 if (op) {
320 if (skipClip) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400321 target->addDrawOp(GrNoClip(), std::move(op));
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400322 } else {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400323 target->addDrawOp(clip, std::move(op));
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400324 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400325 }
Brian Salomon44acb5b2017-07-18 19:59:24 -0400326 }
joshualitt2e2202e2015-12-10 11:22:08 -0800327 }
328}
329
joshualitt8e0ef292016-02-19 14:13:03 -0800330static void calculate_translation(bool applyVM,
331 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
332 const SkMatrix& currentViewMatrix, SkScalar currentX,
333 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
334 if (applyVM) {
335 *transX = newViewMatrix.getTranslateX() +
336 newViewMatrix.getScaleX() * (newX - currentX) +
337 newViewMatrix.getSkewX() * (newY - currentY) -
338 currentViewMatrix.getTranslateX();
339
340 *transY = newViewMatrix.getTranslateY() +
341 newViewMatrix.getSkewY() * (newX - currentX) +
342 newViewMatrix.getScaleY() * (newY - currentY) -
343 currentViewMatrix.getTranslateY();
344 } else {
345 *transX = newX - currentX;
346 *transY = newY - currentY;
347 }
348}
349
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500350void GrAtlasTextBlob::flushBigGlyphs(GrTextUtils::Target* target,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500351 const GrClip& clip, const SkPaint& paint,
joshualitt8e0ef292016-02-19 14:13:03 -0800352 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800353 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800354 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800355 for (int i = 0; i < fBigGlyphs.count(); i++) {
356 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
Jim Van Verth08576e62016-11-16 10:15:23 -0500357 calculate_translation(bigGlyph.fTreatAsBMP, viewMatrix, x, y,
joshualitt8e0ef292016-02-19 14:13:03 -0800358 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800359 SkMatrix ctm;
360 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800361 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
Jim Van Verth08576e62016-11-16 10:15:23 -0500362 if (!bigGlyph.fTreatAsBMP) {
joshualitt8e0ef292016-02-19 14:13:03 -0800363 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800364 }
Brian Salomonf18b1d82017-10-27 11:30:49 -0400365 target->drawPath(clip, bigGlyph.fPath, paint, ctm, nullptr, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800366 }
367}
368
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500369void GrAtlasTextBlob::flushBigRun(GrTextUtils::Target* target,
370 const SkSurfaceProps& props, const SkTextBlobRunIterator& it,
371 const GrClip& clip, const GrTextUtils::Paint& paint,
372 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
373 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800374 size_t textLen = it.glyphCount() * sizeof(uint16_t);
375 const SkPoint& offset = it.offset();
376
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500377 GrTextUtils::RunPaint runPaint(&paint, drawFilter, props);
378 if (!runPaint.modifyForRun(it)) {
joshualitt2e2202e2015-12-10 11:22:08 -0800379 return;
380 }
381
joshualitt2e2202e2015-12-10 11:22:08 -0800382 switch (it.positioning()) {
383 case SkTextBlob::kDefault_Positioning:
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500384 GrTextUtils::DrawBigText(target, clip, runPaint, viewMatrix,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500385 (const char*)it.glyphs(), textLen, x + offset.x(),
386 y + offset.y(), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800387 break;
388 case SkTextBlob::kHorizontal_Positioning:
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500389 GrTextUtils::DrawBigPosText(target, props, clip, runPaint, viewMatrix,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500390 (const char*)it.glyphs(), textLen, it.pos(), 1,
391 SkPoint::Make(x, y + offset.y()), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800392 break;
393 case SkTextBlob::kFull_Positioning:
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500394 GrTextUtils::DrawBigPosText(target, props, clip, runPaint, viewMatrix,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500395 (const char*)it.glyphs(), textLen, it.pos(), 2,
396 SkPoint::Make(x, y), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800397 break;
398 }
399}
400
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500401void GrAtlasTextBlob::flushCached(GrAtlasGlyphCache* atlasGlyphCache, GrTextUtils::Target* target,
Brian Salomon82f44312017-01-11 13:42:54 -0500402 const SkTextBlob* blob, const SkSurfaceProps& props,
joshualitt2e2202e2015-12-10 11:22:08 -0800403 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500404 const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter,
405 const GrClip& clip, const SkMatrix& viewMatrix,
406 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800407 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
408 // it as paths
joshualitt2e2202e2015-12-10 11:22:08 -0800409 SkTextBlobRunIterator it(blob);
410 for (int run = 0; !it.done(); it.next(), run++) {
Jim Van Verthf4c13162018-01-11 16:40:24 -0500411 if (fRuns[run].fTooBigForAtlas) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500412 this->flushBigRun(target, props, it, clip, paint, drawFilter, viewMatrix,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500413 clipBounds, x, y);
joshualitt2e2202e2015-12-10 11:22:08 -0800414 continue;
415 }
Brian Salomonf18b1d82017-10-27 11:30:49 -0400416 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500417 atlasGlyphCache);
joshualitt2e2202e2015-12-10 11:22:08 -0800418 }
419
420 // Now flush big glyphs
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500421 this->flushBigGlyphs(target, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800422}
423
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500424void GrAtlasTextBlob::flushThrowaway(GrAtlasGlyphCache* atlasGlyphCache,
425 GrTextUtils::Target* target,
joshualitt2e2202e2015-12-10 11:22:08 -0800426 const SkSurfaceProps& props,
427 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500428 const GrTextUtils::Paint& paint, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500429 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800430 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800431 for (int run = 0; run < fRunCount; run++) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400432 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500433 atlasGlyphCache);
joshualitt2e2202e2015-12-10 11:22:08 -0800434 }
435
436 // Now flush big glyphs
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500437 this->flushBigGlyphs(target, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800438}
439
Brian Salomon44acb5b2017-07-18 19:59:24 -0400440std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400441 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
442 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400443 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400444 GrTextUtils::Target* target) {
joshualitt7481e752016-01-22 06:08:48 -0800445 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400446 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400447 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
448 distanceAdjustTable, cache, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800449}
joshualitt2e2202e2015-12-10 11:22:08 -0800450
joshualitt259fbf12015-07-21 11:39:34 -0700451void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800452 SkASSERT_RELEASE(l.fSize == r.fSize);
453 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700454
joshualitt2f2ee832016-02-10 08:52:24 -0800455 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
456 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
457 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700458
joshualitt2f2ee832016-02-10 08:52:24 -0800459 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
460 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
461 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700462
joshualitt2f2ee832016-02-10 08:52:24 -0800463 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700464 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
465 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
466 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
467
joshualitt2f2ee832016-02-10 08:52:24 -0800468 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700469 // We can't assert that these have the same translations
470 }
471
joshualitt2f2ee832016-02-10 08:52:24 -0800472 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800473 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
474 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
475 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
476 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700477
joshualitt2f2ee832016-02-10 08:52:24 -0800478 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700479 for (int i = 0; i < l.fRunCount; i++) {
480 const Run& lRun = l.fRuns[i];
481 const Run& rRun = r.fRuns[i];
482
joshualitt259fbf12015-07-21 11:39:34 -0700483 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800484 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500485 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700486 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800487 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700488 }
489
joshualitt259fbf12015-07-21 11:39:34 -0700490
joshualitt2f2ee832016-02-10 08:52:24 -0800491 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
492 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700493 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700494
495 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800496 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
497 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700498 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
499 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700500 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800501 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700502 }
503
504 // color can be changed
505 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800506 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
Jim Van Verthf4c13162018-01-11 16:40:24 -0500507 SkASSERT_RELEASE(lRun.fTooBigForAtlas == rRun.fTooBigForAtlas);
joshualitt259fbf12015-07-21 11:39:34 -0700508
joshualitt2f2ee832016-02-10 08:52:24 -0800509 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700510 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
511 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
512 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
513
joshualitt2f2ee832016-02-10 08:52:24 -0800514 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
515 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700516
joshualitt2f2ee832016-02-10 08:52:24 -0800517 if (lSubRun.strike()) {
518 SkASSERT_RELEASE(rSubRun.strike());
Brian Salomonf856fd12016-12-16 14:24:34 -0500519 SkASSERT_RELEASE(GrAtlasTextStrike::GetKey(*lSubRun.strike()) ==
520 GrAtlasTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800521
522 } else {
523 SkASSERT_RELEASE(!rSubRun.strike());
524 }
525
526 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
527 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
528 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
529 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
530 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
531 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
532 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700533 }
534 }
535}
joshualitt8e0ef292016-02-19 14:13:03 -0800536
537void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
538 SkScalar x, SkScalar y, SkScalar* transX,
539 SkScalar* transY) {
540 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
541 fCurrentViewMatrix, fX, fY, transX, transY);
542 fCurrentViewMatrix = viewMatrix;
543 fX = x;
544 fY = y;
545}