blob: a6dbd672869109555e2d58cd3e498bb18bf639b8 [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"
Brian Salomon344ec422016-12-15 10:58:41 -050012#include "GrRenderTargetContext.h"
joshualitt0a42e682015-12-10 13:20:58 -080013#include "GrTextUtils.h"
joshualitt2e2202e2015-12-10 11:22:08 -080014#include "SkColorFilter.h"
15#include "SkDrawFilter.h"
joshualitte76b4bb2015-12-28 07:23:58 -080016#include "SkGlyphCache.h"
joshualitt2e2202e2015-12-10 11:22:08 -080017#include "SkTextBlobRunIterator.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "ops/GrAtlasTextOp.h"
joshualitt2e2202e2015-12-10 11:22:08 -080019
Florin Malitac337c9e2017-03-10 18:02:29 +000020sk_sp<GrAtlasTextBlob> GrAtlasTextBlob::Make(GrMemoryPool* pool, int glyphCount, int runCount) {
joshualitt92303772016-02-10 11:55:52 -080021 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
22 // and size for the glyphIds array.
23 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
24 size_t size = sizeof(GrAtlasTextBlob) +
25 verticesCount +
26 glyphCount * sizeof(GrGlyph**) +
27 sizeof(GrAtlasTextBlob::Run) * runCount;
28
29 void* allocation = pool->allocate(size);
30 if (CACHE_SANITY_CHECK) {
31 sk_bzero(allocation, size);
32 }
33
Florin Malitac337c9e2017-03-10 18:02:29 +000034 sk_sp<GrAtlasTextBlob> cacheBlob(new (allocation) GrAtlasTextBlob);
joshualitt92303772016-02-10 11:55:52 -080035 cacheBlob->fSize = size;
36
37 // setup offsets for vertices / glyphs
Florin Malitac337c9e2017-03-10 18:02:29 +000038 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) +
39 reinterpret_cast<unsigned char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080040 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
41 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
42
43 // Initialize runs
44 for (int i = 0; i < runCount; i++) {
45 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
46 }
47 cacheBlob->fRunCount = runCount;
48 cacheBlob->fPool = pool;
49 return cacheBlob;
50}
51
joshualitte76b4bb2015-12-28 07:23:58 -080052SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
53 const SkSurfaceProps& props,
brianosmana1e8f8d2016-04-08 06:47:54 -070054 uint32_t scalerContextFlags,
joshualitte76b4bb2015-12-28 07:23:58 -080055 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -080056 const SkMatrix* viewMatrix) {
joshualitte76b4bb2015-12-28 07:23:58 -080057 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
58
59 // if we have an override descriptor for the run, then we should use that
60 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
61 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070062 SkScalerContextEffects effects;
63 skPaint.getScalerContextDescriptor(&effects, desc, props, scalerContextFlags, viewMatrix);
joshualitte76b4bb2015-12-28 07:23:58 -080064 run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
bsalomon8b6fa5e2016-05-19 16:23:47 -070065 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
66 run->fRasterizer = sk_ref_sp(effects.fRasterizer);
67 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Hal Canary144caf52016-11-07 17:57:18 -050068 return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
joshualitte76b4bb2015-12-28 07:23:58 -080069}
70
joshualittf528e0d2015-12-09 06:42:52 -080071void GrAtlasTextBlob::appendGlyph(int runIndex,
72 const SkRect& positions,
73 GrColor color,
Brian Salomonf856fd12016-12-16 14:24:34 -050074 GrAtlasTextStrike* strike,
joshualitta06e6ab2015-12-10 08:54:41 -080075 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070076 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -050077 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
Lee Salzman26d3f212017-01-27 14:31:10 -050078 if (positions.isEmpty()) {
79 return;
80 }
joshualitta06e6ab2015-12-10 08:54:41 -080081
82 // If the glyph is too large we fall back to paths
83 if (glyph->fTooLargeForAtlas) {
Jim Van Verth08576e62016-11-16 10:15:23 -050084 this->appendLargeGlyph(glyph, cache, skGlyph, x, y, scale, treatAsBMP);
joshualitta06e6ab2015-12-10 08:54:41 -080085 return;
86 }
87
joshualittf528e0d2015-12-09 06:42:52 -080088 Run& run = fRuns[runIndex];
89 GrMaskFormat format = glyph->fMaskFormat;
90
91 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
92 if (run.fInitialized && subRun->maskFormat() != format) {
93 subRun = &run.push_back();
94 subRun->setStrike(strike);
95 } else if (!run.fInitialized) {
96 subRun->setStrike(strike);
97 }
98
99 run.fInitialized = true;
100
101 size_t vertexStride = GetVertexStride(format);
102
103 subRun->setMaskFormat(format);
104
joshualitt7481e752016-01-22 06:08:48 -0800105 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800106 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800107
108 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
109
joshualittf528e0d2015-12-09 06:42:52 -0800110 if (kARGB_GrMaskFormat != glyph->fMaskFormat) {
joshualitt18b072d2015-12-07 12:26:12 -0800111 // V0
112 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
113 position->set(positions.fLeft, positions.fTop);
114 SkColor* colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
115 *colorPtr = color;
116 vertex += vertexStride;
117
118 // V1
119 position = reinterpret_cast<SkPoint*>(vertex);
120 position->set(positions.fLeft, positions.fBottom);
121 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
122 *colorPtr = color;
123 vertex += vertexStride;
124
125 // V2
126 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000127 position->set(positions.fRight, positions.fTop);
joshualitt18b072d2015-12-07 12:26:12 -0800128 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
129 *colorPtr = color;
130 vertex += vertexStride;
131
132 // V3
133 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000134 position->set(positions.fRight, positions.fBottom);
joshualitt18b072d2015-12-07 12:26:12 -0800135 colorPtr = reinterpret_cast<SkColor*>(vertex + sizeof(SkPoint));
136 *colorPtr = color;
137 } else {
138 // V0
139 SkPoint* position = reinterpret_cast<SkPoint*>(vertex);
140 position->set(positions.fLeft, positions.fTop);
141 vertex += vertexStride;
142
143 // V1
144 position = reinterpret_cast<SkPoint*>(vertex);
145 position->set(positions.fLeft, positions.fBottom);
146 vertex += vertexStride;
147
148 // V2
149 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000150 position->set(positions.fRight, positions.fTop);
joshualitt18b072d2015-12-07 12:26:12 -0800151 vertex += vertexStride;
152
153 // V3
154 position = reinterpret_cast<SkPoint*>(vertex);
Brian Salomon57caa662017-10-18 12:21:05 +0000155 position->set(positions.fRight, positions.fBottom);
joshualitt18b072d2015-12-07 12:26:12 -0800156 }
157 subRun->appendVertices(vertexStride);
158 fGlyphs[subRun->glyphEndIndex()] = glyph;
159 subRun->glyphAppended();
160}
161
bsalomonc2878e22016-05-17 13:18:03 -0700162void GrAtlasTextBlob::appendLargeGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth08576e62016-11-16 10:15:23 -0500163 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
joshualitta06e6ab2015-12-10 08:54:41 -0800164 if (nullptr == glyph->fPath) {
bsalomonc2878e22016-05-17 13:18:03 -0700165 const SkPath* glyphPath = cache->findPath(skGlyph);
joshualitta06e6ab2015-12-10 08:54:41 -0800166 if (!glyphPath) {
167 return;
168 }
169
170 glyph->fPath = new SkPath(*glyphPath);
171 }
Jim Van Verth08576e62016-11-16 10:15:23 -0500172 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, treatAsBMP));
joshualitta06e6ab2015-12-10 08:54:41 -0800173}
174
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500175bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
176 const SkMaskFilter::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800177 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
178 // If we have LCD text then our canonical color will be set to transparent, in this case we have
179 // to regenerate the blob on any color change
180 // We use the grPaint to get any color filter effects
181 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400182 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800183 return true;
184 }
185
joshualitt8e0ef292016-02-19 14:13:03 -0800186 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800187 return true;
188 }
189
joshualitt8e0ef292016-02-19 14:13:03 -0800190 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800191 return true;
192 }
193
194 // We only cache one masked version
195 if (fKey.fHasBlur &&
196 (fBlurRec.fSigma != blurRec.fSigma ||
197 fBlurRec.fStyle != blurRec.fStyle ||
198 fBlurRec.fQuality != blurRec.fQuality)) {
199 return true;
200 }
201
202 // Similarly, we only cache one version for each style
203 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500204 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
205 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
206 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800207 return true;
208 }
209
210 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
211 // for mixed blobs if this becomes an issue.
212 if (this->hasBitmap() && this->hasDistanceField()) {
213 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800214 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800215 return false;
216 }
217 return true;
218 }
219
220 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800221 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
222 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
223 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
224 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800225 return true;
226 }
227
228 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
229 // but only for integer translations.
230 // This cool bit of math will determine the necessary translation to apply to the already
231 // generated vertex coordinates to move them to the correct position
232 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800233 viewMatrix.getScaleX() * (x - fInitialX) +
234 viewMatrix.getSkewX() * (y - fInitialY) -
235 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800236 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800237 viewMatrix.getSkewY() * (x - fInitialX) +
238 viewMatrix.getScaleY() * (y - fInitialY) -
239 fInitialViewMatrix.getTranslateY();
240 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800241 return true;
242 }
joshualittfd5f6c12015-12-10 07:44:50 -0800243 } else if (this->hasDistanceField()) {
244 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
245 // distance field being generated, so we have to regenerate in those cases
246 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800247 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800248 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
249 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
250 return true;
251 }
joshualittfd5f6c12015-12-10 07:44:50 -0800252 }
253
joshualittfd5f6c12015-12-10 07:44:50 -0800254 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
255 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
256 // the blob anyways at flush time, so no need to regenerate explicitly
257 return false;
258}
259
Brian Salomon44acb5b2017-07-18 19:59:24 -0400260inline std::unique_ptr<GrDrawOp> GrAtlasTextBlob::makeOp(
Brian Salomon344ec422016-12-15 10:58:41 -0500261 const Run::SubRunInfo& info, int glyphCount, int run, int subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400262 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
263 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
264 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400265 GrAtlasGlyphCache* cache, GrRenderTargetContext* renderTargetContext) {
joshualitt2e2202e2015-12-10 11:22:08 -0800266 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800267
Brian Salomon44acb5b2017-07-18 19:59:24 -0400268 GrPaint grPaint;
269 if (!paint.toGrPaint(info.maskFormat(), renderTargetContext, viewMatrix, &grPaint)) {
270 return nullptr;
271 }
Brian Salomonf8334782017-01-03 09:42:58 -0500272 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800273 if (info.drawAsDistanceFields()) {
joshualitt2e2202e2015-12-10 11:22:08 -0800274 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
Brian Salomon44acb5b2017-07-18 19:59:24 -0400275 op = GrAtlasTextOp::MakeDistanceField(
276 std::move(grPaint), glyphCount, cache, distanceAdjustTable,
Brian Salomonf3569f02017-10-24 12:52:33 -0400277 renderTargetContext->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
278 info.hasUseLCDText(), useBGR, info.isAntiAliased());
joshualitt2e2202e2015-12-10 11:22:08 -0800279 } else {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400280 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount, cache);
joshualitt2e2202e2015-12-10 11:22:08 -0800281 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500282 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800283 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400284 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800285 geometry.fBlob = SkRef(this);
286 geometry.fRun = run;
287 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500288 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400289 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800290 geometry.fX = x;
291 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500292 op->init();
Brian Salomon09d994e2016-12-21 11:14:46 -0500293 return std::move(op);
joshualitt2e2202e2015-12-10 11:22:08 -0800294}
295
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500296inline void GrAtlasTextBlob::flushRun(GrRenderTargetContext* rtc, const GrClip& clip, int run,
297 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
298 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon82f44312017-01-11 13:42:54 -0500299 const GrDistanceFieldAdjustTable* distanceAdjustTable,
300 GrAtlasGlyphCache* cache) {
301 int lastRun = fRuns[run].fSubRunInfo.count() - 1;
302 for (int subRun = 0; subRun <= lastRun; subRun++) {
joshualitt2e2202e2015-12-10 11:22:08 -0800303 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
304 int glyphCount = info.glyphCount();
305 if (0 == glyphCount) {
306 continue;
307 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400308
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400309 bool skipClip = false;
310 bool submitOp = true;
311 SkIRect clipRect = SkIRect::MakeEmpty();
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400312 SkRect rtBounds = SkRect::MakeWH(rtc->width(), rtc->height());
313 SkRRect clipRRect;
314 GrAA aa;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400315 // We can clip geometrically if we're not using SDFs,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400316 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400317 if (!info.drawAsDistanceFields() && clip.isRRect(rtBounds, &clipRRect, &aa) &&
318 clipRRect.isRect() && GrAA::kNo == aa) {
319 skipClip = true;
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400320 // We only need to do clipping work if the subrun isn't contained by the clip
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400321 SkRect subRunBounds;
322 this->computeSubRunBounds(&subRunBounds, run, subRun, viewMatrix, x, y);
323 if (!clipRRect.getBounds().contains(subRunBounds)) {
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400324 // If the subrun is completely outside, don't add an op for it
325 if (!clipRRect.getBounds().intersects(subRunBounds)) {
326 submitOp = false;
327 } else {
328 clipRRect.getBounds().round(&clipRect);
329 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400330 }
331 }
332
Jim Van Verthc8a65e32017-10-25 14:25:27 -0400333 if (submitOp) {
334 auto op = this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, clipRect,
335 std::move(paint), props, distanceAdjustTable, cache, rtc);
336 if (op) {
337 if (skipClip) {
338 rtc->addDrawOp(GrNoClip(), std::move(op));
339 } else {
340 rtc->addDrawOp(clip, std::move(op));
341 }
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400342 }
Brian Salomon44acb5b2017-07-18 19:59:24 -0400343 }
joshualitt2e2202e2015-12-10 11:22:08 -0800344 }
345}
346
joshualitt8e0ef292016-02-19 14:13:03 -0800347static void calculate_translation(bool applyVM,
348 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
349 const SkMatrix& currentViewMatrix, SkScalar currentX,
350 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
351 if (applyVM) {
352 *transX = newViewMatrix.getTranslateX() +
353 newViewMatrix.getScaleX() * (newX - currentX) +
354 newViewMatrix.getSkewX() * (newY - currentY) -
355 currentViewMatrix.getTranslateX();
356
357 *transY = newViewMatrix.getTranslateY() +
358 newViewMatrix.getSkewY() * (newX - currentX) +
359 newViewMatrix.getScaleY() * (newY - currentY) -
360 currentViewMatrix.getTranslateY();
361 } else {
362 *transX = newX - currentX;
363 *transY = newY - currentY;
364 }
365}
366
Brian Osman11052242016-10-27 14:47:55 -0400367void GrAtlasTextBlob::flushBigGlyphs(GrContext* context, GrRenderTargetContext* rtc,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500368 const GrClip& clip, const SkPaint& paint,
joshualitt8e0ef292016-02-19 14:13:03 -0800369 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
joshualitt2e2202e2015-12-10 11:22:08 -0800370 const SkIRect& clipBounds) {
joshualitt8e0ef292016-02-19 14:13:03 -0800371 SkScalar transX, transY;
joshualitt2e2202e2015-12-10 11:22:08 -0800372 for (int i = 0; i < fBigGlyphs.count(); i++) {
373 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
Jim Van Verth08576e62016-11-16 10:15:23 -0500374 calculate_translation(bigGlyph.fTreatAsBMP, viewMatrix, x, y,
joshualitt8e0ef292016-02-19 14:13:03 -0800375 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
joshualitt2e2202e2015-12-10 11:22:08 -0800376 SkMatrix ctm;
377 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800378 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
Jim Van Verth08576e62016-11-16 10:15:23 -0500379 if (!bigGlyph.fTreatAsBMP) {
joshualitt8e0ef292016-02-19 14:13:03 -0800380 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800381 }
382
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500383 GrBlurUtils::drawPathWithMaskFilter(context, rtc, clip, bigGlyph.fPath, paint, ctm, nullptr,
384 clipBounds, false);
joshualitt2e2202e2015-12-10 11:22:08 -0800385 }
386}
387
Brian Osman11052242016-10-27 14:47:55 -0400388void GrAtlasTextBlob::flushRunAsPaths(GrContext* context, GrRenderTargetContext* rtc,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500389 const SkSurfaceProps& props, const SkTextBlobRunIterator& it,
390 const GrClip& clip, const GrTextUtils::Paint& paint,
joshualitt2e2202e2015-12-10 11:22:08 -0800391 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
392 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800393 size_t textLen = it.glyphCount() * sizeof(uint16_t);
394 const SkPoint& offset = it.offset();
395
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500396 GrTextUtils::RunPaint runPaint(&paint, drawFilter, props);
397 if (!runPaint.modifyForRun(it)) {
joshualitt2e2202e2015-12-10 11:22:08 -0800398 return;
399 }
400
joshualitt2e2202e2015-12-10 11:22:08 -0800401 switch (it.positioning()) {
402 case SkTextBlob::kDefault_Positioning:
Brian Osman11052242016-10-27 14:47:55 -0400403 GrTextUtils::DrawTextAsPath(context, rtc, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500404 (const char*)it.glyphs(), textLen, x + offset.x(),
405 y + offset.y(), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800406 break;
407 case SkTextBlob::kHorizontal_Positioning:
Brian Osman11052242016-10-27 14:47:55 -0400408 GrTextUtils::DrawPosTextAsPath(context, rtc, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500409 (const char*)it.glyphs(), textLen, it.pos(), 1,
410 SkPoint::Make(x, y + offset.y()), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800411 break;
412 case SkTextBlob::kFull_Positioning:
Brian Osman11052242016-10-27 14:47:55 -0400413 GrTextUtils::DrawPosTextAsPath(context, rtc, props, clip, runPaint, viewMatrix,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500414 (const char*)it.glyphs(), textLen, it.pos(), 2,
415 SkPoint::Make(x, y), clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800416 break;
417 }
418}
419
Brian Salomon82f44312017-01-11 13:42:54 -0500420void GrAtlasTextBlob::flushCached(GrContext* context, GrRenderTargetContext* rtc,
421 const SkTextBlob* blob, const SkSurfaceProps& props,
joshualitt2e2202e2015-12-10 11:22:08 -0800422 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500423 const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter,
424 const GrClip& clip, const SkMatrix& viewMatrix,
425 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800426 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
427 // it as paths
joshualitt2e2202e2015-12-10 11:22:08 -0800428 SkTextBlobRunIterator it(blob);
429 for (int run = 0; !it.done(); it.next(), run++) {
430 if (fRuns[run].fDrawAsPaths) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500431 this->flushRunAsPaths(context, rtc, props, it, clip, paint, drawFilter, viewMatrix,
432 clipBounds, x, y);
joshualitt2e2202e2015-12-10 11:22:08 -0800433 continue;
434 }
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500435 this->flushRun(rtc, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
436 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800437 }
438
439 // Now flush big glyphs
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500440 this->flushBigGlyphs(context, rtc, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800441}
442
Brian Salomon82f44312017-01-11 13:42:54 -0500443void GrAtlasTextBlob::flushThrowaway(GrContext* context, GrRenderTargetContext* rtc,
joshualitt2e2202e2015-12-10 11:22:08 -0800444 const SkSurfaceProps& props,
445 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500446 const GrTextUtils::Paint& paint, const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500447 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
joshualitt8e0ef292016-02-19 14:13:03 -0800448 SkScalar x, SkScalar y) {
joshualitt2e2202e2015-12-10 11:22:08 -0800449 for (int run = 0; run < fRunCount; run++) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500450 this->flushRun(rtc, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
Brian Salomon82f44312017-01-11 13:42:54 -0500451 context->getAtlasGlyphCache());
joshualitt2e2202e2015-12-10 11:22:08 -0800452 }
453
454 // Now flush big glyphs
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500455 this->flushBigGlyphs(context, rtc, clip, paint, viewMatrix, x, y, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800456}
457
Brian Salomon44acb5b2017-07-18 19:59:24 -0400458std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
Brian Salomonf8334782017-01-03 09:42:58 -0500459 int glyphCount, int run, int subRun, const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500460 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400461 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
462 GrRenderTargetContext* rtc) {
joshualitt7481e752016-01-22 06:08:48 -0800463 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400464 SkIRect emptyRect = SkIRect::MakeEmpty();
465 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect,
466 paint, props, distanceAdjustTable, cache, rtc);
joshualitt323c2eb2016-01-20 06:48:47 -0800467}
joshualitt2e2202e2015-12-10 11:22:08 -0800468
joshualitt259fbf12015-07-21 11:39:34 -0700469void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800470 SkASSERT_RELEASE(l.fSize == r.fSize);
471 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700472
joshualitt2f2ee832016-02-10 08:52:24 -0800473 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
474 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
475 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700476
joshualitt2f2ee832016-02-10 08:52:24 -0800477 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
478 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
479 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700480
joshualitt2f2ee832016-02-10 08:52:24 -0800481 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
joshualitt259fbf12015-07-21 11:39:34 -0700482 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
483 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
484 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
485
joshualitt2f2ee832016-02-10 08:52:24 -0800486 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
joshualitt259fbf12015-07-21 11:39:34 -0700487 // We can't assert that these have the same translations
488 }
489
joshualitt2f2ee832016-02-10 08:52:24 -0800490 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800491 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
492 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
493 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
494 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700495
joshualitt2f2ee832016-02-10 08:52:24 -0800496 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700497 for (int i = 0; i < l.fRunCount; i++) {
498 const Run& lRun = l.fRuns[i];
499 const Run& rRun = r.fRuns[i];
500
joshualitt259fbf12015-07-21 11:39:34 -0700501 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800502 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500503 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700504 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800505 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700506 }
507
joshualitt259fbf12015-07-21 11:39:34 -0700508
joshualitt2f2ee832016-02-10 08:52:24 -0800509 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
510 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700511 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700512
513 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800514 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
515 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700516 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
517 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700518 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800519 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700520 }
521
522 // color can be changed
523 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800524 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
525 SkASSERT_RELEASE(lRun.fDrawAsPaths == rRun.fDrawAsPaths);
joshualitt259fbf12015-07-21 11:39:34 -0700526
joshualitt2f2ee832016-02-10 08:52:24 -0800527 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700528 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
529 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
530 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
531
joshualitt2f2ee832016-02-10 08:52:24 -0800532 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
533 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700534
joshualitt2f2ee832016-02-10 08:52:24 -0800535 if (lSubRun.strike()) {
536 SkASSERT_RELEASE(rSubRun.strike());
Brian Salomonf856fd12016-12-16 14:24:34 -0500537 SkASSERT_RELEASE(GrAtlasTextStrike::GetKey(*lSubRun.strike()) ==
538 GrAtlasTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800539
540 } else {
541 SkASSERT_RELEASE(!rSubRun.strike());
542 }
543
544 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
545 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
546 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
547 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
548 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
549 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
550 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700551 }
552 }
553}
joshualitt8e0ef292016-02-19 14:13:03 -0800554
555void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
556 SkScalar x, SkScalar y, SkScalar* transX,
557 SkScalar* transY) {
558 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
559 fCurrentViewMatrix, fX, fY, transX, transY);
560 fCurrentViewMatrix = viewMatrix;
561 fX = x;
562 fY = y;
563}