blob: d55d7ea7a696504b4dc29df7609f5264f70e9e9b [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
Robert Phillips03e4c952019-11-26 16:20:22 -050012#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrOnFlushResourceProvider.h"
14#include "src/gpu/GrOpFlushState.h"
15#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrResourceProvider.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040017#include "src/gpu/GrResourceProviderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000019#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070021
Jim Van Verthfb395102020-02-03 10:11:19 -050022#ifdef DUMP_ATLAS_DATA
23static bool gDumpAtlasData = false;
24#endif
25
Robert Phillipse87106d2020-04-09 14:21:33 -040026#ifdef SK_DEBUG
Herb Derby06296c62020-08-28 13:42:31 -040027void GrDrawOpAtlas::validate(const AtlasLocator& atlasLocator) const {
Robert Phillipse87106d2020-04-09 14:21:33 -040028 // Verify that the plotIndex stored in the PlotLocator is consistent with the glyph rectangle
Herb Derby06296c62020-08-28 13:42:31 -040029 int numPlotsX = fTextureWidth / fPlotWidth;
30 int numPlotsY = fTextureHeight / fPlotHeight;
Robert Phillipse87106d2020-04-09 14:21:33 -040031
Herb Derby06296c62020-08-28 13:42:31 -040032 int plotIndex = atlasLocator.plotIndex();
Herb Derbye1a00892020-08-31 15:12:27 -040033 auto topLeft = atlasLocator.topLeft();
34 int plotX = topLeft.x() / fPlotWidth;
35 int plotY = topLeft.y() / fPlotHeight;
Robert Phillipse87106d2020-04-09 14:21:33 -040036 SkASSERT(plotIndex == (numPlotsY - plotY - 1) * numPlotsX + (numPlotsX - plotX - 1));
37}
38#endif
39
Robert Phillipscd5099c2018-02-09 09:56:56 -050040// When proxy allocation is deferred until flush time the proxies acting as atlases require
41// special handling. This is because the usage that can be determined from the ops themselves
42// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
43// atlases. Extending the usage interval of any op that uses an atlas to the start of the
44// flush (as is done for proxies that are used for sw-generated masks) also won't work because
45// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
46// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
47// (which calls this method).
48void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050049 for (uint32_t i = 0; i < fNumActivePages; ++i) {
50 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050051 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050052 }
53}
54
Robert Phillips4bc70112018-03-01 10:24:02 -050055std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050056 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040057 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050058 int height, int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -050059 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -050060 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050061 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040062 if (!format.isValid()) {
63 return nullptr;
64 }
65
Herb Derby0ef780b2020-01-24 15:57:11 -050066 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType,
67 width, height, plotWidth, plotHeight,
68 generationCounter,
Robert Phillips4bc70112018-03-01 10:24:02 -050069 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050070 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040071 return nullptr;
72 }
73
Herb Derbya90ed952020-01-28 15:55:58 -050074 if (evictor != nullptr) {
75 atlas->fEvictionCallbacks.emplace_back(evictor);
76 }
Robert Phillips256c37b2017-03-01 14:32:46 -050077 return atlas;
78}
79
joshualitt5df175e2015-11-18 13:37:54 -080080////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -050081GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
82 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040083 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
84 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040085 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040086 , fPageIndex(pageIndex)
87 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -050088 , fGenerationCounter(generationCounter)
89 , fGenID(fGenerationCounter->next())
Robert Phillipsbf5bf742020-04-13 09:29:08 -040090 , fPlotLocator(fPageIndex, fPlotIndex, fGenID)
Brian Salomon2ee084e2016-12-16 18:59:19 -050091 , fData(nullptr)
92 , fWidth(width)
93 , fHeight(height)
94 , fX(offX)
95 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -050096 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -050097 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -040098 , fColorType(colorType)
99 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800100#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500101 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800102#endif
103{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500104 // We expect the allocated dimensions to be a multiple of 4 bytes
105 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
106 // The padding for faster uploads only works for 1, 2 and 4 byte texels
107 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800108 fDirtyRect.setEmpty();
109}
joshualitt5bf99f12015-03-13 11:47:42 -0700110
Brian Salomon2ee084e2016-12-16 18:59:19 -0500111GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700112 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800113}
joshualitt5bf99f12015-03-13 11:47:42 -0700114
Herb Derby06296c62020-08-28 13:42:31 -0400115bool GrDrawOpAtlas::Plot::addSubImage(
116 int width, int height, const void* image, AtlasLocator* atlasLocator) {
joshualitt5df175e2015-11-18 13:37:54 -0800117 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700118
Robert Phillips6d3bc292020-04-06 10:29:28 -0400119 SkIPoint16 loc;
120 if (!fRectanizer.addRect(width, height, &loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800121 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700122 }
joshualitt5bf99f12015-03-13 11:47:42 -0700123
Herb Derby06296c62020-08-28 13:42:31 -0400124 GrIRect16 rect = GrIRect16::MakeXYWH(loc.fX, loc.fY, width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400125
jvanverthc3d706f2016-04-20 10:33:27 -0700126 if (!fData) {
Herb Derby06296c62020-08-28 13:42:31 -0400127 fData = reinterpret_cast<unsigned char*>(
128 sk_calloc_throw(fBytesPerPixel * fWidth * fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800129 }
130 size_t rowBytes = width * fBytesPerPixel;
131 const unsigned char* imagePtr = (const unsigned char*)image;
132 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700133 unsigned char* dataPtr = fData;
Herb Derby06296c62020-08-28 13:42:31 -0400134 dataPtr += fBytesPerPixel * fWidth * rect.fTop;
135 dataPtr += fBytesPerPixel * rect.fLeft;
Brian Osmancce3e582016-10-14 11:42:20 -0400136 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500137 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400138 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400139 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400140 dataPtr += fBytesPerPixel * fWidth;
141 imagePtr += rowBytes;
142 }
143 } else {
144 for (int i = 0; i < height; ++i) {
145 memcpy(dataPtr, imagePtr, rowBytes);
146 dataPtr += fBytesPerPixel * fWidth;
147 imagePtr += rowBytes;
148 }
joshualitt5bf99f12015-03-13 11:47:42 -0700149 }
150
Herb Derby06296c62020-08-28 13:42:31 -0400151 fDirtyRect.join({rect.fLeft, rect.fTop, rect.fRight, rect.fBottom});
robertphillips2b0536f2015-11-06 14:10:42 -0800152
Herb Derby06296c62020-08-28 13:42:31 -0400153 rect.offset(fOffset.fX, fOffset.fY);
154 atlasLocator->updateRect(rect);
joshualitt5df175e2015-11-18 13:37:54 -0800155 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700156
joshualitt5df175e2015-11-18 13:37:54 -0800157 return true;
158}
joshualitt5bf99f12015-03-13 11:47:42 -0700159
Brian Salomon943ed792017-10-30 09:37:55 -0400160void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400161 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800162 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400163 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400164 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800165 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700166 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500167 // Clamp to 4-byte aligned boundaries
168 unsigned int clearBits = 0x3 / fBytesPerPixel;
169 fDirtyRect.fLeft &= ~clearBits;
170 fDirtyRect.fRight += clearBits;
171 fDirtyRect.fRight &= ~clearBits;
172 SkASSERT(fDirtyRect.fRight <= fWidth);
173 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700174 dataPtr += rowBytes * fDirtyRect.fTop;
175 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400176
Robert Phillipsacaa6072017-07-28 10:54:53 -0400177 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400178 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800179 fDirtyRect.setEmpty();
180 SkDEBUGCODE(fDirty = false;)
181}
182
Brian Salomon2ee084e2016-12-16 18:59:19 -0500183void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500184 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700185
Herb Derby0ef780b2020-01-24 15:57:11 -0500186 fGenID = fGenerationCounter->next();
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400187 fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400188 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
189 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800190
191 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700192 if (fData) {
193 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700194 }
195
joshualitt5df175e2015-11-18 13:37:54 -0800196 fDirtyRect.setEmpty();
197 SkDEBUGCODE(fDirty = false;)
198}
joshualitt5bf99f12015-03-13 11:47:42 -0700199
joshualitt5bf99f12015-03-13 11:47:42 -0700200///////////////////////////////////////////////////////////////////////////////
201
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400202GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
203 GrColorType colorType, int width, int height,
204 int plotWidth, int plotHeight, GenerationCounter* generationCounter,
205 AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500206 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400207 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400208 , fTextureWidth(width)
209 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500210 , fPlotWidth(plotWidth)
211 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500212 , fGenerationCounter(generationCounter)
213 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400214 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400215 , fFlushesSinceLastUse(0)
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500216 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500217 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500218 int numPlotsX = width/plotWidth;
219 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400220 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400221 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
222 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800223
Jim Van Verth06f593c2018-02-20 11:30:10 -0500224 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700225
Herb Derby0ef780b2020-01-24 15:57:11 -0500226 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700227}
228
Herb Derby4d721712020-01-24 14:31:16 -0500229inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400230 for (EvictionCallback* evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500231 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700232 }
Herb Derby1a496c52020-01-22 17:26:56 -0500233
Herb Derby0ef780b2020-01-24 15:57:11 -0500234 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700235}
236
Herb Derby4d721712020-01-24 14:31:16 -0500237inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400238 AtlasLocator* atlasLocator, Plot* plot) {
239 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400240 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700241
242 // If our most recent upload has already occurred then we have to insert a new
243 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
244 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500245 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700246 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500247 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500248
Greg Daniel9715b6c2019-12-10 15:03:10 -0500249 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
250 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500251
Brian Salomon29b60c92017-10-31 14:42:10 -0400252 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400253 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
254 plotsp->uploadToTexture(writePixels, proxy);
255 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500256 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700257 }
Herb Derby06296c62020-08-28 13:42:31 -0400258 atlasLocator->updatePlotLocator(plot->plotLocator());
259 SkDEBUGCODE(this->validate(*atlasLocator);)
Robert Phillips256c37b2017-03-01 14:32:46 -0500260 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700261}
262
Robert Phillips6d3bc292020-04-06 10:29:28 -0400263bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400264 GrDeferredUploadTarget* target, int width, int height,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400265 const void* image, AtlasLocator* atlasLocator) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500266 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500267
268 // look through all allocated plots for one we can share, in Most Recently Refed order
269 PlotList::Iter plotIter;
270 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
271
272 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500273 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500274
Herb Derby06296c62020-08-28 13:42:31 -0400275 if (plot->addSubImage(width, height, image, atlasLocator)) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400276 return this->updatePlot(target, atlasLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500277 }
278 }
279
280 return false;
281}
282
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400283// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
284//
285// This value is somewhat arbitrary -- the idea is to keep it low enough that
286// a page with unused plots will get removed reasonably quickly, but allow it
287// to hang around for a bit in case it's needed. The assumption is that flushes
288// are rare; i.e., we are not continually refreshing the frame.
Jonathan Backer40c683a2020-05-04 15:00:02 -0400289static constexpr auto kPlotRecentlyUsedCount = 32;
290static constexpr auto kAtlasRecentlyUsedCount = 128;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400291
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500292GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500293 GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400294 int width, int height, const void* image,
295 AtlasLocator* atlasLocator) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700296 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500297 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700298 }
joshualitt5bf99f12015-03-13 11:47:42 -0700299
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400300 const GrCaps& caps = *resourceProvider->caps();
301
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) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400306 if (this->uploadToPage(caps, 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 Daniel9715b6c2019-12-10 15:03:10 -0500322 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
323 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
Robert Phillips6d3bc292020-04-06 10:29:28 -0400338 if (this->uploadToPage(caps, fNumActivePages-1, target, width, height, image,
339 atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500340 return ErrorCode::kSucceeded;
341 } else {
342 // If we fail to upload to a newly activated page then something has gone terribly
343 // wrong - return an error
344 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400345 }
346 }
347
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500348 if (!fNumActivePages) {
349 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700350 }
351
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400352 // Try to find a plot that we can perform an inline upload to.
353 // We prioritize this upload in reverse order of pages to counterbalance the order above.
354 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500355 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400356 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500357 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400358 plot = currentPlot;
359 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500360 }
joshualitt5bf99f12015-03-13 11:47:42 -0700361 }
362
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400363 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
364 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
365 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
366 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500367 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400368 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500369 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700370 }
371
Herb Derby4d721712020-01-24 14:31:16 -0500372 this->processEviction(plot->plotLocator());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400373 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400374 fPages[pageIdx].fPlotList.remove(plot);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400375 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
robertphillips2b0536f2015-11-06 14:10:42 -0800376 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700377
Jim Van Vertha950b632017-09-12 11:54:11 -0400378 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500379 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == 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);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500565 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400566 fFormat, dims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400567 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
568 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500569 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500570 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400571 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500572 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500573
574 // set up allocated plots
John Stilesfbd050b2020-08-03 13:21:46 -0400575 fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY);
Robert Phillips4bc70112018-03-01 10:24:02 -0500576
577 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
578 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
579 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
580 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500581 currPlot->reset(new Plot(
582 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500583
584 // build LRU list
585 fPages[i].fPlotList.addToHead(currPlot->get());
586 ++currPlot;
587 }
588 }
589
590 }
591
592 return true;
593}
594
595
596bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500597 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500598
Greg Daniel9715b6c2019-12-10 15:03:10 -0500599 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500600 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400601 }
602
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400603#ifdef DUMP_ATLAS_DATA
604 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500605 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400606 }
607#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500608
609 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400610 return true;
611}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400612
Robert Phillips4bc70112018-03-01 10:24:02 -0500613
614inline void GrDrawOpAtlas::deactivateLastPage() {
615 SkASSERT(fNumActivePages);
616
617 uint32_t lastPageIndex = fNumActivePages - 1;
618
619 int numPlotsX = fTextureWidth/fPlotWidth;
620 int numPlotsY = fTextureHeight/fPlotHeight;
621
Jim Van Verth106b5c42017-09-26 12:45:29 -0400622 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500623 for (int r = 0; r < numPlotsY; ++r) {
624 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500625 uint32_t plotIndex = r * numPlotsX + c;
626
627 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
628 currPlot->resetRects();
629 currPlot->resetFlushesSinceLastUsed();
630
631 // rebuild the LRU list
632 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
633 SkDEBUGCODE(currPlot->fList = nullptr);
634 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
635 }
636 }
637
638 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500639 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500640 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400641}
Herb Derby15d9ef22018-10-18 13:41:32 -0400642
Jim Van Verthf6206f92018-12-14 08:22:24 -0500643GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
644 static const SkISize kARGBDimensions[] = {
645 {256, 256}, // maxBytes < 2^19
646 {512, 256}, // 2^19 <= maxBytes < 2^20
647 {512, 512}, // 2^20 <= maxBytes < 2^21
648 {1024, 512}, // 2^21 <= maxBytes < 2^22
649 {1024, 1024}, // 2^22 <= maxBytes < 2^23
650 {2048, 1024}, // 2^23 <= maxBytes
651 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400652
Jim Van Verthf6206f92018-12-14 08:22:24 -0500653 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
654 maxBytes >>= 18;
655 // Take the floor of the log to get the index
656 int index = maxBytes > 0
657 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
658 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400659
Jim Van Verthf6206f92018-12-14 08:22:24 -0500660 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
661 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500662 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
663 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
664 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400665}
666
667SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500668 if (kA8_GrMaskFormat == type) {
669 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500670 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
671 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500672 } else {
673 return fARGBDimensions;
674 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400675}
676
Jim Van Verthf6206f92018-12-14 08:22:24 -0500677SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
678 if (kA8_GrMaskFormat == type) {
679 SkISize atlasDimensions = this->atlasDimensions(type);
680 // For A8 we want to grow the plots at larger texture sizes to accept more of the
681 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
682 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400683
Jim Van Verth578b0892018-12-20 20:48:55 +0000684 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
685 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500686 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000687 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400688
Jim Van Verthf6206f92018-12-14 08:22:24 -0500689 return { plotWidth, plotHeight };
690 } else {
691 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
692 return { 256, 256 };
693 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400694}
695
Jim Van Verthf6206f92018-12-14 08:22:24 -0500696constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;