| 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 |
#include "src/gpu/graphite/DrawAtlas.h" |
| 8 |
|
| 9 |
#include "include/core/SkAlphaType.h" |
| 10 |
#include "include/core/SkColorSpace.h" |
| 11 |
#include "include/core/SkImageInfo.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkTypes.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/SkMath.h" |
| 18 |
#include "include/private/base/SkTArray.h" |
| 19 |
#include "include/private/base/SkTPin.h" |
| 20 |
#include "src/base/SkMathPriv.h" |
| 21 |
#include "src/core/SkTraceEvent.h" |
| 22 |
#include "src/gpu/AtlasTypes.h" |
| 23 |
#include "src/gpu/graphite/Caps.h" |
| 24 |
#include "src/gpu/graphite/DrawContext.h" |
| 25 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 26 |
#include "src/gpu/graphite/TextureProxy.h" |
| 27 |
#include "src/gpu/graphite/task/UploadTask.h" |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <atomic> |
| 31 |
#include <memory> |
| 32 |
#include <tuple> |
| 33 |
|
| 34 |
enum SkColorType : int; |
| 35 |
|
| 36 |
using namespace skia_private; |
| 37 |
|
| 38 |
namespace skgpu::graphite { |
| 39 |
|
| 40 |
#if defined(DUMP_ATLAS_DATA) |
| 41 |
static const constexpr bool kDumpAtlasData = true; |
| 42 |
#else |
| 43 |
static const constexpr bool kDumpAtlasData = false; |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef SK_DEBUG |
| 47 |
void DrawAtlas::validate(const AtlasLocator& atlasLocator) const { |
| 48 |
// Verify that the plotIndex stored in the PlotLocator is consistent with the glyph rectangle |
| 49 |
int numPlotsX = fTextureWidth / fPlotWidth; |
| 50 |
int numPlotsY = fTextureHeight / fPlotHeight; |
| 51 |
|
| 52 |
int plotIndex = atlasLocator.plotIndex(); |
| 53 |
auto topLeft = atlasLocator.topLeft(); |
| 54 |
int plotX = topLeft.x() / fPlotWidth; |
| 55 |
int plotY = topLeft.y() / fPlotHeight; |
| 56 |
SkASSERT(plotIndex == (numPlotsY - plotY - 1) * numPlotsX + (numPlotsX - plotX - 1)); |
| 57 |
} |
| 58 |
#endif |
| 59 |
|
| 60 |
std::unique_ptr<DrawAtlas> DrawAtlas::Make(SkColorType colorType, size_t bpp, int width, |
| 61 |
int height, int plotWidth, int plotHeight, |
| 62 |
AtlasGenerationCounter* generationCounter, |
| 63 |
AllowMultitexturing allowMultitexturing, |
| 64 |
UseStorageTextures useStorageTextures, |
| 65 |
PlotEvictionCallback* evictor, |
| 66 |
std::string_view label) { |
| 67 |
std::unique_ptr<DrawAtlas> atlas(new DrawAtlas(colorType, bpp, width, height, |
| 68 |
plotWidth, plotHeight, generationCounter, |
| 69 |
allowMultitexturing, useStorageTextures, label)); |
| 70 |
|
| 71 |
if (evictor != nullptr) { |
| 72 |
atlas->fEvictionCallbacks.emplace_back(evictor); |
| 73 |
} |
| 74 |
return atlas; |
| 75 |
} |
| 76 |
|
| 77 |
/////////////////////////////////////////////////////////////////////////////// |
| 78 |
static uint32_t next_id() { |
| 79 |
static std::atomic<uint32_t> nextID{1}; |
| 80 |
uint32_t id; |
| 81 |
do { |
| 82 |
id = nextID.fetch_add(1, std::memory_order_relaxed); |
| 83 |
} while (id == SK_InvalidGenID); |
| 84 |
return id; |
| 85 |
} |
| 86 |
DrawAtlas::DrawAtlas(SkColorType colorType, size_t bpp, int width, int height, |
| 87 |
int plotWidth, int plotHeight, AtlasGenerationCounter* generationCounter, |
| 88 |
AllowMultitexturing allowMultitexturing, |
| 89 |
UseStorageTextures useStorageTextures, |
| 90 |
std::string_view label) |
| 91 |
: fColorType(colorType) |
| 92 |
, fBytesPerPixel(bpp) |
| 93 |
, fTextureWidth(width) |
| 94 |
, fTextureHeight(height) |
| 95 |
, fPlotWidth(plotWidth) |
| 96 |
, fPlotHeight(plotHeight) |
| 97 |
, fUseStorageTextures(useStorageTextures) |
| 98 |
, fLabel(label) |
| 99 |
, fAtlasID(next_id()) |
| 100 |
, fGenerationCounter(generationCounter) |
| 101 |
, fAtlasGeneration(fGenerationCounter->next()) |
| 102 |
, fPrevFlushToken(Token::InvalidToken()) |
| 103 |
, fFlushesSinceLastUse(0) |
| 104 |
, fMaxPages(allowMultitexturing == AllowMultitexturing::kYes ? |
| 105 |
PlotLocator::kMaxMultitexturePages : 1) |
| 106 |
, fNumActivePages(0) { |
| 107 |
int numPlotsX = width/plotWidth; |
| 108 |
int numPlotsY = height/plotHeight; |
| 109 |
SkASSERT(numPlotsX * numPlotsY <= PlotLocator::kMaxPlots); |
| 110 |
SkASSERTF(fPlotWidth * numPlotsX == fTextureWidth, |
| 111 |
"Invalid DrawAtlas. Plot width: %d, texture width %d", fPlotWidth, fTextureWidth); |
| 112 |
SkASSERTF(fPlotHeight * numPlotsY == fTextureHeight, |
| 113 |
"Invalid DrawAtlas. Plot height: %d, texture height %d", fPlotHeight, fTextureHeight); |
| 114 |
|
| 115 |
fNumPlots = numPlotsX * numPlotsY; |
| 116 |
|
| 117 |
this->createPages(generationCounter); |
| 118 |
} |
| 119 |
|
| 120 |
inline void DrawAtlas::processEvictionAndResetRects(Plot* plot, bool freeData) { |
| 121 |
// Process evictions |
| 122 |
if (!plot->isEmpty()) { |
| 123 |
const PlotLocator& plotLocator = plot->plotLocator(); |
| 124 |
for (PlotEvictionCallback* evictor : fEvictionCallbacks) { |
| 125 |
evictor->evict(plotLocator); |
| 126 |
} |
| 127 |
fAtlasGeneration = fGenerationCounter->next(); |
| 128 |
} |
| 129 |
|
| 130 |
plot->resetRects(freeData); |
| 131 |
} |
| 132 |
|
| 133 |
inline void DrawAtlas::updatePlot(Plot* plot, AtlasLocator* atlasLocator) { |
| 134 |
int pageIdx = plot->pageIndex(); |
| 135 |
this->makeMRU(plot, pageIdx); |
| 136 |
|
| 137 |
// The actual upload will be created in recordUploads(). |
| 138 |
|
| 139 |
atlasLocator->updatePlotLocator(plot->plotLocator()); |
| 140 |
SkDEBUGCODE(this->validate(*atlasLocator);) |
| 141 |
} |
| 142 |
|
| 143 |
bool DrawAtlas::addRectToPage(unsigned int pageIdx, int width, int height, |
| 144 |
AtlasLocator* atlasLocator) { |
| 145 |
SkASSERT(fProxies[pageIdx]); |
| 146 |
|
| 147 |
// look through all allocated plots for one we can share, in Most Recently Refed order |
| 148 |
PlotList::Iter plotIter; |
| 149 |
plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart); |
| 150 |
|
| 151 |
for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) { |
| 152 |
if (plot->addRect(width, height, atlasLocator)) { |
| 153 |
this->updatePlot(plot, atlasLocator); |
| 154 |
return true; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
bool DrawAtlas::recordUploads(DrawContext* dc, Recorder* recorder) { |
| 162 |
TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 163 |
for (uint32_t pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) { |
| 164 |
PlotList::Iter plotIter; |
| 165 |
plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart); |
| 166 |
for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) { |
| 167 |
if (plot->needsUpload()) { |
| 168 |
TextureProxy* proxy = fProxies[pageIdx].get(); |
| 169 |
SkASSERT(proxy); |
| 170 |
|
| 171 |
const void* dataPtr; |
| 172 |
SkIRect dstRect; |
| 173 |
std::tie(dataPtr, dstRect) = plot->prepareForUpload(); |
| 174 |
if (dstRect.isEmpty()) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
std::vector<MipLevel> levels; |
| 179 |
levels.push_back({dataPtr, fBytesPerPixel*fPlotWidth}); |
| 180 |
|
| 181 |
// Src and dst colorInfo are the same |
| 182 |
SkColorInfo colorInfo(fColorType, kUnknown_SkAlphaType, nullptr); |
| 183 |
const UploadSource uploadSource = UploadSource::Make( |
| 184 |
recorder->priv().caps(), *proxy, colorInfo, colorInfo, levels, dstRect); |
| 185 |
if (!uploadSource.isValid()) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
if (!dc->recordUpload(recorder, |
| 189 |
sk_ref_sp(proxy), |
| 190 |
colorInfo, |
| 191 |
colorInfo, |
| 192 |
uploadSource, |
| 193 |
dstRect, |
| 194 |
/*ConditionalUploadContext=*/nullptr)) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
// Number of atlas-related flushes beyond which we consider a plot to no longer be in use. |
| 204 |
// |
| 205 |
// This value is somewhat arbitrary -- the idea is to keep it low enough that |
| 206 |
// a page with unused plots will get removed reasonably quickly, but allow it |
| 207 |
// to hang around for a bit in case it's needed. The assumption is that flushes |
| 208 |
// are rare; i.e., we are not continually refreshing the frame. |
| 209 |
static constexpr auto kPlotRecentlyUsedCount = 32; |
| 210 |
// Number of flushes before we'll try to evict a plot from a sparsely used page. |
| 211 |
static constexpr auto kPlotUsedCountBeforeEvict = 8; |
| 212 |
// Number of flushes beyond which we'll consider the atlas no longer in use. |
| 213 |
static constexpr auto kAtlasRecentlyUsedCount = 128; |
| 214 |
|
| 215 |
DrawAtlas::ErrorCode DrawAtlas::addRect(Recorder* recorder, |
| 216 |
int width, int height, |
| 217 |
AtlasLocator* atlasLocator) { |
| 218 |
if (width > fPlotWidth || height > fPlotHeight || width < 0 || height < 0) { |
| 219 |
return ErrorCode::kError; |
| 220 |
} |
| 221 |
|
| 222 |
// We permit zero-sized rects to allow inverse fills in the PathAtlases to work, |
| 223 |
// but we don't want to enter them in the Rectanizer. So we handle this special case here. |
| 224 |
// For text this should be caught at a higher level, but if not the only end result |
| 225 |
// will be rendering a degenerate quad. |
| 226 |
if (width == 0 || height == 0) { |
| 227 |
if (fNumActivePages == 0) { |
| 228 |
// Make sure we have a Page for the AtlasLocator to refer to |
| 229 |
this->activateNewPage(recorder); |
| 230 |
} |
| 231 |
atlasLocator->updateRect(skgpu::IRect16::MakeXYWH(0, 0, 0, 0)); |
| 232 |
// Use the MRU Plot from the first Page |
| 233 |
atlasLocator->updatePlotLocator(fPages[0].fPlotList.head()->plotLocator()); |
| 234 |
return ErrorCode::kSucceeded; |
| 235 |
} |
| 236 |
|
| 237 |
// Look through each page to see if we can upload without having to flush |
| 238 |
// We prioritize this upload to the first pages, not the most recently used, to make it easier |
| 239 |
// to remove unused pages in reverse page order. |
| 240 |
for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) { |
| 241 |
if (this->addRectToPage(pageIdx, width, height, atlasLocator)) { |
| 242 |
return ErrorCode::kSucceeded; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
// If the above fails, then see if the least recently used plot per page has already been |
| 247 |
// queued for upload if we're at max page allocation, or if the plot has aged out otherwise. |
| 248 |
// We wait until we've grown to the full number of pages to begin evicting already queued |
| 249 |
// plots so that we can maximize the opportunity for reuse. |
| 250 |
// As before we prioritize this upload to the first pages, not the most recently used. |
| 251 |
if (fNumActivePages == this->maxPages()) { |
| 252 |
for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) { |
| 253 |
Plot* plot = fPages[pageIdx].fPlotList.tail(); |
| 254 |
SkASSERT(plot); |
| 255 |
if (plot->lastUseToken() < recorder->priv().tokenTracker()->nextFlushToken()) { |
| 256 |
this->processEvictionAndResetRects(plot, /*freeData=*/false); |
| 257 |
SkDEBUGCODE(bool verify = )plot->addRect(width, height, atlasLocator); |
| 258 |
SkASSERT(verify); |
| 259 |
this->updatePlot(plot, atlasLocator); |
| 260 |
return ErrorCode::kSucceeded; |
| 261 |
} |
| 262 |
} |
| 263 |
} else { |
| 264 |
// If we haven't activated all the available pages, try to create a new one and add to it |
| 265 |
if (!this->activateNewPage(recorder)) { |
| 266 |
return ErrorCode::kError; |
| 267 |
} |
| 268 |
|
| 269 |
if (this->addRectToPage(fNumActivePages-1, width, height, atlasLocator)) { |
| 270 |
return ErrorCode::kSucceeded; |
| 271 |
} else { |
| 272 |
// If we fail to upload to a newly activated page then something has gone terribly |
| 273 |
// wrong - return an error |
| 274 |
return ErrorCode::kError; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (!fNumActivePages) { |
| 279 |
return ErrorCode::kError; |
| 280 |
} |
| 281 |
|
| 282 |
// All plots are currently in use by the current set of draws, so we need to fail. This |
| 283 |
// gives the Device a chance to snap the current set of uploads and draws, advance the draw |
| 284 |
// token, and call back into this function. The subsequent call will have plots available |
| 285 |
// for fresh uploads. |
| 286 |
return ErrorCode::kTryAgain; |
| 287 |
} |
| 288 |
|
| 289 |
DrawAtlas::ErrorCode DrawAtlas::addToAtlas(Recorder* recorder, |
| 290 |
int width, int height, const void* image, |
| 291 |
AtlasLocator* atlasLocator) { |
| 292 |
ErrorCode ec = this->addRect(recorder, width, height, atlasLocator); |
| 293 |
if (ec == ErrorCode::kSucceeded) { |
| 294 |
Plot* plot = this->findPlot(*atlasLocator); |
| 295 |
plot->copySubImage(*atlasLocator, image); |
| 296 |
} |
| 297 |
|
| 298 |
return ec; |
| 299 |
} |
| 300 |
|
| 301 |
SkPixmap DrawAtlas::prepForRender(const AtlasLocator& locator, |
| 302 |
int padding, |
| 303 |
std::optional<SkColor> initialColor) { |
| 304 |
Plot* plot = this->findPlot(locator); |
| 305 |
return plot->prepForRender(locator, padding, initialColor); |
| 306 |
} |
| 307 |
|
| 308 |
void DrawAtlas::compact(Token startTokenForNextFlush) { |
| 309 |
if (fNumActivePages < 1) { |
| 310 |
fPrevFlushToken = startTokenForNextFlush; |
| 311 |
return; |
| 312 |
} |
| 313 |
|
| 314 |
// For all plots, reset number of flushes since used if used this frame. |
| 315 |
PlotList::Iter plotIter; |
| 316 |
bool atlasUsedThisFlush = false; |
| 317 |
for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) { |
| 318 |
plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart); |
| 319 |
while (Plot* plot = plotIter.get()) { |
| 320 |
// Reset number of flushes since used |
| 321 |
if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) { |
| 322 |
plot->resetFlushesSinceLastUsed(); |
| 323 |
atlasUsedThisFlush = true; |
| 324 |
} |
| 325 |
|
| 326 |
plotIter.next(); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (atlasUsedThisFlush) { |
| 331 |
fFlushesSinceLastUse = 0; |
| 332 |
} else { |
| 333 |
++fFlushesSinceLastUse; |
| 334 |
} |
| 335 |
|
| 336 |
// We only try to compact if the atlas was used in the recently completed flush or |
| 337 |
// hasn't been used in a long time. |
| 338 |
// This is to handle the case where a lot of text or path rendering has occurred but then just |
| 339 |
// a blinking cursor is drawn. |
| 340 |
if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) { |
| 341 |
TArray<Plot*> availablePlots; |
| 342 |
uint32_t lastPageIndex = fNumActivePages - 1; |
| 343 |
|
| 344 |
// For all pages but the last one, update number of flushes since used, and check to see |
| 345 |
// if there are any in the first pages that the last page can safely upload to. |
| 346 |
for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) { |
| 347 |
if constexpr (kDumpAtlasData) { |
| 348 |
SkDebugf("page %u: ", pageIndex); |
| 349 |
} |
| 350 |
|
| 351 |
plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart); |
| 352 |
while (Plot* plot = plotIter.get()) { |
| 353 |
// Update number of flushes since plot was last used |
| 354 |
// We only increment the 'sinceLastUsed' count for flushes where the atlas was used |
| 355 |
// to avoid deleting everything when we return to text drawing in the blinking |
| 356 |
// cursor case |
| 357 |
if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) { |
| 358 |
plot->incFlushesSinceLastUsed(); |
| 359 |
} |
| 360 |
|
| 361 |
if constexpr (kDumpAtlasData) { |
| 362 |
SkDebugf("%d ", plot->flushesSinceLastUsed()); |
| 363 |
} |
| 364 |
|
| 365 |
// Count plots we can potentially upload to in all pages except the last one |
| 366 |
// (the potential compactee). |
| 367 |
if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) { |
| 368 |
availablePlots.push_back() = plot; |
| 369 |
} |
| 370 |
|
| 371 |
plotIter.next(); |
| 372 |
} |
| 373 |
|
| 374 |
if constexpr (kDumpAtlasData) { |
| 375 |
SkDebugf("\n"); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
// Count recently used plots in the last page and evict any that are no longer in use. |
| 380 |
// Since we prioritize uploading to the first pages, this will eventually |
| 381 |
// clear out usage of this page unless we have a large need. |
| 382 |
plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart); |
| 383 |
unsigned int usedPlots = 0; |
| 384 |
if constexpr (kDumpAtlasData) { |
| 385 |
SkDebugf("page %u: ", lastPageIndex); |
| 386 |
} |
| 387 |
while (Plot* plot = plotIter.get()) { |
| 388 |
// Update number of flushes since plot was last used |
| 389 |
if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) { |
| 390 |
plot->incFlushesSinceLastUsed(); |
| 391 |
} |
| 392 |
|
| 393 |
if constexpr (kDumpAtlasData) { |
| 394 |
SkDebugf("%d ", plot->flushesSinceLastUsed()); |
| 395 |
} |
| 396 |
|
| 397 |
// If this plot was used recently |
| 398 |
if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) { |
| 399 |
usedPlots++; |
| 400 |
} else if (plot->lastUseToken() != Token::InvalidToken()) { |
| 401 |
// otherwise if aged out just evict it. |
| 402 |
this->processEvictionAndResetRects(plot, /*freeData=*/false); |
| 403 |
} |
| 404 |
plotIter.next(); |
| 405 |
} |
| 406 |
|
| 407 |
if constexpr (kDumpAtlasData) { |
| 408 |
SkDebugf("\n"); |
| 409 |
} |
| 410 |
|
| 411 |
// If recently used plots in the last page are using less than a quarter of the page, try |
| 412 |
// to evict them if there's available space in lower index pages. Since we prioritize |
| 413 |
// uploading to the first pages, this will eventually clear out usage of this page unless |
| 414 |
// we have a large need. |
| 415 |
if (!availablePlots.empty() && usedPlots && usedPlots <= fNumPlots / 4) { |
| 416 |
plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart); |
| 417 |
while (Plot* plot = plotIter.get()) { |
| 418 |
// If this plot was used recently |
| 419 |
int plotFlushes = plot->flushesSinceLastUsed(); |
| 420 |
if (kPlotUsedCountBeforeEvict <= plotFlushes && |
| 421 |
plotFlushes <= kPlotRecentlyUsedCount) { |
| 422 |
// See if there's room in an lower index page and if so evict. |
| 423 |
// We need to be somewhat harsh here so that a handful of plots that are |
| 424 |
// consistently in use don't end up locking the page in memory. |
| 425 |
if (!availablePlots.empty()) { |
| 426 |
this->processEvictionAndResetRects(plot, /*freeData=*/true); |
| 427 |
this->processEvictionAndResetRects(availablePlots.back(), |
| 428 |
/*freeData=*/false); |
| 429 |
availablePlots.pop_back(); |
| 430 |
--usedPlots; |
| 431 |
} |
| 432 |
if (usedPlots == 0 || availablePlots.empty()) { |
| 433 |
break; |
| 434 |
} |
| 435 |
} |
| 436 |
plotIter.next(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
// If none of the plots in the last page have been used recently, delete it. |
| 441 |
if (usedPlots == 0) { |
| 442 |
if constexpr (kDumpAtlasData) { |
| 443 |
SkDebugf("delete %u\n", fNumActivePages-1); |
| 444 |
} |
| 445 |
|
| 446 |
this->deactivateLastPage(); |
| 447 |
fFlushesSinceLastUse = 0; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
fPrevFlushToken = startTokenForNextFlush; |
| 452 |
} |
| 453 |
|
| 454 |
bool DrawAtlas::createPages(AtlasGenerationCounter* generationCounter) { |
| 455 |
SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight)); |
| 456 |
|
| 457 |
int numPlotsX = fTextureWidth/fPlotWidth; |
| 458 |
int numPlotsY = fTextureHeight/fPlotHeight; |
| 459 |
|
| 460 |
for (uint32_t i = 0; i < this->maxPages(); ++i) { |
| 461 |
// Proxies are uncreated at first |
| 462 |
fProxies[i] = nullptr; |
| 463 |
|
| 464 |
// set up allocated plots |
| 465 |
fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY); |
| 466 |
|
| 467 |
sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get(); |
| 468 |
for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) { |
| 469 |
for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) { |
| 470 |
uint32_t plotIndex = r * numPlotsX + c; |
| 471 |
currPlot->reset(new Plot( |
| 472 |
i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType, |
| 473 |
fBytesPerPixel)); |
| 474 |
|
| 475 |
// build LRU list |
| 476 |
fPages[i].fPlotList.addToHead(currPlot->get()); |
| 477 |
++currPlot; |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
return true; |
| 483 |
} |
| 484 |
|
| 485 |
bool DrawAtlas::activateNewPage(Recorder* recorder) { |
| 486 |
SkASSERT(fNumActivePages < this->maxPages()); |
| 487 |
SkASSERT(!fProxies[fNumActivePages]); |
| 488 |
|
| 489 |
const Caps* caps = recorder->priv().caps(); |
| 490 |
auto textureInfo = fUseStorageTextures == UseStorageTextures::kYes |
| 491 |
? caps->getDefaultStorageTextureInfo(fColorType) |
| 492 |
: caps->getDefaultSampledTextureInfo(fColorType, |
| 493 |
Mipmapped::kNo, |
| 494 |
recorder->priv().isProtected(), |
| 495 |
Renderable::kNo); |
| 496 |
fProxies[fNumActivePages] = TextureProxy::Make(caps, |
| 497 |
recorder->priv().resourceProvider(), |
| 498 |
{fTextureWidth, fTextureHeight}, |
| 499 |
textureInfo, |
| 500 |
fLabel, |
| 501 |
skgpu::Budgeted::kYes); |
| 502 |
if (!fProxies[fNumActivePages]) { |
| 503 |
return false; |
| 504 |
} |
| 505 |
|
| 506 |
if constexpr (kDumpAtlasData) { |
| 507 |
SkDebugf("activated page#: %u\n", fNumActivePages); |
| 508 |
} |
| 509 |
|
| 510 |
++fNumActivePages; |
| 511 |
return true; |
| 512 |
} |
| 513 |
|
| 514 |
inline void DrawAtlas::deactivateLastPage() { |
| 515 |
SkASSERT(fNumActivePages); |
| 516 |
|
| 517 |
uint32_t lastPageIndex = fNumActivePages - 1; |
| 518 |
|
| 519 |
int numPlotsX = fTextureWidth/fPlotWidth; |
| 520 |
int numPlotsY = fTextureHeight/fPlotHeight; |
| 521 |
|
| 522 |
fPages[lastPageIndex].fPlotList.reset(); |
| 523 |
for (int r = 0; r < numPlotsY; ++r) { |
| 524 |
for (int c = 0; c < numPlotsX; ++c) { |
| 525 |
uint32_t plotIndex = r * numPlotsX + c; |
| 526 |
|
| 527 |
Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get(); |
| 528 |
this->processEvictionAndResetRects(currPlot, /*freeData=*/true); |
| 529 |
currPlot->resetFlushesSinceLastUsed(); |
| 530 |
|
| 531 |
// rebuild the LRU list |
| 532 |
SkDEBUGCODE(currPlot->resetListPtrs()); |
| 533 |
fPages[lastPageIndex].fPlotList.addToHead(currPlot); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
// remove ref to the texture proxy |
| 538 |
fProxies[lastPageIndex].reset(); |
| 539 |
--fNumActivePages; |
| 540 |
} |
| 541 |
|
| 542 |
void DrawAtlas::markUsedPlotsAsFull() { |
| 543 |
PlotList::Iter plotIter; |
| 544 |
for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) { |
| 545 |
plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart); |
| 546 |
while (Plot* plot = plotIter.get()) { |
| 547 |
plot->markFullIfUsed(); |
| 548 |
plotIter.next(); |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
void DrawAtlas::freeGpuResources(Token token) { |
| 554 |
PlotList::Iter plotIter; |
| 555 |
for (int pageIndex = (int)(fNumActivePages)-1; pageIndex >= 0; --pageIndex) { |
| 556 |
const Page& currPage = fPages[pageIndex]; |
| 557 |
plotIter.init(currPage.fPlotList, PlotList::Iter::kHead_IterStart); |
| 558 |
while (Plot* plot = plotIter.get()) { |
| 559 |
if (plot->lastUseToken().inInterval(fPrevFlushToken, token)) { |
| 560 |
// This page is in use and we can only deactivate pages from high index |
| 561 |
// to low index, so bail. |
| 562 |
return; |
| 563 |
} |
| 564 |
plotIter.next(); |
| 565 |
} |
| 566 |
this->deactivateLastPage(); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
void DrawAtlas::evictAllPlots() { |
| 571 |
PlotList::Iter plotIter; |
| 572 |
for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) { |
| 573 |
plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart); |
| 574 |
while (Plot* plot = plotIter.get()) { |
| 575 |
this->processEvictionAndResetRects(plot, /*freeData=*/true); |
| 576 |
plotIter.next(); |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
#if defined(GPU_TEST_UTILS) |
| 582 |
int DrawAtlas::numAllocatedPlots() const { |
| 583 |
return this->iteratePlots([](const Plot* plot) { |
| 584 |
return plot->hasAllocation(); |
| 585 |
}); |
| 586 |
} |
| 587 |
|
| 588 |
int DrawAtlas::numNonEmptyPlots() const { |
| 589 |
return this->iteratePlots([](const Plot* plot) { |
| 590 |
return !plot->isEmpty(); |
| 591 |
}); |
| 592 |
} |
| 593 |
#endif |
| 594 |
|
| 595 |
DrawAtlasConfig::DrawAtlasConfig(int maxTextureSize, size_t maxBytes) { |
| 596 |
static const SkISize kARGBDimensions[] = { |
| 597 |
{256, 256}, // maxBytes < 2^19 |
| 598 |
{512, 256}, // 2^19 <= maxBytes < 2^20 |
| 599 |
{512, 512}, // 2^20 <= maxBytes < 2^21 |
| 600 |
{1024, 512}, // 2^21 <= maxBytes < 2^22 |
| 601 |
{1024, 1024}, // 2^22 <= maxBytes < 2^23 |
| 602 |
{2048, 1024}, // 2^23 <= maxBytes |
| 603 |
}; |
| 604 |
|
| 605 |
// Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that |
| 606 |
maxBytes >>= 18; |
| 607 |
// Take the floor of the log to get the index |
| 608 |
int index = maxBytes > 0 |
| 609 |
? SkTPin<int>(SkPrevLog2(maxBytes), 0, std::size(kARGBDimensions) - 1) |
| 610 |
: 0; |
| 611 |
|
| 612 |
SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim); |
| 613 |
SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim); |
| 614 |
fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize), |
| 615 |
std::min<int>(kARGBDimensions[index].height(), maxTextureSize)); |
| 616 |
fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim); |
| 617 |
} |
| 618 |
|
| 619 |
SkISize DrawAtlasConfig::atlasDimensions(MaskFormat type) const { |
| 620 |
if (MaskFormat::kA8 == type) { |
| 621 |
// A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size |
| 622 |
return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize), |
| 623 |
std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) }; |
| 624 |
} else { |
| 625 |
return fARGBDimensions; |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
SkISize DrawAtlasConfig::plotDimensions(MaskFormat type) const { |
| 630 |
if (MaskFormat::kA8 == type) { |
| 631 |
SkISize atlasDimensions = this->atlasDimensions(type); |
| 632 |
// For A8 we want to grow the plots at larger texture sizes to accept more of the |
| 633 |
// larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this |
| 634 |
// allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot. |
| 635 |
|
| 636 |
// This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048, |
| 637 |
// and 256x256 plots otherwise. |
| 638 |
int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256; |
| 639 |
int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256; |
| 640 |
|
| 641 |
return { plotWidth, plotHeight }; |
| 642 |
} else { |
| 643 |
// ARGB and LCD always use 256x256 plots -- this has been shown to be faster |
| 644 |
return { 256, 256 }; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
} // namespace skgpu::graphite |