blob: 1bde8d403ff497cb24a41d261387570552112104 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkColorFilter.h"
9#include "include/gpu/GrContext.h"
10#include "src/core/SkMaskFilterBase.h"
11#include "src/core/SkPaintPriv.h"
12#include "src/gpu/GrBlurUtils.h"
13#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrStyle.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040015#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/ops/GrAtlasTextOp.h"
Herb Derbya9047642019-12-06 12:12:11 -050017#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/text/GrTextBlob.h"
19#include "src/gpu/text/GrTextTarget.h"
Ben Wagner75d6db72018-09-20 14:39:39 -040020
Mike Klein79aea6a2018-06-11 10:45:26 -040021#include <new>
joshualitt2e2202e2015-12-10 11:22:08 -080022
Ben Wagner75d6db72018-09-20 14:39:39 -040023template <size_t N> static size_t sk_align(size_t s) {
24 return ((s + (N-1)) / N) * N;
25}
26
Herb Derbya9047642019-12-06 12:12:11 -050027static void calculate_translation(bool applyVM,
28 const SkMatrix& newViewMatrix, SkScalar newX, SkScalar newY,
29 const SkMatrix& currentViewMatrix, SkScalar currentX,
30 SkScalar currentY, SkScalar* transX, SkScalar* transY) {
31 if (applyVM) {
32 *transX = newViewMatrix.getTranslateX() +
33 newViewMatrix.getScaleX() * (newX - currentX) +
34 newViewMatrix.getSkewX() * (newY - currentY) -
35 currentViewMatrix.getTranslateX();
36
37 *transY = newViewMatrix.getTranslateY() +
38 newViewMatrix.getSkewY() * (newX - currentX) +
39 newViewMatrix.getScaleY() * (newY - currentY) -
40 currentViewMatrix.getTranslateY();
41 } else {
42 *transX = newX - currentX;
43 *transY = newY - currentY;
44 }
45}
46
47static SkMatrix make_inverse(const SkMatrix& matrix) {
48 SkMatrix inverseMatrix;
49 if (!matrix.invert(&inverseMatrix)) {
50 inverseMatrix = SkMatrix::I();
51 }
52 return inverseMatrix;
53}
54
55// -- GrTextBlob::Key ------------------------------------------------------------------------------
56GrTextBlob::Key::Key() { sk_bzero(this, sizeof(Key)); }
57
58bool GrTextBlob::Key::operator==(const GrTextBlob::Key& other) const {
59 return 0 == memcmp(this, &other, sizeof(Key));
60}
61
62// -- GrTextBlob::PathGlyph ------------------------------------------------------------------------
63GrTextBlob::PathGlyph::PathGlyph(const SkPath& path, SkPoint origin)
64 : fPath(path)
65 , fOrigin(origin) {}
66
67// -- GrTextBlob::SubRun ---------------------------------------------------------------------------
68GrTextBlob::SubRun::SubRun(SubRunType type, GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec,
69 GrMaskFormat format, const GrTextBlob::SubRunBufferSpec& bufferSpec,
70 sk_sp<GrTextStrike>&& grStrike)
71 : fType{type}
72 , fBlob{textBlob}
73 , fMaskFormat{format}
74 , fGlyphStartIndex{std::get<0>(bufferSpec)}
75 , fGlyphEndIndex{std::get<1>(bufferSpec)}
76 , fVertexStartIndex{std::get<2>(bufferSpec)}
77 , fVertexEndIndex{std::get<3>(bufferSpec)}
78 , fStrikeSpec{strikeSpec}
79 , fStrike{grStrike}
80 , fColor{textBlob->fColor}
81 , fX{textBlob->fInitialOrigin.x()}
82 , fY{textBlob->fInitialOrigin.y()}
83 , fCurrentViewMatrix{textBlob->fInitialViewMatrix} {
84 SkASSERT(type != kTransformedPath);
85}
86
87GrTextBlob::SubRun::SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec)
88 : fType{kTransformedPath}
89 , fBlob{textBlob}
90 , fMaskFormat{kA8_GrMaskFormat}
91 , fGlyphStartIndex{0}
92 , fGlyphEndIndex{0}
93 , fVertexStartIndex{0}
94 , fVertexEndIndex{0}
95 , fStrikeSpec{strikeSpec}
96 , fStrike{nullptr}
97 , fColor{textBlob->fColor}
98 , fPaths{} { }
99
100void GrTextBlob::SubRun::appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables) {
101 GrTextStrike* grStrike = fStrike.get();
102 SkScalar strikeToSource = fStrikeSpec.strikeToSourceRatio();
103 uint32_t glyphCursor = fGlyphStartIndex;
104 size_t vertexCursor = fVertexStartIndex;
105 bool hasW = this->hasW();
106 GrColor color = this->color();
107 // glyphs drawn in perspective must always have a w coord.
108 SkASSERT(hasW || !fBlob->fInitialViewMatrix.hasPerspective());
109 size_t vertexStride = GetVertexStride(fMaskFormat, hasW);
110 // We always write the third position component used by SDFs. If it is unused it gets
111 // overwritten. Similarly, we always write the color and the blob will later overwrite it
112 // with texture coords if it is unused.
113 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
114 for (auto [variant, pos] : drawables) {
115 SkGlyph* skGlyph = variant;
116 GrGlyph* grGlyph = grStrike->getGlyph(*skGlyph);
117 // Only floor the device coordinates.
118 SkRect dstRect;
119 if (!this->needsTransform()) {
120 pos = {SkScalarFloorToScalar(pos.x()), SkScalarFloorToScalar(pos.y())};
121 dstRect = grGlyph->destRect(pos);
122 } else {
123 dstRect = grGlyph->destRect(pos, strikeToSource);
124 }
125
126 this->joinGlyphBounds(dstRect);
127
128 intptr_t vertex = reinterpret_cast<intptr_t>(fBlob->fVertices + vertexCursor);
129
130 // V0
131 *reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fLeft, dstRect.fTop, 1.f};
132 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
133 vertex += vertexStride;
134
135 // V1
136 *reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fLeft, dstRect.fBottom, 1.f};
137 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
138 vertex += vertexStride;
139
140 // V2
141 *reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fRight, dstRect.fTop, 1.f};
142 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
143 vertex += vertexStride;
144
145 // V3
146 *reinterpret_cast<SkPoint3*>(vertex) = {dstRect.fRight, dstRect.fBottom, 1.f};
147 *reinterpret_cast<GrColor*>(vertex + colorOffset) = color;
148
149 vertexCursor += vertexStride * kVerticesPerGlyph;
150 fBlob->fGlyphs[glyphCursor++] = grGlyph;
151 }
152 SkASSERT(glyphCursor == fGlyphEndIndex);
153 SkASSERT(vertexCursor == fVertexEndIndex);
154}
155
156void GrTextBlob::SubRun::resetBulkUseToken() { fBulkUseToken.reset(); }
157
158GrDrawOpAtlas::BulkUseTokenUpdater* GrTextBlob::SubRun::bulkUseToken() { return &fBulkUseToken; }
159void GrTextBlob::SubRun::setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
160GrTextStrike* GrTextBlob::SubRun::strike() const { return fStrike.get(); }
161sk_sp<GrTextStrike> GrTextBlob::SubRun::refStrike() const { return fStrike; }
162void GrTextBlob::SubRun::setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
163uint64_t GrTextBlob::SubRun::atlasGeneration() const { return fAtlasGeneration; }
164size_t GrTextBlob::SubRun::vertexStartIndex() const { return fVertexStartIndex; }
165uint32_t GrTextBlob::SubRun::glyphCount() const { return fGlyphEndIndex - fGlyphStartIndex; }
166uint32_t GrTextBlob::SubRun::glyphStartIndex() const { return fGlyphStartIndex; }
167void GrTextBlob::SubRun::setColor(GrColor color) { fColor = color; }
168GrColor GrTextBlob::SubRun::color() const { return fColor; }
169GrMaskFormat GrTextBlob::SubRun::maskFormat() const { return fMaskFormat; }
170const SkRect& GrTextBlob::SubRun::vertexBounds() const { return fVertexBounds; }
171void GrTextBlob::SubRun::joinGlyphBounds(const SkRect& glyphBounds) {
172 fVertexBounds.joinNonEmptyArg(glyphBounds);
173}
174
175void GrTextBlob::SubRun::init(const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
176 fCurrentViewMatrix = viewMatrix;
177 fX = x;
178 fY = y;
179}
180
181void GrTextBlob::SubRun::computeTranslation(const SkMatrix& viewMatrix,
182 SkScalar x, SkScalar y, SkScalar* transX,
183 SkScalar* transY) {
184 // Don't use the matrix to translate on distance field for fallback subruns.
185 calculate_translation(!this->drawAsDistanceFields() && !this->needsTransform(), viewMatrix,
186 x, y, fCurrentViewMatrix, fX, fY, transX, transY);
187 fCurrentViewMatrix = viewMatrix;
188 fX = x;
189 fY = y;
190}
191
192bool GrTextBlob::SubRun::drawAsDistanceFields() const { return fType == kTransformedSDFT; }
193
194bool GrTextBlob::SubRun::drawAsPaths() const { return fType == kTransformedPath; }
195
196bool GrTextBlob::SubRun::needsTransform() const {
197 return fType == kTransformedPath
198 || fType == kTransformedMask
199 || fType == kTransformedSDFT;
200}
201
202bool GrTextBlob::SubRun::hasW() const {
203 return fBlob->hasW(fType);
204}
205
206void GrTextBlob::SubRun::setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
207bool GrTextBlob::SubRun::hasUseLCDText() const { return fFlags.useLCDText; }
208void GrTextBlob::SubRun::setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
209bool GrTextBlob::SubRun::isAntiAliased() const { return fFlags.antiAliased; }
210const SkStrikeSpec& GrTextBlob::SubRun::strikeSpec() const { return fStrikeSpec; }
211
212// -- GrTextBlob -----------------------------------------------------------------------------------
213void GrTextBlob::operator delete(void* p) { ::operator delete(p); }
214void* GrTextBlob::operator new(size_t) { SK_ABORT("All blobs are created by placement new."); }
215void* GrTextBlob::operator new(size_t, void* p) { return p; }
216
217GrTextBlob::~GrTextBlob() = default;
218
Herb Derbya00da612019-03-04 17:10:01 -0500219sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount,
Herb Derbyeba195f2019-12-03 16:44:47 -0500220 GrStrikeCache* strikeCache,
Herb Derbye9f691d2019-12-04 12:11:13 -0500221 const SkMatrix& viewMatrix,
Herb Derbyeba195f2019-12-03 16:44:47 -0500222 SkPoint origin,
Herb Derbya00da612019-03-04 17:10:01 -0500223 GrColor color,
Herb Derbyeba195f2019-12-03 16:44:47 -0500224 bool forceWForDistanceFields) {
Herb Derbye74c7762019-12-04 14:15:41 -0500225 // Default to no perspective. Implies one of the following vertex formats: kColorTextVASize,
226 // kGrayTextVASize, kLCDTextVASize.
227 static_assert(kColorTextVASize <= kGrayTextVASize && kLCDTextVASize <= kGrayTextVASize);
228 size_t quadSize = kVerticesPerGlyph * kGrayTextVASize;
229 if (viewMatrix.hasPerspective() || forceWForDistanceFields) {
230 // Perspective. Implies one of the following vertex formats: kColorTextPerspectiveVASize,
231 // kGrayTextDFPerspectiveVASize.
232 static_assert(kColorTextPerspectiveVASize <= kGrayTextDFPerspectiveVASize);
233 quadSize = kVerticesPerGlyph * kGrayTextDFPerspectiveVASize;
234 }
235
Herb Derby86240592018-05-24 16:12:31 -0400236 // We allocate size for the GrTextBlob itself, plus size for the vertices array,
joshualitt92303772016-02-10 11:55:52 -0800237 // and size for the glyphIds array.
Herb Derbye74c7762019-12-04 14:15:41 -0500238 size_t verticesCount = glyphCount * quadSize;
Ben Wagner75d6db72018-09-20 14:39:39 -0400239
Herb Derbyf7d5d742018-11-16 13:24:32 -0500240 size_t blobStart = 0;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500241 size_t vertex = sk_align<alignof(char)> (blobStart + sizeof(GrTextBlob) * 1);
242 size_t glyphs = sk_align<alignof(GrGlyph*)> (vertex + sizeof(char) * verticesCount);
243 size_t size = (glyphs + sizeof(GrGlyph*) * glyphCount);
joshualitt92303772016-02-10 11:55:52 -0800244
Herb Derbyb12175f2018-05-23 16:38:09 -0400245 void* allocation = ::operator new (size);
246
joshualitt92303772016-02-10 11:55:52 -0800247 if (CACHE_SANITY_CHECK) {
248 sk_bzero(allocation, size);
249 }
250
Herb Derby00ae9592019-12-03 15:55:56 -0500251 sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{
Herb Derbye9f691d2019-12-04 12:11:13 -0500252 size, strikeCache, viewMatrix, origin, color, forceWForDistanceFields}};
joshualitt92303772016-02-10 11:55:52 -0800253
254 // setup offsets for vertices / glyphs
Herb Derbyf7d5d742018-11-16 13:24:32 -0500255 blob->fVertices = SkTAddOffset<char>(blob.get(), vertex);
Herb Derby1b8dcd12019-11-15 15:21:15 -0500256 blob->fGlyphs = SkTAddOffset<GrGlyph*>(blob.get(), glyphs);
joshualitt92303772016-02-10 11:55:52 -0800257
Herb Derbyf7d5d742018-11-16 13:24:32 -0500258 return blob;
joshualitt92303772016-02-10 11:55:52 -0800259}
260
Herb Derbya9047642019-12-06 12:12:11 -0500261void GrTextBlob::generateFromGlyphRunList(const GrShaderCaps& shaderCaps,
262 const GrTextContext::Options& options,
263 const SkPaint& paint,
264 const SkMatrix& viewMatrix,
265 const SkSurfaceProps& props,
266 const SkGlyphRunList& glyphRunList,
267 SkGlyphRunListPainter* glyphPainter) {
268 const SkPaint& runPaint = glyphRunList.paint();
269 this->initReusableBlob(SkPaintPriv::ComputeLuminanceColor(runPaint));
270
271 glyphPainter->processGlyphRunList(glyphRunList,
272 viewMatrix,
273 props,
274 shaderCaps.supportsDistanceFieldText(),
275 options,
276 this);
277}
278
279void GrTextBlob::setupKey(const GrTextBlob::Key& key, const SkMaskFilterBase::BlurRec& blurRec,
280 const SkPaint& paint) {
281 fKey = key;
282 if (key.fHasBlur) {
283 fBlurRec = blurRec;
284 }
285 if (key.fStyle != SkPaint::kFill_Style) {
286 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
287 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
288 fStrokeInfo.fJoin = paint.getStrokeJoin();
289 }
290}
291const GrTextBlob::Key& GrTextBlob::GetKey(const GrTextBlob& blob) { return blob.fKey; }
292uint32_t GrTextBlob::Hash(const GrTextBlob::Key& key) { return SkOpts::hash(&key, sizeof(Key)); }
293
294bool GrTextBlob::hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
295bool GrTextBlob::hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
296bool GrTextBlob::hasPerspective() const { return fInitialViewMatrix.hasPerspective(); }
297
298void GrTextBlob::setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
299void GrTextBlob::setHasBitmap() { fTextType |= kHasBitmap_TextType; }
300void GrTextBlob::setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) {
301 // we init fMaxMinScale and fMinMaxScale in the constructor
302 fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale);
303 fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale);
304}
305
306size_t GrTextBlob::GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
307 switch (maskFormat) {
308 case kA8_GrMaskFormat:
309 return hasWCoord ? kGrayTextDFPerspectiveVASize : kGrayTextVASize;
310 case kARGB_GrMaskFormat:
311 return hasWCoord ? kColorTextPerspectiveVASize : kColorTextVASize;
312 default:
313 SkASSERT(!hasWCoord);
314 return kLCDTextVASize;
315 }
316}
317
Herb Derby0edb2142018-10-16 17:04:11 -0400318bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
319 const SkMaskFilterBase::BlurRec& blurRec,
320 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittfd5f6c12015-12-10 07:44:50 -0800321 // If we have LCD text then our canonical color will be set to transparent, in this case we have
322 // to regenerate the blob on any color change
323 // We use the grPaint to get any color filter effects
324 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Mike Reedec7278e2019-02-01 14:00:34 -0500325 fLuminanceColor != SkPaintPriv::ComputeLuminanceColor(paint)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800326 return true;
327 }
328
joshualitt8e0ef292016-02-19 14:13:03 -0800329 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800330 return true;
331 }
332
Brian Salomon5c6ac642017-12-19 11:09:32 -0500333 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800334 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800335 return true;
336 }
337
338 // We only cache one masked version
339 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400340 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800341 return true;
342 }
343
344 // Similarly, we only cache one version for each style
345 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400346 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
347 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
348 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800349 return true;
350 }
351
352 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
353 // for mixed blobs if this becomes an issue.
354 if (this->hasBitmap() && this->hasDistanceField()) {
Herb Derbyeba195f2019-12-03 16:44:47 -0500355 // Identical view matrices and we can reuse in all cases
356 return !(fInitialViewMatrix.cheapEqualTo(viewMatrix) && SkPoint{x, y} == fInitialOrigin);
joshualittfd5f6c12015-12-10 07:44:50 -0800357 }
358
359 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800360 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
361 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
362 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
363 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800364 return true;
365 }
366
Herb Derby9830fc42019-09-12 10:58:26 -0400367 // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust
368 // the quads properly. Devise a system that regenerates the quads from original data
369 // using the transform to allow this to be used in general.
370
371 // We can update the positions in the text blob without regenerating the whole
372 // blob, but only for integer translations.
373 // This cool bit of math will determine the necessary translation to apply to the
374 // already generated vertex coordinates to move them to the correct position.
375 // Figure out the translation in view space given a translation in source space.
376 SkScalar transX = viewMatrix.getTranslateX() +
Herb Derbyeba195f2019-12-03 16:44:47 -0500377 viewMatrix.getScaleX() * (x - fInitialOrigin.x()) +
378 viewMatrix.getSkewX() * (y - fInitialOrigin.y()) -
Herb Derby9830fc42019-09-12 10:58:26 -0400379 fInitialViewMatrix.getTranslateX();
380 SkScalar transY = viewMatrix.getTranslateY() +
Herb Derbyeba195f2019-12-03 16:44:47 -0500381 viewMatrix.getSkewY() * (x - fInitialOrigin.x()) +
382 viewMatrix.getScaleY() * (y - fInitialOrigin.y()) -
Herb Derby9830fc42019-09-12 10:58:26 -0400383 fInitialViewMatrix.getTranslateY();
384 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
385 return true;
joshualittfd5f6c12015-12-10 07:44:50 -0800386 }
joshualittfd5f6c12015-12-10 07:44:50 -0800387 } else if (this->hasDistanceField()) {
388 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
389 // distance field being generated, so we have to regenerate in those cases
390 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800391 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800392 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
393 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
394 return true;
395 }
joshualittfd5f6c12015-12-10 07:44:50 -0800396 }
397
joshualittfd5f6c12015-12-10 07:44:50 -0800398 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
399 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
400 // the blob anyways at flush time, so no need to regenerate explicitly
401 return false;
402}
403
Herb Derbyc1b482c2018-08-09 15:02:27 -0400404void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
405 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400406 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400407 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500408
Herb Derby1b8dcd12019-11-15 15:21:15 -0500409 for (auto& subRun : fSubRuns) {
410 if (subRun.drawAsPaths()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400411 SkPaint runPaint{paint};
Herb Derby1b8dcd12019-11-15 15:21:15 -0500412 runPaint.setAntiAlias(subRun.isAntiAliased());
413 // If there are shaders, blurs or styles, the path must be scaled into source
414 // space independently of the CTM. This allows the CTM to be correct for the
415 // different effects.
416 GrStyle style(runPaint);
Herb Derby9f491482018-08-08 11:53:00 -0400417
Herb Derby1b8dcd12019-11-15 15:21:15 -0500418 bool scalePath = runPaint.getShader()
419 || style.applies()
420 || runPaint.getMaskFilter();
Robert Phillips137ca522018-08-15 10:14:33 -0400421
Herb Derby1b8dcd12019-11-15 15:21:15 -0500422 // The origin for the blob may have changed, so figure out the delta.
Herb Derbyeba195f2019-12-03 16:44:47 -0500423 SkVector originShift = SkPoint{x, y} - fInitialOrigin;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500424
425 for (const auto& pathGlyph : subRun.fPaths) {
426 SkMatrix ctm{viewMatrix};
427 SkMatrix pathMatrix = SkMatrix::MakeScale(subRun.fStrikeSpec.strikeToSourceRatio());
428 // Shift the original glyph location in source space to the position of the new
429 // blob.
430 pathMatrix.postTranslate(originShift.x() + pathGlyph.fOrigin.x(),
431 originShift.y() + pathGlyph.fOrigin.y());
Herb Derbydac1ed52018-09-12 17:04:21 -0400432
433 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400434 SkTLazy<SkPath> tmpPath;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500435 const SkPath* path = &pathGlyph.fPath;
Herb Derby2984d262019-11-20 14:40:39 -0500436 if (!scalePath) {
437 // Scale can be applied to CTM -- no effects.
Herb Derby2984d262019-11-20 14:40:39 -0500438 ctm.preConcat(pathMatrix);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400439 } else {
Herb Derby2984d262019-11-20 14:40:39 -0500440 // Scale the outline into source space.
Herb Derbydac1ed52018-09-12 17:04:21 -0400441
Herb Derby2984d262019-11-20 14:40:39 -0500442 // Transform the path form the normalized outline to source space. This
443 // way the CTM will remain the same so it can be used by the effects.
444 SkPath* sourceOutline = tmpPath.init();
445 path->transform(pathMatrix, sourceOutline);
446 sourceOutline->setIsVolatile(true);
447 path = sourceOutline;
Robert Phillips137ca522018-08-15 10:14:33 -0400448 }
449
Robert Phillips46a13382018-08-23 13:53:01 -0400450 // TODO: we are losing the mutability of the path here
451 GrShape shape(*path, paint);
Herb Derbydac1ed52018-09-12 17:04:21 -0400452 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500453 }
Herb Derby1b8dcd12019-11-15 15:21:15 -0500454 } else {
455 int glyphCount = subRun.glyphCount();
Jim Van Verth54d9c882018-02-08 16:14:48 -0500456 if (0 == glyphCount) {
457 continue;
458 }
459
460 bool skipClip = false;
461 bool submitOp = true;
462 SkIRect clipRect = SkIRect::MakeEmpty();
463 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
464 SkRRect clipRRect;
465 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400466 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500467 // and we have an axis-aligned rectangular non-AA clip
Herb Derby1b8dcd12019-11-15 15:21:15 -0500468 if (!subRun.drawAsDistanceFields() && !subRun.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500469 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500470 clipRRect.isRect() && GrAA::kNo == aa) {
471 skipClip = true;
472 // We only need to do clipping work if the subrun isn't contained by the clip
473 SkRect subRunBounds;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500474 this->computeSubRunBounds(&subRunBounds, subRun, viewMatrix, x, y, false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500475 if (!clipRRect.getBounds().contains(subRunBounds)) {
476 // If the subrun is completely outside, don't add an op for it
477 if (!clipRRect.getBounds().intersects(subRunBounds)) {
478 submitOp = false;
479 }
480 else {
481 clipRRect.getBounds().round(&clipRect);
482 }
483 }
484 }
485
486 if (submitOp) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500487 auto op = this->makeOp(subRun, glyphCount, viewMatrix, x, y,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400488 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500489 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500490 if (op) {
491 if (skipClip) {
492 target->addDrawOp(GrNoClip(), std::move(op));
493 }
494 else {
495 target->addDrawOp(clip, std::move(op));
496 }
497 }
498 }
499 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000500 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000501}
502
Herb Derbya9047642019-12-06 12:12:11 -0500503void GrTextBlob::computeSubRunBounds(SkRect* outBounds, const GrTextBlob::SubRun& subRun,
504 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
505 bool needsGlyphTransform) {
506 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
507 // into device space.
508 // We handle vertex bounds differently for distance field text and bitmap text because
509 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
510 // from one blob then we are going to pay the price here of mapping the rect for each run.
511 *outBounds = subRun.vertexBounds();
512 if (needsGlyphTransform) {
513 // Distance field text is positioned with the (X,Y) as part of the glyph position,
514 // and currently the view matrix is applied on the GPU
515 outBounds->offset(SkPoint{x, y} - fInitialOrigin);
516 viewMatrix.mapRect(outBounds);
517 } else {
518 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
519 // device space.
520 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
521
522 boundsMatrix.postTranslate(-fInitialOrigin.x(), -fInitialOrigin.y());
523
524 boundsMatrix.postTranslate(x, y);
525
526 boundsMatrix.postConcat(viewMatrix);
527 boundsMatrix.mapRect(outBounds);
528
529 // Due to floating point numerical inaccuracies, we have to round out here
530 outBounds->roundOut(outBounds);
531 }
joshualitt323c2eb2016-01-20 06:48:47 -0800532}
joshualitt2e2202e2015-12-10 11:22:08 -0800533
Herb Derby86240592018-05-24 16:12:31 -0400534void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800535 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700536
joshualitt2f2ee832016-02-10 08:52:24 -0800537 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
538 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700539
joshualitt2f2ee832016-02-10 08:52:24 -0800540 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
541 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
542 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700543
joshualitt2f2ee832016-02-10 08:52:24 -0800544 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800545 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
546 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
547 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
548 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700549
Herb Derby1b8dcd12019-11-15 15:21:15 -0500550 for(auto t : SkMakeZip(l.fSubRuns, r.fSubRuns)) {
551 const SubRun& lSubRun = std::get<0>(t);
552 const SubRun& rSubRun = std::get<1>(t);
553 SkASSERT(lSubRun.drawAsPaths() == rSubRun.drawAsPaths());
554 if (!lSubRun.drawAsPaths()) {
joshualitt259fbf12015-07-21 11:39:34 -0700555
joshualitt2f2ee832016-02-10 08:52:24 -0800556 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
557 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700558
joshualitt2f2ee832016-02-10 08:52:24 -0800559 if (lSubRun.strike()) {
560 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500561 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
562 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800563
564 } else {
565 SkASSERT_RELEASE(!rSubRun.strike());
566 }
567
568 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
joshualitt2f2ee832016-02-10 08:52:24 -0800569 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
joshualitt2f2ee832016-02-10 08:52:24 -0800570 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
571 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
572 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500573 } else {
574 SkASSERT_RELEASE(lSubRun.fPaths.size() == rSubRun.fPaths.size());
575 for(auto p : SkMakeZip(lSubRun.fPaths, rSubRun.fPaths)) {
576 const PathGlyph& lPath = std::get<0>(p);
577 const PathGlyph& rPath = std::get<1>(p);
578 SkASSERT_RELEASE(lPath.fPath == rPath.fPath);
579 // We can't assert that these have the same translations
580 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500581 }
joshualitt259fbf12015-07-21 11:39:34 -0700582 }
583}
joshualitt8e0ef292016-02-19 14:13:03 -0800584
Herb Derbya9047642019-12-06 12:12:11 -0500585void GrTextBlob::initReusableBlob(SkColor luminanceColor) {
586 fLuminanceColor = luminanceColor;
587}
588
589const GrTextBlob::Key& GrTextBlob::key() const { return fKey; }
590size_t GrTextBlob::size() const { return fSize; }
591
592std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
593 int glyphCount, const SkMatrix& viewMatrix,
594 SkScalar x, SkScalar y, const SkPaint& paint, const SkPMColor4f& filteredColor,
595 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
596 GrTextTarget* target) {
597 GrTextBlob::SubRun& info = fSubRuns[0];
598 SkIRect emptyRect = SkIRect::MakeEmpty();
599 return this->makeOp(info, glyphCount, viewMatrix, x, y, emptyRect,
600 paint, filteredColor, props, distanceAdjustTable, target);
601}
602
603bool GrTextBlob::hasW(GrTextBlob::SubRunType type) const {
604 if (type == kTransformedSDFT) {
605 return this->hasPerspective() || fForceWForDistanceFields;
606 } else if (type == kTransformedMask || type == kTransformedPath) {
607 return this->hasPerspective();
Herb Derbye9f691d2019-12-04 12:11:13 -0500608 }
Herb Derbya9047642019-12-06 12:12:11 -0500609
610 // The viewMatrix is implicitly SkMatrix::I when drawing kDirectMask, because it is not
611 // used.
612 return false;
613}
614
615GrTextBlob::SubRun* GrTextBlob::makeSubRun(SubRunType type,
616 const SkZip<SkGlyphVariant, SkPoint>& drawables,
617 const SkStrikeSpec& strikeSpec,
618 GrMaskFormat format) {
619 bool hasW = this->hasW(type);
620 uint32_t glyphsStart = fGlyphsCursor;
621 fGlyphsCursor += drawables.size();
622 uint32_t glyphsEnd = fGlyphsCursor;
623 size_t verticesStart = fVerticesCursor;
624 fVerticesCursor += drawables.size() * GetVertexStride(format, hasW) * kVerticesPerGlyph;
625 size_t verticesEnd = fVerticesCursor;
626
627 SubRunBufferSpec bufferSpec = std::make_tuple(
628 glyphsStart, glyphsEnd, verticesStart, verticesEnd);
629
630 sk_sp<GrTextStrike> grStrike = strikeSpec.findOrCreateGrStrike(fStrikeCache);
631
632 SubRun& subRun = fSubRuns.emplace_back(
633 type, this, strikeSpec, format, bufferSpec, std::move(grStrike));
634
635 subRun.appendGlyphs(drawables);
636
637 return &subRun;
638}
639
640void GrTextBlob::addSingleMaskFormat(
641 SubRunType type,
642 const SkZip<SkGlyphVariant, SkPoint>& drawables,
643 const SkStrikeSpec& strikeSpec,
644 GrMaskFormat format) {
645 this->makeSubRun(type, drawables, strikeSpec, format);
646}
647
648void GrTextBlob::addMultiMaskFormat(
649 SubRunType type,
650 const SkZip<SkGlyphVariant, SkPoint>& drawables,
651 const SkStrikeSpec& strikeSpec) {
652 this->setHasBitmap();
653 if (drawables.empty()) { return; }
654
655 auto glyphSpan = drawables.get<0>();
656 SkGlyph* glyph = glyphSpan[0];
657 GrMaskFormat format = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
658 size_t startIndex = 0;
659 for (size_t i = 1; i < drawables.size(); i++) {
660 glyph = glyphSpan[i];
661 GrMaskFormat nextFormat = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
662 if (format != nextFormat) {
663 auto sameFormat = drawables.subspan(startIndex, i - startIndex);
664 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
665 format = nextFormat;
666 startIndex = i;
667 }
668 }
669 auto sameFormat = drawables.last(drawables.size() - startIndex);
670 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
671}
672
673void GrTextBlob::addSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
674 const SkStrikeSpec& strikeSpec,
675 const SkFont& runFont,
676 SkScalar minScale,
677 SkScalar maxScale) {
678 this->setHasDistanceField();
679 this->setMinAndMaxScale(minScale, maxScale);
680
681 SubRun* subRun = this->makeSubRun(kTransformedSDFT, drawables, strikeSpec, kA8_GrMaskFormat);
682 subRun->setUseLCDText(runFont.getEdging() == SkFont::Edging::kSubpixelAntiAlias);
683 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbye9f691d2019-12-04 12:11:13 -0500684}
685
Herb Derbyeba195f2019-12-03 16:44:47 -0500686GrTextBlob::GrTextBlob(size_t size,
687 GrStrikeCache* strikeCache,
Herb Derbye9f691d2019-12-04 12:11:13 -0500688 const SkMatrix& viewMatrix,
Herb Derbyeba195f2019-12-03 16:44:47 -0500689 SkPoint origin,
690 GrColor color,
Herb Derby00ae9592019-12-03 15:55:56 -0500691 bool forceWForDistanceFields)
692 : fSize{size}
693 , fStrikeCache{strikeCache}
Herb Derbye9f691d2019-12-04 12:11:13 -0500694 , fInitialViewMatrix{viewMatrix}
695 , fInitialViewMatrixInverse{make_inverse(viewMatrix)}
Herb Derbyeba195f2019-12-03 16:44:47 -0500696 , fInitialOrigin{origin}
Herb Derby00ae9592019-12-03 15:55:56 -0500697 , fForceWForDistanceFields{forceWForDistanceFields}
698 , fColor{color} { }
699
Herb Derbya9047642019-12-06 12:12:11 -0500700inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
701 SubRun& info, int glyphCount,
702 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
703 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
704 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
705 GrMaskFormat format = info.maskFormat();
706
707 GrPaint grPaint;
708 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
709 std::unique_ptr<GrAtlasTextOp> op;
710 if (info.drawAsDistanceFields()) {
711 // TODO: Can we be even smarter based on the dest transfer function?
712 op = GrAtlasTextOp::MakeDistanceField(
713 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
714 target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint),
715 props, info.isAntiAliased(), info.hasUseLCDText());
Herb Derbyeba195f2019-12-03 16:44:47 -0500716 } else {
Herb Derbya9047642019-12-06 12:12:11 -0500717 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
718 info.needsTransform());
719 }
720 GrAtlasTextOp::Geometry& geometry = op->geometry();
721 geometry.fViewMatrix = viewMatrix;
722 geometry.fClipRect = clipRect;
723 geometry.fBlob = SkRef(this);
724 geometry.fSubRunPtr = &info;
725 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
726 geometry.fX = x;
727 geometry.fY = y;
728 op->init();
729 return op;
730}
Herb Derbyeba195f2019-12-03 16:44:47 -0500731
Herb Derbya9047642019-12-06 12:12:11 -0500732void GrTextBlob::processDeviceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
733 const SkStrikeSpec& strikeSpec) {
734 this->addMultiMaskFormat(kDirectMask, drawables, strikeSpec);
735}
Herb Derbyeba195f2019-12-03 16:44:47 -0500736
Herb Derbya9047642019-12-06 12:12:11 -0500737void GrTextBlob::processSourcePaths(const SkZip<SkGlyphVariant, SkPoint>& drawables,
738 const SkFont& runFont,
739 const SkStrikeSpec& strikeSpec) {
740 this->setHasBitmap();
741 SubRun& subRun = fSubRuns.emplace_back(this, strikeSpec);
742 subRun.setAntiAliased(runFont.hasSomeAntiAliasing());
743 for (auto [variant, pos] : drawables) {
744 subRun.fPaths.emplace_back(*variant.path(), pos);
Herb Derbyeba195f2019-12-03 16:44:47 -0500745 }
746}
747
Herb Derbya9047642019-12-06 12:12:11 -0500748void GrTextBlob::processSourceSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
749 const SkStrikeSpec& strikeSpec,
750 const SkFont& runFont,
751 SkScalar minScale,
752 SkScalar maxScale) {
753 this->addSDFT(drawables, strikeSpec, runFont, minScale, maxScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800754}
Herb Derbya9047642019-12-06 12:12:11 -0500755
756void GrTextBlob::processSourceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
757 const SkStrikeSpec& strikeSpec) {
758 this->addMultiMaskFormat(kTransformedMask, drawables, strikeSpec);
759}
760
761// -- GrTextBlob::VertexRegenerator ----------------------------------------------------------------
762enum RegenMask {
763 kNoRegen = 0x0,
764 kRegenPos = 0x1,
765 kRegenCol = 0x2,
766 kRegenTex = 0x4,
767 kRegenGlyph = 0x8,
768};
769
770static void regen_positions(char* vertex, size_t vertexStride, SkScalar transX, SkScalar transY) {
771 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
772 for (int i = 0; i < 4; ++i) {
773 point->fX += transX;
774 point->fY += transY;
775 point = SkTAddOffset<SkPoint>(point, vertexStride);
776 }
777}
778
779static void regen_colors(char* vertex, size_t vertexStride, GrColor color) {
780 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
781 // vertices, hence vertexStride - sizeof(SkIPoint16)
782 size_t colorOffset = vertexStride - sizeof(SkIPoint16) - sizeof(GrColor);
783 GrColor* vcolor = reinterpret_cast<GrColor*>(vertex + colorOffset);
784 for (int i = 0; i < 4; ++i) {
785 *vcolor = color;
786 vcolor = SkTAddOffset<GrColor>(vcolor, vertexStride);
787 }
788}
789
790static void regen_texcoords(char* vertex, size_t vertexStride, const GrGlyph* glyph,
791 bool useDistanceFields) {
792 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
793 // vertices, hence vertexStride - sizeof(SkIPoint16)
794 size_t texCoordOffset = vertexStride - sizeof(SkIPoint16);
795
796 uint16_t u0, v0, u1, v1;
797 SkASSERT(glyph);
798 int width = glyph->fBounds.width();
799 int height = glyph->fBounds.height();
800
801 if (useDistanceFields) {
802 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
803 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
804 u1 = u0 + width - 2 * SK_DistanceFieldInset;
805 v1 = v0 + height - 2 * SK_DistanceFieldInset;
806 } else {
807 u0 = glyph->fAtlasLocation.fX;
808 v0 = glyph->fAtlasLocation.fY;
809 u1 = u0 + width;
810 v1 = v0 + height;
811 }
812 // We pack the 2bit page index in the low bit of the u and v texture coords
813 uint32_t pageIndex = glyph->pageIndex();
814 SkASSERT(pageIndex < 4);
815 uint16_t uBit = (pageIndex >> 1) & 0x1;
816 uint16_t vBit = pageIndex & 0x1;
817 u0 <<= 1;
818 u0 |= uBit;
819 v0 <<= 1;
820 v0 |= vBit;
821 u1 <<= 1;
822 u1 |= uBit;
823 v1 <<= 1;
824 v1 |= vBit;
825
826 uint16_t* textureCoords = reinterpret_cast<uint16_t*>(vertex + texCoordOffset);
827 textureCoords[0] = u0;
828 textureCoords[1] = v0;
829 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
830 textureCoords[0] = u0;
831 textureCoords[1] = v1;
832 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
833 textureCoords[0] = u1;
834 textureCoords[1] = v0;
835 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
836 textureCoords[0] = u1;
837 textureCoords[1] = v1;
838
839#ifdef DISPLAY_PAGE_INDEX
840 // Enable this to visualize the page from which each glyph is being drawn.
841 // Green Red Magenta Cyan -> 0 1 2 3; Black -> error
842 GrColor hackColor;
843 switch (pageIndex) {
844 case 0:
845 hackColor = GrColorPackRGBA(0, 255, 0, 255);
846 break;
847 case 1:
848 hackColor = GrColorPackRGBA(255, 0, 0, 255);;
849 break;
850 case 2:
851 hackColor = GrColorPackRGBA(255, 0, 255, 255);
852 break;
853 case 3:
854 hackColor = GrColorPackRGBA(0, 255, 255, 255);
855 break;
856 default:
857 hackColor = GrColorPackRGBA(0, 0, 0, 255);
858 break;
859 }
860 regen_colors(vertex, vertexStride, hackColor);
861#endif
862}
863
864GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
865 GrTextBlob* blob,
866 GrTextBlob::SubRun* subRun,
867 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
868 GrColor color,
869 GrDeferredUploadTarget* uploadTarget,
870 GrStrikeCache* grStrikeCache,
871 GrAtlasManager* fullAtlasManager)
872 : fResourceProvider(resourceProvider)
873 , fViewMatrix(viewMatrix)
874 , fBlob(blob)
875 , fUploadTarget(uploadTarget)
876 , fGrStrikeCache(grStrikeCache)
877 , fFullAtlasManager(fullAtlasManager)
878 , fSubRun(subRun)
879 , fColor(color) {
880 // Compute translation if any
881 fSubRun->computeTranslation(fViewMatrix, x, y, &fTransX, &fTransY);
882
883 // Because the GrStrikeCache may evict the strike a blob depends on using for
884 // generating its texture coords, we have to track whether or not the strike has
885 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
886 // otherwise we have to get the new strike, and use that to get the correct glyphs.
887 // Because we do not have the packed ids, and thus can't look up our glyphs in the
888 // new strike, we instead keep our ref to the old strike and use the packed ids from
889 // it. These ids will still be valid as long as we hold the ref. When we are done
890 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
891 if (fSubRun->strike()->isAbandoned()) {
892 fRegenFlags |= kRegenGlyph;
893 fRegenFlags |= kRegenTex;
894 }
895 if (kARGB_GrMaskFormat != fSubRun->maskFormat() && fSubRun->color() != color) {
896 fRegenFlags |= kRegenCol;
897 }
898 if (0.f != fTransX || 0.f != fTransY) {
899 fRegenFlags |= kRegenPos;
900 }
901}
902
903bool GrTextBlob::VertexRegenerator::doRegen(GrTextBlob::VertexRegenerator::Result* result,
904 bool regenPos, bool regenCol, bool regenTexCoords,
905 bool regenGlyphs) {
906 SkASSERT(!regenGlyphs || regenTexCoords);
907 if (regenTexCoords) {
908 fSubRun->resetBulkUseToken();
909
910 const SkStrikeSpec& strikeSpec = fSubRun->strikeSpec();
911
912 if (!fMetricsAndImages.isValid()
913 || fMetricsAndImages->descriptor() != strikeSpec.descriptor()) {
914 fMetricsAndImages.init(strikeSpec);
915 }
916
917 if (regenGlyphs) {
918 // Take the glyphs from the old strike, and translate them a new strike.
919 sk_sp<GrTextStrike> newStrike = strikeSpec.findOrCreateGrStrike(fGrStrikeCache);
920
921 // Start this batch at the start of the subRun plus any glyphs that were previously
922 // processed.
923 size_t glyphStart = fSubRun->glyphStartIndex() + fCurrGlyph;
924 SkSpan<GrGlyph*> glyphs{&(fBlob->fGlyphs[glyphStart]),
925 fSubRun->glyphCount() - fCurrGlyph};
926
927 // Convert old glyphs to newStrike.
928 for (auto& glyph : glyphs) {
929 SkPackedGlyphID id = glyph->fPackedID;
930 glyph = newStrike->getGlyph(id, fMetricsAndImages.get());
931 SkASSERT(id == glyph->fPackedID);
932 }
933
934 fSubRun->setStrike(newStrike);
935 }
936 }
937
938 sk_sp<GrTextStrike> grStrike = fSubRun->refStrike();
939 bool hasW = fSubRun->hasW();
940 auto vertexStride = GetVertexStride(fSubRun->maskFormat(), hasW);
941 char* currVertex = fBlob->fVertices + fSubRun->vertexStartIndex() +
942 fCurrGlyph * kVerticesPerGlyph * vertexStride;
943 result->fFirstVertex = currVertex;
944
945 for (int glyphIdx = fCurrGlyph; glyphIdx < (int)fSubRun->glyphCount(); glyphIdx++) {
946 GrGlyph* glyph = nullptr;
947 if (regenTexCoords) {
948 size_t glyphOffset = glyphIdx + fSubRun->glyphStartIndex();
949 glyph = fBlob->fGlyphs[glyphOffset];
950 SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
951
952 if (!fFullAtlasManager->hasGlyph(glyph)) {
953 GrDrawOpAtlas::ErrorCode code;
954 code = grStrike->addGlyphToAtlas(fResourceProvider, fUploadTarget, fGrStrikeCache,
955 fFullAtlasManager, glyph,
956 fMetricsAndImages.get(), fSubRun->maskFormat(),
957 fSubRun->needsTransform());
958 if (GrDrawOpAtlas::ErrorCode::kError == code) {
959 // Something horrible has happened - drop the op
960 return false;
961 }
962 else if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
963 fBrokenRun = glyphIdx > 0;
964 result->fFinished = false;
965 return true;
966 }
967 }
968 auto tokenTracker = fUploadTarget->tokenTracker();
969 fFullAtlasManager->addGlyphToBulkAndSetUseToken(fSubRun->bulkUseToken(), glyph,
970 tokenTracker->nextDrawToken());
971 }
972
973 if (regenPos) {
974 regen_positions(currVertex, vertexStride, fTransX, fTransY);
975 }
976 if (regenCol) {
977 regen_colors(currVertex, vertexStride, fColor);
978 }
979 if (regenTexCoords) {
980 regen_texcoords(currVertex, vertexStride, glyph, fSubRun->drawAsDistanceFields());
981 }
982
983 currVertex += vertexStride * GrAtlasTextOp::kVerticesPerGlyph;
984 ++result->fGlyphsRegenerated;
985 ++fCurrGlyph;
986 }
987
988 // We may have changed the color so update it here
989 fSubRun->setColor(fColor);
990 if (regenTexCoords) {
991 fSubRun->setAtlasGeneration(fBrokenRun
992 ? GrDrawOpAtlas::kInvalidAtlasGeneration
993 : fFullAtlasManager->atlasGeneration(fSubRun->maskFormat()));
994 } else {
995 // For the non-texCoords case we need to ensure that we update the associated use tokens
996 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
997 fUploadTarget->tokenTracker()->nextDrawToken(),
998 fSubRun->maskFormat());
999 }
1000 return true;
1001}
1002
1003bool GrTextBlob::VertexRegenerator::regenerate(GrTextBlob::VertexRegenerator::Result* result) {
1004 uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
1005 // If regenerate() is called multiple times then the atlas gen may have changed. So we check
1006 // this each time.
1007 if (fSubRun->atlasGeneration() != currentAtlasGen) {
1008 fRegenFlags |= kRegenTex;
1009 }
1010
1011 if (fRegenFlags) {
1012 return this->doRegen(result,
1013 fRegenFlags & kRegenPos,
1014 fRegenFlags & kRegenCol,
1015 fRegenFlags & kRegenTex,
1016 fRegenFlags & kRegenGlyph);
1017 } else {
1018 bool hasW = fSubRun->hasW();
1019 auto vertexStride = GetVertexStride(fSubRun->maskFormat(), hasW);
1020 result->fFinished = true;
1021 result->fGlyphsRegenerated = fSubRun->glyphCount() - fCurrGlyph;
1022 result->fFirstVertex = fBlob->fVertices + fSubRun->vertexStartIndex() +
1023 fCurrGlyph * kVerticesPerGlyph * vertexStride;
1024 fCurrGlyph = fSubRun->glyphCount();
1025
1026 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1027 // have a valid atlas generation
1028 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
1029 fUploadTarget->tokenTracker()->nextDrawToken(),
1030 fSubRun->maskFormat());
1031 return true;
1032 }
1033 SK_ABORT("Should not get here");
1034}
1035
1036
1037
1038