blob: 6db65fc6f5dec8aed1c3f4827d92ec4d8c368cfe [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 Derbycd498e12019-12-06 14:17:31 -0500225
Herb Derby99f228e2019-12-06 16:48:55 -0500226 size_t vertexSize = sizeof(skstd::aligned_union_t<0, Mask2DVertex, ARGB2DVertex>);
Herb Derbye74c7762019-12-04 14:15:41 -0500227 if (viewMatrix.hasPerspective() || forceWForDistanceFields) {
Herb Derby99f228e2019-12-06 16:48:55 -0500228 vertexSize = sizeof(skstd::aligned_union_t<0, SDFT3DVertex, ARGB3DVertex>);
Herb Derbye74c7762019-12-04 14:15:41 -0500229 }
230
Herb Derbycd498e12019-12-06 14:17:31 -0500231 size_t quadSize = kVerticesPerGlyph * vertexSize;
232
Herb Derby86240592018-05-24 16:12:31 -0400233 // We allocate size for the GrTextBlob itself, plus size for the vertices array,
joshualitt92303772016-02-10 11:55:52 -0800234 // and size for the glyphIds array.
Herb Derbye74c7762019-12-04 14:15:41 -0500235 size_t verticesCount = glyphCount * quadSize;
Ben Wagner75d6db72018-09-20 14:39:39 -0400236
Herb Derbyf7d5d742018-11-16 13:24:32 -0500237 size_t blobStart = 0;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500238 size_t vertex = sk_align<alignof(char)> (blobStart + sizeof(GrTextBlob) * 1);
239 size_t glyphs = sk_align<alignof(GrGlyph*)> (vertex + sizeof(char) * verticesCount);
240 size_t size = (glyphs + sizeof(GrGlyph*) * glyphCount);
joshualitt92303772016-02-10 11:55:52 -0800241
Herb Derbyb12175f2018-05-23 16:38:09 -0400242 void* allocation = ::operator new (size);
243
joshualitt92303772016-02-10 11:55:52 -0800244 if (CACHE_SANITY_CHECK) {
245 sk_bzero(allocation, size);
246 }
247
Herb Derby00ae9592019-12-03 15:55:56 -0500248 sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{
Herb Derbye9f691d2019-12-04 12:11:13 -0500249 size, strikeCache, viewMatrix, origin, color, forceWForDistanceFields}};
joshualitt92303772016-02-10 11:55:52 -0800250
251 // setup offsets for vertices / glyphs
Herb Derbyf7d5d742018-11-16 13:24:32 -0500252 blob->fVertices = SkTAddOffset<char>(blob.get(), vertex);
Herb Derby1b8dcd12019-11-15 15:21:15 -0500253 blob->fGlyphs = SkTAddOffset<GrGlyph*>(blob.get(), glyphs);
joshualitt92303772016-02-10 11:55:52 -0800254
Herb Derbyf7d5d742018-11-16 13:24:32 -0500255 return blob;
joshualitt92303772016-02-10 11:55:52 -0800256}
257
Herb Derbya9047642019-12-06 12:12:11 -0500258void GrTextBlob::generateFromGlyphRunList(const GrShaderCaps& shaderCaps,
259 const GrTextContext::Options& options,
260 const SkPaint& paint,
261 const SkMatrix& viewMatrix,
262 const SkSurfaceProps& props,
263 const SkGlyphRunList& glyphRunList,
264 SkGlyphRunListPainter* glyphPainter) {
265 const SkPaint& runPaint = glyphRunList.paint();
266 this->initReusableBlob(SkPaintPriv::ComputeLuminanceColor(runPaint));
267
268 glyphPainter->processGlyphRunList(glyphRunList,
269 viewMatrix,
270 props,
271 shaderCaps.supportsDistanceFieldText(),
272 options,
273 this);
274}
275
276void GrTextBlob::setupKey(const GrTextBlob::Key& key, const SkMaskFilterBase::BlurRec& blurRec,
277 const SkPaint& paint) {
278 fKey = key;
279 if (key.fHasBlur) {
280 fBlurRec = blurRec;
281 }
282 if (key.fStyle != SkPaint::kFill_Style) {
283 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
284 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
285 fStrokeInfo.fJoin = paint.getStrokeJoin();
286 }
287}
288const GrTextBlob::Key& GrTextBlob::GetKey(const GrTextBlob& blob) { return blob.fKey; }
289uint32_t GrTextBlob::Hash(const GrTextBlob::Key& key) { return SkOpts::hash(&key, sizeof(Key)); }
290
291bool GrTextBlob::hasDistanceField() const { return SkToBool(fTextType & kHasDistanceField_TextType); }
292bool GrTextBlob::hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
293bool GrTextBlob::hasPerspective() const { return fInitialViewMatrix.hasPerspective(); }
294
295void GrTextBlob::setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
296void GrTextBlob::setHasBitmap() { fTextType |= kHasBitmap_TextType; }
297void GrTextBlob::setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) {
298 // we init fMaxMinScale and fMinMaxScale in the constructor
299 fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale);
300 fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale);
301}
302
303size_t GrTextBlob::GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
304 switch (maskFormat) {
305 case kA8_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500306 return hasWCoord ? sizeof(SDFT3DVertex) : sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500307 case kARGB_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500308 return hasWCoord ? sizeof(ARGB3DVertex) : sizeof(ARGB2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500309 default:
310 SkASSERT(!hasWCoord);
Herb Derbycd498e12019-12-06 14:17:31 -0500311 return sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500312 }
313}
314
Herb Derby0edb2142018-10-16 17:04:11 -0400315bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
316 const SkMaskFilterBase::BlurRec& blurRec,
317 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
joshualittfd5f6c12015-12-10 07:44:50 -0800318 // If we have LCD text then our canonical color will be set to transparent, in this case we have
319 // to regenerate the blob on any color change
320 // We use the grPaint to get any color filter effects
321 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Mike Reedec7278e2019-02-01 14:00:34 -0500322 fLuminanceColor != SkPaintPriv::ComputeLuminanceColor(paint)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800323 return true;
324 }
325
joshualitt8e0ef292016-02-19 14:13:03 -0800326 if (fInitialViewMatrix.hasPerspective() != viewMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800327 return true;
328 }
329
Brian Salomon5c6ac642017-12-19 11:09:32 -0500330 /** This could be relaxed for blobs with only distance field glyphs. */
joshualitt8e0ef292016-02-19 14:13:03 -0800331 if (fInitialViewMatrix.hasPerspective() && !fInitialViewMatrix.cheapEqualTo(viewMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800332 return true;
333 }
334
335 // We only cache one masked version
336 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400337 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800338 return true;
339 }
340
341 // Similarly, we only cache one version for each style
342 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400343 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
344 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
345 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800346 return true;
347 }
348
349 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
350 // for mixed blobs if this becomes an issue.
351 if (this->hasBitmap() && this->hasDistanceField()) {
Herb Derbyeba195f2019-12-03 16:44:47 -0500352 // Identical view matrices and we can reuse in all cases
353 return !(fInitialViewMatrix.cheapEqualTo(viewMatrix) && SkPoint{x, y} == fInitialOrigin);
joshualittfd5f6c12015-12-10 07:44:50 -0800354 }
355
356 if (this->hasBitmap()) {
joshualitt8e0ef292016-02-19 14:13:03 -0800357 if (fInitialViewMatrix.getScaleX() != viewMatrix.getScaleX() ||
358 fInitialViewMatrix.getScaleY() != viewMatrix.getScaleY() ||
359 fInitialViewMatrix.getSkewX() != viewMatrix.getSkewX() ||
360 fInitialViewMatrix.getSkewY() != viewMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800361 return true;
362 }
363
Herb Derby9830fc42019-09-12 10:58:26 -0400364 // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust
365 // the quads properly. Devise a system that regenerates the quads from original data
366 // using the transform to allow this to be used in general.
367
368 // We can update the positions in the text blob without regenerating the whole
369 // blob, but only for integer translations.
370 // This cool bit of math will determine the necessary translation to apply to the
371 // already generated vertex coordinates to move them to the correct position.
372 // Figure out the translation in view space given a translation in source space.
373 SkScalar transX = viewMatrix.getTranslateX() +
Herb Derbyeba195f2019-12-03 16:44:47 -0500374 viewMatrix.getScaleX() * (x - fInitialOrigin.x()) +
375 viewMatrix.getSkewX() * (y - fInitialOrigin.y()) -
Herb Derby9830fc42019-09-12 10:58:26 -0400376 fInitialViewMatrix.getTranslateX();
377 SkScalar transY = viewMatrix.getTranslateY() +
Herb Derbyeba195f2019-12-03 16:44:47 -0500378 viewMatrix.getSkewY() * (x - fInitialOrigin.x()) +
379 viewMatrix.getScaleY() * (y - fInitialOrigin.y()) -
Herb Derby9830fc42019-09-12 10:58:26 -0400380 fInitialViewMatrix.getTranslateY();
381 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
382 return true;
joshualittfd5f6c12015-12-10 07:44:50 -0800383 }
joshualittfd5f6c12015-12-10 07:44:50 -0800384 } else if (this->hasDistanceField()) {
385 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
386 // distance field being generated, so we have to regenerate in those cases
387 SkScalar newMaxScale = viewMatrix.getMaxScale();
joshualitt8e0ef292016-02-19 14:13:03 -0800388 SkScalar oldMaxScale = fInitialViewMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800389 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
390 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
391 return true;
392 }
joshualittfd5f6c12015-12-10 07:44:50 -0800393 }
394
joshualittfd5f6c12015-12-10 07:44:50 -0800395 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
396 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
397 // the blob anyways at flush time, so no need to regenerate explicitly
398 return false;
399}
400
Herb Derbyc1b482c2018-08-09 15:02:27 -0400401void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
402 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400403 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400404 const SkMatrix& viewMatrix, SkScalar x, SkScalar y) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500405
Herb Derby1b8dcd12019-11-15 15:21:15 -0500406 for (auto& subRun : fSubRuns) {
407 if (subRun.drawAsPaths()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400408 SkPaint runPaint{paint};
Herb Derby1b8dcd12019-11-15 15:21:15 -0500409 runPaint.setAntiAlias(subRun.isAntiAliased());
410 // If there are shaders, blurs or styles, the path must be scaled into source
411 // space independently of the CTM. This allows the CTM to be correct for the
412 // different effects.
413 GrStyle style(runPaint);
Herb Derby9f491482018-08-08 11:53:00 -0400414
Herb Derby1b8dcd12019-11-15 15:21:15 -0500415 bool scalePath = runPaint.getShader()
416 || style.applies()
417 || runPaint.getMaskFilter();
Robert Phillips137ca522018-08-15 10:14:33 -0400418
Herb Derby1b8dcd12019-11-15 15:21:15 -0500419 // The origin for the blob may have changed, so figure out the delta.
Herb Derbyeba195f2019-12-03 16:44:47 -0500420 SkVector originShift = SkPoint{x, y} - fInitialOrigin;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500421
422 for (const auto& pathGlyph : subRun.fPaths) {
423 SkMatrix ctm{viewMatrix};
424 SkMatrix pathMatrix = SkMatrix::MakeScale(subRun.fStrikeSpec.strikeToSourceRatio());
425 // Shift the original glyph location in source space to the position of the new
426 // blob.
427 pathMatrix.postTranslate(originShift.x() + pathGlyph.fOrigin.x(),
428 originShift.y() + pathGlyph.fOrigin.y());
Herb Derbydac1ed52018-09-12 17:04:21 -0400429
430 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400431 SkTLazy<SkPath> tmpPath;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500432 const SkPath* path = &pathGlyph.fPath;
Herb Derby2984d262019-11-20 14:40:39 -0500433 if (!scalePath) {
434 // Scale can be applied to CTM -- no effects.
Herb Derby2984d262019-11-20 14:40:39 -0500435 ctm.preConcat(pathMatrix);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400436 } else {
Herb Derby2984d262019-11-20 14:40:39 -0500437 // Scale the outline into source space.
Herb Derbydac1ed52018-09-12 17:04:21 -0400438
Herb Derby2984d262019-11-20 14:40:39 -0500439 // Transform the path form the normalized outline to source space. This
440 // way the CTM will remain the same so it can be used by the effects.
441 SkPath* sourceOutline = tmpPath.init();
442 path->transform(pathMatrix, sourceOutline);
443 sourceOutline->setIsVolatile(true);
444 path = sourceOutline;
Robert Phillips137ca522018-08-15 10:14:33 -0400445 }
446
Robert Phillips46a13382018-08-23 13:53:01 -0400447 // TODO: we are losing the mutability of the path here
448 GrShape shape(*path, paint);
Herb Derbydac1ed52018-09-12 17:04:21 -0400449 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500450 }
Herb Derby1b8dcd12019-11-15 15:21:15 -0500451 } else {
452 int glyphCount = subRun.glyphCount();
Jim Van Verth54d9c882018-02-08 16:14:48 -0500453 if (0 == glyphCount) {
454 continue;
455 }
456
457 bool skipClip = false;
458 bool submitOp = true;
459 SkIRect clipRect = SkIRect::MakeEmpty();
460 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
461 SkRRect clipRRect;
462 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400463 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500464 // and we have an axis-aligned rectangular non-AA clip
Herb Derby1b8dcd12019-11-15 15:21:15 -0500465 if (!subRun.drawAsDistanceFields() && !subRun.needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500466 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500467 clipRRect.isRect() && GrAA::kNo == aa) {
468 skipClip = true;
469 // We only need to do clipping work if the subrun isn't contained by the clip
470 SkRect subRunBounds;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500471 this->computeSubRunBounds(&subRunBounds, subRun, viewMatrix, x, y, false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500472 if (!clipRRect.getBounds().contains(subRunBounds)) {
473 // If the subrun is completely outside, don't add an op for it
474 if (!clipRRect.getBounds().intersects(subRunBounds)) {
475 submitOp = false;
476 }
477 else {
478 clipRRect.getBounds().round(&clipRect);
479 }
480 }
481 }
482
483 if (submitOp) {
Herb Derby1b8dcd12019-11-15 15:21:15 -0500484 auto op = this->makeOp(subRun, glyphCount, viewMatrix, x, y,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400485 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500486 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500487 if (op) {
488 if (skipClip) {
489 target->addDrawOp(GrNoClip(), std::move(op));
490 }
491 else {
492 target->addDrawOp(clip, std::move(op));
493 }
494 }
495 }
496 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000497 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000498}
499
Herb Derbya9047642019-12-06 12:12:11 -0500500void GrTextBlob::computeSubRunBounds(SkRect* outBounds, const GrTextBlob::SubRun& subRun,
501 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
502 bool needsGlyphTransform) {
503 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
504 // into device space.
505 // We handle vertex bounds differently for distance field text and bitmap text because
506 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
507 // from one blob then we are going to pay the price here of mapping the rect for each run.
508 *outBounds = subRun.vertexBounds();
509 if (needsGlyphTransform) {
510 // Distance field text is positioned with the (X,Y) as part of the glyph position,
511 // and currently the view matrix is applied on the GPU
512 outBounds->offset(SkPoint{x, y} - fInitialOrigin);
513 viewMatrix.mapRect(outBounds);
514 } else {
515 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
516 // device space.
517 SkMatrix boundsMatrix = fInitialViewMatrixInverse;
518
519 boundsMatrix.postTranslate(-fInitialOrigin.x(), -fInitialOrigin.y());
520
521 boundsMatrix.postTranslate(x, y);
522
523 boundsMatrix.postConcat(viewMatrix);
524 boundsMatrix.mapRect(outBounds);
525
526 // Due to floating point numerical inaccuracies, we have to round out here
527 outBounds->roundOut(outBounds);
528 }
joshualitt323c2eb2016-01-20 06:48:47 -0800529}
joshualitt2e2202e2015-12-10 11:22:08 -0800530
Herb Derby86240592018-05-24 16:12:31 -0400531void GrTextBlob::AssertEqual(const GrTextBlob& l, const GrTextBlob& r) {
joshualitt2f2ee832016-02-10 08:52:24 -0800532 SkASSERT_RELEASE(l.fSize == r.fSize);
joshualitt259fbf12015-07-21 11:39:34 -0700533
joshualitt2f2ee832016-02-10 08:52:24 -0800534 SkASSERT_RELEASE(l.fBlurRec.fSigma == r.fBlurRec.fSigma);
535 SkASSERT_RELEASE(l.fBlurRec.fStyle == r.fBlurRec.fStyle);
joshualitt259fbf12015-07-21 11:39:34 -0700536
joshualitt2f2ee832016-02-10 08:52:24 -0800537 SkASSERT_RELEASE(l.fStrokeInfo.fFrameWidth == r.fStrokeInfo.fFrameWidth);
538 SkASSERT_RELEASE(l.fStrokeInfo.fMiterLimit == r.fStrokeInfo.fMiterLimit);
539 SkASSERT_RELEASE(l.fStrokeInfo.fJoin == r.fStrokeInfo.fJoin);
joshualitt259fbf12015-07-21 11:39:34 -0700540
joshualitt2f2ee832016-02-10 08:52:24 -0800541 SkASSERT_RELEASE(l.fKey == r.fKey);
joshualitt2f2ee832016-02-10 08:52:24 -0800542 //SkASSERT_RELEASE(l.fPaintColor == r.fPaintColor); // Colors might not actually be identical
543 SkASSERT_RELEASE(l.fMaxMinScale == r.fMaxMinScale);
544 SkASSERT_RELEASE(l.fMinMaxScale == r.fMinMaxScale);
545 SkASSERT_RELEASE(l.fTextType == r.fTextType);
joshualitt259fbf12015-07-21 11:39:34 -0700546
Herb Derby1b8dcd12019-11-15 15:21:15 -0500547 for(auto t : SkMakeZip(l.fSubRuns, r.fSubRuns)) {
548 const SubRun& lSubRun = std::get<0>(t);
549 const SubRun& rSubRun = std::get<1>(t);
550 SkASSERT(lSubRun.drawAsPaths() == rSubRun.drawAsPaths());
551 if (!lSubRun.drawAsPaths()) {
joshualitt259fbf12015-07-21 11:39:34 -0700552
joshualitt2f2ee832016-02-10 08:52:24 -0800553 // TODO we can do this check, but we have to apply the VM to the old vertex bounds
554 //SkASSERT_RELEASE(lSubRun.vertexBounds() == rSubRun.vertexBounds());
joshualitt259fbf12015-07-21 11:39:34 -0700555
joshualitt2f2ee832016-02-10 08:52:24 -0800556 if (lSubRun.strike()) {
557 SkASSERT_RELEASE(rSubRun.strike());
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500558 SkASSERT_RELEASE(GrTextStrike::GetKey(*lSubRun.strike()) ==
559 GrTextStrike::GetKey(*rSubRun.strike()));
joshualitt2f2ee832016-02-10 08:52:24 -0800560
561 } else {
562 SkASSERT_RELEASE(!rSubRun.strike());
563 }
564
565 SkASSERT_RELEASE(lSubRun.vertexStartIndex() == rSubRun.vertexStartIndex());
joshualitt2f2ee832016-02-10 08:52:24 -0800566 SkASSERT_RELEASE(lSubRun.glyphStartIndex() == rSubRun.glyphStartIndex());
joshualitt2f2ee832016-02-10 08:52:24 -0800567 SkASSERT_RELEASE(lSubRun.maskFormat() == rSubRun.maskFormat());
568 SkASSERT_RELEASE(lSubRun.drawAsDistanceFields() == rSubRun.drawAsDistanceFields());
569 SkASSERT_RELEASE(lSubRun.hasUseLCDText() == rSubRun.hasUseLCDText());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500570 } else {
571 SkASSERT_RELEASE(lSubRun.fPaths.size() == rSubRun.fPaths.size());
572 for(auto p : SkMakeZip(lSubRun.fPaths, rSubRun.fPaths)) {
573 const PathGlyph& lPath = std::get<0>(p);
574 const PathGlyph& rPath = std::get<1>(p);
575 SkASSERT_RELEASE(lPath.fPath == rPath.fPath);
576 // We can't assert that these have the same translations
577 }
Jim Van Verth54d9c882018-02-08 16:14:48 -0500578 }
joshualitt259fbf12015-07-21 11:39:34 -0700579 }
580}
joshualitt8e0ef292016-02-19 14:13:03 -0800581
Herb Derbya9047642019-12-06 12:12:11 -0500582void GrTextBlob::initReusableBlob(SkColor luminanceColor) {
583 fLuminanceColor = luminanceColor;
584}
585
586const GrTextBlob::Key& GrTextBlob::key() const { return fKey; }
587size_t GrTextBlob::size() const { return fSize; }
588
589std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
590 int glyphCount, const SkMatrix& viewMatrix,
591 SkScalar x, SkScalar y, const SkPaint& paint, const SkPMColor4f& filteredColor,
592 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
593 GrTextTarget* target) {
594 GrTextBlob::SubRun& info = fSubRuns[0];
595 SkIRect emptyRect = SkIRect::MakeEmpty();
596 return this->makeOp(info, glyphCount, viewMatrix, x, y, emptyRect,
597 paint, filteredColor, props, distanceAdjustTable, target);
598}
599
600bool GrTextBlob::hasW(GrTextBlob::SubRunType type) const {
601 if (type == kTransformedSDFT) {
602 return this->hasPerspective() || fForceWForDistanceFields;
603 } else if (type == kTransformedMask || type == kTransformedPath) {
604 return this->hasPerspective();
Herb Derbye9f691d2019-12-04 12:11:13 -0500605 }
Herb Derbya9047642019-12-06 12:12:11 -0500606
607 // The viewMatrix is implicitly SkMatrix::I when drawing kDirectMask, because it is not
608 // used.
609 return false;
610}
611
612GrTextBlob::SubRun* GrTextBlob::makeSubRun(SubRunType type,
613 const SkZip<SkGlyphVariant, SkPoint>& drawables,
614 const SkStrikeSpec& strikeSpec,
615 GrMaskFormat format) {
616 bool hasW = this->hasW(type);
617 uint32_t glyphsStart = fGlyphsCursor;
618 fGlyphsCursor += drawables.size();
619 uint32_t glyphsEnd = fGlyphsCursor;
620 size_t verticesStart = fVerticesCursor;
621 fVerticesCursor += drawables.size() * GetVertexStride(format, hasW) * kVerticesPerGlyph;
622 size_t verticesEnd = fVerticesCursor;
623
624 SubRunBufferSpec bufferSpec = std::make_tuple(
625 glyphsStart, glyphsEnd, verticesStart, verticesEnd);
626
627 sk_sp<GrTextStrike> grStrike = strikeSpec.findOrCreateGrStrike(fStrikeCache);
628
629 SubRun& subRun = fSubRuns.emplace_back(
630 type, this, strikeSpec, format, bufferSpec, std::move(grStrike));
631
632 subRun.appendGlyphs(drawables);
633
634 return &subRun;
635}
636
637void GrTextBlob::addSingleMaskFormat(
638 SubRunType type,
639 const SkZip<SkGlyphVariant, SkPoint>& drawables,
640 const SkStrikeSpec& strikeSpec,
641 GrMaskFormat format) {
642 this->makeSubRun(type, drawables, strikeSpec, format);
643}
644
645void GrTextBlob::addMultiMaskFormat(
646 SubRunType type,
647 const SkZip<SkGlyphVariant, SkPoint>& drawables,
648 const SkStrikeSpec& strikeSpec) {
649 this->setHasBitmap();
650 if (drawables.empty()) { return; }
651
652 auto glyphSpan = drawables.get<0>();
653 SkGlyph* glyph = glyphSpan[0];
654 GrMaskFormat format = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
655 size_t startIndex = 0;
656 for (size_t i = 1; i < drawables.size(); i++) {
657 glyph = glyphSpan[i];
658 GrMaskFormat nextFormat = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
659 if (format != nextFormat) {
660 auto sameFormat = drawables.subspan(startIndex, i - startIndex);
661 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
662 format = nextFormat;
663 startIndex = i;
664 }
665 }
666 auto sameFormat = drawables.last(drawables.size() - startIndex);
667 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
668}
669
670void GrTextBlob::addSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
671 const SkStrikeSpec& strikeSpec,
672 const SkFont& runFont,
673 SkScalar minScale,
674 SkScalar maxScale) {
675 this->setHasDistanceField();
676 this->setMinAndMaxScale(minScale, maxScale);
677
678 SubRun* subRun = this->makeSubRun(kTransformedSDFT, drawables, strikeSpec, kA8_GrMaskFormat);
679 subRun->setUseLCDText(runFont.getEdging() == SkFont::Edging::kSubpixelAntiAlias);
680 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbye9f691d2019-12-04 12:11:13 -0500681}
682
Herb Derbyeba195f2019-12-03 16:44:47 -0500683GrTextBlob::GrTextBlob(size_t size,
684 GrStrikeCache* strikeCache,
Herb Derbye9f691d2019-12-04 12:11:13 -0500685 const SkMatrix& viewMatrix,
Herb Derbyeba195f2019-12-03 16:44:47 -0500686 SkPoint origin,
687 GrColor color,
Herb Derby00ae9592019-12-03 15:55:56 -0500688 bool forceWForDistanceFields)
689 : fSize{size}
690 , fStrikeCache{strikeCache}
Herb Derbye9f691d2019-12-04 12:11:13 -0500691 , fInitialViewMatrix{viewMatrix}
692 , fInitialViewMatrixInverse{make_inverse(viewMatrix)}
Herb Derbyeba195f2019-12-03 16:44:47 -0500693 , fInitialOrigin{origin}
Herb Derby00ae9592019-12-03 15:55:56 -0500694 , fForceWForDistanceFields{forceWForDistanceFields}
695 , fColor{color} { }
696
Herb Derbya9047642019-12-06 12:12:11 -0500697inline std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
698 SubRun& info, int glyphCount,
699 const SkMatrix& viewMatrix, SkScalar x, SkScalar y, const SkIRect& clipRect,
700 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
701 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
702 GrMaskFormat format = info.maskFormat();
703
704 GrPaint grPaint;
705 target->makeGrPaint(info.maskFormat(), paint, viewMatrix, &grPaint);
706 std::unique_ptr<GrAtlasTextOp> op;
707 if (info.drawAsDistanceFields()) {
708 // TODO: Can we be even smarter based on the dest transfer function?
709 op = GrAtlasTextOp::MakeDistanceField(
710 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
711 target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint),
712 props, info.isAntiAliased(), info.hasUseLCDText());
Herb Derbyeba195f2019-12-03 16:44:47 -0500713 } else {
Herb Derbya9047642019-12-06 12:12:11 -0500714 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
715 info.needsTransform());
716 }
717 GrAtlasTextOp::Geometry& geometry = op->geometry();
718 geometry.fViewMatrix = viewMatrix;
719 geometry.fClipRect = clipRect;
720 geometry.fBlob = SkRef(this);
721 geometry.fSubRunPtr = &info;
722 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
723 geometry.fX = x;
724 geometry.fY = y;
725 op->init();
726 return op;
727}
Herb Derbyeba195f2019-12-03 16:44:47 -0500728
Herb Derbya9047642019-12-06 12:12:11 -0500729void GrTextBlob::processDeviceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
730 const SkStrikeSpec& strikeSpec) {
731 this->addMultiMaskFormat(kDirectMask, drawables, strikeSpec);
732}
Herb Derbyeba195f2019-12-03 16:44:47 -0500733
Herb Derbya9047642019-12-06 12:12:11 -0500734void GrTextBlob::processSourcePaths(const SkZip<SkGlyphVariant, SkPoint>& drawables,
735 const SkFont& runFont,
736 const SkStrikeSpec& strikeSpec) {
737 this->setHasBitmap();
738 SubRun& subRun = fSubRuns.emplace_back(this, strikeSpec);
739 subRun.setAntiAliased(runFont.hasSomeAntiAliasing());
740 for (auto [variant, pos] : drawables) {
741 subRun.fPaths.emplace_back(*variant.path(), pos);
Herb Derbyeba195f2019-12-03 16:44:47 -0500742 }
743}
744
Herb Derbya9047642019-12-06 12:12:11 -0500745void GrTextBlob::processSourceSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
746 const SkStrikeSpec& strikeSpec,
747 const SkFont& runFont,
748 SkScalar minScale,
749 SkScalar maxScale) {
750 this->addSDFT(drawables, strikeSpec, runFont, minScale, maxScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800751}
Herb Derbya9047642019-12-06 12:12:11 -0500752
753void GrTextBlob::processSourceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
754 const SkStrikeSpec& strikeSpec) {
755 this->addMultiMaskFormat(kTransformedMask, drawables, strikeSpec);
756}
757
758// -- GrTextBlob::VertexRegenerator ----------------------------------------------------------------
759enum RegenMask {
760 kNoRegen = 0x0,
761 kRegenPos = 0x1,
762 kRegenCol = 0x2,
763 kRegenTex = 0x4,
764 kRegenGlyph = 0x8,
765};
766
767static void regen_positions(char* vertex, size_t vertexStride, SkScalar transX, SkScalar transY) {
768 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
769 for (int i = 0; i < 4; ++i) {
770 point->fX += transX;
771 point->fY += transY;
772 point = SkTAddOffset<SkPoint>(point, vertexStride);
773 }
774}
775
776static void regen_colors(char* vertex, size_t vertexStride, GrColor color) {
777 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
778 // vertices, hence vertexStride - sizeof(SkIPoint16)
779 size_t colorOffset = vertexStride - sizeof(SkIPoint16) - sizeof(GrColor);
780 GrColor* vcolor = reinterpret_cast<GrColor*>(vertex + colorOffset);
781 for (int i = 0; i < 4; ++i) {
782 *vcolor = color;
783 vcolor = SkTAddOffset<GrColor>(vcolor, vertexStride);
784 }
785}
786
787static void regen_texcoords(char* vertex, size_t vertexStride, const GrGlyph* glyph,
788 bool useDistanceFields) {
789 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
790 // vertices, hence vertexStride - sizeof(SkIPoint16)
791 size_t texCoordOffset = vertexStride - sizeof(SkIPoint16);
792
793 uint16_t u0, v0, u1, v1;
794 SkASSERT(glyph);
795 int width = glyph->fBounds.width();
796 int height = glyph->fBounds.height();
797
798 if (useDistanceFields) {
799 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
800 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
801 u1 = u0 + width - 2 * SK_DistanceFieldInset;
802 v1 = v0 + height - 2 * SK_DistanceFieldInset;
803 } else {
804 u0 = glyph->fAtlasLocation.fX;
805 v0 = glyph->fAtlasLocation.fY;
806 u1 = u0 + width;
807 v1 = v0 + height;
808 }
809 // We pack the 2bit page index in the low bit of the u and v texture coords
810 uint32_t pageIndex = glyph->pageIndex();
811 SkASSERT(pageIndex < 4);
812 uint16_t uBit = (pageIndex >> 1) & 0x1;
813 uint16_t vBit = pageIndex & 0x1;
814 u0 <<= 1;
815 u0 |= uBit;
816 v0 <<= 1;
817 v0 |= vBit;
818 u1 <<= 1;
819 u1 |= uBit;
820 v1 <<= 1;
821 v1 |= vBit;
822
823 uint16_t* textureCoords = reinterpret_cast<uint16_t*>(vertex + texCoordOffset);
824 textureCoords[0] = u0;
825 textureCoords[1] = v0;
826 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
827 textureCoords[0] = u0;
828 textureCoords[1] = v1;
829 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
830 textureCoords[0] = u1;
831 textureCoords[1] = v0;
832 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
833 textureCoords[0] = u1;
834 textureCoords[1] = v1;
835
836#ifdef DISPLAY_PAGE_INDEX
837 // Enable this to visualize the page from which each glyph is being drawn.
838 // Green Red Magenta Cyan -> 0 1 2 3; Black -> error
839 GrColor hackColor;
840 switch (pageIndex) {
841 case 0:
842 hackColor = GrColorPackRGBA(0, 255, 0, 255);
843 break;
844 case 1:
845 hackColor = GrColorPackRGBA(255, 0, 0, 255);;
846 break;
847 case 2:
848 hackColor = GrColorPackRGBA(255, 0, 255, 255);
849 break;
850 case 3:
851 hackColor = GrColorPackRGBA(0, 255, 255, 255);
852 break;
853 default:
854 hackColor = GrColorPackRGBA(0, 0, 0, 255);
855 break;
856 }
857 regen_colors(vertex, vertexStride, hackColor);
858#endif
859}
860
861GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
862 GrTextBlob* blob,
863 GrTextBlob::SubRun* subRun,
864 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
865 GrColor color,
866 GrDeferredUploadTarget* uploadTarget,
867 GrStrikeCache* grStrikeCache,
868 GrAtlasManager* fullAtlasManager)
869 : fResourceProvider(resourceProvider)
870 , fViewMatrix(viewMatrix)
871 , fBlob(blob)
872 , fUploadTarget(uploadTarget)
873 , fGrStrikeCache(grStrikeCache)
874 , fFullAtlasManager(fullAtlasManager)
875 , fSubRun(subRun)
876 , fColor(color) {
877 // Compute translation if any
878 fSubRun->computeTranslation(fViewMatrix, x, y, &fTransX, &fTransY);
879
880 // Because the GrStrikeCache may evict the strike a blob depends on using for
881 // generating its texture coords, we have to track whether or not the strike has
882 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
883 // otherwise we have to get the new strike, and use that to get the correct glyphs.
884 // Because we do not have the packed ids, and thus can't look up our glyphs in the
885 // new strike, we instead keep our ref to the old strike and use the packed ids from
886 // it. These ids will still be valid as long as we hold the ref. When we are done
887 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
888 if (fSubRun->strike()->isAbandoned()) {
889 fRegenFlags |= kRegenGlyph;
890 fRegenFlags |= kRegenTex;
891 }
892 if (kARGB_GrMaskFormat != fSubRun->maskFormat() && fSubRun->color() != color) {
893 fRegenFlags |= kRegenCol;
894 }
895 if (0.f != fTransX || 0.f != fTransY) {
896 fRegenFlags |= kRegenPos;
897 }
898}
899
900bool GrTextBlob::VertexRegenerator::doRegen(GrTextBlob::VertexRegenerator::Result* result,
901 bool regenPos, bool regenCol, bool regenTexCoords,
902 bool regenGlyphs) {
903 SkASSERT(!regenGlyphs || regenTexCoords);
904 if (regenTexCoords) {
905 fSubRun->resetBulkUseToken();
906
907 const SkStrikeSpec& strikeSpec = fSubRun->strikeSpec();
908
909 if (!fMetricsAndImages.isValid()
910 || fMetricsAndImages->descriptor() != strikeSpec.descriptor()) {
911 fMetricsAndImages.init(strikeSpec);
912 }
913
914 if (regenGlyphs) {
915 // Take the glyphs from the old strike, and translate them a new strike.
916 sk_sp<GrTextStrike> newStrike = strikeSpec.findOrCreateGrStrike(fGrStrikeCache);
917
918 // Start this batch at the start of the subRun plus any glyphs that were previously
919 // processed.
920 size_t glyphStart = fSubRun->glyphStartIndex() + fCurrGlyph;
921 SkSpan<GrGlyph*> glyphs{&(fBlob->fGlyphs[glyphStart]),
922 fSubRun->glyphCount() - fCurrGlyph};
923
924 // Convert old glyphs to newStrike.
925 for (auto& glyph : glyphs) {
926 SkPackedGlyphID id = glyph->fPackedID;
927 glyph = newStrike->getGlyph(id, fMetricsAndImages.get());
928 SkASSERT(id == glyph->fPackedID);
929 }
930
931 fSubRun->setStrike(newStrike);
932 }
933 }
934
935 sk_sp<GrTextStrike> grStrike = fSubRun->refStrike();
936 bool hasW = fSubRun->hasW();
937 auto vertexStride = GetVertexStride(fSubRun->maskFormat(), hasW);
938 char* currVertex = fBlob->fVertices + fSubRun->vertexStartIndex() +
939 fCurrGlyph * kVerticesPerGlyph * vertexStride;
940 result->fFirstVertex = currVertex;
941
942 for (int glyphIdx = fCurrGlyph; glyphIdx < (int)fSubRun->glyphCount(); glyphIdx++) {
943 GrGlyph* glyph = nullptr;
944 if (regenTexCoords) {
945 size_t glyphOffset = glyphIdx + fSubRun->glyphStartIndex();
946 glyph = fBlob->fGlyphs[glyphOffset];
947 SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
948
949 if (!fFullAtlasManager->hasGlyph(glyph)) {
950 GrDrawOpAtlas::ErrorCode code;
951 code = grStrike->addGlyphToAtlas(fResourceProvider, fUploadTarget, fGrStrikeCache,
952 fFullAtlasManager, glyph,
953 fMetricsAndImages.get(), fSubRun->maskFormat(),
954 fSubRun->needsTransform());
955 if (GrDrawOpAtlas::ErrorCode::kError == code) {
956 // Something horrible has happened - drop the op
957 return false;
958 }
959 else if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
960 fBrokenRun = glyphIdx > 0;
961 result->fFinished = false;
962 return true;
963 }
964 }
965 auto tokenTracker = fUploadTarget->tokenTracker();
966 fFullAtlasManager->addGlyphToBulkAndSetUseToken(fSubRun->bulkUseToken(), glyph,
967 tokenTracker->nextDrawToken());
968 }
969
970 if (regenPos) {
971 regen_positions(currVertex, vertexStride, fTransX, fTransY);
972 }
973 if (regenCol) {
974 regen_colors(currVertex, vertexStride, fColor);
975 }
976 if (regenTexCoords) {
977 regen_texcoords(currVertex, vertexStride, glyph, fSubRun->drawAsDistanceFields());
978 }
979
980 currVertex += vertexStride * GrAtlasTextOp::kVerticesPerGlyph;
981 ++result->fGlyphsRegenerated;
982 ++fCurrGlyph;
983 }
984
985 // We may have changed the color so update it here
986 fSubRun->setColor(fColor);
987 if (regenTexCoords) {
988 fSubRun->setAtlasGeneration(fBrokenRun
989 ? GrDrawOpAtlas::kInvalidAtlasGeneration
990 : fFullAtlasManager->atlasGeneration(fSubRun->maskFormat()));
991 } else {
992 // For the non-texCoords case we need to ensure that we update the associated use tokens
993 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
994 fUploadTarget->tokenTracker()->nextDrawToken(),
995 fSubRun->maskFormat());
996 }
997 return true;
998}
999
1000bool GrTextBlob::VertexRegenerator::regenerate(GrTextBlob::VertexRegenerator::Result* result) {
1001 uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
1002 // If regenerate() is called multiple times then the atlas gen may have changed. So we check
1003 // this each time.
1004 if (fSubRun->atlasGeneration() != currentAtlasGen) {
1005 fRegenFlags |= kRegenTex;
1006 }
1007
1008 if (fRegenFlags) {
1009 return this->doRegen(result,
1010 fRegenFlags & kRegenPos,
1011 fRegenFlags & kRegenCol,
1012 fRegenFlags & kRegenTex,
1013 fRegenFlags & kRegenGlyph);
1014 } else {
1015 bool hasW = fSubRun->hasW();
1016 auto vertexStride = GetVertexStride(fSubRun->maskFormat(), hasW);
1017 result->fFinished = true;
1018 result->fGlyphsRegenerated = fSubRun->glyphCount() - fCurrGlyph;
1019 result->fFirstVertex = fBlob->fVertices + fSubRun->vertexStartIndex() +
1020 fCurrGlyph * kVerticesPerGlyph * vertexStride;
1021 fCurrGlyph = fSubRun->glyphCount();
1022
1023 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1024 // have a valid atlas generation
1025 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
1026 fUploadTarget->tokenTracker()->nextDrawToken(),
1027 fSubRun->maskFormat());
1028 return true;
1029 }
1030 SK_ABORT("Should not get here");
1031}
1032
1033
1034
1035