| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 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/graphite/text/TextAtlasManager.h" |
| 9 |
|
| 10 |
#include "include/core/SkColorType.h" |
| 11 |
#include "include/core/SkImageInfo.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/core/SkSize.h" |
| 14 |
#include "include/gpu/GpuTypes.h" |
| 15 |
#include "include/gpu/graphite/Recorder.h" |
| 16 |
#include "include/gpu/graphite/TextureInfo.h" |
| 17 |
#include "include/private/base/SkDebug.h" |
| 18 |
#include "include/private/base/SkMalloc.h" |
| 19 |
#include "include/private/base/SkSpan_impl.h" |
| 20 |
#include "include/private/base/SkTLogic.h" |
| 21 |
#include "src/base/SkAutoMalloc.h" |
| 22 |
#include "src/core/SkDistanceFieldGen.h" |
| 23 |
#include "src/core/SkGlyph.h" |
| 24 |
#include "src/core/SkMask.h" |
| 25 |
#include "src/core/SkMasks.h" |
| 26 |
#include "src/core/SkStrikeSpec.h" |
| 27 |
#include "src/gpu/graphite/AtlasProvider.h" |
| 28 |
#include "src/gpu/graphite/Caps.h" |
| 29 |
#include "src/gpu/graphite/DrawAtlas.h" |
| 30 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 31 |
#include "src/gpu/graphite/TextureProxy.h" // IWYU pragma: keep |
| 32 |
#include "src/sksl/SkSLUtil.h" |
| 33 |
#include "src/text/gpu/Glyph.h" |
| 34 |
#include "src/text/gpu/GlyphVector.h" |
| 35 |
#include "src/text/gpu/StrikeCache.h" |
| 36 |
|
| 37 |
#include <cstring> |
| 38 |
#include <new> |
| 39 |
#include <tuple> |
| 40 |
|
| 41 |
using Glyph = sktext::gpu::Glyph; |
| 42 |
|
| 43 |
namespace skgpu::graphite { |
| 44 |
|
| 45 |
TextAtlasManager::TextAtlasManager(Recorder* recorder) |
| 46 |
: fRecorder(recorder) |
| 47 |
, fSupportBilerpAtlas{recorder->priv().caps()->supportBilerpFromGlyphAtlas()} |
| 48 |
, fAtlasConfig{recorder->priv().caps()->maxTextureSize(), |
| 49 |
recorder->priv().caps()->glyphCacheTextureMaximumBytes()} { |
| 50 |
if (!recorder->priv().caps()->allowMultipleAtlasTextures() || |
| 51 |
// multitexturing supported only if range can represent the index + texcoords fully |
| 52 |
!(recorder->priv().caps()->shaderCaps()->fFloatIs32Bits || |
| 53 |
recorder->priv().caps()->shaderCaps()->fIntegerSupport)) { |
| 54 |
fAllowMultitexturing = DrawAtlas::AllowMultitexturing::kNo; |
| 55 |
} else { |
| 56 |
fAllowMultitexturing = DrawAtlas::AllowMultitexturing::kYes; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
TextAtlasManager::~TextAtlasManager() = default; |
| 61 |
|
| 62 |
void TextAtlasManager::freeGpuResources() { |
| 63 |
auto tokenTracker = fRecorder->priv().tokenTracker(); |
| 64 |
for (int i = 0; i < kMaskFormatCount; ++i) { |
| 65 |
if (fAtlases[i]) { |
| 66 |
fAtlases[i]->freeGpuResources(tokenTracker->nextFlushToken()); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
bool TextAtlasManager::hasGlyph(MaskFormat format, Glyph* glyph) { |
| 72 |
SkASSERT(glyph); |
| 73 |
return this->getAtlas(format)->hasID(glyph->fAtlasLocator.plotLocator()); |
| 74 |
} |
| 75 |
|
| 76 |
template <typename INT_TYPE> |
| 77 |
static void expand_bits(INT_TYPE* dst, |
| 78 |
const uint8_t* src, |
| 79 |
int width, |
| 80 |
int height, |
| 81 |
int dstRowBytes, |
| 82 |
int srcRowBytes) { |
| 83 |
for (int y = 0; y < height; ++y) { |
| 84 |
int rowWritesLeft = width; |
| 85 |
const uint8_t* s = src; |
| 86 |
INT_TYPE* d = dst; |
| 87 |
while (rowWritesLeft > 0) { |
| 88 |
unsigned mask = *s++; |
| 89 |
for (int x = 7; x >= 0 && rowWritesLeft; --x, --rowWritesLeft) { |
| 90 |
*d++ = (mask & (1 << x)) ? (INT_TYPE)(~0UL) : 0; |
| 91 |
} |
| 92 |
} |
| 93 |
dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes); |
| 94 |
src += srcRowBytes; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
static void get_packed_glyph_image( |
| 99 |
const SkGlyph& glyph, int dstRB, MaskFormat expectedMaskFormat, void* dst) { |
| 100 |
const int width = glyph.width(); |
| 101 |
const int height = glyph.height(); |
| 102 |
const void* src = glyph.image(); |
| 103 |
SkASSERT(src != nullptr); |
| 104 |
|
| 105 |
MaskFormat maskFormat = Glyph::FormatFromSkGlyph(glyph.maskFormat()); |
| 106 |
if (maskFormat == expectedMaskFormat) { |
| 107 |
int srcRB = glyph.rowBytes(); |
| 108 |
// Notice this comparison is with the glyphs raw mask format, and not its MaskFormat. |
| 109 |
if (glyph.maskFormat() != SkMask::kBW_Format) { |
| 110 |
if (srcRB != dstRB) { |
| 111 |
const int bbp = MaskFormatBytesPerPixel(expectedMaskFormat); |
| 112 |
for (int y = 0; y < height; y++) { |
| 113 |
memcpy(dst, src, width * bbp); |
| 114 |
src = (const char*) src + srcRB; |
| 115 |
dst = (char*) dst + dstRB; |
| 116 |
} |
| 117 |
} else { |
| 118 |
memcpy(dst, src, dstRB * height); |
| 119 |
} |
| 120 |
} else { |
| 121 |
// Handle 8-bit format by expanding the mask to the expected format. |
| 122 |
const uint8_t* bits = reinterpret_cast<const uint8_t*>(src); |
| 123 |
switch (expectedMaskFormat) { |
| 124 |
case MaskFormat::kA8: { |
| 125 |
uint8_t* bytes = reinterpret_cast<uint8_t*>(dst); |
| 126 |
expand_bits(bytes, bits, width, height, dstRB, srcRB); |
| 127 |
break; |
| 128 |
} |
| 129 |
case MaskFormat::kA565: { |
| 130 |
uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst); |
| 131 |
expand_bits(rgb565, bits, width, height, dstRB, srcRB); |
| 132 |
break; |
| 133 |
} |
| 134 |
default: |
| 135 |
SK_ABORT("Invalid MaskFormat"); |
| 136 |
} |
| 137 |
} |
| 138 |
} else if (maskFormat == MaskFormat::kA565 && |
| 139 |
expectedMaskFormat == MaskFormat::kARGB) { |
| 140 |
// Convert if the glyph uses a 565 mask format since it is using LCD text rendering |
| 141 |
// but the expected format is 8888 (will happen on Intel MacOS with Metal since that |
| 142 |
// combination does not support 565). |
| 143 |
static constexpr SkMasks masks{ |
| 144 |
{0b1111'1000'0000'0000, 11, 5}, // Red |
| 145 |
{0b0000'0111'1110'0000, 5, 6}, // Green |
| 146 |
{0b0000'0000'0001'1111, 0, 5}, // Blue |
| 147 |
{0, 0, 0} // Alpha |
| 148 |
}; |
| 149 |
constexpr int a565Bpp = MaskFormatBytesPerPixel(MaskFormat::kA565); |
| 150 |
constexpr int argbBpp = MaskFormatBytesPerPixel(MaskFormat::kARGB); |
| 151 |
constexpr bool kBGRAIsNative = kN32_SkColorType == kBGRA_8888_SkColorType; |
| 152 |
char* dstRow = (char*)dst; |
| 153 |
for (int y = 0; y < height; y++) { |
| 154 |
dst = dstRow; |
| 155 |
for (int x = 0; x < width; x++) { |
| 156 |
uint16_t color565 = 0; |
| 157 |
memcpy(&color565, src, a565Bpp); |
| 158 |
uint32_t color8888; |
| 159 |
// On Windows (and possibly others), font data is stored as BGR. |
| 160 |
// So we need to swizzle the data to reflect that. |
| 161 |
if (kBGRAIsNative) { |
| 162 |
color8888 = masks.getBlue(color565) | |
| 163 |
(masks.getGreen(color565) << 8) | |
| 164 |
(masks.getRed(color565) << 16) | |
| 165 |
(0xFF << 24); |
| 166 |
} else { |
| 167 |
color8888 = masks.getRed(color565) | |
| 168 |
(masks.getGreen(color565) << 8) | |
| 169 |
(masks.getBlue(color565) << 16) | |
| 170 |
(0xFF << 24); |
| 171 |
} |
| 172 |
memcpy(dst, &color8888, argbBpp); |
| 173 |
src = (const char*)src + a565Bpp; |
| 174 |
dst = ( char*)dst + argbBpp; |
| 175 |
} |
| 176 |
dstRow += dstRB; |
| 177 |
} |
| 178 |
} else { |
| 179 |
SkUNREACHABLE; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
MaskFormat TextAtlasManager::resolveMaskFormat(MaskFormat format) const { |
| 184 |
if (MaskFormat::kA565 == format && |
| 185 |
!fRecorder->priv().caps()->getDefaultSampledTextureInfo(kRGB_565_SkColorType, |
| 186 |
/*mipmapped=*/Mipmapped::kNo, |
| 187 |
Protected::kNo, |
| 188 |
Renderable::kNo).isValid()) { |
| 189 |
format = MaskFormat::kARGB; |
| 190 |
} |
| 191 |
return format; |
| 192 |
} |
| 193 |
|
| 194 |
// Returns kSucceeded if glyph successfully added to texture atlas, kTryAgain if a RenderPassTask |
| 195 |
// needs to be snapped before adding the glyph, and kError if it can't be added at all. |
| 196 |
DrawAtlas::ErrorCode TextAtlasManager::addGlyphToAtlas(const SkGlyph& skGlyph, |
| 197 |
Glyph* glyph, |
| 198 |
int srcPadding) { |
| 199 |
#if !defined(SK_DISABLE_SDF_TEXT) |
| 200 |
SkASSERT(0 <= srcPadding && srcPadding <= SK_DistanceFieldInset); |
| 201 |
#else |
| 202 |
SkASSERT(0 <= srcPadding); |
| 203 |
#endif |
| 204 |
|
| 205 |
if (skGlyph.image() == nullptr) { |
| 206 |
return DrawAtlas::ErrorCode::kError; |
| 207 |
} |
| 208 |
SkASSERT(glyph != nullptr); |
| 209 |
|
| 210 |
MaskFormat expectedMaskFormat = this->resolveMaskFormat(glyph->fGlyphEntryKey.fFormat); |
| 211 |
int bytesPerPixel = MaskFormatBytesPerPixel(expectedMaskFormat); |
| 212 |
|
| 213 |
int padding; |
| 214 |
switch (srcPadding) { |
| 215 |
case 0: |
| 216 |
// The direct mask/image case. |
| 217 |
padding = 0; |
| 218 |
if (fSupportBilerpAtlas) { |
| 219 |
// Force direct masks (glyph with no padding) to have padding. |
| 220 |
padding = 1; |
| 221 |
srcPadding = 1; |
| 222 |
} |
| 223 |
break; |
| 224 |
case 1: |
| 225 |
// The transformed mask/image case. |
| 226 |
padding = 1; |
| 227 |
break; |
| 228 |
#if !defined(SK_DISABLE_SDF_TEXT) |
| 229 |
case SK_DistanceFieldInset: |
| 230 |
// The SDFT case. |
| 231 |
// If the srcPadding == SK_DistanceFieldInset (SDFT case) then the padding is built |
| 232 |
// into the image on the glyph; no extra padding needed. |
| 233 |
// TODO: can the SDFT glyph image in the cache be reduced by the padding? |
| 234 |
padding = 0; |
| 235 |
break; |
| 236 |
#endif |
| 237 |
default: |
| 238 |
// The padding is not one of the know forms. |
| 239 |
return DrawAtlas::ErrorCode::kError; |
| 240 |
} |
| 241 |
|
| 242 |
const int width = skGlyph.width() + 2*padding; |
| 243 |
const int height = skGlyph.height() + 2*padding; |
| 244 |
int rowBytes = width * bytesPerPixel; |
| 245 |
size_t size = height * rowBytes; |
| 246 |
|
| 247 |
// Temporary storage for normalizing glyph image. |
| 248 |
SkAutoSMalloc<1024> storage(size); |
| 249 |
void* dataPtr = storage.get(); |
| 250 |
if (padding > 0) { |
| 251 |
sk_bzero(dataPtr, size); |
| 252 |
// Advance in one row and one column. |
| 253 |
dataPtr = (char*)(dataPtr) + rowBytes + bytesPerPixel; |
| 254 |
} |
| 255 |
|
| 256 |
get_packed_glyph_image(skGlyph, rowBytes, expectedMaskFormat, dataPtr); |
| 257 |
|
| 258 |
DrawAtlas* atlas = this->getAtlas(expectedMaskFormat); |
| 259 |
auto errorCode = atlas->addToAtlas(fRecorder, |
| 260 |
width, |
| 261 |
height, |
| 262 |
storage.get(), |
| 263 |
&glyph->fAtlasLocator); |
| 264 |
|
| 265 |
if (errorCode == DrawAtlas::ErrorCode::kSucceeded) { |
| 266 |
glyph->fAtlasLocator.insetSrc(srcPadding); |
| 267 |
} |
| 268 |
|
| 269 |
return errorCode; |
| 270 |
} |
| 271 |
|
| 272 |
bool TextAtlasManager::recordUploads(DrawContext* dc) { |
| 273 |
for (int i = 0; i < skgpu::kMaskFormatCount; i++) { |
| 274 |
if (fAtlases[i] && !fAtlases[i]->recordUploads(dc, fRecorder)) { |
| 275 |
return false; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return true; |
| 280 |
} |
| 281 |
|
| 282 |
void TextAtlasManager::addGlyphToBulkAndSetUseToken(BulkUsePlotUpdater* updater, |
| 283 |
MaskFormat format, |
| 284 |
Glyph* glyph, |
| 285 |
Token token) { |
| 286 |
SkASSERT(glyph); |
| 287 |
if (updater->add(glyph->fAtlasLocator)) { |
| 288 |
this->getAtlas(format)->setLastUseToken(glyph->fAtlasLocator, token); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
void TextAtlasManager::setAtlasDimensionsToMinimum_ForTesting() { |
| 293 |
// Delete any old atlases. |
| 294 |
// This should be safe to do as long as we are not in the middle of a flush. |
| 295 |
for (int i = 0; i < skgpu::kMaskFormatCount; i++) { |
| 296 |
fAtlases[i] = nullptr; |
| 297 |
} |
| 298 |
|
| 299 |
// Set all the atlas sizes to 1x1 plot each. |
| 300 |
new (&fAtlasConfig) DrawAtlasConfig{2048, 0}; |
| 301 |
} |
| 302 |
|
| 303 |
bool TextAtlasManager::initAtlas(MaskFormat format) { |
| 304 |
int index = MaskFormatToAtlasIndex(format); |
| 305 |
if (fAtlases[index] == nullptr) { |
| 306 |
SkColorType colorType = MaskFormatToColorType(format); |
| 307 |
SkISize atlasDimensions = fAtlasConfig.atlasDimensions(format); |
| 308 |
SkISize plotDimensions = fAtlasConfig.plotDimensions(format); |
| 309 |
fAtlases[index] = DrawAtlas::Make(colorType, |
| 310 |
SkColorTypeBytesPerPixel(colorType), |
| 311 |
atlasDimensions.width(), atlasDimensions.height(), |
| 312 |
plotDimensions.width(), plotDimensions.height(), |
| 313 |
/*generationCounter=*/this, |
| 314 |
fAllowMultitexturing, |
| 315 |
DrawAtlas::UseStorageTextures::kNo, |
| 316 |
/*evictor=*/nullptr, |
| 317 |
/*label=*/"TextAtlas"); |
| 318 |
if (!fAtlases[index]) { |
| 319 |
return false; |
| 320 |
} |
| 321 |
} |
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
void TextAtlasManager::compact() { |
| 326 |
auto tokenTracker = fRecorder->priv().tokenTracker(); |
| 327 |
for (int i = 0; i < kMaskFormatCount; ++i) { |
| 328 |
if (fAtlases[i]) { |
| 329 |
fAtlases[i]->compact(tokenTracker->nextFlushToken()); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
} // namespace skgpu::graphite |
| 335 |
|
| 336 |
//////////////////////////////////////////////////////////////////////////////////////////////// |
| 337 |
|
| 338 |
namespace sktext::gpu { |
| 339 |
|
| 340 |
using DrawAtlas = skgpu::graphite::DrawAtlas; |
| 341 |
|
| 342 |
std::tuple<bool, int> GlyphVector::regenerateAtlasForGraphite(int begin, |
| 343 |
int end, |
| 344 |
skgpu::MaskFormat maskFormat, |
| 345 |
int srcPadding, |
| 346 |
skgpu::graphite::Recorder* recorder) { |
| 347 |
auto atlasManager = recorder->priv().atlasProvider()->textAtlasManager(); |
| 348 |
auto tokenTracker = recorder->priv().tokenTracker(); |
| 349 |
|
| 350 |
// TODO: this is not a great place for this -- need a better way to init atlases when needed |
| 351 |
unsigned int numActiveProxies; |
| 352 |
const sk_sp<skgpu::graphite::TextureProxy>* proxies = |
| 353 |
atlasManager->getProxies(maskFormat, &numActiveProxies); |
| 354 |
if (!proxies) { |
| 355 |
SkDebugf("Could not allocate backing texture for atlas\n"); |
| 356 |
return {false, 0}; |
| 357 |
} |
| 358 |
|
| 359 |
uint64_t currentAtlasGen = atlasManager->atlasGeneration(maskFormat); |
| 360 |
|
| 361 |
this->packedGlyphIDToGlyph(recorder->priv().strikeCache(), maskFormat); |
| 362 |
|
| 363 |
if (fAtlasGeneration != currentAtlasGen) { |
| 364 |
// Calculate the texture coordinates for the vertexes during first use (fAtlasGeneration |
| 365 |
// is set to kInvalidAtlasGeneration) or the atlas has changed in subsequent calls.. |
| 366 |
fBulkUseUpdater.reset(); |
| 367 |
|
| 368 |
SkBulkGlyphMetricsAndImages metricsAndImages{fTextStrike->strikeSpec()}; |
| 369 |
|
| 370 |
// Update the atlas information in the GrStrike. |
| 371 |
auto glyphs = fGlyphs.subspan(begin, end - begin); |
| 372 |
int glyphsPlacedInAtlas = 0; |
| 373 |
bool success = true; |
| 374 |
for (const Variant& variant : glyphs) { |
| 375 |
Glyph* gpuGlyph = variant.glyph; |
| 376 |
SkASSERT(gpuGlyph != nullptr); |
| 377 |
SkASSERT(gpuGlyph->fGlyphEntryKey.fFormat == maskFormat); |
| 378 |
if (!atlasManager->hasGlyph(maskFormat, gpuGlyph)) { |
| 379 |
const SkGlyph& skGlyph = |
| 380 |
*metricsAndImages.glyph(gpuGlyph->fGlyphEntryKey.fPackedID); |
| 381 |
auto code = atlasManager->addGlyphToAtlas(skGlyph, gpuGlyph, srcPadding); |
| 382 |
if (code != DrawAtlas::ErrorCode::kSucceeded) { |
| 383 |
success = code != DrawAtlas::ErrorCode::kError; |
| 384 |
break; |
| 385 |
} |
| 386 |
} |
| 387 |
atlasManager->addGlyphToBulkAndSetUseToken( |
| 388 |
&fBulkUseUpdater, maskFormat, gpuGlyph, |
| 389 |
tokenTracker->nextFlushToken()); |
| 390 |
glyphsPlacedInAtlas++; |
| 391 |
} |
| 392 |
|
| 393 |
// Update atlas generation if there are no more glyphs to put in the atlas. |
| 394 |
if (success && begin + glyphsPlacedInAtlas == SkCount(fGlyphs)) { |
| 395 |
// Need to get the freshest value of the atlas' generation because |
| 396 |
// updateTextureCoordinates may have changed it. |
| 397 |
fAtlasGeneration = atlasManager->atlasGeneration(maskFormat); |
| 398 |
} |
| 399 |
|
| 400 |
return {success, glyphsPlacedInAtlas}; |
| 401 |
} else { |
| 402 |
// The atlas hasn't changed, so our texture coordinates are still valid. |
| 403 |
if (end == SkCount(fGlyphs)) { |
| 404 |
// The atlas hasn't changed and the texture coordinates are all still valid. Update |
| 405 |
// all the plots used to the new use token. |
| 406 |
atlasManager->setUseTokenBulk(fBulkUseUpdater, |
| 407 |
tokenTracker->nextFlushToken(), |
| 408 |
maskFormat); |
| 409 |
} |
| 410 |
return {true, end - begin}; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
} // namespace sktext::gpu |