Debian Codegraph @

src/gpu/ganesh/text/GrAtlasManager.cpp

raw
1 /*
2 * Copyright 2018 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
8 #include "src/gpu/ganesh/text/GrAtlasManager.h"
9
10 #include "include/core/SkColorType.h"
11 #include "include/core/SkSize.h"
12 #include "include/core/SkSpan.h"
13 #include "include/private/base/SkMalloc.h"
14 #include "include/private/base/SkTLogic.h"
15 #include "src/base/SkAutoMalloc.h"
16 #include "src/core/SkDistanceFieldGen.h"
17 #include "src/core/SkGlyph.h"
18 #include "src/core/SkMask.h"
19 #include "src/core/SkMasks.h"
20 #include "src/core/SkStrikeSpec.h"
21 #include "src/gpu/ganesh/GrColor.h"
22 #include "src/gpu/ganesh/GrDeferredUpload.h"
23 #include "src/gpu/ganesh/GrMeshDrawTarget.h"
24 #include "src/text/gpu/Glyph.h"
25 #include "src/text/gpu/GlyphVector.h"
26 #include "src/text/gpu/StrikeCache.h"
27
28 #include <cstring>
29 #include <tuple>
30
31 using Glyph = sktext::gpu::Glyph;
32 using MaskFormat = skgpu::MaskFormat;
33
34 GrAtlasManager::GrAtlasManager(GrProxyProvider* proxyProvider,
35 size_t maxTextureBytes,
36 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing,
37 bool supportBilerpAtlas)
38 : fAllowMultitexturing{allowMultitexturing}
39 , fSupportBilerpAtlas{supportBilerpAtlas}
40 , fProxyProvider{proxyProvider}
41 , fCaps{fProxyProvider->refCaps()}
42 , fAtlasConfig{fCaps->maxTextureSize(), maxTextureBytes} { }
43
44 GrAtlasManager::~GrAtlasManager() = default;
45
46 void GrAtlasManager::freeAll() {
47 for (int i = 0; i < skgpu::kMaskFormatCount; ++i) {
48 fAtlases[i] = nullptr;
49 }
50 }
51
52 bool GrAtlasManager::hasGlyph(MaskFormat format, Glyph* glyph) {
53 SkASSERT(glyph);
54 return this->getAtlas(format)->hasID(glyph->fAtlasLocator.plotLocator());
55 }
56
57 template <typename INT_TYPE>
58 static void expand_bits(INT_TYPE* dst,
59 const uint8_t* src,
60 int width,
61 int height,
62 int dstRowBytes,
63 int srcRowBytes) {
64 for (int y = 0; y < height; ++y) {
65 int rowWritesLeft = width;
66 const uint8_t* s = src;
67 INT_TYPE* d = dst;
68 while (rowWritesLeft > 0) {
69 unsigned mask = *s++;
70 for (int x = 7; x >= 0 && rowWritesLeft; --x, --rowWritesLeft) {
71 *d++ = (mask & (1 << x)) ? (INT_TYPE)(~0UL) : 0;
72 }
73 }
74 dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes);
75 src += srcRowBytes;
76 }
77 }
78
79 static void get_packed_glyph_image(
80 const SkGlyph& glyph, int dstRB, MaskFormat expectedMaskFormat, void* dst) {
81 const int width = glyph.width();
82 const int height = glyph.height();
83 const void* src = glyph.image();
84 SkASSERT(src != nullptr);
85
86 MaskFormat maskFormat = Glyph::FormatFromSkGlyph(glyph.maskFormat());
87 if (maskFormat == expectedMaskFormat) {
88 int srcRB = glyph.rowBytes();
89 // Notice this comparison is with the glyphs raw mask format, and not its MaskFormat.
90 if (glyph.maskFormat() != SkMask::kBW_Format) {
91 if (srcRB != dstRB) {
92 const int bbp = MaskFormatBytesPerPixel(expectedMaskFormat);
93 for (int y = 0; y < height; y++) {
94 memcpy(dst, src, width * bbp);
95 src = (const char*) src + srcRB;
96 dst = (char*) dst + dstRB;
97 }
98 } else {
99 memcpy(dst, src, dstRB * height);
100 }
101 } else {
102 // Handle 8-bit format by expanding the mask to the expected format.
103 const uint8_t* bits = reinterpret_cast<const uint8_t*>(src);
104 switch (expectedMaskFormat) {
105 case MaskFormat::kA8: {
106 uint8_t* bytes = reinterpret_cast<uint8_t*>(dst);
107 expand_bits(bytes, bits, width, height, dstRB, srcRB);
108 break;
109 }
110 case MaskFormat::kA565: {
111 uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst);
112 expand_bits(rgb565, bits, width, height, dstRB, srcRB);
113 break;
114 }
115 default:
116 SK_ABORT("Invalid MaskFormat");
117 }
118 }
119 } else if (maskFormat == MaskFormat::kA565 &&
120 expectedMaskFormat == MaskFormat::kARGB) {
121 // Convert if the glyph uses a 565 mask format since it is using LCD text rendering
122 // but the expected format is 8888 (will happen on macOS with Metal since that
123 // combination does not support 565).
124 static constexpr SkMasks masks{
125 {0b1111'1000'0000'0000, 11, 5}, // Red
126 {0b0000'0111'1110'0000, 5, 6}, // Green
127 {0b0000'0000'0001'1111, 0, 5}, // Blue
128 {0, 0, 0} // Alpha
129 };
130 constexpr int a565Bpp = MaskFormatBytesPerPixel(MaskFormat::kA565);
131 constexpr int argbBpp = MaskFormatBytesPerPixel(MaskFormat::kARGB);
132 constexpr bool kBGRAIsNative = kN32_SkColorType == kBGRA_8888_SkColorType;
133 char* dstRow = (char*)dst;
134 for (int y = 0; y < height; y++) {
135 dst = dstRow;
136 for (int x = 0; x < width; x++) {
137 uint16_t color565 = 0;
138 memcpy(&color565, src, a565Bpp);
139 uint32_t color8888;
140 // On Windows (and possibly others), font data is stored as BGR.
141 // So we need to swizzle the data to reflect that.
142 if (kBGRAIsNative) {
143 color8888 = GrColorPackRGBA(masks.getBlue(color565),
144 masks.getGreen(color565),
145 masks.getRed(color565),
146 0xFF);
147 } else {
148 color8888 = GrColorPackRGBA(masks.getRed(color565),
149 masks.getGreen(color565),
150 masks.getBlue(color565),
151 0xFF);
152 }
153 memcpy(dst, &color8888, argbBpp);
154 src = (const char*)src + a565Bpp;
155 dst = (char*)dst + argbBpp;
156 }
157 dstRow += dstRB;
158 }
159 } else {
160 SkUNREACHABLE;
161 }
162 }
163
164 // returns true if glyph successfully added to texture atlas, false otherwise.
165 GrDrawOpAtlas::ErrorCode GrAtlasManager::addGlyphToAtlas(const SkGlyph& skGlyph,
166 Glyph* glyph,
167 int srcPadding,
168 GrResourceProvider* resourceProvider,
169 GrDeferredUploadTarget* uploadTarget) {
170 #if !defined(SK_DISABLE_SDF_TEXT)
171 SkASSERT(0 <= srcPadding && srcPadding <= SK_DistanceFieldInset);
172 #else
173 SkASSERT(0 <= srcPadding);
174 #endif
175
176 if (skGlyph.image() == nullptr) {
177 return GrDrawOpAtlas::ErrorCode::kError;
178 }
179 SkASSERT(glyph != nullptr);
180
181 MaskFormat expectedMaskFormat = this->resolveMaskFormat(glyph->fGlyphEntryKey.fFormat);
182 int bytesPerPixel = MaskFormatBytesPerPixel(expectedMaskFormat);
183
184 int padding;
185 switch (srcPadding) {
186 case 0:
187 // The direct mask/image case.
188 padding = 0;
189 if (fSupportBilerpAtlas) {
190 // Force direct masks (glyph with no padding) to have padding.
191 padding = 1;
192 srcPadding = 1;
193 }
194 break;
195 case 1:
196 // The transformed mask/image case.
197 padding = 1;
198 break;
199 #if !defined(SK_DISABLE_SDF_TEXT)
200 case SK_DistanceFieldInset:
201 // The SDFT case.
202 // If the srcPadding == SK_DistanceFieldInset (SDFT case) then the padding is built
203 // into the image on the glyph; no extra padding needed.
204 // TODO: can the SDFT glyph image in the cache be reduced by the padding?
205 padding = 0;
206 break;
207 #endif
208 default:
209 // The padding is not one of the know forms.
210 return GrDrawOpAtlas::ErrorCode::kError;
211 }
212
213 const int width = skGlyph.width() + 2*padding;
214 const int height = skGlyph.height() + 2*padding;
215 int rowBytes = width * bytesPerPixel;
216 size_t size = height * rowBytes;
217
218 // Temporary storage for normalizing glyph image.
219 SkAutoSMalloc<1024> storage(size);
220 void* dataPtr = storage.get();
221 if (padding > 0) {
222 sk_bzero(dataPtr, size);
223 // Advance in one row and one column.
224 dataPtr = (char*)(dataPtr) + rowBytes + bytesPerPixel;
225 }
226
227 get_packed_glyph_image(skGlyph, rowBytes, expectedMaskFormat, dataPtr);
228
229 auto errorCode = this->addToAtlas(resourceProvider,
230 uploadTarget,
231 expectedMaskFormat,
232 width,
233 height,
234 storage.get(),
235 &glyph->fAtlasLocator);
236
237 if (errorCode == GrDrawOpAtlas::ErrorCode::kSucceeded) {
238 glyph->fAtlasLocator.insetSrc(srcPadding);
239 }
240
241 return errorCode;
242 }
243
244 // add to texture atlas that matches this format
245 GrDrawOpAtlas::ErrorCode GrAtlasManager::addToAtlas(GrResourceProvider* resourceProvider,
246 GrDeferredUploadTarget* target,
247 MaskFormat format,
248 int width, int height, const void* image,
249 skgpu::AtlasLocator* atlasLocator) {
250 return this->getAtlas(format)->addToAtlas(resourceProvider, target, width, height, image,
251 atlasLocator);
252 }
253
254 void GrAtlasManager::addGlyphToBulkAndSetUseToken(skgpu::BulkUsePlotUpdater* updater,
255 MaskFormat format, Glyph* glyph,
256 skgpu::Token token) {
257 SkASSERT(glyph);
258 if (updater->add(glyph->fAtlasLocator)) {
259 this->getAtlas(format)->setLastUseToken(glyph->fAtlasLocator, token);
260 }
261 }
262
263 bool GrAtlasManager::initAtlas(MaskFormat format) {
264 int index = MaskFormatToAtlasIndex(format);
265 if (fAtlases[index] == nullptr) {
266 SkColorType colorType = MaskFormatToColorType(format);
267 GrColorType grColorType = SkColorTypeToGrColorType(colorType);
268 SkISize atlasDimensions = fAtlasConfig.atlasDimensions(format);
269 SkISize plotDimensions = fAtlasConfig.plotDimensions(format);
270
271 const GrBackendFormat backendFormat =
272 fCaps->getDefaultBackendFormat(grColorType, GrRenderable::kNo);
273
274 fAtlases[index] = GrDrawOpAtlas::Make(fProxyProvider, backendFormat,
275 GrColorTypeToSkColorType(grColorType),
276 GrColorTypeBytesPerPixel(grColorType),
277 atlasDimensions.width(), atlasDimensions.height(),
278 plotDimensions.width(), plotDimensions.height(),
279 this,
280 fAllowMultitexturing,
281 nullptr,
282 /*label=*/"TextAtlas");
283 if (!fAtlases[index]) {
284 return false;
285 }
286 }
287 return true;
288 }
289
290 ////////////////////////////////////////////////////////////////////////////////////////////////
291
292 namespace sktext::gpu {
293
294 std::tuple<bool, int> GlyphVector::regenerateAtlasForGanesh(
295 int begin, int end, MaskFormat maskFormat, int srcPadding, GrMeshDrawTarget* target) {
296 GrAtlasManager* atlasManager = target->atlasManager();
297 GrDeferredUploadTarget* uploadTarget = target->deferredUploadTarget();
298
299 uint64_t currentAtlasGen = atlasManager->atlasGeneration(maskFormat);
300
301 this->packedGlyphIDToGlyph(target->strikeCache(), maskFormat);
302
303 if (fAtlasGeneration != currentAtlasGen) {
304 // Calculate the texture coordinates for the vertexes during first use (fAtlasGeneration
305 // is set to kInvalidAtlasGeneration) or the atlas has changed in subsequent calls..
306 fBulkUseUpdater.reset();
307
308 SkBulkGlyphMetricsAndImages metricsAndImages{fTextStrike->strikeSpec()};
309
310 // Update the atlas information in the GrStrike.
311 auto tokenTracker = uploadTarget->tokenTracker();
312 auto glyphs = fGlyphs.subspan(begin, end - begin);
313 int glyphsPlacedInAtlas = 0;
314 bool success = true;
315 for (const Variant& variant : glyphs) {
316 Glyph* gpuGlyph = variant.glyph;
317 SkASSERT(gpuGlyph != nullptr);
318 SkASSERT(gpuGlyph->fGlyphEntryKey.fFormat == maskFormat);
319 if (!atlasManager->hasGlyph(maskFormat, gpuGlyph)) {
320 const SkGlyph& skGlyph =
321 *metricsAndImages.glyph(gpuGlyph->fGlyphEntryKey.fPackedID);
322 auto code = atlasManager->addGlyphToAtlas(
323 skGlyph, gpuGlyph, srcPadding, target->resourceProvider(), uploadTarget);
324 if (code != GrDrawOpAtlas::ErrorCode::kSucceeded) {
325 success = code != GrDrawOpAtlas::ErrorCode::kError;
326 break;
327 }
328 }
329 atlasManager->addGlyphToBulkAndSetUseToken(
330 &fBulkUseUpdater, maskFormat, gpuGlyph,
331 tokenTracker->nextDrawToken());
332 glyphsPlacedInAtlas++;
333 }
334
335 // Update atlas generation if there are no more glyphs to put in the atlas.
336 if (success && begin + glyphsPlacedInAtlas == SkCount(fGlyphs)) {
337 // Need to get the freshest value of the atlas' generation because
338 // updateTextureCoordinates may have changed it.
339 fAtlasGeneration = atlasManager->atlasGeneration(maskFormat);
340 }
341
342 return {success, glyphsPlacedInAtlas};
343 } else {
344 // The atlas hasn't changed, so our texture coordinates are still valid.
345 if (end == SkCount(fGlyphs)) {
346 // The atlas hasn't changed and the texture coordinates are all still valid. Update
347 // all the plots used to the new use token.
348 atlasManager->setUseTokenBulk(fBulkUseUpdater,
349 uploadTarget->tokenTracker()->nextDrawToken(),
350 maskFormat);
351 }
352 return {true, end - begin};
353 }
354 }
355
356 } // namespace sktext::gpu