blob: 77f7abf2d9f954ca201fc3c4f1607b2c977f469f [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
Herb Derby5bf5b042019-12-12 16:37:03 -050023static SkVector calculate_translation(bool applyVM,
24 const SkMatrix& drawMatrix, SkPoint drawOrigin,
25 const SkMatrix& currentViewMatrix, SkPoint currentOrigin) {
26 SkVector translate;
Herb Derbya9047642019-12-06 12:12:11 -050027 if (applyVM) {
Herb Derby5bf5b042019-12-12 16:37:03 -050028 translate.fX = drawMatrix.getTranslateX() +
29 drawMatrix.getScaleX() * (drawOrigin.x() - currentOrigin.x()) +
30 drawMatrix.getSkewX() * (drawOrigin.y() - currentOrigin.y()) -
31 currentViewMatrix.getTranslateX();
Herb Derbya9047642019-12-06 12:12:11 -050032
Herb Derby5bf5b042019-12-12 16:37:03 -050033 translate.fY = drawMatrix.getTranslateY() +
34 drawMatrix.getSkewY() * (drawOrigin.x() - currentOrigin.x()) +
35 drawMatrix.getScaleY() * (drawOrigin.y() - currentOrigin.y()) -
36 currentViewMatrix.getTranslateY();
Herb Derbya9047642019-12-06 12:12:11 -050037 } else {
Herb Derby5bf5b042019-12-12 16:37:03 -050038 translate = drawOrigin - currentOrigin;
Herb Derbya9047642019-12-06 12:12:11 -050039 }
Herb Derby5bf5b042019-12-12 16:37:03 -050040
41 return translate;
Herb Derbya9047642019-12-06 12:12:11 -050042}
43
44static SkMatrix make_inverse(const SkMatrix& matrix) {
45 SkMatrix inverseMatrix;
46 if (!matrix.invert(&inverseMatrix)) {
47 inverseMatrix = SkMatrix::I();
48 }
49 return inverseMatrix;
50}
51
52// -- GrTextBlob::Key ------------------------------------------------------------------------------
53GrTextBlob::Key::Key() { sk_bzero(this, sizeof(Key)); }
54
55bool GrTextBlob::Key::operator==(const GrTextBlob::Key& other) const {
56 return 0 == memcmp(this, &other, sizeof(Key));
57}
58
59// -- GrTextBlob::PathGlyph ------------------------------------------------------------------------
60GrTextBlob::PathGlyph::PathGlyph(const SkPath& path, SkPoint origin)
61 : fPath(path)
62 , fOrigin(origin) {}
63
64// -- GrTextBlob::SubRun ---------------------------------------------------------------------------
Herb Derbye3c7ff42019-12-10 17:49:28 -050065// Hold data to draw the different types of sub run. SubRuns are produced knowing all the
66// glyphs that are included in them.
67class GrTextBlob::SubRun {
68public:
69 // SubRun for masks
70 SubRun(SubRunType type,
71 GrTextBlob* textBlob,
72 const SkStrikeSpec& strikeSpec,
73 GrMaskFormat format,
Herb Derbyc514e7d2019-12-11 17:00:31 -050074 const SkSpan<GrGlyph*>& glyphs, const SkSpan<char>& vertexData,
Herb Derbye3c7ff42019-12-10 17:49:28 -050075 sk_sp<GrTextStrike>&& grStrike);
76
77 // SubRun for paths
78 SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec);
79
80 void appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables);
81
82 // TODO when this object is more internal, drop the privacy
83 void resetBulkUseToken();
84 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken();
85 void setStrike(sk_sp<GrTextStrike> strike);
86 GrTextStrike* strike() const;
Herb Derbye3c7ff42019-12-10 17:49:28 -050087
88 void setAtlasGeneration(uint64_t atlasGeneration);
89 uint64_t atlasGeneration() const;
90
Herb Derbye3c7ff42019-12-10 17:49:28 -050091 void setColor(GrColor color);
92 GrColor color() const;
93
94 GrMaskFormat maskFormat() const;
95
Herb Derbya2d72252019-12-23 15:02:33 -050096 size_t vertexStride() const;
97
Herb Derbye3c7ff42019-12-10 17:49:28 -050098 const SkRect& vertexBounds() const;
99 void joinGlyphBounds(const SkRect& glyphBounds);
100
Herb Derbye3c7ff42019-12-10 17:49:28 -0500101 // This function assumes the translation will be applied before it is called again
Herb Derby5bf5b042019-12-12 16:37:03 -0500102 SkVector computeTranslation(const SkMatrix& drawMatrix, SkPoint drawOrigin);
Herb Derbye3c7ff42019-12-10 17:49:28 -0500103
104 bool drawAsDistanceFields() const;
105 bool drawAsPaths() const;
106 bool needsTransform() const;
107 bool hasW() const;
108
109 // df properties
110 void setUseLCDText(bool useLCDText);
111 bool hasUseLCDText() const;
112 void setAntiAliased(bool antiAliased);
113 bool isAntiAliased() const;
114
115 const SkStrikeSpec& strikeSpec() const;
116
117 SubRun* fNextSubRun{nullptr};
118 const SubRunType fType;
119 GrTextBlob* const fBlob;
120 const GrMaskFormat fMaskFormat;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500121 const SkSpan<GrGlyph*> fGlyphs;
122 const SkSpan<char> fVertexData;
Herb Derbye3c7ff42019-12-10 17:49:28 -0500123 const SkStrikeSpec fStrikeSpec;
124 sk_sp<GrTextStrike> fStrike;
125 struct {
126 bool useLCDText:1;
127 bool antiAliased:1;
128 } fFlags{false, false};
129 GrColor fColor;
130 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
131 SkRect fVertexBounds = SkRectPriv::MakeLargestInverted();
132 uint64_t fAtlasGeneration{GrDrawOpAtlas::kInvalidAtlasGeneration};
Herb Derby5bf5b042019-12-12 16:37:03 -0500133 SkPoint fCurrentOrigin;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500134 SkMatrix fCurrentMatrix;
Herb Derbye3c7ff42019-12-10 17:49:28 -0500135 std::vector<PathGlyph> fPaths;
136}; // SubRun
137
Herb Derbya9047642019-12-06 12:12:11 -0500138GrTextBlob::SubRun::SubRun(SubRunType type, GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec,
Herb Derbyc514e7d2019-12-11 17:00:31 -0500139 GrMaskFormat format,
140 const SkSpan<GrGlyph*>& glyphs, const SkSpan<char>& vertexData,
Herb Derbya9047642019-12-06 12:12:11 -0500141 sk_sp<GrTextStrike>&& grStrike)
142 : fType{type}
143 , fBlob{textBlob}
144 , fMaskFormat{format}
Herb Derbyc514e7d2019-12-11 17:00:31 -0500145 , fGlyphs{glyphs}
146 , fVertexData{vertexData}
Herb Derbya9047642019-12-06 12:12:11 -0500147 , fStrikeSpec{strikeSpec}
148 , fStrike{grStrike}
149 , fColor{textBlob->fColor}
Herb Derby5bf5b042019-12-12 16:37:03 -0500150 , fCurrentOrigin{textBlob->fInitialOrigin}
Herb Derby1c5be7b2019-12-13 12:03:06 -0500151 , fCurrentMatrix{textBlob->fInitialMatrix} {
Herb Derbya9047642019-12-06 12:12:11 -0500152 SkASSERT(type != kTransformedPath);
Herb Derbycb718892019-12-07 00:07:42 -0500153 textBlob->insertSubRun(this);
Herb Derbya9047642019-12-06 12:12:11 -0500154}
155
156GrTextBlob::SubRun::SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec)
157 : fType{kTransformedPath}
158 , fBlob{textBlob}
159 , fMaskFormat{kA8_GrMaskFormat}
Herb Derbyc514e7d2019-12-11 17:00:31 -0500160 , fGlyphs{SkSpan<GrGlyph*>{}}
161 , fVertexData{SkSpan<char>{}}
Herb Derbya9047642019-12-06 12:12:11 -0500162 , fStrikeSpec{strikeSpec}
163 , fStrike{nullptr}
164 , fColor{textBlob->fColor}
Herb Derbycb718892019-12-07 00:07:42 -0500165 , fPaths{} {
166 textBlob->insertSubRun(this);
167}
Herb Derbya9047642019-12-06 12:12:11 -0500168
169void GrTextBlob::SubRun::appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables) {
170 GrTextStrike* grStrike = fStrike.get();
171 SkScalar strikeToSource = fStrikeSpec.strikeToSourceRatio();
Herb Derbyc514e7d2019-12-11 17:00:31 -0500172 GrGlyph** glyphCursor = fGlyphs.data();
173 char* vertexCursor = fVertexData.data();
Herb Derbya9047642019-12-06 12:12:11 -0500174 bool hasW = this->hasW();
175 GrColor color = this->color();
176 // glyphs drawn in perspective must always have a w coord.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500177 SkASSERT(hasW || !fBlob->fInitialMatrix.hasPerspective());
Herb Derbya2d72252019-12-23 15:02:33 -0500178 size_t vertexStride = this->vertexStride();
Herb Derbya9047642019-12-06 12:12:11 -0500179 // We always write the third position component used by SDFs. If it is unused it gets
180 // overwritten. Similarly, we always write the color and the blob will later overwrite it
181 // with texture coords if it is unused.
182 size_t colorOffset = hasW ? sizeof(SkPoint3) : sizeof(SkPoint);
183 for (auto [variant, pos] : drawables) {
184 SkGlyph* skGlyph = variant;
185 GrGlyph* grGlyph = grStrike->getGlyph(*skGlyph);
186 // Only floor the device coordinates.
187 SkRect dstRect;
188 if (!this->needsTransform()) {
189 pos = {SkScalarFloorToScalar(pos.x()), SkScalarFloorToScalar(pos.y())};
190 dstRect = grGlyph->destRect(pos);
191 } else {
192 dstRect = grGlyph->destRect(pos, strikeToSource);
193 }
194
195 this->joinGlyphBounds(dstRect);
196
Herb Derbya9047642019-12-06 12:12:11 -0500197 // V0
Herb Derbyc514e7d2019-12-11 17:00:31 -0500198 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fTop, 1.f};
199 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color;
200 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500201
202 // V1
Herb Derbyc514e7d2019-12-11 17:00:31 -0500203 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fBottom, 1.f};
204 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color;
205 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500206
207 // V2
Herb Derbyc514e7d2019-12-11 17:00:31 -0500208 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fTop, 1.f};
209 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color;
210 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500211
212 // V3
Herb Derbyc514e7d2019-12-11 17:00:31 -0500213 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fBottom, 1.f};
214 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = color;
215 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500216
Herb Derbyc514e7d2019-12-11 17:00:31 -0500217 *glyphCursor++ = grGlyph;
Herb Derbya9047642019-12-06 12:12:11 -0500218 }
Herb Derbya9047642019-12-06 12:12:11 -0500219}
220
221void GrTextBlob::SubRun::resetBulkUseToken() { fBulkUseToken.reset(); }
222
223GrDrawOpAtlas::BulkUseTokenUpdater* GrTextBlob::SubRun::bulkUseToken() { return &fBulkUseToken; }
224void GrTextBlob::SubRun::setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
225GrTextStrike* GrTextBlob::SubRun::strike() const { return fStrike.get(); }
Herb Derbya9047642019-12-06 12:12:11 -0500226void GrTextBlob::SubRun::setAtlasGeneration(uint64_t atlasGeneration) { fAtlasGeneration = atlasGeneration;}
227uint64_t GrTextBlob::SubRun::atlasGeneration() const { return fAtlasGeneration; }
Herb Derbya9047642019-12-06 12:12:11 -0500228void GrTextBlob::SubRun::setColor(GrColor color) { fColor = color; }
229GrColor GrTextBlob::SubRun::color() const { return fColor; }
230GrMaskFormat GrTextBlob::SubRun::maskFormat() const { return fMaskFormat; }
Herb Derbya2d72252019-12-23 15:02:33 -0500231size_t GrTextBlob::SubRun::vertexStride() const {
232 return GetVertexStride(this->maskFormat(), this->hasW());
233}
234
Herb Derbya9047642019-12-06 12:12:11 -0500235const SkRect& GrTextBlob::SubRun::vertexBounds() const { return fVertexBounds; }
236void GrTextBlob::SubRun::joinGlyphBounds(const SkRect& glyphBounds) {
237 fVertexBounds.joinNonEmptyArg(glyphBounds);
238}
239
Herb Derby5bf5b042019-12-12 16:37:03 -0500240SkVector GrTextBlob::SubRun::computeTranslation(
241 const SkMatrix& drawMatrix, SkPoint drawOrigin){
Herb Derbya9047642019-12-06 12:12:11 -0500242 // Don't use the matrix to translate on distance field for fallback subruns.
Herb Derby5bf5b042019-12-12 16:37:03 -0500243
244 SkVector translate = calculate_translation(
245 !this->drawAsDistanceFields() && !this->needsTransform(),
246 drawMatrix, drawOrigin, fCurrentMatrix, fCurrentOrigin);
247
248 // Update SubRun indicating that the vertices now correspond to the origin and matrix used in
249 // the draw.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500250 fCurrentMatrix = drawMatrix;
Herb Derby5bf5b042019-12-12 16:37:03 -0500251 fCurrentOrigin = drawOrigin;
252 return translate;
Herb Derbya9047642019-12-06 12:12:11 -0500253}
254
255bool GrTextBlob::SubRun::drawAsDistanceFields() const { return fType == kTransformedSDFT; }
256
257bool GrTextBlob::SubRun::drawAsPaths() const { return fType == kTransformedPath; }
258
259bool GrTextBlob::SubRun::needsTransform() const {
260 return fType == kTransformedPath
261 || fType == kTransformedMask
262 || fType == kTransformedSDFT;
263}
264
265bool GrTextBlob::SubRun::hasW() const {
266 return fBlob->hasW(fType);
267}
268
269void GrTextBlob::SubRun::setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
270bool GrTextBlob::SubRun::hasUseLCDText() const { return fFlags.useLCDText; }
271void GrTextBlob::SubRun::setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
272bool GrTextBlob::SubRun::isAntiAliased() const { return fFlags.antiAliased; }
273const SkStrikeSpec& GrTextBlob::SubRun::strikeSpec() const { return fStrikeSpec; }
274
275// -- GrTextBlob -----------------------------------------------------------------------------------
276void GrTextBlob::operator delete(void* p) { ::operator delete(p); }
277void* GrTextBlob::operator new(size_t) { SK_ABORT("All blobs are created by placement new."); }
278void* GrTextBlob::operator new(size_t, void* p) { return p; }
279
280GrTextBlob::~GrTextBlob() = default;
281
Herb Derby659e4092019-12-06 15:38:10 -0500282sk_sp<GrTextBlob> GrTextBlob::Make(const SkGlyphRunList& glyphRunList,
Herb Derbyeba195f2019-12-03 16:44:47 -0500283 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500284 const SkMatrix& drawMatrix,
Herb Derbya00da612019-03-04 17:10:01 -0500285 GrColor color,
Herb Derbyeba195f2019-12-03 16:44:47 -0500286 bool forceWForDistanceFields) {
Herb Derbycd498e12019-12-06 14:17:31 -0500287
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500288 static_assert(sizeof(ARGB2DVertex) <= sizeof(Mask2DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500289 static_assert(alignof(ARGB2DVertex) <= alignof(Mask2DVertex));
290 size_t quadSize = sizeof(Mask2DVertex) * kVerticesPerGlyph;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500291 if (drawMatrix.hasPerspective() || forceWForDistanceFields) {
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500292 static_assert(sizeof(ARGB3DVertex) <= sizeof(SDFT3DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500293 static_assert(alignof(ARGB3DVertex) <= alignof(SDFT3DVertex));
294 quadSize = sizeof(SDFT3DVertex) * kVerticesPerGlyph;
Herb Derbye74c7762019-12-04 14:15:41 -0500295 }
296
Herb Derbycb718892019-12-07 00:07:42 -0500297 // We can use the alignment of SDFT3DVertex as a proxy for all Vertex alignments.
298 static_assert(alignof(SDFT3DVertex) >= alignof(Mask2DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500299 // Assume there is no padding needed between glyph pointers and vertices.
Herb Derbycb718892019-12-07 00:07:42 -0500300 static_assert(alignof(GrGlyph*) >= alignof(SDFT3DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500301
302 // In the arena, the layout is GrGlyph*... | SDFT3DVertex... | SubRun, so there is no padding
303 // between GrGlyph* and SDFT3DVertex, but padding is needed between the Mask2DVertex array
304 // and the SubRun.
305 size_t vertexToSubRunPadding = alignof(SDFT3DVertex) - alignof(SubRun);
306 size_t arenaSize =
307 sizeof(GrGlyph*) * glyphRunList.totalGlyphCount()
308 + quadSize * glyphRunList.totalGlyphCount()
309 + glyphRunList.runCount() * (sizeof(SubRun) + vertexToSubRunPadding);
310
311 size_t allocationSize = sizeof(GrTextBlob) + arenaSize;
Ben Wagner75d6db72018-09-20 14:39:39 -0400312
Herb Derbycb718892019-12-07 00:07:42 -0500313 void* allocation = ::operator new (allocationSize);
Herb Derbyb12175f2018-05-23 16:38:09 -0400314
Herb Derbyaebc5f82019-12-10 14:07:10 -0500315 SkColor initialLuminance = SkPaintPriv::ComputeLuminanceColor(glyphRunList.paint());
Herb Derby00ae9592019-12-03 15:55:56 -0500316 sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{
Herb Derby1c5be7b2019-12-13 12:03:06 -0500317 arenaSize, strikeCache, drawMatrix, glyphRunList.origin(),
Herb Derbyaebc5f82019-12-10 14:07:10 -0500318 color, initialLuminance, forceWForDistanceFields}};
joshualitt92303772016-02-10 11:55:52 -0800319
Herb Derbyf7d5d742018-11-16 13:24:32 -0500320 return blob;
joshualitt92303772016-02-10 11:55:52 -0800321}
322
Herb Derbya9047642019-12-06 12:12:11 -0500323void GrTextBlob::setupKey(const GrTextBlob::Key& key, const SkMaskFilterBase::BlurRec& blurRec,
324 const SkPaint& paint) {
325 fKey = key;
326 if (key.fHasBlur) {
327 fBlurRec = blurRec;
328 }
329 if (key.fStyle != SkPaint::kFill_Style) {
330 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
331 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
332 fStrokeInfo.fJoin = paint.getStrokeJoin();
333 }
334}
335const GrTextBlob::Key& GrTextBlob::GetKey(const GrTextBlob& blob) { return blob.fKey; }
336uint32_t GrTextBlob::Hash(const GrTextBlob::Key& key) { return SkOpts::hash(&key, sizeof(Key)); }
337
Herb Derbycb718892019-12-07 00:07:42 -0500338bool GrTextBlob::hasDistanceField() const {
339 return SkToBool(fTextType & kHasDistanceField_TextType);
340}
Herb Derbya9047642019-12-06 12:12:11 -0500341bool GrTextBlob::hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
Herb Derby1c5be7b2019-12-13 12:03:06 -0500342bool GrTextBlob::hasPerspective() const { return fInitialMatrix.hasPerspective(); }
Herb Derbya9047642019-12-06 12:12:11 -0500343
344void GrTextBlob::setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
345void GrTextBlob::setHasBitmap() { fTextType |= kHasBitmap_TextType; }
346void GrTextBlob::setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) {
347 // we init fMaxMinScale and fMinMaxScale in the constructor
348 fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale);
349 fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale);
350}
351
352size_t GrTextBlob::GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
353 switch (maskFormat) {
354 case kA8_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500355 return hasWCoord ? sizeof(SDFT3DVertex) : sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500356 case kARGB_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500357 return hasWCoord ? sizeof(ARGB3DVertex) : sizeof(ARGB2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500358 default:
359 SkASSERT(!hasWCoord);
Herb Derbycd498e12019-12-06 14:17:31 -0500360 return sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500361 }
362}
363
Herb Derby0edb2142018-10-16 17:04:11 -0400364bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
365 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derby5bf5b042019-12-12 16:37:03 -0500366 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
joshualittfd5f6c12015-12-10 07:44:50 -0800367 // If we have LCD text then our canonical color will be set to transparent, in this case we have
368 // to regenerate the blob on any color change
369 // We use the grPaint to get any color filter effects
370 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbyaebc5f82019-12-10 14:07:10 -0500371 fInitialLuminance != SkPaintPriv::ComputeLuminanceColor(paint)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800372 return true;
373 }
374
Herb Derby1c5be7b2019-12-13 12:03:06 -0500375 if (fInitialMatrix.hasPerspective() != drawMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800376 return true;
377 }
378
Brian Salomon5c6ac642017-12-19 11:09:32 -0500379 /** This could be relaxed for blobs with only distance field glyphs. */
Mike Reed2c383152019-12-18 16:47:47 -0500380 if (fInitialMatrix.hasPerspective() && !SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800381 return true;
382 }
383
384 // We only cache one masked version
385 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400386 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800387 return true;
388 }
389
390 // Similarly, we only cache one version for each style
391 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400392 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
393 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
394 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800395 return true;
396 }
397
398 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
399 // for mixed blobs if this becomes an issue.
400 if (this->hasBitmap() && this->hasDistanceField()) {
Herb Derbyeba195f2019-12-03 16:44:47 -0500401 // Identical view matrices and we can reuse in all cases
Mike Reed2c383152019-12-18 16:47:47 -0500402 return !(SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix) &&
403 drawOrigin == fInitialOrigin);
joshualittfd5f6c12015-12-10 07:44:50 -0800404 }
405
406 if (this->hasBitmap()) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500407 if (fInitialMatrix.getScaleX() != drawMatrix.getScaleX() ||
408 fInitialMatrix.getScaleY() != drawMatrix.getScaleY() ||
409 fInitialMatrix.getSkewX() != drawMatrix.getSkewX() ||
410 fInitialMatrix.getSkewY() != drawMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800411 return true;
412 }
413
Herb Derby9830fc42019-09-12 10:58:26 -0400414 // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust
415 // the quads properly. Devise a system that regenerates the quads from original data
416 // using the transform to allow this to be used in general.
417
418 // We can update the positions in the text blob without regenerating the whole
419 // blob, but only for integer translations.
420 // This cool bit of math will determine the necessary translation to apply to the
421 // already generated vertex coordinates to move them to the correct position.
422 // Figure out the translation in view space given a translation in source space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500423 SkScalar transX = drawMatrix.getTranslateX() +
Herb Derby5bf5b042019-12-12 16:37:03 -0500424 drawMatrix.getScaleX() * (drawOrigin.x() - fInitialOrigin.x()) +
425 drawMatrix.getSkewX() * (drawOrigin.y() - fInitialOrigin.y()) -
Herb Derby1c5be7b2019-12-13 12:03:06 -0500426 fInitialMatrix.getTranslateX();
427 SkScalar transY = drawMatrix.getTranslateY() +
Herb Derby5bf5b042019-12-12 16:37:03 -0500428 drawMatrix.getSkewY() * (drawOrigin.x() - fInitialOrigin.x()) +
429 drawMatrix.getScaleY() * (drawOrigin.y() - fInitialOrigin.y()) -
Herb Derby1c5be7b2019-12-13 12:03:06 -0500430 fInitialMatrix.getTranslateY();
Herb Derby9830fc42019-09-12 10:58:26 -0400431 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
432 return true;
joshualittfd5f6c12015-12-10 07:44:50 -0800433 }
joshualittfd5f6c12015-12-10 07:44:50 -0800434 } else if (this->hasDistanceField()) {
435 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
436 // distance field being generated, so we have to regenerate in those cases
Herb Derby1c5be7b2019-12-13 12:03:06 -0500437 SkScalar newMaxScale = drawMatrix.getMaxScale();
438 SkScalar oldMaxScale = fInitialMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800439 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
440 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
441 return true;
442 }
joshualittfd5f6c12015-12-10 07:44:50 -0800443 }
444
joshualittfd5f6c12015-12-10 07:44:50 -0800445 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
446 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
447 // the blob anyways at flush time, so no need to regenerate explicitly
448 return false;
449}
450
Herb Derbyc1b482c2018-08-09 15:02:27 -0400451void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
452 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400453 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Herb Derby5bf5b042019-12-12 16:37:03 -0500454 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500455
Herb Derbycb718892019-12-07 00:07:42 -0500456 for (SubRun* subRun = fFirstSubRun; subRun != nullptr; subRun = subRun->fNextSubRun) {
457 if (subRun->drawAsPaths()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400458 SkPaint runPaint{paint};
Herb Derbycb718892019-12-07 00:07:42 -0500459 runPaint.setAntiAlias(subRun->isAntiAliased());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500460 // If there are shaders, blurs or styles, the path must be scaled into source
461 // space independently of the CTM. This allows the CTM to be correct for the
462 // different effects.
463 GrStyle style(runPaint);
Herb Derby9f491482018-08-08 11:53:00 -0400464
Herb Derby1b8dcd12019-11-15 15:21:15 -0500465 bool scalePath = runPaint.getShader()
466 || style.applies()
467 || runPaint.getMaskFilter();
Robert Phillips137ca522018-08-15 10:14:33 -0400468
Herb Derby1b8dcd12019-11-15 15:21:15 -0500469 // The origin for the blob may have changed, so figure out the delta.
Herb Derby5bf5b042019-12-12 16:37:03 -0500470 SkVector originShift = drawOrigin - fInitialOrigin;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500471
Herb Derbycb718892019-12-07 00:07:42 -0500472 for (const auto& pathGlyph : subRun->fPaths) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500473 SkMatrix ctm{drawMatrix};
Herb Derbycb718892019-12-07 00:07:42 -0500474 SkMatrix pathMatrix = SkMatrix::MakeScale(
475 subRun->fStrikeSpec.strikeToSourceRatio());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500476 // Shift the original glyph location in source space to the position of the new
477 // blob.
478 pathMatrix.postTranslate(originShift.x() + pathGlyph.fOrigin.x(),
479 originShift.y() + pathGlyph.fOrigin.y());
Herb Derbydac1ed52018-09-12 17:04:21 -0400480
481 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400482 SkTLazy<SkPath> tmpPath;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500483 const SkPath* path = &pathGlyph.fPath;
Herb Derby2984d262019-11-20 14:40:39 -0500484 if (!scalePath) {
485 // Scale can be applied to CTM -- no effects.
Herb Derby2984d262019-11-20 14:40:39 -0500486 ctm.preConcat(pathMatrix);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400487 } else {
Herb Derby2984d262019-11-20 14:40:39 -0500488 // Scale the outline into source space.
Herb Derbydac1ed52018-09-12 17:04:21 -0400489
Herb Derby2984d262019-11-20 14:40:39 -0500490 // Transform the path form the normalized outline to source space. This
491 // way the CTM will remain the same so it can be used by the effects.
492 SkPath* sourceOutline = tmpPath.init();
493 path->transform(pathMatrix, sourceOutline);
494 sourceOutline->setIsVolatile(true);
495 path = sourceOutline;
Robert Phillips137ca522018-08-15 10:14:33 -0400496 }
497
Robert Phillips46a13382018-08-23 13:53:01 -0400498 // TODO: we are losing the mutability of the path here
499 GrShape shape(*path, paint);
Herb Derbydac1ed52018-09-12 17:04:21 -0400500 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500501 }
Herb Derby1b8dcd12019-11-15 15:21:15 -0500502 } else {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500503 int glyphCount = subRun->fGlyphs.size();
Jim Van Verth54d9c882018-02-08 16:14:48 -0500504 if (0 == glyphCount) {
505 continue;
506 }
507
508 bool skipClip = false;
509 bool submitOp = true;
510 SkIRect clipRect = SkIRect::MakeEmpty();
511 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
512 SkRRect clipRRect;
513 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400514 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500515 // and we have an axis-aligned rectangular non-AA clip
Herb Derbycb718892019-12-07 00:07:42 -0500516 if (!subRun->drawAsDistanceFields() && !subRun->needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500517 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500518 clipRRect.isRect() && GrAA::kNo == aa) {
519 skipClip = true;
520 // We only need to do clipping work if the subrun isn't contained by the clip
521 SkRect subRunBounds;
Herb Derby5bf5b042019-12-12 16:37:03 -0500522 this->computeSubRunBounds(
523 &subRunBounds, *subRun, drawMatrix, drawOrigin, false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500524 if (!clipRRect.getBounds().contains(subRunBounds)) {
525 // If the subrun is completely outside, don't add an op for it
526 if (!clipRRect.getBounds().intersects(subRunBounds)) {
527 submitOp = false;
528 }
529 else {
530 clipRRect.getBounds().round(&clipRect);
531 }
532 }
533 }
534
535 if (submitOp) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500536 auto op = this->makeOp(*subRun, glyphCount, drawMatrix, drawOrigin,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400537 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500538 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500539 if (op) {
540 if (skipClip) {
541 target->addDrawOp(GrNoClip(), std::move(op));
542 }
543 else {
544 target->addDrawOp(clip, std::move(op));
545 }
546 }
547 }
548 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000549 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000550}
551
Herb Derby1c5be7b2019-12-13 12:03:06 -0500552void GrTextBlob::computeSubRunBounds(SkRect* outBounds, const SubRun& subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500553 const SkMatrix& drawMatrix, SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500554 bool needsGlyphTransform) {
555 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
556 // into device space.
557 // We handle vertex bounds differently for distance field text and bitmap text because
558 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
559 // from one blob then we are going to pay the price here of mapping the rect for each run.
560 *outBounds = subRun.vertexBounds();
561 if (needsGlyphTransform) {
562 // Distance field text is positioned with the (X,Y) as part of the glyph position,
563 // and currently the view matrix is applied on the GPU
Herb Derby5bf5b042019-12-12 16:37:03 -0500564 outBounds->offset(drawOrigin - fInitialOrigin);
Herb Derby1c5be7b2019-12-13 12:03:06 -0500565 drawMatrix.mapRect(outBounds);
Herb Derbya9047642019-12-06 12:12:11 -0500566 } else {
567 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
568 // device space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500569 SkMatrix boundsMatrix = fInitialMatrixInverse;
Herb Derbya9047642019-12-06 12:12:11 -0500570
571 boundsMatrix.postTranslate(-fInitialOrigin.x(), -fInitialOrigin.y());
572
Herb Derby5bf5b042019-12-12 16:37:03 -0500573 boundsMatrix.postTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derbya9047642019-12-06 12:12:11 -0500574
Herb Derby1c5be7b2019-12-13 12:03:06 -0500575 boundsMatrix.postConcat(drawMatrix);
Herb Derbya9047642019-12-06 12:12:11 -0500576 boundsMatrix.mapRect(outBounds);
577
578 // Due to floating point numerical inaccuracies, we have to round out here
579 outBounds->roundOut(outBounds);
580 }
joshualitt323c2eb2016-01-20 06:48:47 -0800581}
joshualitt2e2202e2015-12-10 11:22:08 -0800582
Herb Derbya9047642019-12-06 12:12:11 -0500583const GrTextBlob::Key& GrTextBlob::key() const { return fKey; }
584size_t GrTextBlob::size() const { return fSize; }
585
586std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Herb Derby1c5be7b2019-12-13 12:03:06 -0500587 int glyphCount, const SkMatrix& drawMatrix,
Herb Derby5bf5b042019-12-12 16:37:03 -0500588 SkPoint drawOrigin, const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbya9047642019-12-06 12:12:11 -0500589 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
590 GrTextTarget* target) {
Herb Derbycb718892019-12-07 00:07:42 -0500591 SubRun* info = fFirstSubRun;
Herb Derbya9047642019-12-06 12:12:11 -0500592 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derby5bf5b042019-12-12 16:37:03 -0500593 return this->makeOp(*info, glyphCount, drawMatrix, drawOrigin, emptyRect,
Herb Derbya9047642019-12-06 12:12:11 -0500594 paint, filteredColor, props, distanceAdjustTable, target);
595}
596
597bool GrTextBlob::hasW(GrTextBlob::SubRunType type) const {
598 if (type == kTransformedSDFT) {
599 return this->hasPerspective() || fForceWForDistanceFields;
600 } else if (type == kTransformedMask || type == kTransformedPath) {
601 return this->hasPerspective();
Herb Derbye9f691d2019-12-04 12:11:13 -0500602 }
Herb Derbya9047642019-12-06 12:12:11 -0500603
604 // The viewMatrix is implicitly SkMatrix::I when drawing kDirectMask, because it is not
605 // used.
606 return false;
607}
608
609GrTextBlob::SubRun* GrTextBlob::makeSubRun(SubRunType type,
610 const SkZip<SkGlyphVariant, SkPoint>& drawables,
611 const SkStrikeSpec& strikeSpec,
612 GrMaskFormat format) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500613 SkSpan<GrGlyph*> glyphs{fAlloc.makeArrayDefault<GrGlyph*>(drawables.size()), drawables.size()};
Herb Derbya9047642019-12-06 12:12:11 -0500614 bool hasW = this->hasW(type);
Herb Derbyc514e7d2019-12-11 17:00:31 -0500615 size_t vertexDataSize = drawables.size() * GetVertexStride(format, hasW) * kVerticesPerGlyph;
616 SkSpan<char> vertexData{fAlloc.makeArrayDefault<char>(vertexDataSize), vertexDataSize};
Herb Derbya9047642019-12-06 12:12:11 -0500617
618 sk_sp<GrTextStrike> grStrike = strikeSpec.findOrCreateGrStrike(fStrikeCache);
619
Herb Derbycb718892019-12-07 00:07:42 -0500620 SubRun* subRun = fAlloc.make<SubRun>(
Herb Derbyc514e7d2019-12-11 17:00:31 -0500621 type, this, strikeSpec, format, glyphs, vertexData, std::move(grStrike));
Herb Derbya9047642019-12-06 12:12:11 -0500622
Herb Derbycb718892019-12-07 00:07:42 -0500623 subRun->appendGlyphs(drawables);
Herb Derbya9047642019-12-06 12:12:11 -0500624
Herb Derbycb718892019-12-07 00:07:42 -0500625 return subRun;
Herb Derbya9047642019-12-06 12:12:11 -0500626}
627
628void GrTextBlob::addSingleMaskFormat(
629 SubRunType type,
630 const SkZip<SkGlyphVariant, SkPoint>& drawables,
631 const SkStrikeSpec& strikeSpec,
632 GrMaskFormat format) {
633 this->makeSubRun(type, drawables, strikeSpec, format);
634}
635
636void GrTextBlob::addMultiMaskFormat(
637 SubRunType type,
638 const SkZip<SkGlyphVariant, SkPoint>& drawables,
639 const SkStrikeSpec& strikeSpec) {
640 this->setHasBitmap();
641 if (drawables.empty()) { return; }
642
643 auto glyphSpan = drawables.get<0>();
644 SkGlyph* glyph = glyphSpan[0];
645 GrMaskFormat format = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
646 size_t startIndex = 0;
647 for (size_t i = 1; i < drawables.size(); i++) {
648 glyph = glyphSpan[i];
649 GrMaskFormat nextFormat = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
650 if (format != nextFormat) {
651 auto sameFormat = drawables.subspan(startIndex, i - startIndex);
652 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
653 format = nextFormat;
654 startIndex = i;
655 }
656 }
657 auto sameFormat = drawables.last(drawables.size() - startIndex);
658 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
659}
660
661void GrTextBlob::addSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
662 const SkStrikeSpec& strikeSpec,
663 const SkFont& runFont,
664 SkScalar minScale,
665 SkScalar maxScale) {
666 this->setHasDistanceField();
667 this->setMinAndMaxScale(minScale, maxScale);
668
669 SubRun* subRun = this->makeSubRun(kTransformedSDFT, drawables, strikeSpec, kA8_GrMaskFormat);
670 subRun->setUseLCDText(runFont.getEdging() == SkFont::Edging::kSubpixelAntiAlias);
671 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbye9f691d2019-12-04 12:11:13 -0500672}
673
Herb Derbycb718892019-12-07 00:07:42 -0500674GrTextBlob::GrTextBlob(size_t allocSize,
Herb Derbyeba195f2019-12-03 16:44:47 -0500675 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500676 const SkMatrix& drawMatrix,
Herb Derbyeba195f2019-12-03 16:44:47 -0500677 SkPoint origin,
678 GrColor color,
Herb Derbyaebc5f82019-12-10 14:07:10 -0500679 SkColor initialLuminance,
Herb Derby00ae9592019-12-03 15:55:56 -0500680 bool forceWForDistanceFields)
Herb Derbycb718892019-12-07 00:07:42 -0500681 : fSize{allocSize}
Herb Derby00ae9592019-12-03 15:55:56 -0500682 , fStrikeCache{strikeCache}
Herb Derby1c5be7b2019-12-13 12:03:06 -0500683 , fInitialMatrix{drawMatrix}
684 , fInitialMatrixInverse{make_inverse(drawMatrix)}
Herb Derbyeba195f2019-12-03 16:44:47 -0500685 , fInitialOrigin{origin}
Herb Derby00ae9592019-12-03 15:55:56 -0500686 , fForceWForDistanceFields{forceWForDistanceFields}
Herb Derbycb718892019-12-07 00:07:42 -0500687 , fColor{color}
Herb Derbyaebc5f82019-12-10 14:07:10 -0500688 , fInitialLuminance{initialLuminance}
Herb Derbycb718892019-12-07 00:07:42 -0500689 , fAlloc{SkTAddOffset<char>(this, sizeof(GrTextBlob)), allocSize, allocSize/2} { }
Herb Derby00ae9592019-12-03 15:55:56 -0500690
Herb Derbycb718892019-12-07 00:07:42 -0500691void GrTextBlob::insertSubRun(SubRun* subRun) {
692 if (fFirstSubRun == nullptr) {
693 fFirstSubRun = subRun;
694 fLastSubRun = subRun;
695 } else {
696 fLastSubRun->fNextSubRun = subRun;
697 fLastSubRun = subRun;
698 }
699}
700
701std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Herb Derbya9047642019-12-06 12:12:11 -0500702 SubRun& info, int glyphCount,
Herb Derby5bf5b042019-12-12 16:37:03 -0500703 const SkMatrix& drawMatrix, SkPoint drawOrigin, const SkIRect& clipRect,
Herb Derbya9047642019-12-06 12:12:11 -0500704 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
705 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
706 GrMaskFormat format = info.maskFormat();
707
708 GrPaint grPaint;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500709 target->makeGrPaint(info.maskFormat(), paint, drawMatrix, &grPaint);
Herb Derbya9047642019-12-06 12:12:11 -0500710 std::unique_ptr<GrAtlasTextOp> op;
711 if (info.drawAsDistanceFields()) {
712 // TODO: Can we be even smarter based on the dest transfer function?
713 op = GrAtlasTextOp::MakeDistanceField(
714 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
715 target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint),
716 props, info.isAntiAliased(), info.hasUseLCDText());
Herb Derbyeba195f2019-12-03 16:44:47 -0500717 } else {
Herb Derbya9047642019-12-06 12:12:11 -0500718 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
719 info.needsTransform());
720 }
721 GrAtlasTextOp::Geometry& geometry = op->geometry();
Herb Derby1c5be7b2019-12-13 12:03:06 -0500722 geometry.fDrawMatrix = drawMatrix;
Herb Derbya9047642019-12-06 12:12:11 -0500723 geometry.fClipRect = clipRect;
724 geometry.fBlob = SkRef(this);
725 geometry.fSubRunPtr = &info;
726 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
Herb Derby5bf5b042019-12-12 16:37:03 -0500727 geometry.fDrawOrigin = drawOrigin;
Herb Derbya9047642019-12-06 12:12:11 -0500728 op->init();
729 return op;
730}
Herb Derbyeba195f2019-12-03 16:44:47 -0500731
Herb Derbya9047642019-12-06 12:12:11 -0500732void GrTextBlob::processDeviceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
733 const SkStrikeSpec& strikeSpec) {
734 this->addMultiMaskFormat(kDirectMask, drawables, strikeSpec);
735}
Herb Derbyeba195f2019-12-03 16:44:47 -0500736
Herb Derbya9047642019-12-06 12:12:11 -0500737void GrTextBlob::processSourcePaths(const SkZip<SkGlyphVariant, SkPoint>& drawables,
738 const SkFont& runFont,
739 const SkStrikeSpec& strikeSpec) {
740 this->setHasBitmap();
Herb Derbycb718892019-12-07 00:07:42 -0500741 SubRun* subRun = fAlloc.make<SubRun>(this, strikeSpec);
742 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbya9047642019-12-06 12:12:11 -0500743 for (auto [variant, pos] : drawables) {
Herb Derbycb718892019-12-07 00:07:42 -0500744 subRun->fPaths.emplace_back(*variant.path(), pos);
Herb Derbyeba195f2019-12-03 16:44:47 -0500745 }
746}
747
Herb Derbya9047642019-12-06 12:12:11 -0500748void GrTextBlob::processSourceSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
749 const SkStrikeSpec& strikeSpec,
750 const SkFont& runFont,
751 SkScalar minScale,
752 SkScalar maxScale) {
753 this->addSDFT(drawables, strikeSpec, runFont, minScale, maxScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800754}
Herb Derbya9047642019-12-06 12:12:11 -0500755
756void GrTextBlob::processSourceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
757 const SkStrikeSpec& strikeSpec) {
758 this->addMultiMaskFormat(kTransformedMask, drawables, strikeSpec);
759}
760
761// -- GrTextBlob::VertexRegenerator ----------------------------------------------------------------
Herb Derby5bf5b042019-12-12 16:37:03 -0500762static void regen_positions(char* vertex, size_t vertexStride, SkVector translation) {
Herb Derbya9047642019-12-06 12:12:11 -0500763 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
764 for (int i = 0; i < 4; ++i) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500765 *point += translation;
Herb Derbya9047642019-12-06 12:12:11 -0500766 point = SkTAddOffset<SkPoint>(point, vertexStride);
767 }
768}
769
770static void regen_colors(char* vertex, size_t vertexStride, GrColor color) {
771 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
772 // vertices, hence vertexStride - sizeof(SkIPoint16)
773 size_t colorOffset = vertexStride - sizeof(SkIPoint16) - sizeof(GrColor);
774 GrColor* vcolor = reinterpret_cast<GrColor*>(vertex + colorOffset);
775 for (int i = 0; i < 4; ++i) {
776 *vcolor = color;
777 vcolor = SkTAddOffset<GrColor>(vcolor, vertexStride);
778 }
779}
780
781static void regen_texcoords(char* vertex, size_t vertexStride, const GrGlyph* glyph,
782 bool useDistanceFields) {
783 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
784 // vertices, hence vertexStride - sizeof(SkIPoint16)
785 size_t texCoordOffset = vertexStride - sizeof(SkIPoint16);
786
787 uint16_t u0, v0, u1, v1;
788 SkASSERT(glyph);
789 int width = glyph->fBounds.width();
790 int height = glyph->fBounds.height();
791
792 if (useDistanceFields) {
793 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
794 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
795 u1 = u0 + width - 2 * SK_DistanceFieldInset;
796 v1 = v0 + height - 2 * SK_DistanceFieldInset;
797 } else {
798 u0 = glyph->fAtlasLocation.fX;
799 v0 = glyph->fAtlasLocation.fY;
800 u1 = u0 + width;
801 v1 = v0 + height;
802 }
803 // We pack the 2bit page index in the low bit of the u and v texture coords
804 uint32_t pageIndex = glyph->pageIndex();
805 SkASSERT(pageIndex < 4);
806 uint16_t uBit = (pageIndex >> 1) & 0x1;
807 uint16_t vBit = pageIndex & 0x1;
808 u0 <<= 1;
809 u0 |= uBit;
810 v0 <<= 1;
811 v0 |= vBit;
812 u1 <<= 1;
813 u1 |= uBit;
814 v1 <<= 1;
815 v1 |= vBit;
816
817 uint16_t* textureCoords = reinterpret_cast<uint16_t*>(vertex + texCoordOffset);
818 textureCoords[0] = u0;
819 textureCoords[1] = v0;
820 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
821 textureCoords[0] = u0;
822 textureCoords[1] = v1;
823 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
824 textureCoords[0] = u1;
825 textureCoords[1] = v0;
826 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
827 textureCoords[0] = u1;
828 textureCoords[1] = v1;
829
830#ifdef DISPLAY_PAGE_INDEX
831 // Enable this to visualize the page from which each glyph is being drawn.
832 // Green Red Magenta Cyan -> 0 1 2 3; Black -> error
833 GrColor hackColor;
834 switch (pageIndex) {
835 case 0:
836 hackColor = GrColorPackRGBA(0, 255, 0, 255);
837 break;
838 case 1:
839 hackColor = GrColorPackRGBA(255, 0, 0, 255);;
840 break;
841 case 2:
842 hackColor = GrColorPackRGBA(255, 0, 255, 255);
843 break;
844 case 3:
845 hackColor = GrColorPackRGBA(0, 255, 255, 255);
846 break;
847 default:
848 hackColor = GrColorPackRGBA(0, 0, 0, 255);
849 break;
850 }
851 regen_colors(vertex, vertexStride, hackColor);
852#endif
853}
854
855GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
Herb Derbya9047642019-12-06 12:12:11 -0500856 GrTextBlob::SubRun* subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500857 const SkMatrix& drawMatrix,
858 SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500859 GrColor color,
860 GrDeferredUploadTarget* uploadTarget,
861 GrStrikeCache* grStrikeCache,
862 GrAtlasManager* fullAtlasManager)
863 : fResourceProvider(resourceProvider)
Herb Derby1c5be7b2019-12-13 12:03:06 -0500864 , fDrawMatrix(drawMatrix)
Herb Derbya9047642019-12-06 12:12:11 -0500865 , fUploadTarget(uploadTarget)
866 , fGrStrikeCache(grStrikeCache)
867 , fFullAtlasManager(fullAtlasManager)
868 , fSubRun(subRun)
869 , fColor(color) {
870 // Compute translation if any
Herb Derby5bf5b042019-12-12 16:37:03 -0500871 fDrawTranslation = fSubRun->computeTranslation(fDrawMatrix, drawOrigin);
Herb Derbya9047642019-12-06 12:12:11 -0500872
873 // Because the GrStrikeCache may evict the strike a blob depends on using for
874 // generating its texture coords, we have to track whether or not the strike has
875 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
876 // otherwise we have to get the new strike, and use that to get the correct glyphs.
877 // Because we do not have the packed ids, and thus can't look up our glyphs in the
878 // new strike, we instead keep our ref to the old strike and use the packed ids from
879 // it. These ids will still be valid as long as we hold the ref. When we are done
880 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
Herb Derby73630212019-12-13 16:29:14 -0500881 fActions.regenTextureCoordinates = fSubRun->strike()->isAbandoned();
882 fActions.regenStrike = fSubRun->strike()->isAbandoned();
883 fActions.regenColor = kARGB_GrMaskFormat != fSubRun->maskFormat() && fSubRun->color() != color;
884 fActions.regenPositions = fDrawTranslation.x() != 0.f || fDrawTranslation.y() != 0.f;
Herb Derbya9047642019-12-06 12:12:11 -0500885}
886
Herb Derby73630212019-12-13 16:29:14 -0500887bool GrTextBlob::VertexRegenerator::doRegen(GrTextBlob::VertexRegenerator::Result* result) {
888 SkASSERT(!fActions.regenStrike || fActions.regenTextureCoordinates);
889 if (fActions.regenTextureCoordinates) {
Herb Derbya9047642019-12-06 12:12:11 -0500890 fSubRun->resetBulkUseToken();
891
892 const SkStrikeSpec& strikeSpec = fSubRun->strikeSpec();
893
894 if (!fMetricsAndImages.isValid()
895 || fMetricsAndImages->descriptor() != strikeSpec.descriptor()) {
896 fMetricsAndImages.init(strikeSpec);
897 }
898
Herb Derby73630212019-12-13 16:29:14 -0500899 if (fActions.regenStrike) {
Herb Derbya9047642019-12-06 12:12:11 -0500900 // Take the glyphs from the old strike, and translate them a new strike.
901 sk_sp<GrTextStrike> newStrike = strikeSpec.findOrCreateGrStrike(fGrStrikeCache);
902
903 // Start this batch at the start of the subRun plus any glyphs that were previously
904 // processed.
Herb Derbyc514e7d2019-12-11 17:00:31 -0500905 SkSpan<GrGlyph*> glyphs = fSubRun->fGlyphs.last(fSubRun->fGlyphs.size() - fCurrGlyph);
Herb Derbya9047642019-12-06 12:12:11 -0500906
907 // Convert old glyphs to newStrike.
908 for (auto& glyph : glyphs) {
909 SkPackedGlyphID id = glyph->fPackedID;
910 glyph = newStrike->getGlyph(id, fMetricsAndImages.get());
911 SkASSERT(id == glyph->fPackedID);
912 }
913
914 fSubRun->setStrike(newStrike);
915 }
916 }
917
Herb Derby7cf4a2e2019-12-23 14:51:55 -0500918 GrTextStrike* grStrike = fSubRun->strike();
Herb Derbya2d72252019-12-23 15:02:33 -0500919 auto vertexStride = fSubRun->vertexStride();
Herb Derbyc514e7d2019-12-11 17:00:31 -0500920 char* currVertex = fSubRun->fVertexData.data() + fCurrGlyph * kVerticesPerGlyph * vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500921 result->fFirstVertex = currVertex;
922
Herb Derbyc514e7d2019-12-11 17:00:31 -0500923 for (int glyphIdx = fCurrGlyph; glyphIdx < (int)fSubRun->fGlyphs.size(); glyphIdx++) {
Herb Derbya9047642019-12-06 12:12:11 -0500924 GrGlyph* glyph = nullptr;
Herb Derby73630212019-12-13 16:29:14 -0500925 if (fActions.regenTextureCoordinates) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500926 glyph = fSubRun->fGlyphs[glyphIdx];
Herb Derbya9047642019-12-06 12:12:11 -0500927 SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
928
929 if (!fFullAtlasManager->hasGlyph(glyph)) {
Herb Derby7cf4a2e2019-12-23 14:51:55 -0500930 GrDrawOpAtlas::ErrorCode code = grStrike->addGlyphToAtlas(
931 fResourceProvider, fUploadTarget, fGrStrikeCache, fFullAtlasManager, glyph,
932 fMetricsAndImages.get(), fSubRun->maskFormat(), fSubRun->needsTransform());
Herb Derbya9047642019-12-06 12:12:11 -0500933 if (GrDrawOpAtlas::ErrorCode::kError == code) {
934 // Something horrible has happened - drop the op
935 return false;
936 }
937 else if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
938 fBrokenRun = glyphIdx > 0;
939 result->fFinished = false;
940 return true;
941 }
942 }
943 auto tokenTracker = fUploadTarget->tokenTracker();
944 fFullAtlasManager->addGlyphToBulkAndSetUseToken(fSubRun->bulkUseToken(), glyph,
945 tokenTracker->nextDrawToken());
946 }
947
Herb Derby73630212019-12-13 16:29:14 -0500948 if (fActions.regenPositions) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500949 regen_positions(currVertex, vertexStride, fDrawTranslation);
Herb Derbya9047642019-12-06 12:12:11 -0500950 }
Herb Derby73630212019-12-13 16:29:14 -0500951 if (fActions.regenColor) {
Herb Derbya9047642019-12-06 12:12:11 -0500952 regen_colors(currVertex, vertexStride, fColor);
953 }
Herb Derby73630212019-12-13 16:29:14 -0500954 if (fActions.regenTextureCoordinates) {
Herb Derbya9047642019-12-06 12:12:11 -0500955 regen_texcoords(currVertex, vertexStride, glyph, fSubRun->drawAsDistanceFields());
956 }
957
958 currVertex += vertexStride * GrAtlasTextOp::kVerticesPerGlyph;
959 ++result->fGlyphsRegenerated;
960 ++fCurrGlyph;
961 }
962
963 // We may have changed the color so update it here
964 fSubRun->setColor(fColor);
Herb Derby73630212019-12-13 16:29:14 -0500965 if (fActions.regenTextureCoordinates) {
Herb Derbya9047642019-12-06 12:12:11 -0500966 fSubRun->setAtlasGeneration(fBrokenRun
967 ? GrDrawOpAtlas::kInvalidAtlasGeneration
968 : fFullAtlasManager->atlasGeneration(fSubRun->maskFormat()));
969 } else {
970 // For the non-texCoords case we need to ensure that we update the associated use tokens
971 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
972 fUploadTarget->tokenTracker()->nextDrawToken(),
973 fSubRun->maskFormat());
974 }
975 return true;
976}
977
978bool GrTextBlob::VertexRegenerator::regenerate(GrTextBlob::VertexRegenerator::Result* result) {
979 uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
980 // If regenerate() is called multiple times then the atlas gen may have changed. So we check
981 // this each time.
Herb Derby73630212019-12-13 16:29:14 -0500982 fActions.regenTextureCoordinates |= fSubRun->atlasGeneration() != currentAtlasGen;
Herb Derbya9047642019-12-06 12:12:11 -0500983
Herb Derby73630212019-12-13 16:29:14 -0500984 if (fActions.regenStrike
985 |fActions.regenTextureCoordinates
986 |fActions.regenColor
987 |fActions.regenPositions) {
988 return this->doRegen(result);
Herb Derbya9047642019-12-06 12:12:11 -0500989 } else {
Herb Derbya2d72252019-12-23 15:02:33 -0500990 auto vertexStride = fSubRun->vertexStride();
Herb Derbya9047642019-12-06 12:12:11 -0500991 result->fFinished = true;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500992 result->fGlyphsRegenerated = fSubRun->fGlyphs.size() - fCurrGlyph;
993 result->fFirstVertex = fSubRun->fVertexData.data() +
994 fCurrGlyph * kVerticesPerGlyph * vertexStride;
995 fCurrGlyph = fSubRun->fGlyphs.size();
Herb Derbya9047642019-12-06 12:12:11 -0500996
997 // set use tokens for all of the glyphs in our subrun. This is only valid if we
998 // have a valid atlas generation
999 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
1000 fUploadTarget->tokenTracker()->nextDrawToken(),
1001 fSubRun->maskFormat());
1002 return true;
1003 }
1004 SK_ABORT("Should not get here");
1005}
1006
1007
1008
1009