blob: 615f7e31ef2795f2870ff2bab73f4af27c3074e3 [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"
Greg Daniel0eca74c2020-10-01 13:46:00 -040013#include "src/gpu/GrBackendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrOnFlushResourceProvider.h"
15#include "src/gpu/GrOpFlushState.h"
16#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrResourceProvider.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040018#include "src/gpu/GrResourceProviderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000020#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070022
Jim Van Verthfb395102020-02-03 10:11:19 -050023#ifdef DUMP_ATLAS_DATA
24static bool gDumpAtlasData = false;
25#endif
26
Robert Phillipse87106d2020-04-09 14:21:33 -040027#ifdef SK_DEBUG
Herb Derby06296c62020-08-28 13:42:31 -040028void GrDrawOpAtlas::validate(const AtlasLocator& atlasLocator) const {
Robert Phillipse87106d2020-04-09 14:21:33 -040029 // Verify that the plotIndex stored in the PlotLocator is consistent with the glyph rectangle
Herb Derby06296c62020-08-28 13:42:31 -040030 int numPlotsX = fTextureWidth / fPlotWidth;
31 int numPlotsY = fTextureHeight / fPlotHeight;
Robert Phillipse87106d2020-04-09 14:21:33 -040032
Herb Derby06296c62020-08-28 13:42:31 -040033 int plotIndex = atlasLocator.plotIndex();
Herb Derbye1a00892020-08-31 15:12:27 -040034 auto topLeft = atlasLocator.topLeft();
35 int plotX = topLeft.x() / fPlotWidth;
36 int plotY = topLeft.y() / fPlotHeight;
Robert Phillipse87106d2020-04-09 14:21:33 -040037 SkASSERT(plotIndex == (numPlotsY - plotY - 1) * numPlotsX + (numPlotsX - plotX - 1));
38}
39#endif
40
Robert Phillipscd5099c2018-02-09 09:56:56 -050041// When proxy allocation is deferred until flush time the proxies acting as atlases require
42// special handling. This is because the usage that can be determined from the ops themselves
43// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
44// atlases. Extending the usage interval of any op that uses an atlas to the start of the
45// flush (as is done for proxies that are used for sw-generated masks) also won't work because
46// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
47// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
48// (which calls this method).
49void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050050 for (uint32_t i = 0; i < fNumActivePages; ++i) {
51 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050052 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050053 }
54}
55
Robert Phillips4bc70112018-03-01 10:24:02 -050056std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050057 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040058 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050059 int height, int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -050060 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -050061 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050062 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040063 if (!format.isValid()) {
64 return nullptr;
65 }
66
Herb Derby0ef780b2020-01-24 15:57:11 -050067 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType,
68 width, height, plotWidth, plotHeight,
69 generationCounter,
Robert Phillips4bc70112018-03-01 10:24:02 -050070 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050071 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040072 return nullptr;
73 }
74
Herb Derbya90ed952020-01-28 15:55:58 -050075 if (evictor != nullptr) {
76 atlas->fEvictionCallbacks.emplace_back(evictor);
77 }
Robert Phillips256c37b2017-03-01 14:32:46 -050078 return atlas;
79}
80
joshualitt5df175e2015-11-18 13:37:54 -080081////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -050082GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
83 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040084 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
85 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040086 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040087 , fPageIndex(pageIndex)
88 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -050089 , fGenerationCounter(generationCounter)
90 , fGenID(fGenerationCounter->next())
Robert Phillipsbf5bf742020-04-13 09:29:08 -040091 , fPlotLocator(fPageIndex, fPlotIndex, fGenID)
Brian Salomon2ee084e2016-12-16 18:59:19 -050092 , fData(nullptr)
93 , fWidth(width)
94 , fHeight(height)
95 , fX(offX)
96 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -050097 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -050098 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -040099 , fColorType(colorType)
100 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800101#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500102 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800103#endif
104{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500105 // We expect the allocated dimensions to be a multiple of 4 bytes
106 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
107 // The padding for faster uploads only works for 1, 2 and 4 byte texels
108 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800109 fDirtyRect.setEmpty();
110}
joshualitt5bf99f12015-03-13 11:47:42 -0700111
Brian Salomon2ee084e2016-12-16 18:59:19 -0500112GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700113 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800114}
joshualitt5bf99f12015-03-13 11:47:42 -0700115
Herb Derby06296c62020-08-28 13:42:31 -0400116bool GrDrawOpAtlas::Plot::addSubImage(
117 int width, int height, const void* image, AtlasLocator* atlasLocator) {
joshualitt5df175e2015-11-18 13:37:54 -0800118 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700119
Robert Phillips6d3bc292020-04-06 10:29:28 -0400120 SkIPoint16 loc;
121 if (!fRectanizer.addRect(width, height, &loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800122 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700123 }
joshualitt5bf99f12015-03-13 11:47:42 -0700124
Herb Derby06296c62020-08-28 13:42:31 -0400125 GrIRect16 rect = GrIRect16::MakeXYWH(loc.fX, loc.fY, width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400126
jvanverthc3d706f2016-04-20 10:33:27 -0700127 if (!fData) {
Herb Derby06296c62020-08-28 13:42:31 -0400128 fData = reinterpret_cast<unsigned char*>(
129 sk_calloc_throw(fBytesPerPixel * fWidth * fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800130 }
131 size_t rowBytes = width * fBytesPerPixel;
132 const unsigned char* imagePtr = (const unsigned char*)image;
133 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700134 unsigned char* dataPtr = fData;
Herb Derby06296c62020-08-28 13:42:31 -0400135 dataPtr += fBytesPerPixel * fWidth * rect.fTop;
136 dataPtr += fBytesPerPixel * rect.fLeft;
Brian Osmancce3e582016-10-14 11:42:20 -0400137 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500138 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400139 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400140 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400141 dataPtr += fBytesPerPixel * fWidth;
142 imagePtr += rowBytes;
143 }
144 } else {
145 for (int i = 0; i < height; ++i) {
146 memcpy(dataPtr, imagePtr, rowBytes);
147 dataPtr += fBytesPerPixel * fWidth;
148 imagePtr += rowBytes;
149 }
joshualitt5bf99f12015-03-13 11:47:42 -0700150 }
151
Herb Derby06296c62020-08-28 13:42:31 -0400152 fDirtyRect.join({rect.fLeft, rect.fTop, rect.fRight, rect.fBottom});
robertphillips2b0536f2015-11-06 14:10:42 -0800153
Herb Derby06296c62020-08-28 13:42:31 -0400154 rect.offset(fOffset.fX, fOffset.fY);
155 atlasLocator->updateRect(rect);
joshualitt5df175e2015-11-18 13:37:54 -0800156 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700157
joshualitt5df175e2015-11-18 13:37:54 -0800158 return true;
159}
joshualitt5bf99f12015-03-13 11:47:42 -0700160
Brian Salomon943ed792017-10-30 09:37:55 -0400161void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400162 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800163 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400164 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400165 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800166 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700167 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500168 // Clamp to 4-byte aligned boundaries
169 unsigned int clearBits = 0x3 / fBytesPerPixel;
170 fDirtyRect.fLeft &= ~clearBits;
171 fDirtyRect.fRight += clearBits;
172 fDirtyRect.fRight &= ~clearBits;
173 SkASSERT(fDirtyRect.fRight <= fWidth);
174 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700175 dataPtr += rowBytes * fDirtyRect.fTop;
176 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400177
Robert Phillipsacaa6072017-07-28 10:54:53 -0400178 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400179 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800180 fDirtyRect.setEmpty();
181 SkDEBUGCODE(fDirty = false;)
182}
183
Brian Salomon2ee084e2016-12-16 18:59:19 -0500184void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500185 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700186
Herb Derby0ef780b2020-01-24 15:57:11 -0500187 fGenID = fGenerationCounter->next();
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400188 fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400189 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
190 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800191
192 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700193 if (fData) {
194 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700195 }
196
joshualitt5df175e2015-11-18 13:37:54 -0800197 fDirtyRect.setEmpty();
198 SkDEBUGCODE(fDirty = false;)
199}
joshualitt5bf99f12015-03-13 11:47:42 -0700200
joshualitt5bf99f12015-03-13 11:47:42 -0700201///////////////////////////////////////////////////////////////////////////////
202
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400203GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
204 GrColorType colorType, int width, int height,
205 int plotWidth, int plotHeight, GenerationCounter* generationCounter,
206 AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500207 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400208 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400209 , fTextureWidth(width)
210 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500211 , fPlotWidth(plotWidth)
212 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500213 , fGenerationCounter(generationCounter)
214 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400215 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400216 , fFlushesSinceLastUse(0)
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500217 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500218 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500219 int numPlotsX = width/plotWidth;
220 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400221 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400222 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
223 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800224
Jim Van Verth06f593c2018-02-20 11:30:10 -0500225 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700226
Herb Derby0ef780b2020-01-24 15:57:11 -0500227 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700228}
229
Herb Derby4d721712020-01-24 14:31:16 -0500230inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400231 for (EvictionCallback* evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500232 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700233 }
Herb Derby1a496c52020-01-22 17:26:56 -0500234
Herb Derby0ef780b2020-01-24 15:57:11 -0500235 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700236}
237
Herb Derby4d721712020-01-24 14:31:16 -0500238inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400239 AtlasLocator* atlasLocator, Plot* plot) {
240 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400241 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700242
243 // If our most recent upload has already occurred then we have to insert a new
244 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
245 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500246 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700247 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500248 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500249
Greg Daniel9715b6c2019-12-10 15:03:10 -0500250 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
251 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500252
Brian Salomon29b60c92017-10-31 14:42:10 -0400253 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400254 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
255 plotsp->uploadToTexture(writePixels, proxy);
256 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500257 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700258 }
Herb Derby06296c62020-08-28 13:42:31 -0400259 atlasLocator->updatePlotLocator(plot->plotLocator());
260 SkDEBUGCODE(this->validate(*atlasLocator);)
Robert Phillips256c37b2017-03-01 14:32:46 -0500261 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700262}
263
Greg Daniel0eca74c2020-10-01 13:46:00 -0400264bool GrDrawOpAtlas::uploadToPage(unsigned int pageIdx, GrDeferredUploadTarget* target, int width,
265 int height, 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 Daniel0eca74c2020-10-01 13:46:00 -0400273 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
274 plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500275
Herb Derby06296c62020-08-28 13:42:31 -0400276 if (plot->addSubImage(width, height, image, atlasLocator)) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400277 return this->updatePlot(target, atlasLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500278 }
279 }
280
281 return false;
282}
283
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400284// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
285//
286// This value is somewhat arbitrary -- the idea is to keep it low enough that
287// a page with unused plots will get removed reasonably quickly, but allow it
288// to hang around for a bit in case it's needed. The assumption is that flushes
289// are rare; i.e., we are not continually refreshing the frame.
Jonathan Backer40c683a2020-05-04 15:00:02 -0400290static constexpr auto kPlotRecentlyUsedCount = 32;
291static constexpr auto kAtlasRecentlyUsedCount = 128;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400292
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500293GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500294 GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400295 int width, int height, const void* image,
296 AtlasLocator* atlasLocator) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700297 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500298 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700299 }
joshualitt5bf99f12015-03-13 11:47:42 -0700300
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400301 // Look through each page to see if we can upload without having to flush
302 // We prioritize this upload to the first pages, not the most recently used, to make it easier
303 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500304 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Greg Daniel0eca74c2020-10-01 13:46:00 -0400305 if (this->uploadToPage(pageIdx, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500306 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400307 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400308 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400309
Jim Van Verth712fe732017-09-25 16:53:49 -0400310 // 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 -0400311 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
312 // We wait until we've grown to the full number of pages to begin evicting already flushed
313 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400314 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500315 if (fNumActivePages == this->maxPages()) {
316 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
317 Plot* plot = fPages[pageIdx].fPlotList.tail();
318 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500319 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500320 this->processEvictionAndResetRects(plot);
Greg Daniel0eca74c2020-10-01 13:46:00 -0400321 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
Greg Daniel9715b6c2019-12-10 15:03:10 -0500322 plot->bpp());
Herb Derby06296c62020-08-28 13:42:31 -0400323 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, atlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500324 SkASSERT(verify);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400325 if (!this->updatePlot(target, atlasLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500326 return ErrorCode::kError;
327 }
328 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400329 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500330 }
331 } else {
332 // If we haven't activated all the available pages, try to create a new one and add to it
333 if (!this->activateNewPage(resourceProvider)) {
334 return ErrorCode::kError;
335 }
336
Greg Daniel0eca74c2020-10-01 13:46:00 -0400337 if (this->uploadToPage(fNumActivePages-1, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500338 return ErrorCode::kSucceeded;
339 } else {
340 // If we fail to upload to a newly activated page then something has gone terribly
341 // wrong - return an error
342 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400343 }
344 }
345
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500346 if (!fNumActivePages) {
347 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700348 }
349
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400350 // Try to find a plot that we can perform an inline upload to.
351 // We prioritize this upload in reverse order of pages to counterbalance the order above.
352 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500353 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400354 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500355 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400356 plot = currentPlot;
357 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500358 }
joshualitt5bf99f12015-03-13 11:47:42 -0700359 }
360
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400361 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
362 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
363 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
364 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500365 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400366 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500367 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700368 }
369
Herb Derby4d721712020-01-24 14:31:16 -0500370 this->processEviction(plot->plotLocator());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400371 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400372 fPages[pageIdx].fPlotList.remove(plot);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400373 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
robertphillips2b0536f2015-11-06 14:10:42 -0800374 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700375
Jim Van Vertha950b632017-09-12 11:54:11 -0400376 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel0eca74c2020-10-01 13:46:00 -0400377 SkASSERT(GrBackendFormatBytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
378 newPlot->bpp());
Herb Derby06296c62020-08-28 13:42:31 -0400379 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, atlasLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700380 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800381
robertphillips1f0e3502015-11-10 10:19:50 -0800382 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400383 // one it displaced most likely was uploaded ASAP.
Robert Phillipse87106d2020-04-09 14:21:33 -0400384 // With c++14 we could move sk_sp into lambda to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500385 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500386
Greg Daniel9715b6c2019-12-10 15:03:10 -0500387 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
388 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700389
Brian Salomon943ed792017-10-30 09:37:55 -0400390 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
391 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
392 plotsp->uploadToTexture(writePixels, proxy);
393 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500394 newPlot->setLastUploadToken(lastUploadToken);
395
Herb Derby06296c62020-08-28 13:42:31 -0400396 atlasLocator->updatePlotLocator(newPlot->plotLocator());
397 SkDEBUGCODE(this->validate(*atlasLocator);)
robertphillips2b0536f2015-11-06 14:10:42 -0800398
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500399 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700400}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400401
Brian Salomon943ed792017-10-30 09:37:55 -0400402void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400403 if (fNumActivePages < 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400404 fPrevFlushToken = startTokenForNextFlush;
405 return;
406 }
407
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400408 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400409 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400410 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500411 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400412 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
413 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400414 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400415 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
416 plot->resetFlushesSinceLastUsed();
417 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400418 }
419
420 plotIter.next();
421 }
422 }
423
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400424 if (atlasUsedThisFlush) {
425 fFlushesSinceLastUse = 0;
426 } else {
427 ++fFlushesSinceLastUse;
428 }
429
430 // We only try to compact if the atlas was used in the recently completed flush or
431 // hasn't been used in a long time.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400432 // This is to handle the case where a lot of text or path rendering has occurred but then just
433 // a blinking cursor is drawn.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400434 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500435 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500436 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400437
438 // For all plots but the last one, update number of flushes since used, and check to see
439 // if there are any in the first pages that the last page can safely upload to.
440 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400441#ifdef DUMP_ATLAS_DATA
442 if (gDumpAtlasData) {
443 SkDebugf("page %d: ", pageIndex);
444 }
445#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400446 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
447 while (Plot* plot = plotIter.get()) {
448 // Update number of flushes since plot was last used
449 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
450 // to avoid deleting everything when we return to text drawing in the blinking
451 // cursor case
452 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
453 plot->incFlushesSinceLastUsed();
454 }
455
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400456#ifdef DUMP_ATLAS_DATA
457 if (gDumpAtlasData) {
458 SkDebugf("%d ", plot->flushesSinceLastUsed());
459 }
460#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400461 // Count plots we can potentially upload to in all pages except the last one
462 // (the potential compactee).
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400463 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500464 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400465 }
466
467 plotIter.next();
468 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400469#ifdef DUMP_ATLAS_DATA
470 if (gDumpAtlasData) {
471 SkDebugf("\n");
472 }
473#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400474 }
475
Jim Van Verth06f593c2018-02-20 11:30:10 -0500476 // Count recently used plots in the last page and evict any that are no longer in use.
477 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400478 // clear out usage of this page unless we have a large need.
479 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500480 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400481#ifdef DUMP_ATLAS_DATA
482 if (gDumpAtlasData) {
483 SkDebugf("page %d: ", lastPageIndex);
484 }
485#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400486 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400487 // Update number of flushes since plot was last used
488 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
489 plot->incFlushesSinceLastUsed();
490 }
491
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400492#ifdef DUMP_ATLAS_DATA
493 if (gDumpAtlasData) {
494 SkDebugf("%d ", plot->flushesSinceLastUsed());
495 }
496#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400497 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400498 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400499 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400500 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400501 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400502 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400503 }
504 plotIter.next();
505 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400506#ifdef DUMP_ATLAS_DATA
507 if (gDumpAtlasData) {
508 SkDebugf("\n");
509 }
510#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500511
512 // If recently used plots in the last page are using less than a quarter of the page, try
513 // to evict them if there's available space in earlier pages. Since we prioritize uploading
514 // to the first pages, this will eventually clear out usage of this page unless we have a
515 // large need.
516 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
517 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
518 while (Plot* plot = plotIter.get()) {
519 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400520 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth06f593c2018-02-20 11:30:10 -0500521 // See if there's room in an earlier page and if so evict.
522 // We need to be somewhat harsh here so that a handful of plots that are
523 // consistently in use don't end up locking the page in memory.
524 if (availablePlots.count() > 0) {
525 this->processEvictionAndResetRects(plot);
526 this->processEvictionAndResetRects(availablePlots.back());
527 availablePlots.pop_back();
528 --usedPlots;
529 }
530 if (!usedPlots || !availablePlots.count()) {
531 break;
532 }
533 }
534 plotIter.next();
535 }
536 }
537
Jim Van Verth106b5c42017-09-26 12:45:29 -0400538 // If none of the plots in the last page have been used recently, delete it.
Jim Van Verth26651882020-03-18 15:30:07 +0000539 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400540#ifdef DUMP_ATLAS_DATA
541 if (gDumpAtlasData) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400542 SkDebugf("delete %d\n", fNumActivePages-1);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400543 }
544#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500545 this->deactivateLastPage();
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400546 fFlushesSinceLastUse = 0;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400547 }
548 }
549
550 fPrevFlushToken = startTokenForNextFlush;
551}
552
Herb Derby0ef780b2020-01-24 15:57:11 -0500553bool GrDrawOpAtlas::createPages(
554 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500555 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500556
Brian Salomona56a7462020-02-07 14:17:25 -0500557 SkISize dims = {fTextureWidth, fTextureHeight};
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400558
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400559 int numPlotsX = fTextureWidth/fPlotWidth;
560 int numPlotsY = fTextureHeight/fPlotHeight;
561
Robert Phillips4bc70112018-03-01 10:24:02 -0500562 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500563 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500564 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400565 fFormat, dims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400566 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
567 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500568 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500569 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400570 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500571 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500572
573 // set up allocated plots
John Stilesfbd050b2020-08-03 13:21:46 -0400574 fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY);
Robert Phillips4bc70112018-03-01 10:24:02 -0500575
576 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
577 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
578 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
579 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500580 currPlot->reset(new Plot(
581 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500582
583 // build LRU list
584 fPages[i].fPlotList.addToHead(currPlot->get());
585 ++currPlot;
586 }
587 }
588
589 }
590
591 return true;
592}
593
594
595bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500596 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500597
Greg Daniel9715b6c2019-12-10 15:03:10 -0500598 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500599 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400600 }
601
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400602#ifdef DUMP_ATLAS_DATA
603 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500604 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400605 }
606#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500607
608 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400609 return true;
610}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400611
Robert Phillips4bc70112018-03-01 10:24:02 -0500612
613inline void GrDrawOpAtlas::deactivateLastPage() {
614 SkASSERT(fNumActivePages);
615
616 uint32_t lastPageIndex = fNumActivePages - 1;
617
618 int numPlotsX = fTextureWidth/fPlotWidth;
619 int numPlotsY = fTextureHeight/fPlotHeight;
620
Jim Van Verth106b5c42017-09-26 12:45:29 -0400621 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500622 for (int r = 0; r < numPlotsY; ++r) {
623 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500624 uint32_t plotIndex = r * numPlotsX + c;
625
626 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
627 currPlot->resetRects();
628 currPlot->resetFlushesSinceLastUsed();
629
630 // rebuild the LRU list
631 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
632 SkDEBUGCODE(currPlot->fList = nullptr);
633 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
634 }
635 }
636
637 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500638 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500639 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400640}
Herb Derby15d9ef22018-10-18 13:41:32 -0400641
Jim Van Verthf6206f92018-12-14 08:22:24 -0500642GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
643 static const SkISize kARGBDimensions[] = {
644 {256, 256}, // maxBytes < 2^19
645 {512, 256}, // 2^19 <= maxBytes < 2^20
646 {512, 512}, // 2^20 <= maxBytes < 2^21
647 {1024, 512}, // 2^21 <= maxBytes < 2^22
648 {1024, 1024}, // 2^22 <= maxBytes < 2^23
649 {2048, 1024}, // 2^23 <= maxBytes
650 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400651
Jim Van Verthf6206f92018-12-14 08:22:24 -0500652 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
653 maxBytes >>= 18;
654 // Take the floor of the log to get the index
655 int index = maxBytes > 0
656 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
657 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400658
Jim Van Verthf6206f92018-12-14 08:22:24 -0500659 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
660 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500661 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
662 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
663 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400664}
665
666SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500667 if (kA8_GrMaskFormat == type) {
668 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500669 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
670 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500671 } else {
672 return fARGBDimensions;
673 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400674}
675
Jim Van Verthf6206f92018-12-14 08:22:24 -0500676SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
677 if (kA8_GrMaskFormat == type) {
678 SkISize atlasDimensions = this->atlasDimensions(type);
679 // For A8 we want to grow the plots at larger texture sizes to accept more of the
680 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
681 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400682
Jim Van Verth578b0892018-12-20 20:48:55 +0000683 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
684 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500685 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000686 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400687
Jim Van Verthf6206f92018-12-14 08:22:24 -0500688 return { plotWidth, plotHeight };
689 } else {
690 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
691 return { 256, 256 };
692 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400693}
694
Jim Van Verthf6206f92018-12-14 08:22:24 -0500695constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;