blob: b5141371e21a66af3729da8e72ada13db167ecab [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();
33 auto rect = atlasLocator.rect();
34 int plotX = rect.fLeft / fPlotWidth;
35 int plotY = rect.fTop / 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
Jim Van Verthfb395102020-02-03 10:11:19 -050080std::tuple<uint16_t, uint16_t, int> GrDrawOpAtlas::UnpackIndexFromTexCoords(uint16_t u,
81 uint16_t v) {
82 int pageIndex = 0;
83 if (u & 0x1) {
84 pageIndex |= 0x2;
85 }
86 if (v & 0x1) {
87 pageIndex |= 0x1;
88 }
89 return std::make_tuple(u >> 1, v >> 1, pageIndex);
90}
Robert Phillips256c37b2017-03-01 14:32:46 -050091
joshualitt5df175e2015-11-18 13:37:54 -080092////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -050093GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
94 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040095 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
96 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040097 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040098 , fPageIndex(pageIndex)
99 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -0500100 , fGenerationCounter(generationCounter)
101 , fGenID(fGenerationCounter->next())
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400102 , fPlotLocator(fPageIndex, fPlotIndex, fGenID)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500103 , fData(nullptr)
104 , fWidth(width)
105 , fHeight(height)
106 , fX(offX)
107 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -0500108 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500109 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400110 , fColorType(colorType)
111 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800112#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500113 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800114#endif
115{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500116 // We expect the allocated dimensions to be a multiple of 4 bytes
117 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
118 // The padding for faster uploads only works for 1, 2 and 4 byte texels
119 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800120 fDirtyRect.setEmpty();
121}
joshualitt5bf99f12015-03-13 11:47:42 -0700122
Brian Salomon2ee084e2016-12-16 18:59:19 -0500123GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700124 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800125}
joshualitt5bf99f12015-03-13 11:47:42 -0700126
Herb Derby06296c62020-08-28 13:42:31 -0400127bool GrDrawOpAtlas::Plot::addSubImage(
128 int width, int height, const void* image, AtlasLocator* atlasLocator) {
joshualitt5df175e2015-11-18 13:37:54 -0800129 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700130
Robert Phillips6d3bc292020-04-06 10:29:28 -0400131 SkIPoint16 loc;
132 if (!fRectanizer.addRect(width, height, &loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800133 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700134 }
joshualitt5bf99f12015-03-13 11:47:42 -0700135
Herb Derby06296c62020-08-28 13:42:31 -0400136 GrIRect16 rect = GrIRect16::MakeXYWH(loc.fX, loc.fY, width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400137
jvanverthc3d706f2016-04-20 10:33:27 -0700138 if (!fData) {
Herb Derby06296c62020-08-28 13:42:31 -0400139 fData = reinterpret_cast<unsigned char*>(
140 sk_calloc_throw(fBytesPerPixel * fWidth * fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800141 }
142 size_t rowBytes = width * fBytesPerPixel;
143 const unsigned char* imagePtr = (const unsigned char*)image;
144 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700145 unsigned char* dataPtr = fData;
Herb Derby06296c62020-08-28 13:42:31 -0400146 dataPtr += fBytesPerPixel * fWidth * rect.fTop;
147 dataPtr += fBytesPerPixel * rect.fLeft;
Brian Osmancce3e582016-10-14 11:42:20 -0400148 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500149 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400150 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400151 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400152 dataPtr += fBytesPerPixel * fWidth;
153 imagePtr += rowBytes;
154 }
155 } else {
156 for (int i = 0; i < height; ++i) {
157 memcpy(dataPtr, imagePtr, rowBytes);
158 dataPtr += fBytesPerPixel * fWidth;
159 imagePtr += rowBytes;
160 }
joshualitt5bf99f12015-03-13 11:47:42 -0700161 }
162
Herb Derby06296c62020-08-28 13:42:31 -0400163 fDirtyRect.join({rect.fLeft, rect.fTop, rect.fRight, rect.fBottom});
robertphillips2b0536f2015-11-06 14:10:42 -0800164
Herb Derby06296c62020-08-28 13:42:31 -0400165 rect.offset(fOffset.fX, fOffset.fY);
166 atlasLocator->updateRect(rect);
joshualitt5df175e2015-11-18 13:37:54 -0800167 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700168
joshualitt5df175e2015-11-18 13:37:54 -0800169 return true;
170}
joshualitt5bf99f12015-03-13 11:47:42 -0700171
Brian Salomon943ed792017-10-30 09:37:55 -0400172void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400173 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800174 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400175 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400176 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800177 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700178 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500179 // Clamp to 4-byte aligned boundaries
180 unsigned int clearBits = 0x3 / fBytesPerPixel;
181 fDirtyRect.fLeft &= ~clearBits;
182 fDirtyRect.fRight += clearBits;
183 fDirtyRect.fRight &= ~clearBits;
184 SkASSERT(fDirtyRect.fRight <= fWidth);
185 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700186 dataPtr += rowBytes * fDirtyRect.fTop;
187 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400188
Robert Phillipsacaa6072017-07-28 10:54:53 -0400189 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400190 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800191 fDirtyRect.setEmpty();
192 SkDEBUGCODE(fDirty = false;)
193}
194
Brian Salomon2ee084e2016-12-16 18:59:19 -0500195void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500196 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700197
Herb Derby0ef780b2020-01-24 15:57:11 -0500198 fGenID = fGenerationCounter->next();
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400199 fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400200 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
201 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800202
203 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700204 if (fData) {
205 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700206 }
207
joshualitt5df175e2015-11-18 13:37:54 -0800208 fDirtyRect.setEmpty();
209 SkDEBUGCODE(fDirty = false;)
210}
joshualitt5bf99f12015-03-13 11:47:42 -0700211
joshualitt5bf99f12015-03-13 11:47:42 -0700212///////////////////////////////////////////////////////////////////////////////
213
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400214GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
215 GrColorType colorType, int width, int height,
216 int plotWidth, int plotHeight, GenerationCounter* generationCounter,
217 AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500218 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400219 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400220 , fTextureWidth(width)
221 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500222 , fPlotWidth(plotWidth)
223 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500224 , fGenerationCounter(generationCounter)
225 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400226 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400227 , fFlushesSinceLastUse(0)
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500228 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500229 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500230 int numPlotsX = width/plotWidth;
231 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400232 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400233 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
234 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800235
Jim Van Verth06f593c2018-02-20 11:30:10 -0500236 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700237
Herb Derby0ef780b2020-01-24 15:57:11 -0500238 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700239}
240
Herb Derby4d721712020-01-24 14:31:16 -0500241inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400242 for (EvictionCallback* evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500243 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700244 }
Herb Derby1a496c52020-01-22 17:26:56 -0500245
Herb Derby0ef780b2020-01-24 15:57:11 -0500246 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700247}
248
Herb Derby4d721712020-01-24 14:31:16 -0500249inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400250 AtlasLocator* atlasLocator, Plot* plot) {
251 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400252 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700253
254 // If our most recent upload has already occurred then we have to insert a new
255 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
256 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500257 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700258 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500259 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500260
Greg Daniel9715b6c2019-12-10 15:03:10 -0500261 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
262 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500263
Brian Salomon29b60c92017-10-31 14:42:10 -0400264 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400265 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
266 plotsp->uploadToTexture(writePixels, proxy);
267 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500268 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700269 }
Herb Derby06296c62020-08-28 13:42:31 -0400270 atlasLocator->updatePlotLocator(plot->plotLocator());
271 SkDEBUGCODE(this->validate(*atlasLocator);)
Robert Phillips256c37b2017-03-01 14:32:46 -0500272 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700273}
274
Robert Phillips6d3bc292020-04-06 10:29:28 -0400275bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400276 GrDeferredUploadTarget* target, int width, int height,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400277 const void* image, AtlasLocator* atlasLocator) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500278 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500279
280 // look through all allocated plots for one we can share, in Most Recently Refed order
281 PlotList::Iter plotIter;
282 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
283
284 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500285 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500286
Herb Derby06296c62020-08-28 13:42:31 -0400287 if (plot->addSubImage(width, height, image, atlasLocator)) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400288 return this->updatePlot(target, atlasLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500289 }
290 }
291
292 return false;
293}
294
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400295// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
296//
297// This value is somewhat arbitrary -- the idea is to keep it low enough that
298// a page with unused plots will get removed reasonably quickly, but allow it
299// to hang around for a bit in case it's needed. The assumption is that flushes
300// are rare; i.e., we are not continually refreshing the frame.
Jonathan Backer40c683a2020-05-04 15:00:02 -0400301static constexpr auto kPlotRecentlyUsedCount = 32;
302static constexpr auto kAtlasRecentlyUsedCount = 128;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400303
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500304GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500305 GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400306 int width, int height, const void* image,
307 AtlasLocator* atlasLocator) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700308 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500309 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700310 }
joshualitt5bf99f12015-03-13 11:47:42 -0700311
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400312 const GrCaps& caps = *resourceProvider->caps();
313
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400314 // Look through each page to see if we can upload without having to flush
315 // We prioritize this upload to the first pages, not the most recently used, to make it easier
316 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500317 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400318 if (this->uploadToPage(caps, pageIdx, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500319 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400320 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400321 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400322
Jim Van Verth712fe732017-09-25 16:53:49 -0400323 // 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 -0400324 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
325 // We wait until we've grown to the full number of pages to begin evicting already flushed
326 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400327 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500328 if (fNumActivePages == this->maxPages()) {
329 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
330 Plot* plot = fPages[pageIdx].fPlotList.tail();
331 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500332 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500333 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500334 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
335 plot->bpp());
Herb Derby06296c62020-08-28 13:42:31 -0400336 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, atlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500337 SkASSERT(verify);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400338 if (!this->updatePlot(target, atlasLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500339 return ErrorCode::kError;
340 }
341 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400342 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500343 }
344 } else {
345 // If we haven't activated all the available pages, try to create a new one and add to it
346 if (!this->activateNewPage(resourceProvider)) {
347 return ErrorCode::kError;
348 }
349
Robert Phillips6d3bc292020-04-06 10:29:28 -0400350 if (this->uploadToPage(caps, fNumActivePages-1, target, width, height, image,
351 atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500352 return ErrorCode::kSucceeded;
353 } else {
354 // If we fail to upload to a newly activated page then something has gone terribly
355 // wrong - return an error
356 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400357 }
358 }
359
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500360 if (!fNumActivePages) {
361 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700362 }
363
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400364 // Try to find a plot that we can perform an inline upload to.
365 // We prioritize this upload in reverse order of pages to counterbalance the order above.
366 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500367 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400368 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500369 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400370 plot = currentPlot;
371 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500372 }
joshualitt5bf99f12015-03-13 11:47:42 -0700373 }
374
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400375 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
376 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
377 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
378 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500379 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400380 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500381 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700382 }
383
Herb Derby4d721712020-01-24 14:31:16 -0500384 this->processEviction(plot->plotLocator());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400385 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400386 fPages[pageIdx].fPlotList.remove(plot);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400387 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
robertphillips2b0536f2015-11-06 14:10:42 -0800388 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700389
Jim Van Vertha950b632017-09-12 11:54:11 -0400390 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500391 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
Herb Derby06296c62020-08-28 13:42:31 -0400392 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, atlasLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700393 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800394
robertphillips1f0e3502015-11-10 10:19:50 -0800395 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400396 // one it displaced most likely was uploaded ASAP.
Robert Phillipse87106d2020-04-09 14:21:33 -0400397 // With c++14 we could move sk_sp into lambda to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500398 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500399
Greg Daniel9715b6c2019-12-10 15:03:10 -0500400 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
401 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700402
Brian Salomon943ed792017-10-30 09:37:55 -0400403 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
404 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
405 plotsp->uploadToTexture(writePixels, proxy);
406 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500407 newPlot->setLastUploadToken(lastUploadToken);
408
Herb Derby06296c62020-08-28 13:42:31 -0400409 atlasLocator->updatePlotLocator(newPlot->plotLocator());
410 SkDEBUGCODE(this->validate(*atlasLocator);)
robertphillips2b0536f2015-11-06 14:10:42 -0800411
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500412 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700413}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400414
Brian Salomon943ed792017-10-30 09:37:55 -0400415void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400416 if (fNumActivePages < 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400417 fPrevFlushToken = startTokenForNextFlush;
418 return;
419 }
420
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400421 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400422 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400423 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500424 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400425 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
426 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400427 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400428 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
429 plot->resetFlushesSinceLastUsed();
430 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400431 }
432
433 plotIter.next();
434 }
435 }
436
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400437 if (atlasUsedThisFlush) {
438 fFlushesSinceLastUse = 0;
439 } else {
440 ++fFlushesSinceLastUse;
441 }
442
443 // We only try to compact if the atlas was used in the recently completed flush or
444 // hasn't been used in a long time.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400445 // This is to handle the case where a lot of text or path rendering has occurred but then just
446 // a blinking cursor is drawn.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400447 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500448 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500449 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400450
451 // For all plots but the last one, update number of flushes since used, and check to see
452 // if there are any in the first pages that the last page can safely upload to.
453 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400454#ifdef DUMP_ATLAS_DATA
455 if (gDumpAtlasData) {
456 SkDebugf("page %d: ", pageIndex);
457 }
458#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400459 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
460 while (Plot* plot = plotIter.get()) {
461 // Update number of flushes since plot was last used
462 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
463 // to avoid deleting everything when we return to text drawing in the blinking
464 // cursor case
465 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
466 plot->incFlushesSinceLastUsed();
467 }
468
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400469#ifdef DUMP_ATLAS_DATA
470 if (gDumpAtlasData) {
471 SkDebugf("%d ", plot->flushesSinceLastUsed());
472 }
473#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400474 // Count plots we can potentially upload to in all pages except the last one
475 // (the potential compactee).
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400476 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500477 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400478 }
479
480 plotIter.next();
481 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400482#ifdef DUMP_ATLAS_DATA
483 if (gDumpAtlasData) {
484 SkDebugf("\n");
485 }
486#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400487 }
488
Jim Van Verth06f593c2018-02-20 11:30:10 -0500489 // Count recently used plots in the last page and evict any that are no longer in use.
490 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400491 // clear out usage of this page unless we have a large need.
492 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500493 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400494#ifdef DUMP_ATLAS_DATA
495 if (gDumpAtlasData) {
496 SkDebugf("page %d: ", lastPageIndex);
497 }
498#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400499 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400500 // Update number of flushes since plot was last used
501 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
502 plot->incFlushesSinceLastUsed();
503 }
504
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400505#ifdef DUMP_ATLAS_DATA
506 if (gDumpAtlasData) {
507 SkDebugf("%d ", plot->flushesSinceLastUsed());
508 }
509#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400510 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400511 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400512 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400513 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400514 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400515 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400516 }
517 plotIter.next();
518 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400519#ifdef DUMP_ATLAS_DATA
520 if (gDumpAtlasData) {
521 SkDebugf("\n");
522 }
523#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500524
525 // If recently used plots in the last page are using less than a quarter of the page, try
526 // to evict them if there's available space in earlier pages. Since we prioritize uploading
527 // to the first pages, this will eventually clear out usage of this page unless we have a
528 // large need.
529 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
530 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
531 while (Plot* plot = plotIter.get()) {
532 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400533 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth06f593c2018-02-20 11:30:10 -0500534 // See if there's room in an earlier page and if so evict.
535 // We need to be somewhat harsh here so that a handful of plots that are
536 // consistently in use don't end up locking the page in memory.
537 if (availablePlots.count() > 0) {
538 this->processEvictionAndResetRects(plot);
539 this->processEvictionAndResetRects(availablePlots.back());
540 availablePlots.pop_back();
541 --usedPlots;
542 }
543 if (!usedPlots || !availablePlots.count()) {
544 break;
545 }
546 }
547 plotIter.next();
548 }
549 }
550
Jim Van Verth106b5c42017-09-26 12:45:29 -0400551 // If none of the plots in the last page have been used recently, delete it.
Jim Van Verth26651882020-03-18 15:30:07 +0000552 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400553#ifdef DUMP_ATLAS_DATA
554 if (gDumpAtlasData) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400555 SkDebugf("delete %d\n", fNumActivePages-1);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400556 }
557#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500558 this->deactivateLastPage();
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400559 fFlushesSinceLastUse = 0;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400560 }
561 }
562
563 fPrevFlushToken = startTokenForNextFlush;
564}
565
Herb Derby0ef780b2020-01-24 15:57:11 -0500566bool GrDrawOpAtlas::createPages(
567 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500568 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500569
Brian Salomona56a7462020-02-07 14:17:25 -0500570 SkISize dims = {fTextureWidth, fTextureHeight};
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400571
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400572 int numPlotsX = fTextureWidth/fPlotWidth;
573 int numPlotsY = fTextureHeight/fPlotHeight;
574
Robert Phillips4bc70112018-03-01 10:24:02 -0500575 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500576 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500577 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400578 fFormat, dims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400579 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
580 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500581 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500582 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400583 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500584 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500585
586 // set up allocated plots
John Stilesfbd050b2020-08-03 13:21:46 -0400587 fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY);
Robert Phillips4bc70112018-03-01 10:24:02 -0500588
589 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
590 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
591 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
592 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500593 currPlot->reset(new Plot(
594 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500595
596 // build LRU list
597 fPages[i].fPlotList.addToHead(currPlot->get());
598 ++currPlot;
599 }
600 }
601
602 }
603
604 return true;
605}
606
607
608bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500609 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500610
Greg Daniel9715b6c2019-12-10 15:03:10 -0500611 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500612 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400613 }
614
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400615#ifdef DUMP_ATLAS_DATA
616 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500617 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400618 }
619#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500620
621 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400622 return true;
623}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400624
Robert Phillips4bc70112018-03-01 10:24:02 -0500625
626inline void GrDrawOpAtlas::deactivateLastPage() {
627 SkASSERT(fNumActivePages);
628
629 uint32_t lastPageIndex = fNumActivePages - 1;
630
631 int numPlotsX = fTextureWidth/fPlotWidth;
632 int numPlotsY = fTextureHeight/fPlotHeight;
633
Jim Van Verth106b5c42017-09-26 12:45:29 -0400634 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500635 for (int r = 0; r < numPlotsY; ++r) {
636 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500637 uint32_t plotIndex = r * numPlotsX + c;
638
639 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
640 currPlot->resetRects();
641 currPlot->resetFlushesSinceLastUsed();
642
643 // rebuild the LRU list
644 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
645 SkDEBUGCODE(currPlot->fList = nullptr);
646 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
647 }
648 }
649
650 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500651 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500652 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400653}
Herb Derby15d9ef22018-10-18 13:41:32 -0400654
Jim Van Verthf6206f92018-12-14 08:22:24 -0500655GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
656 static const SkISize kARGBDimensions[] = {
657 {256, 256}, // maxBytes < 2^19
658 {512, 256}, // 2^19 <= maxBytes < 2^20
659 {512, 512}, // 2^20 <= maxBytes < 2^21
660 {1024, 512}, // 2^21 <= maxBytes < 2^22
661 {1024, 1024}, // 2^22 <= maxBytes < 2^23
662 {2048, 1024}, // 2^23 <= maxBytes
663 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400664
Jim Van Verthf6206f92018-12-14 08:22:24 -0500665 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
666 maxBytes >>= 18;
667 // Take the floor of the log to get the index
668 int index = maxBytes > 0
669 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
670 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400671
Jim Van Verthf6206f92018-12-14 08:22:24 -0500672 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
673 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500674 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
675 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
676 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400677}
678
679SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500680 if (kA8_GrMaskFormat == type) {
681 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500682 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
683 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500684 } else {
685 return fARGBDimensions;
686 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400687}
688
Jim Van Verthf6206f92018-12-14 08:22:24 -0500689SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
690 if (kA8_GrMaskFormat == type) {
691 SkISize atlasDimensions = this->atlasDimensions(type);
692 // For A8 we want to grow the plots at larger texture sizes to accept more of the
693 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
694 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400695
Jim Van Verth578b0892018-12-20 20:48:55 +0000696 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
697 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500698 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000699 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400700
Jim Van Verthf6206f92018-12-14 08:22:24 -0500701 return { plotWidth, plotHeight };
702 } else {
703 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
704 return { 256, 256 };
705 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400706}
707
Jim Van Verthf6206f92018-12-14 08:22:24 -0500708constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;