blob: f083c85d8f168f1568b7eca0fc66c0f41aab37d5 [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
Florin Malitac337c9e2017-03-10 18:02:29 +000037 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) +
38 reinterpret_cast<unsigned char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080039 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
40 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
41
42 // Initialize runs
43 for (int i = 0; i < runCount; i++) {
44 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
45 }
46 cacheBlob->fRunCount = runCount;
47 cacheBlob->fPool = pool;
48 return cacheBlob;
49}
50
joshualitte76b4bb2015-12-28 07:23:58 -080051SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
52 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -070053 uint32_t scalerContextFlags,
joshualitte76b4bb2015-12-28 07:23:58 -080054 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -080055 const SkMatrix* viewMatrix) {
joshualitte76b4bb2015-12-28 07:23:58 -080056 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
57
58 // if we have an override descriptor for the run, then we should use that
59 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
60 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070061 SkScalerContextEffects effects;
62 skPaint.getScalerContextDescriptor(&effects, desc, props, scalerContextFlags, viewMatrix);
joshualitte76b4bb2015-12-28 07:23:58 -080063 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
bsalomon8b6fa5e2016-05-19 16:23:47 -070064 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
65 run->fRasterizer = sk_ref_sp(effects.fRasterizer);
66 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Hal Canary144caf52016-11-07 17:57:18 -050067 return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
joshualitte76b4bb2015-12-28 07:23:58 -080068}
69
joshualittf528e0d2015-12-09 06:42:52 -080070void GrAtlasTextBlob::appendGlyph(int runIndex,
71 const SkRect& positions,
72 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -050073 GrAtlasTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080074 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070075 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -050076 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
Lee Salzman26d3f212017-01-27 14:31:10 -050077 if (positions.isEmpty()) {
78 return;
79 }
joshualitta06e6ab2015-12-10 08:54:41 -080080
81 // If the glyph is too large we fall back to paths
82 if (glyph->fTooLargeForAtlas) {
Jim Van Verth08576e62016-11-16 10:15:23 -050083 this->appendLargeGlyph(glyph, cache, skGlyph, x, y, scale, treatAsBMP);
joshualitta06e6ab2015-12-10 08:54:41 -080084 return;
85 }
86
joshualittf528e0d2015-12-09 06:42:52 -080087 Run& run = fRuns[runIndex];
88 GrMaskFormat format = glyph->fMaskFormat;
89
90 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
91 if (run.fInitialized && subRun->maskFormat() != format) {
92 subRun = &run.push_back();
93 subRun->setStrike(strike);
94 } else if (!run.fInitialized) {
95 subRun->setStrike(strike);
96 }
97
98 run.fInitialized = true;
99
100 size_t vertexStride = GetVertexStride(format);
101
102 subRun->setMaskFormat(format);
103
joshualitt7481e752016-01-22 06:08:48 -0800104 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800105 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800106
107 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
108
joshualittf528e0d2015-12-09 06:42:52 -0800109 if (kARGB_GrMaskFormat != glyph->fMaskFormat) {
joshualitt18b072d2015-12-07 12:26:12 -0800110 // V0
111 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
112 position->set(positions.fLeft, positions.fTop);
113 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
114 *colorPtr = color;
115 vertex += vertexStride;
116
117 // V1
118 position = reinterpret_cast<SkPoint*>(vertex);
119 position->set(positions.fLeft, positions.fBottom);
120 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
121 *colorPtr = color;
122 vertex += vertexStride;
123
124 // V2
125 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000126 position->set(positions.fRight, positions.fTop);
joshualitt18b072d2015-12-07 12:26:12 -0800127 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
128 *colorPtr = color;
129 vertex += vertexStride;
130
131 // V3
132 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000133 position->set(positions.fRight, positions.fBottom);
joshualitt18b072d2015-12-07 12:26:12 -0800134 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
135 *colorPtr = color;
136 } else {
137 // V0
138 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
139 position->set(positions.fLeft, positions.fTop);
140 vertex += vertexStride;
141
142 // V1
143 position = reinterpret_cast<SkPoint*>(vertex);
144 position->set(positions.fLeft, positions.fBottom);
145 vertex += vertexStride;
146
147 // V2
148 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000149 position->set(positions.fRight, positions.fTop);
joshualitt18b072d2015-12-07 12:26:12 -0800150 vertex += vertexStride;
151
152 // V3
153 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000154 position->set(positions.fRight, positions.fBottom);
joshualitt18b072d2015-12-07 12:26:12 -0800155 }
156 subRun->appendVertices(vertexStride);
157 fGlyphs[subRun->glyphEndIndex()] = glyph;
158 subRun->glyphAppended();
159}
160
bsalomonc2878e22016-05-17 13:18:03 -0700161void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500162 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
joshualitta06e6ab2015-12-10 08:54:41 -0800163 if (nullptr == glyph->fPath) {
bsalomonc2878e22016-05-17 13:18:03 -0700164 const SkPath* glyphPath = cache->findPath(skGlyph);
joshualitta06e6ab2015-12-10 08:54:41 -0800165 if (!glyphPath) {
166 return;
167 }
168
169 glyph->fPath = new SkPath(*glyphPath);
170 }
Jim Van Verth08576e62016-11-16 10:15:23 -0500171 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, treatAsBMP));
joshualitta06e6ab2015-12-10 08:54:41 -0800172}
173
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500174bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
175 const SkMaskFilter::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800176 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
177 // If we have LCD text then our canonical color will be set to transparent, in this case we have
178 // to regenerate the blob on any color change
179 // We use the grPaint to get any color filter effects
180 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400181 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800182 return true;
183 }
184
joshualitt8e0ef292016-02-19 14:13:03 -0800185 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800186 return true;
187 }
188
joshualitt8e0ef292016-02-19 14:13:03 -0800189 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800190 return true;
191 }
192
193 // We only cache one masked version
194 if (fKey.fHasBlur &&
195 (fBlurRec.fSigma != blurRec.fSigma ||
196 fBlurRec.fStyle != blurRec.fStyle ||
197 fBlurRec.fQuality != blurRec.fQuality)) {
198 return true;
199 }
200
201 // Similarly, we only cache one version for each style
202 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500203 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
204 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
205 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800206 return true;
207 }
208
209 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
210 // for mixed blobs if this becomes an issue.
211 if (this->hasBitmap() && this->hasDistanceField()) {
212 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800213 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800214 return false;
215 }
216 return true;
217 }
218
219 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800220 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
221 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
222 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
223 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800224 return true;
225 }
226
227 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
228 // but only for integer translations.
229 // This cool bit of math will determine the necessary translation to apply to the already
230 // generated vertex coordinates to move them to the correct position
231 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800232 viewMatrix.getScaleX() * (x - fInitialX) +
233 viewMatrix.getSkewX() * (y - fInitialY) -
234 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800235 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800236 viewMatrix.getSkewY() * (x - fInitialX) +
237 viewMatrix.getScaleY() * (y - fInitialY) -
238 fInitialViewMatrix.getTranslateY();
239 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800240 return true;
241 }
joshualittfd5f6c12015-12-10 07:44:50 -0800242 } else if (this->hasDistanceField()) {
243 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
244 // distance field being generated, so we have to regenerate in those cases
245 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800246 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800247 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
248 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
249 return true;
250 }
joshualittfd5f6c12015-12-10 07:44:50 -0800251 }
252
joshualittfd5f6c12015-12-10 07:44:50 -0800253 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
254 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
255 // the blob anyways at flush time, so no need to regenerate explicitly
256 return false;
257}
258
Brian Salomonf18b1d82017-10-27 11:30:49 -0400259inline std::unique_ptr<GrAtlasTextOp> GrAtlasTextBlob::makeOp(
Brian Salomon344ec422016-12-15 10:58:41 -0500260 const Run::SubRunInfo& info, int glyphCount, int run, int subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400261 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
262 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400263 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
264 GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800265 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800266
Brian Salomon44acb5b2017-07-18 19:59:24 -0400267 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400268 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500269 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800270 if (info.drawAsDistanceFields()) {
joshualitt2e2202e2015-12-10 11:22:08 -0800271 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
Brian Salomon44acb5b2017-07-18 19:59:24 -0400272 op = GrAtlasTextOp::MakeDistanceField(
273 std::move(grPaint), glyphCount, cache, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400274 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400275 info.hasUseLCDText(), useBGR, info.isAntiAliased());
joshualitt2e2202e2015-12-10 11:22:08 -0800276 } else {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400277 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount, cache);
joshualitt2e2202e2015-12-10 11:22:08 -0800278 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500279 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800280 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400281 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800282 geometry.fBlob = SkRef(this);
283 geometry.fRun = run;
284 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500285 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400286 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800287 geometry.fX = x;
288 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500289 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400290 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800291}
292
Brian Salomonf18b1d82017-10-27 11:30:49 -0400293inline void GrAtlasTextBlob::flushRun(GrTextUtils::Target* target, const GrClip& clip, int run,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500294 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
295 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon82f44312017-01-11 13:42:54 -0500296 const GrDistanceFieldAdjustTable* distanceAdjustTable,
297 GrAtlasGlyphCache* cache) {
298 int lastRun = fRuns[run].fSubRunInfo.count() - 1;
299 for (int subRun = 0; subRun <= lastRun; subRun++) {
joshualitt2e2202e2015-12-10 11:22:08 -0800300 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
301 int glyphCount = info.glyphCount();
302 if (0 == glyphCount) {
303 continue;
304 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400305
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400306 bool skipClip = false;
307 bool submitOp = true;
308 SkIRect clipRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400309 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400310 SkRRect clipRRect;
311 GrAA aa;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400312 // We can clip geometrically if we're not using SDFs,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400313 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400314 if (!info.drawAsDistanceFields() && clip.isRRect(rtBounds, &clipRRect, &aa) &&
315 clipRRect.isRect() && GrAA::kNo == aa) {
316 skipClip = true;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400317 // We only need to do clipping work if the subrun isn't contained by the clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400318 SkRect subRunBounds;
319 this->computeSubRunBounds(&subRunBounds, run, subRun, viewMatrix, x, y);
320 if (!clipRRect.getBounds().contains(subRunBounds)) {
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400321 // If the subrun is completely outside, don't add an op for it
322 if (!clipRRect.getBounds().intersects(subRunBounds)) {
323 submitOp = false;
324 } else {
325 clipRRect.getBounds().round(&clipRect);
326 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400327 }
328 }
329
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400330 if (submitOp) {
331 auto op = this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, clipRect,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400332 std::move(paint), props, distanceAdjustTable, cache, target);
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400333 if (op) {
334 if (skipClip) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400335 target->addDrawOp(GrNoClip(), std::move(op));
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400336 } else {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400337 target->addDrawOp(clip, std::move(op));
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400338 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400339 }
Brian Salomon44acb5b2017-07-18 19:59:24 -0400340 }
joshualitt2e2202e2015-12-10 11:22:08 -0800341 }
342}
343
joshualitt8e0ef292016-02-19 14:13:03 -0800344static void calculate_translation(bool applyVM,
345 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
346 const SkMatrix& currentViewMatrix, SkScalar currentX,
347 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
348 if (applyVM) {
349 *transX = newViewMatrix.getTranslateX() +
350 newViewMatrix.getScaleX() * (newX - currentX) +
351 newViewMatrix.getSkewX() * (newY - currentY) -
352 currentViewMatrix.getTranslateX();
353
354 *transY = newViewMatrix.getTranslateY() +
355 newViewMatrix.getSkewY() * (newX - currentX) +
356 newViewMatrix.getScaleY() * (newY - currentY) -
357 currentViewMatrix.getTranslateY();
358 } else {
359 *transX = newX - currentX;
360 *transY = newY - currentY;
361 }
362}
363
Brian Salomonf18b1d82017-10-27 11:30:49 -0400364void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrTextUtils::Target* target,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500365 const GrClip& clip, const SkPaint& paint,
joshualitt8e0ef292016-02-19 14:13:03 -0800366 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800367 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800368 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800369 for (int i = 0; i < fBigGlyphs.count(); i++) {
370 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
Jim Van Verth08576e62016-11-16 10:15:23 -0500371 calculate_translation(bigGlyph.fTreatAsBMP, viewMatrix, x, y,
joshualitt8e0ef292016-02-19 14:13:03 -0800372 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800373 SkMatrix ctm;
374 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800375 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
Jim Van Verth08576e62016-11-16 10:15:23 -0500376 if (!bigGlyph.fTreatAsBMP) {
joshualitt8e0ef292016-02-19 14:13:03 -0800377 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800378 }
Brian Salomonf18b1d82017-10-27 11:30:49 -0400379 target->drawPath(clip, bigGlyph.fPath, paint, ctm, nullptr, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800380 }
381}
382
Brian Salomonf18b1d82017-10-27 11:30:49 -0400383void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrTextUtils::Target* target,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500384 const SkSurfaceProps& props, const SkTextBlobRunIterator& it,
385 const GrClip& clip, const GrTextUtils::Paint& paint,
joshualitt2e2202e2015-12-10 11:22:08 -0800386 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
387 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800388 size_t textLen = it.glyphCount() * sizeof(uint16_t);
389 const SkPoint& offset = it.offset();
390
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500391 GrTextUtils::RunPaint runPaint(&paint, drawFilter, props);
392 if (!runPaint.modifyForRun(it)) {
joshualitt2e2202e2015-12-10 11:22:08 -0800393 return;
394 }
395
joshualitt2e2202e2015-12-10 11:22:08 -0800396 switch (it.positioning()) {
397 case SkTextBlob::kDefault_Positioning:
Brian Salomonf18b1d82017-10-27 11:30:49 -0400398 GrTextUtils::DrawTextAsPath(context, target, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500399 (const char*)it.glyphs(), textLen, x + offset.x(),
400 y + offset.y(), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800401 break;
402 case SkTextBlob::kHorizontal_Positioning:
Brian Salomonf18b1d82017-10-27 11:30:49 -0400403 GrTextUtils::DrawPosTextAsPath(context, target, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500404 (const char*)it.glyphs(), textLen, it.pos(), 1,
405 SkPoint::Make(x, y + offset.y()), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800406 break;
407 case SkTextBlob::kFull_Positioning:
Brian Salomonf18b1d82017-10-27 11:30:49 -0400408 GrTextUtils::DrawPosTextAsPath(context, target, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500409 (const char*)it.glyphs(), textLen, it.pos(), 2,
410 SkPoint::Make(x, y), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800411 break;
412 }
413}
414
Brian Salomonf18b1d82017-10-27 11:30:49 -0400415void GrAtlasTextBlob::flushCached(GrContext* context, GrTextUtils::Target* target,
Brian Salomon82f44312017-01-11 13:42:54 -0500416 const SkTextBlob* blob, const SkSurfaceProps& props,
joshualitt2e2202e2015-12-10 11:22:08 -0800417 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500418 const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter,
419 const GrClip& clip, const SkMatrix& viewMatrix,
420 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800421 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
422 // it as paths
joshualitt2e2202e2015-12-10 11:22:08 -0800423 SkTextBlobRunIterator it(blob);
424 for (int run = 0; !it.done(); it.next(), run++) {
425 if (fRuns[run].fDrawAsPaths) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400426 this->flushRunAsPaths(context, target, props, it, clip, paint, drawFilter, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500427 clipBounds, x, y);
joshualitt2e2202e2015-12-10 11:22:08 -0800428 continue;
429 }
Brian Salomonf18b1d82017-10-27 11:30:49 -0400430 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500431 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800432 }
433
434 // Now flush big glyphs
Brian Salomonf18b1d82017-10-27 11:30:49 -0400435 this->flushBigGlyphs(context, target, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800436}
437
Brian Salomonf18b1d82017-10-27 11:30:49 -0400438void GrAtlasTextBlob::flushThrowaway(GrContext* context, GrTextUtils::Target* target,
joshualitt2e2202e2015-12-10 11:22:08 -0800439 const SkSurfaceProps& props,
440 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500441 const GrTextUtils::Paint& paint, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500442 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800443 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800444 for (int run = 0; run < fRunCount; run++) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400445 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Brian Salomon82f44312017-01-11 13:42:54 -0500446 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800447 }
448
449 // Now flush big glyphs
Brian Salomonf18b1d82017-10-27 11:30:49 -0400450 this->flushBigGlyphs(context, target, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800451}
452
Brian Salomon44acb5b2017-07-18 19:59:24 -0400453std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
Brian Salomonf8334782017-01-03 09:42:58 -0500454 int glyphCount, int run, int subRun, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500455 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400456 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400457 GrTextUtils::Target* target) {
joshualitt7481e752016-01-22 06:08:48 -0800458 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400459 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400460 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
461 distanceAdjustTable, cache, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800462}
joshualitt2e2202e2015-12-10 11:22:08 -0800463
joshualitt259fbf12015-07-21 11:39:34 -0700464void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800465 SkASSERT_RELEASE(l.fSize == r.fSize);
466 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700467
joshualitt2f2ee832016-02-10 08:52:24 -0800468 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
469 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
470 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700471
joshualitt2f2ee832016-02-10 08:52:24 -0800472 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
473 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
474 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700475
joshualitt2f2ee832016-02-10 08:52:24 -0800476 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700477 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
478 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
479 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
480
joshualitt2f2ee832016-02-10 08:52:24 -0800481 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700482 // We can't assert that these have the same translations
483 }
484
joshualitt2f2ee832016-02-10 08:52:24 -0800485 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800486 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
487 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
488 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
489 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700490
joshualitt2f2ee832016-02-10 08:52:24 -0800491 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700492 for (int i = 0; i < l.fRunCount; i++) {
493 const Run& lRun = l.fRuns[i];
494 const Run& rRun = r.fRuns[i];
495
joshualitt259fbf12015-07-21 11:39:34 -0700496 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800497 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500498 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700499 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800500 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700501 }
502
joshualitt259fbf12015-07-21 11:39:34 -0700503
joshualitt2f2ee832016-02-10 08:52:24 -0800504 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
505 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700506 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700507
508 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800509 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
510 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700511 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
512 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700513 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800514 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700515 }
516
517 // color can be changed
518 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800519 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
520 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700521
joshualitt2f2ee832016-02-10 08:52:24 -0800522 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700523 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
524 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
525 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
526
joshualitt2f2ee832016-02-10 08:52:24 -0800527 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
528 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700529
joshualitt2f2ee832016-02-10 08:52:24 -0800530 if (lSubRun.strike()) {
531 SkASSERT_RELEASE(rSubRun.strike());
Brian Salomonf856fd12016-12-16 14:24:34 -0500532 SkASSERT_RELEASE(GrAtlasTextStrike::GetKey(*lSubRun.strike()) ==
533 GrAtlasTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800534
535 } else {
536 SkASSERT_RELEASE(!rSubRun.strike());
537 }
538
539 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
540 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
541 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
542 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
543 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
544 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
545 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700546 }
547 }
548}
joshualitt8e0ef292016-02-19 14:13:03 -0800549
550void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
551 SkScalar x, SkScalar y, SkScalar* transX,
552 SkScalar* transY) {
553 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
554 fCurrentViewMatrix, fX, fY, transX, transY);
555 fCurrentViewMatrix = viewMatrix;
556 fX = x;
557 fY = y;
558}