blob: 0b25a34ed14dd9bdfce8796b62933bf0b4331ef6 [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
99 size_t vertexStride = GetVertexStride(format);
100
101 subRun->setMaskFormat(format);
102
joshualitt7481e752016-01-22 06:08:48 -0800103 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800104 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800105
106 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
107
joshualittf528e0d2015-12-09 06:42:52 -0800108 if (kARGB_GrMaskFormat != glyph->fMaskFormat) {
joshualitt18b072d2015-12-07 12:26:12 -0800109 // V0
110 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
111 position->set(positions.fLeft, positions.fTop);
112 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
113 *colorPtr = color;
114 vertex += vertexStride;
115
116 // V1
117 position = reinterpret_cast<SkPoint*>(vertex);
118 position->set(positions.fLeft, positions.fBottom);
119 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
120 *colorPtr = color;
121 vertex += vertexStride;
122
123 // V2
124 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000125 position->set(positions.fRight, positions.fTop);
joshualitt18b072d2015-12-07 12:26:12 -0800126 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
127 *colorPtr = color;
128 vertex += vertexStride;
129
130 // V3
131 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000132 position->set(positions.fRight, positions.fBottom);
joshualitt18b072d2015-12-07 12:26:12 -0800133 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
134 *colorPtr = color;
135 } else {
136 // V0
137 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
138 position->set(positions.fLeft, positions.fTop);
139 vertex += vertexStride;
140
141 // V1
142 position = reinterpret_cast<SkPoint*>(vertex);
143 position->set(positions.fLeft, positions.fBottom);
144 vertex += vertexStride;
145
146 // V2
147 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000148 position->set(positions.fRight, positions.fTop);
joshualitt18b072d2015-12-07 12:26:12 -0800149 vertex += vertexStride;
150
151 // V3
152 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000153 position->set(positions.fRight, positions.fBottom);
joshualitt18b072d2015-12-07 12:26:12 -0800154 }
155 subRun->appendVertices(vertexStride);
156 fGlyphs[subRun->glyphEndIndex()] = glyph;
157 subRun->glyphAppended();
158}
159
bsalomonc2878e22016-05-17 13:18:03 -0700160void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500161 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
joshualitta06e6ab2015-12-10 08:54:41 -0800162 if (nullptr == glyph->fPath) {
bsalomonc2878e22016-05-17 13:18:03 -0700163 const SkPath* glyphPath = cache->findPath(skGlyph);
joshualitta06e6ab2015-12-10 08:54:41 -0800164 if (!glyphPath) {
165 return;
166 }
167
168 glyph->fPath = new SkPath(*glyphPath);
169 }
Jim Van Verth08576e62016-11-16 10:15:23 -0500170 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, treatAsBMP));
joshualitta06e6ab2015-12-10 08:54:41 -0800171}
172
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500173bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
174 const SkMaskFilter::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800175 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
176 // If we have LCD text then our canonical color will be set to transparent, in this case we have
177 // to regenerate the blob on any color change
178 // We use the grPaint to get any color filter effects
179 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400180 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800181 return true;
182 }
183
joshualitt8e0ef292016-02-19 14:13:03 -0800184 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800185 return true;
186 }
187
joshualitt8e0ef292016-02-19 14:13:03 -0800188 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800189 return true;
190 }
191
192 // We only cache one masked version
193 if (fKey.fHasBlur &&
194 (fBlurRec.fSigma != blurRec.fSigma ||
195 fBlurRec.fStyle != blurRec.fStyle ||
196 fBlurRec.fQuality != blurRec.fQuality)) {
197 return true;
198 }
199
200 // Similarly, we only cache one version for each style
201 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500202 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
203 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
204 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800205 return true;
206 }
207
208 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
209 // for mixed blobs if this becomes an issue.
210 if (this->hasBitmap() && this->hasDistanceField()) {
211 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800212 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800213 return false;
214 }
215 return true;
216 }
217
218 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800219 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
220 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
221 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
222 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800223 return true;
224 }
225
226 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
227 // but only for integer translations.
228 // This cool bit of math will determine the necessary translation to apply to the already
229 // generated vertex coordinates to move them to the correct position
230 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800231 viewMatrix.getScaleX() * (x - fInitialX) +
232 viewMatrix.getSkewX() * (y - fInitialY) -
233 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800234 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800235 viewMatrix.getSkewY() * (x - fInitialX) +
236 viewMatrix.getScaleY() * (y - fInitialY) -
237 fInitialViewMatrix.getTranslateY();
238 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800239 return true;
240 }
joshualittfd5f6c12015-12-10 07:44:50 -0800241 } else if (this->hasDistanceField()) {
242 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
243 // distance field being generated, so we have to regenerate in those cases
244 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800245 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800246 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
247 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
248 return true;
249 }
joshualittfd5f6c12015-12-10 07:44:50 -0800250 }
251
joshualittfd5f6c12015-12-10 07:44:50 -0800252 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
253 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
254 // the blob anyways at flush time, so no need to regenerate explicitly
255 return false;
256}
257
Brian Salomonf18b1d82017-10-27 11:30:49 -0400258inline std::unique_ptr<GrAtlasTextOp> GrAtlasTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400259 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400260 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
261 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400262 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
263 GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800264 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800265
Brian Salomon44acb5b2017-07-18 19:59:24 -0400266 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400267 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500268 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800269 if (info.drawAsDistanceFields()) {
joshualitt2e2202e2015-12-10 11:22:08 -0800270 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
Brian Salomon44acb5b2017-07-18 19:59:24 -0400271 op = GrAtlasTextOp::MakeDistanceField(
272 std::move(grPaint), glyphCount, cache, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400273 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400274 info.hasUseLCDText(), useBGR, info.isAntiAliased());
joshualitt2e2202e2015-12-10 11:22:08 -0800275 } else {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400276 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount, cache);
joshualitt2e2202e2015-12-10 11:22:08 -0800277 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500278 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800279 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400280 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800281 geometry.fBlob = SkRef(this);
282 geometry.fRun = run;
283 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500284 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400285 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800286 geometry.fX = x;
287 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500288 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400289 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800290}
291
Brian Salomonf18b1d82017-10-27 11:30:49 -0400292inline void GrAtlasTextBlob::flushRun(GrTextUtils::Target* target, const GrClip& clip, int run,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500293 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
294 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon82f44312017-01-11 13:42:54 -0500295 const GrDistanceFieldAdjustTable* distanceAdjustTable,
296 GrAtlasGlyphCache* cache) {
Jim Van Verth56c37142017-10-31 14:44:25 -0400297 // GrAtlasTextBlob::makeOp only takes uint16_t values for run and subRun indices.
298 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
299 if (run >= (1 << 16)) {
300 return;
301 }
302 int lastRun = SkTMin(fRuns[run].fSubRunInfo.count(), 1 << 16) - 1;
Brian Salomon82f44312017-01-11 13:42:54 -0500303 for (int subRun = 0; subRun <= lastRun; subRun++) {
joshualitt2e2202e2015-12-10 11:22:08 -0800304 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
305 int glyphCount = info.glyphCount();
306 if (0 == glyphCount) {
307 continue;
308 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400309
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400310 bool skipClip = false;
311 bool submitOp = true;
312 SkIRect clipRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400313 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400314 SkRRect clipRRect;
315 GrAA aa;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400316 // We can clip geometrically if we're not using SDFs,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400317 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400318 if (!info.drawAsDistanceFields() && clip.isRRect(rtBounds, &clipRRect, &aa) &&
319 clipRRect.isRect() && GrAA::kNo == aa) {
320 skipClip = true;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400321 // We only need to do clipping work if the subrun isn't contained by the clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400322 SkRect subRunBounds;
323 this->computeSubRunBounds(&subRunBounds, run, subRun, viewMatrix, x, y);
324 if (!clipRRect.getBounds().contains(subRunBounds)) {
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400325 // If the subrun is completely outside, don't add an op for it
326 if (!clipRRect.getBounds().intersects(subRunBounds)) {
327 submitOp = false;
328 } else {
329 clipRRect.getBounds().round(&clipRect);
330 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400331 }
332 }
333
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400334 if (submitOp) {
335 auto op = this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, clipRect,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400336 std::move(paint), props, distanceAdjustTable, cache, target);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400337 if (op) {
338 if (skipClip) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400339 target->addDrawOp(GrNoClip(), std::move(op));
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400340 } else {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400341 target->addDrawOp(clip, std::move(op));
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400342 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400343 }
Brian Salomon44acb5b2017-07-18 19:59:24 -0400344 }
joshualitt2e2202e2015-12-10 11:22:08 -0800345 }
346}
347
joshualitt8e0ef292016-02-19 14:13:03 -0800348static void calculate_translation(bool applyVM,
349 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
350 const SkMatrix& currentViewMatrix, SkScalar currentX,
351 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
352 if (applyVM) {
353 *transX = newViewMatrix.getTranslateX() +
354 newViewMatrix.getScaleX() * (newX - currentX) +
355 newViewMatrix.getSkewX() * (newY - currentY) -
356 currentViewMatrix.getTranslateX();
357
358 *transY = newViewMatrix.getTranslateY() +
359 newViewMatrix.getSkewY() * (newX - currentX) +
360 newViewMatrix.getScaleY() * (newY - currentY) -
361 currentViewMatrix.getTranslateY();
362 } else {
363 *transX = newX - currentX;
364 *transY = newY - currentY;
365 }
366}
367
Brian Salomonf18b1d82017-10-27 11:30:49 -0400368void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrTextUtils::Target* target,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500369 const GrClip& clip, const SkPaint& paint,
joshualitt8e0ef292016-02-19 14:13:03 -0800370 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800371 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800372 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800373 for (int i = 0; i < fBigGlyphs.count(); i++) {
374 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
Jim Van Verth08576e62016-11-16 10:15:23 -0500375 calculate_translation(bigGlyph.fTreatAsBMP, viewMatrix, x, y,
joshualitt8e0ef292016-02-19 14:13:03 -0800376 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800377 SkMatrix ctm;
378 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800379 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
Jim Van Verth08576e62016-11-16 10:15:23 -0500380 if (!bigGlyph.fTreatAsBMP) {
joshualitt8e0ef292016-02-19 14:13:03 -0800381 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800382 }
Brian Salomonf18b1d82017-10-27 11:30:49 -0400383 target->drawPath(clip, bigGlyph.fPath, paint, ctm, nullptr, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800384 }
385}
386
Brian Salomonf18b1d82017-10-27 11:30:49 -0400387void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrTextUtils::Target* target,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500388 const SkSurfaceProps& props, const SkTextBlobRunIterator& it,
389 const GrClip& clip, const GrTextUtils::Paint& paint,
joshualitt2e2202e2015-12-10 11:22:08 -0800390 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
391 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800392 size_t textLen = it.glyphCount() * sizeof(uint16_t);
393 const SkPoint& offset = it.offset();
394
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500395 GrTextUtils::RunPaint runPaint(&paint, drawFilter, props);
396 if (!runPaint.modifyForRun(it)) {
joshualitt2e2202e2015-12-10 11:22:08 -0800397 return;
398 }
399
joshualitt2e2202e2015-12-10 11:22:08 -0800400 switch (it.positioning()) {
401 case SkTextBlob::kDefault_Positioning:
Brian Salomonf18b1d82017-10-27 11:30:49 -0400402 GrTextUtils::DrawTextAsPath(context, target, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500403 (const char*)it.glyphs(), textLen, x + offset.x(),
404 y + offset.y(), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800405 break;
406 case SkTextBlob::kHorizontal_Positioning:
Brian Salomonf18b1d82017-10-27 11:30:49 -0400407 GrTextUtils::DrawPosTextAsPath(context, target, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500408 (const char*)it.glyphs(), textLen, it.pos(), 1,
409 SkPoint::Make(x, y + offset.y()), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800410 break;
411 case SkTextBlob::kFull_Positioning:
Brian Salomonf18b1d82017-10-27 11:30:49 -0400412 GrTextUtils::DrawPosTextAsPath(context, target, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500413 (const char*)it.glyphs(), textLen, it.pos(), 2,
414 SkPoint::Make(x, y), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800415 break;
416 }
417}
418
Brian Salomonf18b1d82017-10-27 11:30:49 -0400419void GrAtlasTextBlob::flushCached(GrContext* context, GrTextUtils::Target* target,
Brian Salomon82f44312017-01-11 13:42:54 -0500420 const SkTextBlob* blob, const SkSurfaceProps& props,
joshualitt2e2202e2015-12-10 11:22:08 -0800421 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500422 const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter,
423 const GrClip& clip, const SkMatrix& viewMatrix,
424 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800425 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
426 // it as paths
joshualitt2e2202e2015-12-10 11:22:08 -0800427 SkTextBlobRunIterator it(blob);
428 for (int run = 0; !it.done(); it.next(), run++) {
429 if (fRuns[run].fDrawAsPaths) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400430 this->flushRunAsPaths(context, target, props, it, clip, paint, drawFilter, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500431 clipBounds, x, y);
joshualitt2e2202e2015-12-10 11:22:08 -0800432 continue;
433 }
Brian Salomonf18b1d82017-10-27 11:30:49 -0400434 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500435 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800436 }
437
438 // Now flush big glyphs
Brian Salomonf18b1d82017-10-27 11:30:49 -0400439 this->flushBigGlyphs(context, target, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800440}
441
Brian Salomonf18b1d82017-10-27 11:30:49 -0400442void GrAtlasTextBlob::flushThrowaway(GrContext* context, GrTextUtils::Target* target,
joshualitt2e2202e2015-12-10 11:22:08 -0800443 const SkSurfaceProps& props,
444 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500445 const GrTextUtils::Paint& paint, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500446 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800447 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800448 for (int run = 0; run < fRunCount; run++) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400449 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Brian Salomon82f44312017-01-11 13:42:54 -0500450 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800451 }
452
453 // Now flush big glyphs
Brian Salomonf18b1d82017-10-27 11:30:49 -0400454 this->flushBigGlyphs(context, target, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800455}
456
Brian Salomon44acb5b2017-07-18 19:59:24 -0400457std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400458 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
459 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400460 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400461 GrTextUtils::Target* target) {
joshualitt7481e752016-01-22 06:08:48 -0800462 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400463 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400464 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
465 distanceAdjustTable, cache, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800466}
joshualitt2e2202e2015-12-10 11:22:08 -0800467
joshualitt259fbf12015-07-21 11:39:34 -0700468void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800469 SkASSERT_RELEASE(l.fSize == r.fSize);
470 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700471
joshualitt2f2ee832016-02-10 08:52:24 -0800472 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
473 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
474 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700475
joshualitt2f2ee832016-02-10 08:52:24 -0800476 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
477 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
478 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700479
joshualitt2f2ee832016-02-10 08:52:24 -0800480 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700481 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
482 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
483 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
484
joshualitt2f2ee832016-02-10 08:52:24 -0800485 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700486 // We can't assert that these have the same translations
487 }
488
joshualitt2f2ee832016-02-10 08:52:24 -0800489 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800490 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
491 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
492 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
493 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700494
joshualitt2f2ee832016-02-10 08:52:24 -0800495 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700496 for (int i = 0; i < l.fRunCount; i++) {
497 const Run& lRun = l.fRuns[i];
498 const Run& rRun = r.fRuns[i];
499
joshualitt259fbf12015-07-21 11:39:34 -0700500 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800501 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500502 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700503 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800504 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700505 }
506
joshualitt259fbf12015-07-21 11:39:34 -0700507
joshualitt2f2ee832016-02-10 08:52:24 -0800508 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
509 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700510 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700511
512 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800513 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
514 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700515 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
516 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700517 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800518 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700519 }
520
521 // color can be changed
522 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800523 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
524 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700525
joshualitt2f2ee832016-02-10 08:52:24 -0800526 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700527 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
528 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
529 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
530
joshualitt2f2ee832016-02-10 08:52:24 -0800531 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
532 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700533
joshualitt2f2ee832016-02-10 08:52:24 -0800534 if (lSubRun.strike()) {
535 SkASSERT_RELEASE(rSubRun.strike());
Brian Salomonf856fd12016-12-16 14:24:34 -0500536 SkASSERT_RELEASE(GrAtlasTextStrike::GetKey(*lSubRun.strike()) ==
537 GrAtlasTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800538
539 } else {
540 SkASSERT_RELEASE(!rSubRun.strike());
541 }
542
543 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
544 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
545 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
546 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
547 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
548 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
549 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700550 }
551 }
552}
joshualitt8e0ef292016-02-19 14:13:03 -0800553
554void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
555 SkScalar x, SkScalar y, SkScalar* transX,
556 SkScalar* transY) {
557 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
558 fCurrentViewMatrix, fX, fY, transX, transY);
559 fCurrentViewMatrix = viewMatrix;
560 fX = x;
561 fY = y;
562}