blob: 253043e8b23d625d1e115f568a390e6ea262befb [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
Brian Salomone2078f12021-05-24 12:40:46 -0400179 writePixels(proxy,
180 fDirtyRect.makeOffset(fOffset.fX, fOffset.fY),
181 fColorType,
182 dataPtr,
183 rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800184 fDirtyRect.setEmpty();
185 SkDEBUGCODE(fDirty = false;)
186}
187
Brian Salomon2ee084e2016-12-16 18:59:19 -0500188void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500189 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700190
Herb Derby0ef780b2020-01-24 15:57:11 -0500191 fGenID = fGenerationCounter->next();
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400192 fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400193 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
194 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800195
196 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700197 if (fData) {
198 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700199 }
200
joshualitt5df175e2015-11-18 13:37:54 -0800201 fDirtyRect.setEmpty();
202 SkDEBUGCODE(fDirty = false;)
203}
joshualitt5bf99f12015-03-13 11:47:42 -0700204
joshualitt5bf99f12015-03-13 11:47:42 -0700205///////////////////////////////////////////////////////////////////////////////
206
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400207GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
208 GrColorType colorType, int width, int height,
209 int plotWidth, int plotHeight, GenerationCounter* generationCounter,
210 AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500211 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400212 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400213 , fTextureWidth(width)
214 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500215 , fPlotWidth(plotWidth)
216 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500217 , fGenerationCounter(generationCounter)
218 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400219 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400220 , fFlushesSinceLastUse(0)
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500221 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500222 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500223 int numPlotsX = width/plotWidth;
224 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400225 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400226 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
227 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800228
Jim Van Verth06f593c2018-02-20 11:30:10 -0500229 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700230
Herb Derby0ef780b2020-01-24 15:57:11 -0500231 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700232}
233
Herb Derby4d721712020-01-24 14:31:16 -0500234inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400235 for (EvictionCallback* evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500236 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700237 }
Herb Derby1a496c52020-01-22 17:26:56 -0500238
Herb Derby0ef780b2020-01-24 15:57:11 -0500239 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700240}
241
Herb Derby4d721712020-01-24 14:31:16 -0500242inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400243 AtlasLocator* atlasLocator, Plot* plot) {
244 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400245 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700246
247 // If our most recent upload has already occurred then we have to insert a new
248 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
249 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500250 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700251 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500252 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500253
Greg Daniel9715b6c2019-12-10 15:03:10 -0500254 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
255 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500256
Brian Salomon29b60c92017-10-31 14:42:10 -0400257 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400258 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
259 plotsp->uploadToTexture(writePixels, proxy);
260 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500261 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700262 }
Herb Derby06296c62020-08-28 13:42:31 -0400263 atlasLocator->updatePlotLocator(plot->plotLocator());
264 SkDEBUGCODE(this->validate(*atlasLocator);)
Robert Phillips256c37b2017-03-01 14:32:46 -0500265 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700266}
267
Greg Daniel0eca74c2020-10-01 13:46:00 -0400268bool GrDrawOpAtlas::uploadToPage(unsigned int pageIdx, GrDeferredUploadTarget* target, int width,
269 int height, const void* image, AtlasLocator* atlasLocator) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500270 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500271
272 // look through all allocated plots for one we can share, in Most Recently Refed order
273 PlotList::Iter plotIter;
274 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
275
276 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel0eca74c2020-10-01 13:46:00 -0400277 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
278 plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500279
Herb Derby06296c62020-08-28 13:42:31 -0400280 if (plot->addSubImage(width, height, image, atlasLocator)) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400281 return this->updatePlot(target, atlasLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500282 }
283 }
284
285 return false;
286}
287
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400288// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
289//
290// This value is somewhat arbitrary -- the idea is to keep it low enough that
291// a page with unused plots will get removed reasonably quickly, but allow it
292// to hang around for a bit in case it's needed. The assumption is that flushes
293// are rare; i.e., we are not continually refreshing the frame.
Jonathan Backer40c683a2020-05-04 15:00:02 -0400294static constexpr auto kPlotRecentlyUsedCount = 32;
295static constexpr auto kAtlasRecentlyUsedCount = 128;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400296
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500297GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500298 GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400299 int width, int height, const void* image,
300 AtlasLocator* atlasLocator) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700301 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500302 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700303 }
joshualitt5bf99f12015-03-13 11:47:42 -0700304
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400305 // Look through each page to see if we can upload without having to flush
306 // We prioritize this upload to the first pages, not the most recently used, to make it easier
307 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500308 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Greg Daniel0eca74c2020-10-01 13:46:00 -0400309 if (this->uploadToPage(pageIdx, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500310 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400311 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400312 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400313
Jim Van Verth712fe732017-09-25 16:53:49 -0400314 // 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 -0400315 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
316 // We wait until we've grown to the full number of pages to begin evicting already flushed
317 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400318 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500319 if (fNumActivePages == this->maxPages()) {
320 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
321 Plot* plot = fPages[pageIdx].fPlotList.tail();
322 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500323 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500324 this->processEvictionAndResetRects(plot);
Greg Daniel0eca74c2020-10-01 13:46:00 -0400325 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
Greg Daniel9715b6c2019-12-10 15:03:10 -0500326 plot->bpp());
Herb Derby06296c62020-08-28 13:42:31 -0400327 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, atlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500328 SkASSERT(verify);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400329 if (!this->updatePlot(target, atlasLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500330 return ErrorCode::kError;
331 }
332 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400333 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500334 }
335 } else {
336 // If we haven't activated all the available pages, try to create a new one and add to it
337 if (!this->activateNewPage(resourceProvider)) {
338 return ErrorCode::kError;
339 }
340
Greg Daniel0eca74c2020-10-01 13:46:00 -0400341 if (this->uploadToPage(fNumActivePages-1, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500342 return ErrorCode::kSucceeded;
343 } else {
344 // If we fail to upload to a newly activated page then something has gone terribly
345 // wrong - return an error
346 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400347 }
348 }
349
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500350 if (!fNumActivePages) {
351 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700352 }
353
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400354 // Try to find a plot that we can perform an inline upload to.
355 // We prioritize this upload in reverse order of pages to counterbalance the order above.
356 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500357 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400358 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500359 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400360 plot = currentPlot;
361 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500362 }
joshualitt5bf99f12015-03-13 11:47:42 -0700363 }
364
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400365 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
366 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
367 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
368 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500369 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400370 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500371 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700372 }
373
Herb Derby4d721712020-01-24 14:31:16 -0500374 this->processEviction(plot->plotLocator());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400375 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400376 fPages[pageIdx].fPlotList.remove(plot);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400377 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
robertphillips2b0536f2015-11-06 14:10:42 -0800378 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700379
Jim Van Vertha950b632017-09-12 11:54:11 -0400380 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel0eca74c2020-10-01 13:46:00 -0400381 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
382 newPlot->bpp());
Herb Derby06296c62020-08-28 13:42:31 -0400383 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, atlasLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700384 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800385
robertphillips1f0e3502015-11-10 10:19:50 -0800386 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400387 // one it displaced most likely was uploaded ASAP.
Robert Phillipse87106d2020-04-09 14:21:33 -0400388 // With c++14 we could move sk_sp into lambda to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500389 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500390
Greg Daniel9715b6c2019-12-10 15:03:10 -0500391 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
392 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700393
Brian Salomon943ed792017-10-30 09:37:55 -0400394 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
395 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
396 plotsp->uploadToTexture(writePixels, proxy);
397 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500398 newPlot->setLastUploadToken(lastUploadToken);
399
Herb Derby06296c62020-08-28 13:42:31 -0400400 atlasLocator->updatePlotLocator(newPlot->plotLocator());
401 SkDEBUGCODE(this->validate(*atlasLocator);)
robertphillips2b0536f2015-11-06 14:10:42 -0800402
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500403 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700404}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400405
Brian Salomon943ed792017-10-30 09:37:55 -0400406void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400407 if (fNumActivePages < 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400408 fPrevFlushToken = startTokenForNextFlush;
409 return;
410 }
411
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400412 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400413 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400414 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500415 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400416 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
417 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400418 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400419 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
420 plot->resetFlushesSinceLastUsed();
421 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400422 }
423
424 plotIter.next();
425 }
426 }
427
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400428 if (atlasUsedThisFlush) {
429 fFlushesSinceLastUse = 0;
430 } else {
431 ++fFlushesSinceLastUse;
432 }
433
434 // We only try to compact if the atlas was used in the recently completed flush or
435 // hasn't been used in a long time.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400436 // This is to handle the case where a lot of text or path rendering has occurred but then just
437 // a blinking cursor is drawn.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400438 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500439 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500440 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400441
442 // For all plots but the last one, update number of flushes since used, and check to see
443 // if there are any in the first pages that the last page can safely upload to.
444 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400445#ifdef DUMP_ATLAS_DATA
446 if (gDumpAtlasData) {
447 SkDebugf("page %d: ", pageIndex);
448 }
449#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400450 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
451 while (Plot* plot = plotIter.get()) {
452 // Update number of flushes since plot was last used
453 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
454 // to avoid deleting everything when we return to text drawing in the blinking
455 // cursor case
456 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
457 plot->incFlushesSinceLastUsed();
458 }
459
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400460#ifdef DUMP_ATLAS_DATA
461 if (gDumpAtlasData) {
462 SkDebugf("%d ", plot->flushesSinceLastUsed());
463 }
464#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400465 // Count plots we can potentially upload to in all pages except the last one
466 // (the potential compactee).
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400467 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500468 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400469 }
470
471 plotIter.next();
472 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400473#ifdef DUMP_ATLAS_DATA
474 if (gDumpAtlasData) {
475 SkDebugf("\n");
476 }
477#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400478 }
479
Jim Van Verth06f593c2018-02-20 11:30:10 -0500480 // Count recently used plots in the last page and evict any that are no longer in use.
481 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400482 // clear out usage of this page unless we have a large need.
483 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500484 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400485#ifdef DUMP_ATLAS_DATA
486 if (gDumpAtlasData) {
487 SkDebugf("page %d: ", lastPageIndex);
488 }
489#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400490 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400491 // Update number of flushes since plot was last used
492 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
493 plot->incFlushesSinceLastUsed();
494 }
495
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400496#ifdef DUMP_ATLAS_DATA
497 if (gDumpAtlasData) {
498 SkDebugf("%d ", plot->flushesSinceLastUsed());
499 }
500#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400501 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400502 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400503 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400504 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400505 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400506 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400507 }
508 plotIter.next();
509 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400510#ifdef DUMP_ATLAS_DATA
511 if (gDumpAtlasData) {
512 SkDebugf("\n");
513 }
514#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500515
516 // If recently used plots in the last page are using less than a quarter of the page, try
517 // to evict them if there's available space in earlier pages. Since we prioritize uploading
518 // to the first pages, this will eventually clear out usage of this page unless we have a
519 // large need.
520 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
521 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
522 while (Plot* plot = plotIter.get()) {
523 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400524 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth06f593c2018-02-20 11:30:10 -0500525 // See if there's room in an earlier page and if so evict.
526 // We need to be somewhat harsh here so that a handful of plots that are
527 // consistently in use don't end up locking the page in memory.
528 if (availablePlots.count() > 0) {
529 this->processEvictionAndResetRects(plot);
530 this->processEvictionAndResetRects(availablePlots.back());
531 availablePlots.pop_back();
532 --usedPlots;
533 }
534 if (!usedPlots || !availablePlots.count()) {
535 break;
536 }
537 }
538 plotIter.next();
539 }
540 }
541
Jim Van Verth106b5c42017-09-26 12:45:29 -0400542 // If none of the plots in the last page have been used recently, delete it.
Jim Van Verth26651882020-03-18 15:30:07 +0000543 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400544#ifdef DUMP_ATLAS_DATA
545 if (gDumpAtlasData) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400546 SkDebugf("delete %d\n", fNumActivePages-1);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400547 }
548#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500549 this->deactivateLastPage();
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400550 fFlushesSinceLastUse = 0;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400551 }
552 }
553
554 fPrevFlushToken = startTokenForNextFlush;
555}
556
Herb Derby0ef780b2020-01-24 15:57:11 -0500557bool GrDrawOpAtlas::createPages(
558 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500559 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500560
Brian Salomona56a7462020-02-07 14:17:25 -0500561 SkISize dims = {fTextureWidth, fTextureHeight};
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400562
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400563 int numPlotsX = fTextureWidth/fPlotWidth;
564 int numPlotsY = fTextureHeight/fPlotHeight;
565
Robert Phillips4bc70112018-03-01 10:24:02 -0500566 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500567 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Brian Salomonb43d6992021-01-05 14:37:40 -0500568 if (GrColorTypeIsAlphaOnly(fColorType)) {
569 swizzle = GrSwizzle::Concat(swizzle, GrSwizzle("aaaa"));
570 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500571 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400572 fFormat, dims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400573 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
574 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500575 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500576 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400577 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500578 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500579
580 // set up allocated plots
John Stilesfbd050b2020-08-03 13:21:46 -0400581 fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY);
Robert Phillips4bc70112018-03-01 10:24:02 -0500582
583 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
584 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
585 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
586 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500587 currPlot->reset(new Plot(
588 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500589
590 // build LRU list
591 fPages[i].fPlotList.addToHead(currPlot->get());
592 ++currPlot;
593 }
594 }
595
596 }
597
598 return true;
599}
600
Robert Phillips4bc70112018-03-01 10:24:02 -0500601bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500602 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500603
Greg Daniel9715b6c2019-12-10 15:03:10 -0500604 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500605 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400606 }
607
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400608#ifdef DUMP_ATLAS_DATA
609 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500610 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400611 }
612#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500613
614 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400615 return true;
616}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400617
Robert Phillips4bc70112018-03-01 10:24:02 -0500618
619inline void GrDrawOpAtlas::deactivateLastPage() {
620 SkASSERT(fNumActivePages);
621
622 uint32_t lastPageIndex = fNumActivePages - 1;
623
624 int numPlotsX = fTextureWidth/fPlotWidth;
625 int numPlotsY = fTextureHeight/fPlotHeight;
626
Jim Van Verth106b5c42017-09-26 12:45:29 -0400627 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500628 for (int r = 0; r < numPlotsY; ++r) {
629 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500630 uint32_t plotIndex = r * numPlotsX + c;
631
632 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
633 currPlot->resetRects();
634 currPlot->resetFlushesSinceLastUsed();
635
636 // rebuild the LRU list
637 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
638 SkDEBUGCODE(currPlot->fList = nullptr);
639 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
640 }
641 }
642
643 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500644 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500645 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400646}
Herb Derby15d9ef22018-10-18 13:41:32 -0400647
Jim Van Verthf6206f92018-12-14 08:22:24 -0500648GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
649 static const SkISize kARGBDimensions[] = {
650 {256, 256}, // maxBytes < 2^19
651 {512, 256}, // 2^19 <= maxBytes < 2^20
652 {512, 512}, // 2^20 <= maxBytes < 2^21
653 {1024, 512}, // 2^21 <= maxBytes < 2^22
654 {1024, 1024}, // 2^22 <= maxBytes < 2^23
655 {2048, 1024}, // 2^23 <= maxBytes
656 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400657
Jim Van Verthf6206f92018-12-14 08:22:24 -0500658 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
659 maxBytes >>= 18;
660 // Take the floor of the log to get the index
661 int index = maxBytes > 0
662 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
663 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400664
Jim Van Verthf6206f92018-12-14 08:22:24 -0500665 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
666 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500667 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
668 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
669 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400670}
671
672SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500673 if (kA8_GrMaskFormat == type) {
674 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500675 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
676 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500677 } else {
678 return fARGBDimensions;
679 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400680}
681
Jim Van Verthf6206f92018-12-14 08:22:24 -0500682SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
683 if (kA8_GrMaskFormat == type) {
684 SkISize atlasDimensions = this->atlasDimensions(type);
685 // For A8 we want to grow the plots at larger texture sizes to accept more of the
686 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
687 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400688
Jim Van Verth578b0892018-12-20 20:48:55 +0000689 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
690 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500691 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000692 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400693
Jim Van Verthf6206f92018-12-14 08:22:24 -0500694 return { plotWidth, plotHeight };
695 } else {
696 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
697 return { 256, 256 };
698 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400699}
700
Jim Van Verthf6206f92018-12-14 08:22:24 -0500701constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;