blob: 8ff86069b7a080a787c3c625c5510c11179cc80c [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 {
Herb Derby23f29762020-01-10 16:26:14 -0500153 return SkTAddOffset<char>(fVertexData.data(), this->quadOffset(index));
154}
155
156size_t GrTextBlob::SubRun::quadOffset(size_t index) const {
157 return index * kVerticesPerGlyph * this->vertexStride();
Herb Derby91fd46a2019-12-26 11:36:30 -0500158}
Herb Derbya2d72252019-12-23 15:02:33 -0500159
Herb Derbya9047642019-12-06 12:12:11 -0500160const SkRect& GrTextBlob::SubRun::vertexBounds() const { return fVertexBounds; }
161void GrTextBlob::SubRun::joinGlyphBounds(const SkRect& glyphBounds) {
162 fVertexBounds.joinNonEmptyArg(glyphBounds);
163}
164
Herb Derbya9047642019-12-06 12:12:11 -0500165bool GrTextBlob::SubRun::drawAsDistanceFields() const { return fType == kTransformedSDFT; }
166
167bool GrTextBlob::SubRun::drawAsPaths() const { return fType == kTransformedPath; }
168
169bool GrTextBlob::SubRun::needsTransform() const {
170 return fType == kTransformedPath
171 || fType == kTransformedMask
172 || fType == kTransformedSDFT;
173}
174
175bool GrTextBlob::SubRun::hasW() const {
176 return fBlob->hasW(fType);
177}
178
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500179void GrTextBlob::SubRun::translateVerticesIfNeeded(
180 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
181 SkVector translation;
182 if (this->needsTransform()) {
183 // If transform is needed, then the vertices are in source space, calculate the source
184 // space translation.
185 translation = drawOrigin - fCurrentOrigin;
186 } else {
187 // Calculate the translation in source space to a translation in device space. Calculate
188 // the translation by mapping (0, 0) through both the current matrix, and the draw
189 // matrix, and taking the difference.
190 SkMatrix currentMatrix{fCurrentMatrix};
191 currentMatrix.preTranslate(fCurrentOrigin.x(), fCurrentOrigin.y());
192 SkPoint currentDeviceOrigin{0, 0};
193 currentMatrix.mapPoints(&currentDeviceOrigin, 1);
194 SkMatrix completeDrawMatrix{drawMatrix};
195 completeDrawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
196 SkPoint drawDeviceOrigin{0, 0};
197 completeDrawMatrix.mapPoints(&drawDeviceOrigin, 1);
198 translation = drawDeviceOrigin - currentDeviceOrigin;
199 }
200
201 if (translation != SkPoint{0, 0}) {
202 size_t vertexStride = this->vertexStride();
203 for (size_t quad = 0; quad < fGlyphs.size(); quad++) {
204 SkPoint* vertexCursor = reinterpret_cast<SkPoint*>(quadStart(quad));
205 for (int i = 0; i < 4; ++i) {
206 *vertexCursor += translation;
207 vertexCursor = SkTAddOffset<SkPoint>(vertexCursor, vertexStride);
208 }
Herb Derby91fd46a2019-12-26 11:36:30 -0500209 }
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500210 fCurrentMatrix = drawMatrix;
211 fCurrentOrigin = drawOrigin;
Herb Derby91fd46a2019-12-26 11:36:30 -0500212 }
213}
214
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500215void GrTextBlob::SubRun::updateVerticesColorIfNeeded(GrColor newColor) {
216 if (this->maskFormat() != kARGB_GrMaskFormat && fCurrentColor != newColor) {
217 size_t vertexStride = this->vertexStride();
218 size_t colorOffset = this->colorOffset();
219 for (size_t quad = 0; quad < fGlyphs.size(); quad++) {
220 GrColor* colorCursor = SkTAddOffset<GrColor>(quadStart(quad), colorOffset);
221 for (int i = 0; i < 4; ++i) {
222 *colorCursor = newColor;
223 colorCursor = SkTAddOffset<GrColor>(colorCursor, vertexStride);
224 }
Herb Derby6ca4f312019-12-26 15:23:49 -0500225 }
Herb Derbydf2c1ee2019-12-26 17:54:41 -0500226 this->fCurrentColor = newColor;
Herb Derby6ca4f312019-12-26 15:23:49 -0500227 }
Herb Derby6ca4f312019-12-26 15:23:49 -0500228}
229
Herb Derbybc131b82020-01-07 16:07:42 -0500230void GrTextBlob::SubRun::updateTexCoords(int begin, int end) {
231 const size_t vertexStride = this->vertexStride();
232 const size_t texCoordOffset = this->texCoordOffset();
233 char* vertex = this->quadStart(begin);
Jim Van Verth3b9c5442020-01-16 14:52:16 -0500234 int16_t* textureCoords = reinterpret_cast<int16_t*>(vertex + texCoordOffset);
Herb Derbybc131b82020-01-07 16:07:42 -0500235 for (int i = begin; i < end; i++) {
236 GrGlyph* glyph = this->fGlyphs[i];
237 SkASSERT(glyph != nullptr);
238
239 int width = glyph->fBounds.width();
240 int height = glyph->fBounds.height();
Jim Van Verth3b9c5442020-01-16 14:52:16 -0500241 int16_t u0, v0, u1, v1;
Herb Derbybc131b82020-01-07 16:07:42 -0500242 if (this->drawAsDistanceFields()) {
243 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
244 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
245 u1 = u0 + width - 2 * SK_DistanceFieldInset;
246 v1 = v0 + height - 2 * SK_DistanceFieldInset;
247 } else {
248 u0 = glyph->fAtlasLocation.fX;
249 v0 = glyph->fAtlasLocation.fY;
250 u1 = u0 + width;
251 v1 = v0 + height;
252 }
253
Jim Van Verth3b9c5442020-01-16 14:52:16 -0500254 // We pack the 2bit page index as the sign bit of the u and v texture coords
Herb Derbybc131b82020-01-07 16:07:42 -0500255 uint32_t pageIndex = glyph->pageIndex();
Jim Van Verth3b9c5442020-01-16 14:52:16 -0500256 std::tie(u0, v0) = GrDrawOpAtlas::PackIndexInTexCoords(u0, v0, pageIndex);
257 std::tie(u1, v1) = GrDrawOpAtlas::PackIndexInTexCoords(u1, v1, pageIndex);
Herb Derbybc131b82020-01-07 16:07:42 -0500258
259 textureCoords[0] = u0;
260 textureCoords[1] = v0;
Jim Van Verth3b9c5442020-01-16 14:52:16 -0500261 textureCoords = SkTAddOffset<int16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500262 textureCoords[0] = u0;
263 textureCoords[1] = v1;
Jim Van Verth3b9c5442020-01-16 14:52:16 -0500264 textureCoords = SkTAddOffset<int16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500265 textureCoords[0] = u1;
266 textureCoords[1] = v0;
Jim Van Verth3b9c5442020-01-16 14:52:16 -0500267 textureCoords = SkTAddOffset<int16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500268 textureCoords[0] = u1;
269 textureCoords[1] = v1;
Jim Van Verth3b9c5442020-01-16 14:52:16 -0500270 textureCoords = SkTAddOffset<int16_t>(textureCoords, vertexStride);
Herb Derbybc131b82020-01-07 16:07:42 -0500271 }
Herb Derbya56a1d72019-12-27 12:12:47 -0500272}
273
Herb Derbya8fa4902020-01-16 15:41:54 -0500274// GrStrikeCache may evict the strike a blob needs to generate texture coordinates.
275// If our strike has been abandoned in this way, we'll translate all our old glyphs
276// over to a new strike and carry on with that.
277void GrTextBlob::SubRun::updateStrikeIfNeeded(SkBulkGlyphMetricsAndImages* metricsAndImages,
278 GrStrikeCache* cache) {
279 if (this->strike()->isAbandoned()) {
280 sk_sp<GrTextStrike> newStrike = this->strikeSpec().findOrCreateGrStrike(cache);
281
282 // Take the glyphs from the old strike, and translate them a new strike.
283 for (size_t i = 0; i < fGlyphs.size(); i++) {
284 fGlyphs[i] = newStrike->getGlyph(fGlyphs[i]->fPackedID, metricsAndImages);
285 }
286
287 this->setStrike(newStrike);
288 }
289}
290
Herb Derbya9047642019-12-06 12:12:11 -0500291void GrTextBlob::SubRun::setUseLCDText(bool useLCDText) { fFlags.useLCDText = useLCDText; }
292bool GrTextBlob::SubRun::hasUseLCDText() const { return fFlags.useLCDText; }
293void GrTextBlob::SubRun::setAntiAliased(bool antiAliased) { fFlags.antiAliased = antiAliased; }
294bool GrTextBlob::SubRun::isAntiAliased() const { return fFlags.antiAliased; }
295const SkStrikeSpec& GrTextBlob::SubRun::strikeSpec() const { return fStrikeSpec; }
296
297// -- GrTextBlob -----------------------------------------------------------------------------------
298void GrTextBlob::operator delete(void* p) { ::operator delete(p); }
299void* GrTextBlob::operator new(size_t) { SK_ABORT("All blobs are created by placement new."); }
300void* GrTextBlob::operator new(size_t, void* p) { return p; }
301
302GrTextBlob::~GrTextBlob() = default;
303
Herb Derby659e4092019-12-06 15:38:10 -0500304sk_sp<GrTextBlob> GrTextBlob::Make(const SkGlyphRunList& glyphRunList,
Herb Derbyeba195f2019-12-03 16:44:47 -0500305 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500306 const SkMatrix& drawMatrix,
Herb Derbya00da612019-03-04 17:10:01 -0500307 GrColor color,
Herb Derbyeba195f2019-12-03 16:44:47 -0500308 bool forceWForDistanceFields) {
Herb Derbycd498e12019-12-06 14:17:31 -0500309
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500310 static_assert(sizeof(ARGB2DVertex) <= sizeof(Mask2DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500311 static_assert(alignof(ARGB2DVertex) <= alignof(Mask2DVertex));
312 size_t quadSize = sizeof(Mask2DVertex) * kVerticesPerGlyph;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500313 if (drawMatrix.hasPerspective() || forceWForDistanceFields) {
Herb Derby3d1a3bb2019-12-06 18:15:49 -0500314 static_assert(sizeof(ARGB3DVertex) <= sizeof(SDFT3DVertex));
Herb Derbycb718892019-12-07 00:07:42 -0500315 static_assert(alignof(ARGB3DVertex) <= alignof(SDFT3DVertex));
316 quadSize = sizeof(SDFT3DVertex) * kVerticesPerGlyph;
Herb Derbye74c7762019-12-04 14:15:41 -0500317 }
318
Herb Derbycb718892019-12-07 00:07:42 -0500319 // We can use the alignment of SDFT3DVertex as a proxy for all Vertex alignments.
320 static_assert(alignof(SDFT3DVertex) >= alignof(Mask2DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500321 // Assume there is no padding needed between glyph pointers and vertices.
Herb Derbycb718892019-12-07 00:07:42 -0500322 static_assert(alignof(GrGlyph*) >= alignof(SDFT3DVertex));
Herb Derbyc514e7d2019-12-11 17:00:31 -0500323
324 // In the arena, the layout is GrGlyph*... | SDFT3DVertex... | SubRun, so there is no padding
325 // between GrGlyph* and SDFT3DVertex, but padding is needed between the Mask2DVertex array
326 // and the SubRun.
327 size_t vertexToSubRunPadding = alignof(SDFT3DVertex) - alignof(SubRun);
328 size_t arenaSize =
329 sizeof(GrGlyph*) * glyphRunList.totalGlyphCount()
330 + quadSize * glyphRunList.totalGlyphCount()
331 + glyphRunList.runCount() * (sizeof(SubRun) + vertexToSubRunPadding);
332
333 size_t allocationSize = sizeof(GrTextBlob) + arenaSize;
Ben Wagner75d6db72018-09-20 14:39:39 -0400334
Herb Derbycb718892019-12-07 00:07:42 -0500335 void* allocation = ::operator new (allocationSize);
Herb Derbyb12175f2018-05-23 16:38:09 -0400336
Herb Derbyaebc5f82019-12-10 14:07:10 -0500337 SkColor initialLuminance = SkPaintPriv::ComputeLuminanceColor(glyphRunList.paint());
Herb Derby00ae9592019-12-03 15:55:56 -0500338 sk_sp<GrTextBlob> blob{new (allocation) GrTextBlob{
Herb Derby1c5be7b2019-12-13 12:03:06 -0500339 arenaSize, strikeCache, drawMatrix, glyphRunList.origin(),
Herb Derbyaebc5f82019-12-10 14:07:10 -0500340 color, initialLuminance, forceWForDistanceFields}};
joshualitt92303772016-02-10 11:55:52 -0800341
Herb Derbyf7d5d742018-11-16 13:24:32 -0500342 return blob;
joshualitt92303772016-02-10 11:55:52 -0800343}
344
Herb Derbya9047642019-12-06 12:12:11 -0500345void GrTextBlob::setupKey(const GrTextBlob::Key& key, const SkMaskFilterBase::BlurRec& blurRec,
346 const SkPaint& paint) {
347 fKey = key;
348 if (key.fHasBlur) {
349 fBlurRec = blurRec;
350 }
351 if (key.fStyle != SkPaint::kFill_Style) {
352 fStrokeInfo.fFrameWidth = paint.getStrokeWidth();
353 fStrokeInfo.fMiterLimit = paint.getStrokeMiter();
354 fStrokeInfo.fJoin = paint.getStrokeJoin();
355 }
356}
357const GrTextBlob::Key& GrTextBlob::GetKey(const GrTextBlob& blob) { return blob.fKey; }
358uint32_t GrTextBlob::Hash(const GrTextBlob::Key& key) { return SkOpts::hash(&key, sizeof(Key)); }
359
Herb Derbycb718892019-12-07 00:07:42 -0500360bool GrTextBlob::hasDistanceField() const {
361 return SkToBool(fTextType & kHasDistanceField_TextType);
362}
Herb Derbya9047642019-12-06 12:12:11 -0500363bool GrTextBlob::hasBitmap() const { return SkToBool(fTextType & kHasBitmap_TextType); }
Herb Derby1c5be7b2019-12-13 12:03:06 -0500364bool GrTextBlob::hasPerspective() const { return fInitialMatrix.hasPerspective(); }
Herb Derbya9047642019-12-06 12:12:11 -0500365
366void GrTextBlob::setHasDistanceField() { fTextType |= kHasDistanceField_TextType; }
367void GrTextBlob::setHasBitmap() { fTextType |= kHasBitmap_TextType; }
368void GrTextBlob::setMinAndMaxScale(SkScalar scaledMin, SkScalar scaledMax) {
369 // we init fMaxMinScale and fMinMaxScale in the constructor
370 fMaxMinScale = SkMaxScalar(scaledMin, fMaxMinScale);
371 fMinMaxScale = SkMinScalar(scaledMax, fMinMaxScale);
372}
373
374size_t GrTextBlob::GetVertexStride(GrMaskFormat maskFormat, bool hasWCoord) {
375 switch (maskFormat) {
376 case kA8_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500377 return hasWCoord ? sizeof(SDFT3DVertex) : sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500378 case kARGB_GrMaskFormat:
Herb Derbycd498e12019-12-06 14:17:31 -0500379 return hasWCoord ? sizeof(ARGB3DVertex) : sizeof(ARGB2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500380 default:
381 SkASSERT(!hasWCoord);
Herb Derbycd498e12019-12-06 14:17:31 -0500382 return sizeof(Mask2DVertex);
Herb Derbya9047642019-12-06 12:12:11 -0500383 }
384}
385
Herb Derby0edb2142018-10-16 17:04:11 -0400386bool GrTextBlob::mustRegenerate(const SkPaint& paint, bool anyRunHasSubpixelPosition,
387 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derby5bf5b042019-12-12 16:37:03 -0500388 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
joshualittfd5f6c12015-12-10 07:44:50 -0800389 // If we have LCD text then our canonical color will be set to transparent, in this case we have
390 // to regenerate the blob on any color change
391 // We use the grPaint to get any color filter effects
392 if (fKey.fCanonicalColor == SK_ColorTRANSPARENT &&
Herb Derbyaebc5f82019-12-10 14:07:10 -0500393 fInitialLuminance != SkPaintPriv::ComputeLuminanceColor(paint)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800394 return true;
395 }
396
Herb Derby1c5be7b2019-12-13 12:03:06 -0500397 if (fInitialMatrix.hasPerspective() != drawMatrix.hasPerspective()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800398 return true;
399 }
400
Brian Salomon5c6ac642017-12-19 11:09:32 -0500401 /** This could be relaxed for blobs with only distance field glyphs. */
Mike Reed2c383152019-12-18 16:47:47 -0500402 if (fInitialMatrix.hasPerspective() && !SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800403 return true;
404 }
405
406 // We only cache one masked version
407 if (fKey.fHasBlur &&
Mike Reed1be1f8d2018-03-14 13:01:17 -0400408 (fBlurRec.fSigma != blurRec.fSigma || fBlurRec.fStyle != blurRec.fStyle)) {
joshualittfd5f6c12015-12-10 07:44:50 -0800409 return true;
410 }
411
412 // Similarly, we only cache one version for each style
413 if (fKey.fStyle != SkPaint::kFill_Style &&
Herb Derbybc6f9c92018-08-08 13:58:45 -0400414 (fStrokeInfo.fFrameWidth != paint.getStrokeWidth() ||
415 fStrokeInfo.fMiterLimit != paint.getStrokeMiter() ||
416 fStrokeInfo.fJoin != paint.getStrokeJoin())) {
joshualittfd5f6c12015-12-10 07:44:50 -0800417 return true;
418 }
419
420 // Mixed blobs must be regenerated. We could probably figure out a way to do integer scrolls
421 // for mixed blobs if this becomes an issue.
422 if (this->hasBitmap() && this->hasDistanceField()) {
Herb Derbyeba195f2019-12-03 16:44:47 -0500423 // Identical view matrices and we can reuse in all cases
Mike Reed2c383152019-12-18 16:47:47 -0500424 return !(SkMatrixPriv::CheapEqual(fInitialMatrix, drawMatrix) &&
425 drawOrigin == fInitialOrigin);
joshualittfd5f6c12015-12-10 07:44:50 -0800426 }
427
428 if (this->hasBitmap()) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500429 if (fInitialMatrix.getScaleX() != drawMatrix.getScaleX() ||
430 fInitialMatrix.getScaleY() != drawMatrix.getScaleY() ||
431 fInitialMatrix.getSkewX() != drawMatrix.getSkewX() ||
432 fInitialMatrix.getSkewY() != drawMatrix.getSkewY()) {
joshualittfd5f6c12015-12-10 07:44:50 -0800433 return true;
434 }
435
Herb Derby9830fc42019-09-12 10:58:26 -0400436 // TODO(herb): this is not needed for full pixel glyph choice, but is needed to adjust
437 // the quads properly. Devise a system that regenerates the quads from original data
438 // using the transform to allow this to be used in general.
439
440 // We can update the positions in the text blob without regenerating the whole
441 // blob, but only for integer translations.
Herb Derby435adfe2020-01-14 15:12:04 -0500442 // Calculate the translation in source space to a translation in device space by mapping
443 // (0, 0) through both the initial matrix and the draw matrix; take the difference.
444 SkMatrix initialMatrix{fInitialMatrix};
445 initialMatrix.preTranslate(fInitialOrigin.x(), fInitialOrigin.y());
446 SkPoint initialDeviceOrigin{0, 0};
447 initialMatrix.mapPoints(&initialDeviceOrigin, 1);
448 SkMatrix completeDrawMatrix{drawMatrix};
449 completeDrawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
450 SkPoint drawDeviceOrigin{0, 0};
451 completeDrawMatrix.mapPoints(&drawDeviceOrigin, 1);
452 SkPoint translation = drawDeviceOrigin - initialDeviceOrigin;
453
454 if (!SkScalarIsInt(translation.x()) || !SkScalarIsInt(translation.y())) {
Herb Derby9830fc42019-09-12 10:58:26 -0400455 return true;
joshualittfd5f6c12015-12-10 07:44:50 -0800456 }
joshualittfd5f6c12015-12-10 07:44:50 -0800457 } else if (this->hasDistanceField()) {
458 // A scale outside of [blob.fMaxMinScale, blob.fMinMaxScale] would result in a different
459 // distance field being generated, so we have to regenerate in those cases
Herb Derby1c5be7b2019-12-13 12:03:06 -0500460 SkScalar newMaxScale = drawMatrix.getMaxScale();
461 SkScalar oldMaxScale = fInitialMatrix.getMaxScale();
joshualittfd5f6c12015-12-10 07:44:50 -0800462 SkScalar scaleAdjust = newMaxScale / oldMaxScale;
463 if (scaleAdjust < fMaxMinScale || scaleAdjust > fMinMaxScale) {
464 return true;
465 }
joshualittfd5f6c12015-12-10 07:44:50 -0800466 }
467
joshualittfd5f6c12015-12-10 07:44:50 -0800468 // It is possible that a blob has neither distanceField nor bitmaptext. This is in the case
469 // when all of the runs inside the blob are drawn as paths. In this case, we always regenerate
470 // the blob anyways at flush time, so no need to regenerate explicitly
471 return false;
472}
473
Herb Derbyc1b482c2018-08-09 15:02:27 -0400474void GrTextBlob::flush(GrTextTarget* target, const SkSurfaceProps& props,
475 const GrDistanceFieldAdjustTable* distanceAdjustTable,
Brian Osmancf860852018-10-31 14:04:39 -0400476 const SkPaint& paint, const SkPMColor4f& filteredColor, const GrClip& clip,
Herb Derby5bf5b042019-12-12 16:37:03 -0500477 const SkMatrix& drawMatrix, SkPoint drawOrigin) {
Jim Van Verth54d9c882018-02-08 16:14:48 -0500478
Herb Derbycb718892019-12-07 00:07:42 -0500479 for (SubRun* subRun = fFirstSubRun; subRun != nullptr; subRun = subRun->fNextSubRun) {
480 if (subRun->drawAsPaths()) {
Herb Derbydac1ed52018-09-12 17:04:21 -0400481 SkPaint runPaint{paint};
Herb Derbycb718892019-12-07 00:07:42 -0500482 runPaint.setAntiAlias(subRun->isAntiAliased());
Herb Derby1b8dcd12019-11-15 15:21:15 -0500483 // If there are shaders, blurs or styles, the path must be scaled into source
484 // space independently of the CTM. This allows the CTM to be correct for the
485 // different effects.
486 GrStyle style(runPaint);
Herb Derby9f491482018-08-08 11:53:00 -0400487
Herb Derby1b8dcd12019-11-15 15:21:15 -0500488 bool scalePath = runPaint.getShader()
489 || style.applies()
490 || runPaint.getMaskFilter();
Robert Phillips137ca522018-08-15 10:14:33 -0400491
Herb Derby1b8dcd12019-11-15 15:21:15 -0500492
Herb Derbycb718892019-12-07 00:07:42 -0500493 for (const auto& pathGlyph : subRun->fPaths) {
Herb Derby1c5be7b2019-12-13 12:03:06 -0500494 SkMatrix ctm{drawMatrix};
Herb Derby7b186102020-01-15 18:11:10 -0500495 ctm.preTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derbycb718892019-12-07 00:07:42 -0500496 SkMatrix pathMatrix = SkMatrix::MakeScale(
497 subRun->fStrikeSpec.strikeToSourceRatio());
Herb Derby7b186102020-01-15 18:11:10 -0500498 pathMatrix.postTranslate(pathGlyph.fOrigin.x(), pathGlyph.fOrigin.y());
Herb Derbydac1ed52018-09-12 17:04:21 -0400499
500 // TmpPath must be in the same scope as GrShape shape below.
Robert Phillips137ca522018-08-15 10:14:33 -0400501 SkTLazy<SkPath> tmpPath;
Herb Derby1b8dcd12019-11-15 15:21:15 -0500502 const SkPath* path = &pathGlyph.fPath;
Herb Derby2984d262019-11-20 14:40:39 -0500503 if (!scalePath) {
504 // Scale can be applied to CTM -- no effects.
Herb Derby2984d262019-11-20 14:40:39 -0500505 ctm.preConcat(pathMatrix);
Robert Phillipsd20d2612018-08-28 10:09:01 -0400506 } else {
Herb Derby2984d262019-11-20 14:40:39 -0500507 // Scale the outline into source space.
Herb Derbydac1ed52018-09-12 17:04:21 -0400508
Herb Derby2984d262019-11-20 14:40:39 -0500509 // Transform the path form the normalized outline to source space. This
510 // way the CTM will remain the same so it can be used by the effects.
511 SkPath* sourceOutline = tmpPath.init();
512 path->transform(pathMatrix, sourceOutline);
513 sourceOutline->setIsVolatile(true);
514 path = sourceOutline;
Robert Phillips137ca522018-08-15 10:14:33 -0400515 }
516
Robert Phillips46a13382018-08-23 13:53:01 -0400517 // TODO: we are losing the mutability of the path here
518 GrShape shape(*path, paint);
Herb Derbydac1ed52018-09-12 17:04:21 -0400519 target->drawShape(clip, runPaint, ctm, shape);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500520 }
Herb Derby1b8dcd12019-11-15 15:21:15 -0500521 } else {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500522 int glyphCount = subRun->fGlyphs.size();
Jim Van Verth54d9c882018-02-08 16:14:48 -0500523 if (0 == glyphCount) {
524 continue;
525 }
526
527 bool skipClip = false;
528 bool submitOp = true;
529 SkIRect clipRect = SkIRect::MakeEmpty();
530 SkRect rtBounds = SkRect::MakeWH(target->width(), target->height());
531 SkRRect clipRRect;
532 GrAA aa;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400533 // We can clip geometrically if we're not using SDFs or transformed glyphs,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500534 // and we have an axis-aligned rectangular non-AA clip
Herb Derbycb718892019-12-07 00:07:42 -0500535 if (!subRun->drawAsDistanceFields() && !subRun->needsTransform() &&
Jim Van Verthcf838c72018-03-05 14:40:36 -0500536 clip.isRRect(rtBounds, &clipRRect, &aa) &&
Jim Van Verth54d9c882018-02-08 16:14:48 -0500537 clipRRect.isRect() && GrAA::kNo == aa) {
538 skipClip = true;
539 // We only need to do clipping work if the subrun isn't contained by the clip
540 SkRect subRunBounds;
Herb Derby5bf5b042019-12-12 16:37:03 -0500541 this->computeSubRunBounds(
542 &subRunBounds, *subRun, drawMatrix, drawOrigin, false);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500543 if (!clipRRect.getBounds().contains(subRunBounds)) {
544 // If the subrun is completely outside, don't add an op for it
545 if (!clipRRect.getBounds().intersects(subRunBounds)) {
546 submitOp = false;
547 }
548 else {
549 clipRRect.getBounds().round(&clipRect);
550 }
551 }
552 }
553
554 if (submitOp) {
Herb Derby5bf5b042019-12-12 16:37:03 -0500555 auto op = this->makeOp(*subRun, glyphCount, drawMatrix, drawOrigin,
Herb Derbybc6f9c92018-08-08 13:58:45 -0400556 clipRect, paint, filteredColor, props, distanceAdjustTable,
Robert Phillips5a66efb2018-03-07 15:13:18 -0500557 target);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500558 if (op) {
559 if (skipClip) {
560 target->addDrawOp(GrNoClip(), std::move(op));
561 }
562 else {
563 target->addDrawOp(clip, std::move(op));
564 }
565 }
566 }
567 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000568 }
Jim Van Verth89737de2018-02-06 21:30:20 +0000569}
570
Herb Derby1c5be7b2019-12-13 12:03:06 -0500571void GrTextBlob::computeSubRunBounds(SkRect* outBounds, const SubRun& subRun,
Herb Derby5bf5b042019-12-12 16:37:03 -0500572 const SkMatrix& drawMatrix, SkPoint drawOrigin,
Herb Derbya9047642019-12-06 12:12:11 -0500573 bool needsGlyphTransform) {
574 // We don't yet position distance field text on the cpu, so we have to map the vertex bounds
575 // into device space.
576 // We handle vertex bounds differently for distance field text and bitmap text because
577 // the vertex bounds of bitmap text are in device space. If we are flushing multiple runs
578 // from one blob then we are going to pay the price here of mapping the rect for each run.
579 *outBounds = subRun.vertexBounds();
580 if (needsGlyphTransform) {
581 // Distance field text is positioned with the (X,Y) as part of the glyph position,
582 // and currently the view matrix is applied on the GPU
Herb Derby5bf5b042019-12-12 16:37:03 -0500583 outBounds->offset(drawOrigin - fInitialOrigin);
Herb Derby1c5be7b2019-12-13 12:03:06 -0500584 drawMatrix.mapRect(outBounds);
Herb Derbya9047642019-12-06 12:12:11 -0500585 } else {
586 // Bitmap text is fully positioned on the CPU, and offset by an (X,Y) translate in
587 // device space.
Herb Derby1c5be7b2019-12-13 12:03:06 -0500588 SkMatrix boundsMatrix = fInitialMatrixInverse;
Herb Derbya9047642019-12-06 12:12:11 -0500589
590 boundsMatrix.postTranslate(-fInitialOrigin.x(), -fInitialOrigin.y());
591
Herb Derby5bf5b042019-12-12 16:37:03 -0500592 boundsMatrix.postTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derbya9047642019-12-06 12:12:11 -0500593
Herb Derby1c5be7b2019-12-13 12:03:06 -0500594 boundsMatrix.postConcat(drawMatrix);
Herb Derbya9047642019-12-06 12:12:11 -0500595 boundsMatrix.mapRect(outBounds);
596
597 // Due to floating point numerical inaccuracies, we have to round out here
598 outBounds->roundOut(outBounds);
599 }
joshualitt323c2eb2016-01-20 06:48:47 -0800600}
joshualitt2e2202e2015-12-10 11:22:08 -0800601
Herb Derbya9047642019-12-06 12:12:11 -0500602const GrTextBlob::Key& GrTextBlob::key() const { return fKey; }
603size_t GrTextBlob::size() const { return fSize; }
604
605std::unique_ptr<GrDrawOp> GrTextBlob::test_makeOp(
Herb Derby1c5be7b2019-12-13 12:03:06 -0500606 int glyphCount, const SkMatrix& drawMatrix,
Herb Derby5bf5b042019-12-12 16:37:03 -0500607 SkPoint drawOrigin, const SkPaint& paint, const SkPMColor4f& filteredColor,
Herb Derbya9047642019-12-06 12:12:11 -0500608 const SkSurfaceProps& props, const GrDistanceFieldAdjustTable* distanceAdjustTable,
609 GrTextTarget* target) {
Herb Derbycb718892019-12-07 00:07:42 -0500610 SubRun* info = fFirstSubRun;
Herb Derbya9047642019-12-06 12:12:11 -0500611 SkIRect emptyRect = SkIRect::MakeEmpty();
Herb Derby5bf5b042019-12-12 16:37:03 -0500612 return this->makeOp(*info, glyphCount, drawMatrix, drawOrigin, emptyRect,
Herb Derbya9047642019-12-06 12:12:11 -0500613 paint, filteredColor, props, distanceAdjustTable, target);
614}
615
616bool GrTextBlob::hasW(GrTextBlob::SubRunType type) const {
617 if (type == kTransformedSDFT) {
618 return this->hasPerspective() || fForceWForDistanceFields;
619 } else if (type == kTransformedMask || type == kTransformedPath) {
620 return this->hasPerspective();
Herb Derbye9f691d2019-12-04 12:11:13 -0500621 }
Herb Derbya9047642019-12-06 12:12:11 -0500622
623 // The viewMatrix is implicitly SkMatrix::I when drawing kDirectMask, because it is not
624 // used.
625 return false;
626}
627
628GrTextBlob::SubRun* GrTextBlob::makeSubRun(SubRunType type,
629 const SkZip<SkGlyphVariant, SkPoint>& drawables,
630 const SkStrikeSpec& strikeSpec,
631 GrMaskFormat format) {
Herb Derbyc514e7d2019-12-11 17:00:31 -0500632 SkSpan<GrGlyph*> glyphs{fAlloc.makeArrayDefault<GrGlyph*>(drawables.size()), drawables.size()};
Herb Derbya9047642019-12-06 12:12:11 -0500633 bool hasW = this->hasW(type);
Herb Derby3d3150c2019-12-23 15:26:44 -0500634
635 SkASSERT(!fInitialMatrix.hasPerspective() || hasW);
636
Herb Derbyc514e7d2019-12-11 17:00:31 -0500637 size_t vertexDataSize = drawables.size() * GetVertexStride(format, hasW) * kVerticesPerGlyph;
638 SkSpan<char> vertexData{fAlloc.makeArrayDefault<char>(vertexDataSize), vertexDataSize};
Herb Derbya9047642019-12-06 12:12:11 -0500639
640 sk_sp<GrTextStrike> grStrike = strikeSpec.findOrCreateGrStrike(fStrikeCache);
641
Herb Derbycb718892019-12-07 00:07:42 -0500642 SubRun* subRun = fAlloc.make<SubRun>(
Herb Derbyc514e7d2019-12-11 17:00:31 -0500643 type, this, strikeSpec, format, glyphs, vertexData, std::move(grStrike));
Herb Derbya9047642019-12-06 12:12:11 -0500644
Herb Derbycb718892019-12-07 00:07:42 -0500645 subRun->appendGlyphs(drawables);
Herb Derbya9047642019-12-06 12:12:11 -0500646
Herb Derbycb718892019-12-07 00:07:42 -0500647 return subRun;
Herb Derbya9047642019-12-06 12:12:11 -0500648}
649
650void GrTextBlob::addSingleMaskFormat(
651 SubRunType type,
652 const SkZip<SkGlyphVariant, SkPoint>& drawables,
653 const SkStrikeSpec& strikeSpec,
654 GrMaskFormat format) {
655 this->makeSubRun(type, drawables, strikeSpec, format);
656}
657
658void GrTextBlob::addMultiMaskFormat(
659 SubRunType type,
660 const SkZip<SkGlyphVariant, SkPoint>& drawables,
661 const SkStrikeSpec& strikeSpec) {
662 this->setHasBitmap();
663 if (drawables.empty()) { return; }
664
665 auto glyphSpan = drawables.get<0>();
666 SkGlyph* glyph = glyphSpan[0];
667 GrMaskFormat format = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
668 size_t startIndex = 0;
669 for (size_t i = 1; i < drawables.size(); i++) {
670 glyph = glyphSpan[i];
671 GrMaskFormat nextFormat = GrGlyph::FormatFromSkGlyph(glyph->maskFormat());
672 if (format != nextFormat) {
673 auto sameFormat = drawables.subspan(startIndex, i - startIndex);
674 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
675 format = nextFormat;
676 startIndex = i;
677 }
678 }
679 auto sameFormat = drawables.last(drawables.size() - startIndex);
680 this->addSingleMaskFormat(type, sameFormat, strikeSpec, format);
681}
682
683void GrTextBlob::addSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
684 const SkStrikeSpec& strikeSpec,
685 const SkFont& runFont,
686 SkScalar minScale,
687 SkScalar maxScale) {
688 this->setHasDistanceField();
689 this->setMinAndMaxScale(minScale, maxScale);
690
691 SubRun* subRun = this->makeSubRun(kTransformedSDFT, drawables, strikeSpec, kA8_GrMaskFormat);
692 subRun->setUseLCDText(runFont.getEdging() == SkFont::Edging::kSubpixelAntiAlias);
693 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbye9f691d2019-12-04 12:11:13 -0500694}
695
Herb Derbycb718892019-12-07 00:07:42 -0500696GrTextBlob::GrTextBlob(size_t allocSize,
Herb Derbyeba195f2019-12-03 16:44:47 -0500697 GrStrikeCache* strikeCache,
Herb Derby1c5be7b2019-12-13 12:03:06 -0500698 const SkMatrix& drawMatrix,
Herb Derbyeba195f2019-12-03 16:44:47 -0500699 SkPoint origin,
700 GrColor color,
Herb Derbyaebc5f82019-12-10 14:07:10 -0500701 SkColor initialLuminance,
Herb Derby00ae9592019-12-03 15:55:56 -0500702 bool forceWForDistanceFields)
Herb Derbycb718892019-12-07 00:07:42 -0500703 : fSize{allocSize}
Herb Derby00ae9592019-12-03 15:55:56 -0500704 , fStrikeCache{strikeCache}
Herb Derby1c5be7b2019-12-13 12:03:06 -0500705 , fInitialMatrix{drawMatrix}
706 , fInitialMatrixInverse{make_inverse(drawMatrix)}
Herb Derbyeba195f2019-12-03 16:44:47 -0500707 , fInitialOrigin{origin}
Herb Derby00ae9592019-12-03 15:55:56 -0500708 , fForceWForDistanceFields{forceWForDistanceFields}
Herb Derbycb718892019-12-07 00:07:42 -0500709 , fColor{color}
Herb Derbyaebc5f82019-12-10 14:07:10 -0500710 , fInitialLuminance{initialLuminance}
Herb Derbycb718892019-12-07 00:07:42 -0500711 , fAlloc{SkTAddOffset<char>(this, sizeof(GrTextBlob)), allocSize, allocSize/2} { }
Herb Derby00ae9592019-12-03 15:55:56 -0500712
Herb Derbycb718892019-12-07 00:07:42 -0500713void GrTextBlob::insertSubRun(SubRun* subRun) {
714 if (fFirstSubRun == nullptr) {
715 fFirstSubRun = subRun;
716 fLastSubRun = subRun;
717 } else {
718 fLastSubRun->fNextSubRun = subRun;
719 fLastSubRun = subRun;
720 }
721}
722
723std::unique_ptr<GrAtlasTextOp> GrTextBlob::makeOp(
Herb Derbya9047642019-12-06 12:12:11 -0500724 SubRun& info, int glyphCount,
Herb Derby5bf5b042019-12-12 16:37:03 -0500725 const SkMatrix& drawMatrix, SkPoint drawOrigin, const SkIRect& clipRect,
Herb Derbya9047642019-12-06 12:12:11 -0500726 const SkPaint& paint, const SkPMColor4f& filteredColor, const SkSurfaceProps& props,
727 const GrDistanceFieldAdjustTable* distanceAdjustTable, GrTextTarget* target) {
728 GrMaskFormat format = info.maskFormat();
729
730 GrPaint grPaint;
Herb Derby1c5be7b2019-12-13 12:03:06 -0500731 target->makeGrPaint(info.maskFormat(), paint, drawMatrix, &grPaint);
Herb Derbya9047642019-12-06 12:12:11 -0500732 std::unique_ptr<GrAtlasTextOp> op;
733 if (info.drawAsDistanceFields()) {
734 // TODO: Can we be even smarter based on the dest transfer function?
735 op = GrAtlasTextOp::MakeDistanceField(
736 target->getContext(), std::move(grPaint), glyphCount, distanceAdjustTable,
737 target->colorInfo().isLinearlyBlended(), SkPaintPriv::ComputeLuminanceColor(paint),
738 props, info.isAntiAliased(), info.hasUseLCDText());
Herb Derbyeba195f2019-12-03 16:44:47 -0500739 } else {
Herb Derbya9047642019-12-06 12:12:11 -0500740 op = GrAtlasTextOp::MakeBitmap(target->getContext(), std::move(grPaint), format, glyphCount,
741 info.needsTransform());
742 }
743 GrAtlasTextOp::Geometry& geometry = op->geometry();
Herb Derby1c5be7b2019-12-13 12:03:06 -0500744 geometry.fDrawMatrix = drawMatrix;
Herb Derbya9047642019-12-06 12:12:11 -0500745 geometry.fClipRect = clipRect;
746 geometry.fBlob = SkRef(this);
747 geometry.fSubRunPtr = &info;
748 geometry.fColor = info.maskFormat() == kARGB_GrMaskFormat ? SK_PMColor4fWHITE : filteredColor;
Herb Derby5bf5b042019-12-12 16:37:03 -0500749 geometry.fDrawOrigin = drawOrigin;
Herb Derbya9047642019-12-06 12:12:11 -0500750 op->init();
751 return op;
752}
Herb Derbyeba195f2019-12-03 16:44:47 -0500753
Herb Derbya9047642019-12-06 12:12:11 -0500754void GrTextBlob::processDeviceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
755 const SkStrikeSpec& strikeSpec) {
756 this->addMultiMaskFormat(kDirectMask, drawables, strikeSpec);
757}
Herb Derbyeba195f2019-12-03 16:44:47 -0500758
Herb Derbya9047642019-12-06 12:12:11 -0500759void GrTextBlob::processSourcePaths(const SkZip<SkGlyphVariant, SkPoint>& drawables,
760 const SkFont& runFont,
761 const SkStrikeSpec& strikeSpec) {
762 this->setHasBitmap();
Herb Derbycb718892019-12-07 00:07:42 -0500763 SubRun* subRun = fAlloc.make<SubRun>(this, strikeSpec);
764 subRun->setAntiAliased(runFont.hasSomeAntiAliasing());
Herb Derbya9047642019-12-06 12:12:11 -0500765 for (auto [variant, pos] : drawables) {
Herb Derbycb718892019-12-07 00:07:42 -0500766 subRun->fPaths.emplace_back(*variant.path(), pos);
Herb Derbyeba195f2019-12-03 16:44:47 -0500767 }
768}
769
Herb Derbya9047642019-12-06 12:12:11 -0500770void GrTextBlob::processSourceSDFT(const SkZip<SkGlyphVariant, SkPoint>& drawables,
771 const SkStrikeSpec& strikeSpec,
772 const SkFont& runFont,
773 SkScalar minScale,
774 SkScalar maxScale) {
775 this->addSDFT(drawables, strikeSpec, runFont, minScale, maxScale);
joshualitt8e0ef292016-02-19 14:13:03 -0800776}
Herb Derbya9047642019-12-06 12:12:11 -0500777
778void GrTextBlob::processSourceMasks(const SkZip<SkGlyphVariant, SkPoint>& drawables,
779 const SkStrikeSpec& strikeSpec) {
780 this->addMultiMaskFormat(kTransformedMask, drawables, strikeSpec);
781}
782
783// -- GrTextBlob::VertexRegenerator ----------------------------------------------------------------
Herb Derbya9047642019-12-06 12:12:11 -0500784GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
Herb Derbya9047642019-12-06 12:12:11 -0500785 GrTextBlob::SubRun* subRun,
Herb Derbya9047642019-12-06 12:12:11 -0500786 GrDeferredUploadTarget* uploadTarget,
Mike Klein99e002f2020-01-16 16:45:03 +0000787 GrStrikeCache* grStrikeCache,
Herb Derbya9047642019-12-06 12:12:11 -0500788 GrAtlasManager* fullAtlasManager)
789 : fResourceProvider(resourceProvider)
Herb Derbya9047642019-12-06 12:12:11 -0500790 , fUploadTarget(uploadTarget)
Mike Klein99e002f2020-01-16 16:45:03 +0000791 , fGrStrikeCache(grStrikeCache)
Herb Derbya9047642019-12-06 12:12:11 -0500792 , fFullAtlasManager(fullAtlasManager)
Herb Derbya8fa4902020-01-16 15:41:54 -0500793 , fSubRun(subRun) { }
Herb Derbya9047642019-12-06 12:12:11 -0500794
Herb Derbyb45558d2020-01-07 16:57:12 -0500795std::tuple<bool, int> GrTextBlob::VertexRegenerator::updateTextureCoordinatesMaybeStrike(
796 const int begin, const int end) {
Herb Derby586f8d02020-01-07 15:28:07 -0500797 fSubRun->resetBulkUseToken();
Herb Derbya9047642019-12-06 12:12:11 -0500798
Herb Derby586f8d02020-01-07 15:28:07 -0500799 const SkStrikeSpec& strikeSpec = fSubRun->strikeSpec();
Herb Derbya9047642019-12-06 12:12:11 -0500800
Herb Derby586f8d02020-01-07 15:28:07 -0500801 if (!fMetricsAndImages.isValid()
Herb Derbya9047642019-12-06 12:12:11 -0500802 || fMetricsAndImages->descriptor() != strikeSpec.descriptor()) {
Herb Derby586f8d02020-01-07 15:28:07 -0500803 fMetricsAndImages.init(strikeSpec);
804 }
805
Herb Derbya8fa4902020-01-16 15:41:54 -0500806 fSubRun->updateStrikeIfNeeded(fMetricsAndImages.get(), fGrStrikeCache);
Herb Derbya9047642019-12-06 12:12:11 -0500807
Herb Derbybc131b82020-01-07 16:07:42 -0500808 // Update the atlas information in the GrStrike.
Herb Derby83b907e2020-01-06 13:44:29 -0500809 auto code = GrDrawOpAtlas::ErrorCode::kSucceeded;
Herb Derby586f8d02020-01-07 15:28:07 -0500810 GrTextStrike* grStrike = fSubRun->strike();
Herb Derbybc131b82020-01-07 16:07:42 -0500811 auto tokenTracker = fUploadTarget->tokenTracker();
Herb Derbyb45558d2020-01-07 16:57:12 -0500812 int i = begin;
813 for (; i < end; i++) {
814 GrGlyph* glyph = fSubRun->fGlyphs[i];
Herb Derby586f8d02020-01-07 15:28:07 -0500815 SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
Herb Derbya9047642019-12-06 12:12:11 -0500816
Herb Derby586f8d02020-01-07 15:28:07 -0500817 if (!fFullAtlasManager->hasGlyph(glyph)) {
818 code = grStrike->addGlyphToAtlas(
Mike Klein99e002f2020-01-16 16:45:03 +0000819 fResourceProvider, fUploadTarget, fGrStrikeCache, fFullAtlasManager, glyph,
820 fMetricsAndImages.get(), fSubRun->maskFormat(), fSubRun->needsTransform());
Herb Derby586f8d02020-01-07 15:28:07 -0500821 if (code != GrDrawOpAtlas::ErrorCode::kSucceeded) {
822 break;
Herb Derbya9047642019-12-06 12:12:11 -0500823 }
Herb Derbya9047642019-12-06 12:12:11 -0500824 }
Herb Derby586f8d02020-01-07 15:28:07 -0500825 fFullAtlasManager->addGlyphToBulkAndSetUseToken(
826 fSubRun->bulkUseToken(), glyph, tokenTracker->nextDrawToken());
Herb Derbya9047642019-12-06 12:12:11 -0500827 }
Herb Derby23f29762020-01-10 16:26:14 -0500828 int glyphsPlacedInAtlas = i - begin;
Herb Derbya9047642019-12-06 12:12:11 -0500829
Herb Derbybc131b82020-01-07 16:07:42 -0500830 // Update the quads with the new atlas coordinates.
Herb Derby23f29762020-01-10 16:26:14 -0500831 fSubRun->updateTexCoords(begin, begin + glyphsPlacedInAtlas);
Herb Derbybc131b82020-01-07 16:07:42 -0500832
Herb Derbyb45558d2020-01-07 16:57:12 -0500833 if (code == GrDrawOpAtlas::ErrorCode::kSucceeded) {
834 // If we reach here with begin > 0, some earlier call to regenerate() exhausted the atlas
835 // before it could place all its glyphs and returned kTryAgain. Invalidate texture
836 // coordinates, forcing them to be regenerated, minding the atlas flush between.
837 fSubRun->fAtlasGeneration =
838 begin > 0 ? GrDrawOpAtlas::kInvalidAtlasGeneration
839 : fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
Herb Derbya9047642019-12-06 12:12:11 -0500840 }
Herb Derby83b907e2020-01-06 13:44:29 -0500841
Herb Derby23f29762020-01-10 16:26:14 -0500842 return {code != GrDrawOpAtlas::ErrorCode::kError, glyphsPlacedInAtlas};
Herb Derbya9047642019-12-06 12:12:11 -0500843}
844
Herb Derby23f29762020-01-10 16:26:14 -0500845std::tuple<bool, int> GrTextBlob::VertexRegenerator::regenerate(int begin, int end) {
Herb Derbya9047642019-12-06 12:12:11 -0500846 uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
847 // If regenerate() is called multiple times then the atlas gen may have changed. So we check
848 // this each time.
Herb Derbya9047642019-12-06 12:12:11 -0500849
Herb Derbya8fa4902020-01-16 15:41:54 -0500850 // TODO: figure out why this needs to latch true instead of maintaining the invariant with
851 // the atlas generation.
852 fRegenerateTextureCoordinates =
853 fRegenerateTextureCoordinates || fSubRun->fAtlasGeneration != currentAtlasGen;
854 if (fSubRun->strike()->isAbandoned() || fRegenerateTextureCoordinates) {
Herb Derby23f29762020-01-10 16:26:14 -0500855 return this->updateTextureCoordinatesMaybeStrike(begin, end);
Herb Derby51a95ce2020-01-08 17:49:51 -0500856 } else {
Herb Derby51a95ce2020-01-08 17:49:51 -0500857 // All glyphs are inserted into the atlas if fCurrGlyph is at the end of fGlyphs.
Herb Derby23f29762020-01-10 16:26:14 -0500858 if (end == (int)fSubRun->fGlyphs.size()) {
Herb Derby51a95ce2020-01-08 17:49:51 -0500859 // Set use tokens for all of the glyphs in our SubRun. This is only valid if we
Brian Salomon43cbd722020-01-03 22:09:12 -0500860 // have a valid atlas generation
861 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
862 fUploadTarget->tokenTracker()->nextDrawToken(),
863 fSubRun->maskFormat());
864 }
Herb Derby23f29762020-01-10 16:26:14 -0500865 return {true, end - begin};
Herb Derbya9047642019-12-06 12:12:11 -0500866 }
Herb Derbya9047642019-12-06 12:12:11 -0500867}