blob: 894a227b4155d854994ef1ebb4fe50581eb258b5 [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 Derbya9047642019-12-06 12:12:11 -0500130GrMaskFormat GrTextBlob::SubRun::maskFormat() const { return fMaskFormat; }
Herb Derbya2d72252019-12-23 15:02:33 -0500131size_t GrTextBlob::SubRun::vertexStride() const {
132 return GetVertexStride(this->maskFormat(), this->hasW());
133}
Herb Derby3d3150c2019-12-23 15:26:44 -0500134size_t GrTextBlob::SubRun::colorOffset() const {
135 return this->hasW() ? offsetof(SDFT3DVertex, color) : offsetof(Mask2DVertex, color);
136}
Herb Derbya56a1d72019-12-27 12:12:47 -0500137
138size_t GrTextBlob::SubRun::texCoordOffset() const {
139 switch (fMaskFormat) {
140 case kA8_GrMaskFormat:
141 return this->hasW() ? offsetof(SDFT3DVertex, atlasPos)
142 : offsetof(Mask2DVertex, atlasPos);
143 case kARGB_GrMaskFormat:
144 return this->hasW() ? offsetof(ARGB3DVertex, atlasPos)
145 : offsetof(ARGB2DVertex, atlasPos);
146 default:
147 SkASSERT(!this->hasW());
148 return offsetof(Mask2DVertex, atlasPos);
149 }
150}
151
Herb Derby91fd46a2019-12-26 11:36:30 -0500152char* GrTextBlob::SubRun::quadStart(size_t index) const {
153 return SkTAddOffset<char>(
154 fVertexData.data(), index * kVerticesPerGlyph * this->vertexStride());
155}
Herb Derbya2d72252019-12-23 15:02:33 -0500156
Herb Derbya9047642019-12-06 12:12:11 -0500157const SkRect& GrTextBlob::SubRun::vertexBounds() const { return fVertexBounds; }
158void GrTextBlob::SubRun::joinGlyphBounds(const SkRect& glyphBounds) {
159 fVertexBounds.joinNonEmptyArg(glyphBounds);
160}
161
Herb Derbya9047642019-12-06 12:12:11 -0500162bool GrTextBlob::SubRun::drawAsDistanceFields() const { return fType == kTransformedSDFT; }
163
164bool GrTextBlob::SubRun::drawAsPaths() const { return fType == kTransformedPath; }
165
166bool GrTextBlob::SubRun::needsTransform() const {
167 return fType == kTransformedPath
168 || fType == kTransformedMask
169 || fType == kTransformedSDFT;
170}
171
172bool GrTextBlob::SubRun::hasW() const {
173 return fBlob->hasW(fType);
174}
175
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500176void GrTextBlob::SubRun::translateVerticesIfNeeded(
177 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
178 SkVector translation;
179 if (this->needsTransform()) {
180 // If transform is needed, then the vertices are in source space, calculate the source
181 // space translation.
182 translation = drawOrigin - fCurrentOrigin;
183 } else {
184 // Calculate the translation in source space to a translation in device space. Calculate
185 // the translation by mapping (0, 0) through both the current matrix, and the draw
186 // matrix, and taking the difference.
187 SkMatrix currentMatrix{fCurrentMatrix};
188 currentMatrix.preTranslate(fCurrentOrigin.x(), fCurrentOrigin.y());
189 SkPoint currentDeviceOrigin{0, 0};
190 currentMatrix.mapPoints(&currentDeviceOrigin, 1);
191 SkMatrix completeDrawMatrix{drawMatrix};
192 completeDrawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
193 SkPoint drawDeviceOrigin{0, 0};
194 completeDrawMatrix.mapPoints(&drawDeviceOrigin, 1);
195 translation = drawDeviceOrigin - currentDeviceOrigin;
196 }
197
198 if (translation != SkPoint{0, 0}) {
199 size_t vertexStride = this->vertexStride();
200 for (size_t quad = 0; quad < fGlyphs.size(); quad++) {
201 SkPoint* vertexCursor = reinterpret_cast<SkPoint*>(quadStart(quad));
202 for (int i = 0; i < 4; ++i) {
203 *vertexCursor += translation;
204 vertexCursor = SkTAddOffset<SkPoint>(vertexCursor, vertexStride);
205 }
Herb Derby91fd46a2019-12-26 11:36:30 -0500206 }
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500207 fCurrentMatrix = drawMatrix;
208 fCurrentOrigin = drawOrigin;
Herb Derby91fd46a2019-12-26 11:36:30 -0500209 }
210}
211
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500212void GrTextBlob::SubRun::updateVerticesColorIfNeeded(GrColor newColor) {
213 if (this->maskFormat() != kARGB_GrMaskFormat && fCurrentColor != newColor) {
214 size_t vertexStride = this->vertexStride();
215 size_t colorOffset = this->colorOffset();
216 for (size_t quad = 0; quad < fGlyphs.size(); quad++) {
217 GrColor* colorCursor = SkTAddOffset<GrColor>(quadStart(quad), colorOffset);
218 for (int i = 0; i < 4; ++i) {
219 *colorCursor = newColor;
220 colorCursor = SkTAddOffset<GrColor>(colorCursor, vertexStride);
221 }
Herb Derby6ca4f312019-12-26 15:23:49 -0500222 }
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500223 this->fCurrentColor = newColor;
Herb Derby6ca4f312019-12-26 15:23:49 -0500224 }
Herb Derby6ca4f312019-12-26 15:23:49 -0500225}
226
Herb Derbybc131b82020-01-07 16:07:42 -0500227void GrTextBlob::SubRun::updateTexCoords(int begin, int end) {
228 const size_t vertexStride = this->vertexStride();
229 const size_t texCoordOffset = this->texCoordOffset();
230 char* vertex = this->quadStart(begin);
Jim Van Verthc46e30e2020-01-14 20:33:36 +0000231 uint16_t* textureCoords = reinterpret_cast<uint16_t*>(vertex + texCoordOffset);
Herb Derbybc131b82020-01-07 16:07:42 -0500232 for (int i = begin; i < end; i++) {
233 GrGlyph* glyph = this->fGlyphs[i];
234 SkASSERT(glyph != nullptr);
235
236 int width = glyph->fBounds.width();
237 int height = glyph->fBounds.height();
Jim Van Verthc46e30e2020-01-14 20:33:36 +0000238 uint16_t u0, v0, u1, v1;
Herb Derbybc131b82020-01-07 16:07:42 -0500239 if (this->drawAsDistanceFields()) {
240 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
241 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
242 u1 = u0 + width - 2 * SK_DistanceFieldInset;
243 v1 = v0 + height - 2 * SK_DistanceFieldInset;
244 } else {
245 u0 = glyph->fAtlasLocation.fX;
246 v0 = glyph->fAtlasLocation.fY;
247 u1 = u0 + width;
248 v1 = v0 + height;
249 }
250
Jim Van Verthc46e30e2020-01-14 20:33:36 +0000251 // We pack the 2bit page index in the low bit of the u and v texture coords
Herb Derbybc131b82020-01-07 16:07:42 -0500252 uint32_t pageIndex = glyph->pageIndex();
Jim Van Verthc46e30e2020-01-14 20:33:36 +0000253 SkASSERT(pageIndex < 4);
254 uint16_t uBit = (pageIndex >> 1u) & 0x1u;
255 uint16_t vBit = pageIndex & 0x1u;
256 u0 <<= 1u;
257 u0 |= uBit;
258 v0 <<= 1u;
259 v0 |= vBit;
260 u1 <<= 1u;
261 u1 |= uBit;
262 v1 <<= 1u;
263 v1 |= vBit;
Herb Derbybc131b82020-01-07 16:07:42 -0500264
265 textureCoords[0] = u0;
266 textureCoords[1] = v0;
Jim Van Verthc46e30e2020-01-14 20:33:36 +0000267 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500268 textureCoords[0] = u0;
269 textureCoords[1] = v1;
Jim Van Verthc46e30e2020-01-14 20:33:36 +0000270 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500271 textureCoords[0] = u1;
272 textureCoords[1] = v0;
Jim Van Verthc46e30e2020-01-14 20:33:36 +0000273 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500274 textureCoords[0] = u1;
275 textureCoords[1] = v1;
Jim Van Verthc46e30e2020-01-14 20:33:36 +0000276 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500277 }
Herb Derbya56a1d72019-12-27 12:12:47 -0500278}
279
Herb Derbya9047642019-12-06 12:12:11 -0500280void GrTextBlob::SubRun::setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
281bool GrTextBlob::SubRun::hasUseLCDText() const { return fFlags.useLCDText; }
282void GrTextBlob::SubRun::setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
283bool GrTextBlob::SubRun::isAntiAliased() const { return fFlags.antiAliased; }
284const SkStrikeSpec& GrTextBlob::SubRun::strikeSpec() const { return fStrikeSpec; }
285
286// -- GrTextBlob -----------------------------------------------------------------------------------
287void GrTextBlob::operator delete(void* p) { ::operator delete(p); }
288void* GrTextBlob::operator new(size_t) { SK_ABORT("All blobs are created by placement new."); }
289void* GrTextBlob::operator new(size_t, void* p) { return p; }
290
291GrTextBlob::~GrTextBlob() = default;
292
Herb Derby659e4092019-12-06 15:38:10 -0500293sk_sp<GrTextBlob> GrTextBlob::Make(const SkGlyphRunList& glyphRunList,
Herb Derbyeba195f2019-12-03 16:44:47 -0500294 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500295 const SkMatrix& drawMatrix,
Herb Derbya00da612019-03-04 17:10:01 -0500296 GrColor color,
Herb Derbyeba195f2019-12-03 16:44:47 -0500297 bool forceWForDistanceFields) {
Herb Derbycd498e12019-12-06 14:17:31 -0500298
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500299 static_assert(sizeof(ARGB2DVertex) <= sizeof(Mask2DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500300 static_assert(alignof(ARGB2DVertex) <= alignof(Mask2DVertex));
301 size_t quadSize = sizeof(Mask2DVertex) * kVerticesPerGlyph;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500302 if (drawMatrix.hasPerspective() || forceWForDistanceFields) {
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500303 static_assert(sizeof(ARGB3DVertex) <= sizeof(SDFT3DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500304 static_assert(alignof(ARGB3DVertex) <= alignof(SDFT3DVertex));
305 quadSize = sizeof(SDFT3DVertex) * kVerticesPerGlyph;
Herb Derbye74c7762019-12-04 14:15:41 -0500306 }
307
Herb Derbycb718892019-12-07 00:07:42 -0500308 // We can use the alignment of SDFT3DVertex as a proxy for all Vertex alignments.
309 static_assert(alignof(SDFT3DVertex) >= alignof(Mask2DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500310 // Assume there is no padding needed between glyph pointers and vertices.
Herb Derbycb718892019-12-07 00:07:42 -0500311 static_assert(alignof(GrGlyph*) >= alignof(SDFT3DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500312
313 // In the arena, the layout is GrGlyph*... | SDFT3DVertex... | SubRun, so there is no padding
314 // between GrGlyph* and SDFT3DVertex, but padding is needed between the Mask2DVertex array
315 // and the SubRun.
316 size_t vertexToSubRunPadding = alignof(SDFT3DVertex) - alignof(SubRun);
317 size_t arenaSize =
318 sizeof(GrGlyph*) * glyphRunList.totalGlyphCount()
319 + quadSize * glyphRunList.totalGlyphCount()
320 + glyphRunList.runCount() * (sizeof(SubRun) + vertexToSubRunPadding);
321
322 size_t allocationSize = sizeof(GrTextBlob) + arenaSize;
Ben Wagner75d6db72018-09-20 14:39:39 -0400323
Herb Derbycb718892019-12-07 00:07:42 -0500324 void* allocation = ::operator new (allocationSize);
Herb Derbyb12175f2018-05-23 16:38:09 -0400325
Herb Derbyaebc5f82019-12-10 14:07:10 -0500326 SkColor initialLuminance = SkPaintPriv::ComputeLuminanceColor(glyphRunList.paint());
Herb Derby00ae9592019-12-03 15:55:56 -0500327 sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{
Herb Derby1c5be7b2019-12-13 12:03:06 -0500328 arenaSize, strikeCache, drawMatrix, glyphRunList.origin(),
Herb Derbyaebc5f82019-12-10 14:07:10 -0500329 color, initialLuminance, forceWForDistanceFields}};
joshualitt92303772016-02-10 11:55:52 -0800330
Herb Derbyf7d5d742018-11-16 13:24:32 -0500331 return blob;
joshualitt92303772016-02-10 11:55:52 -0800332}
333
Herb Derbya9047642019-12-06 12:12:11 -0500334void GrTextBlob::setupKey(const GrTextBlob::Key& key, const SkMaskFilterBase::BlurRec& blurRec,
335 const SkPaint& paint) {
336 fKey = key;
337 if (key.fHasBlur) {
338 fBlurRec = blurRec;
339 }
340 if (key.fStyle != SkPaint::kFill_Style) {
341 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
342 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
343 fStrokeInfo.fJoin = paint.getStrokeJoin();
344 }
345}
346const GrTextBlob::Key& GrTextBlob::GetKey(const GrTextBlob& blob) { return blob.fKey; }
347uint32_t GrTextBlob::Hash(const GrTextBlob::Key& key) { return SkOpts::hash(&key, sizeof(Key)); }
348
Herb Derbycb718892019-12-07 00:07:42 -0500349bool GrTextBlob::hasDistanceField() const {
350 return SkToBool(fTextType & kHasDistanceField_TextType);
351}
Herb Derbya9047642019-12-06 12:12:11 -0500352bool GrTextBlob::hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
Herb Derby1c5be7b2019-12-13 12:03:06 -0500353bool GrTextBlob::hasPerspective() const { return fInitialMatrix.hasPerspective(); }
Herb Derbya9047642019-12-06 12:12:11 -0500354
355void GrTextBlob::setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
356void GrTextBlob::setHasBitmap() { fTextType |= kHasBitmap_TextType; }
357void GrTextBlob::setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) {
358 // we init fMaxMinScale and fMinMaxScale in the constructor
359 fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale);
360 fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale);
361}
362
363size_t GrTextBlob::GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
364 switch (maskFormat) {
365 case kA8_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500366 return hasWCoord ? sizeof(SDFT3DVertex) : sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500367 case kARGB_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500368 return hasWCoord ? sizeof(ARGB3DVertex) : sizeof(ARGB2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500369 default:
370 SkASSERT(!hasWCoord);
Herb Derbycd498e12019-12-06 14:17:31 -0500371 return sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500372 }
373}
374
Herb Derby0edb2142018-10-16 17:04:11 -0400375bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
376 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derby5bf5b042019-12-12 16:37:03 -0500377 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
joshualittfd5f6c12015-12-10 07:44:50 -0800378 // If we have LCD text then our canonical color will be set to transparent, in this case we have
379 // to regenerate the blob on any color change
380 // We use the grPaint to get any color filter effects
381 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbyaebc5f82019-12-10 14:07:10 -0500382 fInitialLuminance != SkPaintPriv::ComputeLuminanceColor(paint)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800383 return true;
384 }
385
Herb Derby1c5be7b2019-12-13 12:03:06 -0500386 if (fInitialMatrix.hasPerspective() != drawMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800387 return true;
388 }
389
Brian Salomon5c6ac642017-12-19 11:09:32 -0500390 /** This could be relaxed for blobs with only distance field glyphs. */
Mike Reed2c383152019-12-18 16:47:47 -0500391 if (fInitialMatrix.hasPerspective() && !SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800392 return true;
393 }
394
395 // We only cache one masked version
396 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400397 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800398 return true;
399 }
400
401 // Similarly, we only cache one version for each style
402 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400403 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
404 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
405 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800406 return true;
407 }
408
409 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
410 // for mixed blobs if this becomes an issue.
411 if (this->hasBitmap() && this->hasDistanceField()) {
Herb Derbyeba195f2019-12-03 16:44:47 -0500412 // Identical view matrices and we can reuse in all cases
Mike Reed2c383152019-12-18 16:47:47 -0500413 return !(SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix) &&
414 drawOrigin == fInitialOrigin);
joshualittfd5f6c12015-12-10 07:44:50 -0800415 }
416
417 if (this->hasBitmap()) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500418 if (fInitialMatrix.getScaleX() != drawMatrix.getScaleX() ||
419 fInitialMatrix.getScaleY() != drawMatrix.getScaleY() ||
420 fInitialMatrix.getSkewX() != drawMatrix.getSkewX() ||
421 fInitialMatrix.getSkewY() != drawMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800422 return true;
423 }
424
Herb Derby9830fc42019-09-12 10:58:26 -0400425 // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust
426 // the quads properly. Devise a system that regenerates the quads from original data
427 // using the transform to allow this to be used in general.
428
429 // We can update the positions in the text blob without regenerating the whole
430 // blob, but only for integer translations.
431 // This cool bit of math will determine the necessary translation to apply to the
432 // already generated vertex coordinates to move them to the correct position.
433 // Figure out the translation in view space given a translation in source space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500434 SkScalar transX = drawMatrix.getTranslateX() +
Herb Derby5bf5b042019-12-12 16:37:03 -0500435 drawMatrix.getScaleX() * (drawOrigin.x() - fInitialOrigin.x()) +
436 drawMatrix.getSkewX() * (drawOrigin.y() - fInitialOrigin.y()) -
Herb Derby1c5be7b2019-12-13 12:03:06 -0500437 fInitialMatrix.getTranslateX();
438 SkScalar transY = drawMatrix.getTranslateY() +
Herb Derby5bf5b042019-12-12 16:37:03 -0500439 drawMatrix.getSkewY() * (drawOrigin.x() - fInitialOrigin.x()) +
440 drawMatrix.getScaleY() * (drawOrigin.y() - fInitialOrigin.y()) -
Herb Derby1c5be7b2019-12-13 12:03:06 -0500441 fInitialMatrix.getTranslateY();
Herb Derby9830fc42019-09-12 10:58:26 -0400442 if (!SkScalarIsInt(transX) || !SkScalarIsInt(transY)) {
443 return true;
joshualittfd5f6c12015-12-10 07:44:50 -0800444 }
joshualittfd5f6c12015-12-10 07:44:50 -0800445 } else if (this->hasDistanceField()) {
446 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
447 // distance field being generated, so we have to regenerate in those cases
Herb Derby1c5be7b2019-12-13 12:03:06 -0500448 SkScalar newMaxScale = drawMatrix.getMaxScale();
449 SkScalar oldMaxScale = fInitialMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800450 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
451 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
452 return true;
453 }
joshualittfd5f6c12015-12-10 07:44:50 -0800454 }
455
joshualittfd5f6c12015-12-10 07:44:50 -0800456 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
457 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
458 // the blob anyways at flush time, so no need to regenerate explicitly
459 return false;
460}
461
Herb Derbyc1b482c2018-08-09 15:02:27 -0400462void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
463 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400464 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Herb Derby5bf5b042019-12-12 16:37:03 -0500465 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500466
Herb Derbycb718892019-12-07 00:07:42 -0500467 for (SubRun* subRun = fFirstSubRun; subRun != nullptr; subRun = subRun->fNextSubRun) {
468 if (subRun->drawAsPaths()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400469 SkPaint runPaint{paint};
Herb Derbycb718892019-12-07 00:07:42 -0500470 runPaint.setAntiAlias(subRun->isAntiAliased());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500471 // If there are shaders, blurs or styles, the path must be scaled into source
472 // space independently of the CTM. This allows the CTM to be correct for the
473 // different effects.
474 GrStyle style(runPaint);
Herb Derby9f491482018-08-08 11:53:00 -0400475
Herb Derby1b8dcd12019-11-15 15:21:15 -0500476 bool scalePath = runPaint.getShader()
477 || style.applies()
478 || runPaint.getMaskFilter();
Robert Phillips137ca522018-08-15 10:14:33 -0400479
Herb Derby1b8dcd12019-11-15 15:21:15 -0500480 // The origin for the blob may have changed, so figure out the delta.
Herb Derby5bf5b042019-12-12 16:37:03 -0500481 SkVector originShift = drawOrigin - fInitialOrigin;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500482
Herb Derbycb718892019-12-07 00:07:42 -0500483 for (const auto& pathGlyph : subRun->fPaths) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500484 SkMatrix ctm{drawMatrix};
Herb Derbycb718892019-12-07 00:07:42 -0500485 SkMatrix pathMatrix = SkMatrix::MakeScale(
486 subRun->fStrikeSpec.strikeToSourceRatio());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500487 // Shift the original glyph location in source space to the position of the new
488 // blob.
489 pathMatrix.postTranslate(originShift.x() + pathGlyph.fOrigin.x(),
490 originShift.y() + pathGlyph.fOrigin.y());
Herb Derbydac1ed52018-09-12 17:04:21 -0400491
492 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400493 SkTLazy<SkPath> tmpPath;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500494 const SkPath* path = &pathGlyph.fPath;
Herb Derby2984d262019-11-20 14:40:39 -0500495 if (!scalePath) {
496 // Scale can be applied to CTM -- no effects.
Herb Derby2984d262019-11-20 14:40:39 -0500497 ctm.preConcat(pathMatrix);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400498 } else {
Herb Derby2984d262019-11-20 14:40:39 -0500499 // Scale the outline into source space.
Herb Derbydac1ed52018-09-12 17:04:21 -0400500
Herb Derby2984d262019-11-20 14:40:39 -0500501 // Transform the path form the normalized outline to source space. This
502 // way the CTM will remain the same so it can be used by the effects.
503 SkPath* sourceOutline = tmpPath.init();
504 path->transform(pathMatrix, sourceOutline);
505 sourceOutline->setIsVolatile(true);
506 path = sourceOutline;
Robert Phillips137ca522018-08-15 10:14:33 -0400507 }
508
Robert Phillips46a13382018-08-23 13:53:01 -0400509 // TODO: we are losing the mutability of the path here
510 GrShape shape(*path, paint);
Herb Derbydac1ed52018-09-12 17:04:21 -0400511 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500512 }
Herb Derby1b8dcd12019-11-15 15:21:15 -0500513 } else {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500514 int glyphCount = subRun->fGlyphs.size();
Jim Van Verth54d9c882018-02-08 16:14:48 -0500515 if (0 == glyphCount) {
516 continue;
517 }
518
519 bool skipClip = false;
520 bool submitOp = true;
521 SkIRect clipRect = SkIRect::MakeEmpty();
522 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
523 SkRRect clipRRect;
524 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400525 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500526 // and we have an axis-aligned rectangular non-AA clip
Herb Derbycb718892019-12-07 00:07:42 -0500527 if (!subRun->drawAsDistanceFields() && !subRun->needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500528 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500529 clipRRect.isRect() && GrAA::kNo == aa) {
530 skipClip = true;
531 // We only need to do clipping work if the subrun isn't contained by the clip
532 SkRect subRunBounds;
Herb Derby5bf5b042019-12-12 16:37:03 -0500533 this->computeSubRunBounds(
534 &subRunBounds, *subRun, drawMatrix, drawOrigin, false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500535 if (!clipRRect.getBounds().contains(subRunBounds)) {
536 // If the subrun is completely outside, don't add an op for it
537 if (!clipRRect.getBounds().intersects(subRunBounds)) {
538 submitOp = false;
539 }
540 else {
541 clipRRect.getBounds().round(&clipRect);
542 }
543 }
544 }
545
546 if (submitOp) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500547 auto op = this->makeOp(*subRun, glyphCount, drawMatrix, drawOrigin,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400548 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500549 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500550 if (op) {
551 if (skipClip) {
552 target->addDrawOp(GrNoClip(), std::move(op));
553 }
554 else {
555 target->addDrawOp(clip, std::move(op));
556 }
557 }
558 }
559 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000560 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000561}
562
Herb Derby1c5be7b2019-12-13 12:03:06 -0500563void GrTextBlob::computeSubRunBounds(SkRect* outBounds, const SubRun& subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500564 const SkMatrix& drawMatrix, SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500565 bool needsGlyphTransform) {
566 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
567 // into device space.
568 // We handle vertex bounds differently for distance field text and bitmap text because
569 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
570 // from one blob then we are going to pay the price here of mapping the rect for each run.
571 *outBounds = subRun.vertexBounds();
572 if (needsGlyphTransform) {
573 // Distance field text is positioned with the (X,Y) as part of the glyph position,
574 // and currently the view matrix is applied on the GPU
Herb Derby5bf5b042019-12-12 16:37:03 -0500575 outBounds->offset(drawOrigin - fInitialOrigin);
Herb Derby1c5be7b2019-12-13 12:03:06 -0500576 drawMatrix.mapRect(outBounds);
Herb Derbya9047642019-12-06 12:12:11 -0500577 } else {
578 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
579 // device space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500580 SkMatrix boundsMatrix = fInitialMatrixInverse;
Herb Derbya9047642019-12-06 12:12:11 -0500581
582 boundsMatrix.postTranslate(-fInitialOrigin.x(), -fInitialOrigin.y());
583
Herb Derby5bf5b042019-12-12 16:37:03 -0500584 boundsMatrix.postTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derbya9047642019-12-06 12:12:11 -0500585
Herb Derby1c5be7b2019-12-13 12:03:06 -0500586 boundsMatrix.postConcat(drawMatrix);
Herb Derbya9047642019-12-06 12:12:11 -0500587 boundsMatrix.mapRect(outBounds);
588
589 // Due to floating point numerical inaccuracies, we have to round out here
590 outBounds->roundOut(outBounds);
591 }
joshualitt323c2eb2016-01-20 06:48:47 -0800592}
joshualitt2e2202e2015-12-10 11:22:08 -0800593
Herb Derbya9047642019-12-06 12:12:11 -0500594const GrTextBlob::Key& GrTextBlob::key() const { return fKey; }
595size_t GrTextBlob::size() const { return fSize; }
596
597std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Herb Derby1c5be7b2019-12-13 12:03:06 -0500598 int glyphCount, const SkMatrix& drawMatrix,
Herb Derby5bf5b042019-12-12 16:37:03 -0500599 SkPoint drawOrigin, const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbya9047642019-12-06 12:12:11 -0500600 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
601 GrTextTarget* target) {
Herb Derbycb718892019-12-07 00:07:42 -0500602 SubRun* info = fFirstSubRun;
Herb Derbya9047642019-12-06 12:12:11 -0500603 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derby5bf5b042019-12-12 16:37:03 -0500604 return this->makeOp(*info, glyphCount, drawMatrix, drawOrigin, emptyRect,
Herb Derbya9047642019-12-06 12:12:11 -0500605 paint, filteredColor, props, distanceAdjustTable, target);
606}
607
608bool GrTextBlob::hasW(GrTextBlob::SubRunType type) const {
609 if (type == kTransformedSDFT) {
610 return this->hasPerspective() || fForceWForDistanceFields;
611 } else if (type == kTransformedMask || type == kTransformedPath) {
612 return this->hasPerspective();
Herb Derbye9f691d2019-12-04 12:11:13 -0500613 }
Herb Derbya9047642019-12-06 12:12:11 -0500614
615 // The viewMatrix is implicitly SkMatrix::I when drawing kDirectMask, because it is not
616 // used.
617 return false;
618}
619
620GrTextBlob::SubRun* GrTextBlob::makeSubRun(SubRunType type,
621 const SkZip<SkGlyphVariant, SkPoint>& drawables,
622 const SkStrikeSpec& strikeSpec,
623 GrMaskFormat format) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500624 SkSpan<GrGlyph*> glyphs{fAlloc.makeArrayDefault<GrGlyph*>(drawables.size()), drawables.size()};
Herb Derbya9047642019-12-06 12:12:11 -0500625 bool hasW = this->hasW(type);
Herb Derby3d3150c2019-12-23 15:26:44 -0500626
627 SkASSERT(!fInitialMatrix.hasPerspective() || hasW);
628
Herb Derbyc514e7d2019-12-11 17:00:31 -0500629 size_t vertexDataSize = drawables.size() * GetVertexStride(format, hasW) * kVerticesPerGlyph;
630 SkSpan<char> vertexData{fAlloc.makeArrayDefault<char>(vertexDataSize), vertexDataSize};
Herb Derbya9047642019-12-06 12:12:11 -0500631
632 sk_sp<GrTextStrike> grStrike = strikeSpec.findOrCreateGrStrike(fStrikeCache);
633
Herb Derbycb718892019-12-07 00:07:42 -0500634 SubRun* subRun = fAlloc.make<SubRun>(
Herb Derbyc514e7d2019-12-11 17:00:31 -0500635 type, this, strikeSpec, format, glyphs, vertexData, std::move(grStrike));
Herb Derbya9047642019-12-06 12:12:11 -0500636
Herb Derbycb718892019-12-07 00:07:42 -0500637 subRun->appendGlyphs(drawables);
Herb Derbya9047642019-12-06 12:12:11 -0500638
Herb Derbycb718892019-12-07 00:07:42 -0500639 return subRun;
Herb Derbya9047642019-12-06 12:12:11 -0500640}
641
642void GrTextBlob::addSingleMaskFormat(
643 SubRunType type,
644 const SkZip<SkGlyphVariant, SkPoint>& drawables,
645 const SkStrikeSpec& strikeSpec,
646 GrMaskFormat format) {
647 this->makeSubRun(type, drawables, strikeSpec, format);
648}
649
650void GrTextBlob::addMultiMaskFormat(
651 SubRunType type,
652 const SkZip<SkGlyphVariant, SkPoint>& drawables,
653 const SkStrikeSpec& strikeSpec) {
654 this->setHasBitmap();
655 if (drawables.empty()) { return; }
656
657 auto glyphSpan = drawables.get<0>();
658 SkGlyph* glyph = glyphSpan[0];
659 GrMaskFormat format = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
660 size_t startIndex = 0;
661 for (size_t i = 1; i < drawables.size(); i++) {
662 glyph = glyphSpan[i];
663 GrMaskFormat nextFormat = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
664 if (format != nextFormat) {
665 auto sameFormat = drawables.subspan(startIndex, i - startIndex);
666 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
667 format = nextFormat;
668 startIndex = i;
669 }
670 }
671 auto sameFormat = drawables.last(drawables.size() - startIndex);
672 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
673}
674
675void GrTextBlob::addSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
676 const SkStrikeSpec& strikeSpec,
677 const SkFont& runFont,
678 SkScalar minScale,
679 SkScalar maxScale) {
680 this->setHasDistanceField();
681 this->setMinAndMaxScale(minScale, maxScale);
682
683 SubRun* subRun = this->makeSubRun(kTransformedSDFT, drawables, strikeSpec, kA8_GrMaskFormat);
684 subRun->setUseLCDText(runFont.getEdging() == SkFont::Edging::kSubpixelAntiAlias);
685 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbye9f691d2019-12-04 12:11:13 -0500686}
687
Herb Derbycb718892019-12-07 00:07:42 -0500688GrTextBlob::GrTextBlob(size_t allocSize,
Herb Derbyeba195f2019-12-03 16:44:47 -0500689 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500690 const SkMatrix& drawMatrix,
Herb Derbyeba195f2019-12-03 16:44:47 -0500691 SkPoint origin,
692 GrColor color,
Herb Derbyaebc5f82019-12-10 14:07:10 -0500693 SkColor initialLuminance,
Herb Derby00ae9592019-12-03 15:55:56 -0500694 bool forceWForDistanceFields)
Herb Derbycb718892019-12-07 00:07:42 -0500695 : fSize{allocSize}
Herb Derby00ae9592019-12-03 15:55:56 -0500696 , fStrikeCache{strikeCache}
Herb Derby1c5be7b2019-12-13 12:03:06 -0500697 , fInitialMatrix{drawMatrix}
698 , fInitialMatrixInverse{make_inverse(drawMatrix)}
Herb Derbyeba195f2019-12-03 16:44:47 -0500699 , fInitialOrigin{origin}
Herb Derby00ae9592019-12-03 15:55:56 -0500700 , fForceWForDistanceFields{forceWForDistanceFields}
Herb Derbycb718892019-12-07 00:07:42 -0500701 , fColor{color}
Herb Derbyaebc5f82019-12-10 14:07:10 -0500702 , fInitialLuminance{initialLuminance}
Herb Derbycb718892019-12-07 00:07:42 -0500703 , fAlloc{SkTAddOffset<char>(this, sizeof(GrTextBlob)), allocSize, allocSize/2} { }
Herb Derby00ae9592019-12-03 15:55:56 -0500704
Herb Derbycb718892019-12-07 00:07:42 -0500705void GrTextBlob::insertSubRun(SubRun* subRun) {
706 if (fFirstSubRun == nullptr) {
707 fFirstSubRun = subRun;
708 fLastSubRun = subRun;
709 } else {
710 fLastSubRun->fNextSubRun = subRun;
711 fLastSubRun = subRun;
712 }
713}
714
715std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Herb Derbya9047642019-12-06 12:12:11 -0500716 SubRun& info, int glyphCount,
Herb Derby5bf5b042019-12-12 16:37:03 -0500717 const SkMatrix& drawMatrix, SkPoint drawOrigin, const SkIRect& clipRect,
Herb Derbya9047642019-12-06 12:12:11 -0500718 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
719 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
720 GrMaskFormat format = info.maskFormat();
721
722 GrPaint grPaint;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500723 target->makeGrPaint(info.maskFormat(), paint, drawMatrix, &grPaint);
Herb Derbya9047642019-12-06 12:12:11 -0500724 std::unique_ptr<GrAtlasTextOp> op;
725 if (info.drawAsDistanceFields()) {
726 // TODO: Can we be even smarter based on the dest transfer function?
727 op = GrAtlasTextOp::MakeDistanceField(
728 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
729 target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint),
730 props, info.isAntiAliased(), info.hasUseLCDText());
Herb Derbyeba195f2019-12-03 16:44:47 -0500731 } else {
Herb Derbya9047642019-12-06 12:12:11 -0500732 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
733 info.needsTransform());
734 }
735 GrAtlasTextOp::Geometry& geometry = op->geometry();
Herb Derby1c5be7b2019-12-13 12:03:06 -0500736 geometry.fDrawMatrix = drawMatrix;
Herb Derbya9047642019-12-06 12:12:11 -0500737 geometry.fClipRect = clipRect;
738 geometry.fBlob = SkRef(this);
739 geometry.fSubRunPtr = &info;
740 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
Herb Derby5bf5b042019-12-12 16:37:03 -0500741 geometry.fDrawOrigin = drawOrigin;
Herb Derbya9047642019-12-06 12:12:11 -0500742 op->init();
743 return op;
744}
Herb Derbyeba195f2019-12-03 16:44:47 -0500745
Herb Derbya9047642019-12-06 12:12:11 -0500746void GrTextBlob::processDeviceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
747 const SkStrikeSpec& strikeSpec) {
748 this->addMultiMaskFormat(kDirectMask, drawables, strikeSpec);
749}
Herb Derbyeba195f2019-12-03 16:44:47 -0500750
Herb Derbya9047642019-12-06 12:12:11 -0500751void GrTextBlob::processSourcePaths(const SkZip<SkGlyphVariant, SkPoint>& drawables,
752 const SkFont& runFont,
753 const SkStrikeSpec& strikeSpec) {
754 this->setHasBitmap();
Herb Derbycb718892019-12-07 00:07:42 -0500755 SubRun* subRun = fAlloc.make<SubRun>(this, strikeSpec);
756 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbya9047642019-12-06 12:12:11 -0500757 for (auto [variant, pos] : drawables) {
Herb Derbycb718892019-12-07 00:07:42 -0500758 subRun->fPaths.emplace_back(*variant.path(), pos);
Herb Derbyeba195f2019-12-03 16:44:47 -0500759 }
760}
761
Herb Derbya9047642019-12-06 12:12:11 -0500762void GrTextBlob::processSourceSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
763 const SkStrikeSpec& strikeSpec,
764 const SkFont& runFont,
765 SkScalar minScale,
766 SkScalar maxScale) {
767 this->addSDFT(drawables, strikeSpec, runFont, minScale, maxScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800768}
Herb Derbya9047642019-12-06 12:12:11 -0500769
770void GrTextBlob::processSourceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
771 const SkStrikeSpec& strikeSpec) {
772 this->addMultiMaskFormat(kTransformedMask, drawables, strikeSpec);
773}
774
775// -- GrTextBlob::VertexRegenerator ----------------------------------------------------------------
Herb Derbya9047642019-12-06 12:12:11 -0500776GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
Herb Derbya9047642019-12-06 12:12:11 -0500777 GrTextBlob::SubRun* subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500778 const SkMatrix& drawMatrix,
779 SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500780 GrColor color,
781 GrDeferredUploadTarget* uploadTarget,
782 GrStrikeCache* grStrikeCache,
783 GrAtlasManager* fullAtlasManager)
784 : fResourceProvider(resourceProvider)
Herb Derbya9047642019-12-06 12:12:11 -0500785 , fUploadTarget(uploadTarget)
786 , fGrStrikeCache(grStrikeCache)
787 , fFullAtlasManager(fullAtlasManager)
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500788 , fSubRun(subRun){
Herb Derbya9047642019-12-06 12:12:11 -0500789 // Because the GrStrikeCache may evict the strike a blob depends on using for
790 // generating its texture coords, we have to track whether or not the strike has
791 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
792 // otherwise we have to get the new strike, and use that to get the correct glyphs.
793 // Because we do not have the packed ids, and thus can't look up our glyphs in the
794 // new strike, we instead keep our ref to the old strike and use the packed ids from
795 // it. These ids will still be valid as long as we hold the ref. When we are done
796 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
Herb Derby73630212019-12-13 16:29:14 -0500797 fActions.regenTextureCoordinates = fSubRun->strike()->isAbandoned();
798 fActions.regenStrike = fSubRun->strike()->isAbandoned();
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500799
800 fSubRun->updateVerticesColorIfNeeded(color);
801 fSubRun->translateVerticesIfNeeded(drawMatrix, drawOrigin);
Herb Derbya9047642019-12-06 12:12:11 -0500802}
803
Herb Derbyb45558d2020-01-07 16:57:12 -0500804std::tuple<bool, int> GrTextBlob::VertexRegenerator::updateTextureCoordinatesMaybeStrike(
805 const int begin, const int end) {
Herb Derby586f8d02020-01-07 15:28:07 -0500806 SkASSERT(fActions.regenTextureCoordinates);
807 fSubRun->resetBulkUseToken();
Herb Derbya9047642019-12-06 12:12:11 -0500808
Herb Derby586f8d02020-01-07 15:28:07 -0500809 const SkStrikeSpec& strikeSpec = fSubRun->strikeSpec();
Herb Derbya9047642019-12-06 12:12:11 -0500810
Herb Derby586f8d02020-01-07 15:28:07 -0500811 if (!fMetricsAndImages.isValid()
Herb Derbya9047642019-12-06 12:12:11 -0500812 || fMetricsAndImages->descriptor() != strikeSpec.descriptor()) {
Herb Derby586f8d02020-01-07 15:28:07 -0500813 fMetricsAndImages.init(strikeSpec);
814 }
815
816 if (fActions.regenStrike) {
817 // Take the glyphs from the old strike, and translate them a new strike.
818 sk_sp<GrTextStrike> newStrike = strikeSpec.findOrCreateGrStrike(fGrStrikeCache);
819
820 // Start this batch at the start of the subRun plus any glyphs that were previously
821 // processed.
Herb Derbyb45558d2020-01-07 16:57:12 -0500822 SkSpan<GrGlyph*> glyphs = fSubRun->fGlyphs.last(fSubRun->fGlyphs.size() - begin);
Herb Derby586f8d02020-01-07 15:28:07 -0500823
824 // Convert old glyphs to newStrike.
825 for (auto& glyph : glyphs) {
826 SkPackedGlyphID id = glyph->fPackedID;
827 glyph = newStrike->getGlyph(id, fMetricsAndImages.get());
828 SkASSERT(id == glyph->fPackedID);
Herb Derbya9047642019-12-06 12:12:11 -0500829 }
830
Herb Derby586f8d02020-01-07 15:28:07 -0500831 fSubRun->setStrike(newStrike);
Herb Derbya9047642019-12-06 12:12:11 -0500832 }
833
Herb Derbybc131b82020-01-07 16:07:42 -0500834 // Update the atlas information in the GrStrike.
Herb Derby83b907e2020-01-06 13:44:29 -0500835 auto code = GrDrawOpAtlas::ErrorCode::kSucceeded;
Herb Derby586f8d02020-01-07 15:28:07 -0500836 GrTextStrike* grStrike = fSubRun->strike();
Herb Derbybc131b82020-01-07 16:07:42 -0500837 auto tokenTracker = fUploadTarget->tokenTracker();
Herb Derbyb45558d2020-01-07 16:57:12 -0500838 int i = begin;
839 for (; i < end; i++) {
840 GrGlyph* glyph = fSubRun->fGlyphs[i];
Herb Derby586f8d02020-01-07 15:28:07 -0500841 SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
Herb Derbya9047642019-12-06 12:12:11 -0500842
Herb Derby586f8d02020-01-07 15:28:07 -0500843 if (!fFullAtlasManager->hasGlyph(glyph)) {
844 code = grStrike->addGlyphToAtlas(
845 fResourceProvider, fUploadTarget, fGrStrikeCache, fFullAtlasManager, glyph,
846 fMetricsAndImages.get(), fSubRun->maskFormat(), fSubRun->needsTransform());
847 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 Derbyb45558d2020-01-07 16:57:12 -0500854 int firstNotInAtlas = i;
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 Derbyb45558d2020-01-07 16:57:12 -0500857 fSubRun->updateTexCoords(begin, firstNotInAtlas);
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 Derbyb45558d2020-01-07 16:57:12 -0500868 return {code != GrDrawOpAtlas::ErrorCode::kError, firstNotInAtlas};
Herb Derbya9047642019-12-06 12:12:11 -0500869}
870
Brian Salomon43cbd722020-01-03 22:09:12 -0500871bool GrTextBlob::VertexRegenerator::regenerate(GrTextBlob::VertexRegenerator::Result* result,
872 int maxGlyphs) {
Herb Derbya9047642019-12-06 12:12:11 -0500873 uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
874 // If regenerate() is called multiple times then the atlas gen may have changed. So we check
875 // this each time.
Herb Derby5919cf62020-01-02 15:10:11 -0500876 fActions.regenTextureCoordinates |= fSubRun->fAtlasGeneration != currentAtlasGen;
Herb Derby586f8d02020-01-07 15:28:07 -0500877 if (fActions.regenStrike) { SkASSERT(fActions.regenTextureCoordinates); }
Herb Derbya9047642019-12-06 12:12:11 -0500878
Herb Derby51a95ce2020-01-08 17:49:51 -0500879 bool ok = true;
880 const int begin = fCurrGlyph;
881 const int end = std::min((int)fSubRun->fGlyphs.size(), begin + maxGlyphs);
882 if (fActions.regenStrike || fActions.regenTextureCoordinates) {
883 int firstGlyphNotInAtlas;
884 std::tie(ok, firstGlyphNotInAtlas) = this->updateTextureCoordinatesMaybeStrike(begin, end);
885 fCurrGlyph = firstGlyphNotInAtlas;
886 } else {
887 fCurrGlyph = end;
888 // All glyphs are inserted into the atlas if fCurrGlyph is at the end of fGlyphs.
889 if (fCurrGlyph == (int)fSubRun->fGlyphs.size()) {
890 // Set use tokens for all of the glyphs in our SubRun. This is only valid if we
Brian Salomon43cbd722020-01-03 22:09:12 -0500891 // have a valid atlas generation
892 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
893 fUploadTarget->tokenTracker()->nextDrawToken(),
894 fSubRun->maskFormat());
895 }
Herb Derbya9047642019-12-06 12:12:11 -0500896 }
Herb Derby51a95ce2020-01-08 17:49:51 -0500897 if (ok) {
898 result->fFinished = fCurrGlyph == (int)fSubRun->fGlyphs.size();
899 result->fGlyphsRegenerated += fCurrGlyph - begin;
900 result->fFirstVertex = fSubRun->quadStart(begin);
901 }
902 return ok;
Herb Derbya9047642019-12-06 12:12:11 -0500903}