blob: a8f401805c77336bd8189e8bd2cd24cec4b3178b [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
Herb Derby3d3150c2019-12-23 15:26:44 -050021#include <cstddef>
Mike Klein79aea6a2018-06-11 10:45:26 -040022#include <new>
joshualitt2e2202e2015-12-10 11:22:08 -080023
Herb Derby5bf5b042019-12-12 16:37:03 -050024static SkVector calculate_translation(bool applyVM,
25 const SkMatrix& drawMatrix, SkPoint drawOrigin,
26 const SkMatrix& currentViewMatrix, SkPoint currentOrigin) {
27 SkVector translate;
Herb Derbya9047642019-12-06 12:12:11 -050028 if (applyVM) {
Herb Derby5bf5b042019-12-12 16:37:03 -050029 translate.fX = drawMatrix.getTranslateX() +
30 drawMatrix.getScaleX() * (drawOrigin.x() - currentOrigin.x()) +
31 drawMatrix.getSkewX() * (drawOrigin.y() - currentOrigin.y()) -
32 currentViewMatrix.getTranslateX();
Herb Derbya9047642019-12-06 12:12:11 -050033
Herb Derby5bf5b042019-12-12 16:37:03 -050034 translate.fY = drawMatrix.getTranslateY() +
35 drawMatrix.getSkewY() * (drawOrigin.x() - currentOrigin.x()) +
36 drawMatrix.getScaleY() * (drawOrigin.y() - currentOrigin.y()) -
37 currentViewMatrix.getTranslateY();
Herb Derbya9047642019-12-06 12:12:11 -050038 } else {
Herb Derby5bf5b042019-12-12 16:37:03 -050039 translate = drawOrigin - currentOrigin;
Herb Derbya9047642019-12-06 12:12:11 -050040 }
Herb Derby5bf5b042019-12-12 16:37:03 -050041
42 return translate;
Herb Derbya9047642019-12-06 12:12:11 -050043}
44
45static SkMatrix make_inverse(const SkMatrix& matrix) {
46 SkMatrix inverseMatrix;
47 if (!matrix.invert(&inverseMatrix)) {
48 inverseMatrix = SkMatrix::I();
49 }
50 return inverseMatrix;
51}
52
53// -- GrTextBlob::Key ------------------------------------------------------------------------------
54GrTextBlob::Key::Key() { sk_bzero(this, sizeof(Key)); }
55
56bool GrTextBlob::Key::operator==(const GrTextBlob::Key& other) const {
57 return 0 == memcmp(this, &other, sizeof(Key));
58}
59
60// -- GrTextBlob::PathGlyph ------------------------------------------------------------------------
61GrTextBlob::PathGlyph::PathGlyph(const SkPath& path, SkPoint origin)
62 : fPath(path)
63 , fOrigin(origin) {}
64
65// -- GrTextBlob::SubRun ---------------------------------------------------------------------------
Herb Derbye3c7ff42019-12-10 17:49:28 -050066// Hold data to draw the different types of sub run. SubRuns are produced knowing all the
67// glyphs that are included in them.
68class GrTextBlob::SubRun {
69public:
70 // SubRun for masks
71 SubRun(SubRunType type,
72 GrTextBlob* textBlob,
73 const SkStrikeSpec& strikeSpec,
74 GrMaskFormat format,
Herb Derbyc514e7d2019-12-11 17:00:31 -050075 const SkSpan<GrGlyph*>& glyphs, const SkSpan<char>& vertexData,
Herb Derbye3c7ff42019-12-10 17:49:28 -050076 sk_sp<GrTextStrike>&& grStrike);
77
78 // SubRun for paths
79 SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec);
80
81 void appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables);
82
83 // TODO when this object is more internal, drop the privacy
84 void resetBulkUseToken();
85 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken();
86 void setStrike(sk_sp<GrTextStrike> strike);
87 GrTextStrike* strike() const;
Herb Derbye3c7ff42019-12-10 17:49:28 -050088
89 void setAtlasGeneration(uint64_t atlasGeneration);
90 uint64_t atlasGeneration() const;
91
Herb Derbye3c7ff42019-12-10 17:49:28 -050092 void setColor(GrColor color);
93 GrColor color() const;
94
95 GrMaskFormat maskFormat() const;
96
Herb Derbya2d72252019-12-23 15:02:33 -050097 size_t vertexStride() const;
Herb Derby3d3150c2019-12-23 15:26:44 -050098 size_t colorOffset() const;
Herb Derbya2d72252019-12-23 15:02:33 -050099
Herb Derbye3c7ff42019-12-10 17:49:28 -0500100 const SkRect& vertexBounds() const;
101 void joinGlyphBounds(const SkRect& glyphBounds);
102
Herb Derbye3c7ff42019-12-10 17:49:28 -0500103 // This function assumes the translation will be applied before it is called again
Herb Derby5bf5b042019-12-12 16:37:03 -0500104 SkVector computeTranslation(const SkMatrix& drawMatrix, SkPoint drawOrigin);
Herb Derbye3c7ff42019-12-10 17:49:28 -0500105
106 bool drawAsDistanceFields() const;
107 bool drawAsPaths() const;
108 bool needsTransform() const;
109 bool hasW() const;
110
111 // df properties
112 void setUseLCDText(bool useLCDText);
113 bool hasUseLCDText() const;
114 void setAntiAliased(bool antiAliased);
115 bool isAntiAliased() const;
116
117 const SkStrikeSpec& strikeSpec() const;
118
119 SubRun* fNextSubRun{nullptr};
120 const SubRunType fType;
121 GrTextBlob* const fBlob;
122 const GrMaskFormat fMaskFormat;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500123 const SkSpan<GrGlyph*> fGlyphs;
124 const SkSpan<char> fVertexData;
Herb Derbye3c7ff42019-12-10 17:49:28 -0500125 const SkStrikeSpec fStrikeSpec;
126 sk_sp<GrTextStrike> fStrike;
127 struct {
128 bool useLCDText:1;
129 bool antiAliased:1;
130 } fFlags{false, false};
131 GrColor fColor;
132 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
133 SkRect fVertexBounds = SkRectPriv::MakeLargestInverted();
134 uint64_t fAtlasGeneration{GrDrawOpAtlas::kInvalidAtlasGeneration};
Herb Derby5bf5b042019-12-12 16:37:03 -0500135 SkPoint fCurrentOrigin;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500136 SkMatrix fCurrentMatrix;
Herb Derbye3c7ff42019-12-10 17:49:28 -0500137 std::vector<PathGlyph> fPaths;
138}; // SubRun
139
Herb Derbya9047642019-12-06 12:12:11 -0500140GrTextBlob::SubRun::SubRun(SubRunType type, GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec,
Herb Derbyc514e7d2019-12-11 17:00:31 -0500141 GrMaskFormat format,
142 const SkSpan<GrGlyph*>& glyphs, const SkSpan<char>& vertexData,
Herb Derbya9047642019-12-06 12:12:11 -0500143 sk_sp<GrTextStrike>&& grStrike)
144 : fType{type}
145 , fBlob{textBlob}
146 , fMaskFormat{format}
Herb Derbyc514e7d2019-12-11 17:00:31 -0500147 , fGlyphs{glyphs}
148 , fVertexData{vertexData}
Herb Derbya9047642019-12-06 12:12:11 -0500149 , fStrikeSpec{strikeSpec}
150 , fStrike{grStrike}
151 , fColor{textBlob->fColor}
Herb Derby5bf5b042019-12-12 16:37:03 -0500152 , fCurrentOrigin{textBlob->fInitialOrigin}
Herb Derby1c5be7b2019-12-13 12:03:06 -0500153 , fCurrentMatrix{textBlob->fInitialMatrix} {
Herb Derbya9047642019-12-06 12:12:11 -0500154 SkASSERT(type != kTransformedPath);
Herb Derbycb718892019-12-07 00:07:42 -0500155 textBlob->insertSubRun(this);
Herb Derbya9047642019-12-06 12:12:11 -0500156}
157
158GrTextBlob::SubRun::SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec)
159 : fType{kTransformedPath}
160 , fBlob{textBlob}
161 , fMaskFormat{kA8_GrMaskFormat}
Herb Derbyc514e7d2019-12-11 17:00:31 -0500162 , fGlyphs{SkSpan<GrGlyph*>{}}
163 , fVertexData{SkSpan<char>{}}
Herb Derbya9047642019-12-06 12:12:11 -0500164 , fStrikeSpec{strikeSpec}
165 , fStrike{nullptr}
166 , fColor{textBlob->fColor}
Herb Derbycb718892019-12-07 00:07:42 -0500167 , fPaths{} {
168 textBlob->insertSubRun(this);
169}
Herb Derbya9047642019-12-06 12:12:11 -0500170
171void GrTextBlob::SubRun::appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables) {
172 GrTextStrike* grStrike = fStrike.get();
173 SkScalar strikeToSource = fStrikeSpec.strikeToSourceRatio();
Herb Derbyc514e7d2019-12-11 17:00:31 -0500174 GrGlyph** glyphCursor = fGlyphs.data();
175 char* vertexCursor = fVertexData.data();
Herb Derbya9047642019-12-06 12:12:11 -0500176 GrColor color = this->color();
Herb Derbya2d72252019-12-23 15:02:33 -0500177 size_t vertexStride = this->vertexStride();
Herb Derbya9047642019-12-06 12:12:11 -0500178 // We always write the third position component used by SDFs. If it is unused it gets
179 // overwritten. Similarly, we always write the color and the blob will later overwrite it
180 // with texture coords if it is unused.
Herb Derby3d3150c2019-12-23 15:26:44 -0500181 size_t colorOffset = this->colorOffset();
Herb Derbya9047642019-12-06 12:12:11 -0500182 for (auto [variant, pos] : drawables) {
183 SkGlyph* skGlyph = variant;
184 GrGlyph* grGlyph = grStrike->getGlyph(*skGlyph);
185 // Only floor the device coordinates.
186 SkRect dstRect;
187 if (!this->needsTransform()) {
188 pos = {SkScalarFloorToScalar(pos.x()), SkScalarFloorToScalar(pos.y())};
189 dstRect = grGlyph->destRect(pos);
190 } else {
191 dstRect = grGlyph->destRect(pos, strikeToSource);
192 }
193
194 this->joinGlyphBounds(dstRect);
195
Herb Derbya9047642019-12-06 12:12:11 -0500196 // V0
Herb Derbyc514e7d2019-12-11 17:00:31 -0500197 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fTop, 1.f};
198 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color;
199 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500200
201 // V1
Herb Derbyc514e7d2019-12-11 17:00:31 -0500202 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fBottom, 1.f};
203 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color;
204 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500205
206 // V2
Herb Derbyc514e7d2019-12-11 17:00:31 -0500207 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fTop, 1.f};
208 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color;
209 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500210
211 // V3
Herb Derbyc514e7d2019-12-11 17:00:31 -0500212 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fBottom, 1.f};
213 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color;
214 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500215
Herb Derbyc514e7d2019-12-11 17:00:31 -0500216 *glyphCursor++ = grGlyph;
Herb Derbya9047642019-12-06 12:12:11 -0500217 }
Herb Derbya9047642019-12-06 12:12:11 -0500218}
219
220void GrTextBlob::SubRun::resetBulkUseToken() { fBulkUseToken.reset(); }
221
222GrDrawOpAtlas::BulkUseTokenUpdater* GrTextBlob::SubRun::bulkUseToken() { return &fBulkUseToken; }
223void GrTextBlob::SubRun::setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
224GrTextStrike* GrTextBlob::SubRun::strike() const { return fStrike.get(); }
Herb Derbya9047642019-12-06 12:12:11 -0500225void GrTextBlob::SubRun::setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
226uint64_t GrTextBlob::SubRun::atlasGeneration() const { return fAtlasGeneration; }
Herb Derbya9047642019-12-06 12:12:11 -0500227void GrTextBlob::SubRun::setColor(GrColor color) { fColor = color; }
228GrColor GrTextBlob::SubRun::color() const { return fColor; }
229GrMaskFormat GrTextBlob::SubRun::maskFormat() const { return fMaskFormat; }
Herb Derbya2d72252019-12-23 15:02:33 -0500230size_t GrTextBlob::SubRun::vertexStride() const {
231 return GetVertexStride(this->maskFormat(), this->hasW());
232}
Herb Derby3d3150c2019-12-23 15:26:44 -0500233size_t GrTextBlob::SubRun::colorOffset() const {
234 return this->hasW() ? offsetof(SDFT3DVertex, color) : offsetof(Mask2DVertex, color);
235}
Herb Derbya2d72252019-12-23 15:02:33 -0500236
Herb Derbya9047642019-12-06 12:12:11 -0500237const SkRect& GrTextBlob::SubRun::vertexBounds() const { return fVertexBounds; }
238void GrTextBlob::SubRun::joinGlyphBounds(const SkRect& glyphBounds) {
239 fVertexBounds.joinNonEmptyArg(glyphBounds);
240}
241
Herb Derby5bf5b042019-12-12 16:37:03 -0500242SkVector GrTextBlob::SubRun::computeTranslation(
243 const SkMatrix& drawMatrix, SkPoint drawOrigin){
Herb Derbya9047642019-12-06 12:12:11 -0500244 // Don't use the matrix to translate on distance field for fallback subruns.
Herb Derby5bf5b042019-12-12 16:37:03 -0500245
246 SkVector translate = calculate_translation(
247 !this->drawAsDistanceFields() && !this->needsTransform(),
248 drawMatrix, drawOrigin, fCurrentMatrix, fCurrentOrigin);
249
250 // Update SubRun indicating that the vertices now correspond to the origin and matrix used in
251 // the draw.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500252 fCurrentMatrix = drawMatrix;
Herb Derby5bf5b042019-12-12 16:37:03 -0500253 fCurrentOrigin = drawOrigin;
254 return translate;
Herb Derbya9047642019-12-06 12:12:11 -0500255}
256
257bool GrTextBlob::SubRun::drawAsDistanceFields() const { return fType == kTransformedSDFT; }
258
259bool GrTextBlob::SubRun::drawAsPaths() const { return fType == kTransformedPath; }
260
261bool GrTextBlob::SubRun::needsTransform() const {
262 return fType == kTransformedPath
263 || fType == kTransformedMask
264 || fType == kTransformedSDFT;
265}
266
267bool GrTextBlob::SubRun::hasW() const {
268 return fBlob->hasW(fType);
269}
270
271void GrTextBlob::SubRun::setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
272bool GrTextBlob::SubRun::hasUseLCDText() const { return fFlags.useLCDText; }
273void GrTextBlob::SubRun::setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
274bool GrTextBlob::SubRun::isAntiAliased() const { return fFlags.antiAliased; }
275const SkStrikeSpec& GrTextBlob::SubRun::strikeSpec() const { return fStrikeSpec; }
276
277// -- GrTextBlob -----------------------------------------------------------------------------------
278void GrTextBlob::operator delete(void* p) { ::operator delete(p); }
279void* GrTextBlob::operator new(size_t) { SK_ABORT("All blobs are created by placement new."); }
280void* GrTextBlob::operator new(size_t, void* p) { return p; }
281
282GrTextBlob::~GrTextBlob() = default;
283
Herb Derby659e4092019-12-06 15:38:10 -0500284sk_sp<GrTextBlob> GrTextBlob::Make(const SkGlyphRunList& glyphRunList,
Herb Derbyeba195f2019-12-03 16:44:47 -0500285 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500286 const SkMatrix& drawMatrix,
Herb Derbya00da612019-03-04 17:10:01 -0500287 GrColor color,
Herb Derbyeba195f2019-12-03 16:44:47 -0500288 bool forceWForDistanceFields) {
Herb Derbycd498e12019-12-06 14:17:31 -0500289
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500290 static_assert(sizeof(ARGB2DVertex) <= sizeof(Mask2DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500291 static_assert(alignof(ARGB2DVertex) <= alignof(Mask2DVertex));
292 size_t quadSize = sizeof(Mask2DVertex) * kVerticesPerGlyph;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500293 if (drawMatrix.hasPerspective() || forceWForDistanceFields) {
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500294 static_assert(sizeof(ARGB3DVertex) <= sizeof(SDFT3DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500295 static_assert(alignof(ARGB3DVertex) <= alignof(SDFT3DVertex));
296 quadSize = sizeof(SDFT3DVertex) * kVerticesPerGlyph;
Herb Derbye74c7762019-12-04 14:15:41 -0500297 }
298
Herb Derbycb718892019-12-07 00:07:42 -0500299 // We can use the alignment of SDFT3DVertex as a proxy for all Vertex alignments.
300 static_assert(alignof(SDFT3DVertex) >= alignof(Mask2DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500301 // Assume there is no padding needed between glyph pointers and vertices.
Herb Derbycb718892019-12-07 00:07:42 -0500302 static_assert(alignof(GrGlyph*) >= alignof(SDFT3DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500303
304 // In the arena, the layout is GrGlyph*... | SDFT3DVertex... | SubRun, so there is no padding
305 // between GrGlyph* and SDFT3DVertex, but padding is needed between the Mask2DVertex array
306 // and the SubRun.
307 size_t vertexToSubRunPadding = alignof(SDFT3DVertex) - alignof(SubRun);
308 size_t arenaSize =
309 sizeof(GrGlyph*) * glyphRunList.totalGlyphCount()
310 + quadSize * glyphRunList.totalGlyphCount()
311 + glyphRunList.runCount() * (sizeof(SubRun) + vertexToSubRunPadding);
312
313 size_t allocationSize = sizeof(GrTextBlob) + arenaSize;
Ben Wagner75d6db72018-09-20 14:39:39 -0400314
Herb Derbycb718892019-12-07 00:07:42 -0500315 void* allocation = ::operator new (allocationSize);
Herb Derbyb12175f2018-05-23 16:38:09 -0400316
Herb Derbyaebc5f82019-12-10 14:07:10 -0500317 SkColor initialLuminance = SkPaintPriv::ComputeLuminanceColor(glyphRunList.paint());
Herb Derby00ae9592019-12-03 15:55:56 -0500318 sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{
Herb Derby1c5be7b2019-12-13 12:03:06 -0500319 arenaSize, strikeCache, drawMatrix, glyphRunList.origin(),
Herb Derbyaebc5f82019-12-10 14:07:10 -0500320 color, initialLuminance, forceWForDistanceFields}};
joshualitt92303772016-02-10 11:55:52 -0800321
Herb Derbyf7d5d742018-11-16 13:24:32 -0500322 return blob;
joshualitt92303772016-02-10 11:55:52 -0800323}
324
Herb Derbya9047642019-12-06 12:12:11 -0500325void GrTextBlob::setupKey(const GrTextBlob::Key& key, const SkMaskFilterBase::BlurRec& blurRec,
326 const SkPaint& paint) {
327 fKey = key;
328 if (key.fHasBlur) {
329 fBlurRec = blurRec;
330 }
331 if (key.fStyle != SkPaint::kFill_Style) {
332 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
333 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
334 fStrokeInfo.fJoin = paint.getStrokeJoin();
335 }
336}
337const GrTextBlob::Key& GrTextBlob::GetKey(const GrTextBlob& blob) { return blob.fKey; }
338uint32_t GrTextBlob::Hash(const GrTextBlob::Key& key) { return SkOpts::hash(&key, sizeof(Key)); }
339
Herb Derbycb718892019-12-07 00:07:42 -0500340bool GrTextBlob::hasDistanceField() const {
341 return SkToBool(fTextType & kHasDistanceField_TextType);
342}
Herb Derbya9047642019-12-06 12:12:11 -0500343bool GrTextBlob::hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
Herb Derby1c5be7b2019-12-13 12:03:06 -0500344bool GrTextBlob::hasPerspective() const { return fInitialMatrix.hasPerspective(); }
Herb Derbya9047642019-12-06 12:12:11 -0500345
346void GrTextBlob::setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
347void GrTextBlob::setHasBitmap() { fTextType |= kHasBitmap_TextType; }
348void GrTextBlob::setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) {
349 // we init fMaxMinScale and fMinMaxScale in the constructor
350 fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale);
351 fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale);
352}
353
354size_t GrTextBlob::GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
355 switch (maskFormat) {
356 case kA8_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500357 return hasWCoord ? sizeof(SDFT3DVertex) : sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500358 case kARGB_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500359 return hasWCoord ? sizeof(ARGB3DVertex) : sizeof(ARGB2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500360 default:
361 SkASSERT(!hasWCoord);
Herb Derbycd498e12019-12-06 14:17:31 -0500362 return sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500363 }
364}
365
Herb Derby0edb2142018-10-16 17:04:11 -0400366bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
367 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derby5bf5b042019-12-12 16:37:03 -0500368 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
joshualittfd5f6c12015-12-10 07:44:50 -0800369 // If we have LCD text then our canonical color will be set to transparent, in this case we have
370 // to regenerate the blob on any color change
371 // We use the grPaint to get any color filter effects
372 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbyaebc5f82019-12-10 14:07:10 -0500373 fInitialLuminance != SkPaintPriv::ComputeLuminanceColor(paint)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800374 return true;
375 }
376
Herb Derby1c5be7b2019-12-13 12:03:06 -0500377 if (fInitialMatrix.hasPerspective() != drawMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800378 return true;
379 }
380
Brian Salomon5c6ac642017-12-19 11:09:32 -0500381 /** This could be relaxed for blobs with only distance field glyphs. */
Mike Reed2c383152019-12-18 16:47:47 -0500382 if (fInitialMatrix.hasPerspective() && !SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800383 return true;
384 }
385
386 // We only cache one masked version
387 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400388 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800389 return true;
390 }
391
392 // Similarly, we only cache one version for each style
393 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400394 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
395 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
396 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800397 return true;
398 }
399
400 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
401 // for mixed blobs if this becomes an issue.
402 if (this->hasBitmap() && this->hasDistanceField()) {
Herb Derbyeba195f2019-12-03 16:44:47 -0500403 // Identical view matrices and we can reuse in all cases
Mike Reed2c383152019-12-18 16:47:47 -0500404 return !(SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix) &&
405 drawOrigin == fInitialOrigin);
joshualittfd5f6c12015-12-10 07:44:50 -0800406 }
407
408 if (this->hasBitmap()) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500409 if (fInitialMatrix.getScaleX() != drawMatrix.getScaleX() ||
410 fInitialMatrix.getScaleY() != drawMatrix.getScaleY() ||
411 fInitialMatrix.getSkewX() != drawMatrix.getSkewX() ||
412 fInitialMatrix.getSkewY() != drawMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800413 return true;
414 }
415
Herb Derby9830fc42019-09-12 10:58:26 -0400416 // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust
417 // the quads properly. Devise a system that regenerates the quads from original data
418 // using the transform to allow this to be used in general.
419
420 // We can update the positions in the text blob without regenerating the whole
421 // blob, but only for integer translations.
422 // This cool bit of math will determine the necessary translation to apply to the
423 // already generated vertex coordinates to move them to the correct position.
424 // Figure out the translation in view space given a translation in source space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500425 SkScalar transX = drawMatrix.getTranslateX() +
Herb Derby5bf5b042019-12-12 16:37:03 -0500426 drawMatrix.getScaleX() * (drawOrigin.x() - fInitialOrigin.x()) +
427 drawMatrix.getSkewX() * (drawOrigin.y() - fInitialOrigin.y()) -
Herb Derby1c5be7b2019-12-13 12:03:06 -0500428 fInitialMatrix.getTranslateX();
429 SkScalar transY = drawMatrix.getTranslateY() +
Herb Derby5bf5b042019-12-12 16:37:03 -0500430 drawMatrix.getSkewY() * (drawOrigin.x() - fInitialOrigin.x()) +
431 drawMatrix.getScaleY() * (drawOrigin.y() - fInitialOrigin.y()) -
Herb Derby1c5be7b2019-12-13 12:03:06 -0500432 fInitialMatrix.getTranslateY();
Herb Derby9830fc42019-09-12 10:58:26 -0400433 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
434 return true;
joshualittfd5f6c12015-12-10 07:44:50 -0800435 }
joshualittfd5f6c12015-12-10 07:44:50 -0800436 } else if (this->hasDistanceField()) {
437 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
438 // distance field being generated, so we have to regenerate in those cases
Herb Derby1c5be7b2019-12-13 12:03:06 -0500439 SkScalar newMaxScale = drawMatrix.getMaxScale();
440 SkScalar oldMaxScale = fInitialMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800441 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
442 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
443 return true;
444 }
joshualittfd5f6c12015-12-10 07:44:50 -0800445 }
446
joshualittfd5f6c12015-12-10 07:44:50 -0800447 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
448 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
449 // the blob anyways at flush time, so no need to regenerate explicitly
450 return false;
451}
452
Herb Derbyc1b482c2018-08-09 15:02:27 -0400453void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
454 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400455 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Herb Derby5bf5b042019-12-12 16:37:03 -0500456 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500457
Herb Derbycb718892019-12-07 00:07:42 -0500458 for (SubRun* subRun = fFirstSubRun; subRun != nullptr; subRun = subRun->fNextSubRun) {
459 if (subRun->drawAsPaths()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400460 SkPaint runPaint{paint};
Herb Derbycb718892019-12-07 00:07:42 -0500461 runPaint.setAntiAlias(subRun->isAntiAliased());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500462 // If there are shaders, blurs or styles, the path must be scaled into source
463 // space independently of the CTM. This allows the CTM to be correct for the
464 // different effects.
465 GrStyle style(runPaint);
Herb Derby9f491482018-08-08 11:53:00 -0400466
Herb Derby1b8dcd12019-11-15 15:21:15 -0500467 bool scalePath = runPaint.getShader()
468 || style.applies()
469 || runPaint.getMaskFilter();
Robert Phillips137ca522018-08-15 10:14:33 -0400470
Herb Derby1b8dcd12019-11-15 15:21:15 -0500471 // The origin for the blob may have changed, so figure out the delta.
Herb Derby5bf5b042019-12-12 16:37:03 -0500472 SkVector originShift = drawOrigin - fInitialOrigin;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500473
Herb Derbycb718892019-12-07 00:07:42 -0500474 for (const auto& pathGlyph : subRun->fPaths) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500475 SkMatrix ctm{drawMatrix};
Herb Derbycb718892019-12-07 00:07:42 -0500476 SkMatrix pathMatrix = SkMatrix::MakeScale(
477 subRun->fStrikeSpec.strikeToSourceRatio());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500478 // Shift the original glyph location in source space to the position of the new
479 // blob.
480 pathMatrix.postTranslate(originShift.x() + pathGlyph.fOrigin.x(),
481 originShift.y() + pathGlyph.fOrigin.y());
Herb Derbydac1ed52018-09-12 17:04:21 -0400482
483 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400484 SkTLazy<SkPath> tmpPath;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500485 const SkPath* path = &pathGlyph.fPath;
Herb Derby2984d262019-11-20 14:40:39 -0500486 if (!scalePath) {
487 // Scale can be applied to CTM -- no effects.
Herb Derby2984d262019-11-20 14:40:39 -0500488 ctm.preConcat(pathMatrix);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400489 } else {
Herb Derby2984d262019-11-20 14:40:39 -0500490 // Scale the outline into source space.
Herb Derbydac1ed52018-09-12 17:04:21 -0400491
Herb Derby2984d262019-11-20 14:40:39 -0500492 // Transform the path form the normalized outline to source space. This
493 // way the CTM will remain the same so it can be used by the effects.
494 SkPath* sourceOutline = tmpPath.init();
495 path->transform(pathMatrix, sourceOutline);
496 sourceOutline->setIsVolatile(true);
497 path = sourceOutline;
Robert Phillips137ca522018-08-15 10:14:33 -0400498 }
499
Robert Phillips46a13382018-08-23 13:53:01 -0400500 // TODO: we are losing the mutability of the path here
501 GrShape shape(*path, paint);
Herb Derbydac1ed52018-09-12 17:04:21 -0400502 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500503 }
Herb Derby1b8dcd12019-11-15 15:21:15 -0500504 } else {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500505 int glyphCount = subRun->fGlyphs.size();
Jim Van Verth54d9c882018-02-08 16:14:48 -0500506 if (0 == glyphCount) {
507 continue;
508 }
509
510 bool skipClip = false;
511 bool submitOp = true;
512 SkIRect clipRect = SkIRect::MakeEmpty();
513 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
514 SkRRect clipRRect;
515 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400516 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500517 // and we have an axis-aligned rectangular non-AA clip
Herb Derbycb718892019-12-07 00:07:42 -0500518 if (!subRun->drawAsDistanceFields() && !subRun->needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500519 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500520 clipRRect.isRect() && GrAA::kNo == aa) {
521 skipClip = true;
522 // We only need to do clipping work if the subrun isn't contained by the clip
523 SkRect subRunBounds;
Herb Derby5bf5b042019-12-12 16:37:03 -0500524 this->computeSubRunBounds(
525 &subRunBounds, *subRun, drawMatrix, drawOrigin, false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500526 if (!clipRRect.getBounds().contains(subRunBounds)) {
527 // If the subrun is completely outside, don't add an op for it
528 if (!clipRRect.getBounds().intersects(subRunBounds)) {
529 submitOp = false;
530 }
531 else {
532 clipRRect.getBounds().round(&clipRect);
533 }
534 }
535 }
536
537 if (submitOp) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500538 auto op = this->makeOp(*subRun, glyphCount, drawMatrix, drawOrigin,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400539 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500540 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500541 if (op) {
542 if (skipClip) {
543 target->addDrawOp(GrNoClip(), std::move(op));
544 }
545 else {
546 target->addDrawOp(clip, std::move(op));
547 }
548 }
549 }
550 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000551 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000552}
553
Herb Derby1c5be7b2019-12-13 12:03:06 -0500554void GrTextBlob::computeSubRunBounds(SkRect* outBounds, const SubRun& subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500555 const SkMatrix& drawMatrix, SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500556 bool needsGlyphTransform) {
557 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
558 // into device space.
559 // We handle vertex bounds differently for distance field text and bitmap text because
560 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
561 // from one blob then we are going to pay the price here of mapping the rect for each run.
562 *outBounds = subRun.vertexBounds();
563 if (needsGlyphTransform) {
564 // Distance field text is positioned with the (X,Y) as part of the glyph position,
565 // and currently the view matrix is applied on the GPU
Herb Derby5bf5b042019-12-12 16:37:03 -0500566 outBounds->offset(drawOrigin - fInitialOrigin);
Herb Derby1c5be7b2019-12-13 12:03:06 -0500567 drawMatrix.mapRect(outBounds);
Herb Derbya9047642019-12-06 12:12:11 -0500568 } else {
569 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
570 // device space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500571 SkMatrix boundsMatrix = fInitialMatrixInverse;
Herb Derbya9047642019-12-06 12:12:11 -0500572
573 boundsMatrix.postTranslate(-fInitialOrigin.x(), -fInitialOrigin.y());
574
Herb Derby5bf5b042019-12-12 16:37:03 -0500575 boundsMatrix.postTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derbya9047642019-12-06 12:12:11 -0500576
Herb Derby1c5be7b2019-12-13 12:03:06 -0500577 boundsMatrix.postConcat(drawMatrix);
Herb Derbya9047642019-12-06 12:12:11 -0500578 boundsMatrix.mapRect(outBounds);
579
580 // Due to floating point numerical inaccuracies, we have to round out here
581 outBounds->roundOut(outBounds);
582 }
joshualitt323c2eb2016-01-20 06:48:47 -0800583}
joshualitt2e2202e2015-12-10 11:22:08 -0800584
Herb Derbya9047642019-12-06 12:12:11 -0500585const GrTextBlob::Key& GrTextBlob::key() const { return fKey; }
586size_t GrTextBlob::size() const { return fSize; }
587
588std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Herb Derby1c5be7b2019-12-13 12:03:06 -0500589 int glyphCount, const SkMatrix& drawMatrix,
Herb Derby5bf5b042019-12-12 16:37:03 -0500590 SkPoint drawOrigin, const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbya9047642019-12-06 12:12:11 -0500591 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
592 GrTextTarget* target) {
Herb Derbycb718892019-12-07 00:07:42 -0500593 SubRun* info = fFirstSubRun;
Herb Derbya9047642019-12-06 12:12:11 -0500594 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derby5bf5b042019-12-12 16:37:03 -0500595 return this->makeOp(*info, glyphCount, drawMatrix, drawOrigin, emptyRect,
Herb Derbya9047642019-12-06 12:12:11 -0500596 paint, filteredColor, props, distanceAdjustTable, target);
597}
598
599bool GrTextBlob::hasW(GrTextBlob::SubRunType type) const {
600 if (type == kTransformedSDFT) {
601 return this->hasPerspective() || fForceWForDistanceFields;
602 } else if (type == kTransformedMask || type == kTransformedPath) {
603 return this->hasPerspective();
Herb Derbye9f691d2019-12-04 12:11:13 -0500604 }
Herb Derbya9047642019-12-06 12:12:11 -0500605
606 // The viewMatrix is implicitly SkMatrix::I when drawing kDirectMask, because it is not
607 // used.
608 return false;
609}
610
611GrTextBlob::SubRun* GrTextBlob::makeSubRun(SubRunType type,
612 const SkZip<SkGlyphVariant, SkPoint>& drawables,
613 const SkStrikeSpec& strikeSpec,
614 GrMaskFormat format) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500615 SkSpan<GrGlyph*> glyphs{fAlloc.makeArrayDefault<GrGlyph*>(drawables.size()), drawables.size()};
Herb Derbya9047642019-12-06 12:12:11 -0500616 bool hasW = this->hasW(type);
Herb Derby3d3150c2019-12-23 15:26:44 -0500617
618 SkASSERT(!fInitialMatrix.hasPerspective() || hasW);
619
Herb Derbyc514e7d2019-12-11 17:00:31 -0500620 size_t vertexDataSize = drawables.size() * GetVertexStride(format, hasW) * kVerticesPerGlyph;
621 SkSpan<char> vertexData{fAlloc.makeArrayDefault<char>(vertexDataSize), vertexDataSize};
Herb Derbya9047642019-12-06 12:12:11 -0500622
623 sk_sp<GrTextStrike> grStrike = strikeSpec.findOrCreateGrStrike(fStrikeCache);
624
Herb Derbycb718892019-12-07 00:07:42 -0500625 SubRun* subRun = fAlloc.make<SubRun>(
Herb Derbyc514e7d2019-12-11 17:00:31 -0500626 type, this, strikeSpec, format, glyphs, vertexData, std::move(grStrike));
Herb Derbya9047642019-12-06 12:12:11 -0500627
Herb Derbycb718892019-12-07 00:07:42 -0500628 subRun->appendGlyphs(drawables);
Herb Derbya9047642019-12-06 12:12:11 -0500629
Herb Derbycb718892019-12-07 00:07:42 -0500630 return subRun;
Herb Derbya9047642019-12-06 12:12:11 -0500631}
632
633void GrTextBlob::addSingleMaskFormat(
634 SubRunType type,
635 const SkZip<SkGlyphVariant, SkPoint>& drawables,
636 const SkStrikeSpec& strikeSpec,
637 GrMaskFormat format) {
638 this->makeSubRun(type, drawables, strikeSpec, format);
639}
640
641void GrTextBlob::addMultiMaskFormat(
642 SubRunType type,
643 const SkZip<SkGlyphVariant, SkPoint>& drawables,
644 const SkStrikeSpec& strikeSpec) {
645 this->setHasBitmap();
646 if (drawables.empty()) { return; }
647
648 auto glyphSpan = drawables.get<0>();
649 SkGlyph* glyph = glyphSpan[0];
650 GrMaskFormat format = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
651 size_t startIndex = 0;
652 for (size_t i = 1; i < drawables.size(); i++) {
653 glyph = glyphSpan[i];
654 GrMaskFormat nextFormat = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
655 if (format != nextFormat) {
656 auto sameFormat = drawables.subspan(startIndex, i - startIndex);
657 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
658 format = nextFormat;
659 startIndex = i;
660 }
661 }
662 auto sameFormat = drawables.last(drawables.size() - startIndex);
663 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
664}
665
666void GrTextBlob::addSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
667 const SkStrikeSpec& strikeSpec,
668 const SkFont& runFont,
669 SkScalar minScale,
670 SkScalar maxScale) {
671 this->setHasDistanceField();
672 this->setMinAndMaxScale(minScale, maxScale);
673
674 SubRun* subRun = this->makeSubRun(kTransformedSDFT, drawables, strikeSpec, kA8_GrMaskFormat);
675 subRun->setUseLCDText(runFont.getEdging() == SkFont::Edging::kSubpixelAntiAlias);
676 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbye9f691d2019-12-04 12:11:13 -0500677}
678
Herb Derbycb718892019-12-07 00:07:42 -0500679GrTextBlob::GrTextBlob(size_t allocSize,
Herb Derbyeba195f2019-12-03 16:44:47 -0500680 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500681 const SkMatrix& drawMatrix,
Herb Derbyeba195f2019-12-03 16:44:47 -0500682 SkPoint origin,
683 GrColor color,
Herb Derbyaebc5f82019-12-10 14:07:10 -0500684 SkColor initialLuminance,
Herb Derby00ae9592019-12-03 15:55:56 -0500685 bool forceWForDistanceFields)
Herb Derbycb718892019-12-07 00:07:42 -0500686 : fSize{allocSize}
Herb Derby00ae9592019-12-03 15:55:56 -0500687 , fStrikeCache{strikeCache}
Herb Derby1c5be7b2019-12-13 12:03:06 -0500688 , fInitialMatrix{drawMatrix}
689 , fInitialMatrixInverse{make_inverse(drawMatrix)}
Herb Derbyeba195f2019-12-03 16:44:47 -0500690 , fInitialOrigin{origin}
Herb Derby00ae9592019-12-03 15:55:56 -0500691 , fForceWForDistanceFields{forceWForDistanceFields}
Herb Derbycb718892019-12-07 00:07:42 -0500692 , fColor{color}
Herb Derbyaebc5f82019-12-10 14:07:10 -0500693 , fInitialLuminance{initialLuminance}
Herb Derbycb718892019-12-07 00:07:42 -0500694 , fAlloc{SkTAddOffset<char>(this, sizeof(GrTextBlob)), allocSize, allocSize/2} { }
Herb Derby00ae9592019-12-03 15:55:56 -0500695
Herb Derbycb718892019-12-07 00:07:42 -0500696void GrTextBlob::insertSubRun(SubRun* subRun) {
697 if (fFirstSubRun == nullptr) {
698 fFirstSubRun = subRun;
699 fLastSubRun = subRun;
700 } else {
701 fLastSubRun->fNextSubRun = subRun;
702 fLastSubRun = subRun;
703 }
704}
705
706std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Herb Derbya9047642019-12-06 12:12:11 -0500707 SubRun& info, int glyphCount,
Herb Derby5bf5b042019-12-12 16:37:03 -0500708 const SkMatrix& drawMatrix, SkPoint drawOrigin, const SkIRect& clipRect,
Herb Derbya9047642019-12-06 12:12:11 -0500709 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
710 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
711 GrMaskFormat format = info.maskFormat();
712
713 GrPaint grPaint;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500714 target->makeGrPaint(info.maskFormat(), paint, drawMatrix, &grPaint);
Herb Derbya9047642019-12-06 12:12:11 -0500715 std::unique_ptr<GrAtlasTextOp> op;
716 if (info.drawAsDistanceFields()) {
717 // TODO: Can we be even smarter based on the dest transfer function?
718 op = GrAtlasTextOp::MakeDistanceField(
719 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
720 target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint),
721 props, info.isAntiAliased(), info.hasUseLCDText());
Herb Derbyeba195f2019-12-03 16:44:47 -0500722 } else {
Herb Derbya9047642019-12-06 12:12:11 -0500723 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
724 info.needsTransform());
725 }
726 GrAtlasTextOp::Geometry& geometry = op->geometry();
Herb Derby1c5be7b2019-12-13 12:03:06 -0500727 geometry.fDrawMatrix = drawMatrix;
Herb Derbya9047642019-12-06 12:12:11 -0500728 geometry.fClipRect = clipRect;
729 geometry.fBlob = SkRef(this);
730 geometry.fSubRunPtr = &info;
731 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
Herb Derby5bf5b042019-12-12 16:37:03 -0500732 geometry.fDrawOrigin = drawOrigin;
Herb Derbya9047642019-12-06 12:12:11 -0500733 op->init();
734 return op;
735}
Herb Derbyeba195f2019-12-03 16:44:47 -0500736
Herb Derbya9047642019-12-06 12:12:11 -0500737void GrTextBlob::processDeviceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
738 const SkStrikeSpec& strikeSpec) {
739 this->addMultiMaskFormat(kDirectMask, drawables, strikeSpec);
740}
Herb Derbyeba195f2019-12-03 16:44:47 -0500741
Herb Derbya9047642019-12-06 12:12:11 -0500742void GrTextBlob::processSourcePaths(const SkZip<SkGlyphVariant, SkPoint>& drawables,
743 const SkFont& runFont,
744 const SkStrikeSpec& strikeSpec) {
745 this->setHasBitmap();
Herb Derbycb718892019-12-07 00:07:42 -0500746 SubRun* subRun = fAlloc.make<SubRun>(this, strikeSpec);
747 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbya9047642019-12-06 12:12:11 -0500748 for (auto [variant, pos] : drawables) {
Herb Derbycb718892019-12-07 00:07:42 -0500749 subRun->fPaths.emplace_back(*variant.path(), pos);
Herb Derbyeba195f2019-12-03 16:44:47 -0500750 }
751}
752
Herb Derbya9047642019-12-06 12:12:11 -0500753void GrTextBlob::processSourceSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
754 const SkStrikeSpec& strikeSpec,
755 const SkFont& runFont,
756 SkScalar minScale,
757 SkScalar maxScale) {
758 this->addSDFT(drawables, strikeSpec, runFont, minScale, maxScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800759}
Herb Derbya9047642019-12-06 12:12:11 -0500760
761void GrTextBlob::processSourceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
762 const SkStrikeSpec& strikeSpec) {
763 this->addMultiMaskFormat(kTransformedMask, drawables, strikeSpec);
764}
765
766// -- GrTextBlob::VertexRegenerator ----------------------------------------------------------------
Herb Derby5bf5b042019-12-12 16:37:03 -0500767static void regen_positions(char* vertex, size_t vertexStride, SkVector translation) {
Herb Derbya9047642019-12-06 12:12:11 -0500768 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
769 for (int i = 0; i < 4; ++i) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500770 *point += translation;
Herb Derbya9047642019-12-06 12:12:11 -0500771 point = SkTAddOffset<SkPoint>(point, vertexStride);
772 }
773}
774
775static void regen_colors(char* vertex, size_t vertexStride, GrColor color) {
776 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
777 // vertices, hence vertexStride - sizeof(SkIPoint16)
778 size_t colorOffset = vertexStride - sizeof(SkIPoint16) - sizeof(GrColor);
779 GrColor* vcolor = reinterpret_cast<GrColor*>(vertex + colorOffset);
780 for (int i = 0; i < 4; ++i) {
781 *vcolor = color;
782 vcolor = SkTAddOffset<GrColor>(vcolor, vertexStride);
783 }
784}
785
786static void regen_texcoords(char* vertex, size_t vertexStride, const GrGlyph* glyph,
787 bool useDistanceFields) {
788 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
789 // vertices, hence vertexStride - sizeof(SkIPoint16)
790 size_t texCoordOffset = vertexStride - sizeof(SkIPoint16);
791
792 uint16_t u0, v0, u1, v1;
793 SkASSERT(glyph);
794 int width = glyph->fBounds.width();
795 int height = glyph->fBounds.height();
796
797 if (useDistanceFields) {
798 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
799 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
800 u1 = u0 + width - 2 * SK_DistanceFieldInset;
801 v1 = v0 + height - 2 * SK_DistanceFieldInset;
802 } else {
803 u0 = glyph->fAtlasLocation.fX;
804 v0 = glyph->fAtlasLocation.fY;
805 u1 = u0 + width;
806 v1 = v0 + height;
807 }
808 // We pack the 2bit page index in the low bit of the u and v texture coords
809 uint32_t pageIndex = glyph->pageIndex();
810 SkASSERT(pageIndex < 4);
811 uint16_t uBit = (pageIndex >> 1) & 0x1;
812 uint16_t vBit = pageIndex & 0x1;
813 u0 <<= 1;
814 u0 |= uBit;
815 v0 <<= 1;
816 v0 |= vBit;
817 u1 <<= 1;
818 u1 |= uBit;
819 v1 <<= 1;
820 v1 |= vBit;
821
822 uint16_t* textureCoords = reinterpret_cast<uint16_t*>(vertex + texCoordOffset);
823 textureCoords[0] = u0;
824 textureCoords[1] = v0;
825 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
826 textureCoords[0] = u0;
827 textureCoords[1] = v1;
828 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
829 textureCoords[0] = u1;
830 textureCoords[1] = v0;
831 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
832 textureCoords[0] = u1;
833 textureCoords[1] = v1;
834
835#ifdef DISPLAY_PAGE_INDEX
836 // Enable this to visualize the page from which each glyph is being drawn.
837 // Green Red Magenta Cyan -> 0 1 2 3; Black -> error
838 GrColor hackColor;
839 switch (pageIndex) {
840 case 0:
841 hackColor = GrColorPackRGBA(0, 255, 0, 255);
842 break;
843 case 1:
844 hackColor = GrColorPackRGBA(255, 0, 0, 255);;
845 break;
846 case 2:
847 hackColor = GrColorPackRGBA(255, 0, 255, 255);
848 break;
849 case 3:
850 hackColor = GrColorPackRGBA(0, 255, 255, 255);
851 break;
852 default:
853 hackColor = GrColorPackRGBA(0, 0, 0, 255);
854 break;
855 }
856 regen_colors(vertex, vertexStride, hackColor);
857#endif
858}
859
860GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
Herb Derbya9047642019-12-06 12:12:11 -0500861 GrTextBlob::SubRun* subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500862 const SkMatrix& drawMatrix,
863 SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500864 GrColor color,
865 GrDeferredUploadTarget* uploadTarget,
866 GrStrikeCache* grStrikeCache,
867 GrAtlasManager* fullAtlasManager)
868 : fResourceProvider(resourceProvider)
Herb Derby1c5be7b2019-12-13 12:03:06 -0500869 , fDrawMatrix(drawMatrix)
Herb Derbya9047642019-12-06 12:12:11 -0500870 , fUploadTarget(uploadTarget)
871 , fGrStrikeCache(grStrikeCache)
872 , fFullAtlasManager(fullAtlasManager)
873 , fSubRun(subRun)
874 , fColor(color) {
875 // Compute translation if any
Herb Derby5bf5b042019-12-12 16:37:03 -0500876 fDrawTranslation = fSubRun->computeTranslation(fDrawMatrix, drawOrigin);
Herb Derbya9047642019-12-06 12:12:11 -0500877
878 // Because the GrStrikeCache may evict the strike a blob depends on using for
879 // generating its texture coords, we have to track whether or not the strike has
880 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
881 // otherwise we have to get the new strike, and use that to get the correct glyphs.
882 // Because we do not have the packed ids, and thus can't look up our glyphs in the
883 // new strike, we instead keep our ref to the old strike and use the packed ids from
884 // it. These ids will still be valid as long as we hold the ref. When we are done
885 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
Herb Derby73630212019-12-13 16:29:14 -0500886 fActions.regenTextureCoordinates = fSubRun->strike()->isAbandoned();
887 fActions.regenStrike = fSubRun->strike()->isAbandoned();
888 fActions.regenColor = kARGB_GrMaskFormat != fSubRun->maskFormat() && fSubRun->color() != color;
889 fActions.regenPositions = fDrawTranslation.x() != 0.f || fDrawTranslation.y() != 0.f;
Herb Derbya9047642019-12-06 12:12:11 -0500890}
891
Herb Derby73630212019-12-13 16:29:14 -0500892bool GrTextBlob::VertexRegenerator::doRegen(GrTextBlob::VertexRegenerator::Result* result) {
893 SkASSERT(!fActions.regenStrike || fActions.regenTextureCoordinates);
894 if (fActions.regenTextureCoordinates) {
Herb Derbya9047642019-12-06 12:12:11 -0500895 fSubRun->resetBulkUseToken();
896
897 const SkStrikeSpec& strikeSpec = fSubRun->strikeSpec();
898
899 if (!fMetricsAndImages.isValid()
900 || fMetricsAndImages->descriptor() != strikeSpec.descriptor()) {
901 fMetricsAndImages.init(strikeSpec);
902 }
903
Herb Derby73630212019-12-13 16:29:14 -0500904 if (fActions.regenStrike) {
Herb Derbya9047642019-12-06 12:12:11 -0500905 // Take the glyphs from the old strike, and translate them a new strike.
906 sk_sp<GrTextStrike> newStrike = strikeSpec.findOrCreateGrStrike(fGrStrikeCache);
907
908 // Start this batch at the start of the subRun plus any glyphs that were previously
909 // processed.
Herb Derbyc514e7d2019-12-11 17:00:31 -0500910 SkSpan<GrGlyph*> glyphs = fSubRun->fGlyphs.last(fSubRun->fGlyphs.size() - fCurrGlyph);
Herb Derbya9047642019-12-06 12:12:11 -0500911
912 // Convert old glyphs to newStrike.
913 for (auto& glyph : glyphs) {
914 SkPackedGlyphID id = glyph->fPackedID;
915 glyph = newStrike->getGlyph(id, fMetricsAndImages.get());
916 SkASSERT(id == glyph->fPackedID);
917 }
918
919 fSubRun->setStrike(newStrike);
920 }
921 }
922
Herb Derby7cf4a2e2019-12-23 14:51:55 -0500923 GrTextStrike* grStrike = fSubRun->strike();
Herb Derbya2d72252019-12-23 15:02:33 -0500924 auto vertexStride = fSubRun->vertexStride();
Herb Derbyc514e7d2019-12-11 17:00:31 -0500925 char* currVertex = fSubRun->fVertexData.data() + fCurrGlyph * kVerticesPerGlyph * vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500926 result->fFirstVertex = currVertex;
927
Herb Derbyc514e7d2019-12-11 17:00:31 -0500928 for (int glyphIdx = fCurrGlyph; glyphIdx < (int)fSubRun->fGlyphs.size(); glyphIdx++) {
Herb Derbya9047642019-12-06 12:12:11 -0500929 GrGlyph* glyph = nullptr;
Herb Derby73630212019-12-13 16:29:14 -0500930 if (fActions.regenTextureCoordinates) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500931 glyph = fSubRun->fGlyphs[glyphIdx];
Herb Derbya9047642019-12-06 12:12:11 -0500932 SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
933
934 if (!fFullAtlasManager->hasGlyph(glyph)) {
Herb Derby7cf4a2e2019-12-23 14:51:55 -0500935 GrDrawOpAtlas::ErrorCode code = grStrike->addGlyphToAtlas(
936 fResourceProvider, fUploadTarget, fGrStrikeCache, fFullAtlasManager, glyph,
937 fMetricsAndImages.get(), fSubRun->maskFormat(), fSubRun->needsTransform());
Herb Derbya9047642019-12-06 12:12:11 -0500938 if (GrDrawOpAtlas::ErrorCode::kError == code) {
939 // Something horrible has happened - drop the op
940 return false;
941 }
942 else if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
943 fBrokenRun = glyphIdx > 0;
944 result->fFinished = false;
945 return true;
946 }
947 }
948 auto tokenTracker = fUploadTarget->tokenTracker();
949 fFullAtlasManager->addGlyphToBulkAndSetUseToken(fSubRun->bulkUseToken(), glyph,
950 tokenTracker->nextDrawToken());
951 }
952
Herb Derby73630212019-12-13 16:29:14 -0500953 if (fActions.regenPositions) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500954 regen_positions(currVertex, vertexStride, fDrawTranslation);
Herb Derbya9047642019-12-06 12:12:11 -0500955 }
Herb Derby73630212019-12-13 16:29:14 -0500956 if (fActions.regenColor) {
Herb Derbya9047642019-12-06 12:12:11 -0500957 regen_colors(currVertex, vertexStride, fColor);
958 }
Herb Derby73630212019-12-13 16:29:14 -0500959 if (fActions.regenTextureCoordinates) {
Herb Derbya9047642019-12-06 12:12:11 -0500960 regen_texcoords(currVertex, vertexStride, glyph, fSubRun->drawAsDistanceFields());
961 }
962
963 currVertex += vertexStride * GrAtlasTextOp::kVerticesPerGlyph;
964 ++result->fGlyphsRegenerated;
965 ++fCurrGlyph;
966 }
967
968 // We may have changed the color so update it here
969 fSubRun->setColor(fColor);
Herb Derby73630212019-12-13 16:29:14 -0500970 if (fActions.regenTextureCoordinates) {
Herb Derbya9047642019-12-06 12:12:11 -0500971 fSubRun->setAtlasGeneration(fBrokenRun
972 ? GrDrawOpAtlas::kInvalidAtlasGeneration
973 : fFullAtlasManager->atlasGeneration(fSubRun->maskFormat()));
974 } else {
975 // For the non-texCoords case we need to ensure that we update the associated use tokens
976 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
977 fUploadTarget->tokenTracker()->nextDrawToken(),
978 fSubRun->maskFormat());
979 }
980 return true;
981}
982
983bool GrTextBlob::VertexRegenerator::regenerate(GrTextBlob::VertexRegenerator::Result* result) {
984 uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
985 // If regenerate() is called multiple times then the atlas gen may have changed. So we check
986 // this each time.
Herb Derby73630212019-12-13 16:29:14 -0500987 fActions.regenTextureCoordinates |= fSubRun->atlasGeneration() != currentAtlasGen;
Herb Derbya9047642019-12-06 12:12:11 -0500988
Herb Derby73630212019-12-13 16:29:14 -0500989 if (fActions.regenStrike
990 |fActions.regenTextureCoordinates
991 |fActions.regenColor
992 |fActions.regenPositions) {
993 return this->doRegen(result);
Herb Derbya9047642019-12-06 12:12:11 -0500994 } else {
Herb Derbya2d72252019-12-23 15:02:33 -0500995 auto vertexStride = fSubRun->vertexStride();
Herb Derbya9047642019-12-06 12:12:11 -0500996 result->fFinished = true;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500997 result->fGlyphsRegenerated = fSubRun->fGlyphs.size() - fCurrGlyph;
998 result->fFirstVertex = fSubRun->fVertexData.data() +
999 fCurrGlyph * kVerticesPerGlyph * vertexStride;
1000 fCurrGlyph = fSubRun->fGlyphs.size();
Herb Derbya9047642019-12-06 12:12:11 -05001001
1002 // set use tokens for all of the glyphs in our subrun. This is only valid if we
1003 // have a valid atlas generation
1004 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
1005 fUploadTarget->tokenTracker()->nextDrawToken(),
1006 fSubRun->maskFormat());
1007 return true;
1008 }
1009 SK_ABORT("Should not get here");
1010}
1011
1012
1013
1014