• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "src/gpu/ganesh/GrDrawOpAtlas.h"
9 
10 #include <memory>
11 
12 #include "include/private/base/SkTPin.h"
13 #include "src/gpu/ganesh/GrBackendUtils.h"
14 #include "src/gpu/ganesh/GrCaps.h"
15 #include "src/gpu/ganesh/GrOnFlushResourceProvider.h"
16 #include "src/gpu/ganesh/GrOpFlushState.h"
17 #include "src/gpu/ganesh/GrProxyProvider.h"
18 #include "src/gpu/ganesh/GrResourceProvider.h"
19 #include "src/gpu/ganesh/GrResourceProviderPriv.h"
20 #include "src/gpu/ganesh/GrSurfaceProxyPriv.h"
21 #include "src/gpu/ganesh/GrTexture.h"
22 #include "src/gpu/ganesh/GrTracing.h"
23 
24 using namespace skia_private;
25 
26 using AtlasLocator = skgpu::AtlasLocator;
27 using AtlasToken = skgpu::AtlasToken;
28 using EvictionCallback = skgpu::PlotEvictionCallback;
29 using GenerationCounter = skgpu::AtlasGenerationCounter;
30 using MaskFormat = skgpu::MaskFormat;
31 using Plot = skgpu::Plot;
32 using PlotList = skgpu::PlotList;
33 using PlotLocator = skgpu::PlotLocator;
34 
35 #if defined(DUMP_ATLAS_DATA)
36 static const constexpr bool kDumpAtlasData = true;
37 #else
38 static const constexpr bool kDumpAtlasData = false;
39 #endif
40 
41 #ifdef SK_DEBUG
validate(const AtlasLocator & atlasLocator) const42 void GrDrawOpAtlas::validate(const AtlasLocator& atlasLocator) const {
43     // Verify that the plotIndex stored in the PlotLocator is consistent with the glyph rectangle
44     int numPlotsX = fTextureWidth / fPlotWidth;
45     int numPlotsY = fTextureHeight / fPlotHeight;
46 
47     int plotIndex = atlasLocator.plotIndex();
48     auto topLeft = atlasLocator.topLeft();
49     int plotX = topLeft.x() / fPlotWidth;
50     int plotY = topLeft.y() / fPlotHeight;
51     SkASSERT(plotIndex == (numPlotsY - plotY - 1) * numPlotsX + (numPlotsX - plotX - 1));
52 }
53 #endif
54 
55 // When proxy allocation is deferred until flush time the proxies acting as atlases require
56 // special handling. This is because the usage that can be determined from the ops themselves
57 // isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
58 // atlases. Extending the usage interval of any op that uses an atlas to the start of the
59 // flush (as is done for proxies that are used for sw-generated masks) also won't work because
60 // the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
61 // must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
62 // (which calls this method).
instantiate(GrOnFlushResourceProvider * onFlushResourceProvider)63 void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
64     for (uint32_t i = 0; i < fNumActivePages; ++i) {
65         // All the atlas pages are now instantiated at flush time in the activeNewPage method.
66         SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
67     }
68 }
69 
Make(GrProxyProvider * proxyProvider,const GrBackendFormat & format,SkColorType colorType,size_t bpp,int width,int height,int plotWidth,int plotHeight,GenerationCounter * generationCounter,AllowMultitexturing allowMultitexturing,EvictionCallback * evictor,std::string_view label)70 std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
71                                                    const GrBackendFormat& format,
72                                                    SkColorType colorType, size_t bpp, int width,
73                                                    int height, int plotWidth, int plotHeight,
74                                                    GenerationCounter* generationCounter,
75                                                    AllowMultitexturing allowMultitexturing,
76                                                    EvictionCallback* evictor,
77                                                    std::string_view label) {
78     if (!format.isValid()) {
79         return nullptr;
80     }
81 
82     std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType, bpp,
83                                                            width, height, plotWidth, plotHeight,
84                                                            generationCounter,
85                                                            allowMultitexturing, label));
86     if (!atlas->createPages(proxyProvider, generationCounter) || !atlas->getViews()[0].proxy()) {
87         return nullptr;
88     }
89 
90     if (evictor != nullptr) {
91         atlas->fEvictionCallbacks.emplace_back(evictor);
92     }
93     return atlas;
94 }
95 
96 ///////////////////////////////////////////////////////////////////////////////
97 
GrDrawOpAtlas(GrProxyProvider * proxyProvider,const GrBackendFormat & format,SkColorType colorType,size_t bpp,int width,int height,int plotWidth,int plotHeight,GenerationCounter * generationCounter,AllowMultitexturing allowMultitexturing,std::string_view label)98 GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
99                              SkColorType colorType, size_t bpp, int width, int height,
100                              int plotWidth, int plotHeight, GenerationCounter* generationCounter,
101                              AllowMultitexturing allowMultitexturing, std::string_view label)
102         : fFormat(format)
103         , fColorType(colorType)
104         , fBytesPerPixel(bpp)
105         , fTextureWidth(width)
106         , fTextureHeight(height)
107         , fPlotWidth(plotWidth)
108         , fPlotHeight(plotHeight)
109         , fLabel(label)
110         , fGenerationCounter(generationCounter)
111         , fAtlasGeneration(fGenerationCounter->next())
112         , fPrevFlushToken(AtlasToken::InvalidToken())
113         , fFlushesSinceLastUse(0)
114         , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ?
115                             PlotLocator::kMaxMultitexturePages : 1)
116         , fNumActivePages(0) {
117     int numPlotsX = width/plotWidth;
118     int numPlotsY = height/plotHeight;
119     SkASSERT(numPlotsX * numPlotsY <= PlotLocator::kMaxPlots);
120     SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
121     SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
122 
123     fNumPlots = numPlotsX * numPlotsY;
124 }
125 
processEviction(PlotLocator plotLocator)126 inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
127     for (EvictionCallback* evictor : fEvictionCallbacks) {
128         evictor->evict(plotLocator);
129     }
130 
131     fAtlasGeneration = fGenerationCounter->next();
132 }
133 
uploadPlotToTexture(GrDeferredTextureUploadWritePixelsFn & writePixels,GrTextureProxy * proxy,Plot * plot)134 void GrDrawOpAtlas::uploadPlotToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
135                                         GrTextureProxy* proxy,
136                                         Plot* plot) {
137     SkASSERT(proxy && proxy->peekTexture());
138     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
139 
140     const void* dataPtr;
141     SkIRect rect;
142     std::tie(dataPtr, rect) = plot->prepareForUpload();
143 
144     writePixels(proxy,
145                 rect,
146                 SkColorTypeToGrColorType(fColorType),
147                 dataPtr,
148                 fBytesPerPixel*fPlotWidth);
149 }
150 
updatePlot(GrDeferredUploadTarget * target,AtlasLocator * atlasLocator,Plot * plot)151 inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
152                                       AtlasLocator* atlasLocator, Plot* plot) {
153     uint32_t pageIdx = plot->pageIndex();
154     if (pageIdx >= fNumActivePages) {
155         return false;
156     }
157     this->makeMRU(plot, pageIdx);
158 
159     // If our most recent upload has already occurred then we have to insert a new
160     // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
161     // This new update will piggy back on that previously scheduled update.
162     if (plot->lastUploadToken() < target->tokenTracker()->nextFlushToken()) {
163         // With c+14 we could move sk_sp into lamba to only ref once.
164         sk_sp<Plot> plotsp(SkRef(plot));
165 
166         GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
167         SkASSERT(proxy && proxy->isInstantiated());  // This is occurring at flush time
168 
169         AtlasToken lastUploadToken = target->addASAPUpload(
170                 [this, plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
171                     this->uploadPlotToTexture(writePixels, proxy, plotsp.get());
172                 });
173         plot->setLastUploadToken(lastUploadToken);
174     }
175     atlasLocator->updatePlotLocator(plot->plotLocator());
176     SkDEBUGCODE(this->validate(*atlasLocator);)
177     return true;
178 }
179 
uploadToPage(unsigned int pageIdx,GrDeferredUploadTarget * target,int width,int height,const void * image,AtlasLocator * atlasLocator)180 bool GrDrawOpAtlas::uploadToPage(unsigned int pageIdx, GrDeferredUploadTarget* target, int width,
181                                  int height, const void* image, AtlasLocator* atlasLocator) {
182     SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
183 
184     // look through all allocated plots for one we can share, in Most Recently Refed order
185     PlotList::Iter plotIter;
186     plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
187 
188     for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
189         SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
190                  plot->bpp());
191 
192         if (plot->addSubImage(width, height, image, atlasLocator)) {
193             return this->updatePlot(target, atlasLocator, plot);
194         }
195     }
196 
197     return false;
198 }
199 
200 // Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
201 //
202 // This value is somewhat arbitrary -- the idea is to keep it low enough that
203 // a page with unused plots will get removed reasonably quickly, but allow it
204 // to hang around for a bit in case it's needed. The assumption is that flushes
205 // are rare; i.e., we are not continually refreshing the frame.
206 static constexpr auto kPlotRecentlyUsedCount = 32;
207 static constexpr auto kAtlasRecentlyUsedCount = 128;
208 
addToAtlas(GrResourceProvider * resourceProvider,GrDeferredUploadTarget * target,int width,int height,const void * image,AtlasLocator * atlasLocator)209 GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
210                                                    GrDeferredUploadTarget* target,
211                                                    int width, int height, const void* image,
212                                                    AtlasLocator* atlasLocator) {
213     if (width > fPlotWidth || height > fPlotHeight) {
214         return ErrorCode::kError;
215     }
216 
217     // Look through each page to see if we can upload without having to flush
218     // We prioritize this upload to the first pages, not the most recently used, to make it easier
219     // to remove unused pages in reverse page order.
220     for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
221         if (this->uploadToPage(pageIdx, target, width, height, image, atlasLocator)) {
222             return ErrorCode::kSucceeded;
223         }
224     }
225 
226     // If the above fails, then see if the least recently used plot per page has already been
227     // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
228     // We wait until we've grown to the full number of pages to begin evicting already flushed
229     // plots so that we can maximize the opportunity for reuse.
230     // As before we prioritize this upload to the first pages, not the most recently used.
231     if (fNumActivePages == this->maxPages()) {
232         for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
233             Plot* plot = fPages[pageIdx].fPlotList.tail();
234             SkASSERT(plot);
235             if (plot->lastUseToken() < target->tokenTracker()->nextFlushToken()) {
236                 this->processEvictionAndResetRects(plot);
237                 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
238                          plot->bpp());
239                 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, atlasLocator);
240                 SkASSERT(verify);
241                 if (!this->updatePlot(target, atlasLocator, plot)) {
242                     return ErrorCode::kError;
243                 }
244                 return ErrorCode::kSucceeded;
245             }
246         }
247     } else {
248         // If we haven't activated all the available pages, try to create a new one and add to it
249         if (!this->activateNewPage(resourceProvider)) {
250             return ErrorCode::kError;
251         }
252 
253         if (this->uploadToPage(fNumActivePages-1, target, width, height, image, atlasLocator)) {
254             return ErrorCode::kSucceeded;
255         } else {
256             // If we fail to upload to a newly activated page then something has gone terribly
257             // wrong - return an error
258             return ErrorCode::kError;
259         }
260     }
261 
262     if (!fNumActivePages) {
263         return ErrorCode::kError;
264     }
265 
266     // Try to find a plot that we can perform an inline upload to.
267     // We prioritize this upload in reverse order of pages to counterbalance the order above.
268     Plot* plot = nullptr;
269     for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
270         Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
271         if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
272             plot = currentPlot;
273             break;
274         }
275     }
276 
277     // If we can't find a plot that is not used in a draw currently being prepared by an op, then
278     // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
279     // function. When that draw is enqueued, the draw token advances, and the subsequent call will
280     // continue past this branch and prepare an inline upload that will occur after the enqueued
281     // draw which references the plot's pre-upload content.
282     if (!plot) {
283         return ErrorCode::kTryAgain;
284     }
285 
286     this->processEviction(plot->plotLocator());
287     int pageIdx = plot->pageIndex();
288     fPages[pageIdx].fPlotList.remove(plot);
289     sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
290     newPlot = plot->clone();
291 
292     fPages[pageIdx].fPlotList.addToHead(newPlot.get());
293     SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
294              newPlot->bpp());
295     SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, atlasLocator);
296     SkASSERT(verify);
297 
298     // Note that this plot will be uploaded inline with the draws whereas the
299     // one it displaced most likely was uploaded ASAP.
300     // With c++14 we could move sk_sp into lambda to only ref once.
301     sk_sp<Plot> plotsp(SkRef(newPlot.get()));
302 
303     GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
304     SkASSERT(proxy && proxy->isInstantiated());
305 
306     AtlasToken lastUploadToken = target->addInlineUpload(
307             [this, plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
308                 this->uploadPlotToTexture(writePixels, proxy, plotsp.get());
309             });
310     newPlot->setLastUploadToken(lastUploadToken);
311 
312     atlasLocator->updatePlotLocator(newPlot->plotLocator());
313     SkDEBUGCODE(this->validate(*atlasLocator);)
314 
315     return ErrorCode::kSucceeded;
316 }
317 
compact(AtlasToken startTokenForNextFlush)318 void GrDrawOpAtlas::compact(AtlasToken startTokenForNextFlush) {
319     if (fNumActivePages < 1) {
320         fPrevFlushToken = startTokenForNextFlush;
321         return;
322     }
323 
324     // For all plots, reset number of flushes since used if used this frame.
325     PlotList::Iter plotIter;
326     bool atlasUsedThisFlush = false;
327     for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
328         plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
329         while (Plot* plot = plotIter.get()) {
330             // Reset number of flushes since used
331             if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
332                 plot->resetFlushesSinceLastUsed();
333                 atlasUsedThisFlush = true;
334             }
335 
336             plotIter.next();
337         }
338     }
339 
340     if (atlasUsedThisFlush) {
341         fFlushesSinceLastUse = 0;
342     } else {
343         ++fFlushesSinceLastUse;
344     }
345 
346     // We only try to compact if the atlas was used in the recently completed flush or
347     // hasn't been used in a long time.
348     // This is to handle the case where a lot of text or path rendering has occurred but then just
349     // a blinking cursor is drawn.
350     if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
351         TArray<Plot*> availablePlots;
352         uint32_t lastPageIndex = fNumActivePages - 1;
353 
354         // For all plots but the last one, update number of flushes since used, and check to see
355         // if there are any in the first pages that the last page can safely upload to.
356         for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
357             if constexpr (kDumpAtlasData) {
358                 SkDebugf("page %u: ", pageIndex);
359             }
360 
361             plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
362             while (Plot* plot = plotIter.get()) {
363                 // Update number of flushes since plot was last used
364                 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
365                 // to avoid deleting everything when we return to text drawing in the blinking
366                 // cursor case
367                 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
368                     plot->incFlushesSinceLastUsed();
369                 }
370 
371                 if constexpr (kDumpAtlasData) {
372                     SkDebugf("%d ", plot->flushesSinceLastUsed());
373                 }
374 
375                 // Count plots we can potentially upload to in all pages except the last one
376                 // (the potential compactee).
377                 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
378                     availablePlots.push_back() = plot;
379                 }
380 
381                 plotIter.next();
382             }
383 
384             if constexpr (kDumpAtlasData) {
385                 SkDebugf("\n");
386             }
387         }
388 
389         // Count recently used plots in the last page and evict any that are no longer in use.
390         // Since we prioritize uploading to the first pages, this will eventually
391         // clear out usage of this page unless we have a large need.
392         plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
393         unsigned int usedPlots = 0;
394         if constexpr (kDumpAtlasData) {
395             SkDebugf("page %u: ", lastPageIndex);
396         }
397 
398         while (Plot* plot = plotIter.get()) {
399             // Update number of flushes since plot was last used
400             if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
401                 plot->incFlushesSinceLastUsed();
402             }
403 
404             if constexpr (kDumpAtlasData) {
405                 SkDebugf("%d ", plot->flushesSinceLastUsed());
406             }
407 
408             // If this plot was used recently
409             if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
410                 usedPlots++;
411             } else if (plot->lastUseToken() != AtlasToken::InvalidToken()) {
412                 // otherwise if aged out just evict it.
413                 this->processEvictionAndResetRects(plot);
414             }
415             plotIter.next();
416         }
417 
418         if constexpr (kDumpAtlasData) {
419             SkDebugf("\n");
420         }
421 
422         // If recently used plots in the last page are using less than a quarter of the page, try
423         // to evict them if there's available space in earlier pages. Since we prioritize uploading
424         // to the first pages, this will eventually clear out usage of this page unless we have a
425         // large need.
426         if (!availablePlots.empty() && usedPlots && usedPlots <= fNumPlots / 4) {
427             plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
428             while (Plot* plot = plotIter.get()) {
429                 // If this plot was used recently
430                 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
431                     // See if there's room in an earlier page and if so evict.
432                     // We need to be somewhat harsh here so that a handful of plots that are
433                     // consistently in use don't end up locking the page in memory.
434                     if (!availablePlots.empty()) {
435                         this->processEvictionAndResetRects(plot);
436                         this->processEvictionAndResetRects(availablePlots.back());
437                         availablePlots.pop_back();
438                         --usedPlots;
439                     }
440                     if (!usedPlots || availablePlots.empty()) {
441                         break;
442                     }
443                 }
444                 plotIter.next();
445             }
446         }
447 
448         // If none of the plots in the last page have been used recently, delete it.
449         if (!usedPlots) {
450             if constexpr (kDumpAtlasData) {
451                 SkDebugf("delete %u\n", fNumActivePages - 1);
452             }
453 
454             this->deactivateLastPage();
455             fFlushesSinceLastUse = 0;
456         }
457     }
458 
459     fPrevFlushToken = startTokenForNextFlush;
460 }
461 
createPages(GrProxyProvider * proxyProvider,GenerationCounter * generationCounter)462 bool GrDrawOpAtlas::createPages(
463         GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
464     SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
465 
466     SkISize dims = {fTextureWidth, fTextureHeight};
467 
468     int numPlotsX = fTextureWidth/fPlotWidth;
469     int numPlotsY = fTextureHeight/fPlotHeight;
470 
471     GrColorType grColorType = SkColorTypeToGrColorType(fColorType);
472 
473     for (uint32_t i = 0; i < this->maxPages(); ++i) {
474         skgpu::Swizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, grColorType);
475         if (GrColorTypeIsAlphaOnly(grColorType)) {
476             swizzle = skgpu::Swizzle::Concat(swizzle, skgpu::Swizzle("aaaa"));
477         }
478         sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(fFormat,
479                                                                  dims,
480                                                                  GrRenderable::kNo,
481                                                                  1,
482                                                                  skgpu::Mipmapped::kNo,
483                                                                  SkBackingFit::kExact,
484                                                                  skgpu::Budgeted::kYes,
485                                                                  GrProtected::kNo,
486                                                                  fLabel,
487                                                                  GrInternalSurfaceFlags::kNone,
488                                                                  GrSurfaceProxy::UseAllocator::kNo);
489         if (!proxy) {
490             return false;
491         }
492         fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
493 
494         // set up allocated plots
495         fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY);
496 
497         sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
498         for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
499             for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
500                 uint32_t plotIndex = r * numPlotsX + c;
501                 currPlot->reset(new Plot(
502                     i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType,
503                     fBytesPerPixel));
504 
505                 // build LRU list
506                 fPages[i].fPlotList.addToHead(currPlot->get());
507                 ++currPlot;
508             }
509         }
510 
511     }
512 
513     return true;
514 }
515 
activateNewPage(GrResourceProvider * resourceProvider)516 bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
517     SkASSERT(fNumActivePages < this->maxPages());
518 
519     if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
520         return false;
521     }
522 
523     if constexpr (kDumpAtlasData) {
524         SkDebugf("activated page#: %u\n", fNumActivePages);
525     }
526 
527     ++fNumActivePages;
528     return true;
529 }
530 
deactivateLastPage()531 inline void GrDrawOpAtlas::deactivateLastPage() {
532     SkASSERT(fNumActivePages);
533 
534     uint32_t lastPageIndex = fNumActivePages - 1;
535 
536     int numPlotsX = fTextureWidth/fPlotWidth;
537     int numPlotsY = fTextureHeight/fPlotHeight;
538 
539     fPages[lastPageIndex].fPlotList.reset();
540     for (int r = 0; r < numPlotsY; ++r) {
541         for (int c = 0; c < numPlotsX; ++c) {
542             uint32_t plotIndex = r * numPlotsX + c;
543 
544             Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
545             currPlot->resetRects();
546             currPlot->resetFlushesSinceLastUsed();
547 
548             // rebuild the LRU list
549             SkDEBUGCODE(currPlot->resetListPtrs());
550             fPages[lastPageIndex].fPlotList.addToHead(currPlot);
551         }
552     }
553 
554     // remove ref to the backing texture
555     fViews[lastPageIndex].proxy()->deinstantiate();
556     --fNumActivePages;
557 }
558 
GrDrawOpAtlasConfig(int maxTextureSize,size_t maxBytes)559 GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
560     static const SkISize kARGBDimensions[] = {
561         {256, 256},   // maxBytes < 2^19
562         {512, 256},   // 2^19 <= maxBytes < 2^20
563         {512, 512},   // 2^20 <= maxBytes < 2^21
564         {1024, 512},  // 2^21 <= maxBytes < 2^22
565         {1024, 1024}, // 2^22 <= maxBytes < 2^23
566         {2048, 1024}, // 2^23 <= maxBytes
567     };
568 
569     // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
570     maxBytes >>= 18;
571     // Take the floor of the log to get the index
572     int index = maxBytes > 0
573         ? SkTPin<int>(SkPrevLog2(maxBytes), 0, std::size(kARGBDimensions) - 1)
574         : 0;
575 
576     SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
577     SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
578     fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
579                         std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
580     fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
581 }
582 
atlasDimensions(MaskFormat type) const583 SkISize GrDrawOpAtlasConfig::atlasDimensions(MaskFormat type) const {
584     if (MaskFormat::kA8 == type) {
585         // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
586         return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
587                  std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
588     } else {
589         return fARGBDimensions;
590     }
591 }
592 
plotDimensions(MaskFormat type) const593 SkISize GrDrawOpAtlasConfig::plotDimensions(MaskFormat type) const {
594     if (MaskFormat::kA8 == type) {
595         SkISize atlasDimensions = this->atlasDimensions(type);
596         // For A8 we want to grow the plots at larger texture sizes to accept more of the
597         // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
598         // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
599 
600         // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
601         // and 256x256 plots otherwise.
602         int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
603         int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
604 
605         return { plotWidth, plotHeight };
606     } else {
607         // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
608         return { 256, 256 };
609     }
610 }
611