blob: 0f5b7ea184f28461040cc0bdf8f4829afc2b75c2 [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
Herb Derby86240592018-05-24 16:12:31 -04008#include "GrTextBlob.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"
Robert Phillips46a13382018-08-23 13:53:01 -040012#include "GrShape.h"
Robert Phillips137ca522018-08-15 10:14:33 -040013#include "GrStyle.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040014#include "GrTextTarget.h"
joshualitt2e2202e2015-12-10 11:22:08 -080015#include "SkColorFilter.h"
Mike Reed80747ef2018-01-23 15:29:32 -050016#include "SkMaskFilterBase.h"
Mike Reedec7278e2019-02-01 14:00:34 -050017#include "SkPaintPriv.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "ops/GrAtlasTextOp.h"
Ben Wagner75d6db72018-09-20 14:39:39 -040019
Mike Klein79aea6a2018-06-11 10:45:26 -040020#include <new>
joshualitt2e2202e2015-12-10 11:22:08 -080021
Ben Wagner75d6db72018-09-20 14:39:39 -040022template <size_t N> static size_t sk_align(size_t s) {
23 return ((s + (N-1)) / N) * N;
24}
25
Herb Derby5424a5e2018-11-14 12:04:38 -050026sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount, GrColor color) {
Herb Derby86240592018-05-24 16:12:31 -040027 // We allocate size for the GrTextBlob itself, plus size for the vertices array,
joshualitt92303772016-02-10 11:55:52 -080028 // and size for the glyphIds array.
29 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
Ben Wagner75d6db72018-09-20 14:39:39 -040030
Herb Derbyf7d5d742018-11-16 13:24:32 -050031 size_t blobStart = 0;
32 size_t vertex = sk_align<alignof(char)> (blobStart + sizeof(GrTextBlob) * 1);
Ben Wagner75d6db72018-09-20 14:39:39 -040033 size_t glyphs = sk_align<alignof(GrGlyph*)> (vertex + sizeof(char) * verticesCount);
34 size_t runs = sk_align<alignof(GrTextBlob::Run)>(glyphs + sizeof(GrGlyph*) * glyphCount);
35 size_t size = (runs + sizeof(GrTextBlob::Run) * runCount);
joshualitt92303772016-02-10 11:55:52 -080036
Herb Derbyb12175f2018-05-23 16:38:09 -040037 void* allocation = ::operator new (size);
38
joshualitt92303772016-02-10 11:55:52 -080039 if (CACHE_SANITY_CHECK) {
40 sk_bzero(allocation, size);
41 }
42
Herb Derbyf7d5d742018-11-16 13:24:32 -050043 sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{}};
44 blob->fSize = size;
joshualitt92303772016-02-10 11:55:52 -080045
46 // setup offsets for vertices / glyphs
Herb Derbyf7d5d742018-11-16 13:24:32 -050047 blob->fVertices = SkTAddOffset<char>(blob.get(), vertex);
48 blob->fGlyphs = SkTAddOffset<GrGlyph*>(blob.get(), glyphs);
49 blob->fRuns = SkTAddOffset<GrTextBlob::Run>(blob.get(), runs);
joshualitt92303772016-02-10 11:55:52 -080050
51 // Initialize runs
52 for (int i = 0; i < runCount; i++) {
Herb Derby5424a5e2018-11-14 12:04:38 -050053 new (&blob->fRuns[i]) GrTextBlob::Run{blob.get(), color};
joshualitt92303772016-02-10 11:55:52 -080054 }
Herb Derbyf7d5d742018-11-16 13:24:32 -050055 blob->fRunCountLimit = runCount;
56 return blob;
joshualitt92303772016-02-10 11:55:52 -080057}
58
Herbert Derby86dbae92019-01-03 13:10:59 -050059void GrTextBlob::Run::setupFont(const SkPaint& skPaint,
60 const SkFont& skFont,
61 const SkDescriptor& cacheDescriptor) {
Herb Derby087fad72019-01-22 14:45:16 -050062 fTypeface = skFont.refTypefaceOrDefault();
Herbert Derby86dbae92019-01-03 13:10:59 -050063 SkScalerContextEffects effects{skPaint};
64 fPathEffect = sk_ref_sp(effects.fPathEffect);
65 fMaskFilter = sk_ref_sp(effects.fMaskFilter);
66 // if we have an override descriptor for the run, then we should use that
67 SkAutoDescriptor* desc =
68 fARGBFallbackDescriptor.get() ? fARGBFallbackDescriptor.get() : &fDescriptor;
69 // Set up the descriptor for possible cache lookups during regen.
70 desc->reset(cacheDescriptor);
71}
72
Herb Derby2bb343c2018-11-08 15:03:48 -050073void GrTextBlob::Run::appendPathGlyph(const SkPath& path, SkPoint position,
Jim Van Verth54d9c882018-02-08 16:14:48 -050074 SkScalar scale, bool preTransformed) {
Herb Derby2bb343c2018-11-08 15:03:48 -050075 fPathGlyphs.push_back(PathGlyph(path, position.x(), position.y(), scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -080076}
77
Herb Derby0edb2142018-10-16 17:04:11 -040078bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
79 const SkMaskFilterBase::BlurRec& blurRec,
80 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittfd5f6c12015-12-10 07:44:50 -080081 // If we have LCD text then our canonical color will be set to transparent, in this case we have
82 // to regenerate the blob on any color change
83 // We use the grPaint to get any color filter effects
84 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Mike Reedec7278e2019-02-01 14:00:34 -050085 fLuminanceColor != SkPaintPriv::ComputeLuminanceColor(paint)) {
joshualittfd5f6c12015-12-10 07:44:50 -080086 return true;
87 }
88
joshualitt8e0ef292016-02-19 14:13:03 -080089 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -080090 return true;
91 }
92
Brian Salomon5c6ac642017-12-19 11:09:32 -050093 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -080094 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -080095 return true;
96 }
97
98 // We only cache one masked version
99 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400100 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800101 return true;
102 }
103
104 // Similarly, we only cache one version for each style
105 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400106 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
107 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
108 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800109 return true;
110 }
111
112 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
113 // for mixed blobs if this becomes an issue.
114 if (this->hasBitmap() && this->hasDistanceField()) {
115 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800116 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800117 return false;
118 }
119 return true;
120 }
121
122 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800123 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
124 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
125 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
126 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800127 return true;
128 }
129
Herb Derbycb65ce72018-10-17 11:39:41 -0400130 // If the text blob only has full pixel glyphs, then fractional part of the position does
131 // not affect the SkGlyphs used.
132 if (anyRunHasSubpixelPosition) {
133 // We can update the positions in the text blob without regenerating the whole
Herb Derby0edb2142018-10-16 17:04:11 -0400134 // blob, but only for integer translations.
135 // This cool bit of math will determine the necessary translation to apply to the
136 // already generated vertex coordinates to move them to the correct position.
137 SkScalar transX = viewMatrix.getTranslateX() +
138 viewMatrix.getScaleX() * (x - fInitialX) +
139 viewMatrix.getSkewX() * (y - fInitialY) -
140 fInitialViewMatrix.getTranslateX();
141 SkScalar transY = viewMatrix.getTranslateY() +
142 viewMatrix.getSkewY() * (x - fInitialX) +
143 viewMatrix.getScaleY() * (y - fInitialY) -
144 fInitialViewMatrix.getTranslateY();
145 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
146 return true;
147 }
joshualittfd5f6c12015-12-10 07:44:50 -0800148 }
joshualittfd5f6c12015-12-10 07:44:50 -0800149 } else if (this->hasDistanceField()) {
150 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
151 // distance field being generated, so we have to regenerate in those cases
152 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800153 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800154 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
155 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
156 return true;
157 }
joshualittfd5f6c12015-12-10 07:44:50 -0800158 }
159
joshualittfd5f6c12015-12-10 07:44:50 -0800160 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
161 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
162 // the blob anyways at flush time, so no need to regenerate explicitly
163 return false;
164}
165
Herb Derby86240592018-05-24 16:12:31 -0400166inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Herb Derby69ff8952018-11-12 11:39:12 -0500167 const SubRun& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400168 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Brian Osmancf860852018-10-31 14:04:39 -0400169 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400170 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800171 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800172
Brian Salomon44acb5b2017-07-18 19:59:24 -0400173 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400174 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500175 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800176 if (info.drawAsDistanceFields()) {
Brian Osman34ec3742018-07-03 10:40:57 -0400177 // TODO: Can we be even smarter based on the dest transfer function?
Brian Salomon44acb5b2017-07-18 19:59:24 -0400178 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillips7c525e62018-06-12 10:11:12 -0400179 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
Mike Reedec7278e2019-02-01 14:00:34 -0500180 target->colorSpaceInfo().isLinearlyBlended(),
181 SkPaintPriv::ComputeLuminanceColor(paint),
Ben Wagner4c329562018-04-18 16:04:46 -0400182 props, info.isAntiAliased(), info.hasUseLCDText());
joshualitt2e2202e2015-12-10 11:22:08 -0800183 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400184 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400185 info.needsTransform());
joshualitt2e2202e2015-12-10 11:22:08 -0800186 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500187 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800188 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400189 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800190 geometry.fBlob = SkRef(this);
191 geometry.fRun = run;
192 geometry.fSubRun = subRun;
Brian Osmancf860852018-10-31 14:04:39 -0400193 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800194 geometry.fX = x;
195 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500196 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400197 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800198}
199
joshualitt8e0ef292016-02-19 14:13:03 -0800200static void calculate_translation(bool applyVM,
201 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
202 const SkMatrix& currentViewMatrix, SkScalar currentX,
203 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
204 if (applyVM) {
205 *transX = newViewMatrix.getTranslateX() +
206 newViewMatrix.getScaleX() * (newX - currentX) +
207 newViewMatrix.getSkewX() * (newY - currentY) -
208 currentViewMatrix.getTranslateX();
209
210 *transY = newViewMatrix.getTranslateY() +
211 newViewMatrix.getSkewY() * (newX - currentX) +
212 newViewMatrix.getScaleY() * (newY - currentY) -
213 currentViewMatrix.getTranslateY();
214 } else {
215 *transX = newX - currentX;
216 *transY = newY - currentY;
217 }
218}
219
Herb Derbyc1b482c2018-08-09 15:02:27 -0400220void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
221 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400222 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400223 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500224
Herb Derby86240592018-05-24 16:12:31 -0400225 // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500226 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
Herb Derby2bb343c2018-11-08 15:03:48 -0500227 int lastRun = SkTMin(fRunCountLimit, (1 << 16)) - 1;
Herb Derbydac1ed52018-09-12 17:04:21 -0400228 // For each run in the GrTextBlob we're going to churn through all the glyphs.
229 // Each run is broken into a path part and a Mask / DFT / ARGB part.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500230 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400231
Jim Van Verth54d9c882018-02-08 16:14:48 -0500232 Run& run = fRuns[runIndex];
233
234 // first flush any path glyphs
235 if (run.fPathGlyphs.count()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400236 SkPaint runPaint{paint};
Mike Reed48b958b2018-12-03 13:09:02 -0500237 runPaint.setAntiAlias(run.fAntiAlias);
Herb Derby9f491482018-08-08 11:53:00 -0400238
Jim Van Verth54d9c882018-02-08 16:14:48 -0500239 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
Herb Derby86240592018-05-24 16:12:31 -0400240 GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
Robert Phillips137ca522018-08-15 10:14:33 -0400241
Herb Derbydac1ed52018-09-12 17:04:21 -0400242 SkMatrix ctm;
Robert Phillips137ca522018-08-15 10:14:33 -0400243 const SkPath* path = &pathGlyph.fPath;
Herb Derbydac1ed52018-09-12 17:04:21 -0400244
245 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400246 SkTLazy<SkPath> tmpPath;
247
Herb Derbydac1ed52018-09-12 17:04:21 -0400248 // The glyph positions and glyph outlines are either in device space or in source
249 // space based on fPreTransformed.
250 if (!pathGlyph.fPreTransformed) {
251 // Positions and outlines are in source space.
Robert Phillips137ca522018-08-15 10:14:33 -0400252
Herb Derbydac1ed52018-09-12 17:04:21 -0400253 ctm = viewMatrix;
Robert Phillipsd20d2612018-08-28 10:09:01 -0400254
Herb Derbydac1ed52018-09-12 17:04:21 -0400255 SkMatrix pathMatrix = SkMatrix::MakeScale(pathGlyph.fScale, pathGlyph.fScale);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400256
Herb Derbydac1ed52018-09-12 17:04:21 -0400257 // The origin for the blob may have changed, so figure out the delta.
258 SkVector originShift = SkPoint{x, y} - SkPoint{fInitialX, fInitialY};
259
260 // Shift the original glyph location in source space to the position of the new
261 // blob.
262 pathMatrix.postTranslate(originShift.x() + pathGlyph.fX,
263 originShift.y() + pathGlyph.fY);
264
265 // If there are shaders, blurs or styles, the path must be scaled into source
266 // space independently of the CTM. This allows the CTM to be correct for the
267 // different effects.
268 GrStyle style(runPaint);
269 bool scalePath = runPaint.getShader()
270 || style.applies()
271 || runPaint.getMaskFilter();
272 if (!scalePath) {
273 // Scale can be applied to CTM -- no effects.
274
275 ctm.preConcat(pathMatrix);
276 } else {
277 // Scale the outline into source space.
278
279 // Transform the path form the normalized outline to source space. This
280 // way the CTM will remain the same so it can be used by the effects.
281 SkPath* sourceOutline = tmpPath.init();
282 path->transform(pathMatrix, sourceOutline);
283 sourceOutline->setIsVolatile(true);
284 path = sourceOutline;
285 }
286
287
Robert Phillipsd20d2612018-08-28 10:09:01 -0400288 } else {
Herb Derbydac1ed52018-09-12 17:04:21 -0400289 // Positions and outlines are in device space.
290
291 SkPoint originalOrigin = {fInitialX, fInitialY};
292 fInitialViewMatrix.mapPoints(&originalOrigin, 1);
293
294 SkPoint newOrigin = {x, y};
295 viewMatrix.mapPoints(&newOrigin, 1);
296
297 // The origin shift in device space.
298 SkPoint originShift = newOrigin - originalOrigin;
299
300 // Shift the original glyph location in device space to the position of the
301 // new blob.
302 ctm = SkMatrix::MakeTrans(originShift.x() + pathGlyph.fX,
303 originShift.y() + pathGlyph.fY);
Robert Phillips137ca522018-08-15 10:14:33 -0400304 }
305
Robert Phillips46a13382018-08-23 13:53:01 -0400306 // TODO: we are losing the mutability of the path here
307 GrShape shape(*path, paint);
308
Herb Derbydac1ed52018-09-12 17:04:21 -0400309 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500310 }
joshualitt2e2202e2015-12-10 11:22:08 -0800311 }
joshualitt2e2202e2015-12-10 11:22:08 -0800312
Jim Van Verth54d9c882018-02-08 16:14:48 -0500313 // then flush each subrun, if any
314 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000315 continue;
316 }
Herb Derbydac1ed52018-09-12 17:04:21 -0400317
Jim Van Verth54d9c882018-02-08 16:14:48 -0500318 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
319 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
Herb Derby69ff8952018-11-12 11:39:12 -0500320 const SubRun& info = run.fSubRunInfo[subRun];
Jim Van Verth54d9c882018-02-08 16:14:48 -0500321 int glyphCount = info.glyphCount();
322 if (0 == glyphCount) {
323 continue;
324 }
325
326 bool skipClip = false;
327 bool submitOp = true;
328 SkIRect clipRect = SkIRect::MakeEmpty();
329 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
330 SkRRect clipRRect;
331 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400332 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500333 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthb515ae72018-05-23 16:44:55 -0400334 if (!info.drawAsDistanceFields() && !info.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500335 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500336 clipRRect.isRect() && GrAA::kNo == aa) {
337 skipClip = true;
338 // We only need to do clipping work if the subrun isn't contained by the clip
339 SkRect subRunBounds;
Jim Van Verth70276912018-06-01 13:46:46 -0400340 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y,
341 false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500342 if (!clipRRect.getBounds().contains(subRunBounds)) {
343 // If the subrun is completely outside, don't add an op for it
344 if (!clipRRect.getBounds().intersects(subRunBounds)) {
345 submitOp = false;
346 }
347 else {
348 clipRRect.getBounds().round(&clipRect);
349 }
350 }
351 }
352
353 if (submitOp) {
354 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400355 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500356 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500357 if (op) {
358 if (skipClip) {
359 target->addDrawOp(GrNoClip(), std::move(op));
360 }
361 else {
362 target->addDrawOp(clip, std::move(op));
363 }
364 }
365 }
366 }
367
Jim Van Verth89737de2018-02-06 21:30:20 +0000368 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000369}
370
Herb Derby86240592018-05-24 16:12:31 -0400371std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400372 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
Brian Osmancf860852018-10-31 14:04:39 -0400373 SkScalar x, SkScalar y, const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400374 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400375 GrTextTarget* target) {
Herb Derby69ff8952018-11-12 11:39:12 -0500376 const GrTextBlob::SubRun& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400377 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derbybc6f9c92018-08-08 13:58:45 -0400378 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect,
379 paint, filteredColor, props, distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800380}
joshualitt2e2202e2015-12-10 11:22:08 -0800381
Herb Derby86240592018-05-24 16:12:31 -0400382void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800383 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700384
joshualitt2f2ee832016-02-10 08:52:24 -0800385 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
386 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700387
joshualitt2f2ee832016-02-10 08:52:24 -0800388 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
389 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
390 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700391
joshualitt2f2ee832016-02-10 08:52:24 -0800392 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800393 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
394 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
395 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
396 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700397
Herb Derby2bb343c2018-11-08 15:03:48 -0500398 SkASSERT_RELEASE(l.fRunCountLimit == r.fRunCountLimit);
399 for (int i = 0; i < l.fRunCountLimit; i++) {
joshualitt259fbf12015-07-21 11:39:34 -0700400 const Run& lRun = l.fRuns[i];
401 const Run& rRun = r.fRuns[i];
402
joshualitt259fbf12015-07-21 11:39:34 -0700403 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800404 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500405 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700406 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800407 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700408 }
409
joshualitt259fbf12015-07-21 11:39:34 -0700410
joshualitt2f2ee832016-02-10 08:52:24 -0800411 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
412 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700413 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700414
Herb Derby317adf72018-11-16 17:29:29 -0500415 if (lRun.fARGBFallbackDescriptor.get()) {
416 SkASSERT_RELEASE(lRun.fARGBFallbackDescriptor->getDesc());
417 SkASSERT_RELEASE(rRun.fARGBFallbackDescriptor.get() && rRun.fARGBFallbackDescriptor->getDesc());
418 SkASSERT_RELEASE(*lRun.fARGBFallbackDescriptor->getDesc() ==
419 *rRun.fARGBFallbackDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700420 } else {
Herb Derby317adf72018-11-16 17:29:29 -0500421 SkASSERT_RELEASE(!rRun.fARGBFallbackDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700422 }
423
424 // color can be changed
425 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800426 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700427
joshualitt2f2ee832016-02-10 08:52:24 -0800428 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700429 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
Herb Derby69ff8952018-11-12 11:39:12 -0500430 const SubRun& lSubRun = lRun.fSubRunInfo[j];
431 const SubRun& rSubRun = rRun.fSubRunInfo[j];
joshualitt259fbf12015-07-21 11:39:34 -0700432
joshualitt2f2ee832016-02-10 08:52:24 -0800433 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
434 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700435
joshualitt2f2ee832016-02-10 08:52:24 -0800436 if (lSubRun.strike()) {
437 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500438 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
439 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800440
441 } else {
442 SkASSERT_RELEASE(!rSubRun.strike());
443 }
444
445 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
446 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
447 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
448 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
449 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
450 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
451 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700452 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500453
454 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
455 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
456 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
457 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
458
459 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
460 // We can't assert that these have the same translations
461 }
joshualitt259fbf12015-07-21 11:39:34 -0700462 }
463}
joshualitt8e0ef292016-02-19 14:13:03 -0800464
Herb Derby69ff8952018-11-12 11:39:12 -0500465void GrTextBlob::SubRun::computeTranslation(const SkMatrix& viewMatrix,
466 SkScalar x, SkScalar y, SkScalar* transX,
467 SkScalar* transY) {
Herb Derby5f7b0142018-12-14 16:47:45 -0500468 // Don't use the matrix to translate on distance field for fallback subruns.
469 calculate_translation(!this->drawAsDistanceFields() && !this->isFallback(), viewMatrix,
470 x, y, fCurrentViewMatrix, fX, fY, transX, transY);
joshualitt8e0ef292016-02-19 14:13:03 -0800471 fCurrentViewMatrix = viewMatrix;
472 fX = x;
473 fY = y;
474}