blob: fe52f19d5198c2eece53978ac631e12482814cff [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"
joshualitte76b4bb32015-12-28 07:23:58 -080016#include "SkGlyphCache.h"
Mike Reed80747ef2018-01-23 15:29:32 -050017#include "SkMaskFilterBase.h"
Herb Derbyeb3f6742018-03-05 14:36:45 -050018#include "SkPaintPriv.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"
Ben Wagner75d6db72018-09-20 14:39:39 -040021
Mike Klein79aea6a2018-06-11 10:45:26 -040022#include <new>
joshualitt2e2202e2015-12-10 11:22:08 -080023
Ben Wagner75d6db72018-09-20 14:39:39 -040024template <size_t N> static size_t sk_align(size_t s) {
25 return ((s + (N-1)) / N) * N;
26}
27
Herb Derby86240592018-05-24 16:12:31 -040028sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount) {
29 // We allocate size for the GrTextBlob itself, plus size for the vertices array,
joshualitt92303772016-02-10 11:55:52 -080030 // and size for the glyphIds array.
31 size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
Ben Wagner75d6db72018-09-20 14:39:39 -040032
33 size_t blob = 0;
34 size_t vertex = sk_align<alignof(char)> (blob + sizeof(GrTextBlob) * 1);
35 size_t glyphs = sk_align<alignof(GrGlyph*)> (vertex + sizeof(char) * verticesCount);
36 size_t runs = sk_align<alignof(GrTextBlob::Run)>(glyphs + sizeof(GrGlyph*) * glyphCount);
37 size_t size = (runs + sizeof(GrTextBlob::Run) * runCount);
joshualitt92303772016-02-10 11:55:52 -080038
Herb Derbyb12175f2018-05-23 16:38:09 -040039 void* allocation = ::operator new (size);
40
joshualitt92303772016-02-10 11:55:52 -080041 if (CACHE_SANITY_CHECK) {
42 sk_bzero(allocation, size);
43 }
44
Herb Derby86240592018-05-24 16:12:31 -040045 sk_sp<GrTextBlob> cacheBlob(new (allocation) GrTextBlob);
joshualitt92303772016-02-10 11:55:52 -080046 cacheBlob->fSize = size;
47
48 // setup offsets for vertices / glyphs
Ben Wagner75d6db72018-09-20 14:39:39 -040049 cacheBlob->fVertices = SkTAddOffset<char>(cacheBlob.get(), vertex);
50 cacheBlob->fGlyphs = SkTAddOffset<GrGlyph*>(cacheBlob.get(), glyphs);
51 cacheBlob->fRuns = SkTAddOffset<GrTextBlob::Run>(cacheBlob.get(), runs);
joshualitt92303772016-02-10 11:55:52 -080052
53 // Initialize runs
54 for (int i = 0; i < runCount; i++) {
Herb Derby86240592018-05-24 16:12:31 -040055 new (&cacheBlob->fRuns[i]) GrTextBlob::Run;
joshualitt92303772016-02-10 11:55:52 -080056 }
Herb Derby2bb343c2018-11-08 15:03:48 -050057 cacheBlob->fRunCountLimit = runCount;
joshualitt92303772016-02-10 11:55:52 -080058 return cacheBlob;
59}
60
Herb Derby2bb343c2018-11-08 15:03:48 -050061SkExclusiveStrikePtr GrTextBlob::Run::setupCache(const SkPaint& skPaint,
62 const SkSurfaceProps& props,
63 SkScalerContextFlags scalerContextFlags,
64 const SkMatrix& viewMatrix) {
joshualitte76b4bb32015-12-28 07:23:58 -080065
66 // if we have an override descriptor for the run, then we should use that
Herb Derby2bb343c2018-11-08 15:03:48 -050067 SkAutoDescriptor* desc = fOverrideDescriptor.get() ? fOverrideDescriptor.get() : &fDescriptor;
bsalomon8b6fa5e2016-05-19 16:23:47 -070068 SkScalerContextEffects effects;
Herb Derby980a48d2018-01-23 13:39:21 -050069 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
Herb Derbyc3415002018-11-08 16:40:26 -050070 skPaint, props, scalerContextFlags, viewMatrix, desc, &effects);
Herb Derby2bb343c2018-11-08 15:03:48 -050071 fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
72 fPathEffect = sk_ref_sp(effects.fPathEffect);
73 fMaskFilter = sk_ref_sp(effects.fMaskFilter);
74 return SkStrikeCache::FindOrCreateStrikeExclusive(*desc->getDesc(), effects, *fTypeface);
joshualitte76b4bb32015-12-28 07:23:58 -080075}
76
Herb Derby2bb343c2018-11-08 15:03:48 -050077static SkRect rect_to_draw(
78 const SkGlyph& glyph, SkPoint origin, SkScalar textScale, bool isDFT) {
joshualitta06e6ab2015-12-10 08:54:41 -080079
Herb Derby2bb343c2018-11-08 15:03:48 -050080 SkScalar dx = SkIntToScalar(glyph.fLeft);
81 SkScalar dy = SkIntToScalar(glyph.fTop);
82 SkScalar width = SkIntToScalar(glyph.fWidth);
83 SkScalar height = SkIntToScalar(glyph.fHeight);
joshualittf528e0d2015-12-09 06:42:52 -080084
Herb Derby2bb343c2018-11-08 15:03:48 -050085 if (isDFT) {
86 dx += SK_DistanceFieldInset;
87 dy += SK_DistanceFieldInset;
88 width -= 2 * SK_DistanceFieldInset;
89 height -= 2 * SK_DistanceFieldInset;
joshualittf528e0d2015-12-09 06:42:52 -080090 }
91
Herb Derby2bb343c2018-11-08 15:03:48 -050092 dx *= textScale;
93 dy *= textScale;
94 width *= textScale;
95 height *= textScale;
joshualittf528e0d2015-12-09 06:42:52 -080096
Herb Derby2bb343c2018-11-08 15:03:48 -050097 return SkRect::MakeXYWH(origin.x() + dx, origin.y() + dy, width, height);
joshualitt18b072d2015-12-07 12:26:12 -080098}
99
Herb Derby2bb343c2018-11-08 15:03:48 -0500100void GrTextBlob::Run::appendGlyph(GrTextBlob* blob,
101 const sk_sp<GrTextStrike>& strike,
102 const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
103 SkPoint origin,
104 const SkPMColor4f& color4f, SkGlyphCache* skGlyphCache,
105 SkScalar textRatio, bool needsTransform) {
106
107 GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
108 skGlyph.getSubXFixed(),
109 skGlyph.getSubYFixed(),
110 maskStyle);
111 GrGlyph* glyph = strike->getGlyph(skGlyph, id, skGlyphCache);
112 if (!glyph) {
113 return;
114 }
115
116 SkASSERT(skGlyph.fWidth == glyph->width());
117 SkASSERT(skGlyph.fHeight == glyph->height());
118
119 bool isDFT = maskStyle == GrGlyph::kDistance_MaskStyle;
120
121 SkRect glyphRect = rect_to_draw(skGlyph, origin, textRatio, isDFT);
122 if (!glyphRect.isEmpty()) {
123 // TODO4F: Preserve float colors
124 GrColor color = color4f.toBytes_RGBA();
125
126 GrMaskFormat format = glyph->fMaskFormat;
127
128 Run::SubRunInfo* subRun = &fSubRunInfo.back();
129 if (fInitialized && subRun->maskFormat() != format) {
130 subRun = &pushBackSubRun();
131 subRun->setStrike(strike);
132 } else if (!fInitialized) {
133 subRun->setStrike(strike);
134 }
135
136 fInitialized = true;
137
138 bool hasW = subRun->hasWCoord();
139 // glyphs drawn in perspective must always have a w coord.
140 SkASSERT(hasW || !blob->fInitialViewMatrix.hasPerspective());
141
142 size_t vertexStride = GetVertexStride(format, hasW);
143
144 subRun->setMaskFormat(format);
145
146 subRun->joinGlyphBounds(glyphRect);
147 subRun->setColor(color);
148
149 intptr_t vertex = reinterpret_cast<intptr_t>(blob->fVertices + subRun->vertexEndIndex());
150
151 // We always write the third position component used by SDFs. If it is unused it gets
152 // overwritten. Similarly, we always write the color and the blob will later overwrite it
153 // with texture coords if it is unused.
154 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
155 // V0
156 *reinterpret_cast<SkPoint3*>(vertex) = {glyphRect.fLeft, glyphRect.fTop, 1.f};
157 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
158 vertex += vertexStride;
159
160 // V1
161 *reinterpret_cast<SkPoint3*>(vertex) = {glyphRect.fLeft, glyphRect.fBottom, 1.f};
162 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
163 vertex += vertexStride;
164
165 // V2
166 *reinterpret_cast<SkPoint3*>(vertex) = {glyphRect.fRight, glyphRect.fTop, 1.f};
167 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
168 vertex += vertexStride;
169
170 // V3
171 *reinterpret_cast<SkPoint3*>(vertex) = {glyphRect.fRight, glyphRect.fBottom, 1.f};
172 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
173
174 subRun->appendVertices(vertexStride);
175 blob->fGlyphs[subRun->glyphEndIndex()] = glyph;
176 subRun->glyphAppended();
177 subRun->setNeedsTransform(needsTransform);
178 }
179}
180
181void GrTextBlob::Run::appendPathGlyph(const SkPath& path, SkPoint position,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500182 SkScalar scale, bool preTransformed) {
Herb Derby2bb343c2018-11-08 15:03:48 -0500183 fPathGlyphs.push_back(PathGlyph(path, position.x(), position.y(), scale, preTransformed));
joshualitta06e6ab2015-12-10 08:54:41 -0800184}
185
Herb Derby0edb2142018-10-16 17:04:11 -0400186bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
187 const SkMaskFilterBase::BlurRec& blurRec,
188 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittfd5f6c12015-12-10 07:44:50 -0800189 // If we have LCD text then our canonical color will be set to transparent, in this case we have
190 // to regenerate the blob on any color change
191 // We use the grPaint to get any color filter effects
192 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400193 fLuminanceColor != paint.computeLuminanceColor()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800194 return true;
195 }
196
joshualitt8e0ef292016-02-19 14:13:03 -0800197 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800198 return true;
199 }
200
Brian Salomon5c6ac642017-12-19 11:09:32 -0500201 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800202 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800203 return true;
204 }
205
206 // We only cache one masked version
207 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400208 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800209 return true;
210 }
211
212 // Similarly, we only cache one version for each style
213 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400214 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
215 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
216 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800217 return true;
218 }
219
220 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
221 // for mixed blobs if this becomes an issue.
222 if (this->hasBitmap() && this->hasDistanceField()) {
223 // Identical viewmatrices and we can reuse in all cases
joshualitt8e0ef292016-02-19 14:13:03 -0800224 if (fInitialViewMatrix.cheapEqualTo(viewMatrix) && x == fInitialX && y == fInitialY) {
joshualittfd5f6c12015-12-10 07:44:50 -0800225 return false;
226 }
227 return true;
228 }
229
230 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800231 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
232 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
233 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
234 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800235 return true;
236 }
237
Herb Derbycb65ce72018-10-17 11:39:41 -0400238 // If the text blob only has full pixel glyphs, then fractional part of the position does
239 // not affect the SkGlyphs used.
240 if (anyRunHasSubpixelPosition) {
241 // We can update the positions in the text blob without regenerating the whole
Herb Derby0edb2142018-10-16 17:04:11 -0400242 // blob, but only for integer translations.
243 // This cool bit of math will determine the necessary translation to apply to the
244 // already generated vertex coordinates to move them to the correct position.
245 SkScalar transX = viewMatrix.getTranslateX() +
246 viewMatrix.getScaleX() * (x - fInitialX) +
247 viewMatrix.getSkewX() * (y - fInitialY) -
248 fInitialViewMatrix.getTranslateX();
249 SkScalar transY = viewMatrix.getTranslateY() +
250 viewMatrix.getSkewY() * (x - fInitialX) +
251 viewMatrix.getScaleY() * (y - fInitialY) -
252 fInitialViewMatrix.getTranslateY();
253 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
254 return true;
255 }
joshualittfd5f6c12015-12-10 07:44:50 -0800256 }
joshualittfd5f6c12015-12-10 07:44:50 -0800257 } else if (this->hasDistanceField()) {
258 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
259 // distance field being generated, so we have to regenerate in those cases
260 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800261 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800262 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
263 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
264 return true;
265 }
joshualittfd5f6c12015-12-10 07:44:50 -0800266 }
267
joshualittfd5f6c12015-12-10 07:44:50 -0800268 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
269 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
270 // the blob anyways at flush time, so no need to regenerate explicitly
271 return false;
272}
273
Herb Derby86240592018-05-24 16:12:31 -0400274inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400275 const Run::SubRunInfo& info, int glyphCount, uint16_t run, uint16_t subRun,
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400276 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
Brian Osmancf860852018-10-31 14:04:39 -0400277 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400278 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
joshualitt2e2202e2015-12-10 11:22:08 -0800279 GrMaskFormat format = info.maskFormat();
joshualitt2e2202e2015-12-10 11:22:08 -0800280
Brian Salomon44acb5b2017-07-18 19:59:24 -0400281 GrPaint grPaint;
Brian Salomonf18b1d82017-10-27 11:30:49 -0400282 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
Brian Salomonf8334782017-01-03 09:42:58 -0500283 std::unique_ptr<GrAtlasTextOp> op;
joshualitt2e2202e2015-12-10 11:22:08 -0800284 if (info.drawAsDistanceFields()) {
Brian Osman34ec3742018-07-03 10:40:57 -0400285 // TODO: Can we be even smarter based on the dest transfer function?
Brian Salomon44acb5b2017-07-18 19:59:24 -0400286 op = GrAtlasTextOp::MakeDistanceField(
Robert Phillips7c525e62018-06-12 10:11:12 -0400287 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400288 target->colorSpaceInfo().isLinearlyBlended(), paint.computeLuminanceColor(),
Ben Wagner4c329562018-04-18 16:04:46 -0400289 props, info.isAntiAliased(), info.hasUseLCDText());
joshualitt2e2202e2015-12-10 11:22:08 -0800290 } else {
Robert Phillips7c525e62018-06-12 10:11:12 -0400291 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400292 info.needsTransform());
joshualitt2e2202e2015-12-10 11:22:08 -0800293 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500294 GrAtlasTextOp::Geometry& geometry = op->geometry();
joshualitt8e0ef292016-02-19 14:13:03 -0800295 geometry.fViewMatrix = viewMatrix;
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400296 geometry.fClipRect = clipRect;
joshualitt2e2202e2015-12-10 11:22:08 -0800297 geometry.fBlob = SkRef(this);
298 geometry.fRun = run;
299 geometry.fSubRun = subRun;
Brian Osmancf860852018-10-31 14:04:39 -0400300 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
joshualitt8e0ef292016-02-19 14:13:03 -0800301 geometry.fX = x;
302 geometry.fY = y;
Brian Salomon09d994e2016-12-21 11:14:46 -0500303 op->init();
Brian Salomonf18b1d82017-10-27 11:30:49 -0400304 return op;
joshualitt2e2202e2015-12-10 11:22:08 -0800305}
306
joshualitt8e0ef292016-02-19 14:13:03 -0800307static void calculate_translation(bool applyVM,
308 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
309 const SkMatrix& currentViewMatrix, SkScalar currentX,
310 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
311 if (applyVM) {
312 *transX = newViewMatrix.getTranslateX() +
313 newViewMatrix.getScaleX() * (newX - currentX) +
314 newViewMatrix.getSkewX() * (newY - currentY) -
315 currentViewMatrix.getTranslateX();
316
317 *transY = newViewMatrix.getTranslateY() +
318 newViewMatrix.getSkewY() * (newX - currentX) +
319 newViewMatrix.getScaleY() * (newY - currentY) -
320 currentViewMatrix.getTranslateY();
321 } else {
322 *transX = newX - currentX;
323 *transY = newY - currentY;
324 }
325}
326
Herb Derbyc1b482c2018-08-09 15:02:27 -0400327void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
328 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400329 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400330 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500331
Herb Derby86240592018-05-24 16:12:31 -0400332 // GrTextBlob::makeOp only takes uint16_t values for run and subRun indices.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500333 // Encountering something larger than this is highly unlikely, so we'll just not draw it.
Herb Derby2bb343c2018-11-08 15:03:48 -0500334 int lastRun = SkTMin(fRunCountLimit, (1 << 16)) - 1;
Herb Derbydac1ed52018-09-12 17:04:21 -0400335 // For each run in the GrTextBlob we're going to churn through all the glyphs.
336 // Each run is broken into a path part and a Mask / DFT / ARGB part.
Jim Van Verth54d9c882018-02-08 16:14:48 -0500337 for (int runIndex = 0; runIndex <= lastRun; runIndex++) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400338
Jim Van Verth54d9c882018-02-08 16:14:48 -0500339 Run& run = fRuns[runIndex];
340
341 // first flush any path glyphs
342 if (run.fPathGlyphs.count()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400343 SkPaint runPaint{paint};
344 runPaint.setFlags((runPaint.getFlags() & ~Run::kPaintFlagsMask) | run.fPaintFlags);
Herb Derby9f491482018-08-08 11:53:00 -0400345
Jim Van Verth54d9c882018-02-08 16:14:48 -0500346 for (int i = 0; i < run.fPathGlyphs.count(); i++) {
Herb Derby86240592018-05-24 16:12:31 -0400347 GrTextBlob::Run::PathGlyph& pathGlyph = run.fPathGlyphs[i];
Robert Phillips137ca522018-08-15 10:14:33 -0400348
Herb Derbydac1ed52018-09-12 17:04:21 -0400349 SkMatrix ctm;
Robert Phillips137ca522018-08-15 10:14:33 -0400350 const SkPath* path = &pathGlyph.fPath;
Herb Derbydac1ed52018-09-12 17:04:21 -0400351
352 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400353 SkTLazy<SkPath> tmpPath;
354
Herb Derbydac1ed52018-09-12 17:04:21 -0400355 // The glyph positions and glyph outlines are either in device space or in source
356 // space based on fPreTransformed.
357 if (!pathGlyph.fPreTransformed) {
358 // Positions and outlines are in source space.
Robert Phillips137ca522018-08-15 10:14:33 -0400359
Herb Derbydac1ed52018-09-12 17:04:21 -0400360 ctm = viewMatrix;
Robert Phillipsd20d2612018-08-28 10:09:01 -0400361
Herb Derbydac1ed52018-09-12 17:04:21 -0400362 SkMatrix pathMatrix = SkMatrix::MakeScale(pathGlyph.fScale, pathGlyph.fScale);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400363
Herb Derbydac1ed52018-09-12 17:04:21 -0400364 // The origin for the blob may have changed, so figure out the delta.
365 SkVector originShift = SkPoint{x, y} - SkPoint{fInitialX, fInitialY};
366
367 // Shift the original glyph location in source space to the position of the new
368 // blob.
369 pathMatrix.postTranslate(originShift.x() + pathGlyph.fX,
370 originShift.y() + pathGlyph.fY);
371
372 // If there are shaders, blurs or styles, the path must be scaled into source
373 // space independently of the CTM. This allows the CTM to be correct for the
374 // different effects.
375 GrStyle style(runPaint);
376 bool scalePath = runPaint.getShader()
377 || style.applies()
378 || runPaint.getMaskFilter();
379 if (!scalePath) {
380 // Scale can be applied to CTM -- no effects.
381
382 ctm.preConcat(pathMatrix);
383 } else {
384 // Scale the outline into source space.
385
386 // Transform the path form the normalized outline to source space. This
387 // way the CTM will remain the same so it can be used by the effects.
388 SkPath* sourceOutline = tmpPath.init();
389 path->transform(pathMatrix, sourceOutline);
390 sourceOutline->setIsVolatile(true);
391 path = sourceOutline;
392 }
393
394
Robert Phillipsd20d2612018-08-28 10:09:01 -0400395 } else {
Herb Derbydac1ed52018-09-12 17:04:21 -0400396 // Positions and outlines are in device space.
397
398 SkPoint originalOrigin = {fInitialX, fInitialY};
399 fInitialViewMatrix.mapPoints(&originalOrigin, 1);
400
401 SkPoint newOrigin = {x, y};
402 viewMatrix.mapPoints(&newOrigin, 1);
403
404 // The origin shift in device space.
405 SkPoint originShift = newOrigin - originalOrigin;
406
407 // Shift the original glyph location in device space to the position of the
408 // new blob.
409 ctm = SkMatrix::MakeTrans(originShift.x() + pathGlyph.fX,
410 originShift.y() + pathGlyph.fY);
Robert Phillips137ca522018-08-15 10:14:33 -0400411 }
412
Robert Phillips46a13382018-08-23 13:53:01 -0400413 // TODO: we are losing the mutability of the path here
414 GrShape shape(*path, paint);
415
Herb Derbydac1ed52018-09-12 17:04:21 -0400416 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500417 }
joshualitt2e2202e2015-12-10 11:22:08 -0800418 }
joshualitt2e2202e2015-12-10 11:22:08 -0800419
Jim Van Verth54d9c882018-02-08 16:14:48 -0500420 // then flush each subrun, if any
421 if (!run.fInitialized) {
Jim Van Verth89737de2018-02-06 21:30:20 +0000422 continue;
423 }
Herb Derbydac1ed52018-09-12 17:04:21 -0400424
Jim Van Verth54d9c882018-02-08 16:14:48 -0500425 int lastSubRun = SkTMin(run.fSubRunInfo.count(), 1 << 16) - 1;
426 for (int subRun = 0; subRun <= lastSubRun; subRun++) {
427 const Run::SubRunInfo& info = run.fSubRunInfo[subRun];
428 int glyphCount = info.glyphCount();
429 if (0 == glyphCount) {
430 continue;
431 }
432
433 bool skipClip = false;
434 bool submitOp = true;
435 SkIRect clipRect = SkIRect::MakeEmpty();
436 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
437 SkRRect clipRRect;
438 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400439 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500440 // and we have an axis-aligned rectangular non-AA clip
Jim Van Verthb515ae72018-05-23 16:44:55 -0400441 if (!info.drawAsDistanceFields() && !info.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500442 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500443 clipRRect.isRect() && GrAA::kNo == aa) {
444 skipClip = true;
445 // We only need to do clipping work if the subrun isn't contained by the clip
446 SkRect subRunBounds;
Jim Van Verth70276912018-06-01 13:46:46 -0400447 this->computeSubRunBounds(&subRunBounds, runIndex, subRun, viewMatrix, x, y,
448 false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500449 if (!clipRRect.getBounds().contains(subRunBounds)) {
450 // If the subrun is completely outside, don't add an op for it
451 if (!clipRRect.getBounds().intersects(subRunBounds)) {
452 submitOp = false;
453 }
454 else {
455 clipRRect.getBounds().round(&clipRect);
456 }
457 }
458 }
459
460 if (submitOp) {
461 auto op = this->makeOp(info, glyphCount, runIndex, subRun, viewMatrix, x, y,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400462 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500463 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500464 if (op) {
465 if (skipClip) {
466 target->addDrawOp(GrNoClip(), std::move(op));
467 }
468 else {
469 target->addDrawOp(clip, std::move(op));
470 }
471 }
472 }
473 }
474
Jim Van Verth89737de2018-02-06 21:30:20 +0000475 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000476}
477
Herb Derby86240592018-05-24 16:12:31 -0400478std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Jim Van Verth56c37142017-10-31 14:44:25 -0400479 int glyphCount, uint16_t run, uint16_t subRun, const SkMatrix& viewMatrix,
Brian Osmancf860852018-10-31 14:04:39 -0400480 SkScalar x, SkScalar y, const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400481 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
Herb Derbyc1b482c2018-08-09 15:02:27 -0400482 GrTextTarget* target) {
Herb Derby86240592018-05-24 16:12:31 -0400483 const GrTextBlob::Run::SubRunInfo& info = fRuns[run].fSubRunInfo[subRun];
Jim Van Verth58c3cce2017-10-19 15:50:24 -0400484 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derbybc6f9c92018-08-08 13:58:45 -0400485 return this->makeOp(info, glyphCount, run, subRun, viewMatrix, x, y, emptyRect,
486 paint, filteredColor, props, distanceAdjustTable, target);
joshualitt323c2eb2016-01-20 06:48:47 -0800487}
joshualitt2e2202e2015-12-10 11:22:08 -0800488
Herb Derby86240592018-05-24 16:12:31 -0400489void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800490 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700491
joshualitt2f2ee832016-02-10 08:52:24 -0800492 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
493 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700494
joshualitt2f2ee832016-02-10 08:52:24 -0800495 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
496 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
497 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700498
joshualitt2f2ee832016-02-10 08:52:24 -0800499 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800500 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
501 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
502 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
503 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700504
Herb Derby2bb343c2018-11-08 15:03:48 -0500505 SkASSERT_RELEASE(l.fRunCountLimit == r.fRunCountLimit);
506 for (int i = 0; i < l.fRunCountLimit; i++) {
joshualitt259fbf12015-07-21 11:39:34 -0700507 const Run& lRun = l.fRuns[i];
508 const Run& rRun = r.fRuns[i];
509
joshualitt259fbf12015-07-21 11:39:34 -0700510 if (lRun.fTypeface.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800511 SkASSERT_RELEASE(rRun.fTypeface.get());
Hal Canary144caf52016-11-07 17:57:18 -0500512 SkASSERT_RELEASE(SkTypeface::Equal(lRun.fTypeface.get(), rRun.fTypeface.get()));
joshualitt259fbf12015-07-21 11:39:34 -0700513 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800514 SkASSERT_RELEASE(!rRun.fTypeface.get());
joshualitt259fbf12015-07-21 11:39:34 -0700515 }
516
joshualitt259fbf12015-07-21 11:39:34 -0700517
joshualitt2f2ee832016-02-10 08:52:24 -0800518 SkASSERT_RELEASE(lRun.fDescriptor.getDesc());
519 SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700520 SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700521
522 if (lRun.fOverrideDescriptor.get()) {
joshualitt2f2ee832016-02-10 08:52:24 -0800523 SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
524 SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
bsalomonc5d07fa2016-05-17 10:17:45 -0700525 SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
526 *rRun.fOverrideDescriptor->getDesc());
joshualitt259fbf12015-07-21 11:39:34 -0700527 } else {
joshualitt2f2ee832016-02-10 08:52:24 -0800528 SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
joshualitt259fbf12015-07-21 11:39:34 -0700529 }
530
531 // color can be changed
532 //SkASSERT(lRun.fColor == rRun.fColor);
joshualitt2f2ee832016-02-10 08:52:24 -0800533 SkASSERT_RELEASE(lRun.fInitialized == rRun.fInitialized);
joshualitt259fbf12015-07-21 11:39:34 -0700534
joshualitt2f2ee832016-02-10 08:52:24 -0800535 SkASSERT_RELEASE(lRun.fSubRunInfo.count() == rRun.fSubRunInfo.count());
joshualitt259fbf12015-07-21 11:39:34 -0700536 for(int j = 0; j < lRun.fSubRunInfo.count(); j++) {
537 const Run::SubRunInfo& lSubRun = lRun.fSubRunInfo[j];
538 const Run::SubRunInfo& rSubRun = rRun.fSubRunInfo[j];
539
joshualitt2f2ee832016-02-10 08:52:24 -0800540 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
541 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700542
joshualitt2f2ee832016-02-10 08:52:24 -0800543 if (lSubRun.strike()) {
544 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500545 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
546 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800547
548 } else {
549 SkASSERT_RELEASE(!rSubRun.strike());
550 }
551
552 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
553 SkASSERT_RELEASE(lSubRun.vertexEndIndex() == rSubRun.vertexEndIndex());
554 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
555 SkASSERT_RELEASE(lSubRun.glyphEndIndex() == rSubRun.glyphEndIndex());
556 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
557 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
558 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
joshualitt259fbf12015-07-21 11:39:34 -0700559 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500560
561 SkASSERT_RELEASE(lRun.fPathGlyphs.count() == rRun.fPathGlyphs.count());
562 for (int i = 0; i < lRun.fPathGlyphs.count(); i++) {
563 const Run::PathGlyph& lPathGlyph = lRun.fPathGlyphs[i];
564 const Run::PathGlyph& rPathGlyph = rRun.fPathGlyphs[i];
565
566 SkASSERT_RELEASE(lPathGlyph.fPath == rPathGlyph.fPath);
567 // We can't assert that these have the same translations
568 }
joshualitt259fbf12015-07-21 11:39:34 -0700569 }
570}
joshualitt8e0ef292016-02-19 14:13:03 -0800571
Herb Derby86240592018-05-24 16:12:31 -0400572void GrTextBlob::Run::SubRunInfo::computeTranslation(const SkMatrix& viewMatrix,
joshualitt8e0ef292016-02-19 14:13:03 -0800573 SkScalar x, SkScalar y, SkScalar* transX,
574 SkScalar* transY) {
575 calculate_translation(!this->drawAsDistanceFields(), viewMatrix, x, y,
576 fCurrentViewMatrix, fX, fY, transX, transY);
577 fCurrentViewMatrix = viewMatrix;
578 fX = x;
579 fY = y;
580}