blob: 887addd25cb8af47192782fd3baaf2657cfdcced [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"
joshualitte76b4bb32015-12-28 07:23:58 -080015#include "SkGlyphCache.h"
Mike Reed80747ef2018-01-23 15:29:32 -050016#include "SkMaskFilterBase.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
Brian Salomon18923f92017-11-06 16:26:02 -050038 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<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
joshualitte76b4bb32015-12-28 07:23:58 -080051SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex,
52 const SkSurfaceProps& props,
Herb Derbyd8327a82018-01-22 14:39:27 -050053 SkScalerContextFlags scalerContextFlags,
joshualitte76b4bb32015-12-28 07:23:58 -080054 const SkPaint& skPaint,
bungemanf6d1e602016-02-22 13:20:28 -080055 const SkMatrix* viewMatrix) {
joshualitte76b4bb32015-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;
Herb Derby980a48d2018-01-23 13:39:21 -050062 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
63 skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
joshualitte76b4bb32015-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);
bsalomon8b6fa5e2016-05-19 16:23:47 -070066 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Hal Canary144caf52016-11-07 17:57:18 -050067 return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
joshualitte76b4bb32015-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 Verth89737de2018-02-06 21:30:20 +000076 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 Verth89737de2018-02-06 21:30:20 +000083 this->appendBigGlyph(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
Brian Salomon5c6ac642017-12-19 11:09:32 -0500100 bool hasW = subRun->hasWCoord();
101 // DF glyphs drawn in perspective must always have a w coord.
102 SkASSERT(hasW || !subRun->drawAsDistanceFields() || !fInitialViewMatrix.hasPerspective());
103 // Non-DF glyphs should never have a w coord.
104 SkASSERT(!hasW || subRun->drawAsDistanceFields());
105
106 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800107
108 subRun->setMaskFormat(format);
109
joshualitt7481e752016-01-22 06:08:48 -0800110 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800111 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800112
113 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
114
Brian Salomon5c6ac642017-12-19 11:09:32 -0500115 // We always write the third position component used by SDFs. If it is unused it gets
116 // overwritten. Similarly, we always write the color and the blob will later overwrite it
117 // with texture coords if it is unused.
118 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
119 // V0
120 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
121 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
122 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800123
Brian Salomon5c6ac642017-12-19 11:09:32 -0500124 // V1
125 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
126 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
127 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800128
Brian Salomon5c6ac642017-12-19 11:09:32 -0500129 // V2
130 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
131 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
132 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800133
Brian Salomon5c6ac642017-12-19 11:09:32 -0500134 // V3
135 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
136 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800137
joshualitt18b072d2015-12-07 12:26:12 -0800138 subRun->appendVertices(vertexStride);
139 fGlyphs[subRun->glyphEndIndex()] = glyph;
140 subRun->glyphAppended();
141}
142
Jim Van Verth89737de2018-02-06 21:30:20 +0000143void GrAtlasTextBlob::appendBigGlyph(GrGlyph* glyph, SkGlyphCache* cache, const SkGlyph& skGlyph,
144 SkScalar x, SkScalar y, SkScalar scale, bool treatAsBMP) {
145 if (nullptr == glyph->fPath) {
146 const SkPath* glyphPath = cache->findPath(skGlyph);
147 if (!glyphPath) {
148 return;
149 }
150
151 glyph->fPath = new SkPath(*glyphPath);
152 }
153 fBigGlyphs.push_back(GrAtlasTextBlob::BigGlyph(*glyph->fPath, x, y, scale, treatAsBMP));
joshualitta06e6ab2015-12-10 08:54:41 -0800154}
155
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500156bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500157 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800158 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
159 // If we have LCD text then our canonical color will be set to transparent, in this case we have
160 // to regenerate the blob on any color change
161 // We use the grPaint to get any color filter effects
162 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400163 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800164 return true;
165 }
166
joshualitt8e0ef292016-02-19 14:13:03 -0800167 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800168 return true;
169 }
170
Brian Salomon5c6ac642017-12-19 11:09:32 -0500171 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800172 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800173 return true;
174 }
175
176 // We only cache one masked version
177 if (fKey.fHasBlur &&
178 (fBlurRec.fSigma != blurRec.fSigma ||
179 fBlurRec.fStyle != blurRec.fStyle ||
180 fBlurRec.fQuality != blurRec.fQuality)) {
181 return true;
182 }
183
184 // Similarly, we only cache one version for each style
185 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500186 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
187 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
188 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800189 return true;
190 }
191
192 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
193 // for mixed blobs if this becomes an issue.
194 if (this->hasBitmap() && this->hasDistanceField()) {
195 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800196 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800197 return false;
198 }
199 return true;
200 }
201
202 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800203 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
204 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
205 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
206 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800207 return true;
208 }
209
210 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
211 // but only for integer translations.
212 // This cool bit of math will determine the necessary translation to apply to the already
213 // generated vertex coordinates to move them to the correct position
214 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800215 viewMatrix.getScaleX() * (x - fInitialX) +
216 viewMatrix.getSkewX() * (y - fInitialY) -
217 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800218 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800219 viewMatrix.getSkewY() * (x - fInitialX) +
220 viewMatrix.getScaleY() * (y - fInitialY) -
221 fInitialViewMatrix.getTranslateY();
222 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800223 return true;
224 }
joshualittfd5f6c12015-12-10 07:44:50 -0800225 } else if (this->hasDistanceField()) {
226 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
227 // distance field being generated, so we have to regenerate in those cases
228 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800229 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800230 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
231 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
232 return true;
233 }
joshualittfd5f6c12015-12-10 07:44:50 -0800234 }
235
joshualittfd5f6c12015-12-10 07:44:50 -0800236 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
237 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
238 // the blob anyways at flush time, so no need to regenerate explicitly
239 return false;
240}
241
Brian Salomonf18b1d82017-10-27 11:30:49 -0400242inline std::unique_ptr<GrAtlasTextOp> GrAtlasTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400243 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400244 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
245 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400246 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
247 GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800248 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800249
Brian Salomon44acb5b2017-07-18 19:59:24 -0400250 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400251 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500252 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800253 if (info.drawAsDistanceFields()) {
joshualitt2e2202e2015-12-10 11:22:08 -0800254 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
Brian Salomon44acb5b2017-07-18 19:59:24 -0400255 op = GrAtlasTextOp::MakeDistanceField(
256 std::move(grPaint), glyphCount, cache, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400257 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400258 info.hasUseLCDText(), useBGR, info.isAntiAliased());
joshualitt2e2202e2015-12-10 11:22:08 -0800259 } else {
Brian Salomon44acb5b2017-07-18 19:59:24 -0400260 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount, cache);
joshualitt2e2202e2015-12-10 11:22:08 -0800261 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500262 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800263 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400264 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800265 geometry.fBlob = SkRef(this);
266 geometry.fRun = run;
267 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500268 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400269 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800270 geometry.fX = x;
271 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500272 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400273 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800274}
275
Jim Van Verth89737de2018-02-06 21:30:20 +0000276inline void GrAtlasTextBlob::flushRun(GrTextUtils::Target* target, const GrClip& clip, int run,
277 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
278 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
279 const GrDistanceFieldAdjustTable* distanceAdjustTable,
280 GrAtlasGlyphCache* cache) {
281 // GrAtlasTextBlob::makeOp only takes uint16_t values for run and subRun indices.
282 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
283 if (run >= (1 << 16)) {
284 return;
285 }
286 int lastRun = SkTMin(fRuns[run].fSubRunInfo.count(), 1 << 16) - 1;
287 for (int subRun = 0; subRun <= lastRun; subRun++) {
288 const Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
289 int glyphCount = info.glyphCount();
290 if (0 == glyphCount) {
291 continue;
292 }
293
294 bool skipClip = false;
295 bool submitOp = true;
296 SkIRect clipRect = SkIRect::MakeEmpty();
297 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
298 SkRRect clipRRect;
299 GrAA aa;
300 // We can clip geometrically if we're not using SDFs,
301 // and we have an axis-aligned rectangular non-AA clip
302 if (!info.drawAsDistanceFields() && clip.isRRect(rtBounds, &clipRRect, &aa) &&
303 clipRRect.isRect() && GrAA::kNo == aa) {
304 skipClip = true;
305 // We only need to do clipping work if the subrun isn't contained by the clip
306 SkRect subRunBounds;
307 this->computeSubRunBounds(&subRunBounds, run, subRun, viewMatrix, x, y);
308 if (!clipRRect.getBounds().contains(subRunBounds)) {
309 // If the subrun is completely outside, don't add an op for it
310 if (!clipRRect.getBounds().intersects(subRunBounds)) {
311 submitOp = false;
312 } else {
313 clipRRect.getBounds().round(&clipRect);
314 }
315 }
316 }
317
318 if (submitOp) {
319 auto op = this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, clipRect,
320 std::move(paint), props, distanceAdjustTable, cache, target);
321 if (op) {
322 if (skipClip) {
323 target->addDrawOp(GrNoClip(), std::move(op));
324 } else {
325 target->addDrawOp(clip, std::move(op));
326 }
327 }
328 }
329 }
330}
331
joshualitt8e0ef292016-02-19 14:13:03 -0800332static void calculate_translation(bool applyVM,
333 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
334 const SkMatrix& currentViewMatrix, SkScalar currentX,
335 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
336 if (applyVM) {
337 *transX = newViewMatrix.getTranslateX() +
338 newViewMatrix.getScaleX() * (newX - currentX) +
339 newViewMatrix.getSkewX() * (newY - currentY) -
340 currentViewMatrix.getTranslateX();
341
342 *transY = newViewMatrix.getTranslateY() +
343 newViewMatrix.getSkewY() * (newX - currentX) +
344 newViewMatrix.getScaleY() * (newY - currentY) -
345 currentViewMatrix.getTranslateY();
346 } else {
347 *transX = newX - currentX;
348 *transY = newY - currentY;
349 }
350}
351
Jim Van Verth89737de2018-02-06 21:30:20 +0000352void GrAtlasTextBlob::flushBigGlyphs(GrTextUtils::Target* target,
353 const GrClip& clip, const SkPaint& paint,
354 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
355 const SkIRect& clipBounds) {
356 SkScalar transX, transY;
357 for (int i = 0; i < fBigGlyphs.count(); i++) {
358 GrAtlasTextBlob::BigGlyph& bigGlyph = fBigGlyphs[i];
359 calculate_translation(bigGlyph.fTreatAsBMP, viewMatrix, x, y,
360 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
361 SkMatrix ctm;
362 ctm.setScale(bigGlyph.fScale, bigGlyph.fScale);
363 ctm.postTranslate(bigGlyph.fX + transX, bigGlyph.fY + transY);
364 if (!bigGlyph.fTreatAsBMP) {
365 ctm.postConcat(viewMatrix);
joshualitt2e2202e2015-12-10 11:22:08 -0800366 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000367 target->drawPath(clip, bigGlyph.fPath, paint, ctm, nullptr, clipBounds);
joshualitt2e2202e2015-12-10 11:22:08 -0800368 }
joshualitt2e2202e2015-12-10 11:22:08 -0800369}
370
Jim Van Verth89737de2018-02-06 21:30:20 +0000371void GrAtlasTextBlob::flushBigRun(GrTextUtils::Target* target,
372 const SkSurfaceProps& props, const SkTextBlobRunIterator& it,
373 const GrClip& clip, const GrTextUtils::Paint& paint,
374 SkDrawFilter* drawFilter, const SkMatrix& viewMatrix,
375 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
376 size_t textLen = it.glyphCount() * sizeof(uint16_t);
377 const SkPoint& offset = it.offset();
378
379 GrTextUtils::RunPaint runPaint(&paint, drawFilter, props);
380 if (!runPaint.modifyForRun(it)) {
381 return;
382 }
383
384 switch (it.positioning()) {
385 case SkTextBlob::kDefault_Positioning:
386 GrTextUtils::DrawBigText(target, clip, runPaint, viewMatrix,
387 (const char*)it.glyphs(), textLen, x + offset.x(),
388 y + offset.y(), clipBounds);
389 break;
390 case SkTextBlob::kHorizontal_Positioning:
391 GrTextUtils::DrawBigPosText(target, props, clip, runPaint, viewMatrix,
392 (const char*)it.glyphs(), textLen, it.pos(), 1,
393 SkPoint::Make(x, y + offset.y()), clipBounds);
394 break;
395 case SkTextBlob::kFull_Positioning:
396 GrTextUtils::DrawBigPosText(target, props, clip, runPaint, viewMatrix,
397 (const char*)it.glyphs(), textLen, it.pos(), 2,
398 SkPoint::Make(x, y), clipBounds);
399 break;
400 }
401}
402
403void GrAtlasTextBlob::flushCached(GrAtlasGlyphCache* atlasGlyphCache, GrTextUtils::Target* target,
404 const SkTextBlob* blob, const SkSurfaceProps& props,
405 const GrDistanceFieldAdjustTable* distanceAdjustTable,
406 const GrTextUtils::Paint& paint, SkDrawFilter* drawFilter,
407 const GrClip& clip, const SkMatrix& viewMatrix,
408 const SkIRect& clipBounds, SkScalar x, SkScalar y) {
409 // We loop through the runs of the blob, flushing each. If any run is too large, then we flush
410 // it as paths
411 SkTextBlobRunIterator it(blob);
412 for (int run = 0; !it.done(); it.next(), run++) {
413 if (fRuns[run].fTooBigForAtlas) {
414 this->flushBigRun(target, props, it, clip, paint, drawFilter, viewMatrix,
415 clipBounds, x, y);
416 continue;
417 }
418 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
419 atlasGlyphCache);
420 }
421
422 // Now flush big glyphs
423 this->flushBigGlyphs(target, clip, paint, viewMatrix, x, y, clipBounds);
424}
425
426void GrAtlasTextBlob::flushThrowaway(GrAtlasGlyphCache* atlasGlyphCache,
427 GrTextUtils::Target* target,
428 const SkSurfaceProps& props,
429 const GrDistanceFieldAdjustTable* distanceAdjustTable,
430 const GrTextUtils::Paint& paint, const GrClip& clip,
431 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
432 SkScalar x, SkScalar y) {
433 for (int run = 0; run < fRunCount; run++) {
434 this->flushRun(target, clip, run, viewMatrix, x, y, paint, props, distanceAdjustTable,
435 atlasGlyphCache);
436 }
437
438 // Now flush big glyphs
439 this->flushBigGlyphs(target, clip, paint, viewMatrix, x, y, clipBounds);
440}
441
Brian Salomon44acb5b2017-07-18 19:59:24 -0400442std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400443 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
444 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Brian Salomon44acb5b2017-07-18 19:59:24 -0400445 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrAtlasGlyphCache* cache,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400446 GrTextUtils::Target* target) {
joshualitt7481e752016-01-22 06:08:48 -0800447 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400448 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400449 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
450 distanceAdjustTable, cache, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800451}
joshualitt2e2202e2015-12-10 11:22:08 -0800452
joshualitt259fbf12015-07-21 11:39:34 -0700453void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800454 SkASSERT_RELEASE(l.fSize == r.fSize);
455 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700456
joshualitt2f2ee832016-02-10 08:52:24 -0800457 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
458 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
459 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700460
joshualitt2f2ee832016-02-10 08:52:24 -0800461 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
462 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
463 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700464
Jim Van Verth89737de2018-02-06 21:30:20 +0000465 SkASSERT_RELEASE(l.fBigGlyphs.count() == r.fBigGlyphs.count());
466 for (int i = 0; i < l.fBigGlyphs.count(); i++) {
467 const BigGlyph& lBigGlyph = l.fBigGlyphs[i];
468 const BigGlyph& rBigGlyph = r.fBigGlyphs[i];
469
470 SkASSERT_RELEASE(lBigGlyph.fPath == rBigGlyph.fPath);
471 // We can't assert that these have the same translations
472 }
473
joshualitt2f2ee832016-02-10 08:52:24 -0800474 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800475 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
476 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
477 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
478 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700479
joshualitt2f2ee832016-02-10 08:52:24 -0800480 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700481 for (int i = 0; i < l.fRunCount; i++) {
482 const Run& lRun = l.fRuns[i];
483 const Run& rRun = r.fRuns[i];
484
joshualitt259fbf12015-07-21 11:39:34 -0700485 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800486 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500487 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700488 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800489 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700490 }
491
joshualitt259fbf12015-07-21 11:39:34 -0700492
joshualitt2f2ee832016-02-10 08:52:24 -0800493 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
494 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700495 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700496
497 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800498 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
499 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700500 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
501 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700502 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800503 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700504 }
505
506 // color can be changed
507 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800508 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
Jim Van Verth89737de2018-02-06 21:30:20 +0000509 SkASSERT_RELEASE(lRun.fTooBigForAtlas == rRun.fTooBigForAtlas);
joshualitt259fbf12015-07-21 11:39:34 -0700510
joshualitt2f2ee832016-02-10 08:52:24 -0800511 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700512 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
513 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
514 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
515
joshualitt2f2ee832016-02-10 08:52:24 -0800516 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
517 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700518
joshualitt2f2ee832016-02-10 08:52:24 -0800519 if (lSubRun.strike()) {
520 SkASSERT_RELEASE(rSubRun.strike());
Brian Salomonf856fd12016-12-16 14:24:34 -0500521 SkASSERT_RELEASE(GrAtlasTextStrike::GetKey(*lSubRun.strike()) ==
522 GrAtlasTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800523
524 } else {
525 SkASSERT_RELEASE(!rSubRun.strike());
526 }
527
528 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
529 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
530 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
531 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
532 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
533 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
534 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700535 }
536 }
537}
joshualitt8e0ef292016-02-19 14:13:03 -0800538
539void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
540 SkScalar x, SkScalar y, SkScalar* transX,
541 SkScalar* transY) {
542 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
543 fCurrentViewMatrix, fX, fY, transX, transY);
544 fCurrentViewMatrix = viewMatrix;
545 fX = x;
546 fY = y;
547}