blob: 49a6b907ca1b05e3c723ee720e423fc7e77ec3f1 [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 ---------------------------------------------------------------------------
46GrTextBlob::SubRun::SubRun(SubRunType type, GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec,
Herb Derbyc514e7d2019-12-11 17:00:31 -050047 GrMaskFormat format,
48 const SkSpan<GrGlyph*>& glyphs, const SkSpan<char>& vertexData,
Herb Derbya9047642019-12-06 12:12:11 -050049 sk_sp<GrTextStrike>&& grStrike)
50 : fType{type}
51 , fBlob{textBlob}
52 , fMaskFormat{format}
Herb Derbyc514e7d2019-12-11 17:00:31 -050053 , fGlyphs{glyphs}
54 , fVertexData{vertexData}
Herb Derbya9047642019-12-06 12:12:11 -050055 , fStrikeSpec{strikeSpec}
56 , fStrike{grStrike}
Herb Derbydf2c1ee2019-12-26 17:54:41 -050057 , fCurrentColor{textBlob->fColor}
Herb Derby5bf5b042019-12-12 16:37:03 -050058 , fCurrentOrigin{textBlob->fInitialOrigin}
Herb Derby1c5be7b2019-12-13 12:03:06 -050059 , fCurrentMatrix{textBlob->fInitialMatrix} {
Herb Derbya9047642019-12-06 12:12:11 -050060 SkASSERT(type != kTransformedPath);
Herb Derbycb718892019-12-07 00:07:42 -050061 textBlob->insertSubRun(this);
Herb Derbya9047642019-12-06 12:12:11 -050062}
63
64GrTextBlob::SubRun::SubRun(GrTextBlob* textBlob, const SkStrikeSpec& strikeSpec)
65 : fType{kTransformedPath}
66 , fBlob{textBlob}
67 , fMaskFormat{kA8_GrMaskFormat}
Herb Derbyc514e7d2019-12-11 17:00:31 -050068 , fGlyphs{SkSpan<GrGlyph*>{}}
69 , fVertexData{SkSpan<char>{}}
Herb Derbya9047642019-12-06 12:12:11 -050070 , fStrikeSpec{strikeSpec}
71 , fStrike{nullptr}
Herb Derbydf2c1ee2019-12-26 17:54:41 -050072 , fCurrentColor{textBlob->fColor}
Herb Derbycb718892019-12-07 00:07:42 -050073 , fPaths{} {
74 textBlob->insertSubRun(this);
75}
Herb Derbya9047642019-12-06 12:12:11 -050076
77void GrTextBlob::SubRun::appendGlyphs(const SkZip<SkGlyphVariant, SkPoint>& drawables) {
78 GrTextStrike* grStrike = fStrike.get();
79 SkScalar strikeToSource = fStrikeSpec.strikeToSourceRatio();
Herb Derbyc514e7d2019-12-11 17:00:31 -050080 GrGlyph** glyphCursor = fGlyphs.data();
81 char* vertexCursor = fVertexData.data();
Herb Derbya2d72252019-12-23 15:02:33 -050082 size_t vertexStride = this->vertexStride();
Herb Derbya9047642019-12-06 12:12:11 -050083 // We always write the third position component used by SDFs. If it is unused it gets
84 // overwritten. Similarly, we always write the color and the blob will later overwrite it
85 // with texture coords if it is unused.
Herb Derby3d3150c2019-12-23 15:26:44 -050086 size_t colorOffset = this->colorOffset();
Herb Derbya9047642019-12-06 12:12:11 -050087 for (auto [variant, pos] : drawables) {
88 SkGlyph* skGlyph = variant;
89 GrGlyph* grGlyph = grStrike->getGlyph(*skGlyph);
90 // Only floor the device coordinates.
91 SkRect dstRect;
92 if (!this->needsTransform()) {
93 pos = {SkScalarFloorToScalar(pos.x()), SkScalarFloorToScalar(pos.y())};
94 dstRect = grGlyph->destRect(pos);
95 } else {
96 dstRect = grGlyph->destRect(pos, strikeToSource);
97 }
98
99 this->joinGlyphBounds(dstRect);
100
Herb Derbya9047642019-12-06 12:12:11 -0500101 // V0
Herb Derbyc514e7d2019-12-11 17:00:31 -0500102 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fTop, 1.f};
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500103 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = fCurrentColor;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500104 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500105
106 // V1
Herb Derbyc514e7d2019-12-11 17:00:31 -0500107 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fLeft, dstRect.fBottom, 1.f};
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500108 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = fCurrentColor;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500109 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500110
111 // V2
Herb Derbyc514e7d2019-12-11 17:00:31 -0500112 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fTop, 1.f};
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500113 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = fCurrentColor;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500114 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500115
116 // V3
Herb Derbyc514e7d2019-12-11 17:00:31 -0500117 *reinterpret_cast<SkPoint3*>(vertexCursor) = {dstRect.fRight, dstRect.fBottom, 1.f};
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500118 *reinterpret_cast<GrColor*>(vertexCursor + colorOffset) = fCurrentColor;
Herb Derbyc514e7d2019-12-11 17:00:31 -0500119 vertexCursor += vertexStride;
Herb Derbya9047642019-12-06 12:12:11 -0500120
Herb Derbyc514e7d2019-12-11 17:00:31 -0500121 *glyphCursor++ = grGlyph;
Herb Derbya9047642019-12-06 12:12:11 -0500122 }
Herb Derbya9047642019-12-06 12:12:11 -0500123}
124
125void GrTextBlob::SubRun::resetBulkUseToken() { fBulkUseToken.reset(); }
126
127GrDrawOpAtlas::BulkUseTokenUpdater* GrTextBlob::SubRun::bulkUseToken() { return &fBulkUseToken; }
128void GrTextBlob::SubRun::setStrike(sk_sp<GrTextStrike> strike) { fStrike = std::move(strike); }
129GrTextStrike* GrTextBlob::SubRun::strike() const { return fStrike.get(); }
Herb Derby59d529c2020-01-15 15:42:21 -0500130GrStrikeCache* GrTextBlob::SubRun::grStrikeCache() const {
131 return fBlob->fStrikeCache;
132}
Herb Derbya9047642019-12-06 12:12:11 -0500133GrMaskFormat GrTextBlob::SubRun::maskFormat() const { return fMaskFormat; }
Herb Derbya2d72252019-12-23 15:02:33 -0500134size_t GrTextBlob::SubRun::vertexStride() const {
135 return GetVertexStride(this->maskFormat(), this->hasW());
136}
Herb Derby3d3150c2019-12-23 15:26:44 -0500137size_t GrTextBlob::SubRun::colorOffset() const {
138 return this->hasW() ? offsetof(SDFT3DVertex, color) : offsetof(Mask2DVertex, color);
139}
Herb Derbya56a1d72019-12-27 12:12:47 -0500140
141size_t GrTextBlob::SubRun::texCoordOffset() const {
142 switch (fMaskFormat) {
143 case kA8_GrMaskFormat:
144 return this->hasW() ? offsetof(SDFT3DVertex, atlasPos)
145 : offsetof(Mask2DVertex, atlasPos);
146 case kARGB_GrMaskFormat:
147 return this->hasW() ? offsetof(ARGB3DVertex, atlasPos)
148 : offsetof(ARGB2DVertex, atlasPos);
149 default:
150 SkASSERT(!this->hasW());
151 return offsetof(Mask2DVertex, atlasPos);
152 }
153}
154
Herb Derby91fd46a2019-12-26 11:36:30 -0500155char* GrTextBlob::SubRun::quadStart(size_t index) const {
Herb Derby23f29762020-01-10 16:26:14 -0500156 return SkTAddOffset<char>(fVertexData.data(), this->quadOffset(index));
157}
158
159size_t GrTextBlob::SubRun::quadOffset(size_t index) const {
160 return index * kVerticesPerGlyph * this->vertexStride();
Herb Derby91fd46a2019-12-26 11:36:30 -0500161}
Herb Derbya2d72252019-12-23 15:02:33 -0500162
Herb Derbya9047642019-12-06 12:12:11 -0500163const SkRect& GrTextBlob::SubRun::vertexBounds() const { return fVertexBounds; }
164void GrTextBlob::SubRun::joinGlyphBounds(const SkRect& glyphBounds) {
165 fVertexBounds.joinNonEmptyArg(glyphBounds);
166}
167
Herb Derbya9047642019-12-06 12:12:11 -0500168bool GrTextBlob::SubRun::drawAsDistanceFields() const { return fType == kTransformedSDFT; }
169
170bool GrTextBlob::SubRun::drawAsPaths() const { return fType == kTransformedPath; }
171
172bool GrTextBlob::SubRun::needsTransform() const {
173 return fType == kTransformedPath
174 || fType == kTransformedMask
175 || fType == kTransformedSDFT;
176}
177
178bool GrTextBlob::SubRun::hasW() const {
179 return fBlob->hasW(fType);
180}
181
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500182void GrTextBlob::SubRun::translateVerticesIfNeeded(
183 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
184 SkVector translation;
185 if (this->needsTransform()) {
186 // If transform is needed, then the vertices are in source space, calculate the source
187 // space translation.
188 translation = drawOrigin - fCurrentOrigin;
189 } else {
190 // Calculate the translation in source space to a translation in device space. Calculate
191 // the translation by mapping (0, 0) through both the current matrix, and the draw
192 // matrix, and taking the difference.
193 SkMatrix currentMatrix{fCurrentMatrix};
194 currentMatrix.preTranslate(fCurrentOrigin.x(), fCurrentOrigin.y());
195 SkPoint currentDeviceOrigin{0, 0};
196 currentMatrix.mapPoints(&currentDeviceOrigin, 1);
197 SkMatrix completeDrawMatrix{drawMatrix};
198 completeDrawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
199 SkPoint drawDeviceOrigin{0, 0};
200 completeDrawMatrix.mapPoints(&drawDeviceOrigin, 1);
201 translation = drawDeviceOrigin - currentDeviceOrigin;
202 }
203
204 if (translation != SkPoint{0, 0}) {
205 size_t vertexStride = this->vertexStride();
206 for (size_t quad = 0; quad < fGlyphs.size(); quad++) {
207 SkPoint* vertexCursor = reinterpret_cast<SkPoint*>(quadStart(quad));
208 for (int i = 0; i < 4; ++i) {
209 *vertexCursor += translation;
210 vertexCursor = SkTAddOffset<SkPoint>(vertexCursor, vertexStride);
211 }
Herb Derby91fd46a2019-12-26 11:36:30 -0500212 }
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500213 fCurrentMatrix = drawMatrix;
214 fCurrentOrigin = drawOrigin;
Herb Derby91fd46a2019-12-26 11:36:30 -0500215 }
216}
217
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500218void GrTextBlob::SubRun::updateVerticesColorIfNeeded(GrColor newColor) {
219 if (this->maskFormat() != kARGB_GrMaskFormat && fCurrentColor != newColor) {
220 size_t vertexStride = this->vertexStride();
221 size_t colorOffset = this->colorOffset();
222 for (size_t quad = 0; quad < fGlyphs.size(); quad++) {
223 GrColor* colorCursor = SkTAddOffset<GrColor>(quadStart(quad), colorOffset);
224 for (int i = 0; i < 4; ++i) {
225 *colorCursor = newColor;
226 colorCursor = SkTAddOffset<GrColor>(colorCursor, vertexStride);
227 }
Herb Derby6ca4f312019-12-26 15:23:49 -0500228 }
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500229 this->fCurrentColor = newColor;
Herb Derby6ca4f312019-12-26 15:23:49 -0500230 }
Herb Derby6ca4f312019-12-26 15:23:49 -0500231}
232
Herb Derbybc131b82020-01-07 16:07:42 -0500233void GrTextBlob::SubRun::updateTexCoords(int begin, int end) {
234 const size_t vertexStride = this->vertexStride();
235 const size_t texCoordOffset = this->texCoordOffset();
236 char* vertex = this->quadStart(begin);
Jim Van Verth7edb0eb2020-01-15 21:56:46 +0000237 uint16_t* textureCoords = reinterpret_cast<uint16_t*>(vertex + texCoordOffset);
Herb Derbybc131b82020-01-07 16:07:42 -0500238 for (int i = begin; i < end; i++) {
239 GrGlyph* glyph = this->fGlyphs[i];
240 SkASSERT(glyph != nullptr);
241
242 int width = glyph->fBounds.width();
243 int height = glyph->fBounds.height();
Jim Van Verth7edb0eb2020-01-15 21:56:46 +0000244 uint16_t u0, v0, u1, v1;
Herb Derbybc131b82020-01-07 16:07:42 -0500245 if (this->drawAsDistanceFields()) {
246 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
247 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
248 u1 = u0 + width - 2 * SK_DistanceFieldInset;
249 v1 = v0 + height - 2 * SK_DistanceFieldInset;
250 } else {
251 u0 = glyph->fAtlasLocation.fX;
252 v0 = glyph->fAtlasLocation.fY;
253 u1 = u0 + width;
254 v1 = v0 + height;
255 }
256
Jim Van Verth7edb0eb2020-01-15 21:56:46 +0000257 // We pack the 2bit page index in the low bit of the u and v texture coords
Herb Derbybc131b82020-01-07 16:07:42 -0500258 uint32_t pageIndex = glyph->pageIndex();
Jim Van Verth7edb0eb2020-01-15 21:56:46 +0000259 SkASSERT(pageIndex < 4);
260 uint16_t uBit = (pageIndex >> 1u) & 0x1u;
261 uint16_t vBit = pageIndex & 0x1u;
262 u0 <<= 1u;
263 u0 |= uBit;
264 v0 <<= 1u;
265 v0 |= vBit;
266 u1 <<= 1u;
267 u1 |= uBit;
268 v1 <<= 1u;
269 v1 |= vBit;
Herb Derbybc131b82020-01-07 16:07:42 -0500270
271 textureCoords[0] = u0;
272 textureCoords[1] = v0;
Jim Van Verth7edb0eb2020-01-15 21:56:46 +0000273 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500274 textureCoords[0] = u0;
275 textureCoords[1] = v1;
Jim Van Verth7edb0eb2020-01-15 21:56:46 +0000276 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500277 textureCoords[0] = u1;
278 textureCoords[1] = v0;
Jim Van Verth7edb0eb2020-01-15 21:56:46 +0000279 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500280 textureCoords[0] = u1;
281 textureCoords[1] = v1;
Jim Van Verth7edb0eb2020-01-15 21:56:46 +0000282 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500283 }
Herb Derbya56a1d72019-12-27 12:12:47 -0500284}
285
Herb Derbya9047642019-12-06 12:12:11 -0500286void GrTextBlob::SubRun::setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
287bool GrTextBlob::SubRun::hasUseLCDText() const { return fFlags.useLCDText; }
288void GrTextBlob::SubRun::setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
289bool GrTextBlob::SubRun::isAntiAliased() const { return fFlags.antiAliased; }
290const SkStrikeSpec& GrTextBlob::SubRun::strikeSpec() const { return fStrikeSpec; }
291
292// -- GrTextBlob -----------------------------------------------------------------------------------
293void GrTextBlob::operator delete(void* p) { ::operator delete(p); }
294void* GrTextBlob::operator new(size_t) { SK_ABORT("All blobs are created by placement new."); }
295void* GrTextBlob::operator new(size_t, void* p) { return p; }
296
297GrTextBlob::~GrTextBlob() = default;
298
Herb Derby659e4092019-12-06 15:38:10 -0500299sk_sp<GrTextBlob> GrTextBlob::Make(const SkGlyphRunList& glyphRunList,
Herb Derbyeba195f2019-12-03 16:44:47 -0500300 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500301 const SkMatrix& drawMatrix,
Herb Derbya00da612019-03-04 17:10:01 -0500302 GrColor color,
Herb Derbyeba195f2019-12-03 16:44:47 -0500303 bool forceWForDistanceFields) {
Herb Derbycd498e12019-12-06 14:17:31 -0500304
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500305 static_assert(sizeof(ARGB2DVertex) <= sizeof(Mask2DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500306 static_assert(alignof(ARGB2DVertex) <= alignof(Mask2DVertex));
307 size_t quadSize = sizeof(Mask2DVertex) * kVerticesPerGlyph;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500308 if (drawMatrix.hasPerspective() || forceWForDistanceFields) {
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500309 static_assert(sizeof(ARGB3DVertex) <= sizeof(SDFT3DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500310 static_assert(alignof(ARGB3DVertex) <= alignof(SDFT3DVertex));
311 quadSize = sizeof(SDFT3DVertex) * kVerticesPerGlyph;
Herb Derbye74c7762019-12-04 14:15:41 -0500312 }
313
Herb Derbycb718892019-12-07 00:07:42 -0500314 // We can use the alignment of SDFT3DVertex as a proxy for all Vertex alignments.
315 static_assert(alignof(SDFT3DVertex) >= alignof(Mask2DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500316 // Assume there is no padding needed between glyph pointers and vertices.
Herb Derbycb718892019-12-07 00:07:42 -0500317 static_assert(alignof(GrGlyph*) >= alignof(SDFT3DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500318
319 // In the arena, the layout is GrGlyph*... | SDFT3DVertex... | SubRun, so there is no padding
320 // between GrGlyph* and SDFT3DVertex, but padding is needed between the Mask2DVertex array
321 // and the SubRun.
322 size_t vertexToSubRunPadding = alignof(SDFT3DVertex) - alignof(SubRun);
323 size_t arenaSize =
324 sizeof(GrGlyph*) * glyphRunList.totalGlyphCount()
325 + quadSize * glyphRunList.totalGlyphCount()
326 + glyphRunList.runCount() * (sizeof(SubRun) + vertexToSubRunPadding);
327
328 size_t allocationSize = sizeof(GrTextBlob) + arenaSize;
Ben Wagner75d6db72018-09-20 14:39:39 -0400329
Herb Derbycb718892019-12-07 00:07:42 -0500330 void* allocation = ::operator new (allocationSize);
Herb Derbyb12175f2018-05-23 16:38:09 -0400331
Herb Derbyaebc5f82019-12-10 14:07:10 -0500332 SkColor initialLuminance = SkPaintPriv::ComputeLuminanceColor(glyphRunList.paint());
Herb Derby00ae9592019-12-03 15:55:56 -0500333 sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{
Herb Derby1c5be7b2019-12-13 12:03:06 -0500334 arenaSize, strikeCache, drawMatrix, glyphRunList.origin(),
Herb Derbyaebc5f82019-12-10 14:07:10 -0500335 color, initialLuminance, forceWForDistanceFields}};
joshualitt92303772016-02-10 11:55:52 -0800336
Herb Derbyf7d5d742018-11-16 13:24:32 -0500337 return blob;
joshualitt92303772016-02-10 11:55:52 -0800338}
339
Herb Derbya9047642019-12-06 12:12:11 -0500340void GrTextBlob::setupKey(const GrTextBlob::Key& key, const SkMaskFilterBase::BlurRec& blurRec,
341 const SkPaint& paint) {
342 fKey = key;
343 if (key.fHasBlur) {
344 fBlurRec = blurRec;
345 }
346 if (key.fStyle != SkPaint::kFill_Style) {
347 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
348 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
349 fStrokeInfo.fJoin = paint.getStrokeJoin();
350 }
351}
352const GrTextBlob::Key& GrTextBlob::GetKey(const GrTextBlob& blob) { return blob.fKey; }
353uint32_t GrTextBlob::Hash(const GrTextBlob::Key& key) { return SkOpts::hash(&key, sizeof(Key)); }
354
Herb Derbycb718892019-12-07 00:07:42 -0500355bool GrTextBlob::hasDistanceField() const {
356 return SkToBool(fTextType & kHasDistanceField_TextType);
357}
Herb Derbya9047642019-12-06 12:12:11 -0500358bool GrTextBlob::hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
Herb Derby1c5be7b2019-12-13 12:03:06 -0500359bool GrTextBlob::hasPerspective() const { return fInitialMatrix.hasPerspective(); }
Herb Derbya9047642019-12-06 12:12:11 -0500360
361void GrTextBlob::setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
362void GrTextBlob::setHasBitmap() { fTextType |= kHasBitmap_TextType; }
363void GrTextBlob::setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) {
364 // we init fMaxMinScale and fMinMaxScale in the constructor
365 fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale);
366 fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale);
367}
368
369size_t GrTextBlob::GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
370 switch (maskFormat) {
371 case kA8_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500372 return hasWCoord ? sizeof(SDFT3DVertex) : sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500373 case kARGB_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500374 return hasWCoord ? sizeof(ARGB3DVertex) : sizeof(ARGB2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500375 default:
376 SkASSERT(!hasWCoord);
Herb Derbycd498e12019-12-06 14:17:31 -0500377 return sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500378 }
379}
380
Herb Derby0edb2142018-10-16 17:04:11 -0400381bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
382 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derby5bf5b042019-12-12 16:37:03 -0500383 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
joshualittfd5f6c12015-12-10 07:44:50 -0800384 // If we have LCD text then our canonical color will be set to transparent, in this case we have
385 // to regenerate the blob on any color change
386 // We use the grPaint to get any color filter effects
387 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbyaebc5f82019-12-10 14:07:10 -0500388 fInitialLuminance != SkPaintPriv::ComputeLuminanceColor(paint)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800389 return true;
390 }
391
Herb Derby1c5be7b2019-12-13 12:03:06 -0500392 if (fInitialMatrix.hasPerspective() != drawMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800393 return true;
394 }
395
Brian Salomon5c6ac642017-12-19 11:09:32 -0500396 /** This could be relaxed for blobs with only distance field glyphs. */
Mike Reed2c383152019-12-18 16:47:47 -0500397 if (fInitialMatrix.hasPerspective() && !SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800398 return true;
399 }
400
401 // We only cache one masked version
402 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400403 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800404 return true;
405 }
406
407 // Similarly, we only cache one version for each style
408 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400409 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
410 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
411 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800412 return true;
413 }
414
415 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
416 // for mixed blobs if this becomes an issue.
417 if (this->hasBitmap() && this->hasDistanceField()) {
Herb Derbyeba195f2019-12-03 16:44:47 -0500418 // Identical view matrices and we can reuse in all cases
Mike Reed2c383152019-12-18 16:47:47 -0500419 return !(SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix) &&
420 drawOrigin == fInitialOrigin);
joshualittfd5f6c12015-12-10 07:44:50 -0800421 }
422
423 if (this->hasBitmap()) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500424 if (fInitialMatrix.getScaleX() != drawMatrix.getScaleX() ||
425 fInitialMatrix.getScaleY() != drawMatrix.getScaleY() ||
426 fInitialMatrix.getSkewX() != drawMatrix.getSkewX() ||
427 fInitialMatrix.getSkewY() != drawMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800428 return true;
429 }
430
Herb Derby9830fc42019-09-12 10:58:26 -0400431 // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust
432 // the quads properly. Devise a system that regenerates the quads from original data
433 // using the transform to allow this to be used in general.
434
435 // We can update the positions in the text blob without regenerating the whole
436 // blob, but only for integer translations.
Herb Derby435adfe2020-01-14 15:12:04 -0500437 // Calculate the translation in source space to a translation in device space by mapping
438 // (0, 0) through both the initial matrix and the draw matrix; take the difference.
439 SkMatrix initialMatrix{fInitialMatrix};
440 initialMatrix.preTranslate(fInitialOrigin.x(), fInitialOrigin.y());
441 SkPoint initialDeviceOrigin{0, 0};
442 initialMatrix.mapPoints(&initialDeviceOrigin, 1);
443 SkMatrix completeDrawMatrix{drawMatrix};
444 completeDrawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
445 SkPoint drawDeviceOrigin{0, 0};
446 completeDrawMatrix.mapPoints(&drawDeviceOrigin, 1);
447 SkPoint translation = drawDeviceOrigin - initialDeviceOrigin;
448
449 if (!SkScalarIsInt(translation.x()) || !SkScalarIsInt(translation.y())) {
Herb Derby9830fc42019-09-12 10:58:26 -0400450 return true;
joshualittfd5f6c12015-12-10 07:44:50 -0800451 }
joshualittfd5f6c12015-12-10 07:44:50 -0800452 } else if (this->hasDistanceField()) {
453 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
454 // distance field being generated, so we have to regenerate in those cases
Herb Derby1c5be7b2019-12-13 12:03:06 -0500455 SkScalar newMaxScale = drawMatrix.getMaxScale();
456 SkScalar oldMaxScale = fInitialMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800457 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
458 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
459 return true;
460 }
joshualittfd5f6c12015-12-10 07:44:50 -0800461 }
462
joshualittfd5f6c12015-12-10 07:44:50 -0800463 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
464 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
465 // the blob anyways at flush time, so no need to regenerate explicitly
466 return false;
467}
468
Herb Derbyc1b482c2018-08-09 15:02:27 -0400469void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
470 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400471 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Herb Derby5bf5b042019-12-12 16:37:03 -0500472 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500473
Herb Derbycb718892019-12-07 00:07:42 -0500474 for (SubRun* subRun = fFirstSubRun; subRun != nullptr; subRun = subRun->fNextSubRun) {
475 if (subRun->drawAsPaths()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400476 SkPaint runPaint{paint};
Herb Derbycb718892019-12-07 00:07:42 -0500477 runPaint.setAntiAlias(subRun->isAntiAliased());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500478 // If there are shaders, blurs or styles, the path must be scaled into source
479 // space independently of the CTM. This allows the CTM to be correct for the
480 // different effects.
481 GrStyle style(runPaint);
Herb Derby9f491482018-08-08 11:53:00 -0400482
Herb Derby1b8dcd12019-11-15 15:21:15 -0500483 bool scalePath = runPaint.getShader()
484 || style.applies()
485 || runPaint.getMaskFilter();
Robert Phillips137ca522018-08-15 10:14:33 -0400486
Herb Derby1b8dcd12019-11-15 15:21:15 -0500487 // The origin for the blob may have changed, so figure out the delta.
Herb Derby5bf5b042019-12-12 16:37:03 -0500488 SkVector originShift = drawOrigin - fInitialOrigin;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500489
Herb Derbycb718892019-12-07 00:07:42 -0500490 for (const auto& pathGlyph : subRun->fPaths) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500491 SkMatrix ctm{drawMatrix};
Herb Derbycb718892019-12-07 00:07:42 -0500492 SkMatrix pathMatrix = SkMatrix::MakeScale(
493 subRun->fStrikeSpec.strikeToSourceRatio());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500494 // Shift the original glyph location in source space to the position of the new
495 // blob.
496 pathMatrix.postTranslate(originShift.x() + pathGlyph.fOrigin.x(),
497 originShift.y() + pathGlyph.fOrigin.y());
Herb Derbydac1ed52018-09-12 17:04:21 -0400498
499 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400500 SkTLazy<SkPath> tmpPath;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500501 const SkPath* path = &pathGlyph.fPath;
Herb Derby2984d262019-11-20 14:40:39 -0500502 if (!scalePath) {
503 // Scale can be applied to CTM -- no effects.
Herb Derby2984d262019-11-20 14:40:39 -0500504 ctm.preConcat(pathMatrix);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400505 } else {
Herb Derby2984d262019-11-20 14:40:39 -0500506 // Scale the outline into source space.
Herb Derbydac1ed52018-09-12 17:04:21 -0400507
Herb Derby2984d262019-11-20 14:40:39 -0500508 // Transform the path form the normalized outline to source space. This
509 // way the CTM will remain the same so it can be used by the effects.
510 SkPath* sourceOutline = tmpPath.init();
511 path->transform(pathMatrix, sourceOutline);
512 sourceOutline->setIsVolatile(true);
513 path = sourceOutline;
Robert Phillips137ca522018-08-15 10:14:33 -0400514 }
515
Robert Phillips46a13382018-08-23 13:53:01 -0400516 // TODO: we are losing the mutability of the path here
517 GrShape shape(*path, paint);
Herb Derbydac1ed52018-09-12 17:04:21 -0400518 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500519 }
Herb Derby1b8dcd12019-11-15 15:21:15 -0500520 } else {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500521 int glyphCount = subRun->fGlyphs.size();
Jim Van Verth54d9c882018-02-08 16:14:48 -0500522 if (0 == glyphCount) {
523 continue;
524 }
525
526 bool skipClip = false;
527 bool submitOp = true;
528 SkIRect clipRect = SkIRect::MakeEmpty();
529 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
530 SkRRect clipRRect;
531 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400532 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500533 // and we have an axis-aligned rectangular non-AA clip
Herb Derbycb718892019-12-07 00:07:42 -0500534 if (!subRun->drawAsDistanceFields() && !subRun->needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500535 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500536 clipRRect.isRect() && GrAA::kNo == aa) {
537 skipClip = true;
538 // We only need to do clipping work if the subrun isn't contained by the clip
539 SkRect subRunBounds;
Herb Derby5bf5b042019-12-12 16:37:03 -0500540 this->computeSubRunBounds(
541 &subRunBounds, *subRun, drawMatrix, drawOrigin, false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500542 if (!clipRRect.getBounds().contains(subRunBounds)) {
543 // If the subrun is completely outside, don't add an op for it
544 if (!clipRRect.getBounds().intersects(subRunBounds)) {
545 submitOp = false;
546 }
547 else {
548 clipRRect.getBounds().round(&clipRect);
549 }
550 }
551 }
552
553 if (submitOp) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500554 auto op = this->makeOp(*subRun, glyphCount, drawMatrix, drawOrigin,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400555 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500556 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500557 if (op) {
558 if (skipClip) {
559 target->addDrawOp(GrNoClip(), std::move(op));
560 }
561 else {
562 target->addDrawOp(clip, std::move(op));
563 }
564 }
565 }
566 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000567 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000568}
569
Herb Derby1c5be7b2019-12-13 12:03:06 -0500570void GrTextBlob::computeSubRunBounds(SkRect* outBounds, const SubRun& subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500571 const SkMatrix& drawMatrix, SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500572 bool needsGlyphTransform) {
573 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
574 // into device space.
575 // We handle vertex bounds differently for distance field text and bitmap text because
576 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
577 // from one blob then we are going to pay the price here of mapping the rect for each run.
578 *outBounds = subRun.vertexBounds();
579 if (needsGlyphTransform) {
580 // Distance field text is positioned with the (X,Y) as part of the glyph position,
581 // and currently the view matrix is applied on the GPU
Herb Derby5bf5b042019-12-12 16:37:03 -0500582 outBounds->offset(drawOrigin - fInitialOrigin);
Herb Derby1c5be7b2019-12-13 12:03:06 -0500583 drawMatrix.mapRect(outBounds);
Herb Derbya9047642019-12-06 12:12:11 -0500584 } else {
585 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
586 // device space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500587 SkMatrix boundsMatrix = fInitialMatrixInverse;
Herb Derbya9047642019-12-06 12:12:11 -0500588
589 boundsMatrix.postTranslate(-fInitialOrigin.x(), -fInitialOrigin.y());
590
Herb Derby5bf5b042019-12-12 16:37:03 -0500591 boundsMatrix.postTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derbya9047642019-12-06 12:12:11 -0500592
Herb Derby1c5be7b2019-12-13 12:03:06 -0500593 boundsMatrix.postConcat(drawMatrix);
Herb Derbya9047642019-12-06 12:12:11 -0500594 boundsMatrix.mapRect(outBounds);
595
596 // Due to floating point numerical inaccuracies, we have to round out here
597 outBounds->roundOut(outBounds);
598 }
joshualitt323c2eb2016-01-20 06:48:47 -0800599}
joshualitt2e2202e2015-12-10 11:22:08 -0800600
Herb Derbya9047642019-12-06 12:12:11 -0500601const GrTextBlob::Key& GrTextBlob::key() const { return fKey; }
602size_t GrTextBlob::size() const { return fSize; }
603
604std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Herb Derby1c5be7b2019-12-13 12:03:06 -0500605 int glyphCount, const SkMatrix& drawMatrix,
Herb Derby5bf5b042019-12-12 16:37:03 -0500606 SkPoint drawOrigin, const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbya9047642019-12-06 12:12:11 -0500607 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
608 GrTextTarget* target) {
Herb Derbycb718892019-12-07 00:07:42 -0500609 SubRun* info = fFirstSubRun;
Herb Derbya9047642019-12-06 12:12:11 -0500610 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derby5bf5b042019-12-12 16:37:03 -0500611 return this->makeOp(*info, glyphCount, drawMatrix, drawOrigin, emptyRect,
Herb Derbya9047642019-12-06 12:12:11 -0500612 paint, filteredColor, props, distanceAdjustTable, target);
613}
614
615bool GrTextBlob::hasW(GrTextBlob::SubRunType type) const {
616 if (type == kTransformedSDFT) {
617 return this->hasPerspective() || fForceWForDistanceFields;
618 } else if (type == kTransformedMask || type == kTransformedPath) {
619 return this->hasPerspective();
Herb Derbye9f691d2019-12-04 12:11:13 -0500620 }
Herb Derbya9047642019-12-06 12:12:11 -0500621
622 // The viewMatrix is implicitly SkMatrix::I when drawing kDirectMask, because it is not
623 // used.
624 return false;
625}
626
627GrTextBlob::SubRun* GrTextBlob::makeSubRun(SubRunType type,
628 const SkZip<SkGlyphVariant, SkPoint>& drawables,
629 const SkStrikeSpec& strikeSpec,
630 GrMaskFormat format) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500631 SkSpan<GrGlyph*> glyphs{fAlloc.makeArrayDefault<GrGlyph*>(drawables.size()), drawables.size()};
Herb Derbya9047642019-12-06 12:12:11 -0500632 bool hasW = this->hasW(type);
Herb Derby3d3150c2019-12-23 15:26:44 -0500633
634 SkASSERT(!fInitialMatrix.hasPerspective() || hasW);
635
Herb Derbyc514e7d2019-12-11 17:00:31 -0500636 size_t vertexDataSize = drawables.size() * GetVertexStride(format, hasW) * kVerticesPerGlyph;
637 SkSpan<char> vertexData{fAlloc.makeArrayDefault<char>(vertexDataSize), vertexDataSize};
Herb Derbya9047642019-12-06 12:12:11 -0500638
639 sk_sp<GrTextStrike> grStrike = strikeSpec.findOrCreateGrStrike(fStrikeCache);
640
Herb Derbycb718892019-12-07 00:07:42 -0500641 SubRun* subRun = fAlloc.make<SubRun>(
Herb Derbyc514e7d2019-12-11 17:00:31 -0500642 type, this, strikeSpec, format, glyphs, vertexData, std::move(grStrike));
Herb Derbya9047642019-12-06 12:12:11 -0500643
Herb Derbycb718892019-12-07 00:07:42 -0500644 subRun->appendGlyphs(drawables);
Herb Derbya9047642019-12-06 12:12:11 -0500645
Herb Derbycb718892019-12-07 00:07:42 -0500646 return subRun;
Herb Derbya9047642019-12-06 12:12:11 -0500647}
648
649void GrTextBlob::addSingleMaskFormat(
650 SubRunType type,
651 const SkZip<SkGlyphVariant, SkPoint>& drawables,
652 const SkStrikeSpec& strikeSpec,
653 GrMaskFormat format) {
654 this->makeSubRun(type, drawables, strikeSpec, format);
655}
656
657void GrTextBlob::addMultiMaskFormat(
658 SubRunType type,
659 const SkZip<SkGlyphVariant, SkPoint>& drawables,
660 const SkStrikeSpec& strikeSpec) {
661 this->setHasBitmap();
662 if (drawables.empty()) { return; }
663
664 auto glyphSpan = drawables.get<0>();
665 SkGlyph* glyph = glyphSpan[0];
666 GrMaskFormat format = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
667 size_t startIndex = 0;
668 for (size_t i = 1; i < drawables.size(); i++) {
669 glyph = glyphSpan[i];
670 GrMaskFormat nextFormat = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
671 if (format != nextFormat) {
672 auto sameFormat = drawables.subspan(startIndex, i - startIndex);
673 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
674 format = nextFormat;
675 startIndex = i;
676 }
677 }
678 auto sameFormat = drawables.last(drawables.size() - startIndex);
679 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
680}
681
682void GrTextBlob::addSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
683 const SkStrikeSpec& strikeSpec,
684 const SkFont& runFont,
685 SkScalar minScale,
686 SkScalar maxScale) {
687 this->setHasDistanceField();
688 this->setMinAndMaxScale(minScale, maxScale);
689
690 SubRun* subRun = this->makeSubRun(kTransformedSDFT, drawables, strikeSpec, kA8_GrMaskFormat);
691 subRun->setUseLCDText(runFont.getEdging() == SkFont::Edging::kSubpixelAntiAlias);
692 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbye9f691d2019-12-04 12:11:13 -0500693}
694
Herb Derbycb718892019-12-07 00:07:42 -0500695GrTextBlob::GrTextBlob(size_t allocSize,
Herb Derbyeba195f2019-12-03 16:44:47 -0500696 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500697 const SkMatrix& drawMatrix,
Herb Derbyeba195f2019-12-03 16:44:47 -0500698 SkPoint origin,
699 GrColor color,
Herb Derbyaebc5f82019-12-10 14:07:10 -0500700 SkColor initialLuminance,
Herb Derby00ae9592019-12-03 15:55:56 -0500701 bool forceWForDistanceFields)
Herb Derbycb718892019-12-07 00:07:42 -0500702 : fSize{allocSize}
Herb Derby00ae9592019-12-03 15:55:56 -0500703 , fStrikeCache{strikeCache}
Herb Derby1c5be7b2019-12-13 12:03:06 -0500704 , fInitialMatrix{drawMatrix}
705 , fInitialMatrixInverse{make_inverse(drawMatrix)}
Herb Derbyeba195f2019-12-03 16:44:47 -0500706 , fInitialOrigin{origin}
Herb Derby00ae9592019-12-03 15:55:56 -0500707 , fForceWForDistanceFields{forceWForDistanceFields}
Herb Derbycb718892019-12-07 00:07:42 -0500708 , fColor{color}
Herb Derbyaebc5f82019-12-10 14:07:10 -0500709 , fInitialLuminance{initialLuminance}
Herb Derbycb718892019-12-07 00:07:42 -0500710 , fAlloc{SkTAddOffset<char>(this, sizeof(GrTextBlob)), allocSize, allocSize/2} { }
Herb Derby00ae9592019-12-03 15:55:56 -0500711
Herb Derbycb718892019-12-07 00:07:42 -0500712void GrTextBlob::insertSubRun(SubRun* subRun) {
713 if (fFirstSubRun == nullptr) {
714 fFirstSubRun = subRun;
715 fLastSubRun = subRun;
716 } else {
717 fLastSubRun->fNextSubRun = subRun;
718 fLastSubRun = subRun;
719 }
720}
721
722std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Herb Derbya9047642019-12-06 12:12:11 -0500723 SubRun& info, int glyphCount,
Herb Derby5bf5b042019-12-12 16:37:03 -0500724 const SkMatrix& drawMatrix, SkPoint drawOrigin, const SkIRect& clipRect,
Herb Derbya9047642019-12-06 12:12:11 -0500725 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
726 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
727 GrMaskFormat format = info.maskFormat();
728
729 GrPaint grPaint;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500730 target->makeGrPaint(info.maskFormat(), paint, drawMatrix, &grPaint);
Herb Derbya9047642019-12-06 12:12:11 -0500731 std::unique_ptr<GrAtlasTextOp> op;
732 if (info.drawAsDistanceFields()) {
733 // TODO: Can we be even smarter based on the dest transfer function?
734 op = GrAtlasTextOp::MakeDistanceField(
735 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
736 target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint),
737 props, info.isAntiAliased(), info.hasUseLCDText());
Herb Derbyeba195f2019-12-03 16:44:47 -0500738 } else {
Herb Derbya9047642019-12-06 12:12:11 -0500739 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
740 info.needsTransform());
741 }
742 GrAtlasTextOp::Geometry& geometry = op->geometry();
Herb Derby1c5be7b2019-12-13 12:03:06 -0500743 geometry.fDrawMatrix = drawMatrix;
Herb Derbya9047642019-12-06 12:12:11 -0500744 geometry.fClipRect = clipRect;
745 geometry.fBlob = SkRef(this);
746 geometry.fSubRunPtr = &info;
747 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
Herb Derby5bf5b042019-12-12 16:37:03 -0500748 geometry.fDrawOrigin = drawOrigin;
Herb Derbya9047642019-12-06 12:12:11 -0500749 op->init();
750 return op;
751}
Herb Derbyeba195f2019-12-03 16:44:47 -0500752
Herb Derbya9047642019-12-06 12:12:11 -0500753void GrTextBlob::processDeviceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
754 const SkStrikeSpec& strikeSpec) {
755 this->addMultiMaskFormat(kDirectMask, drawables, strikeSpec);
756}
Herb Derbyeba195f2019-12-03 16:44:47 -0500757
Herb Derbya9047642019-12-06 12:12:11 -0500758void GrTextBlob::processSourcePaths(const SkZip<SkGlyphVariant, SkPoint>& drawables,
759 const SkFont& runFont,
760 const SkStrikeSpec& strikeSpec) {
761 this->setHasBitmap();
Herb Derbycb718892019-12-07 00:07:42 -0500762 SubRun* subRun = fAlloc.make<SubRun>(this, strikeSpec);
763 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbya9047642019-12-06 12:12:11 -0500764 for (auto [variant, pos] : drawables) {
Herb Derbycb718892019-12-07 00:07:42 -0500765 subRun->fPaths.emplace_back(*variant.path(), pos);
Herb Derbyeba195f2019-12-03 16:44:47 -0500766 }
767}
768
Herb Derbya9047642019-12-06 12:12:11 -0500769void GrTextBlob::processSourceSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
770 const SkStrikeSpec& strikeSpec,
771 const SkFont& runFont,
772 SkScalar minScale,
773 SkScalar maxScale) {
774 this->addSDFT(drawables, strikeSpec, runFont, minScale, maxScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800775}
Herb Derbya9047642019-12-06 12:12:11 -0500776
777void GrTextBlob::processSourceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
778 const SkStrikeSpec& strikeSpec) {
779 this->addMultiMaskFormat(kTransformedMask, drawables, strikeSpec);
780}
781
782// -- GrTextBlob::VertexRegenerator ----------------------------------------------------------------
Herb Derbya9047642019-12-06 12:12:11 -0500783GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
Herb Derbya9047642019-12-06 12:12:11 -0500784 GrTextBlob::SubRun* subRun,
Herb Derbya9047642019-12-06 12:12:11 -0500785 GrDeferredUploadTarget* uploadTarget,
Herb Derbya9047642019-12-06 12:12:11 -0500786 GrAtlasManager* fullAtlasManager)
787 : fResourceProvider(resourceProvider)
Herb Derbya9047642019-12-06 12:12:11 -0500788 , fUploadTarget(uploadTarget)
Herb Derbya9047642019-12-06 12:12:11 -0500789 , fFullAtlasManager(fullAtlasManager)
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500790 , fSubRun(subRun){
Herb Derbya9047642019-12-06 12:12:11 -0500791 // Because the GrStrikeCache may evict the strike a blob depends on using for
792 // generating its texture coords, we have to track whether or not the strike has
793 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
794 // otherwise we have to get the new strike, and use that to get the correct glyphs.
795 // Because we do not have the packed ids, and thus can't look up our glyphs in the
796 // new strike, we instead keep our ref to the old strike and use the packed ids from
797 // it. These ids will still be valid as long as we hold the ref. When we are done
798 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
Herb Derby73630212019-12-13 16:29:14 -0500799 fActions.regenTextureCoordinates = fSubRun->strike()->isAbandoned();
800 fActions.regenStrike = fSubRun->strike()->isAbandoned();
Herb Derbya9047642019-12-06 12:12:11 -0500801}
802
Herb Derbyb45558d2020-01-07 16:57:12 -0500803std::tuple<bool, int> GrTextBlob::VertexRegenerator::updateTextureCoordinatesMaybeStrike(
804 const int begin, const int end) {
Herb Derby586f8d02020-01-07 15:28:07 -0500805 SkASSERT(fActions.regenTextureCoordinates);
806 fSubRun->resetBulkUseToken();
Herb Derbya9047642019-12-06 12:12:11 -0500807
Herb Derby586f8d02020-01-07 15:28:07 -0500808 const SkStrikeSpec& strikeSpec = fSubRun->strikeSpec();
Herb Derbya9047642019-12-06 12:12:11 -0500809
Herb Derby586f8d02020-01-07 15:28:07 -0500810 if (!fMetricsAndImages.isValid()
Herb Derbya9047642019-12-06 12:12:11 -0500811 || fMetricsAndImages->descriptor() != strikeSpec.descriptor()) {
Herb Derby586f8d02020-01-07 15:28:07 -0500812 fMetricsAndImages.init(strikeSpec);
813 }
814
815 if (fActions.regenStrike) {
816 // Take the glyphs from the old strike, and translate them a new strike.
Herb Derby59d529c2020-01-15 15:42:21 -0500817 sk_sp<GrTextStrike> newStrike = strikeSpec.findOrCreateGrStrike(fSubRun->grStrikeCache());
Herb Derby586f8d02020-01-07 15:28:07 -0500818
819 // Start this batch at the start of the subRun plus any glyphs that were previously
820 // processed.
Herb Derbyb45558d2020-01-07 16:57:12 -0500821 SkSpan<GrGlyph*> glyphs = fSubRun->fGlyphs.last(fSubRun->fGlyphs.size() - begin);
Herb Derby586f8d02020-01-07 15:28:07 -0500822
823 // Convert old glyphs to newStrike.
824 for (auto& glyph : glyphs) {
825 SkPackedGlyphID id = glyph->fPackedID;
826 glyph = newStrike->getGlyph(id, fMetricsAndImages.get());
827 SkASSERT(id == glyph->fPackedID);
Herb Derbya9047642019-12-06 12:12:11 -0500828 }
829
Herb Derby586f8d02020-01-07 15:28:07 -0500830 fSubRun->setStrike(newStrike);
Herb Derbya9047642019-12-06 12:12:11 -0500831 }
832
Herb Derbybc131b82020-01-07 16:07:42 -0500833 // Update the atlas information in the GrStrike.
Herb Derby83b907e2020-01-06 13:44:29 -0500834 auto code = GrDrawOpAtlas::ErrorCode::kSucceeded;
Herb Derby586f8d02020-01-07 15:28:07 -0500835 GrTextStrike* grStrike = fSubRun->strike();
Herb Derbybc131b82020-01-07 16:07:42 -0500836 auto tokenTracker = fUploadTarget->tokenTracker();
Herb Derbyb45558d2020-01-07 16:57:12 -0500837 int i = begin;
838 for (; i < end; i++) {
839 GrGlyph* glyph = fSubRun->fGlyphs[i];
Herb Derby586f8d02020-01-07 15:28:07 -0500840 SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
Herb Derbya9047642019-12-06 12:12:11 -0500841
Herb Derby586f8d02020-01-07 15:28:07 -0500842 if (!fFullAtlasManager->hasGlyph(glyph)) {
843 code = grStrike->addGlyphToAtlas(
Herb Derby59d529c2020-01-15 15:42:21 -0500844 fResourceProvider, fUploadTarget, fSubRun->grStrikeCache(), fFullAtlasManager,
845 glyph, fMetricsAndImages.get(), fSubRun->maskFormat(),
846 fSubRun->needsTransform());
Herb Derby586f8d02020-01-07 15:28:07 -0500847 if (code != GrDrawOpAtlas::ErrorCode::kSucceeded) {
848 break;
Herb Derbya9047642019-12-06 12:12:11 -0500849 }
Herb Derbya9047642019-12-06 12:12:11 -0500850 }
Herb Derby586f8d02020-01-07 15:28:07 -0500851 fFullAtlasManager->addGlyphToBulkAndSetUseToken(
852 fSubRun->bulkUseToken(), glyph, tokenTracker->nextDrawToken());
Herb Derbya9047642019-12-06 12:12:11 -0500853 }
Herb Derby23f29762020-01-10 16:26:14 -0500854 int glyphsPlacedInAtlas = i - begin;
Herb Derbya9047642019-12-06 12:12:11 -0500855
Herb Derbybc131b82020-01-07 16:07:42 -0500856 // Update the quads with the new atlas coordinates.
Herb Derby23f29762020-01-10 16:26:14 -0500857 fSubRun->updateTexCoords(begin, begin + glyphsPlacedInAtlas);
Herb Derbybc131b82020-01-07 16:07:42 -0500858
Herb Derbyb45558d2020-01-07 16:57:12 -0500859 if (code == GrDrawOpAtlas::ErrorCode::kSucceeded) {
860 // If we reach here with begin > 0, some earlier call to regenerate() exhausted the atlas
861 // before it could place all its glyphs and returned kTryAgain. Invalidate texture
862 // coordinates, forcing them to be regenerated, minding the atlas flush between.
863 fSubRun->fAtlasGeneration =
864 begin > 0 ? GrDrawOpAtlas::kInvalidAtlasGeneration
865 : fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
Herb Derbya9047642019-12-06 12:12:11 -0500866 }
Herb Derby83b907e2020-01-06 13:44:29 -0500867
Herb Derby23f29762020-01-10 16:26:14 -0500868 return {code != GrDrawOpAtlas::ErrorCode::kError, glyphsPlacedInAtlas};
Herb Derbya9047642019-12-06 12:12:11 -0500869}
870
Herb Derby23f29762020-01-10 16:26:14 -0500871std::tuple<bool, int> GrTextBlob::VertexRegenerator::regenerate(int begin, int end) {
Herb Derbya9047642019-12-06 12:12:11 -0500872 uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
873 // If regenerate() is called multiple times then the atlas gen may have changed. So we check
874 // this each time.
Herb Derby5919cf62020-01-02 15:10:11 -0500875 fActions.regenTextureCoordinates |= fSubRun->fAtlasGeneration != currentAtlasGen;
Herb Derby586f8d02020-01-07 15:28:07 -0500876 if (fActions.regenStrike) { SkASSERT(fActions.regenTextureCoordinates); }
Herb Derbya9047642019-12-06 12:12:11 -0500877
Herb Derby51a95ce2020-01-08 17:49:51 -0500878 if (fActions.regenStrike || fActions.regenTextureCoordinates) {
Herb Derby23f29762020-01-10 16:26:14 -0500879 return this->updateTextureCoordinatesMaybeStrike(begin, end);
Herb Derby51a95ce2020-01-08 17:49:51 -0500880 } else {
Herb Derby51a95ce2020-01-08 17:49:51 -0500881 // All glyphs are inserted into the atlas if fCurrGlyph is at the end of fGlyphs.
Herb Derby23f29762020-01-10 16:26:14 -0500882 if (end == (int)fSubRun->fGlyphs.size()) {
Herb Derby51a95ce2020-01-08 17:49:51 -0500883 // Set use tokens for all of the glyphs in our SubRun. This is only valid if we
Brian Salomon43cbd722020-01-03 22:09:12 -0500884 // have a valid atlas generation
885 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
886 fUploadTarget->tokenTracker()->nextDrawToken(),
887 fSubRun->maskFormat());
888 }
Herb Derby23f29762020-01-10 16:26:14 -0500889 return {true, end - begin};
Herb Derbya9047642019-12-06 12:12:11 -0500890 }
Herb Derbya9047642019-12-06 12:12:11 -0500891}