blob: 1b2b11739cb78a0d1ad0e156d6accfdccbbe029c [file] [log] [blame]
Brian Salomon18923f92017-11-06 16:26:02 -05001/*
2 * Copyright 2016 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
Robert Phillipsc4039ea2018-03-01 11:36:45 -05008#include "GrAtlasManager.h"
Herb Derby86240592018-05-24 16:12:31 -04009#include "GrTextBlob.h"
Herb Derbyc1b482c2018-08-09 15:02:27 -040010#include "GrTextTarget.h"
Brian Salomon18923f92017-11-06 16:26:02 -050011#include "SkDistanceFieldGen.h"
Brian Salomon18923f92017-11-06 16:26:02 -050012#include "ops/GrAtlasTextOp.h"
13
Brian Salomon18923f92017-11-06 16:26:02 -050014enum RegenMask {
15 kNoRegen = 0x0,
16 kRegenPos = 0x1,
17 kRegenCol = 0x2,
18 kRegenTex = 0x4,
Brian Osman5d6be8f2019-01-08 12:02:51 -050019 kRegenGlyph = 0x8,
Brian Salomon18923f92017-11-06 16:26:02 -050020};
21
22////////////////////////////////////////////////////////////////////////////////////////////////////
Brian Osman5d6be8f2019-01-08 12:02:51 -050023
24static void regen_positions(char* vertex, size_t vertexStride, SkScalar transX, SkScalar transY) {
25 SkPoint* point = reinterpret_cast<SkPoint*>(vertex);
26 for (int i = 0; i < 4; ++i) {
27 point->fX += transX;
28 point->fY += transY;
29 point = SkTAddOffset<SkPoint>(point, vertexStride);
30 }
31}
32
33static void regen_colors(char* vertex, size_t vertexStride, GrColor color) {
34 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
35 // vertices, hence vertexStride - sizeof(SkIPoint16)
36 size_t colorOffset = vertexStride - sizeof(SkIPoint16) - sizeof(GrColor);
37 GrColor* vcolor = reinterpret_cast<GrColor*>(vertex + colorOffset);
38 for (int i = 0; i < 4; ++i) {
39 *vcolor = color;
40 vcolor = SkTAddOffset<GrColor>(vcolor, vertexStride);
41 }
42}
43
44static void regen_texcoords(char* vertex, size_t vertexStride, const GrGlyph* glyph,
45 bool useDistanceFields) {
46 // This is a bit wonky, but sometimes we have LCD text, in which case we won't have color
47 // vertices, hence vertexStride - sizeof(SkIPoint16)
48 size_t texCoordOffset = vertexStride - sizeof(SkIPoint16);
49
Brian Salomon18923f92017-11-06 16:26:02 -050050 uint16_t u0, v0, u1, v1;
Brian Osman5d6be8f2019-01-08 12:02:51 -050051 SkASSERT(glyph);
52 int width = glyph->fBounds.width();
53 int height = glyph->fBounds.height();
54
55 if (useDistanceFields) {
56 u0 = glyph->fAtlasLocation.fX + SK_DistanceFieldInset;
57 v0 = glyph->fAtlasLocation.fY + SK_DistanceFieldInset;
58 u1 = u0 + width - 2 * SK_DistanceFieldInset;
59 v1 = v0 + height - 2 * SK_DistanceFieldInset;
60 } else {
61 u0 = glyph->fAtlasLocation.fX;
62 v0 = glyph->fAtlasLocation.fY;
63 u1 = u0 + width;
64 v1 = v0 + height;
65 }
66 // We pack the 2bit page index in the low bit of the u and v texture coords
67 uint32_t pageIndex = glyph->pageIndex();
68 SkASSERT(pageIndex < 4);
69 uint16_t uBit = (pageIndex >> 1) & 0x1;
70 uint16_t vBit = pageIndex & 0x1;
71 u0 <<= 1;
72 u0 |= uBit;
73 v0 <<= 1;
74 v0 |= vBit;
75 u1 <<= 1;
76 u1 |= uBit;
77 v1 <<= 1;
78 v1 |= vBit;
79
80 uint16_t* textureCoords = reinterpret_cast<uint16_t*>(vertex + texCoordOffset);
81 textureCoords[0] = u0;
82 textureCoords[1] = v0;
83 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
84 textureCoords[0] = u0;
85 textureCoords[1] = v1;
86 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
87 textureCoords[0] = u1;
88 textureCoords[1] = v0;
89 textureCoords = SkTAddOffset<uint16_t>(textureCoords, vertexStride);
90 textureCoords[0] = u1;
91 textureCoords[1] = v1;
92
Jim Van Verthfc4f7682018-01-25 16:26:25 -050093#ifdef DISPLAY_PAGE_INDEX
94 // Enable this to visualize the page from which each glyph is being drawn.
95 // Green Red Magenta Cyan -> 0 1 2 3; Black -> error
Brian Osman5d6be8f2019-01-08 12:02:51 -050096 GrColor hackColor;
97 switch (pageIndex) {
98 case 0:
99 hackColor = GrColorPackRGBA(0, 255, 0, 255);
100 break;
101 case 1:
102 hackColor = GrColorPackRGBA(255, 0, 0, 255);;
103 break;
104 case 2:
105 hackColor = GrColorPackRGBA(255, 0, 255, 255);
106 break;
107 case 3:
108 hackColor = GrColorPackRGBA(0, 255, 255, 255);
109 break;
110 default:
111 hackColor = GrColorPackRGBA(0, 0, 0, 255);
112 break;
113 }
114 regen_colors(vertex, vertexStride, hackColor);
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500115#endif
Brian Salomon18923f92017-11-06 16:26:02 -0500116}
117
Herb Derby6dff60e2018-11-12 15:45:49 -0500118GrTextBlob::VertexRegenerator::VertexRegenerator(GrResourceProvider* resourceProvider,
119 GrTextBlob* blob,
120 int runIdx, int subRunIdx,
121 const SkMatrix& viewMatrix, SkScalar x, SkScalar y,
122 GrColor color,
123 GrDeferredUploadTarget* uploadTarget,
Herb Derby081e6f32019-01-16 13:46:02 -0500124 GrStrikeCache* glyphCache,
Herb Derby6dff60e2018-11-12 15:45:49 -0500125 GrAtlasManager* fullAtlasManager,
126 SkExclusiveStrikePtr* lazyCache)
Robert Phillips4bc70112018-03-01 10:24:02 -0500127 : fResourceProvider(resourceProvider)
128 , fViewMatrix(viewMatrix)
Brian Salomon18923f92017-11-06 16:26:02 -0500129 , fBlob(blob)
130 , fUploadTarget(uploadTarget)
131 , fGlyphCache(glyphCache)
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500132 , fFullAtlasManager(fullAtlasManager)
Brian Salomon18923f92017-11-06 16:26:02 -0500133 , fLazyCache(lazyCache)
134 , fRun(&blob->fRuns[runIdx])
135 , fSubRun(&blob->fRuns[runIdx].fSubRunInfo[subRunIdx])
Brian Salomon18923f92017-11-06 16:26:02 -0500136 , fColor(color) {
137 // Compute translation if any
138 fSubRun->computeTranslation(fViewMatrix, x, y, &fTransX, &fTransY);
139
Herb Derby081e6f32019-01-16 13:46:02 -0500140 // Because the GrStrikeCache may evict the strike a blob depends on using for
Brian Salomon18923f92017-11-06 16:26:02 -0500141 // generating its texture coords, we have to track whether or not the strike has
142 // been abandoned. If it hasn't been abandoned, then we can use the GrGlyph*s as is
143 // otherwise we have to get the new strike, and use that to get the correct glyphs.
144 // Because we do not have the packed ids, and thus can't look up our glyphs in the
145 // new strike, we instead keep our ref to the old strike and use the packed ids from
146 // it. These ids will still be valid as long as we hold the ref. When we are done
147 // updating our cache of the GrGlyph*s, we drop our ref on the old strike
148 if (fSubRun->strike()->isAbandoned()) {
149 fRegenFlags |= kRegenGlyph;
150 fRegenFlags |= kRegenTex;
151 }
152 if (kARGB_GrMaskFormat != fSubRun->maskFormat() && fSubRun->color() != color) {
153 fRegenFlags |= kRegenCol;
154 }
155 if (0.f != fTransX || 0.f != fTransY) {
156 fRegenFlags |= kRegenPos;
157 }
158}
159
Brian Osman5d6be8f2019-01-08 12:02:51 -0500160bool GrTextBlob::VertexRegenerator::doRegen(GrTextBlob::VertexRegenerator::Result* result,
161 bool regenPos, bool regenCol, bool regenTexCoords,
162 bool regenGlyphs) {
163 SkASSERT(!regenGlyphs || regenTexCoords);
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500164 sk_sp<GrTextStrike> strike;
Brian Salomon18923f92017-11-06 16:26:02 -0500165 if (regenTexCoords) {
166 fSubRun->resetBulkUseToken();
167
Herb Derby317adf72018-11-16 17:29:29 -0500168 const SkDescriptor* desc = fSubRun->desc();
Brian Salomon18923f92017-11-06 16:26:02 -0500169
170 if (!*fLazyCache || (*fLazyCache)->getDescriptor() != *desc) {
171 SkScalerContextEffects effects;
172 effects.fPathEffect = fRun->fPathEffect.get();
Brian Salomon18923f92017-11-06 16:26:02 -0500173 effects.fMaskFilter = fRun->fMaskFilter.get();
Herb Derby8c4cbf42018-03-09 15:28:04 -0500174 *fLazyCache =
Herb Derbyfa996902018-04-18 11:36:12 -0400175 SkStrikeCache::FindOrCreateStrikeExclusive(*desc, effects, *fRun->fTypeface);
Brian Salomon18923f92017-11-06 16:26:02 -0500176 }
177
178 if (regenGlyphs) {
Herb Derby30595ea2019-02-11 11:25:55 -0500179 strike = fGlyphCache->getStrike((*fLazyCache)->getDescriptor());
Brian Salomon18923f92017-11-06 16:26:02 -0500180 } else {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500181 strike = fSubRun->refStrike();
Brian Salomon18923f92017-11-06 16:26:02 -0500182 }
183 }
184
Brian Salomon5c6ac642017-12-19 11:09:32 -0500185 bool hasW = fSubRun->hasWCoord();
Brian Salomon5c6ac642017-12-19 11:09:32 -0500186 auto vertexStride = GetVertexStride(fSubRun->maskFormat(), hasW);
Brian Salomon18923f92017-11-06 16:26:02 -0500187 char* currVertex = fBlob->fVertices + fSubRun->vertexStartIndex() +
Brian Salomondeb53cc2017-11-08 13:50:53 -0500188 fCurrGlyph * kVerticesPerGlyph * vertexStride;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500189 result->fFirstVertex = currVertex;
Brian Salomon18923f92017-11-06 16:26:02 -0500190
191 for (int glyphIdx = fCurrGlyph; glyphIdx < (int)fSubRun->glyphCount(); glyphIdx++) {
192 GrGlyph* glyph = nullptr;
193 if (regenTexCoords) {
194 size_t glyphOffset = glyphIdx + fSubRun->glyphStartIndex();
195
196 if (regenGlyphs) {
197 // Get the id from the old glyph, and use the new strike to lookup
198 // the glyph.
Herb Derby5a3fdee2018-12-20 14:47:03 -0500199 SkPackedGlyphID id = fBlob->fGlyphs[glyphOffset]->fPackedID;
Herb Derby4c35be02018-12-20 14:03:47 -0500200 fBlob->fGlyphs[glyphOffset] = strike->getGlyph(id, fLazyCache->get());
Brian Salomon18923f92017-11-06 16:26:02 -0500201 SkASSERT(id == fBlob->fGlyphs[glyphOffset]->fPackedID);
202 }
203 glyph = fBlob->fGlyphs[glyphOffset];
204 SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
205
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500206 if (!fFullAtlasManager->hasGlyph(glyph)) {
207 GrDrawOpAtlas::ErrorCode code;
208 code = strike->addGlyphToAtlas(fResourceProvider, fUploadTarget, fGlyphCache,
209 fFullAtlasManager, glyph,
210 fLazyCache->get(), fSubRun->maskFormat(),
Jim Van Verthb515ae72018-05-23 16:44:55 -0400211 fSubRun->needsTransform());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500212 if (GrDrawOpAtlas::ErrorCode::kError == code) {
213 // Something horrible has happened - drop the op
214 return false;
215 }
216 else if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
217 fBrokenRun = glyphIdx > 0;
218 result->fFinished = false;
219 return true;
220 }
Brian Salomon18923f92017-11-06 16:26:02 -0500221 }
Robert Phillips40a29d72018-01-18 12:59:22 -0500222 auto tokenTracker = fUploadTarget->tokenTracker();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500223 fFullAtlasManager->addGlyphToBulkAndSetUseToken(fSubRun->bulkUseToken(), glyph,
224 tokenTracker->nextDrawToken());
Brian Salomon18923f92017-11-06 16:26:02 -0500225 }
226
Brian Osman5d6be8f2019-01-08 12:02:51 -0500227 if (regenPos) {
228 regen_positions(currVertex, vertexStride, fTransX, fTransY);
229 }
230 if (regenCol) {
231 regen_colors(currVertex, vertexStride, fColor);
232 }
233 if (regenTexCoords) {
234 regen_texcoords(currVertex, vertexStride, glyph, fSubRun->drawAsDistanceFields());
235 }
236
Brian Salomondeb53cc2017-11-08 13:50:53 -0500237 currVertex += vertexStride * GrAtlasTextOp::kVerticesPerGlyph;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500238 ++result->fGlyphsRegenerated;
Brian Salomon18923f92017-11-06 16:26:02 -0500239 ++fCurrGlyph;
240 }
241
242 // We may have changed the color so update it here
243 fSubRun->setColor(fColor);
244 if (regenTexCoords) {
245 if (regenGlyphs) {
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500246 fSubRun->setStrike(std::move(strike));
Brian Salomon18923f92017-11-06 16:26:02 -0500247 }
248 fSubRun->setAtlasGeneration(fBrokenRun
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500249 ? GrDrawOpAtlas::kInvalidAtlasGeneration
250 : fFullAtlasManager->atlasGeneration(fSubRun->maskFormat()));
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500251 } else {
252 // For the non-texCoords case we need to ensure that we update the associated use tokens
253 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
254 fUploadTarget->tokenTracker()->nextDrawToken(),
255 fSubRun->maskFormat());
Brian Salomon18923f92017-11-06 16:26:02 -0500256 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500257 return true;
Brian Salomon18923f92017-11-06 16:26:02 -0500258}
259
Herb Derby6dff60e2018-11-12 15:45:49 -0500260bool GrTextBlob::VertexRegenerator::regenerate(GrTextBlob::VertexRegenerator::Result* result) {
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500261 uint64_t currentAtlasGen = fFullAtlasManager->atlasGeneration(fSubRun->maskFormat());
Brian Salomon18923f92017-11-06 16:26:02 -0500262 // If regenerate() is called multiple times then the atlas gen may have changed. So we check
263 // this each time.
264 if (fSubRun->atlasGeneration() != currentAtlasGen) {
265 fRegenFlags |= kRegenTex;
266 }
267
Brian Osman5d6be8f2019-01-08 12:02:51 -0500268 if (fRegenFlags) {
269 return this->doRegen(result,
270 fRegenFlags & kRegenPos,
271 fRegenFlags & kRegenCol,
272 fRegenFlags & kRegenTex,
273 fRegenFlags & kRegenGlyph);
274 } else {
275 bool hasW = fSubRun->hasWCoord();
276 auto vertexStride = GetVertexStride(fSubRun->maskFormat(), hasW);
277 result->fFinished = true;
278 result->fGlyphsRegenerated = fSubRun->glyphCount() - fCurrGlyph;
279 result->fFirstVertex = fBlob->fVertices + fSubRun->vertexStartIndex() +
280 fCurrGlyph * kVerticesPerGlyph * vertexStride;
281 fCurrGlyph = fSubRun->glyphCount();
Brian Salomon18923f92017-11-06 16:26:02 -0500282
Brian Osman5d6be8f2019-01-08 12:02:51 -0500283 // set use tokens for all of the glyphs in our subrun. This is only valid if we
284 // have a valid atlas generation
285 fFullAtlasManager->setUseTokenBulk(*fSubRun->bulkUseToken(),
286 fUploadTarget->tokenTracker()->nextDrawToken(),
287 fSubRun->maskFormat());
288 return true;
Brian Salomon18923f92017-11-06 16:26:02 -0500289 }
290 SK_ABORT("Should not get here");
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500291 return false;
Brian Salomon18923f92017-11-06 16:26:02 -0500292}