blob: c562c55177043daab5b8033a7b5c8940cefcff8e [file] [log] [blame]
joshualitt5bf99f12015-03-13 11:47:42 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrDrawOpAtlas.h"
Robert Phillips32f28182017-02-28 16:20:03 -05009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Mike Klein8aa0edf2020-10-16 11:04:18 -050012#include "include/private/SkTPin.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050013#include "src/core/SkOpts.h"
Greg Daniel0eca74c2020-10-01 13:46:00 -040014#include "src/gpu/GrBackendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrOnFlushResourceProvider.h"
16#include "src/gpu/GrOpFlushState.h"
17#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrResourceProvider.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040019#include "src/gpu/GrResourceProviderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000021#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070023
Jim Van Verthfb395102020-02-03 10:11:19 -050024#ifdef DUMP_ATLAS_DATA
25static bool gDumpAtlasData = false;
26#endif
27
Robert Phillipse87106d2020-04-09 14:21:33 -040028#ifdef SK_DEBUG
Herb Derby06296c62020-08-28 13:42:31 -040029void GrDrawOpAtlas::validate(const AtlasLocator& atlasLocator) const {
Robert Phillipse87106d2020-04-09 14:21:33 -040030 // Verify that the plotIndex stored in the PlotLocator is consistent with the glyph rectangle
Herb Derby06296c62020-08-28 13:42:31 -040031 int numPlotsX = fTextureWidth / fPlotWidth;
32 int numPlotsY = fTextureHeight / fPlotHeight;
Robert Phillipse87106d2020-04-09 14:21:33 -040033
Herb Derby06296c62020-08-28 13:42:31 -040034 int plotIndex = atlasLocator.plotIndex();
Herb Derbye1a00892020-08-31 15:12:27 -040035 auto topLeft = atlasLocator.topLeft();
36 int plotX = topLeft.x() / fPlotWidth;
37 int plotY = topLeft.y() / fPlotHeight;
Robert Phillipse87106d2020-04-09 14:21:33 -040038 SkASSERT(plotIndex == (numPlotsY - plotY - 1) * numPlotsX + (numPlotsX - plotX - 1));
39}
40#endif
41
Robert Phillipscd5099c2018-02-09 09:56:56 -050042// When proxy allocation is deferred until flush time the proxies acting as atlases require
43// special handling. This is because the usage that can be determined from the ops themselves
44// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
45// atlases. Extending the usage interval of any op that uses an atlas to the start of the
46// flush (as is done for proxies that are used for sw-generated masks) also won't work because
47// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
48// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
49// (which calls this method).
50void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050051 for (uint32_t i = 0; i < fNumActivePages; ++i) {
52 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050053 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050054 }
55}
56
Robert Phillips4bc70112018-03-01 10:24:02 -050057std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050058 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040059 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050060 int height, int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -050061 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -050062 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050063 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040064 if (!format.isValid()) {
65 return nullptr;
66 }
67
Herb Derby0ef780b2020-01-24 15:57:11 -050068 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType,
69 width, height, plotWidth, plotHeight,
70 generationCounter,
Robert Phillips4bc70112018-03-01 10:24:02 -050071 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050072 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040073 return nullptr;
74 }
75
Herb Derbya90ed952020-01-28 15:55:58 -050076 if (evictor != nullptr) {
77 atlas->fEvictionCallbacks.emplace_back(evictor);
78 }
Robert Phillips256c37b2017-03-01 14:32:46 -050079 return atlas;
80}
81
joshualitt5df175e2015-11-18 13:37:54 -080082////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -050083GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
84 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040085 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
86 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040087 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040088 , fPageIndex(pageIndex)
89 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -050090 , fGenerationCounter(generationCounter)
91 , fGenID(fGenerationCounter->next())
Robert Phillipsbf5bf742020-04-13 09:29:08 -040092 , fPlotLocator(fPageIndex, fPlotIndex, fGenID)
Brian Salomon2ee084e2016-12-16 18:59:19 -050093 , fData(nullptr)
94 , fWidth(width)
95 , fHeight(height)
96 , fX(offX)
97 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -050098 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -050099 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400100 , fColorType(colorType)
101 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800102#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500103 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800104#endif
105{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500106 // We expect the allocated dimensions to be a multiple of 4 bytes
107 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
108 // The padding for faster uploads only works for 1, 2 and 4 byte texels
109 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800110 fDirtyRect.setEmpty();
111}
joshualitt5bf99f12015-03-13 11:47:42 -0700112
Brian Salomon2ee084e2016-12-16 18:59:19 -0500113GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700114 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800115}
joshualitt5bf99f12015-03-13 11:47:42 -0700116
Herb Derby06296c62020-08-28 13:42:31 -0400117bool GrDrawOpAtlas::Plot::addSubImage(
118 int width, int height, const void* image, AtlasLocator* atlasLocator) {
joshualitt5df175e2015-11-18 13:37:54 -0800119 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700120
Robert Phillips6d3bc292020-04-06 10:29:28 -0400121 SkIPoint16 loc;
122 if (!fRectanizer.addRect(width, height, &loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800123 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700124 }
joshualitt5bf99f12015-03-13 11:47:42 -0700125
Herb Derby06296c62020-08-28 13:42:31 -0400126 GrIRect16 rect = GrIRect16::MakeXYWH(loc.fX, loc.fY, width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400127
jvanverthc3d706f2016-04-20 10:33:27 -0700128 if (!fData) {
Herb Derby06296c62020-08-28 13:42:31 -0400129 fData = reinterpret_cast<unsigned char*>(
130 sk_calloc_throw(fBytesPerPixel * fWidth * fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800131 }
132 size_t rowBytes = width * fBytesPerPixel;
133 const unsigned char* imagePtr = (const unsigned char*)image;
134 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700135 unsigned char* dataPtr = fData;
Herb Derby06296c62020-08-28 13:42:31 -0400136 dataPtr += fBytesPerPixel * fWidth * rect.fTop;
137 dataPtr += fBytesPerPixel * rect.fLeft;
Brian Osmancce3e582016-10-14 11:42:20 -0400138 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500139 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400140 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400141 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400142 dataPtr += fBytesPerPixel * fWidth;
143 imagePtr += rowBytes;
144 }
145 } else {
146 for (int i = 0; i < height; ++i) {
147 memcpy(dataPtr, imagePtr, rowBytes);
148 dataPtr += fBytesPerPixel * fWidth;
149 imagePtr += rowBytes;
150 }
joshualitt5bf99f12015-03-13 11:47:42 -0700151 }
152
Herb Derby06296c62020-08-28 13:42:31 -0400153 fDirtyRect.join({rect.fLeft, rect.fTop, rect.fRight, rect.fBottom});
robertphillips2b0536f2015-11-06 14:10:42 -0800154
Herb Derby06296c62020-08-28 13:42:31 -0400155 rect.offset(fOffset.fX, fOffset.fY);
156 atlasLocator->updateRect(rect);
joshualitt5df175e2015-11-18 13:37:54 -0800157 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700158
joshualitt5df175e2015-11-18 13:37:54 -0800159 return true;
160}
joshualitt5bf99f12015-03-13 11:47:42 -0700161
Brian Salomon943ed792017-10-30 09:37:55 -0400162void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400163 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800164 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400165 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400166 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800167 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700168 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500169 // Clamp to 4-byte aligned boundaries
170 unsigned int clearBits = 0x3 / fBytesPerPixel;
171 fDirtyRect.fLeft &= ~clearBits;
172 fDirtyRect.fRight += clearBits;
173 fDirtyRect.fRight &= ~clearBits;
174 SkASSERT(fDirtyRect.fRight <= fWidth);
175 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700176 dataPtr += rowBytes * fDirtyRect.fTop;
177 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400178
Robert Phillipsacaa6072017-07-28 10:54:53 -0400179 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400180 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800181 fDirtyRect.setEmpty();
182 SkDEBUGCODE(fDirty = false;)
183}
184
Brian Salomon2ee084e2016-12-16 18:59:19 -0500185void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500186 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700187
Herb Derby0ef780b2020-01-24 15:57:11 -0500188 fGenID = fGenerationCounter->next();
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400189 fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400190 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
191 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800192
193 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700194 if (fData) {
195 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700196 }
197
joshualitt5df175e2015-11-18 13:37:54 -0800198 fDirtyRect.setEmpty();
199 SkDEBUGCODE(fDirty = false;)
200}
joshualitt5bf99f12015-03-13 11:47:42 -0700201
joshualitt5bf99f12015-03-13 11:47:42 -0700202///////////////////////////////////////////////////////////////////////////////
203
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400204GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
205 GrColorType colorType, int width, int height,
206 int plotWidth, int plotHeight, GenerationCounter* generationCounter,
207 AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500208 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400209 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400210 , fTextureWidth(width)
211 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500212 , fPlotWidth(plotWidth)
213 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500214 , fGenerationCounter(generationCounter)
215 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400216 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400217 , fFlushesSinceLastUse(0)
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500218 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500219 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500220 int numPlotsX = width/plotWidth;
221 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400222 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400223 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
224 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800225
Jim Van Verth06f593c2018-02-20 11:30:10 -0500226 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700227
Herb Derby0ef780b2020-01-24 15:57:11 -0500228 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700229}
230
Herb Derby4d721712020-01-24 14:31:16 -0500231inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400232 for (EvictionCallback* evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500233 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700234 }
Herb Derby1a496c52020-01-22 17:26:56 -0500235
Herb Derby0ef780b2020-01-24 15:57:11 -0500236 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700237}
238
Herb Derby4d721712020-01-24 14:31:16 -0500239inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400240 AtlasLocator* atlasLocator, Plot* plot) {
241 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400242 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700243
244 // If our most recent upload has already occurred then we have to insert a new
245 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
246 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500247 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700248 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500249 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500250
Greg Daniel9715b6c2019-12-10 15:03:10 -0500251 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
252 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500253
Brian Salomon29b60c92017-10-31 14:42:10 -0400254 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400255 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
256 plotsp->uploadToTexture(writePixels, proxy);
257 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500258 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700259 }
Herb Derby06296c62020-08-28 13:42:31 -0400260 atlasLocator->updatePlotLocator(plot->plotLocator());
261 SkDEBUGCODE(this->validate(*atlasLocator);)
Robert Phillips256c37b2017-03-01 14:32:46 -0500262 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700263}
264
Greg Daniel0eca74c2020-10-01 13:46:00 -0400265bool GrDrawOpAtlas::uploadToPage(unsigned int pageIdx, GrDeferredUploadTarget* target, int width,
266 int height, const void* image, AtlasLocator* atlasLocator) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500267 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500268
269 // look through all allocated plots for one we can share, in Most Recently Refed order
270 PlotList::Iter plotIter;
271 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
272
273 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel0eca74c2020-10-01 13:46:00 -0400274 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
275 plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500276
Herb Derby06296c62020-08-28 13:42:31 -0400277 if (plot->addSubImage(width, height, image, atlasLocator)) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400278 return this->updatePlot(target, atlasLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500279 }
280 }
281
282 return false;
283}
284
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400285// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
286//
287// This value is somewhat arbitrary -- the idea is to keep it low enough that
288// a page with unused plots will get removed reasonably quickly, but allow it
289// to hang around for a bit in case it's needed. The assumption is that flushes
290// are rare; i.e., we are not continually refreshing the frame.
Jonathan Backer40c683a2020-05-04 15:00:02 -0400291static constexpr auto kPlotRecentlyUsedCount = 32;
292static constexpr auto kAtlasRecentlyUsedCount = 128;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400293
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500294GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500295 GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400296 int width, int height, const void* image,
297 AtlasLocator* atlasLocator) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700298 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500299 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700300 }
joshualitt5bf99f12015-03-13 11:47:42 -0700301
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400302 // Look through each page to see if we can upload without having to flush
303 // We prioritize this upload to the first pages, not the most recently used, to make it easier
304 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500305 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Greg Daniel0eca74c2020-10-01 13:46:00 -0400306 if (this->uploadToPage(pageIdx, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500307 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400308 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400309 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400310
Jim Van Verth712fe732017-09-25 16:53:49 -0400311 // If the above fails, then see if the least recently used plot per page has already been
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400312 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
313 // We wait until we've grown to the full number of pages to begin evicting already flushed
314 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400315 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500316 if (fNumActivePages == this->maxPages()) {
317 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
318 Plot* plot = fPages[pageIdx].fPlotList.tail();
319 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500320 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500321 this->processEvictionAndResetRects(plot);
Greg Daniel0eca74c2020-10-01 13:46:00 -0400322 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
Greg Daniel9715b6c2019-12-10 15:03:10 -0500323 plot->bpp());
Herb Derby06296c62020-08-28 13:42:31 -0400324 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, atlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500325 SkASSERT(verify);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400326 if (!this->updatePlot(target, atlasLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500327 return ErrorCode::kError;
328 }
329 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400330 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500331 }
332 } else {
333 // If we haven't activated all the available pages, try to create a new one and add to it
334 if (!this->activateNewPage(resourceProvider)) {
335 return ErrorCode::kError;
336 }
337
Greg Daniel0eca74c2020-10-01 13:46:00 -0400338 if (this->uploadToPage(fNumActivePages-1, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500339 return ErrorCode::kSucceeded;
340 } else {
341 // If we fail to upload to a newly activated page then something has gone terribly
342 // wrong - return an error
343 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400344 }
345 }
346
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500347 if (!fNumActivePages) {
348 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700349 }
350
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400351 // Try to find a plot that we can perform an inline upload to.
352 // We prioritize this upload in reverse order of pages to counterbalance the order above.
353 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500354 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400355 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500356 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400357 plot = currentPlot;
358 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500359 }
joshualitt5bf99f12015-03-13 11:47:42 -0700360 }
361
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400362 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
363 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
364 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
365 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500366 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400367 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500368 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700369 }
370
Herb Derby4d721712020-01-24 14:31:16 -0500371 this->processEviction(plot->plotLocator());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400372 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400373 fPages[pageIdx].fPlotList.remove(plot);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400374 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
robertphillips2b0536f2015-11-06 14:10:42 -0800375 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700376
Jim Van Vertha950b632017-09-12 11:54:11 -0400377 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel0eca74c2020-10-01 13:46:00 -0400378 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
379 newPlot->bpp());
Herb Derby06296c62020-08-28 13:42:31 -0400380 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, atlasLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700381 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800382
robertphillips1f0e3502015-11-10 10:19:50 -0800383 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400384 // one it displaced most likely was uploaded ASAP.
Robert Phillipse87106d2020-04-09 14:21:33 -0400385 // With c++14 we could move sk_sp into lambda to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500386 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500387
Greg Daniel9715b6c2019-12-10 15:03:10 -0500388 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
389 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700390
Brian Salomon943ed792017-10-30 09:37:55 -0400391 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
392 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
393 plotsp->uploadToTexture(writePixels, proxy);
394 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500395 newPlot->setLastUploadToken(lastUploadToken);
396
Herb Derby06296c62020-08-28 13:42:31 -0400397 atlasLocator->updatePlotLocator(newPlot->plotLocator());
398 SkDEBUGCODE(this->validate(*atlasLocator);)
robertphillips2b0536f2015-11-06 14:10:42 -0800399
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500400 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700401}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400402
Brian Salomon943ed792017-10-30 09:37:55 -0400403void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400404 if (fNumActivePages < 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400405 fPrevFlushToken = startTokenForNextFlush;
406 return;
407 }
408
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400409 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400410 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400411 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500412 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400413 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
414 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400415 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400416 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
417 plot->resetFlushesSinceLastUsed();
418 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400419 }
420
421 plotIter.next();
422 }
423 }
424
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400425 if (atlasUsedThisFlush) {
426 fFlushesSinceLastUse = 0;
427 } else {
428 ++fFlushesSinceLastUse;
429 }
430
431 // We only try to compact if the atlas was used in the recently completed flush or
432 // hasn't been used in a long time.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400433 // This is to handle the case where a lot of text or path rendering has occurred but then just
434 // a blinking cursor is drawn.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400435 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500436 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500437 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400438
439 // For all plots but the last one, update number of flushes since used, and check to see
440 // if there are any in the first pages that the last page can safely upload to.
441 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400442#ifdef DUMP_ATLAS_DATA
443 if (gDumpAtlasData) {
444 SkDebugf("page %d: ", pageIndex);
445 }
446#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400447 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
448 while (Plot* plot = plotIter.get()) {
449 // Update number of flushes since plot was last used
450 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
451 // to avoid deleting everything when we return to text drawing in the blinking
452 // cursor case
453 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
454 plot->incFlushesSinceLastUsed();
455 }
456
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400457#ifdef DUMP_ATLAS_DATA
458 if (gDumpAtlasData) {
459 SkDebugf("%d ", plot->flushesSinceLastUsed());
460 }
461#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400462 // Count plots we can potentially upload to in all pages except the last one
463 // (the potential compactee).
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400464 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500465 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400466 }
467
468 plotIter.next();
469 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400470#ifdef DUMP_ATLAS_DATA
471 if (gDumpAtlasData) {
472 SkDebugf("\n");
473 }
474#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400475 }
476
Jim Van Verth06f593c2018-02-20 11:30:10 -0500477 // Count recently used plots in the last page and evict any that are no longer in use.
478 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400479 // clear out usage of this page unless we have a large need.
480 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500481 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400482#ifdef DUMP_ATLAS_DATA
483 if (gDumpAtlasData) {
484 SkDebugf("page %d: ", lastPageIndex);
485 }
486#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400487 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400488 // Update number of flushes since plot was last used
489 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
490 plot->incFlushesSinceLastUsed();
491 }
492
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400493#ifdef DUMP_ATLAS_DATA
494 if (gDumpAtlasData) {
495 SkDebugf("%d ", plot->flushesSinceLastUsed());
496 }
497#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400498 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400499 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400500 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400501 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400502 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400503 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400504 }
505 plotIter.next();
506 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400507#ifdef DUMP_ATLAS_DATA
508 if (gDumpAtlasData) {
509 SkDebugf("\n");
510 }
511#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500512
513 // If recently used plots in the last page are using less than a quarter of the page, try
514 // to evict them if there's available space in earlier pages. Since we prioritize uploading
515 // to the first pages, this will eventually clear out usage of this page unless we have a
516 // large need.
517 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
518 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
519 while (Plot* plot = plotIter.get()) {
520 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400521 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth06f593c2018-02-20 11:30:10 -0500522 // See if there's room in an earlier page and if so evict.
523 // We need to be somewhat harsh here so that a handful of plots that are
524 // consistently in use don't end up locking the page in memory.
525 if (availablePlots.count() > 0) {
526 this->processEvictionAndResetRects(plot);
527 this->processEvictionAndResetRects(availablePlots.back());
528 availablePlots.pop_back();
529 --usedPlots;
530 }
531 if (!usedPlots || !availablePlots.count()) {
532 break;
533 }
534 }
535 plotIter.next();
536 }
537 }
538
Jim Van Verth106b5c42017-09-26 12:45:29 -0400539 // If none of the plots in the last page have been used recently, delete it.
Jim Van Verth26651882020-03-18 15:30:07 +0000540 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400541#ifdef DUMP_ATLAS_DATA
542 if (gDumpAtlasData) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400543 SkDebugf("delete %d\n", fNumActivePages-1);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400544 }
545#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500546 this->deactivateLastPage();
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400547 fFlushesSinceLastUse = 0;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400548 }
549 }
550
551 fPrevFlushToken = startTokenForNextFlush;
552}
553
Herb Derby0ef780b2020-01-24 15:57:11 -0500554bool GrDrawOpAtlas::createPages(
555 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500556 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500557
Brian Salomona56a7462020-02-07 14:17:25 -0500558 SkISize dims = {fTextureWidth, fTextureHeight};
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400559
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400560 int numPlotsX = fTextureWidth/fPlotWidth;
561 int numPlotsY = fTextureHeight/fPlotHeight;
562
Robert Phillips4bc70112018-03-01 10:24:02 -0500563 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500564 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Brian Salomonb43d6992021-01-05 14:37:40 -0500565 if (GrColorTypeIsAlphaOnly(fColorType)) {
566 swizzle = GrSwizzle::Concat(swizzle, GrSwizzle("aaaa"));
567 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500568 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400569 fFormat, dims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400570 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
571 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500572 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500573 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400574 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500575 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500576
577 // set up allocated plots
John Stilesfbd050b2020-08-03 13:21:46 -0400578 fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY);
Robert Phillips4bc70112018-03-01 10:24:02 -0500579
580 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
581 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
582 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
583 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500584 currPlot->reset(new Plot(
585 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500586
587 // build LRU list
588 fPages[i].fPlotList.addToHead(currPlot->get());
589 ++currPlot;
590 }
591 }
592
593 }
594
595 return true;
596}
597
Robert Phillips4bc70112018-03-01 10:24:02 -0500598bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500599 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500600
Greg Daniel9715b6c2019-12-10 15:03:10 -0500601 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500602 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400603 }
604
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400605#ifdef DUMP_ATLAS_DATA
606 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500607 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400608 }
609#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500610
611 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400612 return true;
613}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400614
Robert Phillips4bc70112018-03-01 10:24:02 -0500615
616inline void GrDrawOpAtlas::deactivateLastPage() {
617 SkASSERT(fNumActivePages);
618
619 uint32_t lastPageIndex = fNumActivePages - 1;
620
621 int numPlotsX = fTextureWidth/fPlotWidth;
622 int numPlotsY = fTextureHeight/fPlotHeight;
623
Jim Van Verth106b5c42017-09-26 12:45:29 -0400624 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500625 for (int r = 0; r < numPlotsY; ++r) {
626 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500627 uint32_t plotIndex = r * numPlotsX + c;
628
629 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
630 currPlot->resetRects();
631 currPlot->resetFlushesSinceLastUsed();
632
633 // rebuild the LRU list
634 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
635 SkDEBUGCODE(currPlot->fList = nullptr);
636 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
637 }
638 }
639
640 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500641 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500642 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400643}
Herb Derby15d9ef22018-10-18 13:41:32 -0400644
Jim Van Verthf6206f92018-12-14 08:22:24 -0500645GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
646 static const SkISize kARGBDimensions[] = {
647 {256, 256}, // maxBytes < 2^19
648 {512, 256}, // 2^19 <= maxBytes < 2^20
649 {512, 512}, // 2^20 <= maxBytes < 2^21
650 {1024, 512}, // 2^21 <= maxBytes < 2^22
651 {1024, 1024}, // 2^22 <= maxBytes < 2^23
652 {2048, 1024}, // 2^23 <= maxBytes
653 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400654
Jim Van Verthf6206f92018-12-14 08:22:24 -0500655 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
656 maxBytes >>= 18;
657 // Take the floor of the log to get the index
658 int index = maxBytes > 0
659 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
660 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400661
Jim Van Verthf6206f92018-12-14 08:22:24 -0500662 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
663 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500664 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
665 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
666 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400667}
668
669SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500670 if (kA8_GrMaskFormat == type) {
671 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500672 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
673 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500674 } else {
675 return fARGBDimensions;
676 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400677}
678
Jim Van Verthf6206f92018-12-14 08:22:24 -0500679SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
680 if (kA8_GrMaskFormat == type) {
681 SkISize atlasDimensions = this->atlasDimensions(type);
682 // For A8 we want to grow the plots at larger texture sizes to accept more of the
683 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
684 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400685
Jim Van Verth578b0892018-12-20 20:48:55 +0000686 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
687 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500688 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000689 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400690
Jim Van Verthf6206f92018-12-14 08:22:24 -0500691 return { plotWidth, plotHeight };
692 } else {
693 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
694 return { 256, 256 };
695 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400696}
697
Jim Van Verthf6206f92018-12-14 08:22:24 -0500698constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;