blob: dfe3312f99d38abfceda8e8dc13e708be862c355 [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"
Herb Derbyeb3f6742018-03-05 14:36:45 -050017#include "SkPaintPriv.h"
joshualitt2e2202e2015-12-10 11:22:08 -080018#include "SkTextBlobRunIterator.h"
Jim Van Verth54d9c882018-02-08 16:14:48 -050019#include "SkTextToPathIter.h"
Brian Salomon89527432016-12-16 09:52:16 -050020#include "ops/GrAtlasTextOp.h"
joshualitt2e2202e2015-12-10 11:22:08 -080021
Florin Malitac337c9e2017-03-10 18:02:29 +000022sk_sp<GrAtlasTextBlob> GrAtlasTextBlob::Make(GrMemoryPool* pool, int glyphCount, int runCount) {
joshualitt92303772016-02-10 11:55:52 -080023 // We allocate size for the GrAtlasTextBlob itself, plus size for the vertices array,
24 // and size for the glyphIds array.
25 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
26 size_t size = sizeof(GrAtlasTextBlob) +
27 verticesCount +
28 glyphCount * sizeof(GrGlyph**) +
29 sizeof(GrAtlasTextBlob::Run) * runCount;
30
Robert Phillips303cd582018-02-14 18:54:01 -050031 void* allocation;
32 if (pool) {
33 allocation = pool->allocate(size);
34 } else {
35 allocation = ::operator new (size);
36 }
joshualitt92303772016-02-10 11:55:52 -080037 if (CACHE_SANITY_CHECK) {
38 sk_bzero(allocation, size);
39 }
40
Florin Malitac337c9e2017-03-10 18:02:29 +000041 sk_sp<GrAtlasTextBlob> cacheBlob(new (allocation) GrAtlasTextBlob);
joshualitt92303772016-02-10 11:55:52 -080042 cacheBlob->fSize = size;
43
44 // setup offsets for vertices / glyphs
Brian Salomon18923f92017-11-06 16:26:02 -050045 cacheBlob->fVertices = sizeof(GrAtlasTextBlob) + reinterpret_cast<char*>(cacheBlob.get());
joshualitt92303772016-02-10 11:55:52 -080046 cacheBlob->fGlyphs = reinterpret_cast<GrGlyph**>(cacheBlob->fVertices + verticesCount);
47 cacheBlob->fRuns = reinterpret_cast<GrAtlasTextBlob::Run*>(cacheBlob->fGlyphs + glyphCount);
48
49 // Initialize runs
50 for (int i = 0; i < runCount; i++) {
51 new (&cacheBlob->fRuns[i]) GrAtlasTextBlob::Run;
52 }
53 cacheBlob->fRunCount = runCount;
54 cacheBlob->fPool = pool;
55 return cacheBlob;
56}
57
Herb Derby61d56b92018-03-05 14:23:47 -050058SkExclusiveStrikePtr GrAtlasTextBlob::setupCache(int runIndex,
59 const SkSurfaceProps& props,
60 SkScalerContextFlags scalerContextFlags,
61 const SkPaint& skPaint,
62 const SkMatrix* viewMatrix) {
joshualitte76b4bb32015-12-28 07:23:58 -080063 GrAtlasTextBlob::Run* run = &fRuns[runIndex];
64
65 // if we have an override descriptor for the run, then we should use that
66 SkAutoDescriptor* desc = run->fOverrideDescriptor.get() ? run->fOverrideDescriptor.get() :
67 &run->fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070068 SkScalerContextEffects effects;
Herb Derby980a48d2018-01-23 13:39:21 -050069 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
70 skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
Herb Derbyeb3f6742018-03-05 14:36:45 -050071 run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
bsalomon8b6fa5e2016-05-19 16:23:47 -070072 run->fPathEffect = sk_ref_sp(effects.fPathEffect);
bsalomon8b6fa5e2016-05-19 16:23:47 -070073 run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
Herb Derby61d56b92018-03-05 14:23:47 -050074 return SkGlyphCache::FindOrCreateStrikeExclusive(
75 *desc->getDesc(), effects, *run->fTypeface.get());
joshualitte76b4bb32015-12-28 07:23:58 -080076}
77
joshualittf528e0d2015-12-09 06:42:52 -080078void GrAtlasTextBlob::appendGlyph(int runIndex,
79 const SkRect& positions,
80 GrColor color,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050081 sk_sp<GrTextStrike> strike,
joshualitta06e6ab2015-12-10 08:54:41 -080082 GrGlyph* glyph,
bsalomonc2878e22016-05-17 13:18:03 -070083 SkGlyphCache* cache, const SkGlyph& skGlyph,
Jim Van Verth54d9c882018-02-08 16:14:48 -050084 SkScalar x, SkScalar y, SkScalar scale, bool preTransformed) {
Lee Salzman26d3f212017-01-27 14:31:10 -050085 if (positions.isEmpty()) {
86 return;
87 }
joshualitta06e6ab2015-12-10 08:54:41 -080088
89 // If the glyph is too large we fall back to paths
90 if (glyph->fTooLargeForAtlas) {
Jim Van Verth54d9c882018-02-08 16:14:48 -050091 if (nullptr == glyph->fPath) {
92 const SkPath* glyphPath = cache->findPath(skGlyph);
93 if (!glyphPath) {
94 return;
95 }
96
97 glyph->fPath = new SkPath(*glyphPath);
98 }
99 this->appendPathGlyph(runIndex, *glyph->fPath, x, y, scale, preTransformed);
joshualitta06e6ab2015-12-10 08:54:41 -0800100 return;
101 }
102
joshualittf528e0d2015-12-09 06:42:52 -0800103 Run& run = fRuns[runIndex];
104 GrMaskFormat format = glyph->fMaskFormat;
105
106 Run::SubRunInfo* subRun = &run.fSubRunInfo.back();
107 if (run.fInitialized && subRun->maskFormat() != format) {
108 subRun = &run.push_back();
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500109 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800110 } else if (!run.fInitialized) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500111 subRun->setStrike(std::move(strike));
joshualittf528e0d2015-12-09 06:42:52 -0800112 }
113
114 run.fInitialized = true;
115
Brian Salomon5c6ac642017-12-19 11:09:32 -0500116 bool hasW = subRun->hasWCoord();
117 // DF glyphs drawn in perspective must always have a w coord.
118 SkASSERT(hasW || !subRun->drawAsDistanceFields() || !fInitialViewMatrix.hasPerspective());
119 // Non-DF glyphs should never have a w coord.
120 SkASSERT(!hasW || subRun->drawAsDistanceFields());
121
122 size_t vertexStride = GetVertexStride(format, hasW);
joshualittf528e0d2015-12-09 06:42:52 -0800123
124 subRun->setMaskFormat(format);
125
joshualitt7481e752016-01-22 06:08:48 -0800126 subRun->joinGlyphBounds(positions);
joshualittf9e658b2015-12-09 09:26:44 -0800127 subRun->setColor(color);
joshualitt18b072d2015-12-07 12:26:12 -0800128
129 intptr_t vertex = reinterpret_cast<intptr_t>(this->fVertices + subRun->vertexEndIndex());
130
Brian Salomon5c6ac642017-12-19 11:09:32 -0500131 // We always write the third position component used by SDFs. If it is unused it gets
132 // overwritten. Similarly, we always write the color and the blob will later overwrite it
133 // with texture coords if it is unused.
134 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
135 // V0
136 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fTop, 1.f};
137 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
138 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800139
Brian Salomon5c6ac642017-12-19 11:09:32 -0500140 // V1
141 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fLeft, positions.fBottom, 1.f};
142 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
143 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800144
Brian Salomon5c6ac642017-12-19 11:09:32 -0500145 // V2
146 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fTop, 1.f};
147 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
148 vertex += vertexStride;
joshualitt18b072d2015-12-07 12:26:12 -0800149
Brian Salomon5c6ac642017-12-19 11:09:32 -0500150 // V3
151 *reinterpret_cast<SkPoint3*>(vertex) = {positions.fRight, positions.fBottom, 1.f};
152 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
joshualitt18b072d2015-12-07 12:26:12 -0800153
joshualitt18b072d2015-12-07 12:26:12 -0800154 subRun->appendVertices(vertexStride);
155 fGlyphs[subRun->glyphEndIndex()] = glyph;
156 subRun->glyphAppended();
Jim Van Verthcf838c72018-03-05 14:40:36 -0500157 subRun->setHasScaledGlyphs(SK_Scalar1 != scale);
joshualitt18b072d2015-12-07 12:26:12 -0800158}
159
Jim Van Verth54d9c882018-02-08 16:14:48 -0500160void GrAtlasTextBlob::appendPathGlyph(int runIndex, const SkPath& path, SkScalar x, SkScalar y,
161 SkScalar scale, bool preTransformed) {
162 Run& run = fRuns[runIndex];
163 run.fPathGlyphs.push_back(GrAtlasTextBlob::Run::PathGlyph(path, x, y, scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800164}
165
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500166bool GrAtlasTextBlob::mustRegenerate(const GrTextUtils::Paint& paint,
Mike Reed80747ef2018-01-23 15:29:32 -0500167 const SkMaskFilterBase::BlurRec& blurRec,
joshualittfd5f6c12015-12-10 07:44:50 -0800168 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
169 // If we have LCD text then our canonical color will be set to transparent, in this case we have
170 // to regenerate the blob on any color change
171 // We use the grPaint to get any color filter effects
172 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Jim Van Verthbc2cdd12017-06-08 11:14:35 -0400173 fLuminanceColor != paint.luminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800174 return true;
175 }
176
joshualitt8e0ef292016-02-19 14:13:03 -0800177 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800178 return true;
179 }
180
Brian Salomon5c6ac642017-12-19 11:09:32 -0500181 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800182 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800183 return true;
184 }
185
186 // We only cache one masked version
187 if (fKey.fHasBlur &&
188 (fBlurRec.fSigma != blurRec.fSigma ||
189 fBlurRec.fStyle != blurRec.fStyle ||
190 fBlurRec.fQuality != blurRec.fQuality)) {
191 return true;
192 }
193
194 // Similarly, we only cache one version for each style
195 if (fKey.fStyle != SkPaint::kFill_Style &&
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500196 (fStrokeInfo.fFrameWidth != paint.skPaint().getStrokeWidth() ||
197 fStrokeInfo.fMiterLimit != paint.skPaint().getStrokeMiter() ||
198 fStrokeInfo.fJoin != paint.skPaint().getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800199 return true;
200 }
201
202 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
203 // for mixed blobs if this becomes an issue.
204 if (this->hasBitmap() && this->hasDistanceField()) {
205 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800206 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800207 return false;
208 }
209 return true;
210 }
211
212 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800213 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
214 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
215 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
216 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800217 return true;
218 }
219
220 // We can update the positions in the cachedtextblobs without regenerating the whole blob,
221 // but only for integer translations.
222 // This cool bit of math will determine the necessary translation to apply to the already
223 // generated vertex coordinates to move them to the correct position
224 SkScalar transX = viewMatrix.getTranslateX() +
joshualitt8e0ef292016-02-19 14:13:03 -0800225 viewMatrix.getScaleX() * (x - fInitialX) +
226 viewMatrix.getSkewX() * (y - fInitialY) -
227 fInitialViewMatrix.getTranslateX();
joshualittfd5f6c12015-12-10 07:44:50 -0800228 SkScalar transY = viewMatrix.getTranslateY() +
joshualitt8e0ef292016-02-19 14:13:03 -0800229 viewMatrix.getSkewY() * (x - fInitialX) +
230 viewMatrix.getScaleY() * (y - fInitialY) -
231 fInitialViewMatrix.getTranslateY();
232 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800233 return true;
234 }
joshualittfd5f6c12015-12-10 07:44:50 -0800235 } else if (this->hasDistanceField()) {
236 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
237 // distance field being generated, so we have to regenerate in those cases
238 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800239 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800240 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
241 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
242 return true;
243 }
joshualittfd5f6c12015-12-10 07:44:50 -0800244 }
245
joshualittfd5f6c12015-12-10 07:44:50 -0800246 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
247 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
248 // the blob anyways at flush time, so no need to regenerate explicitly
249 return false;
250}
251
Brian Salomonf18b1d82017-10-27 11:30:49 -0400252inline std::unique_ptr<GrAtlasTextOp> GrAtlasTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400253 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400254 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
255 const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500256 const GrDistanceFieldAdjustTable* distanceAdjustTable,
257 GrRestrictedAtlasManager* restrictedAtlasManager, GrTextUtils::Target* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800258 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800259
Brian Salomon44acb5b2017-07-18 19:59:24 -0400260 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400261 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500262 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800263 if (info.drawAsDistanceFields()) {
joshualitt2e2202e2015-12-10 11:22:08 -0800264 bool useBGR = SkPixelGeometryIsBGR(props.pixelGeometry());
Brian Salomon44acb5b2017-07-18 19:59:24 -0400265 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500266 std::move(grPaint), glyphCount, restrictedAtlasManager, distanceAdjustTable,
Brian Salomonf18b1d82017-10-27 11:30:49 -0400267 target->colorSpaceInfo().isGammaCorrect(), paint.luminanceColor(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400268 info.hasUseLCDText(), useBGR, info.isAntiAliased());
joshualitt2e2202e2015-12-10 11:22:08 -0800269 } else {
Jim Van Verthcf838c72018-03-05 14:40:36 -0500270 op = GrAtlasTextOp::MakeBitmap(std::move(grPaint), format, glyphCount,
271 info.hasScaledGlyphs(), restrictedAtlasManager);
joshualitt2e2202e2015-12-10 11:22:08 -0800272 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500273 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800274 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400275 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800276 geometry.fBlob = SkRef(this);
277 geometry.fRun = run;
278 geometry.fSubRun = subRun;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500279 geometry.fColor =
Brian Osmanec8f8b02017-05-11 10:57:37 -0400280 info.maskFormat() == kARGB_GrMaskFormat ? GrColor_WHITE : paint.filteredPremulColor();
joshualitt8e0ef292016-02-19 14:13:03 -0800281 geometry.fX = x;
282 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500283 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400284 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800285}
286
joshualitt8e0ef292016-02-19 14:13:03 -0800287static void calculate_translation(bool applyVM,
288 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
289 const SkMatrix& currentViewMatrix, SkScalar currentX,
290 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
291 if (applyVM) {
292 *transX = newViewMatrix.getTranslateX() +
293 newViewMatrix.getScaleX() * (newX - currentX) +
294 newViewMatrix.getSkewX() * (newY - currentY) -
295 currentViewMatrix.getTranslateX();
296
297 *transY = newViewMatrix.getTranslateY() +
298 newViewMatrix.getSkewY() * (newX - currentX) +
299 newViewMatrix.getScaleY() * (newY - currentY) -
300 currentViewMatrix.getTranslateY();
301 } else {
302 *transX = newX - currentX;
303 *transY = newY - currentY;
304 }
305}
306
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500307void GrAtlasTextBlob::flush(GrRestrictedAtlasManager* restrictedAtlasManager,
308 GrTextUtils::Target* target, const SkSurfaceProps& props,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500309 const GrDistanceFieldAdjustTable* distanceAdjustTable,
310 const GrTextUtils::Paint& paint, const GrClip& clip,
311 const SkMatrix& viewMatrix, const SkIRect& clipBounds,
312 SkScalar x, SkScalar y) {
313
314 // GrAtlasTextBlob::makeOp only takes uint16_t values for run and subRun indices.
315 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
316 int lastRun = SkTMin(fRunCount, (1 << 16)) - 1;
317 GrTextUtils::RunPaint runPaint(&paint, nullptr, props);
318 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
319 Run& run = fRuns[runIndex];
320
321 // first flush any path glyphs
322 if (run.fPathGlyphs.count()) {
323 SkScalar transX, transY;
324 uint16_t paintFlags = run.fPaintFlags;
325 if (!runPaint.modifyForRun(
326 [paintFlags](SkPaint* p) {
327 p->setFlags((p->getFlags() & ~Run::kPaintFlagsMask) | paintFlags);
328 })) {
329 continue;
330 }
331 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
332 GrAtlasTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
333 calculate_translation(pathGlyph.fPreTransformed, viewMatrix, x, y,
334 fInitialViewMatrix, fInitialX, fInitialY, &transX, &transY);
335 const SkMatrix& ctm = pathGlyph.fPreTransformed ? SkMatrix::I() : viewMatrix;
336 SkMatrix pathMatrix;
337 pathMatrix.setScale(pathGlyph.fScale, pathGlyph.fScale);
338 pathMatrix.postTranslate(pathGlyph.fX + transX, pathGlyph.fY + transY);
339 target->drawPath(clip, pathGlyph.fPath, runPaint, ctm, &pathMatrix, clipBounds);
340 }
joshualitt2e2202e2015-12-10 11:22:08 -0800341 }
joshualitt2e2202e2015-12-10 11:22:08 -0800342
Jim Van Verth54d9c882018-02-08 16:14:48 -0500343 // then flush each subrun, if any
344 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000345 continue;
346 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500347 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
348 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
349 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
350 int glyphCount = info.glyphCount();
351 if (0 == glyphCount) {
352 continue;
353 }
354
355 bool skipClip = false;
356 bool submitOp = true;
357 SkIRect clipRect = SkIRect::MakeEmpty();
358 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
359 SkRRect clipRRect;
360 GrAA aa;
Jim Van Verthcf838c72018-03-05 14:40:36 -0500361 // We can clip geometrically if we're not using SDFs or scaled glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500362 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthcf838c72018-03-05 14:40:36 -0500363 if (!info.drawAsDistanceFields() && !info.hasScaledGlyphs() &&
364 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500365 clipRRect.isRect() && GrAA::kNo == aa) {
366 skipClip = true;
367 // We only need to do clipping work if the subrun isn't contained by the clip
368 SkRect subRunBounds;
369 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y);
370 if (!clipRRect.getBounds().contains(subRunBounds)) {
371 // If the subrun is completely outside, don't add an op for it
372 if (!clipRRect.getBounds().intersects(subRunBounds)) {
373 submitOp = false;
374 }
375 else {
376 clipRRect.getBounds().round(&clipRect);
377 }
378 }
379 }
380
381 if (submitOp) {
382 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
383 clipRect, std::move(paint), props, distanceAdjustTable,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500384 restrictedAtlasManager, target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500385 if (op) {
386 if (skipClip) {
387 target->addDrawOp(GrNoClip(), std::move(op));
388 }
389 else {
390 target->addDrawOp(clip, std::move(op));
391 }
392 }
393 }
394 }
395
Jim Van Verth89737de2018-02-06 21:30:20 +0000396 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000397}
398
Brian Salomon44acb5b2017-07-18 19:59:24 -0400399std::unique_ptr<GrDrawOp> GrAtlasTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400400 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
401 SkScalar x, SkScalar y, const GrTextUtils::Paint& paint, const SkSurfaceProps& props,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500402 const GrDistanceFieldAdjustTable* distanceAdjustTable,
403 GrRestrictedAtlasManager* restrictedAtlasManager, GrTextUtils::Target* target) {
joshualitt7481e752016-01-22 06:08:48 -0800404 const GrAtlasTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400405 SkIRect emptyRect = SkIRect::MakeEmpty();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400406 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect, paint, props,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500407 distanceAdjustTable, restrictedAtlasManager, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800408}
joshualitt2e2202e2015-12-10 11:22:08 -0800409
joshualitt259fbf12015-07-21 11:39:34 -0700410void GrAtlasTextBlob::AssertEqual(const GrAtlasTextBlob& l, const GrAtlasTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800411 SkASSERT_RELEASE(l.fSize == r.fSize);
412 SkASSERT_RELEASE(l.fPool == r.fPool);
joshualitt259fbf12015-07-21 11:39:34 -0700413
joshualitt2f2ee832016-02-10 08:52:24 -0800414 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
415 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
416 SkASSERT_RELEASE(l.fBlurRec.fQuality == r.fBlurRec.fQuality);
joshualitt259fbf12015-07-21 11:39:34 -0700417
joshualitt2f2ee832016-02-10 08:52:24 -0800418 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
419 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
420 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700421
joshualitt2f2ee832016-02-10 08:52:24 -0800422 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800423 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
424 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
425 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
426 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700427
joshualitt2f2ee832016-02-10 08:52:24 -0800428 SkASSERT_RELEASE(l.fRunCount == r.fRunCount);
joshualitt259fbf12015-07-21 11:39:34 -0700429 for (int i = 0; i < l.fRunCount; i++) {
430 const Run& lRun = l.fRuns[i];
431 const Run& rRun = r.fRuns[i];
432
joshualitt259fbf12015-07-21 11:39:34 -0700433 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800434 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500435 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700436 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800437 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700438 }
439
joshualitt259fbf12015-07-21 11:39:34 -0700440
joshualitt2f2ee832016-02-10 08:52:24 -0800441 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
442 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700443 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700444
445 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800446 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
447 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700448 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
449 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700450 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800451 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700452 }
453
454 // color can be changed
455 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800456 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700457
joshualitt2f2ee832016-02-10 08:52:24 -0800458 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700459 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
460 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
461 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
462
joshualitt2f2ee832016-02-10 08:52:24 -0800463 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
464 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700465
joshualitt2f2ee832016-02-10 08:52:24 -0800466 if (lSubRun.strike()) {
467 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500468 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
469 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800470
471 } else {
472 SkASSERT_RELEASE(!rSubRun.strike());
473 }
474
475 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
476 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
477 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
478 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
479 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
480 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
481 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700482 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500483
484 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
485 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
486 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
487 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
488
489 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
490 // We can't assert that these have the same translations
491 }
joshualitt259fbf12015-07-21 11:39:34 -0700492 }
493}
joshualitt8e0ef292016-02-19 14:13:03 -0800494
495void GrAtlasTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
496 SkScalar x, SkScalar y, SkScalar* transX,
497 SkScalar* transY) {
498 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
499 fCurrentViewMatrix, fX, fY, transX, transY);
500 fCurrentViewMatrix = viewMatrix;
501 fX = x;
502 fY = y;
503}