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