blob: 9a48d91a327e42059ebfb793ac89e575bc281441 [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"
Herb Derby91fd46a2019-12-26 11:36:30 -050010#include "include/private/SkTemplates.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkMaskFilterBase.h"
12#include "src/core/SkPaintPriv.h"
13#include "src/gpu/GrBlurUtils.h"
14#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrStyle.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040016#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/ops/GrAtlasTextOp.h"
Herb Derbya9047642019-12-06 12:12:11 -050018#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/text/GrTextBlob.h"
20#include "src/gpu/text/GrTextTarget.h"
Ben Wagner75d6db72018-09-20 14:39:39 -040021
Herb Derby3d3150c2019-12-23 15:26:44 -050022#include <cstddef>
Mike Klein79aea6a2018-06-11 10:45:26 -040023#include <new>
joshualitt2e2202e2015-12-10 11:22:08 -080024
Herb Derbya9047642019-12-06 12:12:11 -050025static SkMatrix make_inverse(const SkMatrix& matrix) {
26 SkMatrix inverseMatrix;
27 if (!matrix.invert(&inverseMatrix)) {
28 inverseMatrix = SkMatrix::I();
29 }
30 return inverseMatrix;
31}
32
33// -- GrTextBlob::Key ------------------------------------------------------------------------------
34GrTextBlob::Key::Key() { sk_bzero(this, sizeof(Key)); }
35
36bool GrTextBlob::Key::operator==(const GrTextBlob::Key& other) const {
37 return 0 == memcmp(this, &other, sizeof(Key));
38}
39
40// -- GrTextBlob::PathGlyph ------------------------------------------------------------------------
41GrTextBlob::PathGlyph::PathGlyph(const SkPath& path, SkPoint origin)
42 : fPath(path)
43 , fOrigin(origin) {}
44
45// -- GrTextBlob::SubRun ---------------------------------------------------------------------------
Herb Derbye3c7ff42019-12-10 17:49:28 -050046// Hold data to draw the different types of sub run. SubRuns are produced knowing all the
47// glyphs that are included in them.
48class GrTextBlob::SubRun {
49public:
50 // SubRun for masks
51 SubRun(SubRunType type,
52 GrTextBlob* textBlob,
53 const SkStrikeSpec& strikeSpec,
54 GrMaskFormat format,
Herb Derbyc514e7d2019-12-11 17:00:31 -050055 const SkSpan<GrGlyph*>& glyphs, const SkSpan<char>& vertexData,
Herb Derbye3c7ff42019-12-10 17:49:28 -050056 sk_sp<GrTextStrike>&& grStrike);
57
58 // SubRun for paths
59 SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec);
60
61 void appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables);
62
63 // TODO when this object is more internal, drop the privacy
64 void resetBulkUseToken();
65 GrDrawOpAtlas::BulkUseTokenUpdater* bulkUseToken();
66 void setStrike(sk_sp<GrTextStrike> strike);
67 GrTextStrike* strike() const;
Herb Derbye3c7ff42019-12-10 17:49:28 -050068
Herb Derbye3c7ff42019-12-10 17:49:28 -050069 GrMaskFormat maskFormat() const;
70
Herb Derbya2d72252019-12-23 15:02:33 -050071 size_t vertexStride() const;
Herb Derby3d3150c2019-12-23 15:26:44 -050072 size_t colorOffset() const;
Herb Derbya56a1d72019-12-27 12:12:47 -050073 size_t texCoordOffset() const;
Herb Derby91fd46a2019-12-26 11:36:30 -050074 char* quadStart(size_t index) const;
Herb Derbya2d72252019-12-23 15:02:33 -050075
Herb Derbye3c7ff42019-12-10 17:49:28 -050076 const SkRect& vertexBounds() const;
77 void joinGlyphBounds(const SkRect& glyphBounds);
78
Herb Derbye3c7ff42019-12-10 17:49:28 -050079 bool drawAsDistanceFields() const;
80 bool drawAsPaths() const;
81 bool needsTransform() const;
Herb Derbye3c7ff42019-12-10 17:49:28 -050082
Herb Derbydf2c1ee2019-12-26 17:54:41 -050083 void translateVerticesIfNeeded(const SkMatrix& drawMatrix, SkPoint drawOrigin);
84 void updateVerticesColorIfNeeded(GrColor newColor);
Herb Derbybc131b82020-01-07 16:07:42 -050085 void updateTexCoords(int begin, int end);
Herb Derby91fd46a2019-12-26 11:36:30 -050086
Herb Derbye3c7ff42019-12-10 17:49:28 -050087 // df properties
88 void setUseLCDText(bool useLCDText);
89 bool hasUseLCDText() const;
90 void setAntiAliased(bool antiAliased);
91 bool isAntiAliased() const;
92
93 const SkStrikeSpec& strikeSpec() const;
94
95 SubRun* fNextSubRun{nullptr};
96 const SubRunType fType;
97 GrTextBlob* const fBlob;
98 const GrMaskFormat fMaskFormat;
Herb Derbyc514e7d2019-12-11 17:00:31 -050099 const SkSpan<GrGlyph*> fGlyphs;
100 const SkSpan<char> fVertexData;
Herb Derbye3c7ff42019-12-10 17:49:28 -0500101 const SkStrikeSpec fStrikeSpec;
102 sk_sp<GrTextStrike> fStrike;
103 struct {
104 bool useLCDText:1;
105 bool antiAliased:1;
106 } fFlags{false, false};
Herb Derbye3c7ff42019-12-10 17:49:28 -0500107 GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
108 SkRect fVertexBounds = SkRectPriv::MakeLargestInverted();
109 uint64_t fAtlasGeneration{GrDrawOpAtlas::kInvalidAtlasGeneration};
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500110 GrColor fCurrentColor;
Herb Derby5bf5b042019-12-12 16:37:03 -0500111 SkPoint fCurrentOrigin;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500112 SkMatrix fCurrentMatrix;
Herb Derbye3c7ff42019-12-10 17:49:28 -0500113 std::vector<PathGlyph> fPaths;
Herb Derby05eb83b2019-12-23 15:42:47 -0500114
115private:
116 bool hasW() const;
117
Herb Derbye3c7ff42019-12-10 17:49:28 -0500118}; // SubRun
119
Herb Derbya9047642019-12-06 12:12:11 -0500120GrTextBlob::SubRun::SubRun(SubRunType type, GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec,
Herb Derbyc514e7d2019-12-11 17:00:31 -0500121 GrMaskFormat format,
122 const SkSpan<GrGlyph*>& glyphs, const SkSpan<char>& vertexData,
Herb Derbya9047642019-12-06 12:12:11 -0500123 sk_sp<GrTextStrike>&& grStrike)
124 : fType{type}
125 , fBlob{textBlob}
126 , fMaskFormat{format}
Herb Derbyc514e7d2019-12-11 17:00:31 -0500127 , fGlyphs{glyphs}
128 , fVertexData{vertexData}
Herb Derbya9047642019-12-06 12:12:11 -0500129 , fStrikeSpec{strikeSpec}
130 , fStrike{grStrike}
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500131 , fCurrentColor{textBlob->fColor}
Herb Derby5bf5b042019-12-12 16:37:03 -0500132 , fCurrentOrigin{textBlob->fInitialOrigin}
Herb Derby1c5be7b2019-12-13 12:03:06 -0500133 , fCurrentMatrix{textBlob->fInitialMatrix} {
Herb Derbya9047642019-12-06 12:12:11 -0500134 SkASSERT(type != kTransformedPath);
Herb Derbycb718892019-12-07 00:07:42 -0500135 textBlob->insertSubRun(this);
Herb Derbya9047642019-12-06 12:12:11 -0500136}
137
138GrTextBlob::SubRun::SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec)
139 : fType{kTransformedPath}
140 , fBlob{textBlob}
141 , fMaskFormat{kA8_GrMaskFormat}
Herb Derbyc514e7d2019-12-11 17:00:31 -0500142 , fGlyphs{SkSpan<GrGlyph*>{}}
143 , fVertexData{SkSpan<char>{}}
Herb Derbya9047642019-12-06 12:12:11 -0500144 , fStrikeSpec{strikeSpec}
145 , fStrike{nullptr}
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500146 , fCurrentColor{textBlob->fColor}
Herb Derbycb718892019-12-07 00:07:42 -0500147 , fPaths{} {
148 textBlob->insertSubRun(this);
149}
Herb Derbya9047642019-12-06 12:12:11 -0500150
151void GrTextBlob::SubRun::appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables) {
152 GrTextStrike* grStrike = fStrike.get();
153 SkScalar strikeToSource = fStrikeSpec.strikeToSourceRatio();
Herb Derbyc514e7d2019-12-11 17:00:31 -0500154 GrGlyph** glyphCursor = fGlyphs.data();
155 char* vertexCursor = fVertexData.data();
Herb Derbya2d72252019-12-23 15:02:33 -0500156 size_t vertexStride = this->vertexStride();
Herb Derbya9047642019-12-06 12:12:11 -0500157 // We always write the third position component used by SDFs. If it is unused it gets
158 // overwritten. Similarly, we always write the color and the blob will later overwrite it
159 // with texture coords if it is unused.
Herb Derby3d3150c2019-12-23 15:26:44 -0500160 size_t colorOffset = this->colorOffset();
Herb Derbya9047642019-12-06 12:12:11 -0500161 for (auto [variant, pos] : drawables) {
162 SkGlyph* skGlyph = variant;
163 GrGlyph* grGlyph = grStrike->getGlyph(*skGlyph);
164 // Only floor the device coordinates.
165 SkRect dstRect;
166 if (!this->needsTransform()) {
167 pos = {SkScalarFloorToScalar(pos.x()), SkScalarFloorToScalar(pos.y())};
168 dstRect = grGlyph->destRect(pos);
169 } else {
170 dstRect = grGlyph->destRect(pos, strikeToSource);
171 }
172
173 this->joinGlyphBounds(dstRect);
174
Herb Derbya9047642019-12-06 12:12:11 -0500175 // V0
Herb Derbyc514e7d2019-12-11 17:00:31 -0500176 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fTop, 1.f};
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500177 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = fCurrentColor;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500178 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500179
180 // V1
Herb Derbyc514e7d2019-12-11 17:00:31 -0500181 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fBottom, 1.f};
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500182 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = fCurrentColor;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500183 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500184
185 // V2
Herb Derbyc514e7d2019-12-11 17:00:31 -0500186 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fTop, 1.f};
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500187 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = fCurrentColor;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500188 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500189
190 // V3
Herb Derbyc514e7d2019-12-11 17:00:31 -0500191 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fBottom, 1.f};
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500192 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = fCurrentColor;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500193 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500194
Herb Derbyc514e7d2019-12-11 17:00:31 -0500195 *glyphCursor++ = grGlyph;
Herb Derbya9047642019-12-06 12:12:11 -0500196 }
Herb Derbya9047642019-12-06 12:12:11 -0500197}
198
199void GrTextBlob::SubRun::resetBulkUseToken() { fBulkUseToken.reset(); }
200
201GrDrawOpAtlas::BulkUseTokenUpdater* GrTextBlob::SubRun::bulkUseToken() { return &fBulkUseToken; }
202void GrTextBlob::SubRun::setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
203GrTextStrike* GrTextBlob::SubRun::strike() const { return fStrike.get(); }
Herb Derbya9047642019-12-06 12:12:11 -0500204GrMaskFormat GrTextBlob::SubRun::maskFormat() const { return fMaskFormat; }
Herb Derbya2d72252019-12-23 15:02:33 -0500205size_t GrTextBlob::SubRun::vertexStride() const {
206 return GetVertexStride(this->maskFormat(), this->hasW());
207}
Herb Derby3d3150c2019-12-23 15:26:44 -0500208size_t GrTextBlob::SubRun::colorOffset() const {
209 return this->hasW() ? offsetof(SDFT3DVertex, color) : offsetof(Mask2DVertex, color);
210}
Herb Derbya56a1d72019-12-27 12:12:47 -0500211
212size_t GrTextBlob::SubRun::texCoordOffset() const {
213 switch (fMaskFormat) {
214 case kA8_GrMaskFormat:
215 return this->hasW() ? offsetof(SDFT3DVertex, atlasPos)
216 : offsetof(Mask2DVertex, atlasPos);
217 case kARGB_GrMaskFormat:
218 return this->hasW() ? offsetof(ARGB3DVertex, atlasPos)
219 : offsetof(ARGB2DVertex, atlasPos);
220 default:
221 SkASSERT(!this->hasW());
222 return offsetof(Mask2DVertex, atlasPos);
223 }
224}
225
Herb Derby91fd46a2019-12-26 11:36:30 -0500226char* GrTextBlob::SubRun::quadStart(size_t index) const {
227 return SkTAddOffset<char>(
228 fVertexData.data(), index * kVerticesPerGlyph * this->vertexStride());
229}
Herb Derbya2d72252019-12-23 15:02:33 -0500230
Herb Derbya9047642019-12-06 12:12:11 -0500231const SkRect& GrTextBlob::SubRun::vertexBounds() const { return fVertexBounds; }
232void GrTextBlob::SubRun::joinGlyphBounds(const SkRect& glyphBounds) {
233 fVertexBounds.joinNonEmptyArg(glyphBounds);
234}
235
Herb Derbya9047642019-12-06 12:12:11 -0500236bool GrTextBlob::SubRun::drawAsDistanceFields() const { return fType == kTransformedSDFT; }
237
238bool GrTextBlob::SubRun::drawAsPaths() const { return fType == kTransformedPath; }
239
240bool GrTextBlob::SubRun::needsTransform() const {
241 return fType == kTransformedPath
242 || fType == kTransformedMask
243 || fType == kTransformedSDFT;
244}
245
246bool GrTextBlob::SubRun::hasW() const {
247 return fBlob->hasW(fType);
248}
249
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500250void GrTextBlob::SubRun::translateVerticesIfNeeded(
251 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
252 SkVector translation;
253 if (this->needsTransform()) {
254 // If transform is needed, then the vertices are in source space, calculate the source
255 // space translation.
256 translation = drawOrigin - fCurrentOrigin;
257 } else {
258 // Calculate the translation in source space to a translation in device space. Calculate
259 // the translation by mapping (0, 0) through both the current matrix, and the draw
260 // matrix, and taking the difference.
261 SkMatrix currentMatrix{fCurrentMatrix};
262 currentMatrix.preTranslate(fCurrentOrigin.x(), fCurrentOrigin.y());
263 SkPoint currentDeviceOrigin{0, 0};
264 currentMatrix.mapPoints(&currentDeviceOrigin, 1);
265 SkMatrix completeDrawMatrix{drawMatrix};
266 completeDrawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
267 SkPoint drawDeviceOrigin{0, 0};
268 completeDrawMatrix.mapPoints(&drawDeviceOrigin, 1);
269 translation = drawDeviceOrigin - currentDeviceOrigin;
270 }
271
272 if (translation != SkPoint{0, 0}) {
273 size_t vertexStride = this->vertexStride();
274 for (size_t quad = 0; quad < fGlyphs.size(); quad++) {
275 SkPoint* vertexCursor = reinterpret_cast<SkPoint*>(quadStart(quad));
276 for (int i = 0; i < 4; ++i) {
277 *vertexCursor += translation;
278 vertexCursor = SkTAddOffset<SkPoint>(vertexCursor, vertexStride);
279 }
Herb Derby91fd46a2019-12-26 11:36:30 -0500280 }
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500281 fCurrentMatrix = drawMatrix;
282 fCurrentOrigin = drawOrigin;
Herb Derby91fd46a2019-12-26 11:36:30 -0500283 }
284}
285
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500286void GrTextBlob::SubRun::updateVerticesColorIfNeeded(GrColor newColor) {
287 if (this->maskFormat() != kARGB_GrMaskFormat && fCurrentColor != newColor) {
288 size_t vertexStride = this->vertexStride();
289 size_t colorOffset = this->colorOffset();
290 for (size_t quad = 0; quad < fGlyphs.size(); quad++) {
291 GrColor* colorCursor = SkTAddOffset<GrColor>(quadStart(quad), colorOffset);
292 for (int i = 0; i < 4; ++i) {
293 *colorCursor = newColor;
294 colorCursor = SkTAddOffset<GrColor>(colorCursor, vertexStride);
295 }
Herb Derby6ca4f312019-12-26 15:23:49 -0500296 }
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500297 this->fCurrentColor = newColor;
Herb Derby6ca4f312019-12-26 15:23:49 -0500298 }
Herb Derby6ca4f312019-12-26 15:23:49 -0500299}
300
Herb Derbybc131b82020-01-07 16:07:42 -0500301void GrTextBlob::SubRun::updateTexCoords(int begin, int end) {
302 const size_t vertexStride = this->vertexStride();
303 const size_t texCoordOffset = this->texCoordOffset();
304 char* vertex = this->quadStart(begin);
Herb Derbya56a1d72019-12-27 12:12:47 -0500305 uint16_t* textureCoords = reinterpret_cast<uint16_t*>(vertex + texCoordOffset);
Herb Derbybc131b82020-01-07 16:07:42 -0500306 for (int i = begin; i < end; i++) {
307 GrGlyph* glyph = this->fGlyphs[i];
308 SkASSERT(glyph != nullptr);
309
310 int width = glyph->fBounds.width();
311 int height = glyph->fBounds.height();
312 uint16_t u0, v0, u1, v1;
313 if (this->drawAsDistanceFields()) {
314 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
315 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
316 u1 = u0 + width - 2 * SK_DistanceFieldInset;
317 v1 = v0 + height - 2 * SK_DistanceFieldInset;
318 } else {
319 u0 = glyph->fAtlasLocation.fX;
320 v0 = glyph->fAtlasLocation.fY;
321 u1 = u0 + width;
322 v1 = v0 + height;
323 }
324
325 // We pack the 2bit page index in the low bit of the u and v texture coords
326 uint32_t pageIndex = glyph->pageIndex();
327 SkASSERT(pageIndex < 4);
328 uint16_t uBit = (pageIndex >> 1u) & 0x1u;
329 uint16_t vBit = pageIndex & 0x1u;
330 u0 <<= 1u;
331 u0 |= uBit;
332 v0 <<= 1u;
333 v0 |= vBit;
334 u1 <<= 1u;
335 u1 |= uBit;
336 v1 <<= 1u;
337 v1 |= vBit;
338
339 textureCoords[0] = u0;
340 textureCoords[1] = v0;
341 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
342 textureCoords[0] = u0;
343 textureCoords[1] = v1;
344 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
345 textureCoords[0] = u1;
346 textureCoords[1] = v0;
347 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
348 textureCoords[0] = u1;
349 textureCoords[1] = v1;
350 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
351 }
Herb Derbya56a1d72019-12-27 12:12:47 -0500352}
353
Herb Derbya9047642019-12-06 12:12:11 -0500354void GrTextBlob::SubRun::setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
355bool GrTextBlob::SubRun::hasUseLCDText() const { return fFlags.useLCDText; }
356void GrTextBlob::SubRun::setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
357bool GrTextBlob::SubRun::isAntiAliased() const { return fFlags.antiAliased; }
358const SkStrikeSpec& GrTextBlob::SubRun::strikeSpec() const { return fStrikeSpec; }
359
360// -- GrTextBlob -----------------------------------------------------------------------------------
361void GrTextBlob::operator delete(void* p) { ::operator delete(p); }
362void* GrTextBlob::operator new(size_t) { SK_ABORT("All blobs are created by placement new."); }
363void* GrTextBlob::operator new(size_t, void* p) { return p; }
364
365GrTextBlob::~GrTextBlob() = default;
366
Herb Derby659e4092019-12-06 15:38:10 -0500367sk_sp<GrTextBlob> GrTextBlob::Make(const SkGlyphRunList& glyphRunList,
Herb Derbyeba195f2019-12-03 16:44:47 -0500368 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500369 const SkMatrix& drawMatrix,
Herb Derbya00da612019-03-04 17:10:01 -0500370 GrColor color,
Herb Derbyeba195f2019-12-03 16:44:47 -0500371 bool forceWForDistanceFields) {
Herb Derbycd498e12019-12-06 14:17:31 -0500372
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500373 static_assert(sizeof(ARGB2DVertex) <= sizeof(Mask2DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500374 static_assert(alignof(ARGB2DVertex) <= alignof(Mask2DVertex));
375 size_t quadSize = sizeof(Mask2DVertex) * kVerticesPerGlyph;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500376 if (drawMatrix.hasPerspective() || forceWForDistanceFields) {
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500377 static_assert(sizeof(ARGB3DVertex) <= sizeof(SDFT3DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500378 static_assert(alignof(ARGB3DVertex) <= alignof(SDFT3DVertex));
379 quadSize = sizeof(SDFT3DVertex) * kVerticesPerGlyph;
Herb Derbye74c7762019-12-04 14:15:41 -0500380 }
381
Herb Derbycb718892019-12-07 00:07:42 -0500382 // We can use the alignment of SDFT3DVertex as a proxy for all Vertex alignments.
383 static_assert(alignof(SDFT3DVertex) >= alignof(Mask2DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500384 // Assume there is no padding needed between glyph pointers and vertices.
Herb Derbycb718892019-12-07 00:07:42 -0500385 static_assert(alignof(GrGlyph*) >= alignof(SDFT3DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500386
387 // In the arena, the layout is GrGlyph*... | SDFT3DVertex... | SubRun, so there is no padding
388 // between GrGlyph* and SDFT3DVertex, but padding is needed between the Mask2DVertex array
389 // and the SubRun.
390 size_t vertexToSubRunPadding = alignof(SDFT3DVertex) - alignof(SubRun);
391 size_t arenaSize =
392 sizeof(GrGlyph*) * glyphRunList.totalGlyphCount()
393 + quadSize * glyphRunList.totalGlyphCount()
394 + glyphRunList.runCount() * (sizeof(SubRun) + vertexToSubRunPadding);
395
396 size_t allocationSize = sizeof(GrTextBlob) + arenaSize;
Ben Wagner75d6db72018-09-20 14:39:39 -0400397
Herb Derbycb718892019-12-07 00:07:42 -0500398 void* allocation = ::operator new (allocationSize);
Herb Derbyb12175f2018-05-23 16:38:09 -0400399
Herb Derbyaebc5f82019-12-10 14:07:10 -0500400 SkColor initialLuminance = SkPaintPriv::ComputeLuminanceColor(glyphRunList.paint());
Herb Derby00ae9592019-12-03 15:55:56 -0500401 sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{
Herb Derby1c5be7b2019-12-13 12:03:06 -0500402 arenaSize, strikeCache, drawMatrix, glyphRunList.origin(),
Herb Derbyaebc5f82019-12-10 14:07:10 -0500403 color, initialLuminance, forceWForDistanceFields}};
joshualitt92303772016-02-10 11:55:52 -0800404
Herb Derbyf7d5d742018-11-16 13:24:32 -0500405 return blob;
joshualitt92303772016-02-10 11:55:52 -0800406}
407
Herb Derbya9047642019-12-06 12:12:11 -0500408void GrTextBlob::setupKey(const GrTextBlob::Key& key, const SkMaskFilterBase::BlurRec& blurRec,
409 const SkPaint& paint) {
410 fKey = key;
411 if (key.fHasBlur) {
412 fBlurRec = blurRec;
413 }
414 if (key.fStyle != SkPaint::kFill_Style) {
415 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
416 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
417 fStrokeInfo.fJoin = paint.getStrokeJoin();
418 }
419}
420const GrTextBlob::Key& GrTextBlob::GetKey(const GrTextBlob& blob) { return blob.fKey; }
421uint32_t GrTextBlob::Hash(const GrTextBlob::Key& key) { return SkOpts::hash(&key, sizeof(Key)); }
422
Herb Derbycb718892019-12-07 00:07:42 -0500423bool GrTextBlob::hasDistanceField() const {
424 return SkToBool(fTextType & kHasDistanceField_TextType);
425}
Herb Derbya9047642019-12-06 12:12:11 -0500426bool GrTextBlob::hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
Herb Derby1c5be7b2019-12-13 12:03:06 -0500427bool GrTextBlob::hasPerspective() const { return fInitialMatrix.hasPerspective(); }
Herb Derbya9047642019-12-06 12:12:11 -0500428
429void GrTextBlob::setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
430void GrTextBlob::setHasBitmap() { fTextType |= kHasBitmap_TextType; }
431void GrTextBlob::setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) {
432 // we init fMaxMinScale and fMinMaxScale in the constructor
433 fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale);
434 fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale);
435}
436
437size_t GrTextBlob::GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
438 switch (maskFormat) {
439 case kA8_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500440 return hasWCoord ? sizeof(SDFT3DVertex) : sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500441 case kARGB_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500442 return hasWCoord ? sizeof(ARGB3DVertex) : sizeof(ARGB2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500443 default:
444 SkASSERT(!hasWCoord);
Herb Derbycd498e12019-12-06 14:17:31 -0500445 return sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500446 }
447}
448
Herb Derby0edb2142018-10-16 17:04:11 -0400449bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
450 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derby5bf5b042019-12-12 16:37:03 -0500451 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
joshualittfd5f6c12015-12-10 07:44:50 -0800452 // If we have LCD text then our canonical color will be set to transparent, in this case we have
453 // to regenerate the blob on any color change
454 // We use the grPaint to get any color filter effects
455 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbyaebc5f82019-12-10 14:07:10 -0500456 fInitialLuminance != SkPaintPriv::ComputeLuminanceColor(paint)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800457 return true;
458 }
459
Herb Derby1c5be7b2019-12-13 12:03:06 -0500460 if (fInitialMatrix.hasPerspective() != drawMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800461 return true;
462 }
463
Brian Salomon5c6ac642017-12-19 11:09:32 -0500464 /** This could be relaxed for blobs with only distance field glyphs. */
Mike Reed2c383152019-12-18 16:47:47 -0500465 if (fInitialMatrix.hasPerspective() && !SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800466 return true;
467 }
468
469 // We only cache one masked version
470 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400471 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800472 return true;
473 }
474
475 // Similarly, we only cache one version for each style
476 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400477 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
478 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
479 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800480 return true;
481 }
482
483 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
484 // for mixed blobs if this becomes an issue.
485 if (this->hasBitmap() && this->hasDistanceField()) {
Herb Derbyeba195f2019-12-03 16:44:47 -0500486 // Identical view matrices and we can reuse in all cases
Mike Reed2c383152019-12-18 16:47:47 -0500487 return !(SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix) &&
488 drawOrigin == fInitialOrigin);
joshualittfd5f6c12015-12-10 07:44:50 -0800489 }
490
491 if (this->hasBitmap()) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500492 if (fInitialMatrix.getScaleX() != drawMatrix.getScaleX() ||
493 fInitialMatrix.getScaleY() != drawMatrix.getScaleY() ||
494 fInitialMatrix.getSkewX() != drawMatrix.getSkewX() ||
495 fInitialMatrix.getSkewY() != drawMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800496 return true;
497 }
498
Herb Derby9830fc42019-09-12 10:58:26 -0400499 // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust
500 // the quads properly. Devise a system that regenerates the quads from original data
501 // using the transform to allow this to be used in general.
502
503 // We can update the positions in the text blob without regenerating the whole
504 // blob, but only for integer translations.
505 // This cool bit of math will determine the necessary translation to apply to the
506 // already generated vertex coordinates to move them to the correct position.
507 // Figure out the translation in view space given a translation in source space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500508 SkScalar transX = drawMatrix.getTranslateX() +
Herb Derby5bf5b042019-12-12 16:37:03 -0500509 drawMatrix.getScaleX() * (drawOrigin.x() - fInitialOrigin.x()) +
510 drawMatrix.getSkewX() * (drawOrigin.y() - fInitialOrigin.y()) -
Herb Derby1c5be7b2019-12-13 12:03:06 -0500511 fInitialMatrix.getTranslateX();
512 SkScalar transY = drawMatrix.getTranslateY() +
Herb Derby5bf5b042019-12-12 16:37:03 -0500513 drawMatrix.getSkewY() * (drawOrigin.x() - fInitialOrigin.x()) +
514 drawMatrix.getScaleY() * (drawOrigin.y() - fInitialOrigin.y()) -
Herb Derby1c5be7b2019-12-13 12:03:06 -0500515 fInitialMatrix.getTranslateY();
Herb Derby9830fc42019-09-12 10:58:26 -0400516 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
517 return true;
joshualittfd5f6c12015-12-10 07:44:50 -0800518 }
joshualittfd5f6c12015-12-10 07:44:50 -0800519 } else if (this->hasDistanceField()) {
520 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
521 // distance field being generated, so we have to regenerate in those cases
Herb Derby1c5be7b2019-12-13 12:03:06 -0500522 SkScalar newMaxScale = drawMatrix.getMaxScale();
523 SkScalar oldMaxScale = fInitialMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800524 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
525 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
526 return true;
527 }
joshualittfd5f6c12015-12-10 07:44:50 -0800528 }
529
joshualittfd5f6c12015-12-10 07:44:50 -0800530 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
531 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
532 // the blob anyways at flush time, so no need to regenerate explicitly
533 return false;
534}
535
Herb Derbyc1b482c2018-08-09 15:02:27 -0400536void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
537 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400538 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Herb Derby5bf5b042019-12-12 16:37:03 -0500539 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500540
Herb Derbycb718892019-12-07 00:07:42 -0500541 for (SubRun* subRun = fFirstSubRun; subRun != nullptr; subRun = subRun->fNextSubRun) {
542 if (subRun->drawAsPaths()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400543 SkPaint runPaint{paint};
Herb Derbycb718892019-12-07 00:07:42 -0500544 runPaint.setAntiAlias(subRun->isAntiAliased());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500545 // If there are shaders, blurs or styles, the path must be scaled into source
546 // space independently of the CTM. This allows the CTM to be correct for the
547 // different effects.
548 GrStyle style(runPaint);
Herb Derby9f491482018-08-08 11:53:00 -0400549
Herb Derby1b8dcd12019-11-15 15:21:15 -0500550 bool scalePath = runPaint.getShader()
551 || style.applies()
552 || runPaint.getMaskFilter();
Robert Phillips137ca522018-08-15 10:14:33 -0400553
Herb Derby1b8dcd12019-11-15 15:21:15 -0500554 // The origin for the blob may have changed, so figure out the delta.
Herb Derby5bf5b042019-12-12 16:37:03 -0500555 SkVector originShift = drawOrigin - fInitialOrigin;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500556
Herb Derbycb718892019-12-07 00:07:42 -0500557 for (const auto& pathGlyph : subRun->fPaths) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500558 SkMatrix ctm{drawMatrix};
Herb Derbycb718892019-12-07 00:07:42 -0500559 SkMatrix pathMatrix = SkMatrix::MakeScale(
560 subRun->fStrikeSpec.strikeToSourceRatio());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500561 // Shift the original glyph location in source space to the position of the new
562 // blob.
563 pathMatrix.postTranslate(originShift.x() + pathGlyph.fOrigin.x(),
564 originShift.y() + pathGlyph.fOrigin.y());
Herb Derbydac1ed52018-09-12 17:04:21 -0400565
566 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400567 SkTLazy<SkPath> tmpPath;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500568 const SkPath* path = &pathGlyph.fPath;
Herb Derby2984d262019-11-20 14:40:39 -0500569 if (!scalePath) {
570 // Scale can be applied to CTM -- no effects.
Herb Derby2984d262019-11-20 14:40:39 -0500571 ctm.preConcat(pathMatrix);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400572 } else {
Herb Derby2984d262019-11-20 14:40:39 -0500573 // Scale the outline into source space.
Herb Derbydac1ed52018-09-12 17:04:21 -0400574
Herb Derby2984d262019-11-20 14:40:39 -0500575 // Transform the path form the normalized outline to source space. This
576 // way the CTM will remain the same so it can be used by the effects.
577 SkPath* sourceOutline = tmpPath.init();
578 path->transform(pathMatrix, sourceOutline);
579 sourceOutline->setIsVolatile(true);
580 path = sourceOutline;
Robert Phillips137ca522018-08-15 10:14:33 -0400581 }
582
Robert Phillips46a13382018-08-23 13:53:01 -0400583 // TODO: we are losing the mutability of the path here
584 GrShape shape(*path, paint);
Herb Derbydac1ed52018-09-12 17:04:21 -0400585 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500586 }
Herb Derby1b8dcd12019-11-15 15:21:15 -0500587 } else {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500588 int glyphCount = subRun->fGlyphs.size();
Jim Van Verth54d9c882018-02-08 16:14:48 -0500589 if (0 == glyphCount) {
590 continue;
591 }
592
593 bool skipClip = false;
594 bool submitOp = true;
595 SkIRect clipRect = SkIRect::MakeEmpty();
596 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
597 SkRRect clipRRect;
598 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400599 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500600 // and we have an axis-aligned rectangular non-AA clip
Herb Derbycb718892019-12-07 00:07:42 -0500601 if (!subRun->drawAsDistanceFields() && !subRun->needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500602 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500603 clipRRect.isRect() && GrAA::kNo == aa) {
604 skipClip = true;
605 // We only need to do clipping work if the subrun isn't contained by the clip
606 SkRect subRunBounds;
Herb Derby5bf5b042019-12-12 16:37:03 -0500607 this->computeSubRunBounds(
608 &subRunBounds, *subRun, drawMatrix, drawOrigin, false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500609 if (!clipRRect.getBounds().contains(subRunBounds)) {
610 // If the subrun is completely outside, don't add an op for it
611 if (!clipRRect.getBounds().intersects(subRunBounds)) {
612 submitOp = false;
613 }
614 else {
615 clipRRect.getBounds().round(&clipRect);
616 }
617 }
618 }
619
620 if (submitOp) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500621 auto op = this->makeOp(*subRun, glyphCount, drawMatrix, drawOrigin,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400622 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500623 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500624 if (op) {
625 if (skipClip) {
626 target->addDrawOp(GrNoClip(), std::move(op));
627 }
628 else {
629 target->addDrawOp(clip, std::move(op));
630 }
631 }
632 }
633 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000634 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000635}
636
Herb Derby1c5be7b2019-12-13 12:03:06 -0500637void GrTextBlob::computeSubRunBounds(SkRect* outBounds, const SubRun& subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500638 const SkMatrix& drawMatrix, SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500639 bool needsGlyphTransform) {
640 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
641 // into device space.
642 // We handle vertex bounds differently for distance field text and bitmap text because
643 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
644 // from one blob then we are going to pay the price here of mapping the rect for each run.
645 *outBounds = subRun.vertexBounds();
646 if (needsGlyphTransform) {
647 // Distance field text is positioned with the (X,Y) as part of the glyph position,
648 // and currently the view matrix is applied on the GPU
Herb Derby5bf5b042019-12-12 16:37:03 -0500649 outBounds->offset(drawOrigin - fInitialOrigin);
Herb Derby1c5be7b2019-12-13 12:03:06 -0500650 drawMatrix.mapRect(outBounds);
Herb Derbya9047642019-12-06 12:12:11 -0500651 } else {
652 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
653 // device space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500654 SkMatrix boundsMatrix = fInitialMatrixInverse;
Herb Derbya9047642019-12-06 12:12:11 -0500655
656 boundsMatrix.postTranslate(-fInitialOrigin.x(), -fInitialOrigin.y());
657
Herb Derby5bf5b042019-12-12 16:37:03 -0500658 boundsMatrix.postTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derbya9047642019-12-06 12:12:11 -0500659
Herb Derby1c5be7b2019-12-13 12:03:06 -0500660 boundsMatrix.postConcat(drawMatrix);
Herb Derbya9047642019-12-06 12:12:11 -0500661 boundsMatrix.mapRect(outBounds);
662
663 // Due to floating point numerical inaccuracies, we have to round out here
664 outBounds->roundOut(outBounds);
665 }
joshualitt323c2eb2016-01-20 06:48:47 -0800666}
joshualitt2e2202e2015-12-10 11:22:08 -0800667
Herb Derbya9047642019-12-06 12:12:11 -0500668const GrTextBlob::Key& GrTextBlob::key() const { return fKey; }
669size_t GrTextBlob::size() const { return fSize; }
670
671std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Herb Derby1c5be7b2019-12-13 12:03:06 -0500672 int glyphCount, const SkMatrix& drawMatrix,
Herb Derby5bf5b042019-12-12 16:37:03 -0500673 SkPoint drawOrigin, const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbya9047642019-12-06 12:12:11 -0500674 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
675 GrTextTarget* target) {
Herb Derbycb718892019-12-07 00:07:42 -0500676 SubRun* info = fFirstSubRun;
Herb Derbya9047642019-12-06 12:12:11 -0500677 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derby5bf5b042019-12-12 16:37:03 -0500678 return this->makeOp(*info, glyphCount, drawMatrix, drawOrigin, emptyRect,
Herb Derbya9047642019-12-06 12:12:11 -0500679 paint, filteredColor, props, distanceAdjustTable, target);
680}
681
682bool GrTextBlob::hasW(GrTextBlob::SubRunType type) const {
683 if (type == kTransformedSDFT) {
684 return this->hasPerspective() || fForceWForDistanceFields;
685 } else if (type == kTransformedMask || type == kTransformedPath) {
686 return this->hasPerspective();
Herb Derbye9f691d2019-12-04 12:11:13 -0500687 }
Herb Derbya9047642019-12-06 12:12:11 -0500688
689 // The viewMatrix is implicitly SkMatrix::I when drawing kDirectMask, because it is not
690 // used.
691 return false;
692}
693
694GrTextBlob::SubRun* GrTextBlob::makeSubRun(SubRunType type,
695 const SkZip<SkGlyphVariant, SkPoint>& drawables,
696 const SkStrikeSpec& strikeSpec,
697 GrMaskFormat format) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500698 SkSpan<GrGlyph*> glyphs{fAlloc.makeArrayDefault<GrGlyph*>(drawables.size()), drawables.size()};
Herb Derbya9047642019-12-06 12:12:11 -0500699 bool hasW = this->hasW(type);
Herb Derby3d3150c2019-12-23 15:26:44 -0500700
701 SkASSERT(!fInitialMatrix.hasPerspective() || hasW);
702
Herb Derbyc514e7d2019-12-11 17:00:31 -0500703 size_t vertexDataSize = drawables.size() * GetVertexStride(format, hasW) * kVerticesPerGlyph;
704 SkSpan<char> vertexData{fAlloc.makeArrayDefault<char>(vertexDataSize), vertexDataSize};
Herb Derbya9047642019-12-06 12:12:11 -0500705
706 sk_sp<GrTextStrike> grStrike = strikeSpec.findOrCreateGrStrike(fStrikeCache);
707
Herb Derbycb718892019-12-07 00:07:42 -0500708 SubRun* subRun = fAlloc.make<SubRun>(
Herb Derbyc514e7d2019-12-11 17:00:31 -0500709 type, this, strikeSpec, format, glyphs, vertexData, std::move(grStrike));
Herb Derbya9047642019-12-06 12:12:11 -0500710
Herb Derbycb718892019-12-07 00:07:42 -0500711 subRun->appendGlyphs(drawables);
Herb Derbya9047642019-12-06 12:12:11 -0500712
Herb Derbycb718892019-12-07 00:07:42 -0500713 return subRun;
Herb Derbya9047642019-12-06 12:12:11 -0500714}
715
716void GrTextBlob::addSingleMaskFormat(
717 SubRunType type,
718 const SkZip<SkGlyphVariant, SkPoint>& drawables,
719 const SkStrikeSpec& strikeSpec,
720 GrMaskFormat format) {
721 this->makeSubRun(type, drawables, strikeSpec, format);
722}
723
724void GrTextBlob::addMultiMaskFormat(
725 SubRunType type,
726 const SkZip<SkGlyphVariant, SkPoint>& drawables,
727 const SkStrikeSpec& strikeSpec) {
728 this->setHasBitmap();
729 if (drawables.empty()) { return; }
730
731 auto glyphSpan = drawables.get<0>();
732 SkGlyph* glyph = glyphSpan[0];
733 GrMaskFormat format = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
734 size_t startIndex = 0;
735 for (size_t i = 1; i < drawables.size(); i++) {
736 glyph = glyphSpan[i];
737 GrMaskFormat nextFormat = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
738 if (format != nextFormat) {
739 auto sameFormat = drawables.subspan(startIndex, i - startIndex);
740 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
741 format = nextFormat;
742 startIndex = i;
743 }
744 }
745 auto sameFormat = drawables.last(drawables.size() - startIndex);
746 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
747}
748
749void GrTextBlob::addSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
750 const SkStrikeSpec& strikeSpec,
751 const SkFont& runFont,
752 SkScalar minScale,
753 SkScalar maxScale) {
754 this->setHasDistanceField();
755 this->setMinAndMaxScale(minScale, maxScale);
756
757 SubRun* subRun = this->makeSubRun(kTransformedSDFT, drawables, strikeSpec, kA8_GrMaskFormat);
758 subRun->setUseLCDText(runFont.getEdging() == SkFont::Edging::kSubpixelAntiAlias);
759 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbye9f691d2019-12-04 12:11:13 -0500760}
761
Herb Derbycb718892019-12-07 00:07:42 -0500762GrTextBlob::GrTextBlob(size_t allocSize,
Herb Derbyeba195f2019-12-03 16:44:47 -0500763 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500764 const SkMatrix& drawMatrix,
Herb Derbyeba195f2019-12-03 16:44:47 -0500765 SkPoint origin,
766 GrColor color,
Herb Derbyaebc5f82019-12-10 14:07:10 -0500767 SkColor initialLuminance,
Herb Derby00ae9592019-12-03 15:55:56 -0500768 bool forceWForDistanceFields)
Herb Derbycb718892019-12-07 00:07:42 -0500769 : fSize{allocSize}
Herb Derby00ae9592019-12-03 15:55:56 -0500770 , fStrikeCache{strikeCache}
Herb Derby1c5be7b2019-12-13 12:03:06 -0500771 , fInitialMatrix{drawMatrix}
772 , fInitialMatrixInverse{make_inverse(drawMatrix)}
Herb Derbyeba195f2019-12-03 16:44:47 -0500773 , fInitialOrigin{origin}
Herb Derby00ae9592019-12-03 15:55:56 -0500774 , fForceWForDistanceFields{forceWForDistanceFields}
Herb Derbycb718892019-12-07 00:07:42 -0500775 , fColor{color}
Herb Derbyaebc5f82019-12-10 14:07:10 -0500776 , fInitialLuminance{initialLuminance}
Herb Derbycb718892019-12-07 00:07:42 -0500777 , fAlloc{SkTAddOffset<char>(this, sizeof(GrTextBlob)), allocSize, allocSize/2} { }
Herb Derby00ae9592019-12-03 15:55:56 -0500778
Herb Derbycb718892019-12-07 00:07:42 -0500779void GrTextBlob::insertSubRun(SubRun* subRun) {
780 if (fFirstSubRun == nullptr) {
781 fFirstSubRun = subRun;
782 fLastSubRun = subRun;
783 } else {
784 fLastSubRun->fNextSubRun = subRun;
785 fLastSubRun = subRun;
786 }
787}
788
789std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Herb Derbya9047642019-12-06 12:12:11 -0500790 SubRun& info, int glyphCount,
Herb Derby5bf5b042019-12-12 16:37:03 -0500791 const SkMatrix& drawMatrix, SkPoint drawOrigin, const SkIRect& clipRect,
Herb Derbya9047642019-12-06 12:12:11 -0500792 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
793 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
794 GrMaskFormat format = info.maskFormat();
795
796 GrPaint grPaint;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500797 target->makeGrPaint(info.maskFormat(), paint, drawMatrix, &grPaint);
Herb Derbya9047642019-12-06 12:12:11 -0500798 std::unique_ptr<GrAtlasTextOp> op;
799 if (info.drawAsDistanceFields()) {
800 // TODO: Can we be even smarter based on the dest transfer function?
801 op = GrAtlasTextOp::MakeDistanceField(
802 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
803 target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint),
804 props, info.isAntiAliased(), info.hasUseLCDText());
Herb Derbyeba195f2019-12-03 16:44:47 -0500805 } else {
Herb Derbya9047642019-12-06 12:12:11 -0500806 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
807 info.needsTransform());
808 }
809 GrAtlasTextOp::Geometry& geometry = op->geometry();
Herb Derby1c5be7b2019-12-13 12:03:06 -0500810 geometry.fDrawMatrix = drawMatrix;
Herb Derbya9047642019-12-06 12:12:11 -0500811 geometry.fClipRect = clipRect;
812 geometry.fBlob = SkRef(this);
813 geometry.fSubRunPtr = &info;
814 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
Herb Derby5bf5b042019-12-12 16:37:03 -0500815 geometry.fDrawOrigin = drawOrigin;
Herb Derbya9047642019-12-06 12:12:11 -0500816 op->init();
817 return op;
818}
Herb Derbyeba195f2019-12-03 16:44:47 -0500819
Herb Derbya9047642019-12-06 12:12:11 -0500820void GrTextBlob::processDeviceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
821 const SkStrikeSpec& strikeSpec) {
822 this->addMultiMaskFormat(kDirectMask, drawables, strikeSpec);
823}
Herb Derbyeba195f2019-12-03 16:44:47 -0500824
Herb Derbya9047642019-12-06 12:12:11 -0500825void GrTextBlob::processSourcePaths(const SkZip<SkGlyphVariant, SkPoint>& drawables,
826 const SkFont& runFont,
827 const SkStrikeSpec& strikeSpec) {
828 this->setHasBitmap();
Herb Derbycb718892019-12-07 00:07:42 -0500829 SubRun* subRun = fAlloc.make<SubRun>(this, strikeSpec);
830 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbya9047642019-12-06 12:12:11 -0500831 for (auto [variant, pos] : drawables) {
Herb Derbycb718892019-12-07 00:07:42 -0500832 subRun->fPaths.emplace_back(*variant.path(), pos);
Herb Derbyeba195f2019-12-03 16:44:47 -0500833 }
834}
835
Herb Derbya9047642019-12-06 12:12:11 -0500836void GrTextBlob::processSourceSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
837 const SkStrikeSpec& strikeSpec,
838 const SkFont& runFont,
839 SkScalar minScale,
840 SkScalar maxScale) {
841 this->addSDFT(drawables, strikeSpec, runFont, minScale, maxScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800842}
Herb Derbya9047642019-12-06 12:12:11 -0500843
844void GrTextBlob::processSourceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
845 const SkStrikeSpec& strikeSpec) {
846 this->addMultiMaskFormat(kTransformedMask, drawables, strikeSpec);
847}
848
849// -- GrTextBlob::VertexRegenerator ----------------------------------------------------------------
Herb Derbya9047642019-12-06 12:12:11 -0500850GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
Herb Derbya9047642019-12-06 12:12:11 -0500851 GrTextBlob::SubRun* subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500852 const SkMatrix& drawMatrix,
853 SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500854 GrColor color,
855 GrDeferredUploadTarget* uploadTarget,
856 GrStrikeCache* grStrikeCache,
857 GrAtlasManager* fullAtlasManager)
858 : fResourceProvider(resourceProvider)
Herb Derbya9047642019-12-06 12:12:11 -0500859 , fUploadTarget(uploadTarget)
860 , fGrStrikeCache(grStrikeCache)
861 , fFullAtlasManager(fullAtlasManager)
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500862 , fSubRun(subRun){
Herb Derbya9047642019-12-06 12:12:11 -0500863 // Because the GrStrikeCache may evict the strike a blob depends on using for
864 // generating its texture coords, we have to track whether or not the strike has
865 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
866 // otherwise we have to get the new strike, and use that to get the correct glyphs.
867 // Because we do not have the packed ids, and thus can't look up our glyphs in the
868 // new strike, we instead keep our ref to the old strike and use the packed ids from
869 // it. These ids will still be valid as long as we hold the ref. When we are done
870 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
Herb Derby73630212019-12-13 16:29:14 -0500871 fActions.regenTextureCoordinates = fSubRun->strike()->isAbandoned();
872 fActions.regenStrike = fSubRun->strike()->isAbandoned();
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500873
874 fSubRun->updateVerticesColorIfNeeded(color);
875 fSubRun->translateVerticesIfNeeded(drawMatrix, drawOrigin);
Herb Derbya9047642019-12-06 12:12:11 -0500876}
877
Herb Derbyb45558d2020-01-07 16:57:12 -0500878std::tuple<bool, int> GrTextBlob::VertexRegenerator::updateTextureCoordinatesMaybeStrike(
879 const int begin, const int end) {
Herb Derby586f8d02020-01-07 15:28:07 -0500880 SkASSERT(fActions.regenTextureCoordinates);
881 fSubRun->resetBulkUseToken();
Herb Derbya9047642019-12-06 12:12:11 -0500882
Herb Derby586f8d02020-01-07 15:28:07 -0500883 const SkStrikeSpec& strikeSpec = fSubRun->strikeSpec();
Herb Derbya9047642019-12-06 12:12:11 -0500884
Herb Derby586f8d02020-01-07 15:28:07 -0500885 if (!fMetricsAndImages.isValid()
Herb Derbya9047642019-12-06 12:12:11 -0500886 || fMetricsAndImages->descriptor() != strikeSpec.descriptor()) {
Herb Derby586f8d02020-01-07 15:28:07 -0500887 fMetricsAndImages.init(strikeSpec);
888 }
889
890 if (fActions.regenStrike) {
891 // Take the glyphs from the old strike, and translate them a new strike.
892 sk_sp<GrTextStrike> newStrike = strikeSpec.findOrCreateGrStrike(fGrStrikeCache);
893
894 // Start this batch at the start of the subRun plus any glyphs that were previously
895 // processed.
Herb Derbyb45558d2020-01-07 16:57:12 -0500896 SkSpan<GrGlyph*> glyphs = fSubRun->fGlyphs.last(fSubRun->fGlyphs.size() - begin);
Herb Derby586f8d02020-01-07 15:28:07 -0500897
898 // Convert old glyphs to newStrike.
899 for (auto& glyph : glyphs) {
900 SkPackedGlyphID id = glyph->fPackedID;
901 glyph = newStrike->getGlyph(id, fMetricsAndImages.get());
902 SkASSERT(id == glyph->fPackedID);
Herb Derbya9047642019-12-06 12:12:11 -0500903 }
904
Herb Derby586f8d02020-01-07 15:28:07 -0500905 fSubRun->setStrike(newStrike);
Herb Derbya9047642019-12-06 12:12:11 -0500906 }
907
Herb Derbybc131b82020-01-07 16:07:42 -0500908 // Update the atlas information in the GrStrike.
Herb Derby83b907e2020-01-06 13:44:29 -0500909 auto code = GrDrawOpAtlas::ErrorCode::kSucceeded;
Herb Derby586f8d02020-01-07 15:28:07 -0500910 GrTextStrike* grStrike = fSubRun->strike();
Herb Derbybc131b82020-01-07 16:07:42 -0500911 auto tokenTracker = fUploadTarget->tokenTracker();
Herb Derbyb45558d2020-01-07 16:57:12 -0500912 int i = begin;
913 for (; i < end; i++) {
914 GrGlyph* glyph = fSubRun->fGlyphs[i];
Herb Derby586f8d02020-01-07 15:28:07 -0500915 SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
Herb Derbya9047642019-12-06 12:12:11 -0500916
Herb Derby586f8d02020-01-07 15:28:07 -0500917 if (!fFullAtlasManager->hasGlyph(glyph)) {
918 code = grStrike->addGlyphToAtlas(
919 fResourceProvider, fUploadTarget, fGrStrikeCache, fFullAtlasManager, glyph,
920 fMetricsAndImages.get(), fSubRun->maskFormat(), fSubRun->needsTransform());
921 if (code != GrDrawOpAtlas::ErrorCode::kSucceeded) {
922 break;
Herb Derbya9047642019-12-06 12:12:11 -0500923 }
Herb Derbya9047642019-12-06 12:12:11 -0500924 }
Herb Derby586f8d02020-01-07 15:28:07 -0500925 fFullAtlasManager->addGlyphToBulkAndSetUseToken(
926 fSubRun->bulkUseToken(), glyph, tokenTracker->nextDrawToken());
Herb Derbya9047642019-12-06 12:12:11 -0500927 }
Herb Derbyb45558d2020-01-07 16:57:12 -0500928 int firstNotInAtlas = i;
Herb Derbya9047642019-12-06 12:12:11 -0500929
Herb Derbybc131b82020-01-07 16:07:42 -0500930 // Update the quads with the new atlas coordinates.
Herb Derbyb45558d2020-01-07 16:57:12 -0500931 fSubRun->updateTexCoords(begin, firstNotInAtlas);
Herb Derbybc131b82020-01-07 16:07:42 -0500932
Herb Derbyb45558d2020-01-07 16:57:12 -0500933 if (code == GrDrawOpAtlas::ErrorCode::kSucceeded) {
934 // If we reach here with begin > 0, some earlier call to regenerate() exhausted the atlas
935 // before it could place all its glyphs and returned kTryAgain. Invalidate texture
936 // coordinates, forcing them to be regenerated, minding the atlas flush between.
937 fSubRun->fAtlasGeneration =
938 begin > 0 ? GrDrawOpAtlas::kInvalidAtlasGeneration
939 : fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
Herb Derbya9047642019-12-06 12:12:11 -0500940 }
Herb Derby83b907e2020-01-06 13:44:29 -0500941
Herb Derbyb45558d2020-01-07 16:57:12 -0500942 return {code != GrDrawOpAtlas::ErrorCode::kError, firstNotInAtlas};
Herb Derbya9047642019-12-06 12:12:11 -0500943}
944
Brian Salomon43cbd722020-01-03 22:09:12 -0500945bool GrTextBlob::VertexRegenerator::regenerate(GrTextBlob::VertexRegenerator::Result* result,
946 int maxGlyphs) {
Herb Derbya9047642019-12-06 12:12:11 -0500947 uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
948 // If regenerate() is called multiple times then the atlas gen may have changed. So we check
949 // this each time.
Herb Derby5919cf62020-01-02 15:10:11 -0500950 fActions.regenTextureCoordinates |= fSubRun->fAtlasGeneration != currentAtlasGen;
Herb Derby586f8d02020-01-07 15:28:07 -0500951 if (fActions.regenStrike) { SkASSERT(fActions.regenTextureCoordinates); }
Herb Derbya9047642019-12-06 12:12:11 -0500952
Herb Derby51a95ce2020-01-08 17:49:51 -0500953 bool ok = true;
954 const int begin = fCurrGlyph;
955 const int end = std::min((int)fSubRun->fGlyphs.size(), begin + maxGlyphs);
956 if (fActions.regenStrike || fActions.regenTextureCoordinates) {
957 int firstGlyphNotInAtlas;
958 std::tie(ok, firstGlyphNotInAtlas) = this->updateTextureCoordinatesMaybeStrike(begin, end);
959 fCurrGlyph = firstGlyphNotInAtlas;
960 } else {
961 fCurrGlyph = end;
962 // All glyphs are inserted into the atlas if fCurrGlyph is at the end of fGlyphs.
963 if (fCurrGlyph == (int)fSubRun->fGlyphs.size()) {
964 // Set use tokens for all of the glyphs in our SubRun. This is only valid if we
Brian Salomon43cbd722020-01-03 22:09:12 -0500965 // have a valid atlas generation
966 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
967 fUploadTarget->tokenTracker()->nextDrawToken(),
968 fSubRun->maskFormat());
969 }
Herb Derbya9047642019-12-06 12:12:11 -0500970 }
Herb Derby51a95ce2020-01-08 17:49:51 -0500971 if (ok) {
972 result->fFinished = fCurrGlyph == (int)fSubRun->fGlyphs.size();
973 result->fGlyphsRegenerated += fCurrGlyph - begin;
974 result->fFirstVertex = fSubRun->quadStart(begin);
975 }
976 return ok;
Herb Derbya9047642019-12-06 12:12:11 -0500977}