blob: 02f3ab0a21f498b02584a2306962a11018820c65 [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);
64 run->fRasterizer = sk_ref_sp(effects.fRasterizer);
65 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Hal Canary144caf52016-11-07 17:57:18 -050066 return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
joshualitte76b4bb2015-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 Verth08576e62016-11-16 10:15:23 -050082 this->appendLargeGlyph(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
bsalomonc2878e22016-05-17 13:18:03 -0700142void GrAtlasTextBlob::appendLargeGlyph(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,
156 const SkMaskFilter::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
Brian Salomonf18b1d82017-10-27 11:30:49 -0400351void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, 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
Brian Salomonf18b1d82017-10-27 11:30:49 -0400370void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrTextUtils::Target* target,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500371 const SkSurfaceProps& props, const SkTextBlobRunIterator& it,
372 const GrClip& clip, const GrTextUtils::Paint& paint,
joshualitt2e2202e2015-12-10 11:22:08 -0800373 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:
Brian Salomonf18b1d82017-10-27 11:30:49 -0400385 GrTextUtils::DrawTextAsPath(context, target, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -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:
Brian Salomonf18b1d82017-10-27 11:30:49 -0400390 GrTextUtils::DrawPosTextAsPath(context, target, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -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:
Brian Salomonf18b1d82017-10-27 11:30:49 -0400395 GrTextUtils::DrawPosTextAsPath(context, target, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -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
Brian Salomonf18b1d82017-10-27 11:30:49 -0400402void GrAtlasTextBlob::flushCached(GrContext* context, 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++) {
412 if (fRuns[run].fDrawAsPaths) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400413 this->flushRunAsPaths(context, target, props, it, clip, paint, drawFilter, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -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,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500418 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800419 }
420
421 // Now flush big glyphs
Brian Salomonf18b1d82017-10-27 11:30:49 -0400422 this->flushBigGlyphs(context, target, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800423}
424
Brian Salomonf18b1d82017-10-27 11:30:49 -0400425void GrAtlasTextBlob::flushThrowaway(GrContext* context, 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,
Brian Salomon82f44312017-01-11 13:42:54 -0500433 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800434 }
435
436 // Now flush big glyphs
Brian Salomonf18b1d82017-10-27 11:30:49 -0400437 this->flushBigGlyphs(context, 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);
507 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
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}